From 8ecc0ee7f5bbcfdc77402db1441f50c5b582a6a2 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sun, 24 Dec 2023 15:59:25 +0700 Subject: [PATCH 01/17] feat: mock user repo --- src/mocks/user/user.mock.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/mocks/user/user.mock.go diff --git a/src/mocks/user/user.mock.go b/src/mocks/user/user.mock.go new file mode 100644 index 0000000..046ba39 --- /dev/null +++ b/src/mocks/user/user.mock.go @@ -0,0 +1,36 @@ +package user + +import ( + "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + "github.com/stretchr/testify/mock" +) + +type RepositoryMock struct { + mock.Mock +} + +func (r *RepositoryMock) FindOne(id string, result *model.User) error { + args := r.Called(id, result) + + if args.Get(0) != nil { + *result = *args.Get(0).(*model.User) + } + + return args.Error(1) +} + +func (r *RepositoryMock) Update(id string, result *model.User) error { + args := r.Called(id, result) + + if args.Get(0) != nil { + *result = *args.Get(0).(*model.User) + } + + return args.Error(1) +} + +func (r *RepositoryMock) Delete(id string) error { + args := r.Called(id) + + return args.Error(0) +} From 605f25b9ddc7251cc5c714edb0e7589850c03576 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sun, 24 Dec 2023 16:20:33 +0700 Subject: [PATCH 02/17] feat: user service --- src/internal/service/user/user.service.go | 76 +++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/internal/service/user/user.service.go diff --git a/src/internal/service/user/user.service.go b/src/internal/service/user/user.service.go new file mode 100644 index 0000000..e01591a --- /dev/null +++ b/src/internal/service/user/user.service.go @@ -0,0 +1,76 @@ +package user + +import ( + "context" + + "github.com/isd-sgcu/johnjud-auth/src/internal/constant" + "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + 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" +) + +type serviceImpl struct { + proto.UnimplementedUserServiceServer + repo userRepo.Repository +} + +func NewService(repo userRepo.Repository) *serviceImpl { + return &serviceImpl{repo: repo} +} + +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.NotFound, "user not found") + } + + return &proto.FindOneUserResponse{User: RawToDto(&raw)}, nil +} + +func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest) (*proto.UpdateUserResponse, error) { + raw := &model.User{ + Email: request.Email, + Password: request.Password, + Firstname: request.Firstname, + Lastname: request.Lastname, + } + + err := s.repo.Update(request.Id, raw) + if err != nil { + return nil, status.Error(codes.NotFound, "user not found") + } + + return &proto.UpdateUserResponse{User: RawToDto(raw)}, 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.NotFound, "something wrong when deleting user") + } + + return &proto.DeleteUserResponse{Success: true}, nil +} + +func RawToDto(in *model.User) *proto.User { + var role string + switch in.Role { + case constant.ADMIN: + role = "admin" + case constant.USER: + role = "user" + } + + return &proto.User{ + Id: in.ID.String(), + Email: in.Email, + Password: in.Password, + Firstname: in.Firstname, + Lastname: in.Lastname, + Role: role, + } +} From 5b8d7c1c2d463560011b7fd6bb147665ae31b8e1 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sun, 24 Dec 2023 17:12:16 +0700 Subject: [PATCH 03/17] fix: add more repo mock methods --- src/internal/service/user/user.service.go | 11 +---------- src/mocks/user/user.mock.go | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/internal/service/user/user.service.go b/src/internal/service/user/user.service.go index e01591a..c73dbaf 100644 --- a/src/internal/service/user/user.service.go +++ b/src/internal/service/user/user.service.go @@ -3,7 +3,6 @@ package user import ( "context" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" userRepo "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" @@ -57,20 +56,12 @@ func (s *serviceImpl) Delete(_ context.Context, request *proto.DeleteUserRequest } func RawToDto(in *model.User) *proto.User { - var role string - switch in.Role { - case constant.ADMIN: - role = "admin" - case constant.USER: - role = "user" - } - return &proto.User{ Id: in.ID.String(), Email: in.Email, Password: in.Password, Firstname: in.Firstname, Lastname: in.Lastname, - Role: role, + Role: string(in.Role), } } diff --git a/src/mocks/user/user.mock.go b/src/mocks/user/user.mock.go index 046ba39..aa77b53 100644 --- a/src/mocks/user/user.mock.go +++ b/src/mocks/user/user.mock.go @@ -9,7 +9,17 @@ type RepositoryMock struct { mock.Mock } -func (r *RepositoryMock) FindOne(id string, result *model.User) error { +func (r *RepositoryMock) FindAll(in *[]*model.User) error { + args := r.Called(in) + + if args.Get(0) != nil { + *in = *args.Get(0).(*[]*model.User) + } + + return args.Error(1) +} + +func (r *RepositoryMock) FindById(id string, result *model.User) error { args := r.Called(id, result) if args.Get(0) != nil { @@ -19,6 +29,16 @@ func (r *RepositoryMock) FindOne(id string, result *model.User) error { return args.Error(1) } +func (r *RepositoryMock) Create(in *model.User) error { + args := r.Called(in) + + if args.Get(0) != nil { + *in = *args.Get(0).(*model.User) + } + + return args.Error(1) +} + func (r *RepositoryMock) Update(id string, result *model.User) error { args := r.Called(id, result) From b85ae9c78153f85b6a609b3ed04f69f21e651fe0 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sun, 24 Dec 2023 17:12:23 +0700 Subject: [PATCH 04/17] feat: user service tests --- Makefile | 8 +- go.mod | 1 + go.sum | 2 + .../service/user/user.service_test.go | 153 ++++++++++++++++++ 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/internal/service/user/user.service_test.go diff --git a/Makefile b/Makefile index 2dd0e9b..fb4f72b 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,8 @@ server: - go run ./src/. \ No newline at end of file + go run ./src/. + +test: + go vet ./... + go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/... + go tool cover -func=coverage.out + go tool cover -html=coverage.out -o coverage.html \ No newline at end of file diff --git a/go.mod b/go.mod index 78f06eb..8952cc3 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.3 toolchain go1.21.5 require ( + github.com/bxcodec/faker/v3 v3.8.1 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-faker/faker/v4 v4.2.0 // indirect diff --git a/go.sum b/go.sum index 5c2ff7c..5248223 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/bxcodec/faker/v3 v3.8.1 h1:qO/Xq19V6uHt2xujwpaetgKhraGCapqY2CRWGD/SqcM= +github.com/bxcodec/faker/v3 v3.8.1/go.mod h1:DdSDccxF5msjFo5aO4vrobRQ8nIApg8kq3QWPEQD6+o= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/src/internal/service/user/user.service_test.go b/src/internal/service/user/user.service_test.go new file mode 100644 index 0000000..9ef163b --- /dev/null +++ b/src/internal/service/user/user.service_test.go @@ -0,0 +1,153 @@ +package user + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/bxcodec/faker/v3" + "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/user" + 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 + 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.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.UpdateUser = &model.User{ + Email: t.User.Email, + Password: t.User.Password, + Firstname: t.User.Firstname, + Lastname: t.User.Lastname, + } +} + +func (t *UserServiceTest) TestFindOneSuccess() { + want := &proto.FindOneUserResponse{User: t.UserDto} + + repo := &mock.RepositoryMock{} + repo.On("FindById", t.User.ID.String(), &model.User{}).Return(t.User, nil) + + srv := NewService(repo) + 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) TestFindOneNotFound() { + repo := &mock.RepositoryMock{} + repo.On("FindById", t.User.ID.String(), &model.User{}).Return(nil, errors.New("Not found user")) + + srv := NewService(repo) + 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) TestUpdateSuccess() { + want := &proto.UpdateUserResponse{User: t.UserDto} + + repo := &mock.RepositoryMock{} + repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(t.User, nil) + + srv := NewService(repo) + actual, err := srv.Update(context.Background(), t.UpdateUserReqMock) + + assert.Nil(t.T(), err) + assert.Equal(t.T(), want, actual) +} + +func (t *UserServiceTest) TestUpdateNotFound() { + repo := &mock.RepositoryMock{} + repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(nil, errors.New("Not found user")) + + srv := NewService(repo) + 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.NotFound, st.Code()) +} + +func (t *UserServiceTest) TestDeleteSuccess() { + want := &proto.DeleteUserResponse{Success: true} + + repo := &mock.RepositoryMock{} + repo.On("Delete", t.User.ID.String()).Return(nil) + + srv := NewService(repo) + 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) TestDeleteNotFound() { + repo := &mock.RepositoryMock{} + repo.On("Delete", t.User.ID.String()).Return(errors.New("Not found user")) + + srv := NewService(repo) + 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.NotFound, st.Code()) +} From 97512be26ba00bc08522acc1052d5368afa7ae28 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Thu, 28 Dec 2023 19:25:11 +0700 Subject: [PATCH 05/17] feat: register user service --- src/main.go | 18 ++++++++++++------ src/pkg/service/user/user.service.go | 11 +++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 src/pkg/service/user/user.service.go diff --git a/src/main.go b/src/main.go index 927aafb..414441b 100644 --- a/src/main.go +++ b/src/main.go @@ -3,6 +3,13 @@ package main import ( "context" "fmt" + "net" + "os" + "os/signal" + "sync" + "syscall" + "time" + "github.com/isd-sgcu/johnjud-auth/src/config" "github.com/isd-sgcu/johnjud-auth/src/database" "github.com/isd-sgcu/johnjud-auth/src/internal/strategy" @@ -11,18 +18,14 @@ import ( "github.com/isd-sgcu/johnjud-auth/src/pkg/service/auth" "github.com/isd-sgcu/johnjud-auth/src/pkg/service/jwt" "github.com/isd-sgcu/johnjud-auth/src/pkg/service/token" + userSrv "github.com/isd-sgcu/johnjud-auth/src/pkg/service/user" authPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" + userPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" "github.com/rs/zerolog/log" "google.golang.org/grpc" "google.golang.org/grpc/health" "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/reflection" - "net" - "os" - "os/signal" - "sync" - "syscall" - "time" ) type operation func(ctx context.Context) error @@ -113,6 +116,8 @@ func main() { bcryptUtil := utils.NewBcryptUtil() userRepo := user.NewRepository(db) + userService := userSrv.NewService(userRepo) + jwtStrategy := strategy.NewJwtStrategy(conf.Jwt.Secret) jwtService := jwt.NewService(conf.Jwt, jwtStrategy, jwtUtil) @@ -121,6 +126,7 @@ func main() { grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer()) authPb.RegisterAuthServiceServer(grpcServer, authService) + userPb.RegisterUserServiceServer(grpcServer, userService) reflection.Register(grpcServer) go func() { diff --git a/src/pkg/service/user/user.service.go b/src/pkg/service/user/user.service.go new file mode 100644 index 0000000..889f1b1 --- /dev/null +++ b/src/pkg/service/user/user.service.go @@ -0,0 +1,11 @@ +package user + +import ( + "github.com/isd-sgcu/johnjud-auth/src/internal/service/user" + userRepo "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" + userProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" +) + +func NewService(userRepository userRepo.Repository) userProto.UserServiceServer { + return user.NewService(userRepository) +} From 313f2c9bca09b1723d19478509eded7d1d3328cd Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Thu, 28 Dec 2023 22:10:09 +0700 Subject: [PATCH 06/17] fix: use new user repo mock --- .../service/user/user.service_test.go | 14 ++--- src/mocks/repository/user/user.mock.go | 5 +- src/mocks/user/user.mock.go | 56 ------------------- 3 files changed, 8 insertions(+), 67 deletions(-) delete mode 100644 src/mocks/user/user.mock.go diff --git a/src/internal/service/user/user.service_test.go b/src/internal/service/user/user.service_test.go index 9ef163b..05c052e 100644 --- a/src/internal/service/user/user.service_test.go +++ b/src/internal/service/user/user.service_test.go @@ -10,7 +10,7 @@ import ( "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/user" + mock "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/user" proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" @@ -75,7 +75,7 @@ func (t *UserServiceTest) SetupTest() { func (t *UserServiceTest) TestFindOneSuccess() { want := &proto.FindOneUserResponse{User: t.UserDto} - repo := &mock.RepositoryMock{} + repo := &mock.UserRepositoryMock{} repo.On("FindById", t.User.ID.String(), &model.User{}).Return(t.User, nil) srv := NewService(repo) @@ -86,7 +86,7 @@ func (t *UserServiceTest) TestFindOneSuccess() { } func (t *UserServiceTest) TestFindOneNotFound() { - repo := &mock.RepositoryMock{} + repo := &mock.UserRepositoryMock{} repo.On("FindById", t.User.ID.String(), &model.User{}).Return(nil, errors.New("Not found user")) srv := NewService(repo) @@ -102,7 +102,7 @@ func (t *UserServiceTest) TestFindOneNotFound() { func (t *UserServiceTest) TestUpdateSuccess() { want := &proto.UpdateUserResponse{User: t.UserDto} - repo := &mock.RepositoryMock{} + repo := &mock.UserRepositoryMock{} repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(t.User, nil) srv := NewService(repo) @@ -113,7 +113,7 @@ func (t *UserServiceTest) TestUpdateSuccess() { } func (t *UserServiceTest) TestUpdateNotFound() { - repo := &mock.RepositoryMock{} + repo := &mock.UserRepositoryMock{} repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(nil, errors.New("Not found user")) srv := NewService(repo) @@ -129,7 +129,7 @@ func (t *UserServiceTest) TestUpdateNotFound() { func (t *UserServiceTest) TestDeleteSuccess() { want := &proto.DeleteUserResponse{Success: true} - repo := &mock.RepositoryMock{} + repo := &mock.UserRepositoryMock{} repo.On("Delete", t.User.ID.String()).Return(nil) srv := NewService(repo) @@ -140,7 +140,7 @@ func (t *UserServiceTest) TestDeleteSuccess() { } func (t *UserServiceTest) TestDeleteNotFound() { - repo := &mock.RepositoryMock{} + repo := &mock.UserRepositoryMock{} repo.On("Delete", t.User.ID.String()).Return(errors.New("Not found user")) srv := NewService(repo) diff --git a/src/mocks/repository/user/user.mock.go b/src/mocks/repository/user/user.mock.go index 817a3be..031ddf2 100644 --- a/src/mocks/repository/user/user.mock.go +++ b/src/mocks/repository/user/user.mock.go @@ -61,9 +61,6 @@ func (m *UserRepositoryMock) Update(id string, user *model.User) error { func (m *UserRepositoryMock) Delete(id string) error { args := m.Called(id) - if args.Get(0) != nil { - return nil - } - return args.Error(1) + return args.Error(0) } diff --git a/src/mocks/user/user.mock.go b/src/mocks/user/user.mock.go deleted file mode 100644 index aa77b53..0000000 --- a/src/mocks/user/user.mock.go +++ /dev/null @@ -1,56 +0,0 @@ -package user - -import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" - "github.com/stretchr/testify/mock" -) - -type RepositoryMock struct { - mock.Mock -} - -func (r *RepositoryMock) FindAll(in *[]*model.User) error { - args := r.Called(in) - - if args.Get(0) != nil { - *in = *args.Get(0).(*[]*model.User) - } - - return args.Error(1) -} - -func (r *RepositoryMock) FindById(id string, result *model.User) error { - args := r.Called(id, result) - - if args.Get(0) != nil { - *result = *args.Get(0).(*model.User) - } - - return args.Error(1) -} - -func (r *RepositoryMock) Create(in *model.User) error { - args := r.Called(in) - - if args.Get(0) != nil { - *in = *args.Get(0).(*model.User) - } - - return args.Error(1) -} - -func (r *RepositoryMock) Update(id string, result *model.User) error { - args := r.Called(id, result) - - if args.Get(0) != nil { - *result = *args.Get(0).(*model.User) - } - - return args.Error(1) -} - -func (r *RepositoryMock) Delete(id string) error { - args := r.Called(id) - - return args.Error(0) -} From d49cada1242b86e3ada63f2c0cb41535bd1d18e2 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Thu, 28 Dec 2023 23:50:05 +0700 Subject: [PATCH 07/17] fix: register user service --- src/internal/service/user/user.service_test.go | 2 +- src/main.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/internal/service/user/user.service_test.go b/src/internal/service/user/user.service_test.go index 05c052e..5a1bfef 100644 --- a/src/internal/service/user/user.service_test.go +++ b/src/internal/service/user/user.service_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - "github.com/bxcodec/faker/v3" + "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" diff --git a/src/main.go b/src/main.go index 438d590..c831c18 100644 --- a/src/main.go +++ b/src/main.go @@ -20,6 +20,7 @@ import ( authSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/auth" jwtSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/jwt" tokenSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/token" + userSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/user" authPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" userPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" "github.com/rs/zerolog/log" @@ -127,6 +128,8 @@ func main() { authRepo := authRp.NewRepository(db) userRepo := userRp.NewRepository(db) + userService := userSvc.NewService(userRepo) + accessTokenCache := cacheRp.NewRepository(cacheDb) refreshTokenCache := cacheRp.NewRepository(cacheDb) From db1afbd6b7449151b30a45c25473d4499b161858 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Fri, 29 Dec 2023 00:12:18 +0700 Subject: [PATCH 08/17] fix: change app port + db name --- config/config.example.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.example.yaml b/config/config.example.yaml index 9c8d302..ad75341 100644 --- a/config/config.example.yaml +++ b/config/config.example.yaml @@ -1,12 +1,12 @@ app: - port: 8081 + port: 3002 debug: true secret: database: host: localhost port: 5432 - name: johnjud-auth-db + name: johnjud_db username: root password: root From 4c91059006f10bc83cdd7935c62b016b3b0c29c5 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Fri, 29 Dec 2023 00:23:11 +0700 Subject: [PATCH 09/17] feat: update proto --- Makefile | 5 ++++- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 127b927..4bd008c 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,7 @@ test: go vet ./... go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/... go tool cover -func=coverage.out - go tool cover -html=coverage.out -o coverage.html \ No newline at end of file + go tool cover -html=coverage.out -o coverage.html + +proto: + go get github.com/isd-sgcu/johnjud-go-proto@latest \ No newline at end of file diff --git a/go.mod b/go.mod index f4f7c02..a93ea58 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.5.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect - github.com/isd-sgcu/johnjud-go-proto v0.1.2 // indirect + github.com/isd-sgcu/johnjud-go-proto v0.1.5 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgx/v5 v5.4.3 // indirect diff --git a/go.sum b/go.sum index 28db50e..c6fc2ba 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/isd-sgcu/johnjud-go-proto v0.0.8 h1:nIQBZgK2OFVrLjVtpeDgwows8poA7LhsI github.com/isd-sgcu/johnjud-go-proto v0.0.8/go.mod h1:HP0w9gC30b5WNnqeFBM9JJZud+pvyikz0+pGFSI/Wjw= github.com/isd-sgcu/johnjud-go-proto v0.1.2 h1:gp1MGHCnJdeuUEI4yCbvpQJ1BfLzR9Tr9Q1kKSdssME= github.com/isd-sgcu/johnjud-go-proto v0.1.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.1.5 h1:ZhMb73xXOSM2OlsfQ8wv6rmbfSIrXwbBumIx/vtdgHc= +github.com/isd-sgcu/johnjud-go-proto v0.1.5/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= From 6c5a413fd5d77da06928eaa9582bc4f78251f59a Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Fri, 29 Dec 2023 00:24:50 +0700 Subject: [PATCH 10/17] fix: auth signup -> signUp (grpc test) --- src/internal/service/auth/auth.service.go | 3 ++- src/internal/service/auth/auth.service_test.go | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/internal/service/auth/auth.service.go b/src/internal/service/auth/auth.service.go index 3b4f080..ffba35d 100644 --- a/src/internal/service/auth/auth.service.go +++ b/src/internal/service/auth/auth.service.go @@ -2,6 +2,7 @@ package auth import ( "context" + "github.com/google/uuid" "github.com/isd-sgcu/johnjud-auth/src/internal/constant" "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" @@ -45,7 +46,7 @@ func (s *serviceImpl) RefreshToken(_ context.Context, request *authProto.Refresh return nil, nil } -func (s *serviceImpl) Signup(_ context.Context, request *authProto.SignUpRequest) (*authProto.SignUpResponse, error) { +func (s *serviceImpl) SignUp(_ context.Context, request *authProto.SignUpRequest) (*authProto.SignUpResponse, error) { hashPassword, err := s.bcryptUtil.GenerateHashedPassword(request.Password) if err != nil { return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) diff --git a/src/internal/service/auth/auth.service_test.go b/src/internal/service/auth/auth.service_test.go index cd2dd60..41113ec 100644 --- a/src/internal/service/auth/auth.service_test.go +++ b/src/internal/service/auth/auth.service_test.go @@ -2,6 +2,9 @@ package auth import ( "context" + "testing" + "time" + "github.com/go-faker/faker/v4" "github.com/golang/mock/gomock" "github.com/google/uuid" @@ -18,8 +21,6 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "gorm.io/gorm" - "testing" - "time" ) type AuthServiceTest struct { @@ -91,7 +92,7 @@ func (t *AuthServiceTest) TestSignupSuccess() { userRepo.On("Create", newUser).Return(createdUser, nil) authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil) - actual, err := authSvc.Signup(t.ctx, t.signupRequest) + actual, err := authSvc.SignUp(t.ctx, t.signupRequest) assert.Nil(t.T(), err) assert.Equal(t.T(), expected.Id, actual.Id) @@ -112,7 +113,7 @@ func (t *AuthServiceTest) TestSignupHashPasswordFailed() { bcryptUtil.On("GenerateHashedPassword", t.signupRequest.Password).Return("", hashPasswordErr) authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil) - actual, err := authSvc.Signup(t.ctx, t.signupRequest) + actual, err := authSvc.SignUp(t.ctx, t.signupRequest) status, ok := status.FromError(err) @@ -146,7 +147,7 @@ func (t *AuthServiceTest) TestSignupCreateUserDuplicateConstraint() { userRepo.On("Create", newUser).Return(nil, createUserErr) authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil) - actual, err := authSvc.Signup(t.ctx, t.signupRequest) + actual, err := authSvc.SignUp(t.ctx, t.signupRequest) status, ok := status.FromError(err) @@ -180,7 +181,7 @@ func (t *AuthServiceTest) TestSignupCreateUserInternalFailed() { userRepo.On("Create", newUser).Return(nil, createUserErr) authSvc := NewService(authRepo, &userRepo, &tokenService, &bcryptUtil) - actual, err := authSvc.Signup(t.ctx, t.signupRequest) + actual, err := authSvc.SignUp(t.ctx, t.signupRequest) status, ok := status.FromError(err) From 7a926f92eb710381baf18dfc77b990c15c49e129 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Fri, 29 Dec 2023 22:23:40 +0700 Subject: [PATCH 11/17] fix: user srv dont return password, hash password --- src/internal/service/user/user.service.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/internal/service/user/user.service.go b/src/internal/service/user/user.service.go index c73dbaf..b5a86af 100644 --- a/src/internal/service/user/user.service.go +++ b/src/internal/service/user/user.service.go @@ -3,7 +3,9 @@ package user import ( "context" + "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" @@ -12,7 +14,8 @@ import ( type serviceImpl struct { proto.UnimplementedUserServiceServer - repo userRepo.Repository + repo userRepo.Repository + bcryptUtil utils.IBcryptUtil } func NewService(repo userRepo.Repository) *serviceImpl { @@ -31,19 +34,24 @@ func (s *serviceImpl) FindOne(_ context.Context, request *proto.FindOneUserReque } func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest) (*proto.UpdateUserResponse, error) { - raw := &model.User{ + 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: request.Password, + Password: hashPassword, Firstname: request.Firstname, Lastname: request.Lastname, } - err := s.repo.Update(request.Id, raw) + err = s.repo.Update(request.Id, updateUser) if err != nil { return nil, status.Error(codes.NotFound, "user not found") } - return &proto.UpdateUserResponse{User: RawToDto(raw)}, nil + return &proto.UpdateUserResponse{User: RawToDto(updateUser)}, nil } func (s *serviceImpl) Delete(_ context.Context, request *proto.DeleteUserRequest) (*proto.DeleteUserResponse, error) { @@ -59,7 +67,6 @@ func RawToDto(in *model.User) *proto.User { return &proto.User{ Id: in.ID.String(), Email: in.Email, - Password: in.Password, Firstname: in.Firstname, Lastname: in.Lastname, Role: string(in.Role), From 3d3d875f4059c08f73c132ce3f09d101027d6b30 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Fri, 29 Dec 2023 22:25:09 +0700 Subject: [PATCH 12/17] fix: add bcrypt to pkg user srv --- src/internal/service/user/user.service.go | 4 ++-- src/main.go | 2 +- src/pkg/service/user/user.service.go | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/internal/service/user/user.service.go b/src/internal/service/user/user.service.go index b5a86af..0d8ecfa 100644 --- a/src/internal/service/user/user.service.go +++ b/src/internal/service/user/user.service.go @@ -18,8 +18,8 @@ type serviceImpl struct { bcryptUtil utils.IBcryptUtil } -func NewService(repo userRepo.Repository) *serviceImpl { - return &serviceImpl{repo: repo} +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) { diff --git a/src/main.go b/src/main.go index c831c18..3fe8b03 100644 --- a/src/main.go +++ b/src/main.go @@ -128,7 +128,7 @@ func main() { authRepo := authRp.NewRepository(db) userRepo := userRp.NewRepository(db) - userService := userSvc.NewService(userRepo) + userService := userSvc.NewService(userRepo, bcryptUtil) accessTokenCache := cacheRp.NewRepository(cacheDb) refreshTokenCache := cacheRp.NewRepository(cacheDb) diff --git a/src/pkg/service/user/user.service.go b/src/pkg/service/user/user.service.go index 889f1b1..fba3fdc 100644 --- a/src/pkg/service/user/user.service.go +++ b/src/pkg/service/user/user.service.go @@ -2,10 +2,11 @@ package user import ( "github.com/isd-sgcu/johnjud-auth/src/internal/service/user" + "github.com/isd-sgcu/johnjud-auth/src/internal/utils" userRepo "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" userProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" ) -func NewService(userRepository userRepo.Repository) userProto.UserServiceServer { - return user.NewService(userRepository) +func NewService(userRepository userRepo.Repository, bcryptUtil utils.IBcryptUtil) userProto.UserServiceServer { + return user.NewService(userRepository, bcryptUtil) } From 084b48f3e1f17483f0830c609db271f8c14dcbc8 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Fri, 29 Dec 2023 22:38:45 +0700 Subject: [PATCH 13/17] fix: user srv text add brcypt, hashed pw --- .../service/user/user.service_test.go | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/internal/service/user/user.service_test.go b/src/internal/service/user/user.service_test.go index 5a1bfef..3e73536 100644 --- a/src/internal/service/user/user.service_test.go +++ b/src/internal/service/user/user.service_test.go @@ -11,6 +11,7 @@ import ( "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" @@ -25,6 +26,8 @@ type UserServiceTest struct { User *model.User UpdateUser *model.User UserDto *proto.User + UserDtoNoPassword *proto.User + HashedPassword string UpdateUserReqMock *proto.UpdateUserRequest } @@ -56,6 +59,14 @@ func (t *UserServiceTest) SetupTest() { 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, @@ -64,21 +75,24 @@ func (t *UserServiceTest) SetupTest() { Lastname: t.User.Lastname, } + t.HashedPassword = faker.Password() + t.UpdateUser = &model.User{ Email: t.User.Email, - Password: t.User.Password, + Password: t.HashedPassword, Firstname: t.User.Firstname, Lastname: t.User.Lastname, } } func (t *UserServiceTest) TestFindOneSuccess() { - want := &proto.FindOneUserResponse{User: t.UserDto} + want := &proto.FindOneUserResponse{User: t.UserDtoNoPassword} repo := &mock.UserRepositoryMock{} repo.On("FindById", t.User.ID.String(), &model.User{}).Return(t.User, nil) - srv := NewService(repo) + 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) @@ -89,7 +103,8 @@ func (t *UserServiceTest) TestFindOneNotFound() { repo := &mock.UserRepositoryMock{} repo.On("FindById", t.User.ID.String(), &model.User{}).Return(nil, errors.New("Not found user")) - srv := NewService(repo) + 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) @@ -100,12 +115,15 @@ func (t *UserServiceTest) TestFindOneNotFound() { } func (t *UserServiceTest) TestUpdateSuccess() { - want := &proto.UpdateUserResponse{User: t.UserDto} + want := &proto.UpdateUserResponse{User: t.UserDtoNoPassword} repo := &mock.UserRepositoryMock{} repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(t.User, nil) - srv := NewService(repo) + 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) @@ -116,7 +134,10 @@ func (t *UserServiceTest) TestUpdateNotFound() { repo := &mock.UserRepositoryMock{} repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(nil, errors.New("Not found user")) - srv := NewService(repo) + 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) @@ -132,7 +153,8 @@ func (t *UserServiceTest) TestDeleteSuccess() { repo := &mock.UserRepositoryMock{} repo.On("Delete", t.User.ID.String()).Return(nil) - srv := NewService(repo) + brcyptUtil := &utils.BcryptUtilMock{} + srv := NewService(repo, brcyptUtil) actual, err := srv.Delete(context.Background(), &proto.DeleteUserRequest{Id: t.UserDto.Id}) assert.Nil(t.T(), err) @@ -143,7 +165,8 @@ func (t *UserServiceTest) TestDeleteNotFound() { repo := &mock.UserRepositoryMock{} repo.On("Delete", t.User.ID.String()).Return(errors.New("Not found user")) - srv := NewService(repo) + brcyptUtil := &utils.BcryptUtilMock{} + srv := NewService(repo, brcyptUtil) actual, err := srv.Delete(context.Background(), &proto.DeleteUserRequest{Id: t.UserDto.Id}) st, ok := status.FromError(err) From 30df31fdc82826651c48195ae90d8aec3f58aa96 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 30 Dec 2023 21:52:22 +0700 Subject: [PATCH 14/17] fix: user service error types --- src/internal/service/user/user.service.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/internal/service/user/user.service.go b/src/internal/service/user/user.service.go index 0d8ecfa..14b46a1 100644 --- a/src/internal/service/user/user.service.go +++ b/src/internal/service/user/user.service.go @@ -2,6 +2,7 @@ package user import ( "context" + "errors" "github.com/isd-sgcu/johnjud-auth/src/internal/constant" "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" @@ -10,6 +11,7 @@ import ( 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 { @@ -27,7 +29,7 @@ func (s *serviceImpl) FindOne(_ context.Context, request *proto.FindOneUserReque err := s.repo.FindById(request.Id, &raw) if err != nil { - return nil, status.Error(codes.NotFound, "user not found") + return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) } return &proto.FindOneUserResponse{User: RawToDto(&raw)}, nil @@ -48,7 +50,10 @@ func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest err = s.repo.Update(request.Id, updateUser) if err != nil { - return nil, status.Error(codes.NotFound, "user not found") + if errors.Is(err, gorm.ErrDuplicatedKey) { + return nil, status.Error(codes.AlreadyExists, constant.DuplicateEmailErrorMessage) + } + return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) } return &proto.UpdateUserResponse{User: RawToDto(updateUser)}, nil @@ -57,7 +62,7 @@ func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest 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.NotFound, "something wrong when deleting user") + return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) } return &proto.DeleteUserResponse{Success: true}, nil From 367aaa4f88f8ee759c8a788a1dbd4466de11f511 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 30 Dec 2023 21:55:39 +0700 Subject: [PATCH 15/17] fix: user srv tests --- src/internal/service/user/user.service_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/internal/service/user/user.service_test.go b/src/internal/service/user/user.service_test.go index 3e73536..3f4d1e0 100644 --- a/src/internal/service/user/user.service_test.go +++ b/src/internal/service/user/user.service_test.go @@ -111,7 +111,7 @@ func (t *UserServiceTest) TestFindOneNotFound() { assert.True(t.T(), ok) assert.Nil(t.T(), actual) - assert.Equal(t.T(), codes.NotFound, st.Code()) + assert.Equal(t.T(), codes.Internal, st.Code()) } func (t *UserServiceTest) TestUpdateSuccess() { @@ -144,7 +144,7 @@ func (t *UserServiceTest) TestUpdateNotFound() { assert.True(t.T(), ok) assert.Nil(t.T(), actual) - assert.Equal(t.T(), codes.NotFound, st.Code()) + assert.Equal(t.T(), codes.Internal, st.Code()) } func (t *UserServiceTest) TestDeleteSuccess() { @@ -172,5 +172,5 @@ func (t *UserServiceTest) TestDeleteNotFound() { st, ok := status.FromError(err) assert.True(t.T(), ok) assert.Nil(t.T(), actual) - assert.Equal(t.T(), codes.NotFound, st.Code()) + assert.Equal(t.T(), codes.Internal, st.Code()) } From dbd6fa12d08e1ce7a8e4908125c085eced6e0c20 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 30 Dec 2023 21:59:42 +0700 Subject: [PATCH 16/17] fix: user srv tests name --- src/internal/service/user/user.service_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/internal/service/user/user.service_test.go b/src/internal/service/user/user.service_test.go index 3f4d1e0..5069207 100644 --- a/src/internal/service/user/user.service_test.go +++ b/src/internal/service/user/user.service_test.go @@ -99,7 +99,7 @@ func (t *UserServiceTest) TestFindOneSuccess() { assert.Equal(t.T(), want, actual) } -func (t *UserServiceTest) TestFindOneNotFound() { +func (t *UserServiceTest) TestFindOneInternalErr() { repo := &mock.UserRepositoryMock{} repo.On("FindById", t.User.ID.String(), &model.User{}).Return(nil, errors.New("Not found user")) @@ -130,7 +130,7 @@ func (t *UserServiceTest) TestUpdateSuccess() { assert.Equal(t.T(), want, actual) } -func (t *UserServiceTest) TestUpdateNotFound() { +func (t *UserServiceTest) TestUpdateInternalErr() { repo := &mock.UserRepositoryMock{} repo.On("Update", t.User.ID.String(), t.UpdateUser).Return(nil, errors.New("Not found user")) @@ -161,7 +161,7 @@ func (t *UserServiceTest) TestDeleteSuccess() { assert.Equal(t.T(), want, actual) } -func (t *UserServiceTest) TestDeleteNotFound() { +func (t *UserServiceTest) TestDeleteInternalErr() { repo := &mock.UserRepositoryMock{} repo.On("Delete", t.User.ID.String()).Return(errors.New("Not found user")) From 23c69ceba75ed3e9b325ba3eace8e77ef19046ad Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 30 Dec 2023 22:02:23 +0700 Subject: [PATCH 17/17] fix: user srv err message --- src/internal/service/user/user.service.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/internal/service/user/user.service.go b/src/internal/service/user/user.service.go index 14b46a1..ab31db0 100644 --- a/src/internal/service/user/user.service.go +++ b/src/internal/service/user/user.service.go @@ -29,7 +29,7 @@ func (s *serviceImpl) FindOne(_ context.Context, request *proto.FindOneUserReque err := s.repo.FindById(request.Id, &raw) if err != nil { - return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) + return nil, status.Error(codes.Internal, "Find one user failed") } return &proto.FindOneUserResponse{User: RawToDto(&raw)}, nil @@ -53,7 +53,7 @@ func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest if errors.Is(err, gorm.ErrDuplicatedKey) { return nil, status.Error(codes.AlreadyExists, constant.DuplicateEmailErrorMessage) } - return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) + return nil, status.Error(codes.Internal, "Update user failed") } return &proto.UpdateUserResponse{User: RawToDto(updateUser)}, nil @@ -62,7 +62,7 @@ func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest 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, constant.InternalServerErrorMessage) + return nil, status.Error(codes.Internal, "Delete user failed") } return &proto.DeleteUserResponse{Success: true}, nil