Skip to content

Commit

Permalink
fix(#123): fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
witash committed Oct 8, 2024
1 parent f325edd commit 7470359
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 106 deletions.
2 changes: 1 addition & 1 deletion cht-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test-targets": "npm run eslint && TZ=Africa/Nairobi mocha --reporter progress test/targets/*.spec.js --timeout 10000",
"test-contact-summary": "npm run eslint && TZ=Africa/Nairobi mocha --reporter progress test/contact-summary/*.spec.js --timeout 10000",
"test-unit": "TZ=Africa/Nairobi mocha --recursive --reporter spec test --timeout 20000",
"deploy": "wait-on http://api:5988/ && sleep 100 && sh ./script.sh"
"deploy": "wait-on http://api:5988/ && sh ./script.sh"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down
3 changes: 3 additions & 0 deletions docker/docker-compose.cht-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ services:
- COUCH_URL=http://${COUCHDB_USER:-admin}:${COUCHDB_PASSWORD:-password}@haproxy:${HAPROXY_PORT:-5984}/medic
- BUILDS_URL=${MARKET_URL_READ:-https://staging.dev.medicmobile.org}/${BUILDS_SERVER:-_couch/builds_4}
- UPGRADE_SERVICE_URL=${UPGRADE_SERVICE_URL:-http://localhost:5100}
ports:
- "5988:5988"
logging:
driver: "local"
options:
Expand Down Expand Up @@ -117,6 +119,7 @@ services:
- "COUCHDB_PASSWORD=${COUCHDB_PASSWORD:-password}"
depends_on:
- couchdb
- api
networks:
- cht-net

Expand Down
10 changes: 5 additions & 5 deletions docker/docker-compose.mediator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ services:
- "OPENHIM_API_URL=${OPENHIM_API_URL:-https://openhim-core:8080}"
- "PORT=${PORT:-6000}"
- "FHIR_URL=${FHIR_URL:-http://openhim-core:5001/fhir}"
- "FHIR_USERNAME=${MEDIATORS_USERNAME:-interop-client}"
- "FHIR_PASSWORD=${MEDIATORS_PASSWORD:-interop-password}"
- "OPENMRS_URL=${OPENMRS_CHANNEL_URL}"
- "OPENMRS_USERNAME=${MEDIATORS_USERNAME}"
- "OPENMRS_PASSWORD=${MEDIATORS_PASSWORD}"
- "FHIR_USERNAME=${FHIR_USERNAME:-interop-client}"
- "FHIR_PASSWORD=${FHIR_PASSWORD:-interop-password}"
- "OPENMRS_CHANNEL_URL=${OPENMRS_CHANNEL_URL:-http://openhim-core:5001/openmrs}"
- "OPENMRS_CHANNEL_USERNAME=${OPENMRS_CHANNEL_USERNAME:-interop-client}"
- "OPENMRS_CHANNEL_PASSWORD=${OPENMRS_CHANNEL_PASSWORD:-interop-password}"
- "CHT_URL=${CHT_URL:-https://nginx}"
- "CHT_USERNAME=${CHT_USERNAME:-admin}"
- "CHT_PASSWORD=${CHT_PASSWORD:-password}"
Expand Down
6 changes: 3 additions & 3 deletions mediator/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export const CHT = {
};

export const OPENMRS = {
url: getEnvironmentVariable('OPENMRS_URL', ''),
username: getEnvironmentVariable('OPENMRS_USERNAME', ''),
password: getEnvironmentVariable('OPENMRS_PASSWORD', ''),
url: getEnvironmentVariable('OPENMRS_CHANNEL_URL', 'http://openhim-core:5001/openmrs'),
username: getEnvironmentVariable('OPENMRS_CHANNEL_USERNAME', 'interop-client'),
password: getEnvironmentVariable('OPENMRS_CHANNEL_PASSWORD', 'interop-password'),
timeout: Number(getEnvironmentVariable('REQUEST_TIMEOUT', '5000'))
};

Expand Down
8 changes: 5 additions & 3 deletions mediator/env.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ OPENHIM_API_URL = "https://openhim-core:8080"
PORT = 6000

FHIR_URL = http://openhim-core:5001/fhir
OPENMRS_URL = http://openhim-core:5001/openmrs
FHIR_USERNAME = interop-client
FHIR_PASSWORD = interop-password

MEDIATORS_USERNAME = interop-client
MEDIATORS_PASSWORD = interop-password
OPENMRS_CHANNEL_URL = http://openhim-core:5001/openmrs
OPENMRS_CHANNEL_USERNAME = interop-client
OPENMRS_CHANNEL_PASSWORD = interop-password

CHT_URL = http://nginx
CHT_USERNAME = admin
Expand Down
13 changes: 1 addition & 12 deletions mediator/src/controllers/cht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ export async function createPatient(chtPatientDoc: any) {
}

const fhirPatient = buildFhirPatientFromCht(chtPatientDoc.doc);
const patientResponse = await getFHIRPatientResource(fhirPatient.id || '');
if (patientResponse.status != 200){
// any error, just return it to caller
return patientResponse;
} else if (patientResponse.data.total > 0) {
// updates not currently supported
return patientResponse;
} else {
// create or update in the FHIR Server
// even for create, sends a PUT request
return updateFhirResource({ ...fhirPatient, resourceType: 'Patient' });
}
return updateFhirResource({ ...fhirPatient, resourceType: 'Patient' });
}

export async function updatePatientIds(chtFormDoc: any) {
Expand Down
2 changes: 1 addition & 1 deletion mediator/src/utils/fhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function copyIdToNamedIdentifier(fromResource: any, toResource: fhir4.Pat
}

export function getIdType(resource: fhir4.Patient | fhir4.Encounter, idType: fhir4.CodeableConcept): string{
return resource?.identifier?.find((id: any) => id?.type.text == idType.text)?.value || '';
return resource?.identifier?.find((id: any) => id?.type?.text == idType.text)?.value || '';
}

export function addId(resource: fhir4.Patient | fhir4.Encounter, idType: fhir4.CodeableConcept, value: string){
Expand Down
4 changes: 4 additions & 0 deletions mediator/src/utils/openmrs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { OPENMRS } from '../../config';
import axios from 'axios';
import { logger } from '../../logger';
import https from 'https';

const axiosOptions = {
auth: {
username: OPENMRS.username,
password: OPENMRS.password,
},
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
timeout: OPENMRS.timeout
};

Expand Down
23 changes: 16 additions & 7 deletions mediator/test/e2e-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ MEDIATORDIR="${BASEDIR}/mediator"
export NODE_ENV=integration
export NODE_TLS_REJECT_UNAUTHORIZED=0

export OPENMRS_HOST=openmrs
export OPENMRS_USERNAME=admin
export OPENMRS_PASSWORD=Admin123

# Cleanup from last test, in case of interruptions
cd $BASEDIR
./startup.sh destroy
Expand All @@ -29,14 +33,20 @@ export FHIR_USERNAME='interop-client'
export FHIR_PASSWORD='interop-password'
export CHT_USERNAME='admin'
export CHT_PASSWORD='password'
export OPENMRS_URL='http://openhim-core:5001/openmrs'
export OPENMRS_USERNAME='interop-client'
export OPENMRS_PASSWORD='interop-password'
export OPENMRS_CHANNEL_URL='http://localhost:5001/openmrs'
export OPENMRS_CHANNEL_USERNAME='interop-client'
export OPENMRS_CHANNEL_PASSWORD='interop-password'

echo 'Waiting for OpenMRS to be ready'
sleep 180
npm run test -t workflows.spec.ts

# Cleanup
unset NODE_ENV
unset NODE_TLS_REJECT_UNAUTHORIZED
unset OPENMRS_HOST
unset OPENMRS_USERNAME
unset OPENMRS_PASSWORD
unset OPENHIM_API_URL
unset FHIR_URL
unset CHT_URL
Expand All @@ -46,9 +56,8 @@ unset FHIR_USERNAME
unset FHIR_PASSWORD
unset CHT_USERNAME
unset CHT_PASSWORD
unset OPENMRS_URL
unset OPENMRS_USERNAME
unset OPENMRS_PASSWORD
unset OPENMRS_CHANNEL_URL
unset OPENMRS_CHANNEL_USERNAME
unset OPENMRS_CHANNEL_PASSWORD
cd $BASEDIR
./startup.sh destroy

171 changes: 97 additions & 74 deletions mediator/test/workflows.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const installMediatorConfiguration = async () => {
});
try {
const res = await request(OPENHIM.apiURL)
.post('/mediators/urn:mediator:ltfu-mediator/channels')
.post('/mediators/urn:mediator:cht-mediator/channels')
.send(['Mediator'])
.set('auth-username', authHeaders['auth-username'])
.set('auth-ts', authHeaders['auth-ts'])
Expand All @@ -46,6 +46,27 @@ const installMediatorConfiguration = async () => {
}
};

const createOpenMRSIdType = async (name: string) => {
const patientIdType = {
name: name,
description: "CHT Patient ID",
required: false,
locationBehavior: "NOT_USED",
uniquenessBehavior: "Unique"
}
try {
const res = await request("http://localhost:8090")
.post('/openmrs/ws/rest/v1/patientidentifiertype')
.auth('admin', 'Admin123')
.send(patientIdType)
if (res.status !== 201) {
throw new Error(`Mediator channel installation failed: Reason ${res.status}`);
}
} catch (error) {
throw new Error(`Mediator channel installation failed ${error}`);
}
};

let placeId: string;
let chwUserName: string;
let chwPassword: string;
Expand Down Expand Up @@ -85,6 +106,81 @@ describe('Workflows', () => {
beforeAll(async () => {
await installMediatorConfiguration();
await configureCHT();
await createOpenMRSIdType('CHT Patient ID');
await createOpenMRSIdType('CHT Document ID');
});

describe('OpenMRS workflow', () => {
it('Should follow the CHT Patient to OpenMRS workflow', async () => {
const checkMediatorResponse = await request(FHIR.url)
.get('/mediator/')
.auth(FHIR.username, FHIR.password);

expect(checkMediatorResponse.status).toBe(200);
expect(checkMediatorResponse.body.status).toBe('success');

const patient = PatientFactory.build({}, { name: 'OpenMRS patient', placeId: placeId });

const createPatientResponse = await request(CHT.url)
.post('/api/v1/people')
.auth(chwUserName, chwPassword)
.send(patient);

expect(createPatientResponse.status).toBe(200);
expect(createPatientResponse.body.ok).toEqual(true);
patientId = createPatientResponse.body.id;

await new Promise((r) => setTimeout(r, 3000));

const retrieveFhirPatientIdResponse = await request(FHIR.url)
.get('/fhir/Patient/?identifier=' + patientId)
.auth(FHIR.username, FHIR.password);

expect(retrieveFhirPatientIdResponse.status).toBe(200);
expect(retrieveFhirPatientIdResponse.body.total).toBe(1);

const triggerOpenMrsSyncPatientResponse = await request(FHIR.url)
.get('/mediator/openmrs/sync')
.auth(FHIR.username, FHIR.password)
.send();

expect(triggerOpenMrsSyncPatientResponse.status).toBe(200);

await new Promise((r) => setTimeout(r, 3000));

const retrieveOpenMrsPatientIdResponse = await request(OPENMRS.url)
.get('/Patient/?identifier=' + patientId)
.auth(OPENMRS.username, OPENMRS.password);

expect(retrieveOpenMrsPatientIdResponse.status).toBe(200);
//this should work after fixing openmrs to have latest fhir omod and cht identifier defined.
//expect(retrieveOpenMrsPatientIdResponse.body.total).toBe(1);

//Validate HAPI updated ids

});

it('Should follow the OpenMRS Patient to CHT workflow', async () => {
const checkMediatorResponse = await request(FHIR.url)
.get('/mediator/')
.auth(FHIR.username, FHIR.password);

expect(checkMediatorResponse.status).toBe(200);
expect(checkMediatorResponse.body.status).toBe('success');

//Create a patient using openMRS api

/*const retrieveFhirPatientIdResponse = await request(FHIR.url)
.get('/fhir/Patient/?identifier=' + patientId)
.auth(FHIR.username, FHIR.password);
expect(retrieveFhirPatientIdResponse.status).toBe(200);
expect(retrieveFhirPatientIdResponse.body.total).toBe(1);*/

//retrieve and validate patient from CHT api
//trigger openmrs sync
//validate id
});
});

