-
Notifications
You must be signed in to change notification settings - Fork 3
/
global-setup.ts
96 lines (81 loc) · 2.94 KB
/
global-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { test as setup, expect } from "@playwright/test";
import type { Page } from "@playwright/test";
import smtpTester from "smtp-tester";
import type { MailServer } from "smtp-tester";
import { load as cheerioLoad } from "cheerio";
import { adminUser } from "./data/admin-user";
import { normalUser } from "./data/normal-user";
import { inactiveUser } from "./data/inactive-user";
import { truncateDb } from "../prisma/truncate-db";
import {
createInactiveUser,
createAdminUser,
createActiveUser,
createFeatureFlags,
} from "../prisma/seeds/seed";
/**
* It's required to run un serial mode because the mail server will try
* to start multiple times with the same port and it will fail (port already in use).
* Remember that the tests are run in parallel by default and Playwright will use
* different workers to run the tests.
*/
setup.describe.configure({ mode: "serial" });
let mailServer: MailServer;
// clean up database before everything.
// This is related to the global teardown not being called if runing tests in UI mode.
// Github issue: https://github.com/microsoft/playwright/issues/22008
// Is closed but still not working.
setup.beforeAll(async () => {
await truncateDb();
await createFeatureFlags();
mailServer = smtpTester.init(4025);
});
setup.afterAll(() => {
mailServer.stop(() => console.log("Mail server stopped"));
});
interface LoginUser {
user: {
email: string;
loginUrl: string;
appUrl: string;
storageSessionPath: string;
};
page: Page;
}
const loginUser = async ({ user, page }: LoginUser) => {
// arrange
await page.goto(user.loginUrl);
await expect(page.getByRole("link", { name: /Login/i })).toBeVisible();
// act
await page.getByLabel(/Email/i).fill(user.email);
await page.getByRole("button", { name: /Sign in with Email/i }).click();
// assert
await expect(
page.getByText(/Please check your email for the magic link/i),
).toBeVisible();
// get the email from the mail server
const { email } = await mailServer.captureOne(user.email, {
wait: 1000,
});
const $ = cheerioLoad(email.html as string);
const emailLink = $("a").attr("href");
await page.goto(emailLink as string);
// assert after login
await expect(page).toHaveURL(user.appUrl);
await expect(page.getByRole("button", { name: /user menu/i })).toBeVisible();
await expect(page.getByRole("link", { name: /Login/i })).not.toBeVisible();
await page.context().storageState({ path: user.storageSessionPath });
console.log(`${user.email} logged in`);
};
setup("let the admin user login with magic email", async ({ page }) => {
await createAdminUser();
await loginUser({ user: adminUser, page });
});
setup("let an active user login with magic email", async ({ page }) => {
await createActiveUser();
await loginUser({ user: normalUser, page });
});
setup("let an innactive user login with magic email", async ({ page }) => {
await createInactiveUser();
await loginUser({ user: inactiveUser, page });
});