Skip to content

Commit

Permalink
Fix e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
massimobiagioli committed Apr 14, 2023
1 parent cbe0841 commit 520e553
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 53 deletions.
9 changes: 4 additions & 5 deletions .env.example
@@ -1,8 +1,7 @@
NODE_ENV=test

PUBLIC_SUPABASE_URL=https://your-project.supabase.co
PUBLIC_SUPABASE_ANON_KEY=your-anon-key

MAILSLURP_APIKEY=your-mailslurp-api-key
MAILSLURP_MAILBOX_ID=your-mailslurp-mailbox-id
MAILSLURP_EMAIL_ADDRESS=your-mailslurp-email-address
# Email
MAILBOX_ID=your-mailbox-id
EMAIL_ADDRESS=your-email-address
INBUCKET_URL=http://127.0.0.1:54324
8 changes: 3 additions & 5 deletions playwright.config.ts
@@ -1,21 +1,19 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import * as dotenv from "dotenv";
import * as dotenv from 'dotenv';

dotenv.config();

process.env['NODE_ENV'] = 'test';

const config: PlaywrightTestConfig = {
use: {
baseURL: 'http://localhost:4173',
storageState: 'storage-state.json',
storageState: 'storage-state.json'
},
webServer: {
command: 'npm run build && npm run preview',
url: 'http://localhost:4173'
},
testDir: 'tests/e2e',
globalSetup: './tests/helper/global-setup.ts',
globalSetup: './tests/helper/global-setup.ts'
};

export default config;
2 changes: 1 addition & 1 deletion src/lib/shared/supabase/auth.ts
Expand Up @@ -7,7 +7,7 @@ export default function createAuth(supabase: SupabaseClient<Database>) {
return supabase.auth.signInWithOtp({
email,
options: {
emailRedirectTo: window.location.origin //(process.env.NODE_ENV === 'test' ? 'http://127.0.0.1:4173' : window.location.origin)
emailRedirectTo: window.location.origin
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion supabase/seed.sql
@@ -1,6 +1,6 @@
INSERT INTO auth.users (instance_id, id, aud, role, email, encrypted_password, email_confirmed_at, invited_at, confirmation_token, confirmation_sent_at, recovery_token, recovery_sent_at, email_change_token_new, email_change, email_change_sent_at, last_sign_in_at, raw_app_meta_data, raw_user_meta_data, is_super_admin, created_at, updated_at, phone, phone_confirmed_at, phone_change, phone_change_token, phone_change_sent_at, email_change_token_current, email_change_confirm_status, banned_until, reauthentication_token, reauthentication_sent_at)
VALUES
('00000000-0000-0000-0000-000000000000'::uuid, '5899f99d-a449-4bfa-8769-19c097aaf1f5'::uuid, 'authenticated', 'authenticated', 'bf0ccc89-9241-4365-b371-f2b30b5aa813@mailslurp.com', '$2a$10$4r8CoPQ2kNK7.h.BKpCcquE80/ib1S8ATma5PTl5D4FJFZffAKjGy', '2022-10-04 03:41:27.39308+00', NULL, '', NULL, '', NULL, '', '', NULL, NULL, '{"provider": "email", "providers": ["email"]}', '{}', NULL, '2022-10-04 03:41:27.391146+00', '2022-10-04 03:41:27.39308+00', NULL, NULL, '', '', NULL, '', 0, NULL, '', NULL);
('00000000-0000-0000-0000-000000000000'::uuid, '5899f99d-a449-4bfa-8769-19c097aaf1f5'::uuid, 'authenticated', 'authenticated', 'tester123@email.com', '$2a$10$4r8CoPQ2kNK7.h.BKpCcquE80/ib1S8ATma5PTl5D4FJFZffAKjGy', '2022-10-04 03:41:27.39308+00', NULL, '', NULL, '', NULL, '', '', NULL, NULL, '{"provider": "email", "providers": ["email"]}', '{}', NULL, '2022-10-04 03:41:27.391146+00', '2022-10-04 03:41:27.39308+00', NULL, NULL, '', '', NULL, '', 0, NULL, '', NULL);

INSERT INTO auth.identities (id,user_id,identity_data,provider,last_sign_in_at,created_at,updated_at)
VALUES
Expand Down
14 changes: 4 additions & 10 deletions tests/e2e/devices.test.ts
@@ -1,15 +1,9 @@
import { expect, test } from '@playwright/test';

test.describe('devices', () => {

test('authenticated', async ({ page }) => {
await page.goto("/devices");

// Print page content to console
//console.log(await page.textContent('body'));

await expect(
page.getByRole('heading', { name: 'Device List' })
).toBeVisible();
await page.goto('/devices');

await expect(page.getByRole('heading', { name: 'Device List' })).toBeVisible();
});
});
});
60 changes: 29 additions & 31 deletions tests/helper/global-setup.ts
@@ -1,37 +1,35 @@
import { chromium, type FullConfig } from '@playwright/test'
import { chromium, type FullConfig } from '@playwright/test';
import { InbucketAPIClient } from 'inbucket-js-client';

const emailAddress = process.env.MAILSLURP_EMAIL_ADDRESS as string;
const mailboxId = process.env.MAILSLURP_MAILBOX_ID as string;
const client = new InbucketAPIClient('http://127.0.0.1:54324');
const emailAddress = process.env.EMAIL_ADDRESS as string;
const mailboxId = process.env.MAILBOX_ID as string;
const client = new InbucketAPIClient(process.env.INBUCKET_URL as string);

async function globalSetup(config: FullConfig) {
const browser = await chromium.launch()
const page = await browser.newPage()

await page.goto(`${config.webServer?.url}/login`)

await page
.locator('input[name="email"]')
.type(emailAddress, { delay: 100 });

await page
.locator('button[type="submit"]')
.click({ delay: 100 });

await page.waitForSelector(
`text=An email was sent with a magic link. Please check your mailbox.`
);

const inbox = await client.mailbox(mailboxId)
const email = await client.message(mailboxId, inbox[0].id)
const matches = email.body?.html.match(/href="([^"]*)"/);
const href = matches?.[1] as string;

await page.goto(href);
await page.context().storageState({ path: 'storage-state.json' })
await browser.close();
await client.prugeMailbox(mailboxId);
const browser = await chromium.launch();
const page = await browser.newPage();

await page.goto(`${config.webServer?.url}/login`);

await page.locator('input[name="email"]').type(emailAddress, { delay: 100 });

await page.locator('button[type="submit"]').click({ delay: 100 });

await page.waitForSelector(
`text=An email was sent with a magic link. Please check your mailbox.`
);

const inbox = await client.mailbox(mailboxId);

const email = await client.message(mailboxId, inbox[inbox.length - 1].id);
const matches = email.body?.html.match(/href="([^"]*)"/);
const href = (matches?.[1] as string).replace(/&amp;/g, '&');

await page.goto(href);
await page.screenshot({ path: 'shit.png' });
await page.context().storageState({ path: 'storage-state.json' });
await browser.close();
await client.prugeMailbox(mailboxId);
}

export default globalSetup
export default globalSetup;

0 comments on commit 520e553

Please sign in to comment.