|
1 | | -import { describe, expect, it } from "@effect/vitest"; |
| 1 | +import { afterEach, describe, expect, it } from "@effect/vitest"; |
2 | 2 |
|
3 | | -import { browserOpenCommand } from "./device-login"; |
| 3 | +import { |
| 4 | + browserOpenCommand, |
| 5 | + discoverCliLogin, |
| 6 | + refreshDeviceTokens, |
| 7 | + requestDeviceCode, |
| 8 | + type CliLoginDiscovery, |
| 9 | +} from "./device-login"; |
| 10 | + |
| 11 | +const originalFetch = globalThis.fetch; |
| 12 | + |
| 13 | +interface FetchCall { |
| 14 | + readonly url: string; |
| 15 | + readonly headers: Record<string, string>; |
| 16 | +} |
| 17 | + |
| 18 | +const responseJson = (body: Record<string, unknown>, status = 200): Response => |
| 19 | + new Response(JSON.stringify(body), { |
| 20 | + status, |
| 21 | + headers: { "content-type": "application/json" }, |
| 22 | + }); |
| 23 | + |
| 24 | +const installFetch = (handler: (url: string, init: RequestInit | undefined) => Response) => { |
| 25 | + globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit) => { |
| 26 | + const url = |
| 27 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 28 | + return Promise.resolve(handler(url, init)); |
| 29 | + }) as typeof fetch; |
| 30 | +}; |
| 31 | + |
| 32 | +const recordCall = (calls: Array<FetchCall>, url: string, init: RequestInit | undefined): void => { |
| 33 | + calls.push({ |
| 34 | + url, |
| 35 | + headers: Object.fromEntries(new Headers(init?.headers).entries()), |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +afterEach(() => { |
| 40 | + globalThis.fetch = originalFetch; |
| 41 | +}); |
4 | 42 |
|
5 | 43 | describe("browserOpenCommand", () => { |
6 | 44 | it("opens Windows browser URLs without cmd.exe", () => { |
@@ -32,3 +70,81 @@ describe("browserOpenCommand", () => { |
32 | 70 | expect(browserOpenCommand("not a url", "win32")).toBeUndefined(); |
33 | 71 | }); |
34 | 72 | }); |
| 73 | + |
| 74 | +describe("device login headers", () => { |
| 75 | + it("sends configured headers when discovering CLI login", async () => { |
| 76 | + const calls: Array<FetchCall> = []; |
| 77 | + installFetch((url, init) => { |
| 78 | + recordCall(calls, url, init); |
| 79 | + return responseJson({ |
| 80 | + provider: "better-auth", |
| 81 | + deviceAuthorizationEndpoint: "https://executor.example/api/auth/device/code", |
| 82 | + tokenEndpoint: "https://executor.example/api/auth/device/token", |
| 83 | + clientId: "executor-cli", |
| 84 | + requestFormat: "json", |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + const discovery = await discoverCliLogin("https://executor.example", { |
| 89 | + headers: { "CF-Access-Client-Id": "client-id" }, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(discovery.clientId).toBe("executor-cli"); |
| 93 | + expect(calls).toHaveLength(1); |
| 94 | + expect(calls[0]?.url).toBe("https://executor.example/api/auth/cli-login"); |
| 95 | + expect(calls[0]?.headers).toMatchObject({ |
| 96 | + accept: "application/json", |
| 97 | + "cf-access-client-id": "client-id", |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("sends configured headers only to same-origin device endpoints", async () => { |
| 102 | + const calls: Array<FetchCall> = []; |
| 103 | + installFetch((url, init) => { |
| 104 | + recordCall(calls, url, init); |
| 105 | + if (url.endsWith("/api/auth/device/code")) { |
| 106 | + return responseJson({ |
| 107 | + device_code: "device-code", |
| 108 | + user_code: "USER-CODE", |
| 109 | + verification_uri: "https://executor.example/device", |
| 110 | + expires_in: 300, |
| 111 | + interval: 5, |
| 112 | + }); |
| 113 | + } |
| 114 | + return responseJson({ |
| 115 | + access_token: "access-token", |
| 116 | + refresh_token: "refresh-token-2", |
| 117 | + expires_in: 600, |
| 118 | + }); |
| 119 | + }); |
| 120 | + const discovery: CliLoginDiscovery = { |
| 121 | + provider: "better-auth", |
| 122 | + deviceAuthorizationEndpoint: "https://executor.example/api/auth/device/code", |
| 123 | + tokenEndpoint: "https://accounts.example/oauth/token", |
| 124 | + clientId: "executor-cli", |
| 125 | + requestFormat: "form", |
| 126 | + }; |
| 127 | + const headers = { "CF-Access-Client-Id": "client-id" }; |
| 128 | + |
| 129 | + await requestDeviceCode(discovery, { serverOrigin: "https://executor.example", headers }); |
| 130 | + await refreshDeviceTokens({ |
| 131 | + tokenEndpoint: discovery.tokenEndpoint, |
| 132 | + clientId: discovery.clientId, |
| 133 | + refreshToken: "refresh-token", |
| 134 | + serverOrigin: "https://executor.example", |
| 135 | + headers, |
| 136 | + }); |
| 137 | + |
| 138 | + expect(calls).toHaveLength(2); |
| 139 | + expect(calls[0]?.headers).toMatchObject({ |
| 140 | + accept: "application/json", |
| 141 | + "content-type": "application/x-www-form-urlencoded", |
| 142 | + "cf-access-client-id": "client-id", |
| 143 | + }); |
| 144 | + expect(calls[1]?.headers).toMatchObject({ |
| 145 | + accept: "application/json", |
| 146 | + "content-type": "application/x-www-form-urlencoded", |
| 147 | + }); |
| 148 | + expect(calls[1]?.headers["cf-access-client-id"]).toBeUndefined(); |
| 149 | + }); |
| 150 | +}); |
0 commit comments