Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variable configuration of parties #77

Merged
merged 7 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 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,9 @@ 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;
if (cfg.PARTIES) {
partiallyordered marked this conversation as resolved.
Show resolved Hide resolved
config.parties = JSON.parse(cfg.PARTIES);
}
partiallyordered marked this conversation as resolved.
Show resolved Hide resolved
};


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/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