Skip to content

Commit 6a13bc4

Browse files
committed
add tests for utils in api-users
1 parent bc31c03 commit 6a13bc4

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGenerateToken(t *testing.T) {
10+
t.Run("GenerateToken", func(t *testing.T) {
11+
// Create a new JWTUtils
12+
jwtUtils := NewJWTUtils()
13+
14+
// Generate a token
15+
token, err := jwtUtils.GenerateToken("user_id")
16+
assert.NoError(t, err, "GenerateToken should not return an error")
17+
assert.NotEmpty(t, token, "Token should not be empty")
18+
})
19+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)