|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
| 12 | + coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" |
| 13 | +) |
| 14 | + |
| 15 | +func TestListAuthFiles_IncludesRecentRequestsBuckets(t *testing.T) { |
| 16 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 17 | + gin.SetMode(gin.TestMode) |
| 18 | + |
| 19 | + manager := coreauth.NewManager(nil, nil, nil) |
| 20 | + record := &coreauth.Auth{ |
| 21 | + ID: "runtime-only-auth-1", |
| 22 | + Provider: "codex", |
| 23 | + Attributes: map[string]string{ |
| 24 | + "runtime_only": "true", |
| 25 | + }, |
| 26 | + Metadata: map[string]any{ |
| 27 | + "type": "codex", |
| 28 | + }, |
| 29 | + } |
| 30 | + if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { |
| 31 | + t.Fatalf("failed to register auth record: %v", errRegister) |
| 32 | + } |
| 33 | + |
| 34 | + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) |
| 35 | + h.tokenStore = &memoryAuthStore{} |
| 36 | + |
| 37 | + rec := httptest.NewRecorder() |
| 38 | + ginCtx, _ := gin.CreateTestContext(rec) |
| 39 | + req := httptest.NewRequest(http.MethodGet, "/v0/management/auth-files", nil) |
| 40 | + ginCtx.Request = req |
| 41 | + |
| 42 | + h.ListAuthFiles(ginCtx) |
| 43 | + |
| 44 | + if rec.Code != http.StatusOK { |
| 45 | + t.Fatalf("expected list status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String()) |
| 46 | + } |
| 47 | + |
| 48 | + var payload map[string]any |
| 49 | + if errUnmarshal := json.Unmarshal(rec.Body.Bytes(), &payload); errUnmarshal != nil { |
| 50 | + t.Fatalf("failed to decode list payload: %v", errUnmarshal) |
| 51 | + } |
| 52 | + filesRaw, ok := payload["files"].([]any) |
| 53 | + if !ok { |
| 54 | + t.Fatalf("expected files array, payload: %#v", payload) |
| 55 | + } |
| 56 | + if len(filesRaw) != 1 { |
| 57 | + t.Fatalf("expected 1 auth entry, got %d", len(filesRaw)) |
| 58 | + } |
| 59 | + |
| 60 | + fileEntry, ok := filesRaw[0].(map[string]any) |
| 61 | + if !ok { |
| 62 | + t.Fatalf("expected file entry object, got %#v", filesRaw[0]) |
| 63 | + } |
| 64 | + |
| 65 | + recentRaw, ok := fileEntry["recent_requests"].([]any) |
| 66 | + if !ok { |
| 67 | + t.Fatalf("expected recent_requests array, got %#v", fileEntry["recent_requests"]) |
| 68 | + } |
| 69 | + if len(recentRaw) != 20 { |
| 70 | + t.Fatalf("expected 20 recent_requests buckets, got %d", len(recentRaw)) |
| 71 | + } |
| 72 | + for idx, item := range recentRaw { |
| 73 | + bucket, ok := item.(map[string]any) |
| 74 | + if !ok { |
| 75 | + t.Fatalf("expected bucket object at %d, got %#v", idx, item) |
| 76 | + } |
| 77 | + if _, ok := bucket["time"].(string); !ok { |
| 78 | + t.Fatalf("expected bucket time string at %d, got %#v", idx, bucket["time"]) |
| 79 | + } |
| 80 | + if _, ok := bucket["success"].(float64); !ok { |
| 81 | + t.Fatalf("expected bucket success number at %d, got %#v", idx, bucket["success"]) |
| 82 | + } |
| 83 | + if _, ok := bucket["failed"].(float64); !ok { |
| 84 | + t.Fatalf("expected bucket failed number at %d, got %#v", idx, bucket["failed"]) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments