From 3e8dec3dac787d87dd33eef9f6bc22d6dc978f93 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Wed, 3 Jan 2024 00:03:35 +0700 Subject: [PATCH] feat: implement validate service --- internal/constant/error.constant.go | 1 + internal/service/auth/auth.service.go | 11 +++- internal/service/auth/auth.service_test.go | 65 ++++++++++++++++++++-- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/internal/constant/error.constant.go b/internal/constant/error.constant.go index 61d157d..8bc79f9 100644 --- a/internal/constant/error.constant.go +++ b/internal/constant/error.constant.go @@ -1,5 +1,6 @@ package constant +const InvalidTokenErrorMessage = "Invalid token" const IncorrectEmailPasswordErrorMessage = "Incorrect email or password" const DuplicateEmailErrorMessage = "Duplicate email" const InternalServerErrorMessage = "Internal server error" diff --git a/internal/service/auth/auth.service.go b/internal/service/auth/auth.service.go index 2467af7..d6bd0b2 100644 --- a/internal/service/auth/auth.service.go +++ b/internal/service/auth/auth.service.go @@ -35,8 +35,15 @@ func NewService(authRepo auth.Repository, userRepo user.Repository, tokenService } func (s *serviceImpl) Validate(_ context.Context, request *authProto.ValidateRequest) (*authProto.ValidateResponse, error) { - // call tokenService.Validate - return nil, nil + userCredential, err := s.tokenService.Validate(request.Token) + if err != nil { + return nil, status.Error(codes.Unauthenticated, constant.InvalidTokenErrorMessage) + } + + return &authProto.ValidateResponse{ + UserId: userCredential.UserID, + Role: string(userCredential.Role), + }, nil } func (s *serviceImpl) RefreshToken(_ context.Context, request *authProto.RefreshTokenRequest) (*authProto.RefreshTokenResponse, error) { diff --git a/internal/service/auth/auth.service_test.go b/internal/service/auth/auth.service_test.go index 48d5589..d31eba9 100644 --- a/internal/service/auth/auth.service_test.go +++ b/internal/service/auth/auth.service_test.go @@ -26,10 +26,11 @@ import ( type AuthServiceTest struct { suite.Suite - ctx context.Context - signupRequest *authProto.SignUpRequest - signInRequest *authProto.SignInRequest - signOutRequest *authProto.SignOutRequest + ctx context.Context + signupRequest *authProto.SignUpRequest + signInRequest *authProto.SignInRequest + signOutRequest *authProto.SignOutRequest + validateRequest *authProto.ValidateRequest } func TestAuthService(t *testing.T) { @@ -51,11 +52,15 @@ func (t *AuthServiceTest) SetupTest() { signOutRequest := &authProto.SignOutRequest{ Token: faker.Word(), } + validateRequest := &authProto.ValidateRequest{ + Token: faker.Word(), + } t.ctx = ctx t.signupRequest = signupRequest t.signInRequest = signInRequest t.signOutRequest = signOutRequest + t.validateRequest = validateRequest } func (t *AuthServiceTest) TestSignupSuccess() { @@ -377,9 +382,57 @@ func (t *AuthServiceTest) TestSignInCreateCredentialFailed() { assert.Equal(t.T(), expected.Error(), err.Error()) } -func (t *AuthServiceTest) TestValidateSuccess() {} +func (t *AuthServiceTest) TestValidateSuccess() { + userCredential := &tokenDto.UserCredential{ + UserID: faker.UUIDDigit(), + Role: constant.USER, + AuthSessionID: faker.UUIDDigit(), + RefreshToken: faker.UUIDDigit(), + } + + expected := &authProto.ValidateResponse{ + UserId: userCredential.UserID, + Role: string(userCredential.Role), + } + + controller := gomock.NewController(t.T()) + + authRepo := mock_auth.NewMockRepository(controller) + userRepo := user.UserRepositoryMock{} + tokenService := token.TokenServiceMock{} + bcryptUtil := utils.BcryptUtilMock{} + + tokenService.On("Validate", t.validateRequest.Token).Return(userCredential, nil) + + authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil) + actual, err := authSvc.Validate(t.ctx, t.validateRequest) + + assert.Nil(t.T(), err) + assert.Equal(t.T(), expected, actual) +} + +func (t *AuthServiceTest) TestValidateFailed() { + validateErr := errors.New("invalid token") + expected := status.Error(codes.Unauthenticated, constant.InvalidTokenErrorMessage) + + controller := gomock.NewController(t.T()) + + authRepo := mock_auth.NewMockRepository(controller) + userRepo := user.UserRepositoryMock{} + tokenService := token.TokenServiceMock{} + bcryptUtil := utils.BcryptUtilMock{} + + tokenService.On("Validate", t.validateRequest.Token).Return(nil, validateErr) + + authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil) + actual, err := authSvc.Validate(t.ctx, t.validateRequest) -func (t *AuthServiceTest) TestValidateFailed() {} + st, ok := status.FromError(err) + assert.Nil(t.T(), actual) + assert.Equal(t.T(), codes.Unauthenticated, st.Code()) + assert.True(t.T(), ok) + assert.Equal(t.T(), expected.Error(), err.Error()) +} func (t *AuthServiceTest) TestRefreshTokenSuccess() {}