From 99145197fd75fb327407446cec261c3d0c9988d2 Mon Sep 17 00:00:00 2001 From: Julien Fraichot Date: Tue, 2 Jun 2020 16:58:18 +0200 Subject: [PATCH] fix(ExplorerAPIs): [#548] throw error early when overwriting explorer is malformed --- src/explorers/index.ts | 20 +++++++++++++++----- test/application/explorers/explorer.test.ts | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/explorers/index.ts b/src/explorers/index.ts index 6ba48f9fd..453b97a5e 100644 --- a/src/explorers/index.ts +++ b/src/explorers/index.ts @@ -17,6 +17,14 @@ export interface TDefaultExplorersPerBlockchain { function cleanupExplorerAPIs (explorerAPIs: ExplorerAPI[], indexes: number[]): void { indexes.forEach(index => explorerAPIs.splice(index, 1)); // remove modified explorer to avoid setting them in the custom option later } + +function validateOverwritingExplorer (explorerAPI: ExplorerAPI): boolean { + if (explorerAPI.key && !explorerAPI.keyPropertyName) { + throw new Error(`Property keyPropertyName is not set for ${explorerAPI.serviceName}. Cannot pass the key property to the service.`); + } + return true; +} + const overwrittenIndexes: number[] = []; export function overwriteDefaultExplorers (explorerAPIs: ExplorerAPI[] = [], defaultExplorers: ExplorerAPI[] = [], lastSetOfExplorers = false): ExplorerAPI[] { @@ -30,11 +38,13 @@ export function overwriteDefaultExplorers (explorerAPIs: ExplorerAPI[] = [], def if (userSetExplorerAPIsName.includes(defaultExplorerAPI.serviceName)) { const immutableExplorerAPI = Object.assign({}, defaultExplorerAPI); const customSetExplorerAPI = explorerAPIs.find(customExplorerAPI => customExplorerAPI.serviceName === defaultExplorerAPI.serviceName); - const overwrittenExplorerAPI = Object.assign(immutableExplorerAPI, customSetExplorerAPI); - overwrittenExplorers.push(overwrittenExplorerAPI); - const explorerAPIsIndex = explorerAPIs.findIndex(explorerAPI => explorerAPI.serviceName === overwrittenExplorerAPI.serviceName); - if (!overwrittenIndexes.includes(explorerAPIsIndex)) { - overwrittenIndexes.push(explorerAPIsIndex); + if (validateOverwritingExplorer(customSetExplorerAPI)) { + const overwrittenExplorerAPI = Object.assign(immutableExplorerAPI, customSetExplorerAPI); + overwrittenExplorers.push(overwrittenExplorerAPI); + const explorerAPIsIndex = explorerAPIs.findIndex(explorerAPI => explorerAPI.serviceName === overwrittenExplorerAPI.serviceName); + if (!overwrittenIndexes.includes(explorerAPIsIndex)) { + overwrittenIndexes.push(explorerAPIsIndex); + } } } else { overwrittenExplorers.push(defaultExplorerAPI); diff --git a/test/application/explorers/explorer.test.ts b/test/application/explorers/explorer.test.ts index 1bf97e77e..1c4a4bed4 100644 --- a/test/application/explorers/explorer.test.ts +++ b/test/application/explorers/explorer.test.ts @@ -83,6 +83,21 @@ describe('Blockchain Explorers test suite', function () { const expectedOutput = [Object.assign(mockDefaultExplorer, fixtureExplorer), BlockcypherAPI]; expect(output).toEqual(expectedOutput); }); + + describe('and the explorer overwrite is malformed', function () { + describe('when a key is set but no keyPropertyName', function () { + it('should throw an error', function () { + const fixtureExplorer: ExplorerAPI = { + serviceName: TRANSACTION_APIS.bitpay, + key: 'a-custom-key' + }; + + expect(() => { + overwriteDefaultExplorers([fixtureExplorer], [BitpayAPI, BlockcypherAPI]); + }).toThrow('Property keyPropertyName is not set for bitpay. Cannot pass the key property to the service.'); + }); + }); + }); }); describe('given it was passed no default explorer match', function () {