From 0f14cd8ad59c155afbcd79cae0b3eb9ac0dde613 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Wed, 2 Dec 2026 01:55:34 +0000 Subject: [PATCH] test(auth): pin the oidc override reload on cached tokens A users file reload that renames an override or changes its limits already reaches a cached token on the next request. The cached principal stores the override object, and a reload builds a new object. The new test uses the same token across two reloads. It checks the new name, the new limits, the accounting name, and that no extra key fetch happens. --- auth_oidc_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/auth_oidc_test.go b/auth_oidc_test.go index f40c7e0..bbcbdb3 100644 --- a/auth_oidc_test.go +++ b/auth_oidc_test.go @@ -242,3 +242,38 @@ func TestOIDC_XAPIKeyNotTreatedAsToken(t *testing.T) { t.Fatalf("discovery hits = %d, want 0", d) } } + +// A reload that renames an override or changes its limits must reach a token +// that is already cached, on the very next request. +func TestOIDC_ReloadReachesCachedTokenNameAndLimits(t *testing.T) { + fx := newOIDCGateway(t, nil) + tok := fx.token(t, nil) + reload := func(name string, rpm int64) { + t.Helper() + err := fx.gw.auth.replaceFileUsers([]*UserConfig{{Name: name, OIDCIdentity: "alice@x.com", Limits: &LimitsConfig{RequestsPerMinute: rpm}}}) + if err != nil { + t.Fatal(err) + } + } + reload("alice-a", 5) + if rec := fx.chat(t, tok); rec.Code != http.StatusOK { + t.Fatalf("status %d", rec.Code) + } + if fx.userRequests("alice-a") != 1 { + t.Fatal("first request must be accounted to alice-a") + } + reload("alice-b", 7) + u, _, st := fx.gw.auth.authenticate(adminRequest(http.MethodPost, "/v1/chat/completions", tok)) + if st != authOK || u.name != "alice-b" || u.limits.RequestsPerMinute != 7 { + t.Fatalf("status %v user %+v, want the renamed override with its new limits", st, u) + } + if rec := fx.chat(t, tok); rec.Code != http.StatusOK { + t.Fatalf("status %d", rec.Code) + } + if fx.userRequests("alice-b") != 1 { + t.Fatal("request after the rename must be accounted to alice-b") + } + if hits := fx.idp.jwksHits.Load(); hits != 1 { + t.Fatalf("jwks hits = %d: the token must stay cached across the reload", hits) + } +}