Skip to content

Commit 4e6990a

Browse files
committed
All tests passing
1 parent 15d2dc0 commit 4e6990a

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

tests/main.spec.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import { test, expect } from '@playwright/test';
33
const mailhog = require('mailhog')();
44

5-
const host = 'http://localhost:8090';
6-
75
const TEST_EMAIL = 'testuser@example.com';
86
const TEST_PASSWORD = 'test_password1234';
97

@@ -13,14 +11,7 @@ let NEW_EMAIL = 'changeduser@example.com';
1311

1412
// Helper to extract the body from a MailHog message
1513
function getMailBody(result) {
16-
if (!result) return '';
17-
// Try common MailHog API shapes
18-
if (result.Content && result.Content.Body) return result.Content.Body;
19-
if (result.Body) return result.Body;
20-
if (result.MIME && result.MIME.Body) return result.MIME.Body;
21-
if (result.html && typeof result.html === 'string') return result.html;
22-
if (result.text && typeof result.text === 'string') return result.text;
23-
return '';
14+
return result.Content.Body;
2415
}
2516

2617
function extractVerifyLinkFromBody(body) {
@@ -30,8 +21,20 @@ function extractVerifyLinkFromBody(body) {
3021
return match ? match[1] : null;
3122
}
3223

24+
function extractConfirmEmailLinkFromBody(body) {
25+
const cleanedBody = body.replace(/=\r?\n/g, '');
26+
const match = cleanedBody.match(/\[Confirm new email\]\(([^)]+)\)/i);
27+
return match ? match[1] : null;
28+
}
29+
30+
function extractResetPasswordLinkFromBody(body) {
31+
const cleanedBody = body.replace(/=\r?\n/g, '');
32+
const match = cleanedBody.match(/\[Reset password\]\(([^)]+)\)/i);
33+
return match ? match[1] : null;
34+
}
35+
3336
async function login(page, email, password = LATEST_PASSWORD) {
34-
await page.goto(`${host}/auth/login`);
37+
await page.goto(`/auth/login`);
3538
await page.fill('input[name="identity"]', email);
3639
await page.fill('input[name="password"]', password);
3740
await page.click('button[type="submit"]');
@@ -43,7 +46,7 @@ async function expectLoggedIn(page) {
4346
}
4447

4548
test('register an account (registration only)', async ({ page }) => {
46-
await page.goto(`${host}/auth/register`);
49+
await page.goto(`/auth/register`);
4750

4851
// Fill in the registration form
4952
await page.fill('input[name="identity"]', TEST_EMAIL);
@@ -89,7 +92,7 @@ test('login with verified account', async ({ page, context }) => {
8992
test('Change password', async ({ page, context }) => {
9093
await login(page, TEST_EMAIL);
9194

92-
await page.goto(`${host}/account/change-password`);
95+
await page.goto(`/account/change-password`);
9396
await page.fill('input[name="currentPassword"]', LATEST_PASSWORD);
9497
const newPassword = 'new_password_' + Date.now();
9598
await page.fill('input[name="newPassword"]', newPassword);
@@ -109,7 +112,7 @@ test('Login with changed password', async ({ page, context }) => {
109112

110113
test('Change email', async ({ page, context }) => {
111114
await login(page, TEST_EMAIL, LATEST_PASSWORD);
112-
await page.goto(`${host}/account/change-email`);
115+
await page.goto(`/account/change-email`);
113116
await page.fill('input[name="identity"]', NEW_EMAIL);
114117
await page.click('button[type="submit"]');
115118
const flashContainer = await page.getByTestId('flashContainer');
@@ -122,16 +125,13 @@ test('Confirm Change email', async ({ page, context }) => {
122125
expect(result).not.toBeNull();
123126

124127
const body = getMailBody(result);
125-
const match = body.match(/\[Confirm new email\]\(([^)]+)\)/i);
126-
expect(match).not.toBeNull();
128+
const confirmLink = extractConfirmEmailLinkFromBody(body);
129+
expect(confirmLink).not.toBeNull();
127130

128-
const confirmLink = match[1];
129131
await page.goto(confirmLink);
130132
await page.fill('input[name="password"]', LATEST_PASSWORD);
131133
await page.click('button[type="submit"]');
132134

133-
await page.screenshot({ path: 'screenshot.png' });
134-
135135
const flashContainer = await page.getByTestId('flashContainer');
136136
await expect(flashContainer).toHaveText("Your email has been changed. You will need to login again.");
137137
});
@@ -142,17 +142,18 @@ test('Login with changed email', async ({ page, context }) => {
142142
});
143143

144144
test('Logout', async ({ page, context }) => {
145-
await page.goto(`${host}/account/logout`);
145+
await page.goto(`/account/logout`);
146146

147147
const cookies = await context.cookies();
148148
const pbAuthCookie = cookies.find(c => c.name === 'pb_auth');
149149
expect(pbAuthCookie).toBeUndefined();
150150
});
151151

152152
test('Send Forgot Password Reset', async ({ page, context }) => {
153-
await page.goto(`${host}/auth/forgot`);
153+
await page.goto(`/auth/forgot`);
154154
await page.fill('input[name="identity"]', NEW_EMAIL);
155155
await page.click('button[type="submit"]');
156+
156157
const flashContainer = await page.getByTestId('flashContainer');
157158
await expect(flashContainer).toHaveText("Password reset email sent, please check your inbox.");
158159
});
@@ -162,14 +163,17 @@ test('Confirm Forgot Password Reset', async ({ page, context }) => {
162163
const RESET_PASSWORD = 'reset_password_9999';
163164
const result = await mailhog.latestTo(NEW_EMAIL);
164165
expect(result).not.toBeNull();
166+
165167
const body = getMailBody(result);
166-
const match = body.match(/\[Reset Password\]\(([^)]+)\)/i);
167-
expect(match).not.toBeNull();
168-
const resetLink = match[1];
169-
await page.goto(resetLink);
170-
await page.fill('input[name="newPassword"]', RESET_PASSWORD);
168+
const resetPasswordLink = extractResetPasswordLinkFromBody(body);
169+
expect(resetPasswordLink).not.toBeNull();
170+
171+
await page.goto(resetPasswordLink);
172+
173+
await page.fill('input[name="password"]', RESET_PASSWORD);
171174
await page.fill('input[name="confirmPassword"]', RESET_PASSWORD);
172175
await page.click('button[type="submit"]');
176+
173177
LATEST_PASSWORD = RESET_PASSWORD;
174178
const flashContainer = await page.getByTestId('flashContainer');
175179
await expect(flashContainer).toHaveText("Your password has been reset, you can now login.");

0 commit comments

Comments
 (0)