Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JOH-26] Validate service #12

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/constant/error.constant.go
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 9 additions & 2 deletions internal/service/auth/auth.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
65 changes: 59 additions & 6 deletions internal/service/auth/auth.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {}

Expand Down
Loading