|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/Wei-Shaw/sub2api/internal/service" |
| 11 | +) |
| 12 | + |
| 13 | +func newCodexReauthTestAccount(accessToken string, extraCreds map[string]any) service.Account { |
| 14 | + creds := map[string]any{ |
| 15 | + "chatgpt_account_id": "workspace-1", |
| 16 | + "chatgpt_user_id": "user-1", |
| 17 | + "access_token": accessToken, |
| 18 | + "model_mapping": map[string]any{"gpt-5.5": "gpt-5.5"}, |
| 19 | + } |
| 20 | + for k, v := range extraCreds { |
| 21 | + creds[k] = v |
| 22 | + } |
| 23 | + return service.Account{ |
| 24 | + ID: 10, |
| 25 | + Name: "existing", |
| 26 | + Platform: service.PlatformOpenAI, |
| 27 | + Type: service.AccountTypeOAuth, |
| 28 | + Status: service.StatusError, |
| 29 | + Credentials: creds, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func buildCodexAuthJSON(t *testing.T, accessToken, refreshToken string) string { |
| 34 | + t.Helper() |
| 35 | + raw, err := json.Marshal(map[string]any{ |
| 36 | + "auth_mode": "chatgpt", |
| 37 | + "OPENAI_API_KEY": nil, |
| 38 | + "tokens": map[string]any{ |
| 39 | + "access_token": accessToken, |
| 40 | + "refresh_token": refreshToken, |
| 41 | + "account_id": "workspace-1", |
| 42 | + }, |
| 43 | + "last_refresh": time.Now().UTC().Format(time.RFC3339Nano), |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("marshal auth.json: %v", err) |
| 47 | + } |
| 48 | + return string(raw) |
| 49 | +} |
| 50 | + |
| 51 | +func TestReauthCodexSessionReplacesCredentialsOfTargetAccountOnly(t *testing.T) { |
| 52 | + oldToken := buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(time.Hour)) |
| 53 | + existing := newCodexReauthTestAccount(oldToken, map[string]any{"refresh_token": "rt-old", "client_id": "old-client"}) |
| 54 | + svc := newCodexImportMemoryAdminService([]service.Account{existing}) |
| 55 | + handler := NewAccountHandler(svc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) |
| 56 | + |
| 57 | + newToken := buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(10*24*time.Hour)) |
| 58 | + result, err := handler.reauthCodexSession(context.Background(), &existing, buildCodexAuthJSON(t, newToken, "rt-new")) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("reauthCodexSession error = %v", err) |
| 61 | + } |
| 62 | + if result == nil { |
| 63 | + t.Fatal("result is nil") |
| 64 | + } |
| 65 | + if len(svc.createdAccounts) != 0 { |
| 66 | + t.Fatalf("created accounts = %d, want 0", len(svc.createdAccounts)) |
| 67 | + } |
| 68 | + if len(svc.updatedAccounts) != 1 || svc.updatedAccounts[0].id != 10 { |
| 69 | + t.Fatalf("updated accounts = %+v, want only account 10", svc.updatedAccounts) |
| 70 | + } |
| 71 | + input := svc.updatedAccounts[0].input |
| 72 | + if got := input.Credentials["access_token"]; got != newToken { |
| 73 | + t.Fatalf("access_token not replaced") |
| 74 | + } |
| 75 | + if got := input.Credentials["refresh_token"]; got != "rt-new" { |
| 76 | + t.Fatalf("refresh_token = %v, want rt-new", got) |
| 77 | + } |
| 78 | + if _, ok := input.Credentials["model_mapping"]; !ok { |
| 79 | + t.Fatal("model_mapping should be preserved") |
| 80 | + } |
| 81 | + if _, ok := input.Credentials["expires_at"]; !ok { |
| 82 | + t.Fatal("expires_at should be derived from the access token") |
| 83 | + } |
| 84 | + if input.Concurrency != nil || input.Priority != nil || input.GroupIDs != nil || input.ProxyID != nil { |
| 85 | + t.Fatalf("scheduling fields must not be touched: %+v", input) |
| 86 | + } |
| 87 | + if input.ExpiresAt != nil || input.AutoPauseOnExpired != nil { |
| 88 | + t.Fatalf("account expiry must not be set when refresh_token is present: %+v", input) |
| 89 | + } |
| 90 | + if svc.updateAccountExtraCalls != 1 { |
| 91 | + t.Fatalf("UpdateAccountExtra calls = %d, want 1 (key-level merge)", svc.updateAccountExtraCalls) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestReauthCodexSessionRejectsDifferentUser(t *testing.T) { |
| 96 | + oldToken := buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(time.Hour)) |
| 97 | + existing := newCodexReauthTestAccount(oldToken, nil) |
| 98 | + svc := newCodexImportMemoryAdminService([]service.Account{existing}) |
| 99 | + handler := NewAccountHandler(svc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) |
| 100 | + |
| 101 | + otherToken := buildCodexAccessToken(t, "workspace-1", "user-2", time.Now().Add(time.Hour)) |
| 102 | + _, err := handler.reauthCodexSession(context.Background(), &existing, buildCodexAuthJSON(t, otherToken, "rt-other")) |
| 103 | + if err == nil || !strings.Contains(err.Error(), "chatgpt_user_id") { |
| 104 | + t.Fatalf("err = %v, want chatgpt_user_id mismatch", err) |
| 105 | + } |
| 106 | + if len(svc.updatedAccounts) != 0 { |
| 107 | + t.Fatalf("updated accounts = %d, want 0", len(svc.updatedAccounts)) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestReauthCodexSessionRejectsMultipleEntries(t *testing.T) { |
| 112 | + oldToken := buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(time.Hour)) |
| 113 | + existing := newCodexReauthTestAccount(oldToken, nil) |
| 114 | + svc := newCodexImportMemoryAdminService([]service.Account{existing}) |
| 115 | + handler := NewAccountHandler(svc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) |
| 116 | + |
| 117 | + a := buildCodexAuthJSON(t, buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(time.Hour)), "rt-a") |
| 118 | + b := buildCodexAuthJSON(t, buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(2*time.Hour)), "rt-b") |
| 119 | + _, err := handler.reauthCodexSession(context.Background(), &existing, "["+a+","+b+"]") |
| 120 | + if err == nil { |
| 121 | + t.Fatal("expected error for multiple entries") |
| 122 | + } |
| 123 | + if len(svc.updatedAccounts) != 0 { |
| 124 | + t.Fatalf("updated accounts = %d, want 0", len(svc.updatedAccounts)) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestReauthCodexSessionAccessTokenOnlyKeepsExistingRefreshToken(t *testing.T) { |
| 129 | + oldToken := buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(time.Hour)) |
| 130 | + existing := newCodexReauthTestAccount(oldToken, map[string]any{"refresh_token": "rt-old", "client_id": "old-client"}) |
| 131 | + svc := newCodexImportMemoryAdminService([]service.Account{existing}) |
| 132 | + handler := NewAccountHandler(svc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) |
| 133 | + |
| 134 | + newToken := buildCodexAccessToken(t, "workspace-1", "user-1", time.Now().Add(2*time.Hour)) |
| 135 | + result, err := handler.reauthCodexSession(context.Background(), &existing, newToken) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("reauthCodexSession error = %v", err) |
| 138 | + } |
| 139 | + input := svc.updatedAccounts[0].input |
| 140 | + if got := input.Credentials["refresh_token"]; got != "rt-old" { |
| 141 | + t.Fatalf("refresh_token = %v, want rt-old", got) |
| 142 | + } |
| 143 | + if got := input.Credentials["client_id"]; got != "old-client" { |
| 144 | + t.Fatalf("client_id = %v, want old-client", got) |
| 145 | + } |
| 146 | + if input.ExpiresAt != nil || input.AutoPauseOnExpired != nil { |
| 147 | + t.Fatalf("account expiry must stay untouched when refresh_token is preserved: %+v", input) |
| 148 | + } |
| 149 | + if len(result.Warnings) == 0 { |
| 150 | + t.Fatal("expected a warning about the preserved refresh_token") |
| 151 | + } |
| 152 | +} |
0 commit comments