Skip to content

Commit f0b0051

Browse files
authored
apikeyauth: check cache miss in tests (#760)
1 parent 268089f commit f0b0051

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

extension/apikeyauthextension/authenticator_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ func TestAuthenticator_Caching(t *testing.T) {
187187
}
188188

189189
func TestAuthenticator_CacheKeyHeaders(t *testing.T) {
190-
srv := newMockElasticsearch(t, newCannedHasPrivilegesHandler(successfulResponse))
190+
var calls int
191+
srv := newMockElasticsearch(t, func(w http.ResponseWriter, r *http.Request) {
192+
h := newCannedHasPrivilegesHandler(successfulResponse)
193+
h.ServeHTTP(w, r)
194+
calls++
195+
})
191196
config := createDefaultConfig().(*Config)
192197
config.Cache.KeyHeaders = []string{"X-Tenant-Id"}
193198
authenticator := newTestAuthenticator(t, srv, config)
@@ -206,6 +211,7 @@ func TestAuthenticator_CacheKeyHeaders(t *testing.T) {
206211
clientInfo := client.FromContext(ctx)
207212
assert.Equal(t, user, clientInfo.Auth.GetAttribute("username"))
208213
assert.Equal(t, "id1", clientInfo.Auth.GetAttribute("api_key"))
214+
assert.Equal(t, 1, calls)
209215

210216
// Different x-tenant-id header value should result in a cache miss,
211217
// despite the API Key ID being the same.
@@ -217,10 +223,16 @@ func TestAuthenticator_CacheKeyHeaders(t *testing.T) {
217223
clientInfo = client.FromContext(ctx)
218224
assert.Equal(t, user, clientInfo.Auth.GetAttribute("username"))
219225
assert.Equal(t, "id1", clientInfo.Auth.GetAttribute("api_key"))
226+
assert.Equal(t, 2, calls)
220227
}
221228

222229
func TestAuthenticator_CacheKeyMetadata(t *testing.T) {
223-
srv := newMockElasticsearch(t, newCannedHasPrivilegesHandler(successfulResponse))
230+
var calls int
231+
srv := newMockElasticsearch(t, func(w http.ResponseWriter, r *http.Request) {
232+
h := newCannedHasPrivilegesHandler(successfulResponse)
233+
h.ServeHTTP(w, r)
234+
calls++
235+
})
224236
config := createDefaultConfig().(*Config)
225237
config.Cache.KeyMetadata = []string{"X-Tenant-Id"}
226238
authenticator := newTestAuthenticator(t, srv, config)
@@ -243,6 +255,7 @@ func TestAuthenticator_CacheKeyMetadata(t *testing.T) {
243255
clientInfo := client.FromContext(ctx)
244256
assert.Equal(t, user, clientInfo.Auth.GetAttribute("username"))
245257
assert.Equal(t, "id1", clientInfo.Auth.GetAttribute("api_key"))
258+
assert.Equal(t, 1, calls)
246259

247260
// Different x-tenant-id header value should result in a cache miss,
248261
// despite the API Key ID being the same.
@@ -258,6 +271,7 @@ func TestAuthenticator_CacheKeyMetadata(t *testing.T) {
258271
clientInfo = client.FromContext(ctx)
259272
assert.Equal(t, user, clientInfo.Auth.GetAttribute("username"))
260273
assert.Equal(t, "id1", clientInfo.Auth.GetAttribute("api_key"))
274+
assert.Equal(t, 2, calls)
261275
}
262276

263277
func TestAuthenticator_CacheTTL(t *testing.T) {

0 commit comments

Comments
 (0)