Files
sign/packages/app-tests/e2e/fixtures/authentication.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-03-28 13:13:29 +08:00
import { type Page } from '@playwright/test';
2025-01-02 15:33:37 +11:00
import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
2024-03-28 13:13:29 +08:00
type LoginOptions = {
page: Page;
email?: string;
password?: string;
/**
* Where to navigate after login.
*/
redirectPath?: string;
};
2024-03-28 13:13:29 +08:00
export const apiSignin = async ({
page,
2025-03-24 15:55:08 +01:00
email = 'example@sign.bls.media',
2024-03-28 13:13:29 +08:00
password = 'password',
redirectPath = '/documents',
2024-03-28 13:13:29 +08:00
}: LoginOptions) => {
const { request } = page.context();
const csrfToken = await getCsrfToken(page);
2025-01-02 15:33:37 +11:00
await request.post(`${NEXT_PUBLIC_WEBAPP_URL()}/api/auth/email-password/authorize`, {
data: {
2024-03-28 13:13:29 +08:00
email,
password,
csrfToken,
},
});
2025-01-02 15:33:37 +11:00
await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}${redirectPath}`);
await page.waitForTimeout(500);
2024-03-28 13:13:29 +08:00
};
export const apiSignout = async ({ page }: { page: Page }) => {
const { request } = page.context();
2025-01-02 15:33:37 +11:00
await request.post(`${NEXT_PUBLIC_WEBAPP_URL()}/api/auth/signout`);
2024-03-28 13:13:29 +08:00
2025-01-02 15:33:37 +11:00
await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}/signin`);
2024-03-28 13:13:29 +08:00
};
const getCsrfToken = async (page: Page) => {
const { request } = page.context();
2025-01-02 15:33:37 +11:00
const response = await request.fetch(`${NEXT_PUBLIC_WEBAPP_URL()}/api/auth/csrf`, {
2024-03-28 13:13:29 +08:00
method: 'get',
});
const { csrfToken } = await response.json();
2025-01-02 15:33:37 +11:00
2024-03-28 13:13:29 +08:00
if (!csrfToken) {
throw new Error('Invalid session');
}
return csrfToken;
};