import { test, expect } from '@chromatic-com/playwright'; import { loginViaUI, loginViaAPI, navigateTo, CONFIG } from '../helpers'; import { TEST_USERS, ROUTES } from '../design-tokens'; test.describe('FONCTIONNEL — Authentification', () => { test('Login avec compte listener — redirige vers /dashboard', async ({ page }) => { await loginViaUI(page, TEST_USERS.listener.email, TEST_USERS.listener.password); await expect(page).toHaveURL(/\/(dashboard|feed|discover)/, { timeout: 15_000 }); }); test('Login avec compte creator — redirige vers /dashboard', async ({ page }) => { await loginViaUI(page, TEST_USERS.creator.email, TEST_USERS.creator.password); await expect(page).toHaveURL(/\/(dashboard|feed|discover)/, { timeout: 15_000 }); }); test('Login avec compte admin — redirige vers /dashboard', async ({ page }) => { await loginViaUI(page, TEST_USERS.admin.email, TEST_USERS.admin.password); await expect(page).toHaveURL(/\/(dashboard|feed|discover)/, { timeout: 15_000 }); }); test('Login avec identifiants invalides — affiche erreur', async ({ page }) => { await page.goto(CONFIG.baseURL + '/login', { waitUntil: 'domcontentloaded' }); await page.waitForLoadState('networkidle').catch(() => {}); await page.locator('main, [role="main"]').first().waitFor({ state: 'visible', timeout: 15_000 }).catch(() => {}); const emailInput = page.locator('input[type="email"]'); await emailInput.waitFor({ state: 'visible', timeout: 10_000 }); await emailInput.fill('wrong@wrong.com'); await page.locator('input[type="password"]').fill('WrongPassword123!'); await page.getByTestId('login-submit').click(); // Doit rester sur /login avec un message d'erreur await page.waitForTimeout(3_000); expect(page.url()).toContain('/login'); const body = await page.textContent('body'); expect(body).toMatch(/error|erreur|invalid|incorrect|identifiants/i); }); test('Page register se charge sans erreur', async ({ page }) => { await navigateTo(page, '/register'); const form = page.getByTestId('register-form').or(page.locator('form')); await expect(form.first()).toBeVisible({ timeout: 10_000 }); // Vérifier les champs requis await expect(page.locator('input[type="email"]')).toBeVisible(); await expect(page.locator('input[type="password"]').first()).toBeVisible(); }); test('Logout — redirige vers /login', async ({ page }) => { await loginViaAPI(page, TEST_USERS.listener.email, TEST_USERS.listener.password); // Chercher le bouton logout (dans la sidebar ou les settings) const logoutBtn = page.getByRole('button', { name: /logout|déconnexion|se déconnecter/i }).first() .or(page.locator('[data-testid="logout-button"]').first()) .or(page.locator('button[aria-label*="logout" i]').first()); if (await logoutBtn.isVisible({ timeout: 5_000 }).catch(() => false)) { await logoutBtn.click(); await page.waitForURL(/\/login/, { timeout: 15_000 }).catch(() => {}); } // Si le bouton n'est pas visible directement, ce n'est pas une erreur critique }); test('Routes protégées redirigent vers /login si non-authentifié', async ({ page }) => { for (const route of ['/dashboard', '/library', '/settings']) { await page.goto(CONFIG.baseURL + route, { waitUntil: 'domcontentloaded' }); await page.waitForLoadState('networkidle').catch(() => {}); await page.waitForTimeout(3_000); expect(page.url(), `${route} devrait rediriger vers /login`).toContain('/login'); } }); test('Forgot password page se charge', async ({ page }) => { await navigateTo(page, '/forgot-password'); await expect(page.locator('input[type="email"]')).toBeVisible({ timeout: 10_000 }); }); });