From c616f9b823112dba1216dc08fb1bf458a82e127d Mon Sep 17 00:00:00 2001 From: Jessica Tarra Date: Wed, 6 Dec 2023 20:23:04 -0300 Subject: [PATCH] Update auth service By extract user validation into separate file. Also update mocks. --- ms/auth/domain/mocks/Appl.go | 14 +----------- ms/auth/domain/mocks/TokenInterface.go | 6 +---- ms/auth/domain/mocks/TokenRepository.go | 14 +----------- ms/auth/domain/mocks/UserRepository.go | 22 +------------------ ms/auth/service/service.go | 24 -------------------- ms/auth/service/validation.go | 29 +++++++++++++++++++++++++ 6 files changed, 33 insertions(+), 76 deletions(-) create mode 100644 ms/auth/service/validation.go diff --git a/ms/auth/domain/mocks/Appl.go b/ms/auth/domain/mocks/Appl.go index 2bbf8b3..249ae0a 100644 --- a/ms/auth/domain/mocks/Appl.go +++ b/ms/auth/domain/mocks/Appl.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.38.0. DO NOT EDIT. +// Code generated by mockery v2.33.2. DO NOT EDIT. package mocks @@ -16,10 +16,6 @@ type Appl struct { func (_m *Appl) ActivateUseCase(tokenPlainText string) (*domain.User, error) { ret := _m.Called(tokenPlainText) - if len(ret) == 0 { - panic("no return value specified for ActivateUseCase") - } - var r0 *domain.User var r1 error if rf, ok := ret.Get(0).(func(string) (*domain.User, error)); ok { @@ -46,10 +42,6 @@ func (_m *Appl) ActivateUseCase(tokenPlainText string) (*domain.User, error) { func (_m *Appl) CreateUseCase(input domain.CreateUserRequest) (*domain.User, error) { ret := _m.Called(input) - if len(ret) == 0 { - panic("no return value specified for CreateUseCase") - } - var r0 *domain.User var r1 error if rf, ok := ret.Get(0).(func(domain.CreateUserRequest) (*domain.User, error)); ok { @@ -76,10 +68,6 @@ func (_m *Appl) CreateUseCase(input domain.CreateUserRequest) (*domain.User, err func (_m *Appl) GetByEmailUseCase(input domain.CreateUserRequest) (*domain.User, error) { ret := _m.Called(input) - if len(ret) == 0 { - panic("no return value specified for GetByEmailUseCase") - } - var r0 *domain.User var r1 error if rf, ok := ret.Get(0).(func(domain.CreateUserRequest) (*domain.User, error)); ok { diff --git a/ms/auth/domain/mocks/TokenInterface.go b/ms/auth/domain/mocks/TokenInterface.go index d3ac042..3750c43 100644 --- a/ms/auth/domain/mocks/TokenInterface.go +++ b/ms/auth/domain/mocks/TokenInterface.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.38.0. DO NOT EDIT. +// Code generated by mockery v2.33.2. DO NOT EDIT. package mocks @@ -18,10 +18,6 @@ type TokenInterface struct { func (_m *TokenInterface) GenerateToken(userID int64, ttl time.Duration, scope string) (*domain.Token, error) { ret := _m.Called(userID, ttl, scope) - if len(ret) == 0 { - panic("no return value specified for GenerateToken") - } - var r0 *domain.Token var r1 error if rf, ok := ret.Get(0).(func(int64, time.Duration, string) (*domain.Token, error)); ok { diff --git a/ms/auth/domain/mocks/TokenRepository.go b/ms/auth/domain/mocks/TokenRepository.go index be1603b..76cf2c7 100644 --- a/ms/auth/domain/mocks/TokenRepository.go +++ b/ms/auth/domain/mocks/TokenRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.38.0. DO NOT EDIT. +// Code generated by mockery v2.33.2. DO NOT EDIT. package mocks @@ -18,10 +18,6 @@ type TokenRepository struct { func (_m *TokenRepository) DeleteAllForUser(scope string, userID int64) error { ret := _m.Called(scope, userID) - if len(ret) == 0 { - panic("no return value specified for DeleteAllForUser") - } - var r0 error if rf, ok := ret.Get(0).(func(string, int64) error); ok { r0 = rf(scope, userID) @@ -36,10 +32,6 @@ func (_m *TokenRepository) DeleteAllForUser(scope string, userID int64) error { func (_m *TokenRepository) Insert(token *domain.Token) error { ret := _m.Called(token) - if len(ret) == 0 { - panic("no return value specified for Insert") - } - var r0 error if rf, ok := ret.Get(0).(func(*domain.Token) error); ok { r0 = rf(token) @@ -54,10 +46,6 @@ func (_m *TokenRepository) Insert(token *domain.Token) error { func (_m *TokenRepository) New(userID int64, ttl time.Duration, scope string) (*domain.Token, error) { ret := _m.Called(userID, ttl, scope) - if len(ret) == 0 { - panic("no return value specified for New") - } - var r0 *domain.Token var r1 error if rf, ok := ret.Get(0).(func(int64, time.Duration, string) (*domain.Token, error)); ok { diff --git a/ms/auth/domain/mocks/UserRepository.go b/ms/auth/domain/mocks/UserRepository.go index 899b775..0b6086a 100644 --- a/ms/auth/domain/mocks/UserRepository.go +++ b/ms/auth/domain/mocks/UserRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.38.0. DO NOT EDIT. +// Code generated by mockery v2.33.2. DO NOT EDIT. package mocks @@ -16,10 +16,6 @@ type UserRepository struct { func (_m *UserRepository) GetForToken(tokenScope string, tokenPlaintext string) (*domain.User, error) { ret := _m.Called(tokenScope, tokenPlaintext) - if len(ret) == 0 { - panic("no return value specified for GetForToken") - } - var r0 *domain.User var r1 error if rf, ok := ret.Get(0).(func(string, string) (*domain.User, error)); ok { @@ -46,10 +42,6 @@ func (_m *UserRepository) GetForToken(tokenScope string, tokenPlaintext string) func (_m *UserRepository) GetUserByEmail(email string) (*domain.User, error) { ret := _m.Called(email) - if len(ret) == 0 { - panic("no return value specified for GetUserByEmail") - } - var r0 *domain.User var r1 error if rf, ok := ret.Get(0).(func(string) (*domain.User, error)); ok { @@ -76,10 +68,6 @@ func (_m *UserRepository) GetUserByEmail(email string) (*domain.User, error) { func (_m *UserRepository) GetUserById(id int64) (*domain.User, error) { ret := _m.Called(id) - if len(ret) == 0 { - panic("no return value specified for GetUserById") - } - var r0 *domain.User var r1 error if rf, ok := ret.Get(0).(func(int64) (*domain.User, error)); ok { @@ -106,10 +94,6 @@ func (_m *UserRepository) GetUserById(id int64) (*domain.User, error) { func (_m *UserRepository) InsertNewUser(user *domain.User) error { ret := _m.Called(user) - if len(ret) == 0 { - panic("no return value specified for InsertNewUser") - } - var r0 error if rf, ok := ret.Get(0).(func(*domain.User) error); ok { r0 = rf(user) @@ -124,10 +108,6 @@ func (_m *UserRepository) InsertNewUser(user *domain.User) error { func (_m *UserRepository) UpdateUser(user *domain.User) error { ret := _m.Called(user) - if len(ret) == 0 { - panic("no return value specified for UpdateUser") - } - var r0 error if rf, ok := ret.Get(0).(func(*domain.User) error); ok { r0 = rf(user) diff --git a/ms/auth/service/service.go b/ms/auth/service/service.go index efbe1bb..2b640bf 100644 --- a/ms/auth/service/service.go +++ b/ms/auth/service/service.go @@ -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" @@ -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") diff --git a/ms/auth/service/validation.go b/ms/auth/service/validation.go new file mode 100644 index 0000000..7891a2f --- /dev/null +++ b/ms/auth/service/validation.go @@ -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") +}