Skip to content

Commit

Permalink
fix: import package name
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitiwat-owen committed Jan 1, 2024
1 parent 2fc36fe commit fbf16df
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 94 deletions.
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"
"github.com/isd-sgcu/johnjud-auth/cfgldr"
database2 "github.com/isd-sgcu/johnjud-auth/database"
"github.com/isd-sgcu/johnjud-auth/database"
authRp "github.com/isd-sgcu/johnjud-auth/internal/repository/auth"
cacheRp "github.com/isd-sgcu/johnjud-auth/internal/repository/cache"
userRp "github.com/isd-sgcu/johnjud-auth/internal/repository/user"
Expand Down Expand Up @@ -95,15 +95,15 @@ func main() {
Msg("Failed to load config")
}

db, err := database2.InitPostgresDatabase(&conf.Database, conf.App.Debug)
db, err := database.InitPostgresDatabase(&conf.Database, conf.App.Debug)
if err != nil {
log.Fatal().
Err(err).
Str("service", "auth").
Msg("Failed to init postgres connection")
}

cacheDb, err := database2.InitRedisConnection(&conf.Redis)
cacheDb, err := database.InitRedisConnection(&conf.Redis)
if err != nil {
log.Fatal().
Err(err).
Expand Down
12 changes: 6 additions & 6 deletions internal/repository/auth/auth.repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package auth
import (
"fmt"
"github.com/google/uuid"
model2 "github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/auth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand All @@ -28,10 +28,10 @@ func (t *AuthRepositoryTest) SetupTest() {

assert.NoError(t.T(), err)

_ = db.Migrator().DropTable(&model2.User{})
_ = db.Migrator().DropTable(&model2.AuthSession{})
_ = db.Migrator().DropTable(&model.User{})
_ = db.Migrator().DropTable(&model.AuthSession{})

err = db.AutoMigrate(&model2.User{}, &model2.AuthSession{})
err = db.AutoMigrate(&model.User{}, &model.AuthSession{})
assert.NoError(t.T(), err)

authRepo := NewRepository(db)
Expand All @@ -41,7 +41,7 @@ func (t *AuthRepositoryTest) SetupTest() {
}

func (t *AuthRepositoryTest) TestCreateSuccess() {
createAuthSession := &model2.AuthSession{
createAuthSession := &model.AuthSession{
UserID: uuid.New(),
}

Expand All @@ -50,7 +50,7 @@ func (t *AuthRepositoryTest) TestCreateSuccess() {
}

func (t *AuthRepositoryTest) TestDeleteSuccess() {
createAuthSession := &model2.AuthSession{
createAuthSession := &model.AuthSession{
UserID: uuid.New(),
}

Expand Down
36 changes: 18 additions & 18 deletions internal/repository/user/user.repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/go-faker/faker/v4"
"github.com/isd-sgcu/johnjud-auth/internal/constant"
model2 "github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/user"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand All @@ -17,7 +17,7 @@ type UserRepositoryTest struct {
suite.Suite
db *gorm.DB
userRepo user.Repository
initialUser *model2.User
initialUser *model.User
}

func TestUserRepository(t *testing.T) {
Expand All @@ -30,15 +30,15 @@ func (t *UserRepositoryTest) SetupTest() {

assert.NoError(t.T(), err)

_ = db.Migrator().DropTable(&model2.User{})
_ = db.Migrator().DropTable(&model2.AuthSession{})
_ = db.Migrator().DropTable(&model.User{})
_ = db.Migrator().DropTable(&model.AuthSession{})

err = db.AutoMigrate(&model2.User{}, &model2.AuthSession{})
err = db.AutoMigrate(&model.User{}, &model.AuthSession{})
assert.NoError(t.T(), err)

userRepository := NewRepository(db)

initialUser := &model2.User{
initialUser := &model.User{
Email: faker.Email(),
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand All @@ -56,14 +56,14 @@ func (t *UserRepositoryTest) SetupTest() {
}

func (t *UserRepositoryTest) TestFindAllSuccess() {
users := &[]*model2.User{}
users := &[]*model.User{}
err := t.userRepo.FindAll(users)
assert.NoError(t.T(), err)
assert.NotEmpty(t.T(), *users)
}

func (t *UserRepositoryTest) TestFindByIdSuccess() {
user := &model2.User{}
user := &model.User{}
err := t.userRepo.FindById(t.initialUser.ID.String(), user)
assert.NoError(t.T(), err)
assert.Equal(t.T(), t.initialUser.ID, user.ID)
Expand All @@ -72,28 +72,28 @@ func (t *UserRepositoryTest) TestFindByIdSuccess() {
func (t *UserRepositoryTest) TestFindByIdNotFound() {
notFoundId := faker.UUIDDigit()

user := &model2.User{}
user := &model.User{}
err := t.userRepo.FindById(notFoundId, user)
assert.Equal(t.T(), gorm.ErrRecordNotFound, err)
}

func (t *UserRepositoryTest) TestFindByEmailSuccess() {
user := &model2.User{}
user := &model.User{}
email := t.initialUser.Email
err := t.userRepo.FindByEmail(email, user)
assert.NoError(t.T(), err)
assert.Equal(t.T(), t.initialUser.ID, user.ID)
}

func (t *UserRepositoryTest) TestFindByEmailNotFound() {
user := &model2.User{}
user := &model.User{}
notFoundEmail := faker.Email()
err := t.userRepo.FindByEmail(notFoundEmail, user)
assert.Equal(t.T(), gorm.ErrRecordNotFound, err)
}

func (t *UserRepositoryTest) TestCreateSuccess() {
createUser := &model2.User{
createUser := &model.User{
Email: faker.Email(),
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand All @@ -106,7 +106,7 @@ func (t *UserRepositoryTest) TestCreateSuccess() {
}

func (t *UserRepositoryTest) TestCreateDuplicateEmail() {
createUser := &model2.User{
createUser := &model.User{
Email: t.initialUser.Email,
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand All @@ -119,7 +119,7 @@ func (t *UserRepositoryTest) TestCreateDuplicateEmail() {
}

func (t *UserRepositoryTest) TestUpdateSuccess() {
updateUser := &model2.User{
updateUser := &model.User{
Email: faker.Email(),
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand All @@ -132,7 +132,7 @@ func (t *UserRepositoryTest) TestUpdateSuccess() {
}

func (t *UserRepositoryTest) TestUpdateDuplicateEmail() {
createUser := &model2.User{
createUser := &model.User{
Email: faker.Email(),
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand All @@ -143,7 +143,7 @@ func (t *UserRepositoryTest) TestUpdateDuplicateEmail() {
err := t.userRepo.Create(createUser)
assert.NoError(t.T(), err)

updateUser := &model2.User{
updateUser := &model.User{
Email: createUser.Email,
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand All @@ -156,7 +156,7 @@ func (t *UserRepositoryTest) TestUpdateDuplicateEmail() {
}

func (t *UserRepositoryTest) TestUpdateNotFound() {
updateUser := &model2.User{
updateUser := &model.User{
Email: faker.Email(),
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand All @@ -170,7 +170,7 @@ func (t *UserRepositoryTest) TestUpdateNotFound() {
}

func (t *UserRepositoryTest) TestDeleteSuccess() {
createUser := &model2.User{
createUser := &model.User{
Email: faker.Email(),
Password: faker.Password(),
Firstname: faker.FirstName(),
Expand Down
32 changes: 16 additions & 16 deletions internal/service/auth/auth.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package auth

import (
"context"
constant2 "github.com/isd-sgcu/johnjud-auth/internal/constant"
model2 "github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"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"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/auth"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/user"
Expand Down Expand Up @@ -49,22 +49,22 @@ func (s *serviceImpl) RefreshToken(_ context.Context, request *authProto.Refresh
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, constant2.InternalServerErrorMessage)
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

createUser := &model2.User{
createUser := &model.User{
Email: request.Email,
Password: hashPassword,
Firstname: request.FirstName,
Lastname: request.LastName,
Role: constant2.USER,
Role: constant.USER,
}
err = s.userRepo.Create(createUser)
if err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
return nil, status.Error(codes.AlreadyExists, constant2.DuplicateEmailErrorMessage)
return nil, status.Error(codes.AlreadyExists, constant.DuplicateEmailErrorMessage)
}
return nil, status.Error(codes.Internal, constant2.InternalServerErrorMessage)
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

return &authProto.SignUpResponse{
Expand All @@ -76,20 +76,20 @@ func (s *serviceImpl) SignUp(_ context.Context, request *authProto.SignUpRequest
}

func (s *serviceImpl) SignIn(_ context.Context, request *authProto.SignInRequest) (*authProto.SignInResponse, error) {
user := &model2.User{}
user := &model.User{}
err := s.userRepo.FindByEmail(request.Email, user)
if err != nil {
return nil, status.Error(codes.PermissionDenied, constant2.IncorrectEmailPasswordErrorMessage)
return nil, status.Error(codes.PermissionDenied, constant.IncorrectEmailPasswordErrorMessage)
}

err = s.bcryptUtil.CompareHashedPassword(user.Password, request.Password)
if err != nil {
return nil, status.Error(codes.PermissionDenied, constant2.IncorrectEmailPasswordErrorMessage)
return nil, status.Error(codes.PermissionDenied, constant.IncorrectEmailPasswordErrorMessage)
}

credential, err := s.createAuthSession(user.ID, user.Role)
if err != nil {
return nil, status.Error(codes.Internal, constant2.InternalServerErrorMessage)
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

return &authProto.SignInResponse{Credential: credential}, nil
Expand All @@ -98,24 +98,24 @@ func (s *serviceImpl) SignIn(_ context.Context, request *authProto.SignInRequest
func (s *serviceImpl) SignOut(_ context.Context, request *authProto.SignOutRequest) (*authProto.SignOutResponse, error) {
userCredential, err := s.tokenService.Validate(request.Token)
if err != nil {
return nil, status.Error(codes.Internal, constant2.InternalServerErrorMessage)
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

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

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

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

func (s *serviceImpl) createAuthSession(userId uuid.UUID, role constant2.Role) (*authProto.Credential, error) {
createAuthSession := &model2.AuthSession{
func (s *serviceImpl) createAuthSession(userId uuid.UUID, role constant.Role) (*authProto.Credential, error) {
createAuthSession := &model.AuthSession{
UserID: userId,
}
err := s.authRepo.Create(createAuthSession)
Expand Down
Loading

0 comments on commit fbf16df

Please sign in to comment.