Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/commands/profiles/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ class ProfilesCreate extends BaseCommand {
const twilioClient = this.getTwilioClient();
try {
// Don't log the response since it contains the secret.
apiKey = await twilioClient.newKeys.create({ friendlyName: apiKeyFriendlyName });
apiKey = await twilioClient.iam.v1.newApiKey.create({
accountSid: this.accountSid,
friendlyName: apiKeyFriendlyName,
});
} catch (error) {
this.logger.debug(error);
throw new TwilioCliError('Could not create an API Key.');
Expand Down
40 changes: 28 additions & 12 deletions test/commands/profiles/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,31 @@ describe('commands', () => {
ctx.testCmd.inquirer.prompt = fakePrompt;
});

const mockSuccess = (api) => {
const mockCredentialValidation = (api) => {
api.get(`/2010-04-01/Accounts/${constants.FAKE_ACCOUNT_SID}.json`).reply(200, {
sid: constants.FAKE_ACCOUNT_SID,
});
api.post(`/2010-04-01/Accounts/${constants.FAKE_ACCOUNT_SID}/Keys.json`).reply(200, {
sid: constants.FAKE_API_KEY,
secret: constants.FAKE_API_SECRET,
});
};

const mockKeyCreation = (api) => {
api
.post('/v1/Keys', (body) => {
const params = typeof body === 'string' ? Object.fromEntries(new URLSearchParams(body)) : body;
return params.AccountSid === constants.FAKE_ACCOUNT_SID && Boolean(params.FriendlyName);
})
.reply(201, {
sid: constants.FAKE_API_KEY,
secret: constants.FAKE_API_SECRET,
});
};

afterEach(() => {
sinon.restore();
});

createTest()
.nock('https://api.twilio.com', mockSuccess)
.nock('https://api.twilio.com', mockCredentialValidation)
.nock('https://iam.twilio.com', mockKeyCreation)
.do((ctx) => ctx.testCmd.run())
.it('runs profiles:create', (ctx) => {
expect(ctx.stdout).to.equal('');
Expand All @@ -70,7 +79,8 @@ describe('commands', () => {
});

createTest([], { profileId: 'profile1', addProjects: ['profile1'] })
.nock('https://api.twilio.com', mockSuccess)
.nock('https://api.twilio.com', mockCredentialValidation)
.nock('https://iam.twilio.com', mockKeyCreation)
.do((ctx) => ctx.testCmd.run())
.it('runs profiles:create with existing profile in Projects', (ctx) => {
expect(ctx.stdout).to.equal('');
Expand All @@ -84,7 +94,8 @@ describe('commands', () => {
});

createTest([], { profileId: 'profile1', addProjects: ['profile1'], removeCred: false })
.nock('https://api.twilio.com', mockSuccess)
.nock('https://api.twilio.com', mockCredentialValidation)
.nock('https://iam.twilio.com', mockKeyCreation)
.do((ctx) => ctx.testCmd.run())
.it('runs profiles:create with existing profile in Projects with Keytar remove failed', (ctx) => {
expect(ctx.stdout).to.equal('');
Expand Down Expand Up @@ -148,7 +159,8 @@ describe('commands', () => {
.it('fails for invalid Auth Tokens');

createTest(['--skip-parameter-validation'])
.nock('https://api.twilio.com', mockSuccess)
.nock('https://api.twilio.com', mockCredentialValidation)
.nock('https://iam.twilio.com', mockKeyCreation)
.do((ctx) => {
const fakePrompt = ctx.testCmd.inquirer.prompt;
fakePrompt.onFirstCall().resolves({
Expand Down Expand Up @@ -200,7 +212,9 @@ describe('commands', () => {
api.get(`/2010-04-01/Accounts/${constants.FAKE_ACCOUNT_SID}.json`).reply(200, {
sid: constants.FAKE_ACCOUNT_SID,
});
api.post(`/2010-04-01/Accounts/${constants.FAKE_ACCOUNT_SID}/Keys.json`).reply(500, {
})
.nock('https://iam.twilio.com', (api) => {
api.post('/v1/Keys').reply(500, {
error: 'oops',
});
})
Expand All @@ -209,15 +223,17 @@ describe('commands', () => {
.it('fails to create an API key');

createTest(['--region', 'dev', '--edge', 'sydney'])
.nock('https://api.sydney.dev.twilio.com', mockSuccess)
.nock('https://api.sydney.dev.twilio.com', mockCredentialValidation)
.nock('https://iam.sydney.dev.twilio.com', mockKeyCreation)
.do(async (ctx) => ctx.testCmd.run())
.it('supports other regions', (ctx) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.contain('configuration saved');
});

createTest(['--region', 'unknown-region'])
.nock('https://api.unknown-region.twilio.com', mockSuccess)
.nock('https://api.unknown-region.twilio.com', mockCredentialValidation)
.nock('https://iam.unknown-region.twilio.com', mockKeyCreation)
.do(async (ctx) => ctx.testCmd.run())
.it('allows unmapped regions without edge', (ctx) => {
expect(ctx.stdout).to.equal('');
Expand Down