Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
*/.env
.env
.DS_Store
mediator/dist
mediator/coverage
Expand Down
45 changes: 32 additions & 13 deletions configurator/config/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
require('dotenv/config');
const dotenv = require('dotenv');
const path = require('path');

const OPENHIM_API_HOSTNAME = process.env.OPENHIM_API_HOSTNAME || 'openhim-core';
const OPENHIM_API_PASSWORD =
process.env.OPENHIM_PASSWORD || 'openhim-password';
const OPENHIM_API_PORT = process.env.OPENHIM_API_PORT || 8080;
const OPENHIM_API_USERNAME =
process.env.OPENHIM_USERNAME || 'root@openhim.org';
const OPENHIM_CLIENT_PASSWORD = process.env.OPENHIM_CLIENT_PASSWORD || 'interop-password';
const OPENHIM_USER_PASSWORD = process.env.OPENHIM_USER_PASSWORD || 'interop-password';
const isDocker = process.env.DOCKER_ENV === 'true';
const isTest = process.env.NODE_ENV === 'integration';

if (!isDocker && !isTest) {
const envPath = path.resolve(__dirname, '../.env');
const res = dotenv.config({ path: envPath });

if (res.error) {
throw new Error(`Error loading .env file at ${ envPath }: ${ res.error }`);
}
}

const getEnv = (key) => {
const value = process.env[key];
if (!value || value.trim() === '') {
throw new Error(
`Missing required environment variable: ${key}`
);
}
return value.trim();
};

const OPENHIM_API_URL = getEnv('OPENHIM_API_URL');
const OPENHIM_API_USERNAME = getEnv('OPENHIM_USERNAME');
const OPENHIM_API_PASSWORD = getEnv('OPENHIM_PASSWORD');
const OPENHIM_CLIENT_PASSWORD = getEnv('OPENHIM_CLIENT_PASSWORD');
const OPENHIM_USER_PASSWORD = getEnv('OPENHIM_USER_PASSWORD');

module.exports = {
OPENHIM_API_HOSTNAME,
OPENHIM_API_PASSWORD,
OPENHIM_API_PORT,
OPENHIM_API_URL,
OPENHIM_API_USERNAME,
OPENHIM_API_PASSWORD,
OPENHIM_CLIENT_PASSWORD,
OPENHIM_USER_PASSWORD
OPENHIM_USER_PASSWORD,
};
13 changes: 7 additions & 6 deletions configurator/env.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
OPENHIM_API_HOSTNAME = 'localhost';
OPENHIM_API_PORT = 8080;
OPENHIM_PASSWORD = 'openhim-password';
OPENHIM_USERNAME = 'root@openhim.org';
OPENHIM_CLIENT_PASSWORD = 'interop-password';
OPENHIM_USER_PASSWORD = 'interop-password';
COUCHDB_USER=admin
COUCHDB_PASSWORD=password
OPENHIM_API_URL=https://openhim-core:8080
OPENHIM_PASSWORD=openhim-password
OPENHIM_USERNAME=root@openhim.org
OPENHIM_CLIENT_PASSWORD=interop-password
OPENHIM_USER_PASSWORD=interop-password
7 changes: 4 additions & 3 deletions configurator/libs/authentication.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const crypto = require('crypto');
const {
OPENHIM_API_USERNAME, OPENHIM_API_PASSWORD,
OPENHIM_API_HOSTNAME, OPENHIM_API_PORT
OPENHIM_API_USERNAME,
OPENHIM_API_PASSWORD,
OPENHIM_API_URL,
} = require('../config');
const {fetch} = require('../utils');

