From 8aca76f8d7a61e06a868ab4d7fb3a07bf26ed50c Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Tue, 19 Mar 2024 13:48:36 +0100 Subject: [PATCH] feat: adjust setup for ui-rework e2e tests Signed-off-by: Raphael Arce --- __test-utils/req-test-token.ts | 44 +++++++++++++--------------------- __tests__/schema.test.ts | 37 +++++++++++++++++----------- supabase/config.toml | 8 +++---- 3 files changed, 43 insertions(+), 46 deletions(-) diff --git a/__test-utils/req-test-token.ts b/__test-utils/req-test-token.ts index 314178f5..0242bcfa 100644 --- a/__test-utils/req-test-token.ts +++ b/__test-utils/req-test-token.ts @@ -1,5 +1,9 @@ import { SignupResponse } from "../_types/user"; -import { SUPABASE_ANON_KEY, SUPABASE_URL } from "./supabase"; +import { + SUPABASE_ANON_KEY, + SUPABASE_URL, + supabaseServiceRoleClient, +} from "./supabase"; export async function requestSupabaseTestToken( email: string, @@ -27,33 +31,17 @@ export async function requestSupabaseTestToken( return json.access_token; } -export async function createSupabaseUser( - email: string, - password: string, - opts?: { returnFullUser: boolean } -) { - const response = await fetch(`${SUPABASE_URL}/auth/v1/signup`, { - method: "POST", - headers: { - "Content-Type": "application/json", - apikey: SUPABASE_ANON_KEY, - }, - body: JSON.stringify({ - email, - password, - }), +export async function createSupabaseUser(email: string, password: string) { + const { error } = await supabaseServiceRoleClient.auth.admin.createUser({ + email, + password, + email_confirm: true, }); - if (!response.ok) { - console.log(response.status); - const json = await response.text(); - throw new Error(`Could not create test user, ${json}`); - } - const json = (await response.json()) as { - access_token: string; - user: { id: string }; - }; - if (opts?.returnFullUser) { - return json; + + if (error) { + console.log(error.message); + throw new Error(`Could not create test user, ${error.message}`); } - return json.access_token; + + return requestSupabaseTestToken(email, password); } diff --git a/__tests__/schema.test.ts b/__tests__/schema.test.ts index 932f7d71..8170c03e 100644 --- a/__tests__/schema.test.ts +++ b/__tests__/schema.test.ts @@ -8,6 +8,7 @@ import { supabaseAnonClient, supabaseServiceRoleClient, } from "../__test-utils/supabase"; +import { requestSupabaseTestToken } from "../__test-utils/req-test-token"; describe("misc test testing the schema function of the database", () => { test("inserting an existing username should alter the new name and add a uuid at end", async () => { const email1 = "someone@email.com"; @@ -49,12 +50,16 @@ describe("misc test testing the schema function of the database", () => { const numberOfTrees = 10; const email = "user@email.com"; await deleteSupabaseUser(email); // clean up before running - const { data, error } = await supabaseAnonClient.auth.signUp({ - email: email, - password: "12345678", - }); + const { data, error } = + await supabaseServiceRoleClient.auth.admin.createUser({ + email: email, + password: "12345678", + email_confirm: true, + }); expect(error).toBeNull(); - expect(data).toBeDefined(); + expect(data.user).toBeDefined(); + const accessToken = await requestSupabaseTestToken(email, "12345678"); + expect(accessToken).toBeDefined(); const { data: trees, error: treesError } = await supabaseAnonClient .from("trees") .select("*") @@ -98,7 +103,7 @@ describe("misc test testing the schema function of the database", () => { headers: { apikey: SUPABASE_ANON_KEY, "Content-Type": "application/json", - Authorization: `Bearer ${data.session?.access_token}`, + Authorization: `Bearer ${accessToken}`, }, }); expect(response.ok).toBeTruthy(); @@ -107,7 +112,7 @@ describe("misc test testing the schema function of the database", () => { await supabaseAnonClient .from("trees_watered") .select("*") - .eq("uuid", data.user?.id); + .eq("uuid", data.user!.id); expect(treesAfterError).toBeNull(); expect(treesAfter).toHaveLength(0); @@ -115,7 +120,7 @@ describe("misc test testing the schema function of the database", () => { await supabaseAnonClient .from("trees_adopted") .select("*") - .eq("uuid", data.user?.id); + .eq("uuid", data.user!.id); expect(adoptedTreesAfterError).toBeNull(); expect(adoptedTreesAfter).toHaveLength(0); await truncateTreesWaterd(); @@ -126,12 +131,16 @@ describe("misc test testing the schema function of the database", () => { const numberOfTrees = 10; await deleteSupabaseUser(email); await truncateTreesWaterd(); - const { data, error } = await supabaseAnonClient.auth.signUp({ - email: email, - password: "12345678", - }); + const { data, error } = + await supabaseServiceRoleClient.auth.admin.createUser({ + email: email, + password: "12345678", + email_confirm: true, + }); expect(error).toBeNull(); - expect(data).toBeDefined(); + expect(data.user).toBeDefined(); + const accessToken = await requestSupabaseTestToken(email, "12345678"); + expect(accessToken).toBeDefined(); const { data: trees, error: treesError } = await supabaseAnonClient .from("trees") .select("*") @@ -164,7 +173,7 @@ describe("misc test testing the schema function of the database", () => { headers: { apikey: SUPABASE_ANON_KEY, "Content-Type": "application/json", - Authorization: `Bearer ${data.session?.access_token}`, + Authorization: `Bearer ${accessToken}`, }, body: JSON.stringify({ username: "bar", diff --git a/supabase/config.toml b/supabase/config.toml index a6361c6a..155e6159 100644 --- a/supabase/config.toml +++ b/supabase/config.toml @@ -40,11 +40,11 @@ file_size_limit = "50MiB" [auth] # The base URL of your website. Used as an allow-list for redirects and for constructing URLs used # in emails. -site_url = "http://localhost:3000" +site_url = "http://localhost:5173" # A list of *exact* URLs that auth providers are permitted to redirect to post authentication. additional_redirect_urls = [ - "https://localhost:3000", - "https://localhost:3000/reset-password" + "https://localhost:5173", + "https://localhost:5173/profile/reset-password" ] # How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 seconds (one # week). @@ -59,7 +59,7 @@ enable_signup = true # addresses. If disabled, only the new email is required to confirm. double_confirm_changes = true # If enabled, users need to confirm their email address before signing in. -enable_confirmations = false +enable_confirmations = true # Use an external OAuth provider. The full list of providers are: `apple`, `azure`, `bitbucket`, # `discord`, `facebook`, `github`, `gitlab`, `google`, `twitch`, `twitter`, `slack`, `spotify`.