|
| 1 | +package cpa |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestFetchAPIKeys(t *testing.T) { |
| 13 | + expectedToken := "test-mgmt-key" |
| 14 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 15 | + if r.URL.Path != "/v0/management/api-keys" { |
| 16 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 17 | + http.NotFound(w, r) |
| 18 | + return |
| 19 | + } |
| 20 | + auth := r.Header.Get("Authorization") |
| 21 | + if auth != "Bearer "+expectedToken { |
| 22 | + t.Errorf("unexpected auth header: %s", auth) |
| 23 | + http.Error(w, "unauthorized", http.StatusUnauthorized) |
| 24 | + return |
| 25 | + } |
| 26 | + w.Header().Set("Content-Type", "application/json") |
| 27 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 28 | + "api-keys": []string{ |
| 29 | + " key-1 ", |
| 30 | + "", |
| 31 | + "key-2", |
| 32 | + "key-1", |
| 33 | + " ", |
| 34 | + "key-3", |
| 35 | + }, |
| 36 | + }) |
| 37 | + })) |
| 38 | + defer server.Close() |
| 39 | + |
| 40 | + ctx := context.Background() |
| 41 | + keys, err := FetchAPIKeys(ctx, server.URL, expectedToken) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("FetchAPIKeys failed: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + expected := []string{"key-1", "key-2", "key-3"} |
| 47 | + if !reflect.DeepEqual(keys, expected) { |
| 48 | + t.Errorf("expected keys %v, got %v", expected, keys) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestFetchModelsWithAPIKey(t *testing.T) { |
| 53 | + expectedToken := "client-key" |
| 54 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 55 | + if r.URL.Path != "/v1/models" { |
| 56 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 57 | + http.NotFound(w, r) |
| 58 | + return |
| 59 | + } |
| 60 | + auth := r.Header.Get("Authorization") |
| 61 | + if auth != "Bearer "+expectedToken { |
| 62 | + t.Errorf("unexpected auth header: %s", auth) |
| 63 | + http.Error(w, "unauthorized", http.StatusUnauthorized) |
| 64 | + return |
| 65 | + } |
| 66 | + w.Header().Set("Content-Type", "application/json") |
| 67 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 68 | + "object": "list", |
| 69 | + "data": []map[string]any{ |
| 70 | + {"id": "gpt-b"}, |
| 71 | + {"id": " gpt-a "}, |
| 72 | + {"id": "gpt-a"}, |
| 73 | + {"id": ""}, |
| 74 | + }, |
| 75 | + }) |
| 76 | + })) |
| 77 | + defer server.Close() |
| 78 | + |
| 79 | + ctx := context.Background() |
| 80 | + models, err := FetchModels(ctx, server.URL, expectedToken) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("FetchModels failed: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + expected := []string{"gpt-a", "gpt-b"} |
| 86 | + if !reflect.DeepEqual(models, expected) { |
| 87 | + t.Errorf("expected models %v, got %v", expected, models) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestFetchModelsAnonymous(t *testing.T) { |
| 92 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 93 | + if r.URL.Path != "/v1/models" { |
| 94 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 95 | + http.NotFound(w, r) |
| 96 | + return |
| 97 | + } |
| 98 | + if auth := r.Header.Get("Authorization"); auth != "" { |
| 99 | + t.Errorf("expected no authorization header, got: %s", auth) |
| 100 | + } |
| 101 | + w.Header().Set("Content-Type", "application/json") |
| 102 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 103 | + "object": "list", |
| 104 | + "data": []map[string]any{ |
| 105 | + {"id": "anon-model-2"}, |
| 106 | + {"id": "anon-model-1"}, |
| 107 | + }, |
| 108 | + }) |
| 109 | + })) |
| 110 | + defer server.Close() |
| 111 | + |
| 112 | + ctx := context.Background() |
| 113 | + models, err := FetchModels(ctx, server.URL, "") |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("FetchModels failed: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + expected := []string{"anon-model-1", "anon-model-2"} |
| 119 | + if !reflect.DeepEqual(models, expected) { |
| 120 | + t.Errorf("expected models %v, got %v", expected, models) |
| 121 | + } |
| 122 | +} |
0 commit comments