From 5f931dff35d0780f4669036063e3b3c8e227b3e5 Mon Sep 17 00:00:00 2001 From: NitiwatOwen Date: Fri, 29 Dec 2023 14:57:45 +0700 Subject: [PATCH] feat: implement signout --- src/internal/service/auth/auth.service.go | 17 ++- .../service/auth/auth.service_test.go | 107 ++++++++++++++++++ src/mocks/service/token/token.mock.go | 2 +- 3 files changed, 123 insertions(+), 3 deletions(-) diff --git a/src/internal/service/auth/auth.service.go b/src/internal/service/auth/auth.service.go index c09c26c..a4628e1 100644 --- a/src/internal/service/auth/auth.service.go +++ b/src/internal/service/auth/auth.service.go @@ -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 } diff --git a/src/internal/service/auth/auth.service_test.go b/src/internal/service/auth/auth.service_test.go index 2df85f4..519a824 100644 --- a/src/internal/service/auth/auth.service_test.go +++ b/src/internal/service/auth/auth.service_test.go @@ -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" @@ -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()) } diff --git a/src/mocks/service/token/token.mock.go b/src/mocks/service/token/token.mock.go index 423d3ae..619e829 100644 --- a/src/mocks/service/token/token.mock.go +++ b/src/mocks/service/token/token.mock.go @@ -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) }