Skip to content

Commit

Permalink
feat(client/config): add way to store credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac authored and nikku committed Nov 15, 2019
1 parent c524dad commit 8848acc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
27 changes: 27 additions & 0 deletions client/src/remote/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
74 changes: 74 additions & 0 deletions client/src/remote/__tests__/ConfigSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});

});

0 comments on commit 8848acc

Please sign in to comment.