From f47e02761d1fa6643f9f5af56c279ad01822dc31 Mon Sep 17 00:00:00 2001 From: bayusamudra5502 <bayusamudra.55.02.com@gmail.com> Date: Thu, 16 Feb 2023 09:58:46 +0700 Subject: [PATCH] feat: adding test --- service/auth/login.go | 8 +++++-- test/utils/password/password_test.go | 31 ++++++++++++++++++++++++++++ utils/password/impl.go | 12 +++-------- 3 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 test/utils/password/password_test.go diff --git a/service/auth/login.go b/service/auth/login.go index 85d6de9..0ed6024 100644 --- a/service/auth/login.go +++ b/service/auth/login.go @@ -19,7 +19,7 @@ func (auth AuthServiceImpl) Login(payload login.LoginRequestPayload) (*login.Log switch { case errors.Is(err, gorm.ErrRecordNotFound): - errorObj = fmt.Errorf("username or password combination not found") + errorObj = fmt.Errorf("username and password combination not found") default: errorObj = err } @@ -28,7 +28,11 @@ func (auth AuthServiceImpl) Login(payload login.LoginRequestPayload) (*login.Log } if err := auth.Check(payload.Password, user.Password); err != nil { - return nil, err + return nil, fmt.Errorf("username and password combination not found") + } + + if !user.IsActivated { + return nil, fmt.Errorf("user is not activated yet") } refreshClaim := tokenModel.UserClaim{ diff --git a/test/utils/password/password_test.go b/test/utils/password/password_test.go new file mode 100644 index 0000000..e7205b8 --- /dev/null +++ b/test/utils/password/password_test.go @@ -0,0 +1,31 @@ +package password_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "gitlab.informatika.org/ocw/ocw-backend/utils/password" +) + +func TestPasswordHash(t *testing.T) { + obj := password.PasswordUtilImpl{} + + t.Run("PasswordCanBeHashed", func(t *testing.T) { + _, err := obj.Hash("admin") + + assert.Nil(t, err) + }) + + t.Run("PasswordCanBeHashAndValidateCorrectly", func(t *testing.T) { + hash, err := obj.Hash("admin") + + assert.Nil(t, err) + + err = obj.Check("admin", hash) + assert.Nil(t, err) + + err = obj.Check("seseorang", hash) + assert.NotNil(t, err) + assert.Equal(t, err.Error(), "password mismatch") + }) +} diff --git a/utils/password/impl.go b/utils/password/impl.go index f9cb7ab..9a660b4 100644 --- a/utils/password/impl.go +++ b/utils/password/impl.go @@ -15,20 +15,14 @@ type PasswordUtilImpl struct { func (e PasswordUtilImpl) Hash(password string) (string, error) { hash, err := bcrypt.GenerateFromPassword([]byte(password), e.Environment.PasswordCost) - return e.Base64Util.Encode(hash), err + return string(hash), err } func (e PasswordUtilImpl) Check(password string, hashedPassword string) error { - hash, err := e.Base64Util.Decode(hashedPassword) + err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) if err != nil { - return err - } - - err = bcrypt.CompareHashAndPassword(hash, []byte(password)) - - if err != nil { - return fmt.Errorf("username or password combination is not found") + return fmt.Errorf("password mismatch") } return nil -- GitLab