package management import ( "context" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" "github.com/gin-gonic/gin" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" fileauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth" coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" ) func TestPatchAuthFileFields_MergeHeadersAndDeleteEmptyValues(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "") store := &memoryAuthStore{} manager := coreauth.NewManager(store, nil, nil) record := &coreauth.Auth{ ID: "test.json", FileName: "test.json", Provider: "claude", Attributes: map[string]string{ "path": "/tmp/test.json", "header:X-Old": "old", "header:X-Remove": "gone", }, Metadata: map[string]any{ "type": "claude", "headers": map[string]any{ "X-Old": "old", "X-Remove": "gone", }, }, } if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { t.Fatalf("failed to register auth record: %v", errRegister) } h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) body := `{"name":"test.json","prefix":"p1","proxy_url":"http://proxy.local","headers":{"X-Old":"new","X-New":"v","X-Remove":" ","X-Nope":""}}` rec := httptest.NewRecorder() ctx, _ := gin.CreateTestContext(rec) req := httptest.NewRequest(http.MethodPatch, "/v0/management/auth-files/fields", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") ctx.Request = req h.PatchAuthFileFields(ctx) if rec.Code != http.StatusOK { t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String()) } updated, ok := manager.GetByID("test.json") if !ok || updated == nil { t.Fatalf("expected auth record to exist after patch") } if updated.Prefix != "p1" { t.Fatalf("prefix = %q, want %q", updated.Prefix, "p1") } if updated.ProxyURL != "http://proxy.local" { t.Fatalf("proxy_url = %q, want %q", updated.ProxyURL, "http://proxy.local") } if updated.Metadata == nil { t.Fatalf("expected metadata to be non-nil") } if got, _ := updated.Metadata["prefix"].(string); got != "p1" { t.Fatalf("metadata.prefix = %q, want %q", got, "p1") } if got, _ := updated.Metadata["proxy_url"].(string); got != "http://proxy.local" { t.Fatalf("metadata.proxy_url = %q, want %q", got, "http://proxy.local") } headersMeta, ok := updated.Metadata["headers"].(map[string]any) if !ok { raw, _ := json.Marshal(updated.Metadata["headers"]) t.Fatalf("metadata.headers = %T (%s), want map[string]any", updated.Metadata["headers"], string(raw)) } if got := headersMeta["X-Old"]; got != "new" { t.Fatalf("metadata.headers.X-Old = %#v, want %q", got, "new") } if got := headersMeta["X-New"]; got != "v" { t.Fatalf("metadata.headers.X-New = %#v, want %q", got, "v") } if _, ok := headersMeta["X-Remove"]; ok { t.Fatalf("expected metadata.headers.X-Remove to be deleted") } if _, ok := headersMeta["X-Nope"]; ok { t.Fatalf("expected metadata.headers.X-Nope to be absent") } if got := updated.Attributes["header:X-Old"]; got != "new" { t.Fatalf("attrs header:X-Old = %q, want %q", got, "new") } if got := updated.Attributes["header:X-New"]; got != "v" { t.Fatalf("attrs header:X-New = %q, want %q", got, "v") } if _, ok := updated.Attributes["header:X-Remove"]; ok { t.Fatalf("expected attrs header:X-Remove to be deleted") } if _, ok := updated.Attributes["header:X-Nope"]; ok { t.Fatalf("expected attrs header:X-Nope to be absent") } } func TestPatchAuthFileFields_HeadersEmptyMapIsNoop(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "") store := &memoryAuthStore{} manager := coreauth.NewManager(store, nil, nil) record := &coreauth.Auth{ ID: "noop.json", FileName: "noop.json", Provider: "claude", Attributes: map[string]string{ "path": "/tmp/noop.json", "header:X-Kee": "1", }, Metadata: map[string]any{ "type": "claude", "headers": map[string]any{ "X-Kee": "1", }, }, } if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { t.Fatalf("failed to register auth record: %v", errRegister) } h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) body := `{"name":"noop.json","note":"hello","headers":{}}` rec := httptest.NewRecorder() ctx, _ := gin.CreateTestContext(rec) req := httptest.NewRequest(http.MethodPatch, "/v0/management/auth-files/fields", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") ctx.Request = req h.PatchAuthFileFields(ctx) if rec.Code != http.StatusOK { t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String()) } updated, ok := manager.GetByID("noop.json") if !ok || updated == nil { t.Fatalf("expected auth record to exist after patch") } if got := updated.Attributes["header:X-Kee"]; got != "1" { t.Fatalf("attrs header:X-Kee = %q, want %q", got, "1") } headersMeta, ok := updated.Metadata["headers"].(map[string]any) if !ok { t.Fatalf("expected metadata.headers to remain a map, got %T", updated.Metadata["headers"]) } if got := headersMeta["X-Kee"]; got != "1" { t.Fatalf("metadata.headers.X-Kee = %#v, want %q", got, "1") } } func TestPatchAuthFileFields_WebsocketsFalseIsUpdate(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "") store := &memoryAuthStore{} manager := coreauth.NewManager(store, nil, nil) record := &coreauth.Auth{ ID: "codex.json", FileName: "codex.json", Provider: "codex", Attributes: map[string]string{ "path": "/tmp/codex.json", "websockets": "true", }, Metadata: map[string]any{ "type": "codex", "websockets": true, }, } if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { t.Fatalf("failed to register auth record: %v", errRegister) } h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) body := `{"name":"codex.json","websockets":false}` rec := httptest.NewRecorder() ctx, _ := gin.CreateTestContext(rec) req := httptest.NewRequest(http.MethodPatch, "/v0/management/auth-files/fields", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") ctx.Request = req h.PatchAuthFileFields(ctx) if rec.Code != http.StatusOK { t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String()) } updated, ok := manager.GetByID("codex.json") if !ok || updated == nil { t.Fatalf("expected auth record to exist after patch") } if got := updated.Attributes["websockets"]; got != "false" { t.Fatalf("attrs websockets = %q, want %q", got, "false") } if got, ok := updated.Metadata["websockets"].(bool); !ok || got { t.Fatalf("metadata.websockets = %#v, want false", updated.Metadata["websockets"]) } } func TestPatchAuthFileFields_ArbitraryFieldsPersistToFile(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "") authDir := t.TempDir() fileName := "generic.json" filePath := filepath.Join(authDir, fileName) store := fileauth.NewFileTokenStore() store.SetBaseDir(authDir) manager := coreauth.NewManager(store, nil, nil) record := &coreauth.Auth{ ID: fileName, FileName: fileName, Provider: "codex", Attributes: map[string]string{ "path": filePath, }, Metadata: map[string]any{ "type": "codex", }, } if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { t.Fatalf("failed to register auth record: %v", errRegister) } h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager) body := `{"name":"generic.json","abc":true,"nested.cde":true,"fgh":{"ijk":true}}` rec := httptest.NewRecorder() ctx, _ := gin.CreateTestContext(rec) req := httptest.NewRequest(http.MethodPatch, "/v0/management/auth-files/fields", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") ctx.Request = req h.PatchAuthFileFields(ctx) if rec.Code != http.StatusOK { t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String()) } raw, errRead := os.ReadFile(filePath) if errRead != nil { t.Fatalf("failed to read updated auth file: %v", errRead) } var data map[string]any if errUnmarshal := json.Unmarshal(raw, &data); errUnmarshal != nil { t.Fatalf("failed to unmarshal updated auth file: %v", errUnmarshal) } if got := data["abc"]; got != true { t.Fatalf("abc = %#v, want true", got) } nested, ok := data["nested"].(map[string]any) if !ok { t.Fatalf("nested = %#v, want object", data["nested"]) } if got := nested["cde"]; got != true { t.Fatalf("nested.cde = %#v, want true", got) } fgh, ok := data["fgh"].(map[string]any) if !ok { t.Fatalf("fgh = %#v, want object", data["fgh"]) } if got := fgh["ijk"]; got != true { t.Fatalf("fgh.ijk = %#v, want true", got) } }