Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolved previous test + updated lib #739

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/entities/clubs/tag/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func ClubTag(clubParams types.RouteParams) {

clubTags := clubParams.Router.Group("/tags")

// api/v1/clubs/:clubID/tags/*
clubTags.Get("/", clubTagController.GetClubTags)
clubTags.Post("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubTagController.CreateClubTags)
clubTags.Delete("/:tagID", clubParams.AuthMiddleware.ClubAuthorizeById, clubTagController.DeleteClubTag)
Expand Down
180 changes: 179 additions & 1 deletion backend/tests/api/club_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,57 @@ import (
"testing"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/errors"
h "github.com/GenerateNU/sac/backend/tests/api/helpers"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

func TestCreateMembershipWorks(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t))

appAssert.TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodPost,
Path: fmt.Sprintf("/api/v1/clubs/%s/members/:userID", clubUUID),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.TesterWithStatus{
Status: fiber.StatusCreated,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var user models.User

err := eaa.App.Conn.Where("id = ?", eaa.App.TestUser.UUID).Preload("Member").First(&user)

eaa.Assert.NilError(err)

eaa.Assert.Equal(2, len(user.Member)) // SAC Super Club and the one just added

eaa.Assert.Equal(clubUUID, user.Member[1].ID) // second club AKA the one just added

var club models.Club

err = eaa.App.Conn.Where("id = ?", clubUUID).Preload("Member").First(&club)

eaa.Assert.NilError(err)

eaa.Assert.Equal(1, len(club.Member))

eaa.Assert.Equal(eaa.App.TestUser.UUID, club.Member[0].ID)
},
},
).Close()
}

func TestClubMemberWorks(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t))

appAssert.TestOnStatus(
h.TestRequest{
Method: fiber.MethodPost,
Path: fmt.Sprintf("/api/v1/users/:userID/member/%s", clubUUID),
Path: fmt.Sprintf("/api/v1/clubs/%s/members/:userID", clubUUID),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
Expand Down Expand Up @@ -56,3 +95,142 @@ func TestClubMemberWorks(t *testing.T) {
},
).Close()
}

func TestCreateMembershipFailsClubIdNotExists(t *testing.T) {
appAssert, _, _ := CreateSampleClub(h.InitTest(t))

uuid := uuid.New()

appAssert.TestOnErrorAndTester(
h.TestRequest{
Method: fiber.MethodPost,
Path: fmt.Sprintf("/api/v1/clubs/%s/members/:userID", uuid),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.ErrorWithTester{
Error: errors.ClubNotFound,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var club models.Club

err := eaa.App.Conn.Where("id = ?", uuid).First(&club).Error

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

func TestCreateMembershipFailsUserIdNotExists(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t))

uuid := uuid.New()

appAssert.TestOnErrorAndTester(
h.TestRequest{
Method: fiber.MethodPost,
Path: fmt.Sprintf("/api/v1/clubs/%s/members/%s", clubUUID, uuid),
Role: &models.Super,
},
h.ErrorWithTester{
Error: errors.UserNotFound,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var user models.User

err := eaa.App.Conn.Where("id = ?", uuid).First(&user).Error

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

func TestDeleteMembershipWorks(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t))

appAssert.TestOnStatus(
h.TestRequest{
Method: fiber.MethodPost,
Path: fmt.Sprintf("/api/v1/clubs/%s/members/:userID", clubUUID),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
fiber.StatusCreated,
).TestOnStatusAndTester(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/clubs/%s/members/:userID", clubUUID),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.TesterWithStatus{
Status: fiber.StatusNoContent,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var user models.User

err := eaa.App.Conn.Where("id = ?", eaa.App.TestUser.UUID).Preload("Member").First(&user)

eaa.Assert.NilError(err)

eaa.Assert.Equal(1, len(user.Member)) // SAC Super Club

var club models.Club

err = eaa.App.Conn.Where("id = ?", clubUUID).Preload("Member").First(&club)

eaa.Assert.NilError(err)

eaa.Assert.Equal(0, len(club.Member))
},
},
).Close()
}

func TestDeleteMembershipFailsClubIdNotExists(t *testing.T) {
appAssert, _, _ := CreateSampleClub(h.InitTest(t))

uuid := uuid.New()

appAssert.TestOnErrorAndTester(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/clubs/%s/members/:userID", uuid),
Role: &models.Super,
TestUserIDReplaces: h.StringToPointer(":userID"),
},
h.ErrorWithTester{
Error: errors.ClubNotFound,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var club models.Club

err := eaa.App.Conn.Where("id = ?", uuid).First(&club).Error

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

func TestDeleteMembershipFailsUserIdNotExists(t *testing.T) {
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t))

uuid := uuid.New()

appAssert.TestOnErrorAndTester(
h.TestRequest{
Method: fiber.MethodDelete,
Path: fmt.Sprintf("/api/v1/clubs/%s/members/%s", clubUUID, uuid),
Role: &models.Super,
},
h.ErrorWithTester{
Error: errors.UserNotFound,
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) {
var user models.User

err := eaa.App.Conn.Where("id = ?", uuid).First(&user).Error

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