Skip to content

Commit

Permalink
Environment variable configuration of parties (#77)
Browse files Browse the repository at this point in the history
* Initial work- parties addable, but no tests, no functionality

* Added test for preconfigured parties

* Added config unit test

* Update src/config.js

* Update src/config.js

* Updated config correctly. Duh.

* Bumped version
  • Loading branch information
partiallyordered committed Sep 3, 2020
1 parent dab8b8e commit 900e19c
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const config = {
reportApi: 3002,
testApi: 3003,
},
parties: [],
};


Expand All @@ -78,6 +79,7 @@ const setConfig = async (cfg) => {
config.ports.simulatorApi = cfg.SIMULATOR_API_LISTEN_PORT || config.ports.simulatorApi;
config.ports.reportApi = cfg.REPORT_API_LISTEN_PORT || config.ports.reportApi;
config.ports.testApi = cfg.TEST_API_LISTEN_PORT || config.ports.testApi;
config.parties = cfg.PARTIES ? JSON.parse(cfg.PARTIES) : config.parties;
};


Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const testApi = new Koa();
(async function start() {
// Set up the config from the environment
await setConfig(process.env);
const conf = getConfig();

// Set up a logger for each running server
const space = Number(process.env.LOG_INDENT);
Expand All @@ -81,7 +82,7 @@ const testApi = new Koa();

// Initialise the model
const model = new Model();
await model.init(process.env.MODEL_DATABASE);
await model.init({ databaseFilePath: process.env.MODEL_DATABASE, parties: conf.parties });

// Log raw to console as a last resort- if the logging framework crashes
const failSafe = async (ctx, next) => {
Expand Down Expand Up @@ -257,7 +258,6 @@ const testApi = new Koa();

// If config specifies TLS, start an HTTPS server; otherwise HTTP
let simServer;
const conf = getConfig();
const simulatorPort = conf.ports.simulatorApi;
const reportPort = conf.ports.reportApi;
const testApiPort = conf.ports.testApi;
Expand Down
6 changes: 5 additions & 1 deletion src/models/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = class Model {
* @param {String} databaseFilepath SqliteDB file path
* @throws {Error}
*/
async init(databaseFilepath) {
async init({ databaseFilepath, parties }) {
if (this.db) {
throw new Error('Attempted to initialise database twice');
}
Expand All @@ -104,6 +104,10 @@ module.exports = class Model {
this.transactionrequest = new TransactionRequest(this.db);
this.transfer = new Transfer(this.db);
this.bulkTransfer = new BulkTransfer(this.db);

if (parties) {
await Promise.all(parties.map((p) => this.party.create(p)));
}
} catch (err) {
throw new Error(err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mojaloop-simulator",
"version": "11.1.0",
"version": "11.2.0",
"description": "A canonical test example implementation of the parties, transfers and quotes resources of the Mojaloop API",
"license": "Apache-2.0",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/test/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const { party, idType, idValue } = require('./constants');

test.beforeEach(async (t) => {
const model = new Model();
await model.init(':memory:');
await model.init({ databaseFilepath: ':memory:' });
// eslint-disable-next-line no-param-reassign
t.context = { state: { model }, response: {} };
});
Expand Down
4 changes: 2 additions & 2 deletions src/test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const {

test.beforeEach(async (t) => {
const model = new Model();
await model.init(':memory:');
await model.init({ databaseFilepath: ':memory:' });
// eslint-disable-next-line no-param-reassign
t.context = { model };
});
Expand Down Expand Up @@ -395,7 +395,7 @@ test('throws if we try to init the db incorrectly', async (t) => {
});

// Assert
t.is(error.message, 'TypeError: Argument 0 must be a string', 'Invalid error message.');
t.is(error.message, 'Cannot destructure property \'databaseFilepath\' of \'undefined\' as it is undefined.', 'Invalid error message.');
});

test('does nothing if trying to close a non existent db', async (t) => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const validQuerystring = stringify({ START_DATE_TIME: '2019-05-20T21:20:56', END
const nonFindableQuerystring = stringify({ START_DATE_TIME: '2019-05-19T21:20:00', END_DATE_TIME: '2019-05-20T21:20:56' });

test.before(async (t) => {
await model.init(process.env.MODEL_DATABASE);
await model.init({ databaseFilepath: process.env.MODEL_DATABASE });
Array.from({ length: 10 }).forEach(async (x, i) => {
quote.quoteId = uuid();
await model.quote.create(quote);
Expand Down
2 changes: 1 addition & 1 deletion src/test/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const { ApiErrorCodes } = require('../models/errors');

test.beforeEach(async (t) => {
const model = new Model();
await model.init(':memory:');
await model.init({ databaseFilepath: ':memory:' });
// eslint-disable-next-line no-param-reassign
t.context = {
state: { model, logger: console }, response: {},
Expand Down
27 changes: 26 additions & 1 deletion src/test/test-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,28 @@ const testOps = [

];

const preconfiguredParties = [
{
...party,
idValue: '123457',
},
{
...party,
idValue: '123458',
},
{
...party,
idValue: '123459',
},
{
...party,
idValue: '123456',
},
];

test.beforeEach(async (t) => {
const model = new Model();
await model.init(':memory:');
await model.init({ databaseFilepath: ':memory:', parties: preconfiguredParties });
// eslint-disable-next-line no-param-reassign
t.context = { state: { model, logger: console }, response: {} };
});
Expand All @@ -125,6 +144,12 @@ test.afterEach(async (t) => {
await t.context.state.model.close();
});

test.only('preconfigured parties should pre-exist', async (t) => {
await handlers.map['/repository/parties'].get(t.context);
const byId = (a, b) => Number(a.idValue) - Number(b.idValue);
t.deepEqual(t.context.response.body.sort(byId), preconfiguredParties.sort(byId));
});

test('should return 200 when reading a party', async (t) => {
// eslint-disable-next-line no-param-reassign
await handlers.map['/repository/parties'].get(t.context);
Expand Down
7 changes: 4 additions & 3 deletions src/test/unit/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

const test = require('ava');
const { setConfig, getConfig } = require('../../config');

const { party } = require('../constants');

// Note: these were originally 3 different tests, which I had to combine into 1
// because of the way that ava tries to run the tests in paralell, which was causing
Expand All @@ -39,10 +39,10 @@ test('Sets the basic config', async (t) => {
const env = {
MUTUAL_TLS_ENABLED: 'false',
HTTPS_ENABLED: 'false',
PARTIES: JSON.stringify([party, party, party]),
};
const expected = {
tls:
{
tls: {
enabled: false,
mutualTLS: { enabled: false },
creds: { ca: null, cert: null, key: null },
Expand All @@ -52,6 +52,7 @@ test('Sets the basic config', async (t) => {
reportApi: 3002,
testApi: 3003,
},
parties: [party, party, party],
};

// Act
Expand Down

0 comments on commit 900e19c

Please sign in to comment.