-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from isd-sgcu/feature/user-service
Feature/user service
- Loading branch information
Showing
11 changed files
with
304 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/isd-sgcu/johnjud-auth/src/internal/constant" | ||
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" | ||
"github.com/isd-sgcu/johnjud-auth/src/internal/utils" | ||
userRepo "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" | ||
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type serviceImpl struct { | ||
proto.UnimplementedUserServiceServer | ||
repo userRepo.Repository | ||
bcryptUtil utils.IBcryptUtil | ||
} | ||
|
||
func NewService(repo userRepo.Repository, bcryptUtil utils.IBcryptUtil) *serviceImpl { | ||
return &serviceImpl{repo: repo, bcryptUtil: bcryptUtil} | ||
} | ||
|
||
func (s *serviceImpl) FindOne(_ context.Context, request *proto.FindOneUserRequest) (*proto.FindOneUserResponse, error) { | ||
raw := model.User{} | ||
|
||
err := s.repo.FindById(request.Id, &raw) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, "Find one user failed") | ||
} | ||
|
||
return &proto.FindOneUserResponse{User: RawToDto(&raw)}, nil | ||
} | ||
|
||
func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest) (*proto.UpdateUserResponse, error) { | ||
hashPassword, err := s.bcryptUtil.GenerateHashedPassword(request.Password) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) | ||
} | ||
|
||
updateUser := &model.User{ | ||
Email: request.Email, | ||
Password: hashPassword, | ||
Firstname: request.Firstname, | ||
Lastname: request.Lastname, | ||
} | ||
|
||
err = s.repo.Update(request.Id, updateUser) | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrDuplicatedKey) { | ||
return nil, status.Error(codes.AlreadyExists, constant.DuplicateEmailErrorMessage) | ||
} | ||
return nil, status.Error(codes.Internal, "Update user failed") | ||
} | ||
|
||
return &proto.UpdateUserResponse{User: RawToDto(updateUser)}, nil | ||
} | ||
|
||
func (s *serviceImpl) Delete(_ context.Context, request *proto.DeleteUserRequest) (*proto.DeleteUserResponse, error) { | ||
err := s.repo.Delete(request.Id) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, "Delete user failed") | ||
} | ||
|
||
return &proto.DeleteUserResponse{Success: true}, nil | ||
} | ||
|
||
func RawToDto(in *model.User) *proto.User { | ||
return &proto.User{ | ||
Id: in.ID.String(), | ||
Email: in.Email, | ||
Firstname: in.Firstname, | ||
Lastname: in.Lastname, | ||
Role: string(in.Role), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-faker/faker/v4" | ||
"github.com/google/uuid" | ||
"github.com/isd-sgcu/johnjud-auth/src/config" | ||
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" | ||
mock "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/user" | ||
"github.com/isd-sgcu/johnjud-auth/src/mocks/utils" | ||
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type UserServiceTest struct { | ||
suite.Suite | ||
config *config.App | ||
User *model.User | ||
UpdateUser *model.User | ||
UserDto *proto.User | ||
UserDtoNoPassword *proto.User | ||
HashedPassword string | ||
UpdateUserReqMock *proto.UpdateUserRequest | ||
} | ||
|
||
func TestUserService(t *testing.T) { | ||
suite.Run(t, new(UserServiceTest)) | ||
} | ||
|
||
func (t *UserServiceTest) SetupTest() { | ||
t.User = &model.User{ | ||
Base: model.Base{ | ||
ID: uuid.New(), | ||
CreatedAt: time.Time{}, | ||
UpdatedAt: time.Time{}, | ||
DeletedAt: gorm.DeletedAt{}, | ||
}, | ||
Email: faker.Email(), | ||
Password: faker.Password(), | ||
Firstname: faker.Username(), | ||
Lastname: faker.Username(), | ||
Role: "user", | ||
} | ||
|
||
t.UserDto = &proto.User{ | ||
Id: t.User.ID.String(), | ||
Email: t.User.Email, | ||
Password: t.User.Password, | ||
Firstname: t.User.Firstname, | ||
Lastname: t.User.Lastname, | ||
Role: string(t.User.Role), | ||
} | ||
|
||
t.UserDtoNoPassword = &proto.User{ | ||
Id: t.User.ID.String(), | ||
Email: t.User.Email, | ||
Firstname: t.User.Firstname, | ||
Lastname: t.User.Lastname, | ||
Role: string(t.User.Role), | ||
} | ||
|
||
t.UpdateUserReqMock = &proto.UpdateUserRequest{ | ||
Id: t.User.ID.String(), | ||
Email: t.User.Email, | ||
Password: t.User.Password, | ||
Firstname: t.User.Firstname, | ||
Lastname: t.User.Lastname, | ||
} | ||
|
||
t.HashedPassword = faker.Password() | ||
|
||
t.UpdateUser = &model.User{ | ||
Email: t.User.Email, | ||
Password: t.HashedPassword, | ||
Firstname: t.User.Firstname, | ||
Lastname: t.User.Lastname, | ||
} | ||
} | ||
|
||
func (t *UserServiceTest) TestFindOneSuccess() { | ||
want := &proto.FindOneUserResponse{User: t.UserDtoNoPassword} | ||
|
||
repo := &mock.UserRepositoryMock{} | ||
repo.On("FindById", t.User.ID.String(), &model.User{}).Return(t.User, nil) | ||
|
||
brcyptUtil := &utils.BcryptUtilMock{} | ||
srv := NewService(repo, brcyptUtil) | ||
actual, err := srv.FindOne(context.Background(), &proto.FindOneUserRequest{Id: t.User.ID.String()}) | ||
|
||
assert.Nil(t.T(), err) | ||
assert.Equal(t.T(), want, actual) | ||
} | ||
|
||
func (t *UserServiceTest) TestFindOneInternalErr() { | ||
repo := &mock.UserRepositoryMock{} | ||
repo.On("FindById", t.User.ID.String(), &model.User{}).Return(nil, errors.New("Not found user")) | ||
|
||
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.Internal, st.Code()) | ||
} | ||
|
||
func (t *UserServiceTest) TestUpdateSuccess() { | ||
want := &proto.UpdateUserResponse{User: t.UserDtoNoPassword} | ||
|
||
repo := &mock.UserRepositoryMock{} | ||
repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(t.User, nil) | ||
|
||
brcyptUtil := &utils.BcryptUtilMock{} | ||
brcyptUtil.On("GenerateHashedPassword", t.User.Password).Return(t.HashedPassword, nil) | ||
|
||
srv := NewService(repo, brcyptUtil) | ||
actual, err := srv.Update(context.Background(), t.UpdateUserReqMock) | ||
|
||
assert.Nil(t.T(), err) | ||
assert.Equal(t.T(), want, actual) | ||
} | ||
|
||
func (t *UserServiceTest) TestUpdateInternalErr() { | ||
repo := &mock.UserRepositoryMock{} | ||
repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(nil, errors.New("Not found user")) | ||
|
||
brcyptUtil := &utils.BcryptUtilMock{} | ||
brcyptUtil.On("GenerateHashedPassword", t.User.Password).Return(t.HashedPassword, nil) | ||
|
||
srv := NewService(repo, brcyptUtil) | ||
actual, err := srv.Update(context.Background(), t.UpdateUserReqMock) | ||
|
||
st, ok := status.FromError(err) | ||
|
||
assert.True(t.T(), ok) | ||
assert.Nil(t.T(), actual) | ||
assert.Equal(t.T(), codes.Internal, st.Code()) | ||
} | ||
|
||
func (t *UserServiceTest) TestDeleteSuccess() { | ||
want := &proto.DeleteUserResponse{Success: true} | ||
|
||
repo := &mock.UserRepositoryMock{} | ||
repo.On("Delete", t.User.ID.String()).Return(nil) | ||
|
||
brcyptUtil := &utils.BcryptUtilMock{} | ||
srv := NewService(repo, brcyptUtil) | ||
actual, err := srv.Delete(context.Background(), &proto.DeleteUserRequest{Id: t.UserDto.Id}) | ||
|
||
assert.Nil(t.T(), err) | ||
assert.Equal(t.T(), want, actual) | ||
} | ||
|
||
func (t *UserServiceTest) TestDeleteInternalErr() { | ||
repo := &mock.UserRepositoryMock{} | ||
repo.On("Delete", t.User.ID.String()).Return(errors.New("Not found user")) | ||
|
||
brcyptUtil := &utils.BcryptUtilMock{} | ||
srv := NewService(repo, brcyptUtil) | ||
actual, err := srv.Delete(context.Background(), &proto.DeleteUserRequest{Id: t.UserDto.Id}) | ||
|
||
st, ok := status.FromError(err) | ||
assert.True(t.T(), ok) | ||
assert.Nil(t.T(), actual) | ||
assert.Equal(t.T(), codes.Internal, st.Code()) | ||
} |
Oops, something went wrong.