HH/e2e/helpers/api-helper.ts
2026-04-08 17:13:35 +03:00

83 lines
2.5 KiB
TypeScript

import { Page, expect, request } from '@playwright/test';
import { RoleName, ROLES } from './helpers';
export class ApiHelper {
private accessToken = '';
constructor(private page: Page) {}
async authenticate(role: RoleName = 'hospital_admin') {
const config = ROLES[role];
const response = await this.page.request.post('/accounts/token/', {
data: {
email: config.email,
password: config.password,
},
});
expect(response.status()).toBe(200);
const body = await response.json();
this.accessToken = body.access;
return this.accessToken;
}
get token() {
return this.accessToken;
}
private headers() {
return { Authorization: `Bearer ${this.accessToken}` };
}
async get(path: string) {
return this.page.request.get(path, { headers: this.headers() });
}
async post(path: string, data: any) {
return this.page.request.post(path, { data, headers: this.headers() });
}
async patch(path: string, data: any) {
return this.page.request.patch(path, { data, headers: this.headers() });
}
async delete(path: string) {
return this.page.request.delete(path, { headers: this.headers() });
}
async createComplaint(data?: Partial<any>) {
const hospital_id = '7e4d08ce-2e0a-4184-8297-7acd418184a6';
const payload = {
patient_name: `E2E Test Patient ${Date.now()}`,
national_id: `E2E${Date.now()}`,
relation_to_patient: 'patient',
incident_date: '2026-01-15',
description: 'E2E automated test complaint - please ignore',
hospital: hospital_id,
complaint_type: 'complaint',
...data,
};
const resp = await this.post('/complaints/api/complaints/', payload);
return { response: resp, body: await resp.json().catch(() => null) };
}
async createInquiry(data?: Partial<any>) {
const hospital_id = '7e4d08ce-2e0a-4184-8297-7acd418184a6';
const payload = {
contact_name: `E2E Test Contact ${Date.now()}`,
contact_phone: '0500000001',
contact_email: `e2e-test-${Date.now()}@test.com`,
subject: `E2E automated test inquiry ${Date.now()}`,
message: 'E2E automated test inquiry - please ignore',
hospital: hospital_id,
category: 'general',
...data,
};
const resp = await this.post('/complaints/api/inquiries/', payload);
return { response: resp, body: await resp.json().catch(() => null) };
}
}
export const HOSPITAL_ID = '7e4d08ce-2e0a-4184-8297-7acd418184a6';
export const DEPT_ID = 'db141bc8-fe52-43bf-a162-b8872e4f8488';
export const LOCATION_ID = '49';