describe('Loss To Follow-Up (LTFU) workflow', () => {
Expand Down Expand Up @@ -183,77 +279,4 @@ describe('Workflows', () => {
expect(retrieveFhirDbEncounter.body.total).toBe(1);
});
});

describe('OpenMRS workflow', () => {
it('Should follow the CHT Patient to OpenMRS workflow', async () => {
const checkMediatorResponse = await request(FHIR.url)
.get('/mediator/')
.auth(FHIR.username, FHIR.password);

expect(checkMediatorResponse.status).toBe(200);
expect(checkMediatorResponse.body.status).toBe('success');

const patient = PatientFactory.build({}, { name: 'OpenMRS patient', placeId: placeId });

const createPatientResponse = await request(CHT.url)
.post('/api/v1/people')
.auth(chwUserName, chwPassword)
.send(patient);

expect(createPatientResponse.status).toBe(200);
expect(createPatientResponse.body.ok).toEqual(true);
patientId = createPatientResponse.body.id;

await new Promise((r) => setTimeout(r, 3000));

const retrieveFhirPatientIdResponse = await request(FHIR.url)
.get('/fhir/Patient/?identifier=' + patientId)
.auth(FHIR.username, FHIR.password);

expect(retrieveFhirPatientIdResponse.status).toBe(200);
expect(retrieveFhirPatientIdResponse.body.total).toBe(1);

const triggerOpenMrsSyncPatientResponse = await request(FHIR.url)
.post('/mediator/cht/sync')
.auth(FHIR.username, FHIR.password)
.send();

expect(triggerOpenMrsSyncPatientResponse.status).toBe(200);

await new Promise((r) => setTimeout(r, 30000));

const retrieveOpenMrsPatientIdResponse = await request(OPENMRS.url)
.get('/Patient/?identifier=' + patientId)
.auth(OPENMRS.username, OPENMRS.password);

expect(retrieveOpenMrsPatientIdResponse.status).toBe(200);
//this should work after fixing openmrs to have latest fhir omod and cht identifier defined.
//expect(retrieveOpenMrsPatientIdResponse.body.total).toBe(1);

//Validate HAPI updated ids

});

it('Should follow the OpenMRS Patient to CHT workflow', async () => {
const checkMediatorResponse = await request(FHIR.url)
.get('/mediator/')
.auth(FHIR.username, FHIR.password);

expect(checkMediatorResponse.status).toBe(200);
expect(checkMediatorResponse.body.status).toBe('success');

//Create a patient using openMRS api

/*const retrieveFhirPatientIdResponse = await request(FHIR.url)
.get('/fhir/Patient/?identifier=' + patientId)
.auth(FHIR.username, FHIR.password);
expect(retrieveFhirPatientIdResponse.status).toBe(200);
expect(retrieveFhirPatientIdResponse.body.total).toBe(1);*/

//retrieve and validate patient from CHT api
//trigger openmrs sync
//validate id
});
});
});

0 comments on commit 7470359

Please sign in to comment.