From 78bdfad8b9b5371d3c641f999589204b4a213a7f Mon Sep 17 00:00:00 2001 From: April Hall Date: Wed, 26 Feb 2025 11:29:37 -0500 Subject: [PATCH] feat: E2E Tests for change password --- playwright.config.ts | 4 +- tests/updatePassword.spec.ts | 83 ++++++++++++++++++++++++++++++++++++ tests/updateUsername.spec.ts | 8 ++-- 3 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 tests/updatePassword.spec.ts diff --git a/playwright.config.ts b/playwright.config.ts index 2bb2182..4949b1e 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -13,11 +13,10 @@ export default defineConfig({ // since they need user accounts to run { - name: 'tests', + name: 'main', use: { ...devices['Desktop Chrome'] }, dependencies: ['signup'], testMatch: /(.+\.)?(test|spec)\.[jt]s/, - fullyParallel: true, }, ], reporter: 'list', @@ -26,6 +25,7 @@ export default defineConfig({ port: 5173, reuseExistingServer: true, }, + workers: 1, use: { baseURL: 'http://localhost:5173', trace: 'on-first-retry', diff --git a/tests/updatePassword.spec.ts b/tests/updatePassword.spec.ts new file mode 100644 index 0000000..d601198 --- /dev/null +++ b/tests/updatePassword.spec.ts @@ -0,0 +1,83 @@ +import { test, expect, type Page, type Locator } from '@playwright/test'; +import { login } from './utils'; + +async function expectError(message: string, page: Page) { + const errorMessageLocator = page.locator(`.text-sm.text-red-500:has-text("${message}")`); + await expect(errorMessageLocator).toBeVisible(); +} + +test.describe('Password Update Form', () => { + let page: Page; + let currentPasswordInput: Locator; + let newPasswordInput: Locator; + let submitButton: Locator; + const currentPassword = 'Password1234!'; + + test.beforeEach(async ({ browser }) => { + page = await browser.newPage(); + + // Login and navigate + await login(page); + await page.goto('/account', { timeout: 30000, waitUntil: 'domcontentloaded' }); + + // Initialize locators + currentPasswordInput = page.locator('input#currentPassword'); + newPasswordInput = page.locator('input#newPassword'); + submitButton = page.getByRole('button', { name: 'Update Password' }); + }); + + // Test passwords can't be the same + test('show not allow same password', async () => { + await currentPasswordInput.fill(currentPassword); + await newPasswordInput.fill(currentPassword); + await submitButton.click(); + + // Check for error message + await expectError('New password cannot be the same as old password.', page); + }); + + // Test invalid current password + test('should show error for invalid current password', async () => { + await currentPasswordInput.fill('wrongPassword'); + await newPasswordInput.fill('newPassword123!'); + await submitButton.click(); + + // Check for error message + await expectError('Invalid password', page); + }); + + // Test validation error for weak new password + test('should show validation error for weak new password', async () => { + await currentPasswordInput.fill(currentPassword); + await newPasswordInput.fill('weak'); + await submitButton.click(); + + // Check for error message + await expectError('New password must be at least 8 characters.', page); + }); + + // Test empty fields validation + test('should show validation errors when fields are empty', async () => { + // Leave fields empty and try to submit + await submitButton.click(); + + // Check for error messages on both fields + await expectError('Password must not be empty.', page); + }); + + // Test update functionalityz + test('should successfully update usser password', async () => { + await currentPasswordInput.fill(currentPassword); + await newPasswordInput.fill('newPassword123!'); + await submitButton.click(); + + // Undo password change so other tests still pass + await currentPasswordInput.fill('newPassword123!'); + await newPasswordInput.fill(currentPassword); + await submitButton.click(); + + // Look for the success message + const successMessageLocator = page.locator('p.text-sm.text-green-500:has-text("Password updated.")'); + await expect(successMessageLocator).toBeVisible(); + }); +}); diff --git a/tests/updateUsername.spec.ts b/tests/updateUsername.spec.ts index 3424101..91018c0 100644 --- a/tests/updateUsername.spec.ts +++ b/tests/updateUsername.spec.ts @@ -10,13 +10,15 @@ test.describe('Username Update Form', () => { test.beforeEach(async ({ browser }) => { page = await browser.newPage(); + + // Login and navigate + await login(page); + await page.goto('/account', { timeout: 30000, waitUntil: 'domcontentloaded' }); + // Initialize locators usernameInput = page.locator('input#username'); submitButton = page.getByRole('button', { name: 'Update Username' }); currentUsernameElement = page.locator('#currentuser-username'); - - await login(page); - page.goto('/account'); }); // Test that the username will change