Skip to content

Commit

Permalink
Add patient tests for current route features
Browse files Browse the repository at this point in the history
  • Loading branch information
samau3 committed Aug 15, 2024
1 parent 43d6ff8 commit 9d6d5e4
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 5 deletions.
11 changes: 11 additions & 0 deletions server/test/fixtures/db/Patient.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
entity: patient
connectedFields: ['createdBy', 'updatedBy']
items:
patient1:
id: 27963f68-ebc1-408a-8bb5-8fbe54671064
firstName: John
middleName: A
lastName: Doe
dateOfBirth: "2000-10-05T00:00:00.000Z"
createdBy: '@user1'
updatedBy: '@user1'
215 changes: 210 additions & 5 deletions server/test/routes/api/v1/patients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('/api/v1/patients', () => {
describe('POST /register', () => {
it('should return an error if not an ADMIN, STAFF or VOLUNTEER user', async (t) => {
const app = await build(t);
const reply = await app
await t.loadFixtures();

let reply = await app
.inject()
.post('/api/v1/patients/register')
.payload({
Expand All @@ -32,6 +34,20 @@ describe('/api/v1/patients', () => {
});

assert.deepStrictEqual(reply.statusCode, StatusCodes.UNAUTHORIZED);

let headers = await t.authenticate('[email protected]', 'test');
reply = await app
.inject()
.post('/api/v1/patients/register')
.payload({
firstName: 'John',
middleName: 'A',
lastName: 'Doe',
dateOfBirth: '1990-01-01',
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.FORBIDDEN);
});

it('should allow ADMIN to register a new patient', async (t) => {
Expand All @@ -51,7 +67,13 @@ describe('/api/v1/patients', () => {

assert.deepStrictEqual(reply.statusCode, StatusCodes.CREATED);
const result = JSON.parse(reply.body);
assert.ok(result.id);
assert.deepStrictEqual(result, {
id: result.id,
firstName: 'John',
middleName: 'A',
lastName: 'Doe',
dateOfBirth: '1990-01-01',
});
});

it('should allow STAFF to register a new patient', async (t) => {
Expand All @@ -71,7 +93,13 @@ describe('/api/v1/patients', () => {

assert.deepStrictEqual(reply.statusCode, StatusCodes.CREATED);
const result = JSON.parse(reply.body);
assert.ok(result.id);
assert.deepStrictEqual(result, {
id: result.id,
firstName: 'John',
middleName: 'A',
lastName: 'Doe',
dateOfBirth: '1990-01-01',
});
});

it('should allow VOLUNTEER to register a new patient', async (t) => {
Expand All @@ -91,10 +119,15 @@ describe('/api/v1/patients', () => {

assert.deepStrictEqual(reply.statusCode, StatusCodes.CREATED);
const result = JSON.parse(reply.body);
assert.ok(result.id);
assert.deepStrictEqual(result, {
id: result.id,
firstName: 'John',
middleName: 'A',
lastName: 'Doe',
dateOfBirth: '1990-01-01',
});
});


it('errors if missing required fields', async (t) => {
const app = await build(t);
await t.loadFixtures();
Expand All @@ -116,6 +149,178 @@ describe('/api/v1/patients', () => {
});

describe('PATCH /update/:patientId', () => {
it('should return an error if not an ADMIN, STAFF or VOLUNTEER user', async (t) => {
const app = await build(t);
await t.loadFixtures();

let reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
firstName: 'John',
middleName: 'A',
lastName: 'Doe',
dateOfBirth: '1990-01-01',
});

assert.deepStrictEqual(reply.statusCode, StatusCodes.UNAUTHORIZED);

let headers = await t.authenticate('[email protected]', 'test');
reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
firstName: 'John',
middleName: 'A',
lastName: 'Doe',
dateOfBirth: '1990-01-01',
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.FORBIDDEN);
});

it('should allow ADMIN to update a patient', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
patientData: {
firstName: 'Jane',
dateOfBirth: '1990-01-01',
},
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse(reply.body);
assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064");
assert.deepStrictEqual(firstName, 'Jane');
assert.deepStrictEqual(middleName, 'A');
assert.deepStrictEqual(lastName, 'Doe');
assert.deepStrictEqual(dateOfBirth, '1990-01-01');
});

it('should allow STAFF to update a patient', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
patientData: {
firstName: 'Jack',
dateOfBirth: '1990-02-01',
},
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse(reply.body);
assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064");
assert.deepStrictEqual(firstName, 'Jack');
assert.deepStrictEqual(middleName, 'A');
assert.deepStrictEqual(lastName, 'Doe');
assert.deepStrictEqual(dateOfBirth, '1990-02-01');
});

it('should allow VOLUNTEER to update a patient', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
patientData: {
firstName: 'Jill',
dateOfBirth: '1990-03-01',
},
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse(reply.body);
assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064");
assert.deepStrictEqual(firstName, 'Jill');
assert.deepStrictEqual(middleName, 'A');
assert.deepStrictEqual(lastName, 'Doe');
assert.deepStrictEqual(dateOfBirth, '1990-03-01');
});

it('should allow ADMIN to update a patient with contact data', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
contactData: {
firstName: 'Jane',
lastName: 'Doe',
phone: '123-456-7890',
relationship: 'Mother',
},
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
const { emergencyContact } = JSON.parse(reply.body);
assert.deepStrictEqual(emergencyContact, {
id: emergencyContact.id,
firstName: 'Jane',
middleName: '',
lastName: 'Doe',
phone: '123-456-7890',
relationship: 'Mother',
});

});

it('should allow ADMIN to update a patient with medical data', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
medicalData: {
allergies: [
{
id: '5c057fc3-15d2-40fc-b664-707d04ba66c2',
},
],
medications: [
{
id: '583c7775-9466-4dab-8a4d-edf1056f097f',
},
],
conditions: [
{
id: '471c8529-81fc-4129-8ca0-f1b7406ed90c',
},
],
},
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
const { id, firstName, middleName, lastName, dateOfBirth, allergies, medications, conditions } = JSON.parse(reply.body);
assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064");
assert.deepStrictEqual(firstName, 'John');
assert.deepStrictEqual(middleName, 'A');
assert.deepStrictEqual(lastName, 'Doe');
assert.deepStrictEqual(dateOfBirth, '2000-10-05');
assert.deepStrictEqual(allergies[0].id, '5c057fc3-15d2-40fc-b664-707d04ba66c2');
assert.deepStrictEqual(medications[0].id, '583c7775-9466-4dab-8a4d-edf1056f097f');
assert.deepStrictEqual(conditions[0].id, '471c8529-81fc-4129-8ca0-f1b7406ed90c');
});

});
});

0 comments on commit 9d6d5e4

Please sign in to comment.