diff --git a/config/Config.js b/config/Config.js index 8283341..25a97ad 100644 --- a/config/Config.js +++ b/config/Config.js @@ -13,6 +13,7 @@ class Config { CLIENT_ID_ENV: 'CLIENT_ID', PASSWORD_ENV: 'PASSWORD', USER_KEY_ENV: 'USER_KEY', + SERVICE_ACCOUNT_ID_ENV: 'SERVICE_ACCOUNT_ID', SUBSCRIPTION_ID_ENV: 'SUBSCRIPTION_ID', EXTRACTION_API_HOST_ENV: 'EXTRACTION_API_HOST', EXTRACTION_API_HOST_DEFAULT: 'https://api.dowjones.com', @@ -56,6 +57,11 @@ class Config { } } + // NOTE: "service_account_id" is a legacy, alternate name for "user_key" from the customer's perspective + if (!accountCreds.user_key && accountCreds.service_account_id) { + accountCreds.user_key = accountCreds.service_account_id; + } + return this._areCredsSet(accountCreds) ? accountCreds : new Error( 'Error: No account credentials specified\n' + 'Must specify user_id, client_id, and password as args to Listener constructor, env vars, or via customerConfig.json file\n' + @@ -68,12 +74,13 @@ class Config { user_id: process.env[this.Constants.USER_ID_ENV], client_id: process.env[this.Constants.CLIENT_ID_ENV], password: process.env[this.Constants.PASSWORD_ENV], - user_key: process.env[this.Constants.USER_KEY_ENV] + user_key: process.env[this.Constants.USER_KEY_ENV], + service_account_id: process.env[this.Constants.SERVICE_ACCOUNT_ID_ENV] }; } _areCredsSet(accountCreds) { - return accountCreds && (accountCreds.user_key || (accountCreds.user_id && accountCreds.client_id && accountCreds.password)); + return accountCreds && (accountCreds.user_key || accountCreds.service_account_id || (accountCreds.user_id && accountCreds.client_id && accountCreds.password)); } } diff --git a/config/ConfigFileUtil.js b/config/ConfigFileUtil.js index 2f561f2..5bfce5f 100644 --- a/config/ConfigFileUtil.js +++ b/config/ConfigFileUtil.js @@ -47,6 +47,7 @@ class ConfigFileUtil { return { user_key: _.trim(this.config.user_key), + service_account_id: _.trim(this.config.service_account_id), user_id: _.trim(this.config.user_id), client_id: _.trim(this.config.client_id), password: _.trim(this.config.password) diff --git a/tests/config/testCustomerConfigServiceAccountId.json b/tests/config/testCustomerConfigServiceAccountId.json new file mode 100644 index 0000000..ab2b9d6 --- /dev/null +++ b/tests/config/testCustomerConfigServiceAccountId.json @@ -0,0 +1,7 @@ +{ + "service_account_id": "foo", + "user_id": "account@account.com", + "client_id": "clientID123", + "password": "Password123", + "subscription_id": "bar" +} \ No newline at end of file diff --git a/tests/configFileUtilTests.js b/tests/configFileUtilTests.js index b59ce73..7250bf2 100644 --- a/tests/configFileUtilTests.js +++ b/tests/configFileUtilTests.js @@ -6,6 +6,7 @@ const path = require('path'); describe('configFileUtil', () => { let sandbox; const pathConfig = path.join(__dirname, './config/testCustomerConfig.json'); + const serviceAccountIdPathConfig = path.join(__dirname, './config/testCustomerConfigServiceAccountId.json'); beforeEach(() => { sandbox = sinon.sandbox.create(); @@ -28,6 +29,19 @@ describe('configFileUtil', () => { expect(userKey).toBe('foo'); }); + it('should get the correct customer user key when set with service_account_id param.', () => { + // Arrange + const configFileUtil = new ConfigFileUtil(); + configFileUtil.setConfigFilePath(serviceAccountIdPathConfig); + + // Act + const creds = configFileUtil.getAccountCredentials(); + const userKey = creds.service_account_id; + + // Assert + expect(userKey).toBe('foo'); + }); + it('should get the correct customer service account credentials.', () => { // Arrange const configFileUtil = new ConfigFileUtil(); diff --git a/tests/configTests.js b/tests/configTests.js index 102e41a..31c685c 100644 --- a/tests/configTests.js +++ b/tests/configTests.js @@ -49,6 +49,19 @@ describe('config', () => { expect(actualUserKey).toBe(expectedUserKey); }); + it('should prioritize service_account_id from env over service account creds in config file', () => { + // Arrange + const expectedUserKey = '123A'; + process.env[config.Constants.SERVICE_ACCOUNT_ID_ENV] = expectedUserKey; + + // Act + const creds = config.getAccountCredentials(); + const actualUserKey = creds.user_key; + + // Assert + expect(actualUserKey).toBe(expectedUserKey); + }); + it('should prioritize credentials set thru env vars over config file', () => { // Arrange const expectedUserId = 'userId'; @@ -109,6 +122,20 @@ describe('config', () => { expect(accountCreds.user_id).toBe(undefined); }); + it('should prioritize service_account_id param over service account credentials in config file', () => { + // Arrange + const userKeyExpected = '123'; + const configSpecial = new Config({ service_account_id: userKeyExpected }); + + // Act + const accountCreds = configSpecial.getAccountCredentials(); + const userKeyActual = accountCreds.user_key; + + // Assert + expect(userKeyExpected).toBe(userKeyActual); + expect(accountCreds.user_id).toBe(undefined); + }); + it('should get the correct subscription ID from config file if no overrides present.', () => { // Arrange const expectedSub = 'bar';