Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

connectAuth.test.ts

78 lines3.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 buildConnectAuthorizeRequestUrl,
5 buildConnectClerkAuthorizeUrl,
6 connectCallbackUrl,
7 encodeConnectAuthCode,
8 parseConnectAuthCode,
9 readConnectAuthorizeRequest,
10} from "./connectAuth.ts";
11​
12describe("connectAuth", () => {
13 it("round-trips state and challenge through the authorize URL fragment", () => {
14 const url = buildConnectAuthorizeRequestUrl({
15 hostedAppUrl: "https://app.t3.codes",
16 state: "q7mK9xV2pL4nR8sT6wYzAQ",
17 challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
18 });
19 const parsed = new URL(url);
20​
21 expect(parsed.origin).toBe("https://app.t3.codes");
22 expect(parsed.pathname).toBe("/connect");
23 expect(parsed.search).toBe("");
24 expect(readConnectAuthorizeRequest(parsed)).toEqual({
25 state: "q7mK9xV2pL4nR8sT6wYzAQ",
26 challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
27 });
28 });
29​
30 it("rejects authorize requests missing state or challenge", () => {
31 expect(readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect"))).toBeNull();
32 expect(
33 readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect#state=abc")),
34 ).toBeNull();
35 expect(
36 readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect#challenge=abc")),
37 ).toBeNull();
38 });
39​
40 it("builds a PKCE authorize URL against the Clerk endpoint", () => {
41 const url = new URL(
42 buildConnectClerkAuthorizeUrl({
43 authorizationEndpoint: "https://clerk.t3.codes/oauth/authorize",
44 clientId: "oauthapp_123",
45 redirectUri: connectCallbackUrl("https://app.t3.codes"),
46 scopes: ["openid", "profile", "email"],
47 state: "state-1",
48 challenge: "challenge-1",
49 }),
50 );
51​
52 expect(url.origin).toBe("https://clerk.t3.codes");
53 expect(url.pathname).toBe("/oauth/authorize");
54 expect(url.searchParams.get("client_id")).toBe("oauthapp_123");
55 expect(url.searchParams.get("redirect_uri")).toBe("https://app.t3.codes/connect/callback");
56 expect(url.searchParams.get("response_type")).toBe("code");
57 expect(url.searchParams.get("scope")).toBe("openid profile email");
58 expect(url.searchParams.get("state")).toBe("state-1");
59 expect(url.searchParams.get("code_challenge")).toBe("challenge-1");
60 expect(url.searchParams.get("code_challenge_method")).toBe("S256");
61 });
62​
63 it("round-trips the out-of-band authorization code and preserves dots inside it", () => {
64 const blob = encodeConnectAuthCode({ code: "az9.code.chunk", state: "state-uuid" });
65 expect(parseConnectAuthCode(blob)).toEqual({ code: "az9.code.chunk", state: "state-uuid" });
66 expect(parseConnectAuthCode(` ${blob}\n`)).toEqual({
67 code: "az9.code.chunk",
68 state: "state-uuid",
69 });
70 });
71​
72 it("rejects malformed out-of-band authorization codes", () => {
73 expect(parseConnectAuthCode("")).toBeNull();
74 expect(parseConnectAuthCode("no-separator")).toBeNull();
75 expect(parseConnectAuthCode(".leading")).toBeNull();
76 expect(parseConnectAuthCode("trailing.")).toBeNull();
77 });
78});