2024-05-03 19:38:09 +00:00
|
|
|
import { APIRequestContext, expect } from '@playwright/test';
|
|
|
|
|
|
|
|
const ensureLeadingSlash = (endpoint: string) =>
|
|
|
|
endpoint[0] === '/' ? endpoint : '/' + endpoint;
|
|
|
|
|
2024-05-21 15:37:35 +00:00
|
|
|
export const authedRequest = async ({
|
|
|
|
request,
|
|
|
|
method,
|
|
|
|
endpoint,
|
|
|
|
data
|
|
|
|
}: {
|
|
|
|
request: APIRequestContext;
|
|
|
|
method: 'post' | 'put';
|
|
|
|
endpoint: string;
|
|
|
|
data: Record<string, unknown>;
|
|
|
|
}) => {
|
2024-05-03 19:38:09 +00:00
|
|
|
const csrfToken = (await request.storageState()).cookies.find(
|
|
|
|
c => c.name === 'csrf_token'
|
|
|
|
)?.value;
|
|
|
|
|
|
|
|
expect(csrfToken).toBeTruthy();
|
|
|
|
|
2024-05-21 15:37:35 +00:00
|
|
|
const res = await request[method](
|
2024-05-03 19:38:09 +00:00
|
|
|
process.env.API_LOCATION + ensureLeadingSlash(endpoint),
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
headers: { 'csrf-token': csrfToken! }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
expect(res.status()).toBe(200);
|
|
|
|
return res;
|
|
|
|
};
|