|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { startOAuthCallbackServer } from "../lib/oauth-server.js"; |
| 3 | + |
| 4 | +describe("oauth callback server", () => { |
| 5 | + it("returns success page and resolves authorization code", async () => { |
| 6 | + const callbackServer = await startOAuthCallbackServer({ |
| 7 | + state: "expected-state", |
| 8 | + timeoutMs: 10_000, |
| 9 | + port: 0, |
| 10 | + }); |
| 11 | + |
| 12 | + const response = await fetch( |
| 13 | + `${callbackServer.redirectUri}?code=test-code&state=expected-state`, |
| 14 | + ); |
| 15 | + const html = await response.text(); |
| 16 | + |
| 17 | + expect(response.status).toBe(200); |
| 18 | + expect(html).toContain("Login complete"); |
| 19 | + await expect(callbackServer.waitForCode).resolves.toBe("test-code"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("returns error page and rejects on state mismatch", async () => { |
| 23 | + const callbackServer = await startOAuthCallbackServer({ |
| 24 | + state: "expected-state", |
| 25 | + timeoutMs: 10_000, |
| 26 | + port: 0, |
| 27 | + }); |
| 28 | + const rejection = callbackServer.waitForCode.then( |
| 29 | + () => new Error("Expected OAuth state mismatch."), |
| 30 | + (error) => error as Error, |
| 31 | + ); |
| 32 | + |
| 33 | + const response = await fetch( |
| 34 | + `${callbackServer.redirectUri}?code=test-code&state=wrong-state`, |
| 35 | + ); |
| 36 | + const html = await response.text(); |
| 37 | + |
| 38 | + expect(response.status).toBe(400); |
| 39 | + expect(html).toContain("Authentication failed"); |
| 40 | + const error = await rejection; |
| 41 | + expect(error.message).toBe("OAuth state mismatch."); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns error page and rejects when OAuth provider sends an error", async () => { |
| 45 | + const callbackServer = await startOAuthCallbackServer({ |
| 46 | + state: "expected-state", |
| 47 | + timeoutMs: 10_000, |
| 48 | + port: 0, |
| 49 | + }); |
| 50 | + const rejection = callbackServer.waitForCode.then( |
| 51 | + () => new Error("Expected OAuth provider error."), |
| 52 | + (error) => error as Error, |
| 53 | + ); |
| 54 | + |
| 55 | + const response = await fetch( |
| 56 | + `${callbackServer.redirectUri}?error=access_denied&error_description=User%20denied`, |
| 57 | + ); |
| 58 | + const html = await response.text(); |
| 59 | + |
| 60 | + expect(response.status).toBe(400); |
| 61 | + expect(html).toContain("Authentication failed"); |
| 62 | + const error = await rejection; |
| 63 | + expect(error.message).toBe("OAuth authorization denied: User denied"); |
| 64 | + }); |
| 65 | +}); |
0 commit comments