-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1d7e16
commit ed7c035
Showing
4 changed files
with
339 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,60 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/GenerateNU/sac/backend/src/models" | ||
h "github.com/GenerateNU/sac/backend/tests/api/helpers" | ||
"github.com/goccy/go-json" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func TestClubFollowerWorks(t *testing.T) { | ||
appAssert := h.InitTest(t) | ||
|
||
appAssert, _, clubUUID := CreateSampleClub(appAssert) | ||
|
||
appAssert.TestOnStatus( | ||
h.TestRequest{ | ||
Method: fiber.MethodPost, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", clubUUID), | ||
Role: &models.Super, | ||
TestUserIDReplaces: h.StringToPointer(":userID"), | ||
}, | ||
fiber.StatusCreated, | ||
).TestOnStatusAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodGet, | ||
Path: fmt.Sprintf("/api/v1/clubs/%s/followers", clubUUID), | ||
Role: &models.Super, | ||
}, | ||
h.TesterWithStatus{ | ||
Status: fiber.StatusOK, | ||
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) { | ||
var followers []models.User | ||
|
||
err := json.NewDecoder(resp.Body).Decode(&followers) | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(1, len(followers)) | ||
|
||
var club models.Club | ||
|
||
err = eaa.App.Conn.Where("id = ?", clubUUID).First(&club).Error | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
var dbFollowers []models.User | ||
|
||
err = eaa.App.Conn.Model(&club).Association("Follower").Find(&dbFollowers) | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(len(dbFollowers), len(followers)) | ||
}, | ||
}, | ||
).Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,288 @@ | ||
package tests | ||
|
||
// func TestCreateFollowingWorks(t *testing.T) { | ||
// appAssert, userUUID, clubUUID := CreateSampleClub(h.InitTest(t)) | ||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
// appAssert.TestOnStatusAndTester( | ||
// h.TestRequest{ | ||
// Method: fiber.MethodPost, | ||
// Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID), | ||
// }, | ||
// h.TesterWithStatus{ | ||
// Status: fiber.StatusCreated, | ||
// Tester: func(eaa h.ExistingAppAssert, resp *http.Response) { | ||
// var user models.User | ||
"github.com/GenerateNU/sac/backend/src/errors" | ||
"github.com/GenerateNU/sac/backend/src/models" | ||
h "github.com/GenerateNU/sac/backend/tests/api/helpers" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/google/uuid" | ||
) | ||
|
||
// err := eaa.App.Conn.Where("id = ?", userUUID).Preload("Follower").First(&user) | ||
func TestCreateFollowingWorks(t *testing.T) { | ||
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t)) | ||
|
||
// eaa.Assert.NilError(err) | ||
appAssert.TestOnStatusAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodPost, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", 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 | ||
|
||
// eaa.Assert.Equal(1, len(user.Follower)) | ||
err := eaa.App.Conn.Where("id = ?", eaa.App.TestUser.UUID).Preload("Follower").First(&user) | ||
|
||
// eaa.Assert.Equal(clubUUID, user.Follower[0].ID) | ||
eaa.Assert.NilError(err) | ||
|
||
// var club models.Club | ||
eaa.Assert.Equal(1, len(user.Follower)) | ||
|
||
// err = eaa.App.Conn.Where("id = ?", clubUUID).Preload("Follower").First(&club) | ||
eaa.Assert.Equal(clubUUID, user.Follower[0].ID) | ||
|
||
// eaa.Assert.NilError(err) | ||
var club models.Club | ||
|
||
// eaa.Assert.Equal(1, len(club.Follower)) | ||
err = eaa.App.Conn.Where("id = ?", clubUUID).Preload("Follower").First(&club) | ||
|
||
// eaa.Assert.Equal(userUUID, club.Follower[0].ID) | ||
// }, | ||
// }, | ||
// ).Close() | ||
// } | ||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(1, len(club.Follower)) | ||
|
||
eaa.Assert.Equal(eaa.App.TestUser.UUID, club.Follower[0].ID) | ||
}, | ||
}, | ||
).Close() | ||
} | ||
|
||
func TestCreateFollowingFailsClubIdNotExists(t *testing.T) { | ||
appAssert, _, _ := CreateSampleClub(h.InitTest(t)) | ||
|
||
uuid := uuid.New() | ||
|
||
appAssert.TestOnErrorAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodPost, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", 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 TestCreateFollowingFailsUserIdNotExists(t *testing.T) { | ||
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t)) | ||
|
||
uuid := uuid.New() | ||
|
||
appAssert.TestOnErrorAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodPost, | ||
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", uuid, clubUUID), | ||
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 TestDeleteFollowingWorks(t *testing.T) { | ||
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t)) | ||
|
||
appAssert.TestOnStatus( | ||
h.TestRequest{ | ||
Method: fiber.MethodPost, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", clubUUID), | ||
Role: &models.Super, | ||
TestUserIDReplaces: h.StringToPointer(":userID"), | ||
}, | ||
fiber.StatusCreated, | ||
).TestOnStatusAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodDelete, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", 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("Follower").First(&user) | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(0, len(user.Follower)) | ||
|
||
var club models.Club | ||
|
||
err = eaa.App.Conn.Where("id = ?", clubUUID).Preload("Follower").First(&club) | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(0, len(club.Follower)) | ||
}, | ||
}, | ||
).Close() | ||
} | ||
|
||
func TestDeleteFollowingNotFollowing(t *testing.T) { | ||
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t)) | ||
|
||
appAssert.TestOnStatusAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodDelete, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", 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("Follower").First(&user) | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(0, len(user.Follower)) | ||
|
||
var club models.Club | ||
|
||
err = eaa.App.Conn.Where("id = ?", clubUUID).Preload("Follower").First(&club) | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(0, len(club.Follower)) | ||
}, | ||
}, | ||
).Close() | ||
} | ||
|
||
func TestDeleteFollowingFailsClubIdNotExists(t *testing.T) { | ||
appAssert, _, _ := CreateSampleClub(h.InitTest(t)) | ||
|
||
uuid := uuid.New() | ||
|
||
appAssert.TestOnErrorAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodDelete, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", 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 TestDeleteFollowingFailsUserIdNotExists(t *testing.T) { | ||
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t)) | ||
|
||
uuid := uuid.New() | ||
|
||
appAssert.TestOnErrorAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodDelete, | ||
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", uuid, clubUUID), | ||
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 TestGetFollowingWorks(t *testing.T) { | ||
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t)) | ||
|
||
appAssert.TestOnStatus( | ||
h.TestRequest{ | ||
Method: fiber.MethodPost, | ||
Path: fmt.Sprintf("/api/v1/users/:userID/follower/%s", clubUUID), | ||
Role: &models.Super, | ||
TestUserIDReplaces: h.StringToPointer(":userID"), | ||
}, | ||
fiber.StatusCreated, | ||
).TestOnStatusAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodGet, | ||
Path: "/api/v1/users/:userID/follower", | ||
Role: &models.Super, | ||
TestUserIDReplaces: h.StringToPointer(":userID"), | ||
}, | ||
h.TesterWithStatus{ | ||
Status: fiber.StatusOK, | ||
Tester: func(eaa h.ExistingAppAssert, resp *http.Response) { | ||
var clubs []models.Club | ||
|
||
err := json.NewDecoder(resp.Body).Decode(&clubs) | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(1, len(clubs)) | ||
|
||
var dbClubs []models.Club | ||
|
||
err = eaa.App.Conn.Where("id = ?", clubUUID).Preload("Follower").First(&dbClubs).Error | ||
|
||
eaa.Assert.NilError(err) | ||
|
||
eaa.Assert.Equal(1, len(clubs)) | ||
}, | ||
}, | ||
).Close() | ||
} | ||
|
||
func TestGetFollowingFailsUserIdNotExists(t *testing.T) { | ||
appAssert, _, _ := CreateSampleClub(h.InitTest(t)) | ||
|
||
uuid := uuid.New() | ||
|
||
appAssert.TestOnErrorAndTester( | ||
h.TestRequest{ | ||
Method: fiber.MethodGet, | ||
Path: fmt.Sprintf("/api/v1/users/%s/follower", 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() | ||
} |