Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add login save prompt e2e test (#1707)
Leendert de Borst6823c2053325
diff --git a/apps/browser-extension/tests/e2e/09-save-login-prompt.spec.ts b/apps/browser-extension/tests/e2e/09-save-login-prompt.spec.tsnew file mode 100644index 000000000..a75bc59f1--- /dev/null+++ b/apps/browser-extension/tests/e2e/09-save-login-prompt.spec.ts@@ -0,0 +1,406 @@+/**+ * Category 9: Save Login Prompt (Requires API + Authentication)+ *+ * These tests verify the save login prompt functionality that appears when+ * users submit login forms on arbitrary websites.+ *+ * Test scenarios:+ * 1. Prompt appears after submitting login form+ * 2. Clicking "Save" adds the credential to the vault+ * 3. Clicking dismiss (X) does not add the credential+ * 4. Clicking "Never for this site" blocks future prompts for that domain+ */+import fs from 'fs';+import path from 'path';+import { fileURLToPath } from 'url';++import type { Page } from '@playwright/test';++import { test, expect, TestClient } from '../fixtures';++const __filename = fileURLToPath(import.meta.url);+const __dirname = path.dirname(__filename);++/**+ * Path to the test login page HTML fixture+ */+const TEST_PAGE_PATH = path.join(__dirname, '..', 'fixtures', 'test-pages', 'login.html');+const TEST_PAGE_HTML = fs.readFileSync(TEST_PAGE_PATH, 'utf-8');++/**+ * Helper to setup route interception to serve our test login page for a given domain.+ */+async function setupTestPageRoute(page: Page, domain: string): Promise<void> {+ await page.route(`https://${domain}/**`, (route) => {+ route.fulfill({+ status: 200,+ contentType: 'text/html',+ body: TEST_PAGE_HTML,+ });+ });+}++/**+ * Helper to check if the save prompt is visible in the shadow DOM.+ */+async function isSavePromptVisible(page: Page): Promise<boolean> {+ return page.evaluate(() => {+ // Check both possible shadow root containers+ const savePromptUI = document.querySelector('aliasvault-save-prompt');+ if (savePromptUI && savePromptUI.shadowRoot) {+ const prompt = savePromptUI.shadowRoot.querySelector('.av-save-prompt--visible');+ if (prompt) {+ return true;+ }+ }++ const mainUI = document.querySelector('aliasvault-ui');+ if (mainUI && mainUI.shadowRoot) {+ const prompt = mainUI.shadowRoot.querySelector('.av-save-prompt--visible');+ if (prompt) {+ return true;+ }+ }++ return false;+ });+}++/**+ * Helper to wait for save prompt to appear.+ */+async function waitForSavePrompt(page: Page, timeout: number = 10000): Promise<void> {+ const startTime = Date.now();+ while (Date.now() - startTime < timeout) {+ if (await isSavePromptVisible(page)) {+ // Give it a moment to fully render+ await page.waitForTimeout(100);+ return;+ }+ await page.waitForTimeout(200);+ }++ throw new Error(`Save prompt did not appear within ${timeout}ms`);+}++/**+ * Helper to click a button in the save prompt.+ */+async function clickSavePromptButton(page: Page, buttonClass: string): Promise<void> {+ await page.evaluate((btnClass) => {+ // Check both possible shadow root containers+ const containers = [+ document.querySelector('aliasvault-save-prompt'),+ document.querySelector('aliasvault-ui'),+ ];++ for (const container of containers) {+ if (container && container.shadowRoot) {+ const button = container.shadowRoot.querySelector(btnClass) as HTMLButtonElement;+ if (button) {+ button.click();+ return;+ }+ }+ }+ throw new Error(`Button ${btnClass} not found in save prompt`);+ }, buttonClass);+}++/**+ * Helper to fill the service name input in the save prompt.+ */+async function fillServiceName(page: Page, serviceName: string): Promise<void> {+ await page.evaluate((name) => {+ const containers = [+ document.querySelector('aliasvault-save-prompt'),+ document.querySelector('aliasvault-ui'),+ ];++ for (const container of containers) {+ if (container && container.shadowRoot) {+ const input = container.shadowRoot.querySelector('.av-save-prompt__service-input') as HTMLInputElement;+ if (input) {+ // Clear and set value, then dispatch events to ensure React/JS picks up the change+ input.focus();+ input.value = name;+ input.dispatchEvent(new Event('input', { bubbles: true }));+ input.dispatchEvent(new Event('change', { bubbles: true }));+ return;+ }+ }+ }+ throw new Error('Service name input not found in save prompt');+ }, serviceName);+}++/**+ * Helper to submit the login form on the test page.+ * Waits for the content script to initialize before filling and submitting.+ */+async function submitLoginForm(page: Page, username: string, password: string): Promise<void> {+ // Wait for the content script to initialize (it has a 750ms delay + some buffer)+ await page.waitForTimeout(1000);++ await page.fill('input#username', username);+ await page.fill('input#password', password);++ // Click outside the form fields to dismiss any autofill popup that might be blocking the submit button+ // We click on the form title which is safely above the input fields+ await page.click('h1');+ await page.waitForTimeout(200);++ // Click the submit button - this triggers the LoginDetector's button click handler+ await page.click('button[type="submit"]');++ // Wait a bit for the login detector's debounce (100ms) and processing+ await page.waitForTimeout(300);+}++/**+ * Helper to enable the login save feature.+ */+async function enableLoginSaveFeature(client: TestClient): Promise<void> {+ await client.popup.evaluate(() => {+ return new Promise<void>((resolve) => {+ chrome.storage.local.set({ loginSaveEnabled: true }, () => {+ resolve();+ });+ });+ });+}++/**+ * Helper to clear blocked domains list.+ */+async function clearBlockedDomains(client: TestClient): Promise<void> {+ await client.popup.evaluate(() => {+ return new Promise<void>((resolve) => {+ chrome.storage.local.remove('loginSaveBlockedDomains', () => {+ resolve();+ });+ });+ });+}++/**+ * Helper to get the blocked domains list.+ */+async function getBlockedDomains(client: TestClient): Promise<string[]> {+ return client.popup.evaluate(() => {+ return new Promise<string[]>((resolve) => {+ chrome.storage.local.get('loginSaveBlockedDomains', (result) => {+ resolve(result.loginSaveBlockedDomains || []);+ });+ });+ });+}++test.describe.serial('9. Save Login Prompt', () => {+ let client: TestClient;++ test.afterAll(async () => {+ await client?.cleanup();+ });++ test('9.1 should login, enable save feature, and verify prompt appears on form submit', async ({ testUser, apiUrl }) => {+ client = await TestClient.create();+ await client.login(apiUrl, testUser.username, testUser.password);++ // Enable E2E test mode for open shadow DOM+ await client.enableE2ETestMode();++ // Enable the login save feature+ await enableLoginSaveFeature(client);++ // Clear any blocked domains from previous test runs+ await clearBlockedDomains(client);++ // Create a test page on a domain+ const testPage = await client.context.newPage();+ await setupTestPageRoute(testPage, 'save-test-domain.com');+ await testPage.goto('https://save-test-domain.com/login');+ await testPage.waitForLoadState('networkidle');++ // Submit the login form+ await submitLoginForm(testPage, '[email protected]', 'TestPassword123!');++ // Wait for the save prompt to appear+ await waitForSavePrompt(testPage);++ // Verify the prompt is visible+ const promptVisible = await isSavePromptVisible(testPage);+ expect(promptVisible).toBe(true);++ await testPage.screenshot({ path: 'tests/screenshots/9.1-save-prompt-visible.png' });++ // Dismiss the prompt for now (we'll test save functionality in next test)+ await clickSavePromptButton(testPage, '.av-save-prompt__btn--dismiss');++ await testPage.close();+ });++ test('9.2 should save credential to vault when clicking Save', async () => {+ // Create a test page on a new domain+ const testPage = await client.context.newPage();+ await setupTestPageRoute(testPage, 'save-credential-test.com');+ await testPage.goto('https://save-credential-test.com/login');+ await testPage.waitForLoadState('networkidle');++ // Submit the login form+ const testUsername = '[email protected]';+ const testPassword = 'SaveTestPass123!';+ await submitLoginForm(testPage, testUsername, testPassword);++ // Wait for the save prompt to appear+ await waitForSavePrompt(testPage);++ // Verify prompt is actually visible before proceeding+ const promptVisibleBeforeSave = await isSavePromptVisible(testPage);+ expect(promptVisibleBeforeSave).toBe(true);++ await testPage.screenshot({ path: 'tests/screenshots/9.2-prompt-visible.png' });++ // Fill in a custom service name+ const serviceName = 'Save Credential Test Site';+ await fillServiceName(testPage, serviceName);++ await testPage.screenshot({ path: 'tests/screenshots/9.2-before-save.png' });++ // Click the Save button+ await clickSavePromptButton(testPage, '.av-save-prompt__btn--save');++ // Wait for the credential to be saved and synced+ await testPage.waitForTimeout(2000);++ await testPage.screenshot({ path: 'tests/screenshots/9.2-after-save.png' });++ // Trigger a sync to ensure the vault is up to date+ await client.triggerSync();++ // Verify the credential was added to the vault+ await client.goToVault().then(c => c.waitForVaultReady());+ await client.verifyCredentialExists(serviceName);++ await client.screenshot('9.2-credential-saved-in-vault.png');++ await testPage.close();+ });++ test('9.3 should NOT save credential when clicking Dismiss', async () => {+ // Get the current vault item count+ await client.goToVault().then(c => c.waitForVaultReady());+ const initialItemCount = await client.popup.locator('ul#items-list > li').count();++ // Create a test page on a new domain+ const testPage = await client.context.newPage();+ await setupTestPageRoute(testPage, 'dismiss-test-domain.com');+ await testPage.goto('https://dismiss-test-domain.com/login');+ await testPage.waitForLoadState('networkidle');++ // Submit the login form+ await submitLoginForm(testPage, '[email protected]', 'DismissTestPass123!');++ // Wait for the save prompt to appear+ await waitForSavePrompt(testPage);++ await testPage.screenshot({ path: 'tests/screenshots/9.3-prompt-before-dismiss.png' });++ // Click the Dismiss button (X icon)+ await clickSavePromptButton(testPage, '.av-save-prompt__btn--dismiss');++ // Wait to ensure the dismiss action completes+ await testPage.waitForTimeout(500);++ // Verify the prompt is no longer visible+ const promptStillVisible = await isSavePromptVisible(testPage);+ expect(promptStillVisible).toBe(false);++ // Verify no new credential was added to the vault+ await client.goToVault().then(c => c.waitForVaultReady());+ const finalItemCount = await client.popup.locator('ul#items-list > li').count();+ expect(finalItemCount).toBe(initialItemCount);++ await client.screenshot('9.3-vault-unchanged-after-dismiss.png');++ await testPage.close();+ });++ test('9.4 should block future prompts when clicking Never for this site', async () => {+ // Create a test page on a specific domain+ const blockedDomain = 'never-save-domain.com';+ const testPage = await client.context.newPage();+ await setupTestPageRoute(testPage, blockedDomain);+ await testPage.goto(`https://${blockedDomain}/login`);+ await testPage.waitForLoadState('networkidle');++ // Submit the login form+ await submitLoginForm(testPage, '[email protected]', 'NeverSavePass123!');++ // Wait for the save prompt to appear+ await waitForSavePrompt(testPage);++ await testPage.screenshot({ path: 'tests/screenshots/9.4-prompt-before-never.png' });++ // Click the "Never for this site" button+ await clickSavePromptButton(testPage, '.av-save-prompt__btn--never');++ // Wait to ensure the action completes+ await testPage.waitForTimeout(500);++ // Verify the prompt is no longer visible+ const promptStillVisible = await isSavePromptVisible(testPage);+ expect(promptStillVisible).toBe(false);++ // Verify the domain was added to the blocked list+ const blockedDomains = await getBlockedDomains(client);+ expect(blockedDomains).toContain(blockedDomain);++ await testPage.screenshot({ path: 'tests/screenshots/9.4-prompt-dismissed.png' });++ // Now try submitting the form again on the same domain+ // First, refresh the page to get a clean state+ await testPage.goto(`https://${blockedDomain}/login`);+ await testPage.waitForLoadState('networkidle');++ // Submit the form again with different credentials+ await submitLoginForm(testPage, '[email protected]', 'AnotherPass123!');++ // Wait a bit to see if the prompt would appear+ await testPage.waitForTimeout(3000);++ // Verify the prompt does NOT appear this time+ const promptAppearedAgain = await isSavePromptVisible(testPage);+ expect(promptAppearedAgain).toBe(false);++ await testPage.screenshot({ path: 'tests/screenshots/9.4-no-prompt-on-blocked-domain.png' });++ await testPage.close();+ });++ test('9.5 should show prompt on new domain even after blocking another', async () => {+ // Verify that blocking one domain doesn't affect other domains+ const newDomain = 'new-unblocked-domain.com';+ const testPage = await client.context.newPage();+ await setupTestPageRoute(testPage, newDomain);+ await testPage.goto(`https://${newDomain}/login`);+ await testPage.waitForLoadState('networkidle');++ // Submit the login form+ await submitLoginForm(testPage, '[email protected]', 'NewDomainPass123!');++ // Wait for the save prompt to appear+ await waitForSavePrompt(testPage);++ // Verify the prompt is visible (this domain is not blocked)+ const promptVisible = await isSavePromptVisible(testPage);+ expect(promptVisible).toBe(true);++ await testPage.screenshot({ path: 'tests/screenshots/9.5-prompt-on-unblocked-domain.png' });++ // Clean up - dismiss the prompt+ await clickSavePromptButton(testPage, '.av-save-prompt__btn--dismiss');++ await testPage.close();+ });+});diff --git a/apps/browser-extension/tests/fixtures/test-pages/login.html b/apps/browser-extension/tests/fixtures/test-pages/login.htmlindex 8fa2b6e9f..521f3dc0b 100644--- a/apps/browser-extension/tests/fixtures/test-pages/login.html+++ b/apps/browser-extension/tests/fixtures/test-pages/login.html@@ -4,6 +4,7 @@<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>E2E Test Form</title>+ <link rel="icon" type="image/x-icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🔐</text></svg>"><style>body {font-family: Arial, sans-serif;@@ -35,6 +36,7 @@color: #555;}input[type="text"],+ input[type="email"],input[type="password"] {width: 100%;padding: 0.75rem;@@ -60,20 +62,90 @@button[type="submit"]:hover {background-color: #0056b3;}+ button[type="submit"]:disabled {+ background-color: #ccc;+ cursor: not-allowed;+ }+ .status-message {+ margin-top: 1rem;+ padding: 0.75rem;+ border-radius: 4px;+ text-align: center;+ display: none;+ }+ .status-message.success {+ display: block;+ background-color: #d4edda;+ color: #155724;+ border: 1px solid #c3e6cb;+ }+ .status-message.error {+ display: block;+ background-color: #f8d7da;+ color: #721c24;+ border: 1px solid #f5c6cb;+ }+ .status-message.loading {+ display: block;+ background-color: #cce5ff;+ color: #004085;+ border: 1px solid #b8daff;+ }</style></head><body>- <form class="login-form" action="#" method="post">+ <form class="login-form" id="loginForm" method="post"><h1>Login</h1><div class="form-group">- <label for="username">Username</label>- <input type="text" id="username" name="username" autocomplete="username" placeholder="Enter username">+ <label for="username">Username or Email</label>+ <input type="text" id="username" name="username" autocomplete="username" placeholder="Enter username or email" required></div><div class="form-group"><label for="password">Password</label>- <input type="password" id="password" name="password" autocomplete="current-password" placeholder="Enter password">+ <input type="password" id="password" name="password" autocomplete="current-password" placeholder="Enter password" required></div>- <button type="submit">Sign In</button>+ <button type="submit" id="submitBtn">Sign In</button>+ <div id="statusMessage" class="status-message"></div></form>++ <script>+ /**+ * Simulates a realistic AJAX-based login flow.+ * This mimics how modern web applications handle login submissions.+ */+ document.getElementById('loginForm').addEventListener('submit', function(event) {+ // Prevent the default form submission (like most modern sites do)+ event.preventDefault();++ const username = document.getElementById('username').value;+ const password = document.getElementById('password').value;+ const submitBtn = document.getElementById('submitBtn');+ const statusMessage = document.getElementById('statusMessage');++ // Basic validation+ if (!username || !password) {+ statusMessage.textContent = 'Please fill in all fields';+ statusMessage.className = 'status-message error';+ return;+ }++ // Show loading state (simulating network request)+ submitBtn.disabled = true;+ submitBtn.textContent = 'Signing in...';+ statusMessage.textContent = 'Authenticating...';+ statusMessage.className = 'status-message loading';++ // Simulate async login request (like a real AJAX call)+ setTimeout(function() {+ // Simulate successful login+ submitBtn.textContent = 'Signed In';+ statusMessage.textContent = 'Login successful! Redirecting...';+ statusMessage.className = 'status-message success';++ // In a real app, this would redirect or update the UI+ // For testing, we just show success state+ }, 500);+ });+ </script></body></html>