52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import type { INestApplication } from '@nestjs/common';
|
|
import request from 'supertest';
|
|
import {
|
|
E2E_ROLE_LIST,
|
|
type E2ERole,
|
|
E2E_SEED_CREDENTIALS,
|
|
} from '../fixtures/e2e-roles.js';
|
|
import type { E2ESeedFixtures } from './e2e-fixtures.helper.js';
|
|
import { expectSuccessEnvelope } from './e2e-http.helper.js';
|
|
|
|
export type E2EAccessTokenMap = Record<E2ERole, string>;
|
|
|
|
function resolveRoleHospitalId(role: E2ERole, fixtures: E2ESeedFixtures) {
|
|
if (role === 'SYSTEM_ADMIN') {
|
|
return undefined;
|
|
}
|
|
|
|
return fixtures.hospitalAId;
|
|
}
|
|
|
|
export async function loginAsRole(
|
|
app: INestApplication,
|
|
role: E2ERole,
|
|
fixtures: E2ESeedFixtures,
|
|
): Promise<string> {
|
|
const credential = E2E_SEED_CREDENTIALS[role];
|
|
const response = await request(app.getHttpServer())
|
|
.post('/auth/login')
|
|
.send({
|
|
phone: credential.phone,
|
|
password: credential.password,
|
|
role: credential.role,
|
|
hospitalId: resolveRoleHospitalId(role, fixtures),
|
|
});
|
|
|
|
expectSuccessEnvelope(response, 201);
|
|
return response.body.data.accessToken as string;
|
|
}
|
|
|
|
export async function loginAllRoles(
|
|
app: INestApplication,
|
|
fixtures: E2ESeedFixtures,
|
|
): Promise<E2EAccessTokenMap> {
|
|
const tokenEntries = await Promise.all(
|
|
E2E_ROLE_LIST.map(
|
|
async (role) => [role, await loginAsRole(app, role, fixtures)] as const,
|
|
),
|
|
);
|
|
|
|
return Object.fromEntries(tokenEntries) as E2EAccessTokenMap;
|
|
}
|