Skip to content

Commit

Permalink
done.
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 14, 2024
1 parent b1d7e16 commit ed7c035
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 26 deletions.
2 changes: 1 addition & 1 deletion backend/src/transactions/club_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GetClubFollowers(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]mod

var users []models.User

if err := db.Model(&club).Association("Followers").Find(&users); err != nil {
if err := db.Model(&club).Association("Follower").Find(&users); err != nil {
return nil, &errors.FailedToGetClubFollowers
}

Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/user_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Er
return &errors.ClubNotFound
}

if err := db.Model(&user).Association("Follower").Append(&club); err != nil {
user.Follower = append(user.Follower, *club)

if err := db.Model(&user).Association("Follower").Replace(user.Follower); err != nil {
return &errors.FailedToUpdateUser
}

Expand Down
59 changes: 59 additions & 0 deletions backend/tests/api/club_follower_test.go
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()
}
300 changes: 276 additions & 24 deletions backend/tests/api/user_follower_test.go
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()
}

0 comments on commit ed7c035

Please sign in to comment.