Skip to content

Commit

Permalink
Remove implicit test dependency on keytar presence (#64)
Browse files Browse the repository at this point in the history
Adds tests for getting credentials with a keytar mock.
  • Loading branch information
childish-sambino authored Sep 5, 2019
1 parent 81d9348 commit 9694751
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/base-commands/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const DEFAULT_LOG_LEVEL = 'info';
const DEFAULT_OUTPUT_FORMAT = 'columns';

class BaseCommand extends Command {
constructor(argv, config, secureStorage) {
constructor(argv, config) {
super(argv, config);
this.configFile = new Config('');
this.userConfig = new ConfigData();
this.secureStorage = secureStorage || new SecureStorage();
this.secureStorage = new SecureStorage();
}

get inquirer() {
Expand Down
24 changes: 10 additions & 14 deletions test/base-commands/twilio-client-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,16 @@ describe('base-commands', () => {
.twilioCliEnv(Config)
.stderr()
.do(async ctx => {
ctx.testCmd = new CommandClass(
args,
ctx.fakeConfig,
mockSecureStorage ?
{
async getCredentials(profileId) {
return {
apiKey: constants.FAKE_API_KEY,
apiSecret: constants.FAKE_API_SECRET + profileId
};
}
} :
undefined
);
ctx.testCmd = new CommandClass(args, ctx.fakeConfig);
ctx.testCmd.secureStorage =
{
async getCredentials(profileId) {
return {
apiKey: mockSecureStorage ? constants.FAKE_API_KEY : 'error',
apiSecret: constants.FAKE_API_SECRET + profileId
};
}
};

// This is essentially what oclif does behind the scenes.
try {
Expand Down
32 changes: 32 additions & 0 deletions test/services/secure-storage.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { expect, test } = require('@twilio/cli-test');
const { SecureStorage, STORAGE_LOCATIONS } = require('../../src/services/secure-storage');
const { TwilioCliError } = require('../../src/services/error');

describe('services', () => {
describe('secure-storage', () => {
Expand Down Expand Up @@ -27,5 +28,36 @@ describe('services', () => {
const secureStorage = new SecureStorage('openbsd');
expect(secureStorage.storageLocation).to.eq(undefined);
});

describe('getCredentials', () => {
const setup = getPassword => test
.do(ctx => {
ctx.secureStorage = new SecureStorage();
Object.defineProperty(ctx.secureStorage, 'keytar', {
get: () => ({ getPassword })
});
});

setup(async () => 'key|password')
.it('handles a key-password pair', async ctx => {
const expected = { apiKey: 'key', apiSecret: 'password' };
expect(await ctx.secureStorage.getCredentials('Pro File')).to.eql(expected);
});

setup(async () => {
throw new Error('WHOA!');
})
.it('handles a keytar error', async ctx => {
const expected = { apiKey: 'error', apiSecret: 'WHOA!' };
expect(await ctx.secureStorage.getCredentials('No File')).to.eql(expected);
});

setup(async () => {
throw new TwilioCliError('WOE!');
})
.do(ctx => ctx.secureStorage.getCredentials('Woe File'))
.catch(/WOE!/)
.it('passes a TwilioCliError error through');
});
});
});

0 comments on commit 9694751

Please sign in to comment.