|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { getLiveSessionGitHubToken } from "../../src/auth/github-oauth"; |
| 4 | +import { createAprRepoForCustomerSession } from "../../src/orb/apr-repo-creation"; |
| 5 | +import { createTestEnv } from "../helpers/d1"; |
| 6 | + |
| 7 | +// Mock the session-token lookup so no real session/DB state is needed. The mocked value is an opaque, |
| 8 | +// obviously-fake placeholder — never a PEM/private-key-shaped fixture (a prior attempt at a sibling APR |
| 9 | +// module was auto-closed by the secret scanner for exactly that). |
| 10 | +vi.mock("../../src/auth/github-oauth", async (importOriginal) => ({ |
| 11 | + ...(await importOriginal<typeof import("../../src/auth/github-oauth")>()), |
| 12 | + getLiveSessionGitHubToken: vi.fn(), |
| 13 | +})); |
| 14 | +const mockedToken = vi.mocked(getLiveSessionGitHubToken); |
| 15 | + |
| 16 | +/** Capture the outbound request so we can assert the endpoint, method, auth, and body. */ |
| 17 | +function stubFetch(handler: (url: string, init: RequestInit) => Response): void { |
| 18 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => handler(String(input), init ?? {})); |
| 19 | +} |
| 20 | + |
| 21 | +describe("createAprRepoForCustomerSession (#7637)", () => { |
| 22 | + beforeEach(() => { |
| 23 | + mockedToken.mockReset(); |
| 24 | + mockedToken.mockResolvedValue("gho_customer_session_token"); |
| 25 | + }); |
| 26 | + afterEach(() => { |
| 27 | + vi.unstubAllGlobals(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("POSTs to /user/repos with the customer session's own token, defaulting to private", async () => { |
| 31 | + let seenUrl = ""; |
| 32 | + let seenInit: RequestInit = {}; |
| 33 | + stubFetch((url, init) => { |
| 34 | + seenUrl = url; |
| 35 | + seenInit = init; |
| 36 | + return new Response( |
| 37 | + JSON.stringify({ full_name: "joesmoe/widgets", html_url: "https://github.com/joesmoe/widgets", node_id: "R_abc123" }), |
| 38 | + { status: 201 }, |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + const env = createTestEnv(); |
| 43 | + const result = await createAprRepoForCustomerSession(env, "session-1", "widgets"); |
| 44 | + |
| 45 | + expect(mockedToken).toHaveBeenCalledWith(env, "session-1"); |
| 46 | + expect(seenUrl).toBe("https://api.github.com/user/repos"); |
| 47 | + expect(seenInit.method).toBe("POST"); |
| 48 | + expect((seenInit.headers as Record<string, string>).authorization).toBe("Bearer gho_customer_session_token"); |
| 49 | + expect(JSON.parse(String(seenInit.body))).toEqual({ name: "widgets", private: true }); |
| 50 | + expect(result).toEqual({ |
| 51 | + created: true, |
| 52 | + fullName: "joesmoe/widgets", |
| 53 | + htmlUrl: "https://github.com/joesmoe/widgets", |
| 54 | + nodeId: "R_abc123", |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("passes through an explicit private:false and an optional description", async () => { |
| 59 | + let seenInit: RequestInit = {}; |
| 60 | + stubFetch((_url, init) => { |
| 61 | + seenInit = init; |
| 62 | + return new Response( |
| 63 | + JSON.stringify({ full_name: "joesmoe/widgets", html_url: "https://github.com/joesmoe/widgets", node_id: "R_abc123" }), |
| 64 | + { status: 201 }, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + await createAprRepoForCustomerSession(createTestEnv(), "session-1", "widgets", { private: false, description: "A widget repo" }); |
| 69 | + |
| 70 | + expect(JSON.parse(String(seenInit.body))).toEqual({ name: "widgets", private: false, description: "A widget repo" }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("fails closed without calling GitHub when the customer session has no live token", async () => { |
| 74 | + mockedToken.mockResolvedValue(null); |
| 75 | + const calls: string[] = []; |
| 76 | + stubFetch((url) => { |
| 77 | + calls.push(url); |
| 78 | + return new Response("", { status: 200 }); |
| 79 | + }); |
| 80 | + |
| 81 | + const result = await createAprRepoForCustomerSession(createTestEnv(), "session-1", "widgets"); |
| 82 | + |
| 83 | + expect(result).toEqual({ created: false, status: null, error: "customer_session_token_unavailable" }); |
| 84 | + expect(calls).toEqual([]); |
| 85 | + }); |
| 86 | + |
| 87 | + it("returns a structured failure on a GitHub API error (e.g. a repo-name collision) without throwing", async () => { |
| 88 | + stubFetch(() => new Response("Repository creation failed.", { status: 422 })); |
| 89 | + |
| 90 | + const result = await createAprRepoForCustomerSession(createTestEnv(), "session-1", "widgets"); |
| 91 | + |
| 92 | + expect(result).toEqual({ created: false, status: 422, error: "Repository creation failed." }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("fails closed when GitHub returns 2xx but the payload is missing required fields", async () => { |
| 96 | + stubFetch(() => new Response(JSON.stringify({}), { status: 201 })); |
| 97 | + |
| 98 | + const result = await createAprRepoForCustomerSession(createTestEnv(), "session-1", "widgets"); |
| 99 | + |
| 100 | + expect(result).toEqual({ created: false, status: 201, error: "repo creation response missing required fields" }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments