Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 4, 2024
1 parent fbc8147 commit 727e858
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 112 deletions.
3 changes: 3 additions & 0 deletions backend/src/services/club.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package services

import (
"fmt"

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/transactions"
Expand Down Expand Up @@ -45,6 +47,7 @@ func (c *ClubService) GetClubs(limit string, page string) ([]models.Club, *error

func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, *errors.Error) {
if err := c.Validate.Struct(clubBody); err != nil {
fmt.Print(err)
return nil, &errors.FailedToValidateClub
}

Expand Down
16 changes: 8 additions & 8 deletions backend/tests/api/category_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func AssertTagsWithBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Respon
}

func TestGetCategoryTagsWorks(t *testing.T) {
appAssert, categoryUUID, tagID := CreateSampleTag(t)
appAssert, categoryUUID, tagID := CreateSampleTag(h.InitTest(t))

body := SampleTagFactory(categoryUUID)
(*body)["id"] = tagID
Expand All @@ -66,7 +66,7 @@ func TestGetCategoryTagsWorks(t *testing.T) {
}

func TestGetCategoryTagsFailsCategoryBadRequest(t *testing.T) {
appAssert, _ := CreateSampleCategory(t, nil)
appAssert, _ := CreateSampleCategory(h.InitTest(t))

badRequests := []string{
"0",
Expand All @@ -91,7 +91,7 @@ func TestGetCategoryTagsFailsCategoryBadRequest(t *testing.T) {
}

func TestGetCategoryTagsFailsCategoryNotFound(t *testing.T) {
appAssert, _ := CreateSampleCategory(t, nil)
appAssert, _ := CreateSampleCategory(h.InitTest(t))

uuid := uuid.New()

Expand All @@ -112,7 +112,7 @@ func TestGetCategoryTagsFailsCategoryNotFound(t *testing.T) {
}

func TestGetCategoryTagWorks(t *testing.T) {
existingAppAssert, categoryUUID, tagUUID := CreateSampleTag(t)
existingAppAssert, categoryUUID, tagUUID := CreateSampleTag(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
h.TestRequest{
Expand All @@ -130,7 +130,7 @@ func TestGetCategoryTagWorks(t *testing.T) {
}

func TestGetCategoryTagFailsCategoryBadRequest(t *testing.T) {
appAssert, _, tagUUID := CreateSampleTag(t)
appAssert, _, tagUUID := CreateSampleTag(h.InitTest(t))

badRequests := []string{
"0",
Expand All @@ -154,7 +154,7 @@ func TestGetCategoryTagFailsCategoryBadRequest(t *testing.T) {
}

func TestGetCategoryTagFailsTagBadRequest(t *testing.T) {
appAssert, categoryUUID := CreateSampleCategory(t, nil)
appAssert, categoryUUID := CreateSampleCategory(h.InitTest(t))

badRequests := []string{
"0",
Expand All @@ -178,7 +178,7 @@ func TestGetCategoryTagFailsTagBadRequest(t *testing.T) {
}

func TestGetCategoryTagFailsCategoryNotFound(t *testing.T) {
appAssert, _, tagUUID := CreateSampleTag(t)
appAssert, _, tagUUID := CreateSampleTag(h.InitTest(t))

appAssert.TestOnError(
h.TestRequest{
Expand All @@ -191,7 +191,7 @@ func TestGetCategoryTagFailsCategoryNotFound(t *testing.T) {
}

func TestGetCategoryTagFailsTagNotFound(t *testing.T) {
appAssert, categoryUUID := CreateSampleCategory(t, nil)
appAssert, categoryUUID := CreateSampleCategory(h.InitTest(t))

appAssert.TestOnError(
h.TestRequest{
Expand Down
27 changes: 11 additions & 16 deletions backend/tests/api/category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,9 @@ func AssertSampleCategoryBodyRespDB(app h.TestApp, assert *assert.A, resp *http.
return AssertCategoryBodyRespDB(app, assert, resp, SampleCategoryFactory())
}

func CreateSampleCategory(t *testing.T, existingAppAssert *h.ExistingAppAssert) (h.ExistingAppAssert, uuid.UUID) {
func CreateSampleCategory(existingAppAssert h.ExistingAppAssert) (h.ExistingAppAssert, uuid.UUID) {
var sampleCategoryUUID uuid.UUID

if existingAppAssert == nil {
newAppAssert := h.InitTest(t)
existingAppAssert = &newAppAssert
}

existingAppAssert.TestOnStatusAndDB(
h.TestRequest{
Method: fiber.MethodPost,
Expand All @@ -94,11 +89,11 @@ func CreateSampleCategory(t *testing.T, existingAppAssert *h.ExistingAppAssert)
},
)

return *existingAppAssert, sampleCategoryUUID
return existingAppAssert, sampleCategoryUUID
}

func TestCreateCategoryWorks(t *testing.T) {
existingAppAssert, _ := CreateSampleCategory(t, nil)
existingAppAssert, _ := CreateSampleCategory(h.InitTest(t))
existingAppAssert.Close()
}

Expand Down Expand Up @@ -173,7 +168,7 @@ func TestCreateCategoryFailsIfNameIsMissing(t *testing.T) {
}

func TestCreateCategoryFailsIfCategoryWithThatNameAlreadyExists(t *testing.T) {
existingAppAssert, _ := CreateSampleCategory(t, nil)
existingAppAssert, _ := CreateSampleCategory(h.InitTest(t))

TestNumCategoriesRemainsAt1 := func(app h.TestApp, assert *assert.A, resp *http.Response) {
AssertNumCategoriesRemainsAtN(app, assert, resp, 1)
Expand Down Expand Up @@ -201,7 +196,7 @@ func TestCreateCategoryFailsIfCategoryWithThatNameAlreadyExists(t *testing.T) {
}

func TestGetCategoryWorks(t *testing.T) {
existingAppAssert, uuid := CreateSampleCategory(t, nil)
existingAppAssert, uuid := CreateSampleCategory(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
h.TestRequest{
Expand Down Expand Up @@ -254,7 +249,7 @@ func TestGetCategoryFailsNotFound(t *testing.T) {
}

func TestGetCategoriesWorks(t *testing.T) {
existingAppAssert, _ := CreateSampleCategory(t, nil)
existingAppAssert, _ := CreateSampleCategory(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
h.TestRequest{
Expand Down Expand Up @@ -315,7 +310,7 @@ func AssertUpdatedCategoryBodyRespDB(app h.TestApp, assert *assert.A, resp *http
}

func TestUpdateCategoryWorks(t *testing.T) {
existingAppAssert, uuid := CreateSampleCategory(t, nil)
existingAppAssert, uuid := CreateSampleCategory(h.InitTest(t))

category := map[string]interface{}{
"id": uuid,
Expand All @@ -341,7 +336,7 @@ func TestUpdateCategoryWorks(t *testing.T) {
}

func TestUpdateCategoryWorksWithSameDetails(t *testing.T) {
existingAppAssert, uuid := CreateSampleCategory(t, nil)
existingAppAssert, uuid := CreateSampleCategory(h.InitTest(t))

category := *SampleCategoryFactory()
category["id"] = uuid
Expand Down Expand Up @@ -391,7 +386,7 @@ func TestUpdateCategoryFailsBadRequest(t *testing.T) {
}

func TestDeleteCategoryWorks(t *testing.T) {
existingAppAssert, uuid := CreateSampleCategory(t, nil)
existingAppAssert, uuid := CreateSampleCategory(h.InitTest(t))

existingAppAssert.TestOnStatusAndDB(
h.TestRequest{
Expand All @@ -407,7 +402,7 @@ func TestDeleteCategoryWorks(t *testing.T) {
}

func TestDeleteCategoryFailsBadRequest(t *testing.T) {
existingAppAssert, _ := CreateSampleCategory(t, nil)
existingAppAssert, _ := CreateSampleCategory(h.InitTest(t))

badRequests := []string{
"0",
Expand Down Expand Up @@ -435,7 +430,7 @@ func TestDeleteCategoryFailsBadRequest(t *testing.T) {
}

func TestDeleteCategoryFailsNotFound(t *testing.T) {
existingAppAssert, _ := CreateSampleCategory(t, nil)
existingAppAssert, _ := CreateSampleCategory(h.InitTest(t))

existingAppAssert.TestOnErrorAndDB(
h.TestRequest{
Expand Down
36 changes: 16 additions & 20 deletions backend/tests/api/club_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func AssertClubBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Response,

assert.Equal(1, len(dbAdmins))

assert.Equal((*body)["user_id"].(uuid.UUID), dbAdmins[0].ID)
assert.Equal(*(*body)["user_id"].(*uuid.UUID), dbAdmins[0].ID)
assert.Equal((*body)["name"].(string), dbClub.Name)
assert.Equal((*body)["preview"].(string), dbClub.Preview)
assert.Equal((*body)["description"].(string), dbClub.Description)
Expand Down Expand Up @@ -131,38 +131,34 @@ func AssertClubWithBodyRespDBMostRecent(app h.TestApp, assert *assert.A, resp *h
func AssertSampleClubBodyRespDB(app h.TestApp, assert *assert.A, resp *http.Response, userID uuid.UUID) uuid.UUID {
sampleClub := SampleClubFactory(&userID)
(*sampleClub)["num_members"] = 1

return AssertClubBodyRespDB(app, assert, resp, sampleClub)
}

func CreateSampleClub(t *testing.T, existingAppAssert *h.ExistingAppAssert) (eaa h.ExistingAppAssert, studentUUID uuid.UUID, clubUUID uuid.UUID) {
appAssert, userID, _ := CreateSampleStudent(t, existingAppAssert)

func CreateSampleClub(existingAppAssert h.ExistingAppAssert) (eaa h.ExistingAppAssert, studentUUID uuid.UUID, clubUUID uuid.UUID) {
var sampleClubUUID uuid.UUID

appAssert.TestOnStatusAndDB(
newAppAssert := existingAppAssert.TestOnStatusAndDB(
h.TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/clubs/",
Body: SampleClubFactory(&userID),
Role: &models.Super,
Method: fiber.MethodPost,
Path: "/api/v1/clubs/",
Body: SampleClubFactory(nil),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer("user_id"),
},
h.TesterWithStatus{
Status: fiber.StatusCreated,
Tester: func(app h.TestApp, assert *assert.A, resp *http.Response) {
sampleClubUUID = AssertSampleClubBodyRespDB(app, assert, resp, userID)
sampleClubUUID = AssertSampleClubBodyRespDB(app, assert, resp, app.TestUser.UUID)
},
},
)

if existingAppAssert == nil {
return appAssert, userID, sampleClubUUID
} else {
return *existingAppAssert, userID, sampleClubUUID
}
return existingAppAssert, newAppAssert.App.TestUser.UUID, sampleClubUUID
}

func TestCreateClubWorks(t *testing.T) {
existingAppAssert, _, _ := CreateSampleClub(t, nil)
existingAppAssert, _, _ := CreateSampleClub(h.InitTest(t))
existingAppAssert.Close()
}

Expand Down Expand Up @@ -315,7 +311,7 @@ func TestCreateClubFailsOnInvalidLogo(t *testing.T) {

// TODO: need to be able to join the club
func TestUpdateClubWorks(t *testing.T) {
appAssert, studentUUID, clubUUID := CreateSampleClub(t, nil)
appAssert, studentUUID, clubUUID := CreateSampleClub(h.InitTest(t))

updatedClub := SampleClubFactory(&studentUUID)
(*updatedClub)["name"] = "Updated Name"
Expand All @@ -339,7 +335,7 @@ func TestUpdateClubWorks(t *testing.T) {

// TODO: need to be able to join the club to try to update
func TestUpdateClubFailsOnInvalidBody(t *testing.T) {
appAssert, studentUUID, clubUUID := CreateSampleClub(t, nil)
appAssert, studentUUID, clubUUID := CreateSampleClub(h.InitTest(t))

body := SampleClubFactory(&studentUUID)

Expand Down Expand Up @@ -432,7 +428,7 @@ func TestUpdateClubFailsOnClubIdNotExist(t *testing.T) {
Path: fmt.Sprintf("/api/v1/clubs/%s", uuid),
Body: SampleClubFactory(nil),
Role: &models.Student,
TestUserIDRequired: h.BoolToPointer(true),
TestUserIDReplaces: h.StringToPointer("user_id"),
},
h.ErrorWithTester{
Error: errors.ClubNotFound,
Expand All @@ -449,7 +445,7 @@ func TestUpdateClubFailsOnClubIdNotExist(t *testing.T) {

// TODO: need to be able to join the club
func TestDeleteClubWorks(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(t, nil)
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t))

appAssert.TestOnStatusAndDB(
h.TestRequest{
Expand Down
24 changes: 12 additions & 12 deletions backend/tests/api/helpers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,29 @@ type TestRequest struct {
Body *map[string]interface{}
Headers *map[string]string
Role *models.UserRole
TestUserIDRequired *bool
TestUserIDReplaces *string
}

func (app TestApp) Send(request TestRequest) (*http.Response, error) {
address := fmt.Sprintf("%s%s", app.Address, request.Path)

var req *http.Request

if request.TestUserIDRequired != nil && *request.TestUserIDRequired {
request.Path = strings.Replace(request.Path, ":userID", app.TestUser.UUID.String(), 1)
address = fmt.Sprintf("%s%s", app.Address, request.Path)
if request.TestUserIDReplaces != nil {
if strings.Contains(request.Path, *request.TestUserIDReplaces) {
request.Path = strings.Replace(request.Path, *request.TestUserIDReplaces, app.TestUser.UUID.String(), 1)
address = fmt.Sprintf("%s%s", app.Address, request.Path)
}
if request.Body != nil {
if _, ok := (*request.Body)[*request.TestUserIDReplaces]; ok {
(*request.Body)[*request.TestUserIDReplaces] = app.TestUser.UUID.String()
}
}
}

if request.Body == nil {
req = httptest.NewRequest(request.Method, address, nil)
} else {
if app.TestUser != nil && request.TestUserIDRequired != nil && *request.TestUserIDRequired {
(*request.Body)["id"] = app.TestUser.UUID
}

bodyBytes, err := json.Marshal(request.Body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -78,10 +82,6 @@ func (app TestApp) Send(request TestRequest) (*http.Response, error) {
}

func (request TestRequest) test(existingAppAssert ExistingAppAssert) (ExistingAppAssert, *http.Response) {
if request.Role != nil {
existingAppAssert.App.Auth(*request.Role)
}

if existingAppAssert.App.TestUser == nil && request.Role != nil {
existingAppAssert.App.Auth(*request.Role)
}
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/api/helpers/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func AllCasingPermutations(word string) []string {
return results
}

func BoolToPointer(b bool) *bool {
return &b
func StringToPointer(s string) *string {
return &s
}
Loading

0 comments on commit 727e858

Please sign in to comment.