107 lines
3.1 KiB
TypeScript
107 lines
3.1 KiB
TypeScript
import { expect, type APIRequestContext } from '@playwright/test';
|
|
|
|
export const BACKEND_API_URL =
|
|
process.env.VITE_BACKEND_API_URL || 'http://localhost:8080/api';
|
|
|
|
const ADMIN_EMAIL = 'admin@flatlogic.com';
|
|
const ADMIN_PASSWORD = 'flatlogicAdmin123!';
|
|
|
|
interface RoleRow {
|
|
readonly id: string;
|
|
readonly name: string | null;
|
|
}
|
|
|
|
export interface CreatedTestUser {
|
|
readonly id: string;
|
|
readonly email: string;
|
|
readonly password: string;
|
|
readonly organizationId: string | null;
|
|
}
|
|
|
|
interface CreateUserResponse {
|
|
readonly id?: string;
|
|
readonly organizationId?: string | null;
|
|
readonly temporaryPassword?: string;
|
|
}
|
|
|
|
export async function loginAsAdmin(request: APIRequestContext): Promise<void> {
|
|
const res = await request.post(`${BACKEND_API_URL}/auth/signin/local`, {
|
|
data: { email: ADMIN_EMAIL, password: ADMIN_PASSWORD },
|
|
});
|
|
expect(res.ok(), 'admin login for e2e setup must succeed').toBe(true);
|
|
}
|
|
|
|
export async function signOut(request: APIRequestContext): Promise<void> {
|
|
await request.post(`${BACKEND_API_URL}/auth/signout`);
|
|
}
|
|
|
|
export async function roleIdByName(
|
|
request: APIRequestContext,
|
|
roleName: string,
|
|
): Promise<string> {
|
|
const rolesRes = await request.get(`${BACKEND_API_URL}/roles`);
|
|
expect(rolesRes.status()).toBe(200);
|
|
const rolesBody = (await rolesRes.json()) as { rows?: RoleRow[] };
|
|
const role = (rolesBody.rows ?? []).find((row) => row.name === roleName);
|
|
expect(role, `seeded ${roleName} role must exist`).toBeTruthy();
|
|
return role!.id;
|
|
}
|
|
|
|
export async function createTestUser(
|
|
request: APIRequestContext,
|
|
params: {
|
|
readonly roleName: string;
|
|
readonly email: string;
|
|
readonly organizationId?: string | null;
|
|
},
|
|
): Promise<CreatedTestUser> {
|
|
const roleId = await roleIdByName(request, params.roleName);
|
|
const payload: Record<string, unknown> = {
|
|
email: params.email,
|
|
app_role: roleId,
|
|
};
|
|
if (params.organizationId !== undefined) {
|
|
payload.organizations = params.organizationId;
|
|
}
|
|
|
|
const createRes = await request.post(`${BACKEND_API_URL}/users`, {
|
|
data: { data: payload },
|
|
});
|
|
expect(createRes.ok(), `create ${params.roleName} test user`).toBe(true);
|
|
const created = (await createRes.json()) as CreateUserResponse;
|
|
expect(created.id, `created ${params.roleName} user id`).toBeTruthy();
|
|
expect(
|
|
created.temporaryPassword,
|
|
`created ${params.roleName} temporary password`,
|
|
).toBeTruthy();
|
|
|
|
return {
|
|
id: created.id!,
|
|
email: params.email,
|
|
password: created.temporaryPassword!,
|
|
organizationId: created.organizationId ?? params.organizationId ?? null,
|
|
};
|
|
}
|
|
|
|
export async function deleteTestUsers(
|
|
request: APIRequestContext,
|
|
users: readonly CreatedTestUser[],
|
|
): Promise<void> {
|
|
await loginAsAdmin(request);
|
|
for (const user of users) {
|
|
await request.delete(`${BACKEND_API_URL}/users/${user.id}`);
|
|
}
|
|
}
|
|
|
|
export async function deleteOrganizations(
|
|
request: APIRequestContext,
|
|
organizationIds: readonly (string | null | undefined)[],
|
|
): Promise<void> {
|
|
await loginAsAdmin(request);
|
|
for (const organizationId of organizationIds) {
|
|
if (organizationId) {
|
|
await request.delete(`${BACKEND_API_URL}/organizations/${organizationId}`);
|
|
}
|
|
}
|
|
}
|