|
1 | 1 | import { expect, test } from '@playwright/test'; |
2 | 2 | import { PLAYWRIGHT_BASE_URL } from '../playwright/config'; |
| 3 | +import { getPasswordResetUrl } from './utils/mailpit'; |
3 | 4 |
|
4 | 5 | async function registerNewUser(page, email, password) { |
5 | 6 | await page.goto(PLAYWRIGHT_BASE_URL + '/register'); |
@@ -35,14 +36,200 @@ test('can register and delete account', async ({ page }) => { |
35 | 36 | await registerNewUser(page, email, password); |
36 | 37 | await page.goto(PLAYWRIGHT_BASE_URL + '/user/profile'); |
37 | 38 | await page.getByRole('button', { name: 'Delete Account' }).click(); |
| 39 | + await expect(page.getByRole('dialog')).toBeVisible(); |
38 | 40 | await page.getByPlaceholder('Password').fill(password); |
39 | | - await page.getByRole('button', { name: 'Delete Account' }).click(); |
| 41 | + await page.getByRole('dialog').getByRole('button', { name: 'Delete Account' }).click(); |
40 | 42 | await page.waitForURL(PLAYWRIGHT_BASE_URL + '/login'); |
41 | 43 | await page.goto(PLAYWRIGHT_BASE_URL + '/login'); |
42 | 44 | await page.getByLabel('Email').fill(email); |
43 | 45 | await page.getByLabel('Password').fill(password); |
44 | 46 | await page.getByRole('button', { name: 'Log in' }).click(); |
45 | | - await expect(page.getByRole('paragraph')).toContainText( |
| 47 | + await expect(page.getByRole('alert')).toContainText( |
46 | 48 | 'These credentials do not match our records.' |
47 | 49 | ); |
48 | 50 | }); |
| 51 | + |
| 52 | +test('shows error for invalid email on forgot password', async ({ page }) => { |
| 53 | + await page.goto(PLAYWRIGHT_BASE_URL + '/forgot-password'); |
| 54 | + |
| 55 | + // Request password reset with non-existent email |
| 56 | + await page.getByLabel('Email').fill('nonexistent@example.com'); |
| 57 | + await page.getByRole('button', { name: 'Email Password Reset Link' }).click(); |
| 58 | + |
| 59 | + // Should show error message |
| 60 | + await expect(page.getByText("We can't find a user with that email address.")).toBeVisible(); |
| 61 | +}); |
| 62 | + |
| 63 | +test('shows browser validation for invalid email format on forgot password', async ({ page }) => { |
| 64 | + await page.goto(PLAYWRIGHT_BASE_URL + '/forgot-password'); |
| 65 | + |
| 66 | + // Request password reset with invalid email format |
| 67 | + const emailInput = page.getByLabel('Email'); |
| 68 | + await emailInput.fill('notanemail'); |
| 69 | + |
| 70 | + // Check for browser validation - the input should be invalid |
| 71 | + const isInvalid = await emailInput.evaluate((el: HTMLInputElement) => !el.validity.valid); |
| 72 | + expect(isInvalid).toBe(true); |
| 73 | +}); |
| 74 | + |
| 75 | +test('shows browser validation for empty email on forgot password', async ({ page }) => { |
| 76 | + await page.goto(PLAYWRIGHT_BASE_URL + '/forgot-password'); |
| 77 | + |
| 78 | + // The email input is required, so it should be invalid when empty |
| 79 | + const emailInput = page.getByLabel('Email'); |
| 80 | + |
| 81 | + // Check for browser validation - the input should be invalid because it's required and empty |
| 82 | + const isInvalid = await emailInput.evaluate((el: HTMLInputElement) => el.validity.valueMissing); |
| 83 | + expect(isInvalid).toBe(true); |
| 84 | +}); |
| 85 | + |
| 86 | +test('can reset password via email link', async ({ page, request }) => { |
| 87 | + // First register a new user |
| 88 | + const email = `john+${Math.round(Math.random() * 10000)}@doe.com`; |
| 89 | + const originalPassword = 'suchagreatpassword123'; |
| 90 | + const newPassword = 'mynewsecurepassword456'; |
| 91 | + await registerNewUser(page, email, originalPassword); |
| 92 | + |
| 93 | + // Log out |
| 94 | + await page.getByTestId('current_user_button').click(); |
| 95 | + await page.getByText('Log Out').click(); |
| 96 | + await page.waitForURL(PLAYWRIGHT_BASE_URL + '/login'); |
| 97 | + |
| 98 | + // Request password reset |
| 99 | + await page.goto(PLAYWRIGHT_BASE_URL + '/forgot-password'); |
| 100 | + await page.getByLabel('Email').fill(email); |
| 101 | + await page.getByRole('button', { name: 'Email Password Reset Link' }).click(); |
| 102 | + await expect(page.getByText('We have emailed your password reset link.')).toBeVisible(); |
| 103 | + |
| 104 | + // Get password reset URL from email |
| 105 | + const resetUrl = await getPasswordResetUrl(request, email); |
| 106 | + |
| 107 | + // Navigate to reset page |
| 108 | + await page.goto(resetUrl); |
| 109 | + |
| 110 | + // Fill in new password |
| 111 | + await page.getByLabel('Password', { exact: true }).fill(newPassword); |
| 112 | + await page.getByLabel('Confirm Password').fill(newPassword); |
| 113 | + await page.getByRole('button', { name: 'Reset Password' }).click(); |
| 114 | + |
| 115 | + // Should redirect to login page after successful reset |
| 116 | + await page.waitForURL(PLAYWRIGHT_BASE_URL + '/login'); |
| 117 | + |
| 118 | + // Try logging in with new password |
| 119 | + await page.getByLabel('Email').fill(email); |
| 120 | + await page.getByLabel('Password').fill(newPassword); |
| 121 | + await page.getByRole('button', { name: 'Log in' }).click(); |
| 122 | + await expect(page.getByTestId('dashboard_view')).toBeVisible(); |
| 123 | +}); |
| 124 | + |
| 125 | +test('shows validation error for password mismatch on reset', async ({ page, request }) => { |
| 126 | + // First register a new user |
| 127 | + const email = `john+${Math.round(Math.random() * 10000)}@doe.com`; |
| 128 | + const originalPassword = 'suchagreatpassword123'; |
| 129 | + await registerNewUser(page, email, originalPassword); |
| 130 | + |
| 131 | + // Log out |
| 132 | + await page.getByTestId('current_user_button').click(); |
| 133 | + await page.getByText('Log Out').click(); |
| 134 | + await page.waitForURL(PLAYWRIGHT_BASE_URL + '/login'); |
| 135 | + |
| 136 | + // Request password reset |
| 137 | + await page.goto(PLAYWRIGHT_BASE_URL + '/forgot-password'); |
| 138 | + await page.getByLabel('Email').fill(email); |
| 139 | + await page.getByRole('button', { name: 'Email Password Reset Link' }).click(); |
| 140 | + await expect(page.getByText('We have emailed your password reset link.')).toBeVisible(); |
| 141 | + |
| 142 | + // Get password reset URL from email |
| 143 | + const resetUrl = await getPasswordResetUrl(request, email); |
| 144 | + |
| 145 | + // Navigate to reset page |
| 146 | + await page.goto(resetUrl); |
| 147 | + |
| 148 | + // Fill in mismatched passwords |
| 149 | + await page.getByLabel('Password', { exact: true }).fill('newpassword123'); |
| 150 | + await page.getByLabel('Confirm Password').fill('differentpassword456'); |
| 151 | + await page.getByRole('button', { name: 'Reset Password' }).click(); |
| 152 | + |
| 153 | + // Should show validation error |
| 154 | + await expect(page.getByText('The password field confirmation does not match.')).toBeVisible(); |
| 155 | +}); |
| 156 | + |
| 157 | +test('shows validation error for short password on reset', async ({ page, request }) => { |
| 158 | + // First register a new user |
| 159 | + const email = `john+${Math.round(Math.random() * 10000)}@doe.com`; |
| 160 | + const originalPassword = 'suchagreatpassword123'; |
| 161 | + await registerNewUser(page, email, originalPassword); |
| 162 | + |
| 163 | + // Log out |
| 164 | + await page.getByTestId('current_user_button').click(); |
| 165 | + await page.getByText('Log Out').click(); |
| 166 | + await page.waitForURL(PLAYWRIGHT_BASE_URL + '/login'); |
| 167 | + |
| 168 | + // Request password reset |
| 169 | + await page.goto(PLAYWRIGHT_BASE_URL + '/forgot-password'); |
| 170 | + await page.getByLabel('Email').fill(email); |
| 171 | + await page.getByRole('button', { name: 'Email Password Reset Link' }).click(); |
| 172 | + await expect(page.getByText('We have emailed your password reset link.')).toBeVisible(); |
| 173 | + |
| 174 | + // Get password reset URL from email |
| 175 | + const resetUrl = await getPasswordResetUrl(request, email); |
| 176 | + |
| 177 | + // Navigate to reset page |
| 178 | + await page.goto(resetUrl); |
| 179 | + |
| 180 | + // Fill in short password |
| 181 | + await page.getByLabel('Password', { exact: true }).fill('short'); |
| 182 | + await page.getByLabel('Confirm Password').fill('short'); |
| 183 | + await page.getByRole('button', { name: 'Reset Password' }).click(); |
| 184 | + |
| 185 | + // Should show validation error about minimum length |
| 186 | + await expect(page.getByText('must be at least')).toBeVisible(); |
| 187 | +}); |
| 188 | + |
| 189 | +test('shows error for invalid login credentials', async ({ page }) => { |
| 190 | + await page.goto(PLAYWRIGHT_BASE_URL + '/login'); |
| 191 | + await page.getByLabel('Email').fill('nonexistent@example.com'); |
| 192 | + await page.getByLabel('Password').fill('wrongpassword123'); |
| 193 | + await page.getByRole('button', { name: 'Log in' }).click(); |
| 194 | + |
| 195 | + await expect( |
| 196 | + page.getByText('These credentials do not match our records.') |
| 197 | + ).toBeVisible(); |
| 198 | +}); |
| 199 | + |
| 200 | +test('shows error when registering with existing email', async ({ page }) => { |
| 201 | + const email = `john+${Math.round(Math.random() * 10000)}@doe.com`; |
| 202 | + const password = 'suchagreatpassword123'; |
| 203 | + |
| 204 | + // Register first user |
| 205 | + await registerNewUser(page, email, password); |
| 206 | + |
| 207 | + // Log out |
| 208 | + await page.getByTestId('current_user_button').click(); |
| 209 | + await page.getByText('Log Out').click(); |
| 210 | + await page.waitForURL(PLAYWRIGHT_BASE_URL + '/login'); |
| 211 | + |
| 212 | + // Try to register with the same email |
| 213 | + await page.goto(PLAYWRIGHT_BASE_URL + '/register'); |
| 214 | + await page.getByLabel('Name').fill('Another User'); |
| 215 | + await page.getByLabel('Email').fill(email); |
| 216 | + await page.getByLabel('Password', { exact: true }).fill(password); |
| 217 | + await page.getByLabel('Confirm Password').fill(password); |
| 218 | + await page.getByLabel('I agree to the Terms of').click(); |
| 219 | + await page.getByRole('button', { name: 'Register' }).click(); |
| 220 | + |
| 221 | + // Should show error about email already taken |
| 222 | + await expect(page.getByText('The resource already exists.')).toBeVisible(); |
| 223 | +}); |
| 224 | + |
| 225 | +test('shows validation error for weak password on registration', async ({ page }) => { |
| 226 | + await page.goto(PLAYWRIGHT_BASE_URL + '/register'); |
| 227 | + await page.getByLabel('Name').fill('Weak Password User'); |
| 228 | + await page.getByLabel('Email').fill(`weak+${Math.round(Math.random() * 10000)}@test.com`); |
| 229 | + await page.getByLabel('Password', { exact: true }).fill('short'); |
| 230 | + await page.getByLabel('Confirm Password').fill('short'); |
| 231 | + await page.getByLabel('I agree to the Terms of').click(); |
| 232 | + await page.getByRole('button', { name: 'Register' }).click(); |
| 233 | + |
| 234 | + await expect(page.getByText('must be at least')).toBeVisible(); |
| 235 | +}); |
0 commit comments