Skip to content

Commit

Permalink
feat: implement signout
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitiwat-owen committed Dec 29, 2023
1 parent ce783fb commit 5f931df
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/internal/service/auth/auth.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,21 @@ func (s *serviceImpl) SignIn(_ context.Context, request *authProto.SignInRequest
}

func (s *serviceImpl) SignOut(_ context.Context, request *authProto.SignOutRequest) (*authProto.SignOutResponse, error) {
// validate
// remove cache
userCredential, err := s.tokenService.Validate(request.Token)
if err != nil {
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

err = s.tokenService.RemoveTokenCache(userCredential.RefreshToken)
if err != nil {
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

err = s.authRepo.Delete(userCredential.AuthSessionID)
if err != nil {
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

return &authProto.SignOutResponse{IsSuccess: true}, nil
}

Expand Down
107 changes: 107 additions & 0 deletions src/internal/service/auth/auth.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token"
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
mock_auth "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/auth"
"github.com/isd-sgcu/johnjud-auth/src/mocks/repository/user"
Expand Down Expand Up @@ -388,10 +389,116 @@ func (t *AuthServiceTest) TestRefreshTokenCreateCredentialFailed() {}
func (t *AuthServiceTest) TestRefreshTokenUpdateTokenFailed() {}

func (t *AuthServiceTest) TestSignOutSuccess() {
userCredential := &tokenDto.UserCredential{
UserID: faker.UUIDDigit(),
Role: constant.USER,
AuthSessionID: faker.UUIDDigit(),
RefreshToken: faker.UUIDDigit(),
}

expected := &authProto.SignOutResponse{
IsSuccess: true,
}

controller := gomock.NewController(t.T())

authRepo := mock_auth.NewMockRepository(controller)
userRepo := user.UserRepositoryMock{}
tokenService := token.TokenServiceMock{}
bcryptUtil := utils.BcryptUtilMock{}

tokenService.On("Validate", t.signOutRequest.Token).Return(userCredential, nil)
tokenService.On("RemoveTokenCache", userCredential.RefreshToken).Return(nil)
authRepo.EXPECT().Delete(userCredential.AuthSessionID).Return(nil)

authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil)
actual, err := authSvc.SignOut(t.ctx, t.signOutRequest)

assert.Nil(t.T(), err)
assert.Equal(t.T(), expected, actual)
}

func (t *AuthServiceTest) TestSignOutValidateFailed() {
validateErr := errors.New("internal server error")
expected := status.Error(codes.Internal, constant.InternalServerErrorMessage)
controller := gomock.NewController(t.T())

authRepo := mock_auth.NewMockRepository(controller)
userRepo := user.UserRepositoryMock{}
tokenService := token.TokenServiceMock{}
bcryptUtil := utils.BcryptUtilMock{}

tokenService.On("Validate", t.signOutRequest.Token).Return(nil, validateErr)

authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil)
actual, err := authSvc.SignOut(t.ctx, t.signOutRequest)

st, ok := status.FromError(err)
assert.Nil(t.T(), actual)
assert.Equal(t.T(), codes.Internal, st.Code())
assert.True(t.T(), ok)
assert.Equal(t.T(), expected.Error(), err.Error())
}

func (t *AuthServiceTest) TestSignOutRemoveTokenCacheFailed() {
userCredential := &tokenDto.UserCredential{
UserID: faker.UUIDDigit(),
Role: constant.USER,
AuthSessionID: faker.UUIDDigit(),
RefreshToken: faker.UUIDDigit(),
}
removeTokenErr := errors.New("internal server error")

expected := status.Error(codes.Internal, constant.InternalServerErrorMessage)

controller := gomock.NewController(t.T())

authRepo := mock_auth.NewMockRepository(controller)
userRepo := user.UserRepositoryMock{}
tokenService := token.TokenServiceMock{}
bcryptUtil := utils.BcryptUtilMock{}

tokenService.On("Validate", t.signOutRequest.Token).Return(userCredential, nil)
tokenService.On("RemoveTokenCache", userCredential.RefreshToken).Return(removeTokenErr)

authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil)
actual, err := authSvc.SignOut(t.ctx, t.signOutRequest)

st, ok := status.FromError(err)
assert.Nil(t.T(), actual)
assert.Equal(t.T(), codes.Internal, st.Code())
assert.True(t.T(), ok)
assert.Equal(t.T(), expected.Error(), err.Error())
}

func (t *AuthServiceTest) TestSignOutDeleteAuthSessionFailed() {
userCredential := &tokenDto.UserCredential{
UserID: faker.UUIDDigit(),
Role: constant.USER,
AuthSessionID: faker.UUIDDigit(),
RefreshToken: faker.UUIDDigit(),
}
deleteAuthErr := errors.New("internal server error")

expected := status.Error(codes.Internal, constant.InternalServerErrorMessage)

controller := gomock.NewController(t.T())

authRepo := mock_auth.NewMockRepository(controller)
userRepo := user.UserRepositoryMock{}
tokenService := token.TokenServiceMock{}
bcryptUtil := utils.BcryptUtilMock{}

tokenService.On("Validate", t.signOutRequest.Token).Return(userCredential, nil)
tokenService.On("RemoveTokenCache", userCredential.RefreshToken).Return(nil)
authRepo.EXPECT().Delete(userCredential.AuthSessionID).Return(deleteAuthErr)

authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil)
actual, err := authSvc.SignOut(t.ctx, t.signOutRequest)

st, ok := status.FromError(err)
assert.Nil(t.T(), actual)
assert.Equal(t.T(), codes.Internal, st.Code())
assert.True(t.T(), ok)
assert.Equal(t.T(), expected.Error(), err.Error())
}
2 changes: 1 addition & 1 deletion src/mocks/service/token/token.mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ func (m *TokenServiceMock) CreateRefreshToken() string {
}

func (m *TokenServiceMock) RemoveTokenCache(refreshToken string) error {
args := m.Called()
args := m.Called(refreshToken)
return args.Error(0)
}

0 comments on commit 5f931df

Please sign in to comment.