|
| 1 | +import { ConvexHttpClient } from "convex/browser"; |
| 2 | +import { ConvexReactClient } from "convex/react"; |
| 3 | +import { decodeJwt } from "jose"; |
| 4 | +import { it } from "../helpers"; |
| 5 | +import { createApp } from "./js-helpers"; |
| 6 | + |
| 7 | + |
| 8 | +class MockWebSocket { |
| 9 | + static last: MockWebSocket | undefined; |
| 10 | + url: string; |
| 11 | + onopen: ((ev: any) => void) | null = null; |
| 12 | + onmessage: ((ev: any) => void) | null = null; |
| 13 | + onclose: ((ev: any) => void) | null = null; |
| 14 | + sent: Array<{ raw: any, json: any }> = []; |
| 15 | + constructor(url: string) { |
| 16 | + this.url = url; |
| 17 | + MockWebSocket.last = this; |
| 18 | + } |
| 19 | + send(data: any) { |
| 20 | + let json: any; |
| 21 | + try { |
| 22 | + json = JSON.parse(String(data)); |
| 23 | + } catch { |
| 24 | + json = null; |
| 25 | + } |
| 26 | + this.sent.push({ raw: data, json }); |
| 27 | + } |
| 28 | + close() { |
| 29 | + if (this.onclose) this.onclose({ code: 1000, reason: "" } as any); |
| 30 | + } |
| 31 | + open() { |
| 32 | + if (this.onopen) this.onopen({} as any); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const signIn = async (clientApp: any) => { |
| 37 | + await clientApp.signUpWithCredential({ |
| 38 | + email: "test@test.com", |
| 39 | + password: "password", |
| 40 | + verificationCallbackUrl: "http://localhost:3000", |
| 41 | + }); |
| 42 | + await clientApp.signInWithCredential({ |
| 43 | + email: "test@test.com", |
| 44 | + password: "password", |
| 45 | + }); |
| 46 | +}; |
| 47 | + |
| 48 | +it("should provide a valid auth getter for Convex React client", async ({ expect }) => { |
| 49 | + const { clientApp } = await createApp({}); |
| 50 | + await signIn(clientApp); |
| 51 | + |
| 52 | + const getter = clientApp.getConvexClientAuth({ tokenStore: "memory" }); |
| 53 | + const token2 = await getter({ forceRefreshToken: true }); |
| 54 | + expect(typeof token2).toBe("string"); |
| 55 | + expect((token2 as string).length).toBeGreaterThan(1); |
| 56 | + |
| 57 | + const convex = new ConvexReactClient("http://localhost:1234", { webSocketConstructor: MockWebSocket as any, expectAuth: true }); |
| 58 | + convex.setAuth(getter); |
| 59 | + MockWebSocket.last?.open(); |
| 60 | + // wait up to 1s (10 x 100ms) until both Connect and Authenticate messages are seen |
| 61 | + let connectMsg: any = undefined; |
| 62 | + let authMsg: any = undefined; |
| 63 | + for (let i = 0; i < 10; i++) { |
| 64 | + const msgs = (MockWebSocket.last?.sent ?? []).map(m => m.json); |
| 65 | + connectMsg = msgs.find(m => m?.type === "Connect"); |
| 66 | + authMsg = msgs.find(m => m?.type === "Authenticate" && m?.tokenType === "User"); |
| 67 | + if (connectMsg && authMsg) break; |
| 68 | + await new Promise(r => setTimeout(r, 100)); |
| 69 | + } |
| 70 | + expect(connectMsg).toBeDefined(); |
| 71 | + expect(authMsg).toBeDefined(); |
| 72 | + expect((authMsg as any).value).toBe(token2); |
| 73 | +}); |
| 74 | + |
| 75 | +it("should provide a valid auth token for Convex HTTP client", async ({ expect }) => { |
| 76 | + const { clientApp } = await createApp({}); |
| 77 | + await signIn(clientApp); |
| 78 | + |
| 79 | + const token = await clientApp.getConvexHttpClientAuth({ tokenStore: "memory" }); |
| 80 | + expect(typeof token).toBe("string"); |
| 81 | + expect(token.length).toBeGreaterThan(1); |
| 82 | + |
| 83 | + const user = await clientApp.getUser({ or: "throw" }); |
| 84 | + const payload: any = decodeJwt(token); |
| 85 | + expect(payload.sub).toBe(user.id); |
| 86 | + |
| 87 | + const convex = new ConvexHttpClient("http://localhost:1234"); |
| 88 | + convex.setAuth(token); |
| 89 | + |
| 90 | + const originalFetch = globalThis.fetch; |
| 91 | + let capturedAuth: string | undefined; |
| 92 | + globalThis.fetch = (async (_input: any, init?: any) => { |
| 93 | + capturedAuth = init?.headers?.Authorization; |
| 94 | + return new Response(JSON.stringify({ status: "success", value: null, logLines: [] }), { status: 200, headers: { "Content-Type": "application/json" } }); |
| 95 | + }) as any; |
| 96 | + try { |
| 97 | + await (convex as any).function("any"); |
| 98 | + } finally { |
| 99 | + globalThis.fetch = originalFetch; |
| 100 | + } |
| 101 | + expect(capturedAuth).toBe(`Bearer ${token}`); |
| 102 | +}); |
| 103 | + |
| 104 | +it("should map convex ctx identity to partial user", async ({ expect }) => { |
| 105 | + const { clientApp } = await createApp({}); |
| 106 | + await signIn(clientApp); |
| 107 | + |
| 108 | + const user = await clientApp.getUser({ or: "throw" }); |
| 109 | + const identity = { |
| 110 | + subject: user.id, |
| 111 | + name: user.displayName, |
| 112 | + email: user.primaryEmail, |
| 113 | + email_verified: user.primaryEmailVerified, |
| 114 | + is_anonymous: user.isAnonymous, |
| 115 | + } as const; |
| 116 | + |
| 117 | + const ctx: any = { |
| 118 | + auth: { |
| 119 | + getUserIdentity: async () => identity, |
| 120 | + }, |
| 121 | + }; |
| 122 | + |
| 123 | + const partialUser = await clientApp.getPartialUser({ from: "convex", ctx }); |
| 124 | + expect(partialUser).toEqual({ |
| 125 | + id: user.id, |
| 126 | + displayName: user.displayName, |
| 127 | + primaryEmail: user.primaryEmail, |
| 128 | + primaryEmailVerified: user.primaryEmailVerified, |
| 129 | + isAnonymous: user.isAnonymous, |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +it("should return null partial user when convex identity is absent", async ({ expect }) => { |
| 134 | + const { clientApp } = await createApp({}); |
| 135 | + await signIn(clientApp); |
| 136 | + |
| 137 | + const ctx: any = { |
| 138 | + auth: { |
| 139 | + getUserIdentity: async () => null, |
| 140 | + }, |
| 141 | + }; |
| 142 | + |
| 143 | + const partialUser = await clientApp.getPartialUser({ from: "convex", ctx }); |
| 144 | + expect(partialUser).toBeNull(); |
| 145 | +}); |
| 146 | + |
| 147 | + |
| 148 | +it("should return the server user when provided Convex ctx identity", async ({ expect }) => { |
| 149 | + const { clientApp, serverApp } = await createApp({}); |
| 150 | + await signIn(clientApp); |
| 151 | + |
| 152 | + const user = await clientApp.getUser({ or: "throw" }); |
| 153 | + const identity = { subject: user.id } as const; |
| 154 | + |
| 155 | + const ctx: any = { |
| 156 | + auth: { |
| 157 | + getUserIdentity: async () => identity, |
| 158 | + }, |
| 159 | + }; |
| 160 | + |
| 161 | + const serverUser = await serverApp.getUser({ from: "convex", ctx }); |
| 162 | + expect(serverUser).not.toBeNull(); |
| 163 | + expect(serverUser!.id).toBe(user.id); |
| 164 | + expect(serverUser!.isAnonymous).toBe(false); |
| 165 | +}); |
| 166 | + |
| 167 | +it("should return null when Convex ctx identity is absent for server getUser", async ({ expect }) => { |
| 168 | + const { serverApp } = await createApp({}); |
| 169 | + |
| 170 | + const ctx: any = { |
| 171 | + auth: { |
| 172 | + getUserIdentity: async () => null, |
| 173 | + }, |
| 174 | + }; |
| 175 | + |
| 176 | + const serverUser = await serverApp.getUser({ from: "convex", ctx }); |
| 177 | + expect(serverUser).toBeNull(); |
| 178 | +}); |
| 179 | + |
| 180 | + |
0 commit comments