oggui/ogWebconsole/e2e/helpers/auth.ts

40 lines
1.6 KiB
TypeScript

import { Page, expect } from '@playwright/test';
import { APP_URL, TEST_PASS, TEST_USERS } from './api';
export type Role = keyof typeof TEST_USERS;
/**
* Hace login en la GUI con el usuario de test del rol indicado.
* Espera a que la redirección post-login termine (URL cambia a /groups).
*/
export async function loginAs(page: Page, role: Role): Promise<void> {
const { username } = TEST_USERS[role];
await page.goto(`${APP_URL}/auth/login`);
await page.locator('input[name="username"]').fill(username);
await page.locator('input[name="password"]').fill(TEST_PASS);
await page.locator('button[type="submit"]').click();
await expect(page).toHaveURL(/\/groups/, { timeout: 10_000 });
}
/**
* Navega a una ruta y devuelve la URL final (útil para comprobar redirecciones).
*/
export async function navigateTo(page: Page, path: string): Promise<string> {
await page.goto(`${APP_URL}${path}`);
await page.waitForLoadState('networkidle');
return page.url();
}
/** Comprueba que un enlace del sidebar ES visible */
export async function expectSidebarLink(page: Page, routerLink: string, visible: true): Promise<void>;
/** Comprueba que un enlace del sidebar NO está en el DOM */
export async function expectSidebarLink(page: Page, routerLink: string, visible: false): Promise<void>;
export async function expectSidebarLink(page: Page, routerLink: string, visible: boolean): Promise<void> {
const locator = page.locator(`[routerLink="${routerLink}"], a[href="${routerLink}"]`).first();
if (visible) {
await expect(locator).toBeVisible({ timeout: 5_000 });
} else {
await expect(locator).not.toBeAttached({ timeout: 5_000 });
}
}