Skip to content

Commit

Permalink
removed redundant auth level | reuse models.UserRole
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 3, 2024
1 parent c775a24 commit c178ee6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion backend/src/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/google/uuid"

type UserRole string

const (
var (
Super UserRole = "super"
Student UserRole = "student"
)
Expand Down
28 changes: 10 additions & 18 deletions backend/tests/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ import (
"gorm.io/gorm"
)

type AuthLevel string

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

type TestUser struct {
Email string
Password string
Expand Down Expand Up @@ -76,8 +68,8 @@ func SampleStudentJSONFactory(sampleStudent models.User, rawPassword string) *ma
}
}

func (app *TestApp) Auth(level AuthLevel) {
if level == SuperUser {
func (app *TestApp) Auth(role models.UserRole) {
if role == models.Super {
superUser, superUserErr := database.SuperUser(app.Settings.SuperUser)
if superUserErr != nil {
panic(superUserErr)
Expand Down Expand Up @@ -119,7 +111,7 @@ func (app *TestApp) Auth(level AuthLevel) {
AccessToken: accessToken,
RefreshToken: refreshToken,
}
} else if level == StudentUser {
} else if role == models.Student {
studentUser, rawPassword := SampleStudentFactory()

_, err := app.Send(TestRequest{
Expand Down Expand Up @@ -275,11 +267,11 @@ func (eaa ExistingAppAssert) Close() {
}

type TestRequest struct {
Method string
Path string
Body *map[string]interface{}
Headers *map[string]string
AuthLevel *AuthLevel
Method string
Path string
Body *map[string]interface{}
Headers *map[string]string
Role *models.UserRole
}

func (app TestApp) Send(request TestRequest) (*http.Response, error) {
Expand Down Expand Up @@ -331,8 +323,8 @@ func (request TestRequest) Test(t *testing.T, existingAppAssert *ExistingAppAsse
if existingAppAssert == nil {
app, assert := InitTest(t)

if request.AuthLevel != nil {
app.Auth(*request.AuthLevel)
if request.Role != nil {
app.Auth(*request.Role)
}
existingAppAssert = &ExistingAppAssert{
App: app,
Expand Down
12 changes: 6 additions & 6 deletions backend/tests/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

func TestGetUsersWorksForSuper(t *testing.T) {
TestRequest{
Method: fiber.MethodGet,
Path: "/api/v1/users/",
AuthLevel: &SuperUser,
Method: fiber.MethodGet,
Path: "/api/v1/users/",
Role: &models.Super,
}.TestOnStatusAndDB(t, nil,
DBTesterWithStatus{
Status: fiber.StatusOK,
Expand Down Expand Up @@ -61,9 +61,9 @@ func TestGetUsersWorksForSuper(t *testing.T) {

func TestGetUsersFailsForStudent(t *testing.T) {
TestRequest{
Method: fiber.MethodGet,
Path: "/api/v1/users/",
AuthLevel: &StudentUser,
Method: fiber.MethodGet,
Path: "/api/v1/users/",
Role: &models.Student,
}.TestOnError(t, nil, errors.Unauthorized).Close()
}

Expand Down

0 comments on commit c178ee6

Please sign in to comment.