|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
| 12 | +) |
| 13 | + |
| 14 | +func writeTestConfigFile(t *testing.T) string { |
| 15 | + t.Helper() |
| 16 | + |
| 17 | + dir := t.TempDir() |
| 18 | + path := filepath.Join(dir, "config.yaml") |
| 19 | + if errWrite := os.WriteFile(path, []byte("{}\n"), 0o600); errWrite != nil { |
| 20 | + t.Fatalf("failed to write test config: %v", errWrite) |
| 21 | + } |
| 22 | + return path |
| 23 | +} |
| 24 | + |
| 25 | +func TestDeleteGeminiKey_RequiresBaseURLWhenAPIKeyDuplicated(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + gin.SetMode(gin.TestMode) |
| 28 | + |
| 29 | + h := &Handler{ |
| 30 | + cfg: &config.Config{ |
| 31 | + GeminiKey: []config.GeminiKey{ |
| 32 | + {APIKey: "shared-key", BaseURL: "https://a.example.com"}, |
| 33 | + {APIKey: "shared-key", BaseURL: "https://b.example.com"}, |
| 34 | + }, |
| 35 | + }, |
| 36 | + configFilePath: writeTestConfigFile(t), |
| 37 | + } |
| 38 | + |
| 39 | + rec := httptest.NewRecorder() |
| 40 | + c, _ := gin.CreateTestContext(rec) |
| 41 | + c.Request = httptest.NewRequest(http.MethodDelete, "/v0/management/gemini-api-key?api-key=shared-key", nil) |
| 42 | + |
| 43 | + h.DeleteGeminiKey(c) |
| 44 | + |
| 45 | + if rec.Code != http.StatusBadRequest { |
| 46 | + t.Fatalf("status = %d, want %d; body=%s", rec.Code, http.StatusBadRequest, rec.Body.String()) |
| 47 | + } |
| 48 | + if got := len(h.cfg.GeminiKey); got != 2 { |
| 49 | + t.Fatalf("gemini keys len = %d, want 2", got) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestDeleteGeminiKey_DeletesOnlyMatchingBaseURL(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + gin.SetMode(gin.TestMode) |
| 56 | + |
| 57 | + h := &Handler{ |
| 58 | + cfg: &config.Config{ |
| 59 | + GeminiKey: []config.GeminiKey{ |
| 60 | + {APIKey: "shared-key", BaseURL: "https://a.example.com"}, |
| 61 | + {APIKey: "shared-key", BaseURL: "https://b.example.com"}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + configFilePath: writeTestConfigFile(t), |
| 65 | + } |
| 66 | + |
| 67 | + rec := httptest.NewRecorder() |
| 68 | + c, _ := gin.CreateTestContext(rec) |
| 69 | + c.Request = httptest.NewRequest(http.MethodDelete, "/v0/management/gemini-api-key?api-key=shared-key&base-url=https://a.example.com", nil) |
| 70 | + |
| 71 | + h.DeleteGeminiKey(c) |
| 72 | + |
| 73 | + if rec.Code != http.StatusOK { |
| 74 | + t.Fatalf("status = %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String()) |
| 75 | + } |
| 76 | + if got := len(h.cfg.GeminiKey); got != 1 { |
| 77 | + t.Fatalf("gemini keys len = %d, want 1", got) |
| 78 | + } |
| 79 | + if got := h.cfg.GeminiKey[0].BaseURL; got != "https://b.example.com" { |
| 80 | + t.Fatalf("remaining base-url = %q, want %q", got, "https://b.example.com") |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestDeleteClaudeKey_DeletesEmptyBaseURLWhenExplicitlyProvided(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + gin.SetMode(gin.TestMode) |
| 87 | + |
| 88 | + h := &Handler{ |
| 89 | + cfg: &config.Config{ |
| 90 | + ClaudeKey: []config.ClaudeKey{ |
| 91 | + {APIKey: "shared-key", BaseURL: ""}, |
| 92 | + {APIKey: "shared-key", BaseURL: "https://claude.example.com"}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + configFilePath: writeTestConfigFile(t), |
| 96 | + } |
| 97 | + |
| 98 | + rec := httptest.NewRecorder() |
| 99 | + c, _ := gin.CreateTestContext(rec) |
| 100 | + c.Request = httptest.NewRequest(http.MethodDelete, "/v0/management/claude-api-key?api-key=shared-key&base-url=", nil) |
| 101 | + |
| 102 | + h.DeleteClaudeKey(c) |
| 103 | + |
| 104 | + if rec.Code != http.StatusOK { |
| 105 | + t.Fatalf("status = %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String()) |
| 106 | + } |
| 107 | + if got := len(h.cfg.ClaudeKey); got != 1 { |
| 108 | + t.Fatalf("claude keys len = %d, want 1", got) |
| 109 | + } |
| 110 | + if got := h.cfg.ClaudeKey[0].BaseURL; got != "https://claude.example.com" { |
| 111 | + t.Fatalf("remaining base-url = %q, want %q", got, "https://claude.example.com") |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestDeleteVertexCompatKey_DeletesOnlyMatchingBaseURL(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + gin.SetMode(gin.TestMode) |
| 118 | + |
| 119 | + h := &Handler{ |
| 120 | + cfg: &config.Config{ |
| 121 | + VertexCompatAPIKey: []config.VertexCompatKey{ |
| 122 | + {APIKey: "shared-key", BaseURL: "https://a.example.com"}, |
| 123 | + {APIKey: "shared-key", BaseURL: "https://b.example.com"}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + configFilePath: writeTestConfigFile(t), |
| 127 | + } |
| 128 | + |
| 129 | + rec := httptest.NewRecorder() |
| 130 | + c, _ := gin.CreateTestContext(rec) |
| 131 | + c.Request = httptest.NewRequest(http.MethodDelete, "/v0/management/vertex-api-key?api-key=shared-key&base-url=https://b.example.com", nil) |
| 132 | + |
| 133 | + h.DeleteVertexCompatKey(c) |
| 134 | + |
| 135 | + if rec.Code != http.StatusOK { |
| 136 | + t.Fatalf("status = %d, want %d; body=%s", rec.Code, http.StatusOK, rec.Body.String()) |
| 137 | + } |
| 138 | + if got := len(h.cfg.VertexCompatAPIKey); got != 1 { |
| 139 | + t.Fatalf("vertex keys len = %d, want 1", got) |
| 140 | + } |
| 141 | + if got := h.cfg.VertexCompatAPIKey[0].BaseURL; got != "https://a.example.com" { |
| 142 | + t.Fatalf("remaining base-url = %q, want %q", got, "https://a.example.com") |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestDeleteCodexKey_RequiresBaseURLWhenAPIKeyDuplicated(t *testing.T) { |
| 147 | + t.Parallel() |
| 148 | + gin.SetMode(gin.TestMode) |
| 149 | + |
| 150 | + h := &Handler{ |
| 151 | + cfg: &config.Config{ |
| 152 | + CodexKey: []config.CodexKey{ |
| 153 | + {APIKey: "shared-key", BaseURL: "https://a.example.com"}, |
| 154 | + {APIKey: "shared-key", BaseURL: "https://b.example.com"}, |
| 155 | + }, |
| 156 | + }, |
| 157 | + configFilePath: writeTestConfigFile(t), |
| 158 | + } |
| 159 | + |
| 160 | + rec := httptest.NewRecorder() |
| 161 | + c, _ := gin.CreateTestContext(rec) |
| 162 | + c.Request = httptest.NewRequest(http.MethodDelete, "/v0/management/codex-api-key?api-key=shared-key", nil) |
| 163 | + |
| 164 | + h.DeleteCodexKey(c) |
| 165 | + |
| 166 | + if rec.Code != http.StatusBadRequest { |
| 167 | + t.Fatalf("status = %d, want %d; body=%s", rec.Code, http.StatusBadRequest, rec.Body.String()) |
| 168 | + } |
| 169 | + if got := len(h.cfg.CodexKey); got != 2 { |
| 170 | + t.Fatalf("codex keys len = %d, want 2", got) |
| 171 | + } |
| 172 | +} |
0 commit comments