Repository navigation
Expand file tree
/
Copy pathauth_test.go
More file actions
133 lines (110 loc) · 3.11 KB
/
Copy pathauth_test.go
File metadata and controls
133 lines (110 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package auth
import (
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
"staticsend/pkg/models"
)
func TestHashPassword(t *testing.T) {
password := "testpassword123"
hash, err := HashPassword(password)
if err != nil {
t.Fatalf("HashPassword failed: %v", err)
}
if hash == "" {
t.Error("HashPassword returned empty string")
}
// Test that the same password produces different hashes (due to salt)
hash2, err := HashPassword(password)
if err != nil {
t.Fatalf("HashPassword failed on second call: %v", err)
}
if hash == hash2 {
t.Error("HashPassword produced identical hashes for same password")
}
}
func TestCheckPassword(t *testing.T) {
password := "testpassword123"
hash, err := HashPassword(password)
if err != nil {
t.Fatalf("HashPassword failed: %v", err)
}
// Test correct password
err = CheckPassword(password, hash)
if err != nil {
t.Errorf("CheckPassword failed with correct password: %v", err)
}
// Test incorrect password
err = CheckPassword("wrongpassword", hash)
if err == nil {
t.Error("CheckPassword should have failed with incorrect password")
}
}
func TestGenerateAndValidateToken(t *testing.T) {
user := &models.User{
ID: 1,
Email: "test@example.com",
}
secretKey, err := GenerateSecretKey()
if err != nil {
t.Fatalf("GenerateSecretKey failed: %v", err)
}
// Generate token
tokenString, err := GenerateToken(user, secretKey)
if err != nil {
t.Fatalf("GenerateToken failed: %v", err)
}
if tokenString == "" {
t.Error("GenerateToken returned empty string")
}
// Validate token
claims, err := ValidateToken(tokenString, secretKey)
if err != nil {
t.Fatalf("ValidateToken failed: %v", err)
}
// Check claims
if claims["sub"] != float64(user.ID) {
t.Errorf("Expected sub claim %d, got %v", user.ID, claims["sub"])
}
if claims["email"] != user.Email {
t.Errorf("Expected email claim %s, got %v", user.Email, claims["email"])
}
// Test with wrong secret key
wrongKey := []byte("wrong-secret-key")
_, err = ValidateToken(tokenString, wrongKey)
if err == nil {
t.Error("ValidateToken should have failed with wrong secret key")
}
// Test with expired token (manipulate claims)
expiredClaims := jwt.MapClaims{
"sub": user.ID,
"email": user.Email,
"exp": time.Now().Add(-time.Hour).Unix(), // Expired 1 hour ago
"iat": time.Now().Add(-2 * time.Hour).Unix(),
}
expiredToken := jwt.NewWithClaims(jwt.SigningMethodHS256, expiredClaims)
expiredTokenString, err := expiredToken.SignedString(secretKey)
if err != nil {
t.Fatalf("Failed to create expired token: %v", err)
}
_, err = ValidateToken(expiredTokenString, secretKey)
if err == nil {
t.Error("ValidateToken should have failed with expired token")
}
}
func TestGenerateSecretKey(t *testing.T) {
key1, err := GenerateSecretKey()
if err != nil {
t.Fatalf("GenerateSecretKey failed: %v", err)
}
key2, err := GenerateSecretKey()
if err != nil {
t.Fatalf("GenerateSecretKey failed on second call: %v", err)
}
if len(key1) != 32 {
t.Errorf("Expected key length 32, got %d", len(key1))
}
if string(key1) == string(key2) {
t.Error("GenerateSecretKey produced identical keys")
}
}