-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathauth_jwt_methods_test.go
More file actions
269 lines (238 loc) · 8.85 KB
/
auth_jwt_methods_test.go
File metadata and controls
269 lines (238 loc) · 8.85 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package jwt
import (
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestGinJWTMiddleware_FunctionalOptionsOnly(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("EnableRedisStoreDefault", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
// Test EnableRedisStore with no options (default)
result := middleware.EnableRedisStore()
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.NotNil(t, middleware.RedisConfig, "should set default Redis config")
assert.Equal(t, "localhost:6379", middleware.RedisConfig.Addr, "should set default address")
})
t.Run("EnableRedisStoreWithSingleOption", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
testAddr := "redis.example.com:6379"
// Test EnableRedisStore with single option
result := middleware.EnableRedisStore(WithRedisAddr(testAddr))
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.Equal(t, testAddr, middleware.RedisConfig.Addr, "should set custom address")
// Should still have defaults for other values
assert.Equal(t, "", middleware.RedisConfig.Password, "should have default empty password")
assert.Equal(t, 0, middleware.RedisConfig.DB, "should have default DB")
})
t.Run("EnableRedisStoreWithMultipleOptions", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
testAddr := "redis.example.com:6379"
testPassword := "testpass"
testDB := 5
// Test EnableRedisStore with multiple options
result := middleware.EnableRedisStore(
WithRedisAddr(testAddr),
WithRedisAuth(testPassword, testDB),
)
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.Equal(t, testAddr, middleware.RedisConfig.Addr, "should set custom address")
assert.Equal(t, testPassword, middleware.RedisConfig.Password, "should set custom password")
assert.Equal(t, testDB, middleware.RedisConfig.DB, "should set custom DB")
})
t.Run("EnableRedisStoreWithCacheOptions", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
cacheSize := 64 * 1024 * 1024 // 64MB
cacheTTL := 30 * time.Second
// Test EnableRedisStore with cache options
result := middleware.EnableRedisStore(
WithRedisCache(cacheSize, cacheTTL),
)
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.NotNil(t, middleware.RedisConfig, "should create Redis config")
assert.Equal(t, cacheSize, middleware.RedisConfig.CacheSize, "should set cache size")
assert.Equal(t, cacheTTL, middleware.RedisConfig.CacheTTL, "should set cache TTL")
})
t.Run("EnableRedisStoreWithPoolOptions", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
poolSize := 20
maxIdleTime := 2 * time.Hour
maxLifetime := 4 * time.Hour
// Test EnableRedisStore with pool options
result := middleware.EnableRedisStore(
WithRedisPool(poolSize, maxIdleTime, maxLifetime),
)
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.Equal(t, poolSize, middleware.RedisConfig.PoolSize, "should set pool size")
assert.Equal(
t,
maxIdleTime,
middleware.RedisConfig.ConnMaxIdleTime,
"should set max idle time",
)
assert.Equal(
t,
maxLifetime,
middleware.RedisConfig.ConnMaxLifetime,
"should set max lifetime",
)
})
t.Run("EnableRedisStoreWithKeyPrefix", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
keyPrefix := "test-app:"
// Test EnableRedisStore with key prefix
result := middleware.EnableRedisStore(
WithRedisKeyPrefix(keyPrefix),
)
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.Equal(t, keyPrefix, middleware.RedisConfig.KeyPrefix, "should set key prefix")
})
t.Run("EnableRedisStoreWithAllOptions", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
testAddr := "custom.redis.com:6379"
testPassword := "custom-password"
testDB := 3
cacheSize := 256 * 1024 * 1024 // 256MB
cacheTTL := 5 * time.Minute
poolSize := 25
maxIdleTime := 3 * time.Hour
maxLifetime := 6 * time.Hour
keyPrefix := "custom-prefix:"
// Test EnableRedisStore with all options
result := middleware.EnableRedisStore(
WithRedisAddr(testAddr),
WithRedisAuth(testPassword, testDB),
WithRedisCache(cacheSize, cacheTTL),
WithRedisPool(poolSize, maxIdleTime, maxLifetime),
WithRedisKeyPrefix(keyPrefix),
)
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.Equal(t, testAddr, middleware.RedisConfig.Addr, "should set address")
assert.Equal(t, testPassword, middleware.RedisConfig.Password, "should set password")
assert.Equal(t, testDB, middleware.RedisConfig.DB, "should set DB")
assert.Equal(t, cacheSize, middleware.RedisConfig.CacheSize, "should set cache size")
assert.Equal(t, cacheTTL, middleware.RedisConfig.CacheTTL, "should set cache TTL")
assert.Equal(t, poolSize, middleware.RedisConfig.PoolSize, "should set pool size")
assert.Equal(
t,
maxIdleTime,
middleware.RedisConfig.ConnMaxIdleTime,
"should set max idle time",
)
assert.Equal(
t,
maxLifetime,
middleware.RedisConfig.ConnMaxLifetime,
"should set max lifetime",
)
assert.Equal(t, keyPrefix, middleware.RedisConfig.KeyPrefix, "should set key prefix")
})
t.Run("MultipleEnableRedisStoreCalls", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
// First call
middleware.EnableRedisStore(
WithRedisAddr("first.redis.com:6379"),
WithRedisAuth("first-pass", 1),
)
// Second call should override the first
result := middleware.EnableRedisStore(
WithRedisAddr("second.redis.com:6379"),
WithRedisAuth("second-pass", 2),
WithRedisKeyPrefix("second:"),
)
assert.Equal(t, middleware, result, "should return self for chaining")
assert.True(t, middleware.UseRedisStore, "should enable Redis store")
assert.Equal(
t,
"second.redis.com:6379",
middleware.RedisConfig.Addr,
"should use second address",
)
assert.Equal(
t,
"second-pass",
middleware.RedisConfig.Password,
"should use second password",
)
assert.Equal(t, 2, middleware.RedisConfig.DB, "should use second DB")
assert.Equal(t, "second:", middleware.RedisConfig.KeyPrefix, "should use second key prefix")
})
t.Run("DefaultConfiguration", func(t *testing.T) {
middleware := &GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
IdentityKey: "id",
}
// Test EnableRedisStore with defaults
result := middleware.EnableRedisStore()
config := result.RedisConfig
// Verify all default values
assert.Equal(t, "localhost:6379", config.Addr, "should have default address")
assert.Equal(t, "", config.Password, "should have empty default password")
assert.Equal(t, 0, config.DB, "should have default DB")
assert.Equal(t, 128*1024*1024, config.CacheSize, "should have default cache size")
assert.Equal(t, time.Minute, config.CacheTTL, "should have default cache TTL")
assert.Equal(t, 10, config.PoolSize, "should have default pool size")
assert.Equal(t, 30*time.Minute, config.ConnMaxIdleTime, "should have default max idle time")
assert.Equal(t, time.Hour, config.ConnMaxLifetime, "should have default max lifetime")
assert.Equal(t, "gin-jwt:", config.KeyPrefix, "should have default key prefix")
})
}