|
1 | | -import { describe, test, expect } from "bun:test"; |
| 1 | +import { describe, test, expect, afterEach, afterAll, mock } from "bun:test"; |
2 | 2 | import { mkdtemp, rm } from "node:fs/promises"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
5 | 5 |
|
6 | | -import { buildProviderSubmitHandler } from "./provider-setup-submit.js"; |
7 | | -import { loadLocalSettings, loadSettings, localSettingsPath } from "../config/settings.js"; |
| 6 | +import type { OAuthScopeCheckResult } from "../auth/oauth-scope-check.js"; |
| 7 | + |
| 8 | +// The oauth branch probes real provider scope over the network; stub the |
| 9 | +// check so these tests exercise buildProviderSubmitHandler's own branching |
| 10 | +// (ok / insufficient-scope / unavailable) without a live call. Restored after |
| 11 | +// this file's tests run so a mock never leaks into another suite that |
| 12 | +// imports provider-setup-submit.js and expects the real probe. |
| 13 | +const realOAuthScopeCheck = { ...(await import("../auth/oauth-scope-check.js")) }; |
| 14 | +let scopeCheckResult: OAuthScopeCheckResult = { status: "ok" }; |
| 15 | +mock.module("../auth/oauth-scope-check.js", () => ({ |
| 16 | + ...realOAuthScopeCheck, |
| 17 | + checkOAuthProviderScope: async () => scopeCheckResult, |
| 18 | +})); |
| 19 | + |
| 20 | +afterAll(() => { |
| 21 | + mock.module("../auth/oauth-scope-check.js", () => realOAuthScopeCheck); |
| 22 | +}); |
| 23 | + |
| 24 | +const { buildProviderSubmitHandler } = await import("./provider-setup-submit.js"); |
| 25 | +const { loadLocalSettings, loadSettings, localSettingsPath } = |
| 26 | + await import("../config/settings.js"); |
8 | 27 | import type { ProviderFormValues, SubmitPhase } from "./provider-setup.js"; |
9 | 28 |
|
10 | 29 | const noopSetPhase = (_phase: SubmitPhase): void => {}; |
@@ -192,4 +211,127 @@ describe("buildProviderSubmitHandler", () => { |
192 | 211 | expect(resolvedModel).toBe("claude-sonnet-4"); |
193 | 212 | }); |
194 | 213 | }); |
| 214 | + |
| 215 | + describe("OAuth-issued token scope validation (CL-5710)", () => { |
| 216 | + afterEach(() => { |
| 217 | + scopeCheckResult = { status: "ok" }; |
| 218 | + }); |
| 219 | + |
| 220 | + test("valid scope: onboarding completes", async () => { |
| 221 | + await withTempDir(async (dir) => { |
| 222 | + scopeCheckResult = { status: "ok" }; |
| 223 | + const path = join(dir, "settings.json"); |
| 224 | + const localPath = localSettingsPath(dir); |
| 225 | + const submit = buildProviderSubmitHandler(path, null, localPath); |
| 226 | + |
| 227 | + await submit( |
| 228 | + { |
| 229 | + name: "", |
| 230 | + baseURL: "https://chatgpt.com/backend-api", |
| 231 | + apiKey: "", |
| 232 | + model: "gpt-5", |
| 233 | + oauthProfile: "work", |
| 234 | + }, |
| 235 | + noopSetPhase, |
| 236 | + { |
| 237 | + skipValidation: false, |
| 238 | + oauth: { kind: "codex", providerName: "codex/work", profile: "work" }, |
| 239 | + }, |
| 240 | + ); |
| 241 | + |
| 242 | + const local = await loadLocalSettings(localPath); |
| 243 | + expect(local).toEqual({ provider: "codex/work", model: "gpt-5" }); |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
| 247 | + test("definitively insufficient scope: onboarding is rejected with a setup-attributable message, not a raw adapter error", async () => { |
| 248 | + await withTempDir(async (dir) => { |
| 249 | + scopeCheckResult = { |
| 250 | + status: "insufficient-scope", |
| 251 | + message: "Your Codex sign-in doesn't carry API access. Reconnect Codex and try again.", |
| 252 | + }; |
| 253 | + const path = join(dir, "settings.json"); |
| 254 | + const localPath = localSettingsPath(dir); |
| 255 | + const submit = buildProviderSubmitHandler(path, null, localPath); |
| 256 | + |
| 257 | + await expect( |
| 258 | + submit( |
| 259 | + { |
| 260 | + name: "", |
| 261 | + baseURL: "https://chatgpt.com/backend-api", |
| 262 | + apiKey: "", |
| 263 | + model: "gpt-5", |
| 264 | + oauthProfile: "work", |
| 265 | + }, |
| 266 | + noopSetPhase, |
| 267 | + { |
| 268 | + skipValidation: false, |
| 269 | + oauth: { kind: "codex", providerName: "codex/work", profile: "work" }, |
| 270 | + }, |
| 271 | + ), |
| 272 | + ).rejects.toThrow(/reconnect codex/i); |
| 273 | + |
| 274 | + // Nothing is persisted on a proven scope failure. |
| 275 | + expect(await loadSettings(path)).toBeNull(); |
| 276 | + expect(await loadLocalSettings(localPath)).toBeNull(); |
| 277 | + }); |
| 278 | + }); |
| 279 | + |
| 280 | + test("check-unavailable (network blip): onboarding still completes, not blocked", async () => { |
| 281 | + await withTempDir(async (dir) => { |
| 282 | + scopeCheckResult = { |
| 283 | + status: "unavailable", |
| 284 | + message: "Couldn't confirm Codex API access right now.", |
| 285 | + }; |
| 286 | + const path = join(dir, "settings.json"); |
| 287 | + const localPath = localSettingsPath(dir); |
| 288 | + const submit = buildProviderSubmitHandler(path, null, localPath); |
| 289 | + |
| 290 | + await submit( |
| 291 | + { |
| 292 | + name: "", |
| 293 | + baseURL: "https://chatgpt.com/backend-api", |
| 294 | + apiKey: "", |
| 295 | + model: "gpt-5", |
| 296 | + oauthProfile: "work", |
| 297 | + }, |
| 298 | + noopSetPhase, |
| 299 | + { |
| 300 | + skipValidation: false, |
| 301 | + oauth: { kind: "codex", providerName: "codex/work", profile: "work" }, |
| 302 | + }, |
| 303 | + ); |
| 304 | + |
| 305 | + const local = await loadLocalSettings(localPath); |
| 306 | + expect(local).toEqual({ provider: "codex/work", model: "gpt-5" }); |
| 307 | + }); |
| 308 | + }); |
| 309 | + |
| 310 | + test("skipValidation bypasses the scope probe entirely", async () => { |
| 311 | + await withTempDir(async (dir) => { |
| 312 | + scopeCheckResult = { status: "insufficient-scope", message: "should never be thrown" }; |
| 313 | + const path = join(dir, "settings.json"); |
| 314 | + const localPath = localSettingsPath(dir); |
| 315 | + const submit = buildProviderSubmitHandler(path, null, localPath); |
| 316 | + |
| 317 | + await submit( |
| 318 | + { |
| 319 | + name: "", |
| 320 | + baseURL: "https://chatgpt.com/backend-api", |
| 321 | + apiKey: "", |
| 322 | + model: "gpt-5", |
| 323 | + oauthProfile: "work", |
| 324 | + }, |
| 325 | + noopSetPhase, |
| 326 | + { |
| 327 | + skipValidation: true, |
| 328 | + oauth: { kind: "codex", providerName: "codex/work", profile: "work" }, |
| 329 | + }, |
| 330 | + ); |
| 331 | + |
| 332 | + const local = await loadLocalSettings(localPath); |
| 333 | + expect(local).toEqual({ provider: "codex/work", model: "gpt-5" }); |
| 334 | + }); |
| 335 | + }); |
| 336 | + }); |
195 | 337 | }); |
0 commit comments