From 0a0de5fc8992dfa7014686d148dee7e68428b119 Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Thu, 21 Jul 2016 23:57:27 +0900 Subject: [PATCH] added tests for api keys --- .../deploy/compile/events/apiGateway/index.js | 3 + .../compile/events/apiGateway/lib/apiKeys.js | 9 +- .../compile/events/apiGateway/lib/methods.js | 2 + .../compile/events/apiGateway/tests/all.js | 1 + .../events/apiGateway/tests/apiKeys.js | 95 +++++++++++++++++++ .../compile/events/apiGateway/tests/index.js | 1 + .../events/apiGateway/tests/methods.js | 12 +++ 7 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/index.js b/lib/plugins/aws/deploy/compile/events/apiGateway/index.js index 1c2be55dd..ccd419cb1 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/index.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/index.js @@ -5,6 +5,7 @@ const forEach = require('lodash').forEach; const validate = require('./lib/validate'); const compileRestApi = require('./lib/restApi'); +const compileApiKeys = require('./lib/apiKeys'); const compileResources = require('./lib/resources'); const compileMethods = require('./lib/methods'); const compileAuthorizers = require('./lib/authorizers'); @@ -21,6 +22,7 @@ class AwsCompileApigEvents { this, validate, compileRestApi, + compileApiKeys, compileResources, compileMethods, compileAuthorizers, @@ -43,6 +45,7 @@ class AwsCompileApigEvents { return BbPromise.bind(this) .then(this.validate) .then(this.compileRestApi) + .then(this.compileApiKeys) .then(this.compileResources) .then(this.compileMethods) .then(this.compileAuthorizers) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js index 11e77c378..c3a40c99d 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js @@ -5,10 +5,13 @@ const BbPromise = require('bluebird'); module.exports = { compileApiKeys() { - if (typeof this.serverless.service.provider === 'object' && - this.serverless.service.provider.apiKeys) { + if (this.serverless.service.provider.apiKeys) { + if (this.serverless.service.provider.apiKeys.constructor !== Array) { + throw new this.serverless.classes.Error('apiKeys property must be an array'); + } + this.serverless.service.provider.apiKeys.forEach((apiKey, index) => { - if (typeof apikey !== 'string') { + if (typeof apiKey !== 'string') { throw new this.serverless.classes.Error('API Keys must be strings'); } diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js index 66da00f83..5c70d5827 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js @@ -150,6 +150,8 @@ module.exports = { methodTemplateJson.DependsOn = AuthorizerLogicalId; } + if (event.http.private) methodTemplateJson.Properties.ApiKeyRequired = true; + const methodObject = { [`${normalizedMethod}MethodApigEvent${extractedResourceId}`]: methodTemplateJson, diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/all.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/all.js index ae2f47a85..dd9f7bdc4 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/all.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/all.js @@ -3,6 +3,7 @@ require('./index'); require('./validate'); require('./restApi'); +require('./apiKeys'); require('./resources'); require('./methods'); require('./authorizers'); diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js new file mode 100644 index 000000000..37e7f52fe --- /dev/null +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js @@ -0,0 +1,95 @@ +'use strict'; + +const expect = require('chai').expect; +const AwsCompileApigEvents = require('../index'); +const Serverless = require('../../../../../../../Serverless'); + +describe('#compileApiKeys()', () => { + let serverless; + let awsCompileApigEvents; + + beforeEach(() => { + serverless = new Serverless(); + serverless.service.service = 'first-service'; + serverless.service.provider = { + name: 'aws', + apiKeys: ['1234567890'], + }; + serverless.service.resources = { Resources: {} }; + serverless.service.environment = { + stages: { + dev: { + regions: { + 'us-east-1': { + vars: { + iamRoleArnLambda: + 'arn:aws:iam::12345678:role/service-dev-IamRoleLambda-FOO12345678', + }, + }, + }, + }, + }, + }; + const options = { + stage: 'dev', + region: 'us-east-1', + }; + awsCompileApigEvents = new AwsCompileApigEvents(serverless, options); + awsCompileApigEvents.serverless.service.functions = { + first: { + events: [ + { + http: { + path: 'users/create', + method: 'POST', + private: true, + }, + }, + ], + }, + }; + }); + + it('should compile api key resource', () => awsCompileApigEvents + .compileApiKeys().then(() => { + expect( + awsCompileApigEvents.serverless.service.resources.Resources.ApiKeyApigEvent0.Type + ).to.equal('AWS::ApiGateway::ApiKey'); + + expect( + awsCompileApigEvents.serverless.service.resources.Resources.ApiKeyApigEvent0.Properties + .Enabled + ).to.equal(true); + + expect( + awsCompileApigEvents.serverless.service.resources.Resources.ApiKeyApigEvent0.Properties + .Name + ).to.equal('1234567890'); + + expect( + awsCompileApigEvents.serverless.service.resources.Resources.ApiKeyApigEvent0.Properties + .StageKeys[0].RestApiId.Ref + ).to.equal('RestApiApigEvent'); + + expect( + awsCompileApigEvents.serverless.service.resources.Resources.ApiKeyApigEvent0.Properties + .StageKeys[0].StageName + ).to.equal('dev'); + + expect( + awsCompileApigEvents.serverless.service.resources.Resources.ApiKeyApigEvent0.Properties + .StageKeys[0].RestApiId.Ref + ).to.equal('RestApiApigEvent'); + }) + ); + + it('throw error if apiKey property is not an array', () => { + awsCompileApigEvents.serverless.service.provider.apiKeys = 2; + expect(() => awsCompileApigEvents.compileApiKeys()).to.throw(Error); + }); + + it('throw error if an apiKey is not a string', () => { + awsCompileApigEvents.serverless.service.provider.apiKeys = [2]; + expect(() => awsCompileApigEvents.compileApiKeys()).to.throw(Error); + }); +}); diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/index.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/index.js index 86f0a4c0d..e76b23e5d 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/index.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/index.js @@ -9,6 +9,7 @@ const Serverless = require('../../../../../../../Serverless'); describe('AwsCompileApigEvents', () => { const serverless = new Serverless(); serverless.service.resources = { Resources: {} }; + serverless.service.provider = { name: 'aws' }; serverless.service.functions = { first: { events: [ diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js index ff32e7d86..eb14853c7 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js @@ -149,6 +149,18 @@ describe('#compileMethods()', () => { }); }); + it('should set api key as required if private endpoint', () => { + awsCompileApigEvents.serverless.service.functions + .first.events[0].http.private = true; + + return awsCompileApigEvents.compileMethods().then(() => { + expect( + awsCompileApigEvents.serverless.service.resources.Resources + .PostMethodApigEvent0.Properties.ApiKeyRequired + ).to.equal(true); + }); + }); + it('should set the correct lambdaUri', () => { const lambdaUri = `arn:aws:apigateway:${ awsCompileApigEvents.options.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${