|
| 1 | +// Github Copilot tests cover device-flow login behavior. |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { |
| 4 | + runGitHubCopilotDeviceFlow, |
| 5 | + setGitHubCopilotDeviceFlowFetchGuardForTesting, |
| 6 | +} from "./login.js"; |
| 7 | + |
| 8 | +const DEVICE_CODE_URL = "https://github.com/login/device/code"; |
| 9 | +const ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"; |
| 10 | + |
| 11 | +// A valid device code payload GitHub returns on the first step. |
| 12 | +const VALID_DEVICE_CODE_BODY = { |
| 13 | + device_code: "dev-code-abc123", |
| 14 | + user_code: "ABCD-1234", |
| 15 | + verification_uri: "https://github.com/login/device", |
| 16 | + expires_in: 900, |
| 17 | + interval: 5, |
| 18 | +}; |
| 19 | + |
| 20 | +function guardResponse(body: unknown, status = 200, url = DEVICE_CODE_URL) { |
| 21 | + return { |
| 22 | + response: new Response(JSON.stringify(body), { |
| 23 | + status, |
| 24 | + headers: { "Content-Type": "application/json" }, |
| 25 | + }), |
| 26 | + finalUrl: url, |
| 27 | + release: vi.fn(async () => {}), |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +afterEach(() => { |
| 32 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(null); |
| 33 | + vi.restoreAllMocks(); |
| 34 | +}); |
| 35 | + |
| 36 | +describe("runGitHubCopilotDeviceFlow — normal flow", () => { |
| 37 | + it("returns authorized status and access token on successful flow", async () => { |
| 38 | + let callIdx = 0; |
| 39 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(async (params) => { |
| 40 | + callIdx += 1; |
| 41 | + if (callIdx === 1) { |
| 42 | + expect(params.url).toBe(DEVICE_CODE_URL); |
| 43 | + return guardResponse(VALID_DEVICE_CODE_BODY); |
| 44 | + } |
| 45 | + expect(params.url).toBe(ACCESS_TOKEN_URL); |
| 46 | + return guardResponse( |
| 47 | + { access_token: "ghu_tok_xyz", token_type: "bearer" }, |
| 48 | + 200, |
| 49 | + ACCESS_TOKEN_URL, |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + const showCode = vi.fn(async () => {}); |
| 54 | + const result = await runGitHubCopilotDeviceFlow({ showCode }); |
| 55 | + |
| 56 | + expect(result).toEqual({ status: "authorized", accessToken: "ghu_tok_xyz" }); |
| 57 | + expect(showCode).toHaveBeenCalledWith({ |
| 58 | + verificationUrl: "https://github.com/login/device", |
| 59 | + userCode: "ABCD-1234", |
| 60 | + expiresInMs: expect.any(Number), |
| 61 | + }); |
| 62 | + expect(callIdx).toBe(2); |
| 63 | + }); |
| 64 | + |
| 65 | + it("returns access_denied when GitHub rejects the authorization", async () => { |
| 66 | + let callIdx = 0; |
| 67 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(async () => { |
| 68 | + callIdx += 1; |
| 69 | + if (callIdx === 1) { |
| 70 | + return guardResponse(VALID_DEVICE_CODE_BODY); |
| 71 | + } |
| 72 | + return guardResponse({ error: "access_denied" }, 200, ACCESS_TOKEN_URL); |
| 73 | + }); |
| 74 | + |
| 75 | + const result = await runGitHubCopilotDeviceFlow({ |
| 76 | + showCode: vi.fn(async () => {}), |
| 77 | + }); |
| 78 | + expect(result).toEqual({ status: "access_denied" }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("returns expired when GitHub reports expired_token", async () => { |
| 82 | + let callIdx = 0; |
| 83 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(async () => { |
| 84 | + callIdx += 1; |
| 85 | + if (callIdx === 1) { |
| 86 | + return guardResponse(VALID_DEVICE_CODE_BODY); |
| 87 | + } |
| 88 | + return guardResponse({ error: "expired_token" }, 200, ACCESS_TOKEN_URL); |
| 89 | + }); |
| 90 | + |
| 91 | + const result = await runGitHubCopilotDeviceFlow({ |
| 92 | + showCode: vi.fn(async () => {}), |
| 93 | + }); |
| 94 | + expect(result).toEqual({ status: "expired" }); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe("runGitHubCopilotDeviceFlow — HTTP error propagation", () => { |
| 99 | + it("throws with failureLabel on non-OK device code response", async () => { |
| 100 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(async () => guardResponse({}, 401)); |
| 101 | + |
| 102 | + await expect(runGitHubCopilotDeviceFlow({ showCode: vi.fn() })).rejects.toThrow( |
| 103 | + "GitHub device code failed: HTTP 401", |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it("throws with failureLabel on non-OK access token response", async () => { |
| 108 | + let callIdx = 0; |
| 109 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(async () => { |
| 110 | + callIdx += 1; |
| 111 | + if (callIdx === 1) { |
| 112 | + return guardResponse(VALID_DEVICE_CODE_BODY); |
| 113 | + } |
| 114 | + return guardResponse({}, 500, ACCESS_TOKEN_URL); |
| 115 | + }); |
| 116 | + |
| 117 | + await expect(runGitHubCopilotDeviceFlow({ showCode: vi.fn(async () => {}) })).rejects.toThrow( |
| 118 | + "GitHub device token failed: HTTP 500", |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("postGitHubDeviceFlowForm — response size bound", () => { |
| 124 | + it("bounds oversized device code body and cancels the stream", async () => { |
| 125 | + const chunk = new Uint8Array(1024 * 1024); // 1 MiB |
| 126 | + let readCount = 0; |
| 127 | + let canceled = false; |
| 128 | + // 64 chunks × 1 MiB = 64 MiB — far exceeds the 16 MiB cap |
| 129 | + const oversizedBody = new ReadableStream<Uint8Array>({ |
| 130 | + pull(controller) { |
| 131 | + if (readCount >= 64) { |
| 132 | + controller.close(); |
| 133 | + return; |
| 134 | + } |
| 135 | + readCount += 1; |
| 136 | + controller.enqueue(chunk); |
| 137 | + }, |
| 138 | + cancel() { |
| 139 | + canceled = true; |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(async () => ({ |
| 144 | + response: new Response(oversizedBody, { |
| 145 | + status: 200, |
| 146 | + headers: { "Content-Type": "application/json" }, |
| 147 | + }), |
| 148 | + finalUrl: DEVICE_CODE_URL, |
| 149 | + release: async () => {}, |
| 150 | + })); |
| 151 | + |
| 152 | + await expect(runGitHubCopilotDeviceFlow({ showCode: vi.fn() })).rejects.toThrow( |
| 153 | + "github-copilot.device-flow", |
| 154 | + ); |
| 155 | + |
| 156 | + // Stream must be cancelled before all 64 MiB are consumed |
| 157 | + expect(readCount).toBeLessThan(64); |
| 158 | + expect(canceled).toBe(true); |
| 159 | + }); |
| 160 | + |
| 161 | + it("bounds oversized access token body and cancels the stream", async () => { |
| 162 | + const chunk = new Uint8Array(1024 * 1024); // 1 MiB |
| 163 | + let readCount = 0; |
| 164 | + let canceled = false; |
| 165 | + let callIdx = 0; |
| 166 | + |
| 167 | + setGitHubCopilotDeviceFlowFetchGuardForTesting(async () => { |
| 168 | + callIdx += 1; |
| 169 | + if (callIdx === 1) { |
| 170 | + return guardResponse(VALID_DEVICE_CODE_BODY); |
| 171 | + } |
| 172 | + |
| 173 | + const oversizedBody = new ReadableStream<Uint8Array>({ |
| 174 | + pull(controller) { |
| 175 | + if (readCount >= 64) { |
| 176 | + controller.close(); |
| 177 | + return; |
| 178 | + } |
| 179 | + readCount += 1; |
| 180 | + controller.enqueue(chunk); |
| 181 | + }, |
| 182 | + cancel() { |
| 183 | + canceled = true; |
| 184 | + }, |
| 185 | + }); |
| 186 | + |
| 187 | + return { |
| 188 | + response: new Response(oversizedBody, { |
| 189 | + status: 200, |
| 190 | + headers: { "Content-Type": "application/json" }, |
| 191 | + }), |
| 192 | + finalUrl: ACCESS_TOKEN_URL, |
| 193 | + release: async () => {}, |
| 194 | + }; |
| 195 | + }); |
| 196 | + |
| 197 | + await expect(runGitHubCopilotDeviceFlow({ showCode: vi.fn(async () => {}) })).rejects.toThrow( |
| 198 | + "github-copilot.device-flow", |
| 199 | + ); |
| 200 | + |
| 201 | + // Stream must be cancelled before all 64 MiB are consumed |
| 202 | + expect(readCount).toBeLessThan(64); |
| 203 | + expect(canceled).toBe(true); |
| 204 | + }); |
| 205 | +}); |
0 commit comments