mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
The auth snapshot built users from the key digest map only. The keyless principal never appeared in usage, catalog, metrics or fleet views, and its rejection counter was pruned on a users-file reload. The snapshot now adds the anonymous principal once, with source anonymous.
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func anonSnapshotGateway(t *testing.T, anonGroup string) (http.Handler, *Gateway) {
|
|
t.Helper()
|
|
return memberLimitsGateway(t,
|
|
map[string]*GroupConfig{"friends": {}, "public": {}},
|
|
[]*UserConfig{{Name: "a", Group: "friends", APIKey: "sk-a", Admin: true}},
|
|
func(c *Config) {
|
|
c.Users.AnonymousGroup = anonGroup
|
|
c.Metrics = &MetricsConfig{Enabled: true}
|
|
},
|
|
)
|
|
}
|
|
|
|
func TestSnapshot_IncludesAnonymousPrincipal(t *testing.T) {
|
|
a, err := newAuthStore(anonAuthCfg())
|
|
require.NoError(t, err)
|
|
users, groups := a.snapshot()
|
|
|
|
var anon []userSummary
|
|
for _, u := range users {
|
|
if u.name == anonymousUserName {
|
|
anon = append(anon, u)
|
|
}
|
|
}
|
|
require.Len(t, anon, 1)
|
|
assert.Equal(t, "anonymous", anon[0].source)
|
|
assert.False(t, anon[0].admin)
|
|
assert.Equal(t, "public", anon[0].groupName)
|
|
assert.Equal(t, []string{"public"}, anon[0].groups)
|
|
for _, g := range groups {
|
|
if g.name == "public" {
|
|
assert.Equal(t, 1, g.memberCount)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSnapshot_NoAnonymousWhenUnset(t *testing.T) {
|
|
cfg := anonAuthCfg()
|
|
cfg.Users.AnonymousGroup = ""
|
|
a, err := newAuthStore(cfg)
|
|
require.NoError(t, err)
|
|
users, _ := a.snapshot()
|
|
for _, u := range users {
|
|
assert.NotEqual(t, anonymousUserName, u.name)
|
|
}
|
|
}
|
|
|
|
func TestAdminUsage_ListsAnonymousUser(t *testing.T) {
|
|
h, gw := anonSnapshotGateway(t, "public")
|
|
rec := memberLimitsChat(t, h, "")
|
|
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
|
|
|
e := findUsageEntry(t, gw.buildAdminUsage().Users, anonymousUserName)
|
|
assert.NotZero(t, e.RequestsPerDay)
|
|
}
|
|
|
|
func TestScopeExists_AnonymousUser(t *testing.T) {
|
|
h, _ := anonSnapshotGateway(t, "public")
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminUsageHistoryPath+"?scope=user:anonymous&metric=req&window=hour", "sk-a"))
|
|
assert.NotEqual(t, http.StatusNotFound, rec.Code, rec.Body.String())
|
|
}
|
|
|
|
func TestPruneRejectionsAfterReload_KeepsAnonymous(t *testing.T) {
|
|
_, gw := anonSnapshotGateway(t, "public")
|
|
gw.limiter.rejections.increment("user", anonymousUserName)
|
|
gw.pruneRejectionsAfterReload()
|
|
|
|
var kept bool
|
|
for _, s := range gw.limiter.rejections.snapshot() {
|
|
if s.kind == "user" && s.id == anonymousUserName {
|
|
kept = true
|
|
}
|
|
}
|
|
assert.True(t, kept, "anonymous rejection counter must survive a users-file reload")
|
|
}
|
|
|
|
func TestUsageMetrics_ExportAnonymousUser(t *testing.T) {
|
|
h, _ := anonSnapshotGateway(t, "public")
|
|
require.Equal(t, http.StatusOK, memberLimitsChat(t, h, "").Code)
|
|
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, metricsRequest(http.MethodGet, metricsPathDefault, "sk-a"))
|
|
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
|
_, samples := parsePrometheusText(t, rec.Body.Bytes())
|
|
var found bool
|
|
for _, s := range samples {
|
|
if s.name == "llmgateway_requests_total" && s.labels["scope_kind"] == "user" && s.labels["scope_id"] == anonymousUserName {
|
|
found = true
|
|
}
|
|
}
|
|
assert.True(t, found)
|
|
}
|