|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestComparePassword(t *testing.T) { |
| 10 | + t.Run("ComparePassword", func(t *testing.T) { |
| 11 | + // Create a new PasswordHasher |
| 12 | + passwordHasher := NewPasswordHasher() |
| 13 | + |
| 14 | + // Hash a password |
| 15 | + hashedPassword, err := passwordHasher.HashPassword("password") |
| 16 | + assert.NoError(t, err, "HashPassword should not return an error") |
| 17 | + |
| 18 | + // Compare the password |
| 19 | + err = passwordHasher.ComparePassword(hashedPassword, "password") |
| 20 | + assert.NoError(t, err, "ComparePassword should not return an error") |
| 21 | + |
| 22 | + }) |
| 23 | + |
| 24 | + t.Run("ComparePasswordFail", func(t *testing.T) { |
| 25 | + // Create a new PasswordHasher |
| 26 | + passwordHasher := NewPasswordHasher() |
| 27 | + |
| 28 | + // Hash a password |
| 29 | + hashedPassword, err := passwordHasher.HashPassword("password") |
| 30 | + assert.NoError(t, err, "HashPassword should not return an error") |
| 31 | + |
| 32 | + // Compare the password |
| 33 | + err = passwordHasher.ComparePassword(hashedPassword, "wrong_password") |
| 34 | + assert.Error(t, err, "ComparePassword should return an error") |
| 35 | + }) |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +func TestHashPassword(t *testing.T) { |
| 40 | + t.Run("HashPassword", func(t *testing.T) { |
| 41 | + // Create a new PasswordHasher |
| 42 | + passwordHasher := NewPasswordHasher() |
| 43 | + |
| 44 | + // Hash a password |
| 45 | + hashedPassword, err := passwordHasher.HashPassword("password") |
| 46 | + assert.NoError(t, err, "HashPassword should not return an error") |
| 47 | + assert.NotEmpty(t, hashedPassword, "Hashed password should not be empty") |
| 48 | + }) |
| 49 | +} |
0 commit comments