Skip to content

Commit

Permalink
Update auth service
Browse files Browse the repository at this point in the history
By extract user validation into separate file. Also update mocks.
  • Loading branch information
jessicatarra committed Dec 6, 2023
1 parent 8fba890 commit c616f9b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 76 deletions.
14 changes: 1 addition & 13 deletions ms/auth/domain/mocks/Appl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions ms/auth/domain/mocks/TokenInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions ms/auth/domain/mocks/TokenRepository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 1 addition & 21 deletions ms/auth/domain/mocks/UserRepository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions ms/auth/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package service

import (
"github.com/jessicatarra/greenlight/internal/errors"
"github.com/jessicatarra/greenlight/internal/password"
"github.com/jessicatarra/greenlight/internal/request"
"github.com/jessicatarra/greenlight/internal/response"
"github.com/jessicatarra/greenlight/internal/utils"
"github.com/jessicatarra/greenlight/ms/auth/domain"
"github.com/julienschmidt/httprouter"
"net/http"
Expand Down Expand Up @@ -64,28 +62,6 @@ func (r *resource) create(res http.ResponseWriter, req *http.Request) {
}
}

func ValidateUser(input domain.CreateUserRequest, existingUser *domain.User) {
input.Validator.CheckField(input.Name != "", "name", "must be provided")
input.Validator.CheckField(len(input.Name) <= 500, "name", "must not be more than 500 bytes long")

ValidateEmail(input, existingUser)

ValidatePassword(input)
}

func ValidatePassword(input domain.CreateUserRequest) {
input.Validator.CheckField(input.Password != "", "Password", "Password is required")
input.Validator.CheckField(len(input.Password) >= 8, "Password", "Password is too short")
input.Validator.CheckField(len(input.Password) <= 72, "Password", "Password is too long")
input.Validator.CheckField(utils.NotIn(input.Password, password.CommonPasswords...), "Password", "Password is too common")
}

func ValidateEmail(input domain.CreateUserRequest, existingUser *domain.User) {
input.Validator.CheckField(input.Email != "", "Email", "Email is required")
input.Validator.CheckField(utils.Matches(input.Email, utils.RgxEmail), "Email", "Must be a valid email address")
input.Validator.CheckField(existingUser == nil, "Email", "Email is already in use")
}

func (r *resource) activate(res http.ResponseWriter, req *http.Request) {
//TODO implement me
panic("implement me")
Expand Down
29 changes: 29 additions & 0 deletions ms/auth/service/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package service

import (
"github.com/jessicatarra/greenlight/internal/password"
"github.com/jessicatarra/greenlight/internal/utils"
"github.com/jessicatarra/greenlight/ms/auth/domain"
)

func ValidateUser(input domain.CreateUserRequest, existingUser *domain.User) {
input.Validator.CheckField(input.Name != "", "name", "must be provided")
input.Validator.CheckField(len(input.Name) <= 500, "name", "must not be more than 500 bytes long")

ValidateEmail(input, existingUser)

ValidatePassword(input)
}

func ValidatePassword(input domain.CreateUserRequest) {
input.Validator.CheckField(input.Password != "", "Password", "Password is required")
input.Validator.CheckField(len(input.Password) >= 8, "Password", "Password is too short")
input.Validator.CheckField(len(input.Password) <= 72, "Password", "Password is too long")
input.Validator.CheckField(utils.NotIn(input.Password, password.CommonPasswords...), "Password", "Password is too common")
}

func ValidateEmail(input domain.CreateUserRequest, existingUser *domain.User) {
input.Validator.CheckField(input.Email != "", "Email", "Email is required")
input.Validator.CheckField(utils.Matches(input.Email, utils.RgxEmail), "Email", "Must be a valid email address")
input.Validator.CheckField(existingUser == nil, "Email", "Email is already in use")
}

0 comments on commit c616f9b

Please sign in to comment.