From 9d6d5e4f0968fd44a0d0df450afa7cbdd5082eff Mon Sep 17 00:00:00 2001 From: samau3 Date: Wed, 14 Aug 2024 22:40:11 -0700 Subject: [PATCH] Add patient tests for current route features --- server/test/fixtures/db/Patient.yml | 11 ++ server/test/routes/api/v1/patients.test.js | 215 ++++++++++++++++++++- 2 files changed, 221 insertions(+), 5 deletions(-) create mode 100644 server/test/fixtures/db/Patient.yml diff --git a/server/test/fixtures/db/Patient.yml b/server/test/fixtures/db/Patient.yml new file mode 100644 index 00000000..43f85647 --- /dev/null +++ b/server/test/fixtures/db/Patient.yml @@ -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' \ No newline at end of file diff --git a/server/test/routes/api/v1/patients.test.js b/server/test/routes/api/v1/patients.test.js index 63679464..b2a0ae5c 100644 --- a/server/test/routes/api/v1/patients.test.js +++ b/server/test/routes/api/v1/patients.test.js @@ -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({ @@ -32,6 +34,20 @@ describe('/api/v1/patients', () => { }); assert.deepStrictEqual(reply.statusCode, StatusCodes.UNAUTHORIZED); + + let headers = await t.authenticate('first.responder@test.com', '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) => { @@ -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) => { @@ -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) => { @@ -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(); @@ -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('first.responder@test.com', '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('admin.user@test.com', '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('staff.user@test.com', '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('volunteer.user@test.com', '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('admin.user@test.com', '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('admin.user@test.com', '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'); + }); }); });