Expand Down Expand Up @@ -44,7 +45,7 @@ const generateAuthHeaders = async (options) => {

function generateApiOptions (endpoint) {
return {
apiURL: `https://${OPENHIM_API_HOSTNAME}:${OPENHIM_API_PORT}`,
apiURL: OPENHIM_API_URL,
apiEndpoint: endpoint,
username: OPENHIM_API_USERNAME,
password: OPENHIM_API_PASSWORD,
Expand Down
30 changes: 8 additions & 22 deletions docker/docker-compose.mediator.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
services:
mediator:
build:
build:
context: ../mediator
dockerfile: ./Dockerfile
container_name: mediator
ports:
- "6000:6000"
env_file:
- ../mediator/.env
environment:
- "OPENHIM_USERNAME=${OPENHIM_USERNAME:-interop@openhim.org}"
- "OPENHIM_PASSWORD=${OPENHIM_PASSWORD:-interop-password}"
- "OPENHIM_API_URL=${OPENHIM_API_URL:-https://openhim-core:8080}"
- "PORT=${PORT:-6000}"
- "FHIR_URL=${FHIR_URL:-http://openhim-core:5001/fhir}"
- "FHIR_USERNAME=${FHIR_USERNAME:-interop-client}"
- "FHIR_PASSWORD=${FHIR_PASSWORD:-interop-password}"
- "CHT_URL=${CHT_URL:-https://nginx}"
- "CHT_USERNAME=${CHT_USERNAME:-admin}"
- "CHT_PASSWORD=${CHT_PASSWORD:-password}"

- DOCKER_ENV=true
depends_on:
- configurator
restart: 'unless-stopped'
networks:
- cht-net

configurator:
build:
context: ../
dockerfile: ./configurator/Dockerfile
env_file:
- ../configurator/.env
environment:
- "COUCHDB_USER=${COUCHDB_USER:-admin}"
- "COUCHDB_PASSWORD=${COUCHDB_PASSWORD:-password}"
- "OPENHIM_API_HOSTNAME=${OPENHIM_API_HOSTNAME:-openhim-core}"
- "OPENHIM_API_PORT=${OPENHIM_API_PORT:-8080}"
- "OPENHIM_PASSWORD=${OPENHIM_PASSWORD:-openhim-password}"
- "OPENHIM_USERNAME=${OPENHIM_USERNAME:-root@openhim.org}"
- "OPENHIM_CLIENT_PASSWORD=${OPENHIM_CLIENT_PASSWORD:-interop-password}"
- "OPENHIM_USER_PASSWORD=${OPENHIM_USER_PASSWORD:-interop-password}"
- DOCKER_ENV=true
networks:
- cht-net

networks:
cht-net:
name: ${CHT_NETWORK:-cht-net}

46 changes: 29 additions & 17 deletions mediator/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import * as dotenv from 'dotenv';
dotenv.config();
import path from 'path';

export const PORT = process.env.PORT || 6000;
const isDocker = process.env.DOCKER_ENV === 'true';
const isTest = process.env.NODE_ENV === 'integration';

if (!isDocker && !isTest) {
const envPath = path.resolve(__dirname, '../.env');
const res = dotenv.config({
path: envPath
});
if (res.error) {
throw new Error(`Error loading .env file at ${envPath}: ${res.error}`);
}
}

export const PORT = getEnvironmentVariable('PORT');

export const OPENHIM = {
username: getEnvironmentVariable('OPENHIM_USERNAME', 'interop@openhim.org'),
password: getEnvironmentVariable('OPENHIM_PASSWORD', 'interop-password'),
apiURL: getEnvironmentVariable('OPENHIM_API_URL', 'https://openhim-core:8080'),
username: getEnvironmentVariable('OPENHIM_USERNAME'),
password: getEnvironmentVariable('OPENHIM_PASSWORD'),
apiURL: getEnvironmentVariable('OPENHIM_API_URL'),
trustSelfSigned: true,
};

export const FHIR = {
url: getEnvironmentVariable('FHIR_URL', 'http://openhim-core:5001/fhir'),
username: getEnvironmentVariable('FHIR_USERNAME', 'interop-client'),
password: getEnvironmentVariable('FHIR_PASSWORD', 'interop-password'),
url: getEnvironmentVariable('FHIR_URL'),
username: getEnvironmentVariable('FHIR_USERNAME'),
password: getEnvironmentVariable('FHIR_PASSWORD'),
};

export const CHT = {
url: getEnvironmentVariable('CHT_URL', 'https://nginx'),
username: getEnvironmentVariable('CHT_USERNAME', 'admin'),
password: getEnvironmentVariable('CHT_PASSWORD', 'password'),
url: getEnvironmentVariable('CHT_URL'),
username: getEnvironmentVariable('CHT_USERNAME'),
password: getEnvironmentVariable('CHT_PASSWORD'),
};


function getEnvironmentVariable(env: string, def: string) {
if (process.env.NODE_ENV === 'test') {
return def;
function getEnvironmentVariable(env: string) {
const value = process.env[env];
if (typeof value === 'undefined' || value === '') {
throw new Error(`Missing required environment variable: ${env}`);
}

return process.env[env] || def;
return value;
}
20 changes: 10 additions & 10 deletions mediator/env.template
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
OPENHIM_USERNAME = "interop@openhim.org"
OPENHIM_PASSWORD = "password"
OPENHIM_API_URL = "https://openhim-core:8080"
PORT = 6000
FHIR_URL = http://openhim-core:5001/fhir
FHIR_USERNAME = interop-client
FHIR_PASSWORD = interop-password
CHT_URL = http://nginx
CHT_USERNAME = medic
CHT_PASSWORD = password
OPENHIM_USERNAME=interop@openhim.org
OPENHIM_PASSWORD=interop-password
OPENHIM_API_URL=https://openhim-core:8080
PORT=6000
FHIR_URL=http://openhim-core:5001/fhir
FHIR_USERNAME=interop-client
FHIR_PASSWORD=interop-password
CHT_URL=https://nginx
CHT_USERNAME=admin
CHT_PASSWORD=password
File renamed without changes.
10 changes: 10 additions & 0 deletions mediator/jest.e2e.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const baseConfig = require('./jest.base.config.js');

module.exports = {
...baseConfig,
displayName: 'e2e',
testMatch: [
'<rootDir>/test/**/*.spec.ts',
],
// Note: setupFilesAfterEnv is not included here
};
21 changes: 21 additions & 0 deletions mediator/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
jest.mock('dotenv', () => ({
config: jest.fn(() => {
// Set the environment variables you need for your tests
process.env.DOCKER_ENV = 'false';
process.env.PORT = '8080';
process.env.OPENHIM_USERNAME = 'mock_openhim_user';
process.env.OPENHIM_PASSWORD = 'mock_openhim_password';
process.env.OPENHIM_API_URL = 'http://mock-openhim.com';
process.env.FHIR_URL = 'http://mock-fhir.com';
process.env.FHIR_USERNAME = 'mock_fhir_user';
process.env.FHIR_PASSWORD = 'mock_fhir_password';
process.env.CHT_URL = 'http://mock-cht.com';
process.env.CHT_USERNAME = 'mock_cht_user';
process.env.CHT_PASSWORD = 'mock_cht_password';
return { error: null };
}),
}));

jest.mock('path', () => ({
resolve: jest.fn(() => '/mock/path/.env'),
}));
11 changes: 11 additions & 0 deletions mediator/jest.unit.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const baseConfig = require('./jest.base.config.js');

module.exports = {
...baseConfig,
displayName: 'unit',
testMatch: [
'<rootDir>/src/**/*.spec.ts',
'!<rootDir>/test/**/*',
],
setupFilesAfterEnv: ['./jest.setup.ts'],
};
2 changes: 1 addition & 1 deletion mediator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test:watch": "jest --watch",
"coverage": "jest --coverage",
"clean": "rm -rf ./dist",
"unit-test": "jest src/",
"unit-test": "jest --config ./jest.unit.config.js",
"e2e-test": "test/e2e-test.sh"
},
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions mediator/src/utils/cht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import axios from 'axios';
import { CHT } from '../../config';
import { generateBasicAuthUrl } from './url';
import https from 'https';
import path from 'path';

export async function createChtRecord(patientId: string) {
const record = {
Expand All @@ -24,5 +23,6 @@ export async function createChtRecord(patientId: string) {

export const generateChtRecordsApiUrl = (chtUrl: string, username: string, password: string) => {
const endpoint = generateBasicAuthUrl(chtUrl, username, password);
return path.join(endpoint, '/api/v2/records');

return new URL('/api/v2/records', endpoint).toString();
};
Loading