added tests for api keys

This commit is contained in:
Eslam A. Hefnawy 2016-07-21 23:57:27 +09:00
parent bbe02fd3d3
commit 0a0de5fc89
7 changed files with 120 additions and 3 deletions

View File

@ -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)

View File

@ -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');
}

View File

@ -150,6 +150,8 @@ module.exports = {
methodTemplateJson.DependsOn = AuthorizerLogicalId;
}
if (event.http.private) methodTemplateJson.Properties.ApiKeyRequired = true;
const methodObject = {
[`${normalizedMethod}MethodApigEvent${extractedResourceId}`]:
methodTemplateJson,

View File

@ -3,6 +3,7 @@
require('./index');
require('./validate');
require('./restApi');
require('./apiKeys');
require('./resources');
require('./methods');
require('./authorizers');

View File

@ -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);
});
});

View File

@ -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: [

View File

@ -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:${