diff --git a/config/default.json b/config/default.json index 95d4435..81c0daa 100644 --- a/config/default.json +++ b/config/default.json @@ -1,6 +1,16 @@ { "PORT": 3003, "HOSTNAME": "http://bulk-api-adapter", + "PROTOCOL_VERSIONS": { + "CONTENT": "1.1", + "ACCEPT": { + "DEFAULT": "1", + "VALIDATELIST": [ + "1", + "1.1" + ] + } + }, "ENDPOINT_SOURCE_URL": "http://localhost:3001/participants/{{fsp}}/endpoints", "ENDPOINT_HEALTH_URL": "http://localhost:3001/health", "ENDPOINT_CACHE_CONFIG": { diff --git a/src/lib/config.js b/src/lib/config.js index a8c3a4f..eecf65f 100644 --- a/src/lib/config.js +++ b/src/lib/config.js @@ -1,5 +1,25 @@ const RC = require('parse-strings-in-object')(require('rc')('BKAPI', require('../../config/default.json'))) +const DEFAULT_PROTOCOL_VERSION = { + CONTENT: '1.1', + ACCEPT: { + DEFAULT: '1', // This is not currently used by this service, but it is here for consistency between services. In future if we need to default the ACCEPT protocol, then this should be used. + VALIDATELIST: [ + '1', + '1.1' + ] + } +} + +const getProtocolVersions = (defaultProtocolVersions, overrideProtocolVersions) => { + const T_PROTOCOL_VERSION = { ...defaultProtocolVersions, ...overrideProtocolVersions } + if (overrideProtocolVersions && overrideProtocolVersions.ACCEPT) T_PROTOCOL_VERSION.ACCEPT = { ...defaultProtocolVersions.ACCEPT, ...overrideProtocolVersions.ACCEPT } + if (T_PROTOCOL_VERSION.ACCEPT && T_PROTOCOL_VERSION.ACCEPT.VALIDATELIST && (typeof T_PROTOCOL_VERSION.ACCEPT.VALIDATELIST === 'string' || T_PROTOCOL_VERSION.ACCEPT.VALIDATELIST instanceof String)) { + T_PROTOCOL_VERSION.ACCEPT.VALIDATELIST = JSON.parse(T_PROTOCOL_VERSION.ACCEPT.VALIDATELIST) + } + return T_PROTOCOL_VERSION +} + // Set config object to be returned const config = { HOSTNAME: RC.HOSTNAME.replace(/\/$/, ''), @@ -19,7 +39,8 @@ const config = { INSTRUMENTATION_METRICS_CONFIG: RC.INSTRUMENTATION.METRICS.config, ENDPOINT_SECURITY: RC.ENDPOINT_SECURITY, ENDPOINT_SECURITY_TLS: RC.ENDPOINT_SECURITY.TLS, - MAX_FULFIL_TIMEOUT_DURATION_SECONDS: RC.MAX_FULFIL_TIMEOUT_DURATION_SECONDS + MAX_FULFIL_TIMEOUT_DURATION_SECONDS: RC.MAX_FULFIL_TIMEOUT_DURATION_SECONDS, + PROTOCOL_VERSIONS: getProtocolVersions(DEFAULT_PROTOCOL_VERSION, RC.PROTOCOL_VERSIONS) } module.exports = config diff --git a/src/shared/plugins.js b/src/shared/plugins.js index 18410e0..a5a2f7e 100644 --- a/src/shared/plugins.js +++ b/src/shared/plugins.js @@ -64,6 +64,30 @@ const registerPlugins = async (server) => { plugin: require('hapi-auth-bearer-token') }) + // Helper to construct FSPIOPHeaderValidation option configuration + const getOptionsForFSPIOPHeaderValidation = () => { + // configure supported FSPIOP Content-Type versions + const supportedProtocolContentVersions = [Config.PROTOCOL_VERSIONS.CONTENT.toString()] + + // configure supported FSPIOP Accept version + const supportedProtocolAcceptVersions = [] + for (const version of Config.PROTOCOL_VERSIONS.ACCEPT.VALIDATELIST) { + supportedProtocolAcceptVersions.push(version.toString()) + } + + // configure FSPIOP resources + const resources = [ + 'transfers' + ] + + // return FSPIOPHeaderValidation plugin options + return { + resources, + supportedProtocolContentVersions, + supportedProtocolAcceptVersions + } + } + await server.register([ Inert, Vision, @@ -71,7 +95,10 @@ const registerPlugins = async (server) => { ErrorHandling, CentralServices.Util.Hapi.HapiRawPayload, CentralServices.Util.Hapi.HapiEventPlugin, - CentralServices.Util.Hapi.FSPIOPHeaderValidation + { + plugin: CentralServices.Util.Hapi.FSPIOPHeaderValidation.plugin, + options: getOptionsForFSPIOPHeaderValidation() + } ]) } diff --git a/test/unit/lib/config.test.js b/test/unit/lib/config.test.js new file mode 100644 index 0000000..f94878d --- /dev/null +++ b/test/unit/lib/config.test.js @@ -0,0 +1,85 @@ +/***** + License + -------------- + Copyright © 2017 Bill & Melinda Gates Foundation + The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + Contributors + -------------- + This is the official list of the Mojaloop project contributors for this file. + Names of the original copyright holders (individuals or organizations) + should be listed with a '*' in the first column. People who have + contributed from an organization can be listed under the organization + that actually holds the copyright for their contributions (see the + Gates Foundation organization for an example). Those individuals should have + their names indented and be marked with a '-'. Email address can be added + optionally within square brackets . + * Gates Foundation + - Name Surname + + * Shashikant Hirugade + * Miguel de Barros + + -------------- +******/ +'use strict' + +const src = '../../../src/' +const Test = require('tapes')(require('tape')) +const Util = require('@mojaloop/central-services-shared').Util +const Default = require('../../../config/default.json') +const Proxyquire = require('proxyquire') + +Test('Config tests', configTest => { + let sandbox + const Sinon = require('sinon') + + configTest.beforeEach(t => { + sandbox = Sinon.createSandbox() + t.end() + }) + + configTest.afterEach(t => { + sandbox.restore() + t.end() + }) + + configTest.test('getFileContent should', async getFileContentTest => { + getFileContentTest.test('should not throw', test => { + try { + const DefaultStub = Util.clone(Default) + const Config = Proxyquire(`${src}/lib/config`, { + '../../config/default.json': DefaultStub + }) + test.ok(Config) + test.ok('pass') + } catch (e) { + test.fail('should throw') + } + test.end() + }) + + getFileContentTest.test('should pass ENV var BKAPI_PROTOCOL_VERSIONS__ACCEPT__VALIDATELIST as a string', test => { + try { + const DefaultStub = Util.clone(Default) + // set env var + const validateList = ['1'] + process.env.BKAPI_PROTOCOL_VERSIONS__ACCEPT__VALIDATELIST = JSON.stringify(validateList) + const Config = Proxyquire(`${src}/lib/config`, { + '../../config/default.json': DefaultStub + }) + test.ok(Config) + test.ok('pass') + test.deepEqual(Config.PROTOCOL_VERSIONS.ACCEPT.VALIDATELIST, validateList) + } catch (e) { + test.fail('should throw') + } + test.end() + }) + + getFileContentTest.end() + }) + + configTest.end() +})