Skip to content

Commit a0adead

Browse files
committed
add some tests
1 parent 786d382 commit a0adead

File tree

4 files changed

+277
-11
lines changed

4 files changed

+277
-11
lines changed

lib/deploy/events/apiGateway/methods.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ module.exports = {
1010
this.pluginhttpValidated.events.forEach((event) => {
1111
const resourceId = this.getResourceId(event.http.path);
1212
const resourceName = this.getResourceName(event.http.path);
13-
const requestParameters = (event.http.request && event.http.request.parameters) || {};
1413

1514
const template = {
1615
Type: 'AWS::ApiGateway::Method',
1716
Properties: {
1817
HttpMethod: event.http.method.toUpperCase(),
19-
RequestParameters: requestParameters,
18+
RequestParameters: {},
2019
AuthorizationType: 'NONE',
2120
ResourceId: resourceId,
2221
RestApiId: { Ref: this.apiGatewayRestApiLogicalId },

lib/deploy/events/apiGateway/methods.test.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Serverless = require('serverless/lib/Serverless');
55
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
66
const ServerlessStepFunctions = require('./../../../index');
77

8-
describe('#compileMethods()', () => {
8+
describe('#methods()', () => {
99
let serverless;
1010
let serverlessStepFunctions;
1111

@@ -38,11 +38,27 @@ describe('#compileMethods()', () => {
3838
};
3939
});
4040

41-
it('should create a method resource', () => serverlessStepFunctions
42-
.compileMethods().then(() => {
43-
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
44-
.Resources)
45-
.to.have.property('ApiGatewayMethodapiGatewayResourceNamesPost');
46-
})
47-
);
41+
describe('#compileMethods()', () => {
42+
it('should create a method resource', () => serverlessStepFunctions
43+
.compileMethods().then(() => {
44+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
45+
.Resources)
46+
.to.have.property('ApiGatewayMethodapiGatewayResourceNamesPost');
47+
})
48+
);
49+
});
50+
51+
describe('#getMethodIntegration()', () => {
52+
it('should return a corresponding Integration resource', () => {
53+
expect(serverlessStepFunctions.getMethodIntegration('stateMachine').Properties)
54+
.to.have.property('Integration');
55+
});
56+
});
57+
58+
describe('#getMethodResponses()', () => {
59+
it('should return a corresponding methodResponses resource', () => {
60+
expect(serverlessStepFunctions.getMethodResponses().Properties)
61+
.to.have.property('MethodResponses');
62+
});
63+
});
4864
});

lib/deploy/events/apiGateway/resources.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ module.exports = {
5151
}
5252
return resourcePaths;
5353
}, []);
54-
// (stable) sort so that parents get processed before children
5554
return _.sortBy(paths, path => path.split('/').length);
5655
},
5756

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const Serverless = require('serverless/lib/Serverless');
5+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
6+
const ServerlessStepFunctions = require('./../../../index');
7+
8+
describe('#compileResources()', () => {
9+
let serverless;
10+
let serverlessStepFunctions;
11+
12+
beforeEach(() => {
13+
serverless = new Serverless();
14+
serverless.setProvider('aws', new AwsProvider(serverless));
15+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
16+
serverlessStepFunctions = new ServerlessStepFunctions(serverless);
17+
serverlessStepFunctions.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
18+
serverlessStepFunctions.pluginhttpValidated = {};
19+
});
20+
21+
// sorted makes parent refs easier
22+
it('should construct the correct (sorted) resourcePaths array', () => {
23+
serverlessStepFunctions.pluginhttpValidated.events = [
24+
{
25+
http: {
26+
path: '',
27+
method: 'GET',
28+
},
29+
},
30+
{
31+
http: {
32+
path: 'foo/bar',
33+
method: 'POST',
34+
},
35+
},
36+
{
37+
http: {
38+
path: 'bar/-',
39+
method: 'GET',
40+
},
41+
},
42+
{
43+
http: {
44+
path: 'bar/foo',
45+
method: 'GET',
46+
},
47+
},
48+
{
49+
http: {
50+
path: 'bar/{id}/foobar',
51+
method: 'GET',
52+
},
53+
},
54+
{
55+
http: {
56+
path: 'bar/{id}',
57+
method: 'GET',
58+
},
59+
},
60+
{
61+
http: {
62+
path: 'bar/{foo_id}',
63+
method: 'GET',
64+
},
65+
},
66+
{
67+
http: {
68+
path: 'bar/{foo_id}/foobar',
69+
method: 'GET',
70+
},
71+
},
72+
];
73+
expect(serverlessStepFunctions.getResourcePaths()).to.deep.equal([
74+
'foo',
75+
'bar',
76+
'foo/bar',
77+
'bar/-',
78+
'bar/foo',
79+
'bar/{id}',
80+
'bar/{foo_id}',
81+
'bar/{id}/foobar',
82+
'bar/{foo_id}/foobar',
83+
]);
84+
});
85+
86+
it('should reference the appropriate ParentId', () => {
87+
serverlessStepFunctions.pluginhttpValidated.events = [
88+
{
89+
http: {
90+
path: 'foo/bar',
91+
method: 'POST',
92+
},
93+
},
94+
{
95+
http: {
96+
path: 'bar/-',
97+
method: 'GET',
98+
},
99+
},
100+
{
101+
http: {
102+
path: 'bar/foo',
103+
method: 'GET',
104+
},
105+
},
106+
{
107+
http: {
108+
path: 'bar/{id}/foobar',
109+
method: 'GET',
110+
},
111+
},
112+
{
113+
http: {
114+
path: 'bar/{id}',
115+
method: 'GET',
116+
},
117+
},
118+
];
119+
return serverlessStepFunctions.compileResources().then(() => {
120+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
121+
.Resources.ApiGatewayResourceFoo.Properties.ParentId['Fn::GetAtt'][0])
122+
.to.equal('ApiGatewayRestApi');
123+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
124+
.Resources.ApiGatewayResourceFoo.Properties.ParentId['Fn::GetAtt'][1])
125+
.to.equal('RootResourceId');
126+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
127+
.Resources.ApiGatewayResourceFooBar.Properties.ParentId.Ref)
128+
.to.equal('ApiGatewayResourceFoo');
129+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
130+
.Resources.ApiGatewayResourceBarIdVar.Properties.ParentId.Ref)
131+
.to.equal('ApiGatewayResourceBar');
132+
});
133+
});
134+
135+
it('should construct the correct resourceLogicalIds object', () => {
136+
serverlessStepFunctions.pluginhttpValidated.events = [
137+
{
138+
http: {
139+
path: '',
140+
method: 'POST',
141+
},
142+
},
143+
{
144+
http: {
145+
path: 'foo',
146+
method: 'GET',
147+
},
148+
},
149+
{
150+
http: {
151+
path: 'foo/{foo_id}/bar',
152+
method: 'GET',
153+
},
154+
},
155+
{
156+
http: {
157+
path: 'baz/foo',
158+
method: 'GET',
159+
},
160+
},
161+
];
162+
return serverlessStepFunctions.compileResources().then(() => {
163+
expect(serverlessStepFunctions.apiGatewayResourceLogicalIds).to.deep.equal({
164+
baz: 'ApiGatewayResourceBaz',
165+
'baz/foo': 'ApiGatewayResourceBazFoo',
166+
foo: 'ApiGatewayResourceFoo',
167+
'foo/{foo_id}': 'ApiGatewayResourceFooFooidVar',
168+
'foo/{foo_id}/bar': 'ApiGatewayResourceFooFooidVarBar',
169+
});
170+
});
171+
});
172+
173+
it('should construct resourceLogicalIds that do not collide', () => {
174+
serverlessStepFunctions.pluginhttpValidated.events = [
175+
{
176+
http: {
177+
path: 'foo/bar',
178+
method: 'POST',
179+
},
180+
},
181+
{
182+
http: {
183+
path: 'foo/{bar}',
184+
method: 'GET',
185+
},
186+
},
187+
];
188+
return serverlessStepFunctions.compileResources().then(() => {
189+
expect(serverlessStepFunctions.apiGatewayResourceLogicalIds).to.deep.equal({
190+
foo: 'ApiGatewayResourceFoo',
191+
'foo/bar': 'ApiGatewayResourceFooBar',
192+
'foo/{bar}': 'ApiGatewayResourceFooBarVar',
193+
});
194+
});
195+
});
196+
197+
it('should set the appropriate Pathpart', () => {
198+
serverlessStepFunctions.pluginhttpValidated.events = [
199+
{
200+
http: {
201+
path: 'foo/{bar}',
202+
method: 'GET',
203+
},
204+
},
205+
{
206+
http: {
207+
path: 'foo/bar',
208+
method: 'GET',
209+
},
210+
},
211+
{
212+
http: {
213+
path: 'foo/{bar}/baz',
214+
method: 'GET',
215+
},
216+
},
217+
];
218+
return serverlessStepFunctions.compileResources().then(() => {
219+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
220+
.Resources.ApiGatewayResourceFooBar.Properties.PathPart)
221+
.to.equal('bar');
222+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
223+
.Resources.ApiGatewayResourceFooBarVar.Properties.PathPart)
224+
.to.equal('{bar}');
225+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
226+
.Resources.ApiGatewayResourceFooBarVarBaz.Properties.PathPart)
227+
.to.equal('baz');
228+
});
229+
});
230+
231+
it('should handle root resource references', () => {
232+
serverlessStepFunctions.pluginhttpValidated.events = [
233+
{
234+
http: {
235+
path: '',
236+
method: 'GET',
237+
},
238+
},
239+
];
240+
return serverlessStepFunctions.compileResources().then(() => {
241+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
242+
.Resources).to.deep.equal({});
243+
});
244+
});
245+
246+
describe('#getResourceName()', () => {
247+
it('should return empty if empty string gives to argument', () => {
248+
expect(serverlessStepFunctions.getResourceName(''))
249+
.to.equal('');
250+
});
251+
});
252+
});

0 commit comments

Comments
 (0)