diff options
Diffstat (limited to 'server/test')
-rw-r--r-- | server/test/guest_test.go | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/server/test/guest_test.go b/server/test/guest_test.go new file mode 100644 index 0000000..c675ddb --- /dev/null +++ b/server/test/guest_test.go @@ -0,0 +1,129 @@ +package test + +import ( + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" + + "git.huntm.net/wedding/server/guest" + "github.com/jackc/pgx/v5/pgxpool" +) + +var ( + user = os.Getenv("USER") + pass = os.Getenv("PASS") + host = "localhost" + port = "5432" + database = "postgres" +) + +func TestUpdateRSVP(test *testing.T) { + db, err := pgxpool.New(context.Background(), fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, pass, host, port, database)) + if err != nil { + test.Error(err) + } + defer db.Close() + store := guest.NewMemStore(db) + guestHandler := guest.NewGuestHandler(store) + + login := logIn(guestHandler, test) + token := login.Token + + addPartyGuest(guestHandler, token, test) + guestSlice := getGuests(guestHandler, token, test) + guest := guestSlice[0] + assertEquals(test, guest.Attendance, "accept") + assertEquals(test, guest.Email, "mhunteman@yahoo.com") + 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") + + deletePartyGuest(guestHandler, token, test) + guestSlice = getGuests(guestHandler, token, test) + guest = guestSlice[0] + assertEquals(test, guest.Attendance, "decline") + assertEquals(test, guest.Email, "") + assertEquals(test, guest.Message, "") + assertEquals(test, guest.PartySize, 1) +} + +func logIn(guestHandler *guest.GuestHandler, test *testing.T) guest.LoginResponse { + response := httptest.NewRecorder() + loginRequest, err := http.NewRequest(http.MethodPost, "http://localhost:8080/guest/login", + strings.NewReader(getCredentials())) + if err != nil { + test.Error(err) + } + guestHandler.ServeHTTP(response, loginRequest) + assertEquals(test, response.Result().StatusCode, 200) + var login guest.LoginResponse + if err = json.NewDecoder(response.Body).Decode(&login); err != nil { + log.Fatal(err) + } + return login +} + +func addPartyGuest(guestHandler *guest.GuestHandler, token string, test *testing.T) { + response := httptest.NewRecorder() + guestWithParty := getGuestWithParty() + putRequest, err := http.NewRequest(http.MethodPut, "http://localhost:8080/guest/1", strings.NewReader(guestWithParty)) + if err != nil { + test.Error(err) + } + putRequest.Header.Set("Authorization", token) + guestHandler.ServeHTTP(response, putRequest) + assertEquals(test, response.Result().StatusCode, 200) +} + +func getGuests(guestHandler *guest.GuestHandler, token string, test *testing.T) []guest.Guest { + response := httptest.NewRecorder() + getRequest, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/guest/", nil) + getRequest.Header.Set("Authorization", token) + guestHandler.ServeHTTP(response, getRequest) + assertEquals(test, response.Result().StatusCode, 200) + var guestSlice []guest.Guest + if err := json.NewDecoder(response.Body).Decode(&guestSlice); err != nil { + test.Error(err) + } + return guestSlice +} + +func deletePartyGuest(guestHandler *guest.GuestHandler, token string, test *testing.T) { + response := httptest.NewRecorder() + putRequest, err := http.NewRequest(http.MethodPut, "http://localhost:8080/guest/1", strings.NewReader(getGuestWithoutParty())) + if err != nil { + test.Error(err) + } + putRequest.Header.Set("Authorization", token) + guestHandler.ServeHTTP(response, putRequest) + assertEquals(test, response.Result().StatusCode, 200) +} + +func assertEquals(test testing.TB, actual any, expected any) { + test.Helper() + if actual != expected { + test.Errorf("Actual: %s, expected: %s", actual, expected) + } +} + +func getCredentials() string { + return "{ \"firstName\": \"Michael\", \"lastName\": \"Hunteman\"}" +} + +func getGuestWithParty() string { + return `{"id":1,"firstName":"Michael","lastName":"Hunteman", "attendance":"accept", + "email":"mhunteman@yahoo.com","message":"We'll be there!","partySize":2, + "partyList":[{"firstName":"Madison","lastName":"Rossitto"}]}` +} + +func getGuestWithoutParty() string { + return `{"id":1,"firstName":"Michael","lastName":"Hunteman","attendance":"decline", + "email":"","message":"","partySize":1,"partyList":[]}` +} |