diff options
Diffstat (limited to 'server/test')
-rw-r--r-- | server/test/guest_test.go | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/server/test/guest_test.go b/server/test/guest_test.go index f6cc243..26bd143 100644 --- a/server/test/guest_test.go +++ b/server/test/guest_test.go @@ -78,9 +78,8 @@ func logInGuest(guestHandler *guest.GuestHandler, test *testing.T) guest.LoginRe func addPartyGuest(guestHandler *guest.GuestHandler, token string, test *testing.T) { response := httptest.NewRecorder() - guestWithParty := getGuestWithParty() putRequest, err := http.NewRequest(http.MethodPut, "http://localhost:8080/guests/1", - strings.NewReader(guestWithParty)) + strings.NewReader(getGuestWithParty())) if err != nil { test.Error(err) } @@ -107,7 +106,10 @@ func logInAdmin(adminHandler *admin.AdminHandler, test *testing.T) admin.LoginRe func getGuests(guestHandler *guest.GuestHandler, token string, test *testing.T) []guest.Guest { response := httptest.NewRecorder() - getRequest, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/guests/", nil) + getRequest, err := http.NewRequest(http.MethodGet, "http://localhost:8080/guests/", nil) + if err != nil { + test.Error(err) + } getRequest.Header.Set("Authorization", token) guestHandler.ServeHTTP(response, getRequest) assertEquals(test, response.Result().StatusCode, 200) @@ -164,3 +166,55 @@ func getGuestWithoutParty() string { return `{"id":1,"firstName":"Michael","lastName":"Hunteman","attendance":"", "email":"","message":"","partySize":1,"partyList":[]}` } + +func TestAddGuest(test *testing.T) { + databasePool, err := pgxpool.New(context.Background(), + fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, password, host, port, database)) + if err != nil { + test.Error(err) + } + defer databasePool.Close() + guestStore := guest.NewStore(databasePool) + guestHandler := guest.NewGuestHandler(guestStore) + adminStore := admin.NewStore(databasePool) + adminHandler := admin.NewAdminHandler(adminStore, guestStore) + + adminLogin := logInAdmin(adminHandler, test) + adminToken := adminLogin.Token + guests := getGuests(guestHandler, adminToken, test) + assertEquals(test, findGuest(guests), findGuest([]guest.Guest{})) + postGuest(guestHandler, adminToken, test) + + guests = getGuests(guestHandler, adminToken, test) + guest := findGuest(guests) + assertEquals(test, guest.Attendance, "accept") + assertEquals(test, guest.Email, "mhunteman@cox.net") + assertEquals(test, guest.Message, "We'll be there!") + assertEquals(test, guest.PartySize, 2) + assertEquals(test, guest.PartyList[0].FirstName, "Madison") + assertEquals(test, guest.PartyList[0].LastName, "Rossitto") + deleteGuest(guestHandler, adminToken, test) +} + +func deleteGuest(guestHandler *guest.GuestHandler, token string, test *testing.T) { + response := httptest.NewRecorder() + deleteRequest, err := http.NewRequest(http.MethodDelete, "http://localhost:8080/guests/1", nil) + if err != nil { + test.Error(err) + } + deleteRequest.Header.Set("Authorization", token) + guestHandler.ServeHTTP(response, deleteRequest) + assertEquals(test, response.Result().StatusCode, 200) +} + +func postGuest(guestHandler *guest.GuestHandler, token string, test *testing.T) { + response := httptest.NewRecorder() + putRequest, err := http.NewRequest(http.MethodPost, "http://localhost:8080/guests/", + strings.NewReader(getGuestWithParty())) + if err != nil { + test.Error(err) + } + putRequest.Header.Set("Authorization", token) + guestHandler.ServeHTTP(response, putRequest) + assertEquals(test, response.Result().StatusCode, 200) +} |