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

connectCliAuth.test.ts

109 lines4.1 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 buildConnectCliClerkAuthorizeUrl,
5 connectCliSignInRedirectUrl,
6 hasConnectCliAuthConfig,
7 readConnectCliCallbackResult,
8} from "./connectCliAuth";
9​
10// Any pk_test_* key decodes to <base64 hostname>.clerk.accounts.dev.
11const TEST_PUBLISHABLE_KEY = `pk_test_${btoa("witty-mole-42.clerk.accounts.dev$")}`;
12​
13describe("connectCliAuth", () => {
14 afterEach(() => {
15 vi.unstubAllEnvs();
16 });
17​
18 it("requires both the publishable key and the CLI OAuth client id", () => {
19 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
20 vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay");
21 vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.com");
22 expect(hasConnectCliAuthConfig()).toBe(false);
23​
24 vi.stubEnv("VITE_CLERK_CLI_OAUTH_CLIENT_ID", "oauthapp_123");
25 expect(hasConnectCliAuthConfig()).toBe(true);
26 });
27​
28 it("builds the Clerk authorize URL with the configured hosted origin's callback", () => {
29 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
30 vi.stubEnv("VITE_CLERK_CLI_OAUTH_CLIENT_ID", "oauthapp_123");
31 vi.stubEnv("VITE_HOSTED_APP_URL", "https://nightly.app.t3.codes");
32​
33 const authorizeUrl = buildConnectCliClerkAuthorizeUrl({
34 state: "state-1",
35 challenge: "challenge-1",
36 });
37 expect(authorizeUrl).not.toBeNull();
38​
39 const url = new URL(authorizeUrl!);
40 expect(url.hostname).toBe("witty-mole-42.clerk.accounts.dev");
41 expect(url.pathname).toBe("/oauth/authorize");
42 expect(url.searchParams.get("redirect_uri")).toBe(
43 "https://nightly.app.t3.codes/connect/callback",
44 );
45 expect(url.searchParams.get("state")).toBe("state-1");
46 expect(url.searchParams.get("code_challenge")).toBe("challenge-1");
47 expect(url.searchParams.get("code_challenge_method")).toBe("S256");
48 });
49​
50 it("redirects straight to the CLI's loopback listener when the request carries a port", () => {
51 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
52 vi.stubEnv("VITE_CLERK_CLI_OAUTH_CLIENT_ID", "oauthapp_123");
53​
54 const authorizeUrl = buildConnectCliClerkAuthorizeUrl({
55 state: "state-1",
56 challenge: "challenge-1",
57 loopbackPort: 34338,
58 });
59 expect(authorizeUrl).not.toBeNull();
60​
61 const url = new URL(authorizeUrl!);
62 expect(url.searchParams.get("redirect_uri")).toBe("http://127.0.0.1:34338/callback");
63 expect(url.searchParams.get("state")).toBe("state-1");
64 });
65​
66 it("returns null when the CLI OAuth client id is not configured", () => {
67 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
68 expect(
69 buildConnectCliClerkAuthorizeUrl({ state: "state-1", challenge: "challenge-1" }),
70 ).toBeNull();
71 });
72​
73 it("sends the sign-in redirect to the authorize endpoint, not back to /connect", () => {
74 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
75 vi.stubEnv("VITE_CLERK_CLI_OAUTH_CLIENT_ID", "oauthapp_123");
76​
77 const connectUrl = "https://app.t3.codes/connect#state=state-1&challenge=challenge-1";
78 const redirectUrl = connectCliSignInRedirectUrl(
79 { state: "state-1", challenge: "challenge-1" },
80 connectUrl,
81 );
82​
83 expect(redirectUrl).not.toBe(connectUrl);
84 expect(new URL(redirectUrl).pathname).toBe("/oauth/authorize");
85 });
86​
87 it("falls back to the current URL when the authorize URL cannot be built", () => {
88 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
89​
90 const connectUrl = "https://app.t3.codes/connect#state=state-1&challenge=challenge-1";
91 expect(
92 connectCliSignInRedirectUrl({ state: "state-1", challenge: "challenge-1" }, connectUrl),
93 ).toBe(connectUrl);
94 });
95​
96 it("reads the code and state Clerk echoes back to the callback", () => {
97 expect(
98 readConnectCliCallbackResult(
99 new URL("https://app.t3.codes/connect/callback?code=abc&state=state-1"),
100 ),
101 ).toEqual({ code: "abc", state: "state-1" });
102 expect(
103 readConnectCliCallbackResult(new URL("https://app.t3.codes/connect/callback?code=abc")),
104 ).toBeNull();
105 expect(
106 readConnectCliCallbackResult(new URL("https://app.t3.codes/connect/callback?state=s")),
107 ).toBeNull();
108 });
109});