fix: Corriger les tests E2E de blocage utilisateur qui échouent de manière intermittente

Le test user-blocking-session échouait sur Firefox car Playwright
détruit le navigateur au timeout, puis le bloc finally tentait de
fermer des contextes déjà détruits. Les appels browserContext.close()
sont désormais protégés par .catch().

Le test user-blocking ne réinitialisait pas l'état du compte cible
entre les exécutions, ce qui faisait échouer la recherche du bouton
"Bloquer" si l'utilisateur était resté suspendu d'une exécution
précédente.
This commit is contained in:
2026-02-20 19:37:16 +01:00
parent 6fd084063f
commit e5203097ef
2 changed files with 32 additions and 7 deletions

View File

@@ -36,6 +36,18 @@ test.describe('User Blocking Mid-Session [P1]', () => {
);
// Ensure target user is unblocked before tests start
resetTargetUser();
});
/**
* Resets the target user to active state via SQL.
* Called at the start of tests that need the user unblocked,
* so that Playwright retries don't fail because a previous
* attempt already blocked the user server-side.
*/
function resetTargetUser() {
const projectRoot = join(__dirname, '../..');
const composeFile = join(projectRoot, 'compose.yaml');
try {
execSync(
`docker compose -f "${composeFile}" exec -T php php bin/console dbal:run-sql "UPDATE users SET statut = 'active', blocked_at = NULL, blocked_reason = NULL WHERE email = '${TARGET_EMAIL}'" 2>&1`,
@@ -44,7 +56,7 @@ test.describe('User Blocking Mid-Session [P1]', () => {
} catch {
// Ignore cleanup errors
}
});
}
async function loginAsAdmin(page: import('@playwright/test').Page) {
await page.goto(`${ALPHA_URL}/login`);
@@ -92,7 +104,7 @@ test.describe('User Blocking Mid-Session [P1]', () => {
await page.getByRole('button', { name: /confirmer le blocage/i }).click();
// Wait for the success message
await expect(page.locator('.alert-success')).toBeVisible({ timeout: 5000 });
await expect(page.locator('.alert-success')).toBeVisible({ timeout: 10000 });
}
async function unblockUserViaAdmin(page: import('@playwright/test').Page) {
@@ -112,13 +124,16 @@ test.describe('User Blocking Mid-Session [P1]', () => {
await expect(unblockButton).toBeVisible();
await unblockButton.click();
await expect(page.locator('.alert-success')).toBeVisible({ timeout: 5000 });
await expect(page.locator('.alert-success')).toBeVisible({ timeout: 10000 });
}
// ============================================================================
// AC1: Admin blocks a user mid-session
// ============================================================================
test('[P1] admin blocks user mid-session - blocked user next request results in redirect', async ({ browser }) => {
// Reset target user state so retries work (a previous attempt may have blocked the user server-side)
resetTargetUser();
// Use two separate browser contexts to simulate two concurrent sessions
const adminContext = await browser.newContext();
const targetContext = await browser.newContext();
@@ -142,8 +157,8 @@ test.describe('User Blocking Mid-Session [P1]', () => {
// The blocked user should be redirected to login (API returns 401/403)
await expect(targetPage).toHaveURL(/\/login/, { timeout: 10000 });
} finally {
await adminContext.close();
await targetContext.close();
await adminContext.close().catch(() => {});
await targetContext.close().catch(() => {});
}
});
@@ -179,7 +194,7 @@ test.describe('User Blocking Mid-Session [P1]', () => {
const updatedRow = adminPage.locator('tr', { has: adminPage.locator(`text=${TARGET_EMAIL}`) });
await expect(updatedRow.locator('.status-active')).toContainText('Actif');
} finally {
await adminContext.close();
await adminContext.close().catch(() => {});
}
// Now the user should be able to log in again (use a new context)
@@ -195,7 +210,7 @@ test.describe('User Blocking Mid-Session [P1]', () => {
// Should redirect to dashboard (successful login)
await expect(userPage).toHaveURL(/\/dashboard/, { timeout: 30000 });
} finally {
await userContext.close();
await userContext.close().catch(() => {});
}
});