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) + } +}