Skip to content
Snippets Groups Projects
Commit f47e0276 authored by Bayu Samudra's avatar Bayu Samudra
Browse files

feat: adding test

parent a1904683
1 merge request!5Feat/s1 sb8 login api (emergency merge)
Pipeline #51659 passed with stage
in 2 minutes and 31 seconds
......@@ -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{
......
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")
})
}
......@@ -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
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment