22import { test , expect } from '@playwright/test' ;
33const mailhog = require ( 'mailhog' ) ( ) ;
44
5- const host = 'http://localhost:8090' ;
6-
75const TEST_EMAIL = 'testuser@example.com' ;
86const 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
1513function 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
2617function 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 ( / \[ C o n f i r m n e w e m a i l \] \( ( [ ^ ) ] + ) \) / 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 ( / \[ R e s e t p a s s w o r d \] \( ( [ ^ ) ] + ) \) / i) ;
33+ return match ? match [ 1 ] : null ;
34+ }
35+
3336async 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
4548test ( '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 }) => {
8992test ( '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
110113test ( '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 ( / \[ C o n f i r m n e w e m a i l \] \( ( [ ^ ) ] + ) \) / 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
144144test ( '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
152152test ( '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 ( / \[ R e s e t P a s s w o r d \] \( ( [ ^ ) ] + ) \) / 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