-
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
7e359ac
commit c775a24
Showing
5 changed files
with
165 additions
and
70 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 |
---|---|---|
|
@@ -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" | ||
|
@@ -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 { | ||
|
@@ -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) | ||
|
@@ -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, | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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 { | ||
|
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
Oops, something went wrong.