diff --git a/backend/tests/api/category_tag_test.go b/backend/tests/api/category_tag_test.go index 6f466b62a..ab941f843 100644 --- a/backend/tests/api/category_tag_test.go +++ b/backend/tests/api/category_tag_test.go @@ -50,11 +50,12 @@ func TestGetCategoryTagsWorks(t *testing.T) { body := SampleTagFactory(categoryUUID) (*body)["id"] = tagID - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags", categoryUUID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags", categoryUUID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -76,11 +77,14 @@ func TestGetCategoryTagsFailsCategoryBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags", badRequest), - Role: &models.Super, - }.TestOnError(t, &appAssert, errors.FailedToValidateID) + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags", badRequest), + Role: &models.Super, + }, + errors.FailedToValidateID, + ) } appAssert.Close() @@ -91,28 +95,31 @@ func TestGetCategoryTagsFailsCategoryNotFound(t *testing.T) { uuid := uuid.New() - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags", uuid), - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, h.ErrorWithTester{ - Error: errors.CategoryNotFound, - Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { - var category models.Category - err := app.Conn.Where("id = ?", uuid).First(&category).Error - assert.Assert(err != nil) + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags", uuid), + Role: &models.Super, + }, h.ErrorWithTester{ + Error: errors.CategoryNotFound, + Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { + var category models.Category + err := app.Conn.Where("id = ?", uuid).First(&category).Error + assert.Assert(err != nil) + }, }, - }).Close() + ).Close() } func TestGetCategoryTagWorks(t *testing.T) { existingAppAssert, categoryUUID, tagUUID := CreateSampleTag(t) - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", categoryUUID, tagUUID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", categoryUUID, tagUUID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -134,11 +141,13 @@ func TestGetCategoryTagFailsCategoryBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", badRequest, tagUUID), - Role: &models.Super, - }.TestOnError(t, &appAssert, errors.FailedToValidateID) + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", badRequest, tagUUID), + Role: &models.Super, + }, errors.FailedToValidateID, + ) } appAssert.Close() @@ -156,11 +165,13 @@ func TestGetCategoryTagFailsTagBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", categoryUUID, badRequest), - Role: &models.Super, - }.TestOnError(t, &appAssert, errors.FailedToValidateID) + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", categoryUUID, badRequest), + Role: &models.Super, + }, + errors.FailedToValidateID) } appAssert.Close() @@ -169,19 +180,25 @@ func TestGetCategoryTagFailsTagBadRequest(t *testing.T) { func TestGetCategoryTagFailsCategoryNotFound(t *testing.T) { appAssert, _, tagUUID := CreateSampleTag(t) - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", uuid.New(), tagUUID), - Role: &models.Super, - }.TestOnError(t, &appAssert, errors.TagNotFound).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", uuid.New(), tagUUID), + Role: &models.Super, + }, + errors.TagNotFound, + ).Close() } func TestGetCategoryTagFailsTagNotFound(t *testing.T) { appAssert, categoryUUID := CreateSampleCategory(t, nil) - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", categoryUUID, uuid.New()), - Role: &models.Super, - }.TestOnError(t, &appAssert, errors.TagNotFound).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s/tags/%s", categoryUUID, uuid.New()), + Role: &models.Super, + }, + errors.TagNotFound, + ).Close() } diff --git a/backend/tests/api/category_test.go b/backend/tests/api/category_test.go index 5db838466..bc722260b 100644 --- a/backend/tests/api/category_test.go +++ b/backend/tests/api/category_test.go @@ -74,12 +74,18 @@ func AssertSampleCategoryBodyRespDB(app h.TestApp, assert *assert.A, resp *http. func CreateSampleCategory(t *testing.T, existingAppAssert *h.ExistingAppAssert) (h.ExistingAppAssert, uuid.UUID) { var sampleCategoryUUID uuid.UUID - newAppAssert := h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/categories/", - Body: SampleCategoryFactory(), - Role: &models.Super, - }.TestOnStatusAndDB(t, existingAppAssert, + if existingAppAssert == nil { + newAppAssert := h.InitTest(t) + existingAppAssert = &newAppAssert + } + + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/categories/", + Body: SampleCategoryFactory(), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -88,11 +94,7 @@ func CreateSampleCategory(t *testing.T, existingAppAssert *h.ExistingAppAssert) }, ) - if existingAppAssert == nil { - return newAppAssert, sampleCategoryUUID - } else { - return *existingAppAssert, sampleCategoryUUID - } + return *existingAppAssert, sampleCategoryUUID } func TestCreateCategoryWorks(t *testing.T) { @@ -101,15 +103,16 @@ func TestCreateCategoryWorks(t *testing.T) { } func TestCreateCategoryIgnoresid(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/categories/", - Body: &map[string]interface{}{ - "id": 12, - "name": "Foo", + h.InitTest(t).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/categories/", + Body: &map[string]interface{}{ + "id": 12, + "name": "Foo", + }, + Role: &models.Super, }, - Role: &models.Super, - }.TestOnStatusAndDB(t, nil, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -138,14 +141,15 @@ func AssertNumCategoriesRemainsAtN(app h.TestApp, assert *assert.A, resp *http.R } func TestCreateCategoryFailsIfNameIsNotString(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/categories/", - Body: &map[string]interface{}{ - "name": 1231, + h.InitTest(t).TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/categories/", + Body: &map[string]interface{}{ + "name": 1231, + }, + Role: &models.Super, }, - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, h.ErrorWithTester{ Error: errors.FailedToParseRequestBody, Tester: AssertNoCategories, @@ -154,12 +158,13 @@ func TestCreateCategoryFailsIfNameIsNotString(t *testing.T) { } func TestCreateCategoryFailsIfNameIsMissing(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/categories/", - Body: &map[string]interface{}{}, - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + h.InitTest(t).TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/categories/", + Body: &map[string]interface{}{}, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateCategory, Tester: AssertNoCategories, @@ -178,12 +183,13 @@ func TestCreateCategoryFailsIfCategoryWithThatNameAlreadyExists(t *testing.T) { modifiedSampleCategoryBody := *SampleCategoryFactory() modifiedSampleCategoryBody["name"] = permutation - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/categories/", - Body: &modifiedSampleCategoryBody, - Role: &models.Super, - }.TestOnErrorAndDB(t, &existingAppAssert, + existingAppAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/categories/", + Body: &modifiedSampleCategoryBody, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.CategoryAlreadyExists, Tester: TestNumCategoriesRemainsAt1, @@ -197,11 +203,12 @@ func TestCreateCategoryFailsIfCategoryWithThatNameAlreadyExists(t *testing.T) { func TestGetCategoryWorks(t *testing.T) { existingAppAssert, uuid := CreateSampleCategory(t, nil) - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s", uuid), - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s", uuid), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -212,6 +219,8 @@ func TestGetCategoryWorks(t *testing.T) { } func TestGetCategoryFailsBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -221,30 +230,38 @@ func TestGetCategoryFailsBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s", badRequest), - Role: &models.Super, - }.TestOnError(t, nil, errors.FailedToValidateID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s", badRequest), + Role: &models.Super, + }, + errors.FailedToValidateID, + ) } + + appAssert.Close() } func TestGetCategoryFailsNotFound(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/categories/%s", uuid.New()), - Role: &models.Super, - }.TestOnError(t, nil, errors.CategoryNotFound).Close() + h.InitTest(t).TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/categories/%s", uuid.New()), + Role: &models.Super, + }, errors.CategoryNotFound, + ).Close() } func TestGetCategoriesWorks(t *testing.T) { existingAppAssert, _ := CreateSampleCategory(t, nil) - h.TestRequest{ - Method: fiber.MethodGet, - Path: "/api/v1/categories/", - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: "/api/v1/categories/", + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -309,12 +326,13 @@ func TestUpdateCategoryWorks(t *testing.T) { AssertUpdatedCategoryBodyRespDB(app, assert, resp, &category) } - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/categories/%s", uuid), - Body: &category, - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/categories/%s", uuid), + Body: &category, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: AssertUpdatedCategoryBodyRespDB, @@ -332,12 +350,13 @@ func TestUpdateCategoryWorksWithSameDetails(t *testing.T) { AssertUpdatedCategoryBodyRespDB(app, assert, resp, &category) } - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/categories/%s", uuid), - Body: &category, - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/categories/%s", uuid), + Body: &category, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: AssertSampleCategoryBodyRespDB, @@ -346,6 +365,8 @@ func TestUpdateCategoryWorksWithSameDetails(t *testing.T) { } func TestUpdateCategoryFailsBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -355,23 +376,29 @@ func TestUpdateCategoryFailsBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/categories/%s", badRequest), - Body: SampleCategoryFactory(), - Role: &models.Super, - }.TestOnError(t, nil, errors.FailedToValidateID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/categories/%s", badRequest), + Body: SampleCategoryFactory(), + Role: &models.Super, + }, + errors.FailedToValidateID, + ) } + + appAssert.Close() } func TestDeleteCategoryWorks(t *testing.T) { existingAppAssert, uuid := CreateSampleCategory(t, nil) - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/categories/%s", uuid), - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/categories/%s", uuid), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusNoContent, Tester: AssertNoCategories, @@ -391,11 +418,12 @@ func TestDeleteCategoryFailsBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/categories/%s", badRequest), - Role: &models.Super, - }.TestOnErrorAndDB(t, &existingAppAssert, + existingAppAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/categories/%s", badRequest), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateID, Tester: Assert1Category, @@ -409,11 +437,12 @@ func TestDeleteCategoryFailsBadRequest(t *testing.T) { func TestDeleteCategoryFailsNotFound(t *testing.T) { existingAppAssert, _ := CreateSampleCategory(t, nil) - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/categories/%s", uuid.New()), - Role: &models.Super, - }.TestOnErrorAndDB(t, &existingAppAssert, + existingAppAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/categories/%s", uuid.New()), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.CategoryNotFound, Tester: Assert1Category, diff --git a/backend/tests/api/club_test.go b/backend/tests/api/club_test.go index cb93dea86..fb5851c92 100644 --- a/backend/tests/api/club_test.go +++ b/backend/tests/api/club_test.go @@ -139,12 +139,13 @@ func CreateSampleClub(t *testing.T, existingAppAssert *h.ExistingAppAssert) (eaa var sampleClubUUID uuid.UUID - newAppAssert := h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/clubs/", - Body: SampleClubFactory(&userID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/clubs/", + Body: SampleClubFactory(&userID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -154,7 +155,7 @@ func CreateSampleClub(t *testing.T, existingAppAssert *h.ExistingAppAssert) (eaa ) if existingAppAssert == nil { - return newAppAssert, userID, sampleClubUUID + return appAssert, userID, sampleClubUUID } else { return *existingAppAssert, userID, sampleClubUUID } @@ -166,11 +167,11 @@ func TestCreateClubWorks(t *testing.T) { } func TestGetClubsWorks(t *testing.T) { - h.TestRequest{ + h.InitTest(t).TestOnStatusAndDB(h.TestRequest{ Method: fiber.MethodGet, Path: "/api/v1/clubs/", Role: &models.Super, - }.TestOnStatusAndDB(t, nil, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -240,12 +241,13 @@ func AssertCreateBadClubDataFails(t *testing.T, jsonKey string, badValues []inte sampleClubPermutation := *SampleClubFactory(&uuid) sampleClubPermutation[jsonKey] = badValue - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/clubs/", - Body: &sampleClubPermutation, - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/clubs/", + Body: &sampleClubPermutation, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateClub, Tester: TestNumClubsRemainsAt1, @@ -319,12 +321,13 @@ func TestUpdateClubWorks(t *testing.T) { (*updatedClub)["name"] = "Updated Name" (*updatedClub)["preview"] = "Updated Preview" - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/clubs/%s", clubUUID), - Body: updatedClub, - Role: &models.Super, - }.TestOnStatusAndDB(t, &appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/clubs/%s", clubUUID), + Body: updatedClub, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -347,12 +350,13 @@ func TestUpdateClubFailsOnInvalidBody(t *testing.T) { {"application_link": "Not an URL"}, {"logo": "@12394X_2"}, } { - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/clubs/%s", clubUUID), - Body: &invalidData, - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/clubs/%s", clubUUID), + Body: &invalidData, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateClub, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -392,6 +396,8 @@ func TestUpdateClubFailsOnInvalidBody(t *testing.T) { } func TestUpdateClubFailsBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -403,26 +409,31 @@ func TestUpdateClubFailsBadRequest(t *testing.T) { sampleStudent, rawPassword := h.SampleStudentFactory() for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/clubs/%s", badRequest), - Body: h.SampleStudentJSONFactory(sampleStudent, rawPassword), - Role: &models.Super, - }.TestOnError(t, nil, errors.FailedToParseUUID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/clubs/%s", badRequest), + Body: h.SampleStudentJSONFactory(sampleStudent, rawPassword), + Role: &models.Super, + }, + errors.FailedToParseUUID, + ) } + + appAssert.Close() } // TODO: should this be unauthorized or not found? func TestUpdateClubFailsOnClubIdNotExist(t *testing.T) { uuid := uuid.New() - h.TestRequest{ + h.InitTest(t).TestOnErrorAndDB(h.TestRequest{ Method: fiber.MethodPatch, Path: fmt.Sprintf("/api/v1/clubs/%s", uuid), Body: SampleClubFactory(nil), Role: &models.Student, TestUserIDRequired: h.BoolToPointer(true), - }.TestOnErrorAndDB(t, nil, + }, h.ErrorWithTester{ Error: errors.ClubNotFound, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -440,11 +451,12 @@ func TestUpdateClubFailsOnClubIdNotExist(t *testing.T) { func TestDeleteClubWorks(t *testing.T) { appAssert, _, clubUUID := CreateSampleClub(t, nil) - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/clubs/%s", clubUUID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/clubs/%s", clubUUID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusNoContent, Tester: TestNumClubsRemainsAt1, @@ -455,11 +467,12 @@ func TestDeleteClubWorks(t *testing.T) { // TODO: should this be unauthorized or not found? func TestDeleteClubNotExist(t *testing.T) { uuid := uuid.New() - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/clubs/%s", uuid), - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + h.InitTest(t).TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/clubs/%s", uuid), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.Unauthorized, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -476,6 +489,8 @@ func TestDeleteClubNotExist(t *testing.T) { } func TestDeleteClubBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -485,10 +500,14 @@ func TestDeleteClubBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/clubs/%s", badRequest), - Role: &models.Super, - }.TestOnError(t, nil, errors.FailedToParseUUID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/clubs/%s", badRequest), + Role: &models.Super, + }, errors.FailedToParseUUID, + ) } + + appAssert.Close() } diff --git a/backend/tests/api/health_test.go b/backend/tests/api/health_test.go index 789be8a82..52e51fe9d 100644 --- a/backend/tests/api/health_test.go +++ b/backend/tests/api/health_test.go @@ -8,10 +8,11 @@ import ( ) func TestHealthWorks(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: "/health", - }.TestOnStatus(t, nil, + h.InitTest(t).TestOnStatus( + h.TestRequest{ + Method: fiber.MethodGet, + Path: "/health", + }, fiber.StatusOK, ).Close() } diff --git a/backend/tests/api/helpers/requests.go b/backend/tests/api/helpers/requests.go index e740cff49..ff3cf9b3c 100644 --- a/backend/tests/api/helpers/requests.go +++ b/backend/tests/api/helpers/requests.go @@ -6,7 +6,6 @@ import ( "net/http" "net/http/httptest" "strings" - "testing" "github.com/GenerateNU/sac/backend/src/errors" "github.com/GenerateNU/sac/backend/src/models" @@ -78,33 +77,24 @@ func (app TestApp) Send(request TestRequest) (*http.Response, error) { return resp, nil } -func (request TestRequest) Test(t *testing.T, existingAppAssert *ExistingAppAssert) (ExistingAppAssert, *http.Response) { - if existingAppAssert == nil { - app, assert := InitTest(t) - - if request.Role != nil { - app.Auth(*request.Role) - } +func (request TestRequest) test(existingAppAssert ExistingAppAssert) (ExistingAppAssert, *http.Response) { + if request.Role != nil { + existingAppAssert.App.Auth(*request.Role) + } - existingAppAssert = &ExistingAppAssert{ - App: app, - Assert: assert, - } - } else { - if existingAppAssert.App.TestUser == nil && request.Role != nil { - existingAppAssert.App.Auth(*request.Role) - } + if existingAppAssert.App.TestUser == nil && request.Role != nil { + existingAppAssert.App.Auth(*request.Role) } resp, err := existingAppAssert.App.Send(request) existingAppAssert.Assert.NilError(err) - return *existingAppAssert, resp + return existingAppAssert, resp } -func (request TestRequest) TestOnStatus(t *testing.T, existingAppAssert *ExistingAppAssert, status int) ExistingAppAssert { - appAssert, resp := request.Test(t, existingAppAssert) +func (existingAppAssert ExistingAppAssert) TestOnStatus(request TestRequest, status int) ExistingAppAssert { + appAssert, resp := request.test(existingAppAssert) _, assert := appAssert.App, appAssert.Assert @@ -113,8 +103,8 @@ func (request TestRequest) TestOnStatus(t *testing.T, existingAppAssert *Existin return appAssert } -func (request *TestRequest) testOn(t *testing.T, existingAppAssert *ExistingAppAssert, status int, key string, value string) (ExistingAppAssert, *http.Response) { - appAssert, resp := request.Test(t, existingAppAssert) +func (request *TestRequest) testOn(existingAppAssert ExistingAppAssert, status int, key string, value string) (ExistingAppAssert, *http.Response) { + appAssert, resp := request.test(existingAppAssert) assert := appAssert.Assert var respBody map[string]interface{} @@ -128,8 +118,8 @@ func (request *TestRequest) testOn(t *testing.T, existingAppAssert *ExistingAppA return appAssert, resp } -func (request TestRequest) TestOnError(t *testing.T, existingAppAssert *ExistingAppAssert, expectedError errors.Error) ExistingAppAssert { - appAssert, _ := request.testOn(t, existingAppAssert, expectedError.StatusCode, "error", expectedError.Message) +func (existingAppAssert ExistingAppAssert) TestOnError(request TestRequest, expectedError errors.Error) ExistingAppAssert { + appAssert, _ := request.testOn(existingAppAssert, expectedError.StatusCode, "error", expectedError.Message) return appAssert } @@ -138,19 +128,19 @@ type ErrorWithTester struct { Tester Tester } -func (request TestRequest) TestOnErrorAndDB(t *testing.T, existingAppAssert *ExistingAppAssert, errorWithDBTester ErrorWithTester) ExistingAppAssert { - appAssert, resp := request.testOn(t, existingAppAssert, errorWithDBTester.Error.StatusCode, "error", errorWithDBTester.Error.Message) +func (existingAppAssert ExistingAppAssert) TestOnErrorAndDB(request TestRequest, errorWithDBTester ErrorWithTester) ExistingAppAssert { + appAssert, resp := request.testOn(existingAppAssert, errorWithDBTester.Error.StatusCode, "error", errorWithDBTester.Error.Message) errorWithDBTester.Tester(appAssert.App, appAssert.Assert, resp) return appAssert } -func (request TestRequest) TestOnMessage(t *testing.T, existingAppAssert *ExistingAppAssert, status int, message string) ExistingAppAssert { - request.testOn(t, existingAppAssert, status, "message", message) - return *existingAppAssert +func (existingAppAssert ExistingAppAssert) TestOnMessage(request TestRequest, status int, message string) ExistingAppAssert { + request.testOn(existingAppAssert, status, "message", message) + return existingAppAssert } -func (request TestRequest) TestOnMessageAndDB(t *testing.T, existingAppAssert *ExistingAppAssert, status int, message string, dbTester Tester) ExistingAppAssert { - appAssert, resp := request.testOn(t, existingAppAssert, status, "message", message) +func (existingAppAssert ExistingAppAssert) TestOnMessageAndDB(request TestRequest, status int, message string, dbTester Tester) ExistingAppAssert { + appAssert, resp := request.testOn(existingAppAssert, status, "message", message) dbTester(appAssert.App, appAssert.Assert, resp) return appAssert } @@ -162,8 +152,8 @@ type TesterWithStatus struct { Tester } -func (request TestRequest) TestOnStatusAndDB(t *testing.T, existingAppAssert *ExistingAppAssert, testerStatus TesterWithStatus) ExistingAppAssert { - appAssert, resp := request.Test(t, existingAppAssert) +func (existingAppAssert ExistingAppAssert) TestOnStatusAndDB(request TestRequest, testerStatus TesterWithStatus) ExistingAppAssert { + appAssert, resp := request.test(existingAppAssert) app, assert := appAssert.App, appAssert.Assert assert.Equal(testerStatus.Status, resp.StatusCode) diff --git a/backend/tests/api/helpers/test.go b/backend/tests/api/helpers/test.go index 80afe5091..9cc54aba5 100644 --- a/backend/tests/api/helpers/test.go +++ b/backend/tests/api/helpers/test.go @@ -6,11 +6,14 @@ import ( "github.com/huandu/go-assert" ) -func InitTest(t *testing.T) (TestApp, *assert.A) { +func InitTest(t *testing.T) ExistingAppAssert { assert := assert.New(t) app, err := spawnApp() assert.NilError(err) - return *app, assert + return ExistingAppAssert{ + App: *app, + Assert: assert, + } } diff --git a/backend/tests/api/tag_test.go b/backend/tests/api/tag_test.go index dec75da99..381c42d48 100644 --- a/backend/tests/api/tag_test.go +++ b/backend/tests/api/tag_test.go @@ -46,10 +46,11 @@ func AssertTagWithBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Respons } func AssertSampleTagBodyRespDB(t *testing.T, app h.TestApp, assert *assert.A, resp *http.Response) uuid.UUID { - appAssert, uuid := CreateSampleCategory(t, &h.ExistingAppAssert{ - App: app, - Assert: assert, - }) + appAssert, uuid := CreateSampleCategory(t, + &h.ExistingAppAssert{ + App: app, + Assert: assert, + }) return AssertTagWithBodyRespDB(appAssert.App, appAssert.Assert, resp, SampleTagFactory(uuid)) } @@ -60,12 +61,13 @@ func CreateSampleTag(t *testing.T) (appAssert h.ExistingAppAssert, categoryUUID tagUUID = AssertTagWithBodyRespDB(app, assert, resp, SampleTagFactory(categoryUUID)) } - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/tags/", - Body: SampleTagFactory(categoryUUID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/tags/", + Body: SampleTagFactory(categoryUUID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: AssertSampleTagBodyRespDB, @@ -99,6 +101,8 @@ func Assert1Tag(app h.TestApp, assert *assert.A, resp *http.Response) { } func TestCreateTagFailsBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badBodys := []map[string]interface{}{ { "name": "Generate", @@ -111,21 +115,26 @@ func TestCreateTagFailsBadRequest(t *testing.T) { } for _, badBody := range badBodys { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/tags/", - Body: &badBody, - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/tags/", + Body: &badBody, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToParseRequestBody, Tester: AssertNoTags, }, - ).Close() + ) } + + appAssert.Close() } func TestCreateTagFailsValidation(t *testing.T) { + appAssert := h.InitTest(t) + badBodys := []map[string]interface{}{ { "name": "Generate", @@ -137,28 +146,32 @@ func TestCreateTagFailsValidation(t *testing.T) { } for _, badBody := range badBodys { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/tags/", - Body: &badBody, - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/tags/", + Body: &badBody, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateTag, Tester: AssertNoTags, }, - ).Close() + ) } + + appAssert.Close() } func TestGetTagWorks(t *testing.T) { existingAppAssert, categoryUUID, tagUUID := CreateSampleTag(t) - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -169,6 +182,8 @@ func TestGetTagWorks(t *testing.T) { } func TestGetTagFailsBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -178,20 +193,28 @@ func TestGetTagFailsBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/tags/%s", badRequest), - Role: &models.Super, - }.TestOnError(t, nil, errors.FailedToValidateID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/tags/%s", badRequest), + Role: &models.Super, + }, + errors.FailedToValidateID, + ) } + + appAssert.Close() } func TestGetTagFailsNotFound(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/tags/%s", uuid.New()), - Role: &models.Super, - }.TestOnError(t, nil, errors.TagNotFound).Close() + h.InitTest(t).TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/tags/%s", uuid.New()), + Role: &models.Super, + }, + errors.TagNotFound, + ).Close() } func TestUpdateTagWorksUpdateName(t *testing.T) { @@ -204,12 +227,13 @@ func TestUpdateTagWorksUpdateName(t *testing.T) { tagUUID = AssertTagWithBodyRespDB(app, assert, resp, &generateNUTag) } - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), - Body: &generateNUTag, - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), + Body: &generateNUTag, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: AssertUpdatedTagBodyRespDB, @@ -229,12 +253,13 @@ func TestUpdateTagWorksUpdateCategory(t *testing.T) { technologyCategoryUUID = AssertCategoryWithBodyRespDBMostRecent(app, assert, resp, &technologyCategory) } - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/categories/", - Body: &technologyCategory, - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/categories/", + Body: &technologyCategory, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: AssertNewCategoryBodyRespDB, @@ -247,12 +272,13 @@ func TestUpdateTagWorksUpdateCategory(t *testing.T) { AssertTagWithBodyRespDB(app, assert, resp, &technologyTag) } - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), - Body: &technologyTag, - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), + Body: &technologyTag, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: AssertUpdatedTagBodyRespDB, @@ -263,12 +289,13 @@ func TestUpdateTagWorksUpdateCategory(t *testing.T) { func TestUpdateTagWorksWithSameDetails(t *testing.T) { existingAppAssert, categoryUUID, tagUUID := CreateSampleTag(t) - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), - Body: SampleTagFactory(categoryUUID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), + Body: SampleTagFactory(categoryUUID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -290,23 +317,29 @@ func TestUpdateTagFailsBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/tags/%s", badRequest), - Body: SampleTagFactory(uuid), - Role: &models.Super, - }.TestOnError(t, &appAssert, errors.FailedToValidateID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/tags/%s", badRequest), + Body: SampleTagFactory(uuid), + Role: &models.Super, + }, + errors.FailedToValidateID, + ) } + + appAssert.Close() } func TestDeleteTagWorks(t *testing.T) { existingAppAssert, _, tagUUID := CreateSampleTag(t) - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), - Role: &models.Super, - }.TestOnStatusAndDB(t, &existingAppAssert, + existingAppAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/tags/%s", tagUUID), + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusNoContent, Tester: AssertNoTags, @@ -326,11 +359,12 @@ func TestDeleteTagFailsBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/tags/%s", badRequest), - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/tags/%s", badRequest), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateID, Tester: Assert1Tag, @@ -344,11 +378,12 @@ func TestDeleteTagFailsBadRequest(t *testing.T) { func TestDeleteTagFailsNotFound(t *testing.T) { appAssert, _, _ := CreateSampleTag(t) - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/tags/%s", uuid.New()), - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/tags/%s", uuid.New()), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.TagNotFound, Tester: Assert1Tag, diff --git a/backend/tests/api/user_tag_test.go b/backend/tests/api/user_tag_test.go index 0c568abaf..0778c4b62 100644 --- a/backend/tests/api/user_tag_test.go +++ b/backend/tests/api/user_tag_test.go @@ -58,20 +58,21 @@ func SampleTagIDsFactory(tagIDs *[]uuid.UUID) *map[string]interface{} { func CreateSetOfTags(t *testing.T, appAssert *h.ExistingAppAssert) ([]uuid.UUID, *h.ExistingAppAssert) { if appAssert == nil { - app, assert := h.InitTest(t) - appAssert = &h.ExistingAppAssert{App: app, Assert: assert} + newAppAssert := h.InitTest(t) + appAssert = &newAppAssert } categories := SampleCategoriesFactory() categoryIDs := []uuid.UUID{} for _, category := range *categories { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/categories/", - Body: &category, - Role: &models.Super, - }.TestOnStatusAndDB(t, appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/categories/", + Body: &category, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -91,12 +92,13 @@ func CreateSetOfTags(t *testing.T, appAssert *h.ExistingAppAssert) ([]uuid.UUID, tagIDs := []uuid.UUID{} for _, tag := range *tags { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/tags/", - Body: &tag, - Role: &models.Super, - }.TestOnStatusAndDB(t, appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/tags/", + Body: &tag, + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -148,6 +150,8 @@ func AssertSampleUserTagsRespDB(app h.TestApp, assert *assert.A, resp *http.Resp } func TestCreateUserTagsFailsOnInvalidDataType(t *testing.T) { + appAssert := h.InitTest(t) + // Invalid tag data types: invalidTags := []interface{}{ []string{"1", "2", "34"}, @@ -160,17 +164,24 @@ func TestCreateUserTagsFailsOnInvalidDataType(t *testing.T) { malformedTag := *SampleTagIDsFactory(nil) malformedTag["tags"] = tag - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/:userID/tags/", - Body: &malformedTag, - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnError(t, nil, errors.FailedToParseRequestBody).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/:userID/tags/", + Body: &malformedTag, + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, + errors.FailedToParseRequestBody, + ) } + + appAssert.Close() } func TestCreateUserTagsFailsOnInvalidUserID(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -180,13 +191,18 @@ func TestCreateUserTagsFailsOnInvalidUserID(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodPost, - Path: fmt.Sprintf("/api/v1/users/%s/tags", badRequest), - Body: SampleTagIDsFactory(nil), - Role: &models.Student, - }.TestOnError(t, nil, errors.FailedToParseUUID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodPost, + Path: fmt.Sprintf("/api/v1/users/%s/tags", badRequest), + Body: SampleTagIDsFactory(nil), + Role: &models.Student, + }, + errors.FailedToParseUUID, + ) } + + appAssert.Close() } type UUIDSlice []uuid.UUID @@ -194,6 +210,8 @@ type UUIDSlice []uuid.UUID var testUUID = uuid.New() func TestCreateUserTagsFailsOnInvalidKey(t *testing.T) { + appAssert := h.InitTest(t) + invalidBody := []map[string]interface{}{ { "tag": UUIDSlice{testUUID, testUUID}, @@ -204,26 +222,32 @@ func TestCreateUserTagsFailsOnInvalidKey(t *testing.T) { } for _, body := range invalidBody { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/:userID/tags/", - Body: &body, - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnError(t, nil, errors.FailedToValidateUserTags).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/:userID/tags/", + Body: &body, + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, + errors.FailedToValidateUserTags, + ) } + + appAssert.Close() } // TODO: should this be unauthorized or not found? func TestCreateUserTagsFailsOnNonExistentUser(t *testing.T) { uuid := uuid.New() - h.TestRequest{ - Method: fiber.MethodPost, - Path: fmt.Sprintf("/api/v1/users/%s/tags/", uuid), - Body: SampleTagIDsFactory(nil), - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + h.InitTest(t).TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: fmt.Sprintf("/api/v1/users/%s/tags/", uuid), + Body: SampleTagIDsFactory(nil), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.UserNotFound, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -233,7 +257,7 @@ func TestCreateUserTagsFailsOnNonExistentUser(t *testing.T) { assert.Error(err) }, }, - ) + ).Close() } func TestCreateUserTagsWorks(t *testing.T) { @@ -241,13 +265,14 @@ func TestCreateUserTagsWorks(t *testing.T) { tagUUIDs, appAssert := CreateSetOfTags(t, nil) // Confirm adding real tags adds them to the user: - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/:userID/tags/", - Body: SampleTagIDsFactory(&tagUUIDs), - Role: &models.Super, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatusAndDB(t, appAssert, + appAssert.TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/:userID/tags/", + Body: SampleTagIDsFactory(&tagUUIDs), + Role: &models.Super, + TestUserIDRequired: h.BoolToPointer(true), + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -260,13 +285,14 @@ func TestCreateUserTagsWorks(t *testing.T) { } func TestCreateUserTagsNoneAddedIfInvalid(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/:userID/tags/", - Body: SampleTagIDsFactory(nil), - Role: &models.Super, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatusAndDB(t, nil, + h.InitTest(t).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/:userID/tags/", + Body: SampleTagIDsFactory(nil), + Role: &models.Super, + TestUserIDRequired: h.BoolToPointer(true), + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -284,20 +310,23 @@ func TestCreateUserTagsNoneAddedIfInvalid(t *testing.T) { // TODO: should this be unauthorized or not found? func TestGetUserTagsFailsOnNonExistentUser(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/users/%s/tags/", uuid.New()), - Role: &models.Super, - }.TestOnError(t, nil, errors.UserNotFound).Close() + h.InitTest(t).TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/users/%s/tags/", uuid.New()), + Role: &models.Super, + }, errors.UserNotFound, + ).Close() } func TestGetUserTagsReturnsEmptyListWhenNoneAdded(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: "/api/v1/users/:userID/tags/", - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatusAndDB(t, nil, + h.InitTest(t).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: "/api/v1/users/:userID/tags/", + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -314,32 +343,31 @@ func TestGetUserTagsReturnsEmptyListWhenNoneAdded(t *testing.T) { } func TestGetUserTagsReturnsCorrectList(t *testing.T) { - // Create a set of tags: tagUUIDs, appAssert := CreateSetOfTags(t, nil) - // Add the tags: - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/:userID/tags/", - Body: SampleTagIDsFactory(&tagUUIDs), - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatus(t, appAssert, fiber.StatusCreated) - - // Get the tags: - h.TestRequest{ - Method: fiber.MethodGet, - Path: "/api/v1/users/:userID/tags/", - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatusAndDB(t, appAssert, + newAppAssert := *appAssert + + newAppAssert.TestOnStatus( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/:userID/tags/", + Body: SampleTagIDsFactory(&tagUUIDs), + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, + fiber.StatusCreated, + ).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: "/api/v1/users/:userID/tags/", + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { AssertSampleUserTagsRespDB(app, assert, resp, app.TestUser.UUID) }, }, - ) - - appAssert.Close() + ).Close() } diff --git a/backend/tests/api/user_test.go b/backend/tests/api/user_test.go index ebd8f8eea..1814c14c2 100644 --- a/backend/tests/api/user_test.go +++ b/backend/tests/api/user_test.go @@ -21,11 +21,12 @@ import ( ) func TestGetUsersWorksForSuper(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: "/api/v1/users/", - Role: &models.Super, - }.TestOnStatusAndDB(t, nil, + h.InitTest(t).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: "/api/v1/users/", + Role: &models.Super, + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -61,20 +62,24 @@ func TestGetUsersWorksForSuper(t *testing.T) { } func TestGetUsersFailsForStudent(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: "/api/v1/users/", - Role: &models.Student, - }.TestOnError(t, nil, errors.Unauthorized).Close() + h.InitTest(t).TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: "/api/v1/users/", + Role: &models.Super, + }, + errors.Unauthorized, + ).Close() } func TestGetUserWorks(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodGet, - Path: "/api/v1/users/:userID", - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatusAndDB(t, nil, + h.InitTest(t).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: "/api/v1/users/:userID", + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -106,6 +111,8 @@ func TestGetUserWorks(t *testing.T) { } func TestGetUserFailsBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -115,23 +122,29 @@ func TestGetUserFailsBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/users/%s", badRequest), - Role: &models.Super, - }.TestOnError(t, nil, errors.FailedToParseUUID).Close() + appAssert.TestOnError( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/users/%s", badRequest), + Role: &models.Super, + }, + errors.FailedToParseUUID, + ) } + + appAssert.Close() } // TODO: should this be not found or unauthorized? func TestGetUserFailsNotExist(t *testing.T) { uuid := uuid.New() - h.TestRequest{ - Method: fiber.MethodGet, - Path: fmt.Sprintf("/api/v1/users/%s", uuid), - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + h.InitTest(t).TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodGet, + Path: fmt.Sprintf("/api/v1/users/%s", uuid), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.UserNotFound, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -150,16 +163,17 @@ func TestUpdateUserWorks(t *testing.T) { newFirstName := "Michael" newLastName := "Brennan" - h.TestRequest{ - Method: fiber.MethodPatch, - Path: "/api/v1/users/:userID", - Body: &map[string]interface{}{ - "first_name": newFirstName, - "last_name": newLastName, + h.InitTest(t).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: "/api/v1/users/:userID", + Body: &map[string]interface{}{ + "first_name": newFirstName, + "last_name": newLastName, + }, + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), }, - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatusAndDB(t, nil, h.TesterWithStatus{ Status: fiber.StatusOK, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -199,6 +213,8 @@ func TestUpdateUserWorks(t *testing.T) { // TODO: should this be unauthorized or fail on processing request func TestUpdateUserFailsOnInvalidBody(t *testing.T) { + appAssert := h.InitTest(t) + for _, invalidData := range []map[string]interface{}{ {"email": "not.northeastern@gmail.com"}, {"nuid": "1800-123-4567"}, @@ -206,22 +222,27 @@ func TestUpdateUserFailsOnInvalidBody(t *testing.T) { {"year": 1963}, {"college": "UT-Austin"}, } { - h.TestRequest{ - Method: fiber.MethodPatch, - Path: "/api/v1/users/:userID", - Body: &invalidData, - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnErrorAndDB(t, nil, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: "/api/v1/users/:userID", + Body: &invalidData, + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, h.ErrorWithTester{ Error: errors.FailedToValidateUser, Tester: TestNumUsersRemainsAt2, }, - ).Close() + ) } + + appAssert.Close() } func TestUpdateUserFailsBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -233,13 +254,17 @@ func TestUpdateUserFailsBadRequest(t *testing.T) { sampleStudent, rawPassword := h.SampleStudentFactory() for _, badRequest := range badRequests { - h.TestRequest{ + appAssert.TestOnError(h.TestRequest{ Method: fiber.MethodPatch, Path: fmt.Sprintf("/api/v1/users/%s", badRequest), Body: h.SampleStudentJSONFactory(sampleStudent, rawPassword), Role: &models.Student, - }.TestOnError(t, nil, errors.FailedToParseUUID).Close() + }, + errors.FailedToParseUUID, + ) } + + appAssert.Close() } // TODO: should this be unauthorized or not found? @@ -248,12 +273,13 @@ func TestUpdateUserFailsOnIdNotExist(t *testing.T) { sampleStudent, rawPassword := h.SampleStudentFactory() - h.TestRequest{ - Method: fiber.MethodPatch, - Path: fmt.Sprintf("/api/v1/users/%s", uuid), - Body: h.SampleStudentJSONFactory(sampleStudent, rawPassword), - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + h.InitTest(t).TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPatch, + Path: fmt.Sprintf("/api/v1/users/%s", uuid), + Body: h.SampleStudentJSONFactory(sampleStudent, rawPassword), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.UserNotFound, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -269,12 +295,13 @@ func TestUpdateUserFailsOnIdNotExist(t *testing.T) { // TODO: should this be unauthorized? func TestDeleteUserWorks(t *testing.T) { - h.TestRequest{ - Method: fiber.MethodDelete, - Path: "/api/v1/users/:userID", - Role: &models.Student, - TestUserIDRequired: h.BoolToPointer(true), - }.TestOnStatusAndDB(t, nil, + h.InitTest(t).TestOnStatusAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: "/api/v1/users/:userID", + Role: &models.Student, + TestUserIDRequired: h.BoolToPointer(true), + }, h.TesterWithStatus{ Status: fiber.StatusNoContent, Tester: TestNumUsersRemainsAt1, @@ -285,12 +312,13 @@ func TestDeleteUserWorks(t *testing.T) { // TODO: how should this work now? func TestDeleteUserNotExist(t *testing.T) { uuid := uuid.New() - h.TestRequest{ + + h.InitTest(t).TestOnErrorAndDB(h.TestRequest{ Method: fiber.MethodDelete, Path: fmt.Sprintf("/api/v1/users/%s", uuid), Role: &models.Super, TestUserIDRequired: h.BoolToPointer(true), - }.TestOnErrorAndDB(t, nil, + }, h.ErrorWithTester{ Error: errors.UserNotFound, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -307,6 +335,8 @@ func TestDeleteUserNotExist(t *testing.T) { } func TestDeleteUserBadRequest(t *testing.T) { + appAssert := h.InitTest(t) + badRequests := []string{ "0", "-1", @@ -316,17 +346,20 @@ func TestDeleteUserBadRequest(t *testing.T) { } for _, badRequest := range badRequests { - h.TestRequest{ - Method: fiber.MethodDelete, - Path: fmt.Sprintf("/api/v1/users/%s", badRequest), - Role: &models.Super, - }.TestOnErrorAndDB(t, nil, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodDelete, + Path: fmt.Sprintf("/api/v1/users/%s", badRequest), + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToParseUUID, Tester: TestNumUsersRemainsAt1, }, ) } + + appAssert.Close() } func AssertUserWithIDBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Response, body *map[string]interface{}) uuid.UUID { @@ -376,16 +409,21 @@ func AssertSampleUserBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Resp } func CreateSampleStudent(t *testing.T, existingAppAssert *h.ExistingAppAssert) (h.ExistingAppAssert, uuid.UUID, *map[string]interface{}) { + if existingAppAssert == nil { + newAppAssert := h.InitTest(t) + existingAppAssert = &newAppAssert + } + var uuid uuid.UUID sampleStudent, rawPassword := h.SampleStudentFactory() - newAppAssert := h.TestRequest{ + existingAppAssert.TestOnStatusAndDB(h.TestRequest{ Method: fiber.MethodPost, Path: "/api/v1/users/", Body: h.SampleStudentJSONFactory(sampleStudent, rawPassword), Role: &models.Super, - }.TestOnStatusAndDB(t, existingAppAssert, + }, h.TesterWithStatus{ Status: fiber.StatusCreated, Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) { @@ -394,11 +432,7 @@ func CreateSampleStudent(t *testing.T, existingAppAssert *h.ExistingAppAssert) ( }, ) - if existingAppAssert == nil { - return newAppAssert, uuid, h.SampleStudentJSONFactory(sampleStudent, rawPassword) - } else { - return *existingAppAssert, uuid, h.SampleStudentJSONFactory(sampleStudent, rawPassword) - } + return *existingAppAssert, uuid, h.SampleStudentJSONFactory(sampleStudent, rawPassword) } func AssertNumUsersRemainsAtN(app h.TestApp, assert *assert.A, resp *http.Response, n int) { @@ -429,19 +463,18 @@ func TestCreateUserFailsIfUserWithEmailAlreadyExists(t *testing.T) { (*body)["id"] = studentUUID - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/", - Body: body, - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/", + Body: body, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.UserAlreadyExists, Tester: TestNumUsersRemainsAt2, }, - ) - - appAssert.Close() + ).Close() } func TestCreateUserFailsIfUserWithNUIDAlreadyExists(t *testing.T) { @@ -455,12 +488,13 @@ func TestCreateUserFailsIfUserWithNUIDAlreadyExists(t *testing.T) { (*slightlyDifferentSampleStudentJSON)["last_name"] = "Doe" (*slightlyDifferentSampleStudentJSON)["email"] = "doe.john@northeastern.edu" - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/", - Body: slightlyDifferentSampleStudentJSON, - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/", + Body: slightlyDifferentSampleStudentJSON, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.UserAlreadyExists, Tester: TestNumUsersRemainsAt2, @@ -477,18 +511,20 @@ func AssertCreateBadDataFails(t *testing.T, jsonKey string, badValues []interfac sampleUserPermutation := *h.SampleStudentJSONFactory(sampleStudent, rawPassword) sampleUserPermutation[jsonKey] = badValue - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/", - Body: &sampleUserPermutation, - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/", + Body: &sampleUserPermutation, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateUser, Tester: TestNumUsersRemainsAt2, }, ) } + appAssert.Close() } @@ -569,17 +605,19 @@ func TestCreateUserFailsOnMissingFields(t *testing.T) { sampleUserPermutation := *h.SampleStudentJSONFactory(sampleStudent, rawPassword) delete(sampleUserPermutation, missingField) - h.TestRequest{ - Method: fiber.MethodPost, - Path: "/api/v1/users/", - Body: &sampleUserPermutation, - Role: &models.Super, - }.TestOnErrorAndDB(t, &appAssert, + appAssert.TestOnErrorAndDB( + h.TestRequest{ + Method: fiber.MethodPost, + Path: "/api/v1/users/", + Body: &sampleUserPermutation, + Role: &models.Super, + }, h.ErrorWithTester{ Error: errors.FailedToValidateUser, Tester: TestNumUsersRemainsAt2, }, ) } + appAssert.Close() }