Skip to content

Commit

Permalink
student user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 3, 2024
1 parent 7e359ac commit c775a24
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 70 deletions.
2 changes: 0 additions & 2 deletions backend/src/auth/tokens.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package auth

import (
"fmt"
"time"

"github.com/GenerateNU/sac/backend/src/config"
Expand Down Expand Up @@ -71,7 +70,6 @@ func CreateRefreshToken(id string, refreshExpiresAfter uint, refreshTokenSecret

func SignToken(token *jwt.Token, secret string) (*string, *errors.Error) {
if token == nil || secret == "" {
fmt.Println(token)
return nil, &errors.FailedToSignToken
}

Expand Down
22 changes: 12 additions & 10 deletions backend/tests/api/club_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func AssertSampleClubBodyRespDB(app TestApp, assert *assert.A, resp *http.Respon
return AssertClubBodyRespDB(app, assert, resp, SampleClubFactory(userID))
}

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

var sampleClubUUID uuid.UUID

Expand Down Expand Up @@ -230,7 +230,7 @@ var TestNumClubsRemainsAt1 = func(app TestApp, assert *assert.A, resp *http.Resp
}

func AssertCreateBadClubDataFails(t *testing.T, jsonKey string, badValues []interface{}) {
appAssert, uuid := CreateSampleUser(t, nil)
appAssert, uuid, _ := CreateSampleStudent(t, nil)

for _, badValue := range badValues {
sampleClubPermutation := *SampleClubFactory(uuid)
Expand Down Expand Up @@ -307,9 +307,9 @@ func TestCreateClubFailsOnInvalidLogo(t *testing.T) {
}

func TestUpdateClubWorks(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)
appAssert, studentUUID, clubUUID := CreateSampleClub(t, nil)

updatedClub := SampleClubFactory(userUUID)
updatedClub := SampleClubFactory(studentUUID)
(*updatedClub)["name"] = "Updated Name"
(*updatedClub)["preview"] = "Updated Preview"

Expand All @@ -328,9 +328,9 @@ func TestUpdateClubWorks(t *testing.T) {
}

func TestUpdateClubFailsOnInvalidBody(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)
appAssert, studentUUID, clubUUID := CreateSampleClub(t, nil)

body := SampleClubFactory(userUUID)
body := SampleClubFactory(studentUUID)

for _, invalidData := range []map[string]interface{}{
{"description": "Not a URL"},
Expand Down Expand Up @@ -391,24 +391,26 @@ func TestUpdateClubFailsBadRequest(t *testing.T) {
"null",
}

sampleStudent, rawPassword := SampleStudentFactory()

for _, badRequest := range badRequests {
TestRequest{
Method: fiber.MethodPatch,
Path: fmt.Sprintf("/api/v1/clubs/%s", badRequest),
Body: SampleUserFactory(),
Body: SampleStudentJSONFactory(sampleStudent, rawPassword),
}.TestOnError(t, nil, errors.FailedToValidateID).Close()
}
}

func TestUpdateClubFailsOnClubIdNotExist(t *testing.T) {
appAssert, userUUID := CreateSampleUser(t, nil)
appAssert, studentUUID, _ := CreateSampleStudent(t, nil)

uuid := uuid.New()

TestRequest{
Method: fiber.MethodPatch,
Path: fmt.Sprintf("/api/v1/clubs/%s", uuid),
Body: SampleClubFactory(userUUID),
Body: SampleClubFactory(studentUUID),
}.TestOnErrorAndDB(t, &appAssert,
ErrorWithDBTester{
Error: errors.ClubNotFound,
Expand Down
91 changes: 86 additions & 5 deletions backend/tests/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"strings"
"testing"

"github.com/GenerateNU/sac/backend/src/auth"
"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/backend/src/database"
"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/server"

"github.com/goccy/go-json"
Expand All @@ -28,8 +30,9 @@ import (
type AuthLevel string

var (
SuperUser AuthLevel = "super_user"
LoggedOut AuthLevel = "logged_out"
SuperUser AuthLevel = "super_user"
StudentUser AuthLevel = "sample_user"
LoggedOut AuthLevel = "logged_out"
)

type TestUser struct {
Expand All @@ -39,6 +42,40 @@ type TestUser struct {
RefreshToken string
}

func SampleStudentFactory() (models.User, string) {
password := "1234567890&"
hashedPassword, err := auth.ComputePasswordHash(password)
if err != nil {
panic(err)
}

return models.User{
Role: models.Student,
FirstName: "Jane",
LastName: "Doe",
Email: "[email protected]",
PasswordHash: *hashedPassword,
NUID: "001234567",
College: models.KCCS,
Year: models.Third,
}, password
}

func SampleStudentJSONFactory(sampleStudent models.User, rawPassword string) *map[string]interface{} {
if sampleStudent.Role != models.Student {
panic("User is not a student")
}
return &map[string]interface{}{
"first_name": sampleStudent.FirstName,
"last_name": sampleStudent.LastName,
"email": sampleStudent.Email,
"password": rawPassword,
"nuid": sampleStudent.NUID,
"college": string(sampleStudent.College),
"year": int(sampleStudent.Year),
}
}

func (app *TestApp) Auth(level AuthLevel) {
if level == SuperUser {
superUser, superUserErr := database.SuperUser(app.Settings.SuperUser)
Expand Down Expand Up @@ -82,6 +119,51 @@ func (app *TestApp) Auth(level AuthLevel) {
AccessToken: accessToken,
RefreshToken: refreshToken,
}
} else if level == StudentUser {
studentUser, rawPassword := SampleStudentFactory()

_, err := app.Send(TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/users/",
Body: SampleStudentJSONFactory(studentUser, rawPassword),
})
if err != nil {
panic(err)
}

resp, err := app.Send(TestRequest{
Method: fiber.MethodPost,
Path: "/api/v1/auth/login",
Body: &map[string]interface{}{
"email": studentUser.Email,
"password": rawPassword,
},
})
if err != nil {
panic(err)
}

var accessToken string
var refreshToken string

for _, cookie := range resp.Cookies() {
if cookie.Name == "access_token" {
accessToken = cookie.Value
} else if cookie.Name == "refresh_token" {
refreshToken = cookie.Value
}
}

if accessToken == "" || refreshToken == "" {
panic("Failed to authenticate sample student user")
}

app.TestUser = &TestUser{
Email: studentUser.Email,
Password: rawPassword,
AccessToken: accessToken,
RefreshToken: refreshToken,
}
}
}

Expand Down Expand Up @@ -287,13 +369,12 @@ func (request *TestRequest) testOn(t *testing.T, existingAppAssert *ExistingAppA
assert.Equal(value, respBody[key].(string))

assert.Equal(status, resp.StatusCode)

return appAssert, resp
}

func (request TestRequest) TestOnError(t *testing.T, existingAppAssert *ExistingAppAssert, expectedError errors.Error) ExistingAppAssert {
request.testOn(t, existingAppAssert, expectedError.StatusCode, "error", expectedError.Message)
return *existingAppAssert
appAssert, _ := request.testOn(t, existingAppAssert, expectedError.StatusCode, "error", expectedError.Message)
return appAssert
}

type ErrorWithDBTester struct {
Expand Down
10 changes: 5 additions & 5 deletions backend/tests/api/user_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ type UUIDSlice []uuid.UUID
var testUUID = uuid.New()

func TestCreateUserTagsFailsOnInvalidKey(t *testing.T) {
appAssert, uuid := CreateSampleUser(t, nil)
appAssert, uuid, _ := CreateSampleStudent(t, nil)

invalidBody := []map[string]interface{}{
{
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestCreateUserTagsFailsOnNonExistentUser(t *testing.T) {
}

func TestCreateUserTagsWorks(t *testing.T) {
appAssert, uuid := CreateSampleUser(t, nil)
appAssert, uuid, _ := CreateSampleStudent(t, nil)

// Create a set of tags:
tagUUIDs := CreateSetOfTags(t, appAssert)
Expand All @@ -237,7 +237,7 @@ func TestCreateUserTagsWorks(t *testing.T) {
}

func TestCreateUserTagsNoneAddedIfInvalid(t *testing.T) {
appAssert, uuid := CreateSampleUser(t, nil)
appAssert, uuid, _ := CreateSampleStudent(t, nil)

TestRequest{
Method: fiber.MethodPost,
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestGetUserTagsFailsOnNonExistentUser(t *testing.T) {
}

func TestGetUserTagsReturnsEmptyListWhenNoneAdded(t *testing.T) {
appAssert, uuid := CreateSampleUser(t, nil)
appAssert, uuid, _ := CreateSampleStudent(t, nil)

TestRequest{
Method: fiber.MethodGet,
Expand All @@ -293,7 +293,7 @@ func TestGetUserTagsReturnsEmptyListWhenNoneAdded(t *testing.T) {
}

func TestGetUserTagsReturnsCorrectList(t *testing.T) {
appAssert, uuid := CreateSampleUser(t, nil)
appAssert, uuid, _ := CreateSampleStudent(t, nil)

// Create a set of tags:
tagUUIDs := CreateSetOfTags(t, appAssert)
Expand Down
Loading

0 comments on commit c775a24

Please sign in to comment.