UNPKG

happy-client

Version:

A JavaScript client to interact with Happy backend.

732 lines (668 loc) 29.1 kB
'use strict'; const {describe, it} = require('mocha'); const assert = require('chai').assert; const HappyClient = require('./client'); const uuidv5 = require('uuid/v5'); const uuidv1 = require('uuid/v1'); const normalizeUrl = require('./clientutils').normalizeUrl; const dotenv = require('dotenv'); dotenv.config(); const happy_fhir_url = process.env.HAPPY_FHIR_URL; const happy_backend_url = process.env.HAPPY_BACKEND_URL; const client = new HappyClient(happy_backend_url, happy_fhir_url); const axios = require('axios'); function uuid() { return uuidv5("Unit test", uuidv1()).replace("-", "") + uuidv5("Unit test", uuidv1()).replace("-", "") + uuidv5("Unit test", uuidv1()).replace("-", ""); } const practitioner_user_name = uuid(); const patient_user_name = uuid(); const password = uuid(); let practitioner_auth = null; let patient_auth = null; let practitioner_reference = null; let patient_reference = null; let practitioner_resource_type = null; let patient_resource_type = null; let practitioner_id = null; let patient_id = null; let medication_reference = null; console.log("Practitioner's user name: " + practitioner_user_name); console.log("Patient's user name: " + patient_user_name); describe('Happy client test suite', function () { this.timeout(30000); it('Test read users vatar', function(done){ axios.get(client.avatarUrl("test")).then(function(response){ assert.strictEqual(response.status, 200); done(); }); }); it('Test normalize url to avoid dumb errors', function () { assert.strictEqual("example.com/test", normalizeUrl("example.com", "test")); assert.strictEqual("example.com/test", normalizeUrl("example.com", "/", "test")); assert.strictEqual("example.com/test", normalizeUrl("example.com/", "/test/")); assert.strictEqual("example.com/test", normalizeUrl("/example.com/", "/test/")); }); it('Register Practitioner should be possible', function () { assert.strictEqual(client.happy_fhir_url, happy_fhir_url, "The client initialization should be consistent."); assert.strictEqual(client.happy_backend_url, happy_backend_url, "The client initialization should be consistent."); }); it('Client should strip trailing /', function () { const test_client = new HappyClient(happy_backend_url + "/", happy_fhir_url + "/"); assert.strictEqual(test_client.happy_fhir_url, happy_fhir_url, "The client should strip the trailing / from happy fhir client"); assert.strictEqual(test_client.happy_backend_url, happy_backend_url, "The client should strip the trailing / from happy backend client"); assert.throws(function () { new HappyClient(); }); }); it('Register a practitioner should return 201', function (done) { assert.throws(function () { client.registerPractitioner(); }); client.registerPractitioner( "Heilbronn", "Germany", "12345", "BW", "Max-Planck-Strasse 31", "1970-01-01", false, "Mustermann", "male", "Max", "123456", "maxmustermann@hs-heilbronn.de", "1234567", "123456789", practitioner_user_name, password ).then(function (response) { assert.strictEqual(response.status, 201, "Creating a random practitioner should be successful"); assert.isNotNull(response.headers.authorization, "Authorization header should be included"); assert.isNotNull(response.data.userName, "User name should not be null"); assert.isNotNull(response.data.fhirReference, "Fhir reference should be included"); assert.isNotNull(response.data.fhirResourceType, "Fhir resource type should be included"); assert.isNotNull(response.data.fhirResourceId, "Fhir resource id should be included"); assert.isNotNull(response.data.links, "Hateoas links should be included"); console.log("Patient created", JSON.stringify(response.data)); done(); }).catch(function (error) { console.log(error); console.log(error.response.data); console.log(error.response.status); throw new Error(error.response.data); }); }); it('Register a patient should return 201', function (done) { assert.throws(function () { client.registerPatient(); }); client.registerPatient( "Heilbronn", "Germany", "12345", "BW", "Max-Planck-Strasse 31", "1970-01-01", false, "Mustermann", "male", "Max", "123456", "maxmustermann@hs-heilbronn.de", "7890", "192837465", patient_user_name, password ).then(function (response) { assert.strictEqual(response.status, 201, "Creating a random practitioner should be successful"); assert.isNotNull(response.headers.authorization, "Authorization header should be included"); assert.isNotNull(response.data.userName, "User name should not be null"); assert.isNotNull(response.data.fhirReference, "Fhir reference should be included"); assert.isNotNull(response.data.fhirResourceType, "Fhir resource type should be included"); assert.isNotNull(response.data.fhirResourceId, "Fhir resource id should be included"); assert.isNotNull(response.data.links, "Hateoas links should be included"); console.log("Practitioner created", JSON.stringify(response.data)); done(); }).catch(function (error) { throw new Error(error.response.data); }); }); it('Login patient should return 200 and an auth code', function (done) { assert.throws(function () { client.login(); }); client.login(patient_user_name, password) .then(function (response) { assert.strictEqual(response.status, 200, "Login with valid password should return 200"); patient_auth = response.headers['authorization']; patient_reference = response.data['fhirReference']; patient_resource_type = response.data['fhirResourceType']; patient_id = response.data['fhirResourceId']; let patient_user_name_response = response.data['userName']; assert.strictEqual(patient_user_name_response, patient_user_name, "No fucking hard coded user name"); assert(patient_auth, "Patient auth should be obtained"); assert(patient_reference, 'Fhir reference from response should be obtained'); assert(patient_resource_type, "Fhir resource type should be obtained"); assert(patient_id, "Fhir id should be obtained"); console.log("Patient bearer token", patient_auth); done(); }).catch(function (error) { throw new Error(error.response.data); }); }); it('Login practitioner should return 200 and an auth code', function (done) { assert.throws(function () { client.login(); }); client.login(practitioner_user_name, password) .then(function (response) { assert.strictEqual(response.status, 200, "Login with valid password should return 200"); practitioner_auth = response.headers['authorization']; practitioner_reference = response.data['fhirReference']; practitioner_resource_type = response.data['fhirResourceType']; practitioner_id = response.data['fhirResourceId']; assert(practitioner_auth, "Practitioner auth should be obtained"); assert(practitioner_reference, 'Fhir reference from response should be obtained'); assert(practitioner_resource_type, "Fhir resource type should be obtained"); assert(practitioner_id, "Fhir id should be obtained"); let practitioner_user_name_response = response.data['userName']; assert.strictEqual(practitioner_user_name_response, practitioner_user_name, "No fucking hard coded user name"); console.log("Practitioner bearer token", patient_auth); done(); }).catch(function (error) { console.log(error.response.data); done(); }); }); it('Create a test observation', function (done) { assert.throws(function () { client.create(); }); client.create({ 'resourceType': 'Observation', 'subject': { 'reference': patient_reference, 'type': 'Patient' }, 'note': 'Eat an apple.', 'status': 'final', }, 'Observation', patient_auth).then( function (response) { assert.strictEqual(201, response.status); assert.strictEqual('Observation', response.data['resourceType']); done(); } ).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test read function with a practitioner', function (done) { assert.throws(function () { client.read(); }); client.read("Practitioner", practitioner_id, practitioner_auth) .then(function (response) { assert.strictEqual(200, response.status); assert.strictEqual("Practitioner", response.data["resourceType"]); console.log("Practitioner readed", JSON.stringify(response.data)); done() }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test read function with a patient', function (done) { assert.throws(function () { client.read(); }); client.read("Patient", patient_id, patient_auth) .then(function (response) { assert.strictEqual(200, response.status); assert.strictEqual("Patient", response.data["resourceType"]); console.log("Patient readed", JSON.stringify(response.data)); done() }).catch(function (error) { throw new Error(error); }); }); it('Test read eager function with a patient', function (done) { assert.throws(function () { client.readEager(); }); client.readEager("Encounter", patient_auth) .then(function (response) { assert.strictEqual(200, response.status); console.log(response.data); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Read everything from a patient with false token should not be possible', function (done) { assert.throws(function () { client.readPatientEverything(); }); client.readPatientEverything(patient_id, "asdf").then(function (response) { assert.strictEqual(403, response.status); done(); }).catch(function (error) { assert.strictEqual(403, error.response.status); done(); }); }); it('Read everything from a patient', function (done) { assert.throws(function () { client.readPatientEverything(); }); client.readPatientEverything(patient_id, patient_auth).then(function (response) { assert.strictEqual(200, response.status); assert.strictEqual("Bundle", response.data["resourceType"]); done(); }).catch(function (error) { throw new Error(error); }) }); it('Retrieve every patient from a practitioner should return empty list', function (done) { assert.throws(function () { client.retrieveAllPatientsForPractitioner(); }); client.retrieveAllPatientsForPractitioner(practitioner_id, practitioner_auth) .then(function (response) { assert.strictEqual(200, response.status); assert.strictEqual('Bundle', response.data['resourceType']); done(); }).catch(function (error) { throw new Error(error); }); }); it('Grant access on doctor should be okay', function (done) { assert.throws(function () { client.grantAccess(); }); client.grantAccess(patient_user_name, practitioner_user_name, true, null, null, patient_auth) .then(function (response) { assert.strictEqual(201, response.status); assert.strictEqual("Access granted", response.data); done(); }); }); it('Retrieve every resource a patient has', function (done) { assert.throws(function () { client.getResourceReference(); }); client.getResourceReference(patient_user_name, patient_auth) .then(function (response) { assert.strictEqual(200, response.status); response.data.forEach(function (data) { assert.strictEqual(patient_user_name, data['owner']['userName']); }); done(); }).catch(function (error) { throw new Error(error); }); }); it('Retrieve every resource a practitioner has', function (done) { assert.throws(function () { client.getResourceReference(); }); client.getResourceReference(practitioner_user_name, practitioner_auth) .then(function (response) { assert.strictEqual(200, response.status); response.data.forEach(function (data) { assert.strictEqual(practitioner_user_name, data['owner']['userName']); }); done(); }).catch(function (error) { throw new Error(error); }); }); it('Retrieve every resource a patient has sorted by types', function (done) { assert.throws(function () { client.getResourceReferenceByResourceType(); }); client.getResourceReferenceByResourceType(patient_user_name, "Patient", patient_auth) .then(function (response) { assert.strictEqual(200, response.status); response.data.forEach(function (data) { assert.strictEqual(patient_user_name, data['owner']['userName']); }); done(); }).catch(function (error) { throw new Error(error); }); }); it('Retrieve every resource a practitioner has sorted by types', function (done) { assert.throws(function () { client.getResourceReferenceByResourceType(); }); client.getResourceReferenceByResourceType(practitioner_user_name, "Practitioner", practitioner_auth) .then(function (response) { assert.strictEqual(200, response.status); response.data.forEach(function (data) { assert.strictEqual(practitioner_user_name, data['owner']['userName']); }); done(); }).catch(function (error) { throw new Error(error); }); }); it('Retrieve every patient from a practitioner should again should return full list', function (done) { assert.throws(function () { client.retrieveAllPatientsForPractitioner(); }); client.retrieveAllPatientsForPractitioner(practitioner_id, practitioner_auth) .then(function (response) { console.log(response.data); assert.strictEqual(200, response.status); assert.strictEqual('Bundle', response.data['resourceType']); done(); }).catch(function (error) { throw new Error(error); }); }); it('Patients should be able to see who can see himself', function (done) { assert.throws(function () { client.whocanseeme(); }); client.whocanseeme(patient_id, patient_auth) .then(function (response) { assert.strictEqual(200, response.status); assert.strictEqual('Bundle', response.data['resourceType']); let practitioner = response.data['entry'][0]; assert.strictEqual("Practitioner", practitioner['resource']['resourceType']); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Retrieve every practitioner a patient has granted accesses on', function (done) { assert.throws(function () { client.getGrantedAccess(); }); client.getGrantedAccess(patient_user_name, patient_auth) .then(function (response) { assert.strictEqual(200, response.status); response.data.forEach(function (data) { assert.strictEqual(practitioner_user_name, data['targetedUser']); }); done(); }).catch(function (error) { throw new Error(error); }); }); it('Test forget password', function (done) { assert.throws(function () { client.forgotPassword(); }); client.forgotPassword(patient_user_name) .then(function () { done(); }).catch(function () { done(); }); }); it('Test change password', function (done) { assert.throws(function () { client.changePassword(); }); client.changePassword(patient_user_name, "test") .then(function () { done(); }).catch(function () { done(); }); }); it('Test get access patients', function (done) { assert.throws(function () { client.getAccess(); }); client.getAccess(patient_user_name, patient_auth) .then(function (response) { assert.strictEqual(200, response.status); response.data.forEach(function (data) { assert.strictEqual(patient_user_name, data["targetedUser"]) }); done(); }).catch(function (error) { throw new Error(error); }); }); it('Test get access practitioners', function (done) { assert.throws(function () { client.getAccess(); }); client.getAccess(practitioner_user_name, practitioner_auth) .then(function (response) { assert.strictEqual(200, response.status); response.data.forEach(function (data) { assert.strictEqual(practitioner_user_name, data["targetedUser"]) }); done(); }).catch(function (error) { throw new Error(error); }); }); it('Test create resource on behalf', function (done) { assert.throws(function () { client.createResourceOnBehalf(); }); client.createResourceOnBehalf({"resourceType": "DiagnosticReport"}, "DiagnosticReport", patient_user_name, practitioner_auth) .then(function (response) { assert.strictEqual(200, response.status); done(); }).catch(function (error) { throw new Error(error); }); }); it('Test reset current password', function (done) { assert.throws(function () { client.resetCurrentPassword(); }); client.resetCurrentPassword("newpassword", password, patient_auth) .then(function (response) { assert.strictEqual(200, response.status); done(); }).catch(function (error) { throw new Error(error); }); }); it('Test Encounter Template', function (done) { assert.throws(function () { client.encounterTemplate(); }); const encounter = client.encounterTemplate(patient_reference, practitioner_reference, "finished", "2019-11-04", "2019-11-04", "Routine Appendectomy. Appendix was inflamed and in retro-caecal position", uuid()); client.create(encounter, "Encounter", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test procedureTemplate', function (done) { assert.throws(function () { client.procedureTemplate(); }); const procedure = client.procedureTemplate(patient_reference, practitioner_reference, "completed", "2019-11-04", "Appendectomy", "Routine Appendectomy. Appendix was inflamed and in retro-caecal position"); client.create(procedure, "Procedure", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test medicationTemplate', function (done) { assert.throws(function () { client.medicationTemplate(); }); const medication = client.medicationTemplate("active", uuid(), "123456" ); client.create(medication, "Medication", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); medication_reference = response.data['resourceType'] + "/" + response.data['id']; done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test medicationDispenseTemplate', function (done) { assert.throws(function () { client.medicationDispenseTemplate(); }); const medicationDispense = client.medicationDispenseTemplate(patient_reference, practitioner_reference, "preparation", medication_reference, "1970-01-01", uuid(), uuid(), uuid(), 12345); client.create(medicationDispense, "MedicationDispense", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test appointmentTemplate', function (done) { assert.throws(function () { client.appointmentTemplate(); }); const appointment = client.appointmentTemplate(patient_reference, practitioner_reference, "proposed", uuid(), "1970-01-01", uuid(), uuid()); client.create(appointment, "Appointment", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test diagnostic report', function (done) { assert.throws(function () { client.diagnosticReportTemplate(); }); const diagnosticReport = client.diagnosticReportTemplate(patient_reference, practitioner_reference, "registered", uuid(), uuid()); client.create(diagnosticReport, "DiagnosticReport", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test composition', function (done) { assert.throws(function () { client.compositionTemplate(); }); const composition = client.compositionTemplate(patient_reference, "final", "1970-01-01", uuid(), uuid(), "rgb(203, 123, 49)"); client.create(composition, "Composition", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test condition', function (done) { assert.throws(function () { client.conditionTemplate(); }); const condition = client.conditionTemplate(patient_reference, practitioner_reference, "1970-01-01", uuid(), uuid()); client.create(condition, "Condition", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test medication request', function (done) { assert.throws(function () { client.medicationRequestTemplate(); }); const medicationRequest = client.medicationRequestTemplate(patient_reference, practitioner_reference, medication_reference, uuid(), uuid() ,"1970-01-01", "2020-01-01"); client.create(medicationRequest, "MedicationRequest", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test consent', function (done) { assert.throws(function () { client.consentTemplate(); }); const consent = client.consentTemplate(patient_reference, true, true, true, true, uuid(), uuid(), uuid(), uuid()); client.create(consent, "Consent", practitioner_auth) .then(function (response) { assert.strictEqual(201, response.status); assert(response.data['resourceType']); assert(response.data['id']); client.delete("Consent", response.data['id'], practitioner_auth) .then(function (response) { assert.strictEqual(200, response.status); done(); }); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test task', function (done) { assert.throws(function () { client.taskTemplate(); }); const task = client.taskTemplate(patient_reference, uuid(), 12,"1", uuid(), "2019-11-20T09:29:50.899Z"); console.log("This task will be sent to server", JSON.stringify(task)); client.create(task, "Task", patient_auth) .then(function (response) { assert.strictEqual(201, response.status); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); it('Test delete access', function (done) { assert.throws(function () { client.deleteGrantedAccess(); }); client.deleteGrantedAccess(practitioner_user_name, patient_auth) .then(function (response) { assert.strictEqual(200, response.status); done(); }).catch(function (error) { console.log(error.response.data); throw new Error(error); }); }); });