diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index fde827ffc..64a090d30 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -271,10 +271,10 @@ class AwsProvider { // TODO: Add listeners, put Debug statments here... // req.on('send', function (r) {console.log(r)}); - return BbPromise.fromCallback(cb => { + const promise = req.promise ? req.promise() : BbPromise.fromCallback(cb => { req.send(cb); - }) - .catch(err => { + }); + return promise.catch(err => { let message = err.message; if (err.message === 'Missing credentials in config') { const errorMessage = [ diff --git a/lib/plugins/aws/provider/awsProvider.test.js b/lib/plugins/aws/provider/awsProvider.test.js index d736f8314..914f110b8 100644 --- a/lib/plugins/aws/provider/awsProvider.test.js +++ b/lib/plugins/aws/provider/awsProvider.test.js @@ -98,7 +98,7 @@ describe('AwsProvider', () => { '${opt:stage, \'prod\'}', ]; stages.forEach(stage => { - it(`should not throw an error before variable population + it(`should not throw an error before variable population even if http event is present and stage is ${stage}`, () => { const config = { stage, @@ -286,6 +286,39 @@ describe('AwsProvider', () => { }); }); + it('should call correct aws method with a promise', () => { + // mocking API Gateway for testing + class FakeAPIGateway { + constructor(credentials) { + this.credentials = credentials; + } + + getRestApis() { + return { + promise: () => BbPromise.resolve({ called: true }), + }; + } + } + awsProvider.sdk = { + APIGateway: FakeAPIGateway, + }; + awsProvider.serverless.service.environment = { + vars: {}, + stages: { + dev: { + vars: { + profile: 'default', + }, + regions: {}, + }, + }, + }; + + return awsProvider.request('APIGateway', 'getRestApis', {}).then(data => { + expect(data.called).to.equal(true); + }); + }); + it('should request to the specified region if region in options set', () => { // mocking S3 for testing @@ -316,7 +349,7 @@ describe('AwsProvider', () => { }, }, }; - expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region }); + expect(awsProvider.getCredentials().region).to.eql(options.region); return awsProvider .request('CloudFormation', @@ -326,7 +359,7 @@ describe('AwsProvider', () => { .then(data => { expect(data).to.eql({ region: 'ap-northeast-1' }); // Requesting different region should not affect region in credentials - expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region }); + expect(awsProvider.getCredentials().region).to.eql(options.region); }); }); it('should retry if error code is 429', (done) => {