Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
:
  • Loading branch information
Alder Whiteford authored and Alder Whiteford committed Mar 5, 2024
1 parent b37328f commit 1ef80b3
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/src/controllers/user_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ func (ut *UserTagController) DeleteUserTag(c *fiber.Ctx) error {
}

return c.SendStatus(fiber.StatusNoContent)
}
}
2 changes: 1 addition & 1 deletion backend/src/server/routes/user_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func UserTag(usersRouter fiber.Router, userTagService services.UserTagServiceInt

userTags.Post("/", userTagController.CreateUserTags)
userTags.Get("/", userTagController.GetUserTags)

tagID := userTags.Group("/:tagID")
tagID.Delete("/", userTagController.DeleteUserTag)
}
1 change: 0 additions & 1 deletion backend/src/transactions/user_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func DeleteUserTag(db *gorm.DB, id uuid.UUID, tagID uuid.UUID) *errors.Error {
return err
}


if err := db.Model(&user).Association("Tag").Delete(&tag); err != nil {
return &errors.FailedToUpdateUser
}
Expand Down
203 changes: 203 additions & 0 deletions backend/tests/api/user_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/transactions"
h "github.com/GenerateNU/sac/backend/tests/api/helpers"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -368,3 +369,205 @@ func TestGetUserTagsReturnsCorrectList(t *testing.T) {
},
).Close()
}

func TestDeleteUserTagFailsOnNonExistentUser(t *testing.T) {
userID := uuid.New()
tagID := uuid.New()

h.InitTest(t).TestOnErrorAndTester(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/%s/tags/%s/", userID, tagID),
Role: &models.Super,
},
h.ErrorWithTester{
Error: errors.UserNotFound,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var dbUser models.User

err := eaa.App.Conn.First(&dbUser, userID).Error

eaa.Assert.Assert(err != nil)
},
},
).Close()
}

func TestDeleteUserTagFailsOnNonExistentTag(t *testing.T) {
tagID := uuid.New()

h.InitTest(t).TestOnErrorAndTester(
h.TestRequest{
Method: fiber.MethodDelete,

Check failure on line 401 in backend/tests/api/user_tag_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
Path: fmt.Sprintf("/api/v1/users/:userID/tags/%s/", tagID),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.ErrorWithTester{
Error: errors.TagNotFound,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var dbTag models.Tag

err := eaa.App.Conn.First(&dbTag, tagID).Error
eaa.Assert.Assert(err != nil)
},
},
).Close()
}

func TestDeleteUserTagFailsOnInvalidUserUUID(t *testing.T) {
appAssert := h.InitTest(t)

badUserUUIDs := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}

for _, badUserUUID := range badUserUUIDs {
appAssert = appAssert.TestOnError(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/%s/tags/%s/", badUserUUID, uuid.New()),
Role: &models.Super,
},
errors.FailedToValidateID,
)
}

appAssert.Close()
}

func TestDeleteUserTagFailsOnInvalidTagUUID(t *testing.T) {
appAssert := h.InitTest(t)

badTagUUIDs := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}

for _, badTagUUID := range badTagUUIDs {
appAssert = appAssert.TestOnError(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/:userID/tags/%s/", badTagUUID),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
errors.FailedToValidateID,
)
}

appAssert.Close()

}

Check failure on line 468 in backend/tests/api/user_tag_test.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary trailing newline (whitespace)

func TestDeleteUserTagDoesNotAlterTagListOnNonAssociation(t *testing.T) {
tagUUIDs, appAssert := CreateSetOfTags(h.InitTest(t))
appAssert.Assert.Assert(len(tagUUIDs) > 1)

// Tag to be queried:
tagID := tagUUIDs[0]

// Tags to be added to the user:
tagUUIDs = tagUUIDs[1:]

appAssert.TestOnStatus(
h.TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/users/:userID/tags/",
Body: SampleTagIDsFactory(&tagUUIDs),
Role: &models.Student,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
fiber.StatusCreated,
)

userTagsBeforeDeletion, err := transactions.GetUserTags(appAssert.App.Conn, appAssert.App.TestUser.UUID);
appAssert.Assert.NilError(&err)

appAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/:userID/tags/%s/", tagID),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.TesterWithStatus{
Status: fiber.StatusNoContent,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var dbUser models.User

err := eaa.App.Conn.Where("id = ?", appAssert.App.TestUser.UUID).Preload("Tag").First(&dbUser).Error
eaa.Assert.NilError(err)

eaa.Assert.Equal(dbUser.Tag, userTagsBeforeDeletion)
},
},
).Close()
}

func TestDeleteUserTagRemovesTagFromUser(t *testing.T) {
tagUUIDs, appAssert := CreateSetOfTags(h.InitTest(t))
appAssert.Assert.Assert(len(tagUUIDs) > 1)

tagID := tagUUIDs[0]

appAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/users/:userID/tags/",
Body: SampleTagIDsFactory(&tagUUIDs),
Role: &models.Student,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.TesterWithStatus{
Status: fiber.StatusCreated,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var dbUser models.User

err := eaa.App.Conn.Where("id = ?", eaa.App.TestUser.UUID).Preload("Tag").First(&dbUser)
eaa.Assert.NilError(err)

eaa.Assert.Equal(len(dbUser.Tag), len(tagUUIDs))

var dbTag models.Tag

err = eaa.App.Conn.Where("id = ?", tagID).Preload("User").First(&dbTag)
eaa.Assert.NilError(err)

eaa.Assert.Equal(len(dbTag.User), 1)
},
},
).TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/users/:userID/tags/%s/", tagID),
Role: &models.Student,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.TesterWithStatus{
Status: fiber.StatusNoContent,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var dbUser models.User

err := eaa.App.Conn.Where("id = ?", eaa.App.TestUser.UUID).Preload("Tag").First(&dbUser)
eaa.Assert.NilError(err)

eaa.Assert.Equal(len(dbUser.Tag), len(tagUUIDs) - 1)

var dbTag models.Tag

err = eaa.App.Conn.Where("id = ?", tagID).Preload("User").First(&dbTag)
eaa.Assert.NilError(err)

eaa.Assert.Equal(len(dbTag.User), 0)
},
},
)
}

0 comments on commit 1ef80b3

Please sign in to comment.