Files
fiberops-web/e2e/helpers/auth.ts

44 lines
1.6 KiB
TypeScript

import { Page, request as playwrightRequest } from '@playwright/test';
export const DEMO = {
tenant: 'demo-isp',
email: 'admin@demo-isp.com',
password: 'Admin123!',
};
// Use the public API URL — Node.js requests bypass browser CORS
const API_URL = 'https://fiberops-api.juankibin.space';
export async function loginViaAPI(creds = DEMO) {
const ctx = await playwrightRequest.newContext({ baseURL: API_URL });
const resp = await ctx.post('/api/v1/auth/login', {
data: { tenantSlug: creds.tenant, email: creds.email, password: creds.password },
headers: { 'Content-Type': 'application/json', 'x-tenant-slug': creds.tenant },
});
const data = await resp.json();
await ctx.dispose();
return data; // { accessToken, refreshToken, user }
}
export async function login(page: Page, creds = DEMO) {
// Step 1: Get token via Node.js HTTP — bypasses browser CORS entirely
const { accessToken, user } = await loginViaAPI(creds);
// Step 2: Inject zustand persist state into localStorage before navigation
await page.goto('/login');
await page.evaluate(
({ token, tenant, u }) => {
// Zustand persist format: { state: {...}, version: 0 }
localStorage.setItem('fiberops_auth', JSON.stringify({
state: { accessToken: token, tenantSlug: tenant, user: u },
version: 0,
}));
},
{ token: accessToken, tenant: creds.tenant, u: user }
);
// Step 3: Navigate to dashboard — auth guard reads localStorage and passes
await page.goto('/dashboard');
await page.waitForURL(/\/(dashboard|clients|settings)/, { timeout: 15000 });
}