Files
bakery-ia/frontend/tests/auth/login.spec.ts

122 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-11-14 07:46:29 +01:00
import { test, expect } from '@playwright/test';
import { login, logout, TEST_USER } from '../helpers/auth';
2025-11-15 21:21:06 +01:00
import { acceptCookieConsent } from '../helpers/utils';
2025-11-14 07:46:29 +01:00
test.describe('Login Flow', () => {
test.beforeEach(async ({ page }) => {
// Start at login page
await page.goto('/login');
2025-11-15 21:21:06 +01:00
// Accept cookie consent if present
await acceptCookieConsent(page);
2025-11-14 07:46:29 +01:00
});
test('should display login form', async ({ page }) => {
2025-11-15 21:21:06 +01:00
// Verify login page elements are visible (support both English and Spanish)
await expect(page.getByLabel(/email|correo/i)).toBeVisible();
await expect(page.getByRole('textbox', { name: /password|contraseña/i })).toBeVisible();
await expect(page.getByRole('button', { name: /log in|sign in|login|acceder/i })).toBeVisible();
2025-11-14 07:46:29 +01:00
});
test('should successfully login with valid credentials', async ({ page }) => {
// Fill in credentials
2025-11-15 21:21:06 +01:00
await page.getByLabel(/email|correo/i).fill(TEST_USER.email);
await page.getByRole('textbox', { name: /password|contraseña/i }).fill(TEST_USER.password);
2025-11-14 07:46:29 +01:00
// Click login button
2025-11-15 21:21:06 +01:00
await page.getByRole('button', { name: /log in|sign in|login|acceder/i }).click();
2025-11-14 07:46:29 +01:00
// Should redirect to dashboard or app
await expect(page).toHaveURL(/\/(app|dashboard)/, { timeout: 10000 });
// Verify we see dashboard content
await expect(page.locator('body')).toContainText(/dashboard|panel de control/i);
});
test('should show error with invalid email', async ({ page }) => {
// Fill in invalid credentials
2025-11-15 21:21:06 +01:00
await page.getByLabel(/email|correo/i).fill('invalid@email.com');
await page.getByRole('textbox', { name: /password|contraseña/i }).fill('wrongpassword');
2025-11-14 07:46:29 +01:00
// Click login button
2025-11-15 21:21:06 +01:00
await page.getByRole('button', { name: /log in|sign in|login|acceder/i }).click();
2025-11-14 07:46:29 +01:00
// Should show error message
await expect(page.locator('body')).toContainText(/invalid|incorrect|error|credenciales/i, {
timeout: 5000,
});
// Should stay on login page
await expect(page).toHaveURL(/\/login/);
});
test('should show validation error for empty email', async ({ page }) => {
// Try to submit without email
2025-11-15 21:21:06 +01:00
await page.getByRole('textbox', { name: /password|contraseña/i }).fill('somepassword');
await page.getByRole('button', { name: /log in|sign in|login|acceder/i }).click();
2025-11-14 07:46:29 +01:00
// Should show validation error (either inline or toast)
const bodyText = await page.locator('body').textContent();
expect(bodyText).toMatch(/required|obligatorio|necesario/i);
});
test('should show validation error for empty password', async ({ page }) => {
// Try to submit without password
2025-11-15 21:21:06 +01:00
await page.getByLabel(/email|correo/i).fill('test@example.com');
await page.getByRole('button', { name: /log in|sign in|login|acceder/i }).click();
2025-11-14 07:46:29 +01:00
// Should show validation error
const bodyText = await page.locator('body').textContent();
expect(bodyText).toMatch(/required|obligatorio|necesario/i);
});
test('should toggle password visibility', async ({ page }) => {
2025-11-15 21:21:06 +01:00
const passwordInput = page.getByRole('textbox', { name: /password|contraseña/i });
2025-11-14 07:46:29 +01:00
// Initially should be password type
await expect(passwordInput).toHaveAttribute('type', 'password');
// Look for toggle button (eye icon, "show password", etc.)
2025-11-15 21:21:06 +01:00
const toggleButton = page.getByRole('button', { name: /show|mostrar.*password|contraseña/i });
2025-11-14 07:46:29 +01:00
2025-11-15 21:21:06 +01:00
const isToggleVisible = await toggleButton.isVisible({ timeout: 2000 }).catch(() => false);
if (isToggleVisible) {
2025-11-14 07:46:29 +01:00
await toggleButton.click();
// Should change to text type
await expect(passwordInput).toHaveAttribute('type', 'text');
// Toggle back
await toggleButton.click();
await expect(passwordInput).toHaveAttribute('type', 'password');
}
});
test('should have link to registration page', async ({ page }) => {
2025-11-15 21:21:06 +01:00
// Look for register/signup button or link
const registerButton = page.getByRole('button', { name: /register|sign up|crear cuenta|registrar/i });
const registerLink = page.getByRole('link', { name: /register|sign up|crear cuenta|registrar/i });
const isButtonVisible = await registerButton.isVisible({ timeout: 2000 }).catch(() => false);
const isLinkVisible = await registerLink.isVisible({ timeout: 2000 }).catch(() => false);
2025-11-14 07:46:29 +01:00
2025-11-15 21:21:06 +01:00
if (isLinkVisible) {
2025-11-14 07:46:29 +01:00
await expect(registerLink).toHaveAttribute('href', /\/register/);
2025-11-15 21:21:06 +01:00
} else if (isButtonVisible) {
// If it's a button, just verify it exists
await expect(registerButton).toBeVisible();
2025-11-14 07:46:29 +01:00
}
});
test('should redirect to app if already logged in', async ({ page, context }) => {
// First login
await login(page, TEST_USER);
// Try to go to login page again
await page.goto('/login');
// Should redirect to app/dashboard
await expect(page).toHaveURL(/\/(app|dashboard)/, { timeout: 5000 });
});
});