|
| 1 | +import { createClient } from "@supabase/supabase-js"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Creates a Supabase admin client using the secret (service role) key. |
| 5 | + * Used in E2E tests to create/delete temporary test users and query data |
| 6 | + * that bypasses RLS. |
| 7 | + */ |
| 8 | +function getAdminClient() { |
| 9 | + const url = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 10 | + const secretKey = process.env.SUPABASE_SECRET_KEY; |
| 11 | + |
| 12 | + if (!url || !secretKey) { |
| 13 | + throw new Error( |
| 14 | + "NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SECRET_KEY must be set for admin E2E helpers" |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + return createClient(url, secretKey, { |
| 19 | + auth: { autoRefreshToken: false, persistSession: false }, |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +interface TestUser { |
| 24 | + id: string; |
| 25 | + email: string; |
| 26 | + password: string; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Creates a temporary test user via the Supabase Admin API. |
| 31 | + * The user is auto-confirmed so they can sign in immediately. |
| 32 | + */ |
| 33 | +export async function createTestUser( |
| 34 | + email: string, |
| 35 | + password: string, |
| 36 | + displayName: string |
| 37 | +): Promise<TestUser> { |
| 38 | + const admin = getAdminClient(); |
| 39 | + |
| 40 | + const { data, error } = await admin.auth.admin.createUser({ |
| 41 | + email, |
| 42 | + password, |
| 43 | + email_confirm: true, |
| 44 | + user_metadata: { display_name: displayName }, |
| 45 | + }); |
| 46 | + |
| 47 | + if (error) { |
| 48 | + throw new Error(`Failed to create test user ${email}: ${error.message}`); |
| 49 | + } |
| 50 | + |
| 51 | + return { id: data.user.id, email, password }; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Deletes a test user and all their data via the Supabase Admin API. |
| 56 | + */ |
| 57 | +export async function deleteTestUser(userId: string): Promise<void> { |
| 58 | + const admin = getAdminClient(); |
| 59 | + |
| 60 | + // Delete memberships first (the user's personal workspace will cascade) |
| 61 | + await admin.from("members").delete().eq("user_id", userId); |
| 62 | + |
| 63 | + // Delete workspaces created by this user |
| 64 | + await admin.from("workspaces").delete().eq("created_by", userId); |
| 65 | + |
| 66 | + // Delete profile |
| 67 | + await admin.from("profiles").delete().eq("id", userId); |
| 68 | + |
| 69 | + // Delete the auth user |
| 70 | + const { error } = await admin.auth.admin.deleteUser(userId); |
| 71 | + if (error) { |
| 72 | + console.warn(`Failed to delete test user ${userId}: ${error.message}`); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Fetches the invite token for a given email and workspace from the database. |
| 78 | + * Bypasses RLS via the admin client. |
| 79 | + */ |
| 80 | +export async function getInviteToken( |
| 81 | + workspaceId: string, |
| 82 | + email: string |
| 83 | +): Promise<string> { |
| 84 | + const admin = getAdminClient(); |
| 85 | + |
| 86 | + const { data, error } = await admin |
| 87 | + .from("workspace_invites") |
| 88 | + .select("token") |
| 89 | + .eq("workspace_id", workspaceId) |
| 90 | + .ilike("email", email) |
| 91 | + .is("accepted_at", null) |
| 92 | + .order("created_at", { ascending: false }) |
| 93 | + .limit(1) |
| 94 | + .maybeSingle(); |
| 95 | + |
| 96 | + if (error || !data) { |
| 97 | + throw new Error( |
| 98 | + `No pending invite found for ${email}: ${error?.message ?? "not found"}` |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + return data.token; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Fetches the workspace ID and slug for a user's first non-personal workspace, |
| 107 | + * or their personal workspace if no team workspaces exist. |
| 108 | + */ |
| 109 | +export async function getWorkspaceForUser( |
| 110 | + userId: string |
| 111 | +): Promise<{ id: string; slug: string }> { |
| 112 | + const admin = getAdminClient(); |
| 113 | + |
| 114 | + const { data, error } = await admin |
| 115 | + .from("members") |
| 116 | + .select("workspace_id, workspaces(id, slug, is_personal)") |
| 117 | + .eq("user_id", userId) |
| 118 | + .limit(10); |
| 119 | + |
| 120 | + if (error || !data || data.length === 0) { |
| 121 | + throw new Error( |
| 122 | + `No workspace found for user ${userId}: ${error?.message ?? "no memberships"}` |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + // Prefer non-personal workspace, fall back to personal |
| 127 | + for (const row of data) { |
| 128 | + const ws = row.workspaces as unknown as { |
| 129 | + id: string; |
| 130 | + slug: string; |
| 131 | + is_personal: boolean; |
| 132 | + }; |
| 133 | + if (!ws.is_personal) { |
| 134 | + return { id: ws.id, slug: ws.slug }; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + const ws = data[0].workspaces as unknown as { id: string; slug: string }; |
| 139 | + return { id: ws.id, slug: ws.slug }; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Cleans up any pending invites for a given email across all workspaces. |
| 144 | + */ |
| 145 | +export async function cleanupInvitesForEmail(email: string): Promise<void> { |
| 146 | + const admin = getAdminClient(); |
| 147 | + await admin |
| 148 | + .from("workspace_invites") |
| 149 | + .delete() |
| 150 | + .ilike("email", email) |
| 151 | + .is("accepted_at", null); |
| 152 | +} |
0 commit comments