|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/gin-gonic/gin" |
| 14 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| 15 | + fileauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth" |
| 16 | + coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" |
| 17 | +) |
| 18 | + |
| 19 | +func TestPatchAuthFileStatus_MergesDiskMetadataBeforePersist(t *testing.T) { |
| 20 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 21 | + gin.SetMode(gin.TestMode) |
| 22 | + |
| 23 | + authDir := t.TempDir() |
| 24 | + fileName := "codex.json" |
| 25 | + filePath := filepath.Join(authDir, fileName) |
| 26 | + original := `{"type":"codex","access_token":"access","refresh_token":"refresh","email":"u@example.com","nested":{"keep":true}}` |
| 27 | + if errWrite := os.WriteFile(filePath, []byte(original), 0o600); errWrite != nil { |
| 28 | + t.Fatalf("failed to write auth file: %v", errWrite) |
| 29 | + } |
| 30 | + |
| 31 | + store := fileauth.NewFileTokenStore() |
| 32 | + store.SetBaseDir(authDir) |
| 33 | + manager := coreauth.NewManager(store, nil, nil) |
| 34 | + record := &coreauth.Auth{ |
| 35 | + ID: fileName, |
| 36 | + FileName: fileName, |
| 37 | + Provider: "codex", |
| 38 | + Attributes: map[string]string{ |
| 39 | + "path": filePath, |
| 40 | + }, |
| 41 | + Metadata: map[string]any{ |
| 42 | + "name": fileName, |
| 43 | + }, |
| 44 | + } |
| 45 | + if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { |
| 46 | + t.Fatalf("failed to register auth record: %v", errRegister) |
| 47 | + } |
| 48 | + if errWrite := os.WriteFile(filePath, []byte(original), 0o600); errWrite != nil { |
| 49 | + t.Fatalf("failed to restore full auth file after registration: %v", errWrite) |
| 50 | + } |
| 51 | + |
| 52 | + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager) |
| 53 | + rec := httptest.NewRecorder() |
| 54 | + ctx, _ := gin.CreateTestContext(rec) |
| 55 | + req := httptest.NewRequest(http.MethodPost, "/v0/management/auth-files?name="+fileName, strings.NewReader(`{"name":"codex.json","disabled":true}`)) |
| 56 | + req.Header.Set("Content-Type", "application/json") |
| 57 | + ctx.Request = req |
| 58 | + |
| 59 | + h.PatchAuthFileStatus(ctx) |
| 60 | + |
| 61 | + if rec.Code != http.StatusOK { |
| 62 | + t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String()) |
| 63 | + } |
| 64 | + raw, errRead := os.ReadFile(filePath) |
| 65 | + if errRead != nil { |
| 66 | + t.Fatalf("failed to read auth file: %v", errRead) |
| 67 | + } |
| 68 | + var data map[string]any |
| 69 | + if errUnmarshal := json.Unmarshal(raw, &data); errUnmarshal != nil { |
| 70 | + t.Fatalf("failed to unmarshal auth file: %v; raw=%s", errUnmarshal, string(raw)) |
| 71 | + } |
| 72 | + if got := data["type"]; got != "codex" { |
| 73 | + t.Fatalf("type = %#v, want codex; raw=%s", got, string(raw)) |
| 74 | + } |
| 75 | + if got := data["access_token"]; got != "access" { |
| 76 | + t.Fatalf("access_token = %#v, want preserved access; raw=%s", got, string(raw)) |
| 77 | + } |
| 78 | + if got := data["refresh_token"]; got != "refresh" { |
| 79 | + t.Fatalf("refresh_token = %#v, want preserved refresh; raw=%s", got, string(raw)) |
| 80 | + } |
| 81 | + if got := data["disabled"]; got != true { |
| 82 | + t.Fatalf("disabled = %#v, want true; raw=%s", got, string(raw)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestPatchAuthFileStatus_AcceptsMetadataAndTypeFromRequest(t *testing.T) { |
| 87 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 88 | + gin.SetMode(gin.TestMode) |
| 89 | + |
| 90 | + authDir := t.TempDir() |
| 91 | + fileName := "partial.json" |
| 92 | + filePath := filepath.Join(authDir, fileName) |
| 93 | + if errWrite := os.WriteFile(filePath, []byte(`{"name":"partial.json"}`), 0o600); errWrite != nil { |
| 94 | + t.Fatalf("failed to write auth file: %v", errWrite) |
| 95 | + } |
| 96 | + |
| 97 | + store := fileauth.NewFileTokenStore() |
| 98 | + store.SetBaseDir(authDir) |
| 99 | + manager := coreauth.NewManager(store, nil, nil) |
| 100 | + record := &coreauth.Auth{ |
| 101 | + ID: fileName, |
| 102 | + FileName: fileName, |
| 103 | + Provider: "unknown", |
| 104 | + Attributes: map[string]string{ |
| 105 | + "path": filePath, |
| 106 | + }, |
| 107 | + Metadata: map[string]any{ |
| 108 | + "name": fileName, |
| 109 | + }, |
| 110 | + } |
| 111 | + if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { |
| 112 | + t.Fatalf("failed to register auth record: %v", errRegister) |
| 113 | + } |
| 114 | + |
| 115 | + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager) |
| 116 | + body := `{"name":"partial.json","disabled":false,"type":"codex","metadata":{"access_token":"access","refresh_token":"refresh","email":"u@example.com"}}` |
| 117 | + rec := httptest.NewRecorder() |
| 118 | + ctx, _ := gin.CreateTestContext(rec) |
| 119 | + req := httptest.NewRequest(http.MethodPost, "/v0/management/auth-files?name="+fileName, strings.NewReader(body)) |
| 120 | + req.Header.Set("Content-Type", "application/json") |
| 121 | + ctx.Request = req |
| 122 | + |
| 123 | + h.PatchAuthFileStatus(ctx) |
| 124 | + |
| 125 | + if rec.Code != http.StatusOK { |
| 126 | + t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String()) |
| 127 | + } |
| 128 | + raw, errRead := os.ReadFile(filePath) |
| 129 | + if errRead != nil { |
| 130 | + t.Fatalf("failed to read auth file: %v", errRead) |
| 131 | + } |
| 132 | + var data map[string]any |
| 133 | + if errUnmarshal := json.Unmarshal(raw, &data); errUnmarshal != nil { |
| 134 | + t.Fatalf("failed to unmarshal auth file: %v; raw=%s", errUnmarshal, string(raw)) |
| 135 | + } |
| 136 | + if got := data["type"]; got != "codex" { |
| 137 | + t.Fatalf("type = %#v, want codex; raw=%s", got, string(raw)) |
| 138 | + } |
| 139 | + if got := data["access_token"]; got != "access" { |
| 140 | + t.Fatalf("access_token = %#v, want request metadata access; raw=%s", got, string(raw)) |
| 141 | + } |
| 142 | + if got := data["disabled"]; got != false { |
| 143 | + t.Fatalf("disabled = %#v, want false; raw=%s", got, string(raw)) |
| 144 | + } |
| 145 | + |
| 146 | + updated, ok := manager.GetByID(fileName) |
| 147 | + if !ok || updated == nil { |
| 148 | + t.Fatalf("expected updated auth") |
| 149 | + } |
| 150 | + if got := updated.Provider; got != "codex" { |
| 151 | + t.Fatalf("updated.Provider = %q, want codex", got) |
| 152 | + } |
| 153 | +} |
0 commit comments