added tests for compile resources

This commit is contained in:
Eslam A. Hefnawy 2016-06-14 13:12:33 +02:00 committed by Philipp Muens
parent 01afb8aefe
commit 7dfd5d938a
3 changed files with 57 additions and 32 deletions

View File

@ -6,7 +6,7 @@ const validateInput = require('./lib/validateInput');
const compileRestApi = require('./lib/compileRestApi');
const compileDeployment = require('./lib/compileDeployment');
const compileStage = require('./lib/compileStage');
const compileResource = require('./lib/compileResource');
const compileResources = require('./lib/compileResources');
const compileMethod = require('./lib/compileMethod');
const compilePermission = require('./lib/compilePermission');
@ -21,7 +21,7 @@ class AwsCompileApigEvents {
compileRestApi,
compileDeployment,
compileStage,
compileResource,
compileResources,
compileMethod,
compilePermission
);
@ -32,7 +32,7 @@ class AwsCompileApigEvents {
.then(this.compileRestApi)
.then(this.compileDeployment)
.then(this.compileStage)
.then(this.compileResource)
.then(this.compileResources)
.then(this.compileMethod)
.then(this.compilePermission),
};

View File

@ -4,8 +4,10 @@ const BbPromise = require('bluebird');
const _ = require('lodash');
module.exports = {
compileResource() {
compileResources() {
this.resourcePaths = [];
this.resourceLogicalIds = {};
_.forEach(this.serverless.service.functions, (functionObj) => {
_.forEach(functionObj.events.aws.http_endpoints, (pathParam) => {
let path = pathParam;
@ -29,19 +31,25 @@ module.exports = {
const resourcePath = path;
const resourceIndex = this.resourcePaths.indexOf(resourcePath);
const resourceLogicalId = `ApigResource${resourceIndex}`;
this.resourceLogicalIds[resourcePath] = resourceLogicalId;
resourcesArray.pop();
const resourceParentPath = resourcesArray.join('/');
const resourceParentIndex = this.resourcePaths.indexOf(resourceParentPath);
const resourceParentLogicalId = `ApigResource${resourceParentIndex}`;
let resourceParentId;
if (resourcesArray.length === 0) {
resourceParentId = '{ "Fn::GetAtt": ["RestApiApigEvent", "RootResourceId"] }';
} else {
const resourceParentPath = resourcesArray.join('/');
const resourceParentIndex = this.resourcePaths.indexOf(resourceParentPath);
resourceParentId = `{ "Ref" : "ApigResource${resourceParentIndex}" }`;
}
const resourceTemplate = `
{
"Type" : "AWS::ApiGateway::Resource",
"Properties" : {
"ParentId" : { "Ref" : "${resourceParentLogicalId}" },
"ParentId" : ${resourceParentId},
"PathPart" : "${resourceName}",
"RestApiId" : { "Ref" : "${resourceParentLogicalId}" }
"RestApiId" : { "Ref" : "RestApiApigEvent" }
}
}
`;

View File

@ -1,37 +1,54 @@
'use strict';
const expect = require('chai').expect;
const compileResources = require('../lib/compileResource');
const AwsCompileApigEvents = require('../awsCompileApigEvents');
const Serverless = require('../../../Serverless');
describe('compileResources', () => {
describe('#awsCompileResources()', () => {
let serverless;
let awsCompileApigEvents;
beforeEach(() => {
compileResources.serverless = {
service: {
functions: {
hello: {
events: {
aws: {
http_endpoints: {
post: 'users/create',
get: 'users/create/list',
},
},
},
},
},
resources: {
serverless = new Serverless();
serverless.init();
serverless.service.resources = { aws: { Resources: {} } };
awsCompileApigEvents = new AwsCompileApigEvents(serverless);
awsCompileApigEvents.serverless.service.functions = {
hello: {
events: {
aws: {
Resources: {},
http_endpoints: {
post: 'foo/bar',
get: 'bar/foo',
},
},
},
},
};
});
it('should construct the correct paths array', () => {
return compileResources.compileResource().then(() => {
console.log(compileResources.paths);
});
});
it('should construct the correct resourcePaths array', () => awsCompileApigEvents
.compileResources().then(() => {
const expectedResourcePaths = ['foo/bar', 'foo', 'bar/foo', 'bar'];
expect(awsCompileApigEvents.resourcePaths).to.deep.equal(expectedResourcePaths);
}));
it('should construct the correct resourceLogicalIds object', () => awsCompileApigEvents
.compileResources().then(() => {
const expectedResourceLogicalIds = {
'foo/bar': 'ApigResource0',
foo: 'ApigResource1',
'bar/foo': 'ApigResource2',
bar: 'ApigResource3',
};
expect(awsCompileApigEvents.resourceLogicalIds).to.deep.equal(expectedResourceLogicalIds);
}));
it('should compile to the correct CF resources', () => awsCompileApigEvents
.compileResources().then(() => {
expect(awsCompileApigEvents.serverless.service.resources.aws.Resources
.ApigResource0.Properties.PathPart).to.equal('bar');
expect(awsCompileApigEvents.serverless.service.resources.aws.Resources
.ApigResource0.Properties.ParentId.Ref).to.equal('ApigResource1');
}));
});