Skip to content

Commit

Permalink
Merge pull request #17 from isd-sgcu/JOH-27/user-svc-fix
Browse files Browse the repository at this point in the history
[JOH-27] User Service NotFound err
  • Loading branch information
bookpanda authored Jan 7, 2024
2 parents d1b28ea + 9c82720 commit dc2a85e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Change made

- [ ]  New features
- [ ]  Bug fixes
- [ ]  Breaking changes
## Describe what you have done
-
### New Features
-
### Fix
-
### Others
-
2 changes: 2 additions & 0 deletions internal/constant/error.constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ const InvalidTokenErrorMessage = "Invalid token"
const IncorrectEmailPasswordErrorMessage = "Incorrect email or password"
const DuplicateEmailErrorMessage = "Duplicate email"
const InternalServerErrorMessage = "Internal server error"

const UserNotFoundErrorMessage = "User not found"
4 changes: 4 additions & 0 deletions internal/service/user/user.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"context"
"errors"

"github.com/isd-sgcu/johnjud-auth/internal/constant"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/utils"
Expand All @@ -29,6 +30,9 @@ func (s *serviceImpl) FindOne(_ context.Context, request *proto.FindOneUserReque

err := s.repo.FindById(request.Id, &raw)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, constant.UserNotFoundErrorMessage)
}
return nil, status.Error(codes.Internal, "Find one user failed")
}

Expand Down
20 changes: 18 additions & 2 deletions internal/service/user/user.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package user
import (
"context"
"errors"
"testing"
"time"

"github.com/isd-sgcu/johnjud-auth/cfgldr"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
mock "github.com/isd-sgcu/johnjud-auth/mocks/repository/user"
"github.com/isd-sgcu/johnjud-auth/mocks/utils"
"testing"
"time"

"github.com/go-faker/faker/v4"
"github.com/google/uuid"
Expand Down Expand Up @@ -99,6 +100,21 @@ func (t *UserServiceTest) TestFindOneSuccess() {
assert.Equal(t.T(), want, actual)
}

func (t *UserServiceTest) TestFindOneNotFoundErr() {
repo := &mock.UserRepositoryMock{}
repo.On("FindById", t.User.ID.String(), &model.User{}).Return(nil, gorm.ErrRecordNotFound)

brcyptUtil := &utils.BcryptUtilMock{}
srv := NewService(repo, brcyptUtil)
actual, err := srv.FindOne(context.Background(), &proto.FindOneUserRequest{Id: t.User.ID.String()})

st, ok := status.FromError(err)

assert.True(t.T(), ok)
assert.Nil(t.T(), actual)
assert.Equal(t.T(), codes.NotFound, st.Code())
}

func (t *UserServiceTest) TestFindOneInternalErr() {
repo := &mock.UserRepositoryMock{}
repo.On("FindById", t.User.ID.String(), &model.User{}).Return(nil, errors.New("Not found user"))
Expand Down

0 comments on commit dc2a85e

Please sign in to comment.