Skip to content

Commit

Permalink
Merge pull request #246 from technologiestiftung/feat/adjust-local-se…
Browse files Browse the repository at this point in the history
…tup-for-ui-rework-e2e-tests

feat: adjust setup for ui-rework e2e tests
  • Loading branch information
raphael-arce authored Mar 20, 2024
2 parents c8e80b5 + 8aca76f commit ec80770
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 46 deletions.
44 changes: 16 additions & 28 deletions __test-utils/req-test-token.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
}
37 changes: 23 additions & 14 deletions __tests__/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]";
Expand Down Expand Up @@ -49,12 +50,16 @@ describe("misc test testing the schema function of the database", () => {
const numberOfTrees = 10;
const email = "[email protected]";
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("*")
Expand Down Expand Up @@ -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();
Expand All @@ -107,15 +112,15 @@ 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);

const { data: adoptedTreesAfter, error: adoptedTreesAfterError } =
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();
Expand All @@ -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("*")
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions supabase/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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`.
Expand Down

0 comments on commit ec80770

Please sign in to comment.