Files
cms-client/tests/e2e/helpers/auth.ts
2026-04-18 12:14:30 +08:00

58 lines
1.5 KiB
TypeScript

import type { Page } from '@playwright/test';
import type { ServiceUserUserInfoData } from '$lib/api';
import { mock } from './mock';
/**
* Inject session cookies + register the GET /user/info override. The full
* magic-link → token-exchange → cookie-set pipeline is NOT re-run; that path
* is exercised end-to-end exactly once in auth.spec.ts. Other tests skip
* straight to the authenticated state.
*
* Cookies omit `secure: true` (real cookies in src/lib/server/session.ts use
* it) — browsers drop Secure cookies on http://localhost. Intentional
* divergence; tests verify auth-gated behavior, not cookie flag plumbing.
*
* Must run BEFORE the first page.goto(). The `loggedInUser` fixture in
* fixtures.ts ensures this ordering.
*/
export async function loginAsMockUser(
page: Page,
opts: { user?: Partial<ServiceUserUserInfoData> } = {}
): Promise<ServiceUserUserInfoData> {
const user: ServiceUserUserInfoData = {
user_id: '1',
email: 'alice@test.local',
nickname: 'Alice',
username: 'alice',
permission_level: 10,
allow_public: true,
...opts.user
};
await page.context().addCookies([
{
name: 'access_token',
value: 'test-access',
domain: 'localhost',
path: '/app',
httpOnly: true,
sameSite: 'Lax'
},
{
name: 'refresh_token',
value: 'test-refresh',
domain: 'localhost',
path: '/app',
httpOnly: true,
sameSite: 'Lax'
}
]);
await mock.override('GET', '/user/info', {
status: 200,
body: { status: 200, data: user }
});
return user;
}