diff --git a/client/src/remote/Config.js b/client/src/remote/Config.js index 9871aa4de6..278988ff37 100644 --- a/client/src/remote/Config.js +++ b/client/src/remote/Config.js @@ -166,4 +166,31 @@ export default class Config { return configForPlugin; } + async getCredentials(service, account, defaultValue = null) { + const credentials = await this.get('credentials') || {}; + + const configForService = credentials[ service ]; + + if (!configForService) { + return defaultValue; + } + + const value = configForService[ account ]; + + if (isNil(value)) { + return defaultValue; + } + + return value; + } + + async setCredentials(service, account, value) { + const credentials = await this.get('credentials') || {}; + + const configForService = credentials[ service ] = credentials[ service ] || {}; + + configForService[ account ] = value; + + return this.set('credentials', credentials); + } } diff --git a/client/src/remote/__tests__/ConfigSpec.js b/client/src/remote/__tests__/ConfigSpec.js index 8e100c884a..7403a05f30 100644 --- a/client/src/remote/__tests__/ConfigSpec.js +++ b/client/src/remote/__tests__/ConfigSpec.js @@ -315,4 +315,78 @@ describe('config', function() { }); + + describe('#getCredentials', function() { + + it('should get', async function() { + + // given + const credentialsConfig = { + service: { + account: { + username: 'demo', + password: 'demo' + } + } + }; + backend.setSendResponse(credentialsConfig); + + // when + const credentialsFromConfig = await config.getCredentials('service', 'account'); + + // then + expect(credentialsFromConfig).to.equal(credentialsConfig.service.account); + }); + + + it('should return default value if no credentials stored', async function() { + + // given + const credentials = { + username: 'demo', + password: 'demo' + }; + backend.setSendResponse(null); + + // when + const credentialsFromConfig = await config.getCredentials('service', 'account', credentials); + + // then + expect(credentialsFromConfig).to.equal(credentials); + }); + + + it('should return default value if no credentials for account stored', async function() { + + // given + const credentials = { + username: 'demo', + password: 'demo' + }; + backend.setSendResponse({ service: {} }); + + // when + const credentialsFromConfig = await config.getCredentials('service', 'account', credentials); + + // then + expect(credentialsFromConfig).to.equal(credentials); + }); + }); + + + describe('#setCredentials', function() { + + it('should set', async function() { + + // given + backend.setSendResponse(null); + + // when + await config.setCredentials('service', 'account', 'foo'); + + // then + // expect not to fail + }); + }); + }); \ No newline at end of file