Skip to content

Commit 2a74eed

Browse files
committed
add some tests
1 parent a0adead commit 2a74eed

File tree

3 files changed

+235
-11
lines changed

3 files changed

+235
-11
lines changed

lib/deploy/events/apiGateway/validate.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ module.exports = {
2727
};
2828
},
2929

30-
isCoreHttpevents() {
31-
_.forEach(this.serverless.service.functions, (functionObject) => {
32-
_.forEach(functionObject.events, (event) => {
33-
if (_.has(event, 'http')) {
34-
return true;
35-
}
36-
return false;
37-
});
38-
});
39-
},
40-
4130
getHttp(event, stateMachineName) {
4231
if (typeof event.http === 'object') {
4332
return event.http;
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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('#httpValidate()', () => {
9+
let serverless;
10+
let serverlessStepFunctions;
11+
12+
beforeEach(() => {
13+
serverless = new Serverless();
14+
serverless.setProvider('aws', new AwsProvider(serverless));
15+
const options = {
16+
stage: 'dev',
17+
region: 'us-east-1',
18+
};
19+
serverlessStepFunctions = new ServerlessStepFunctions(serverless, options);
20+
});
21+
22+
it('should ignore non-http events', () => {
23+
serverlessStepFunctions.serverless.service.stepFunctions = {
24+
stateMachines: {
25+
first: {
26+
events: [
27+
{
28+
ignored: {},
29+
},
30+
],
31+
},
32+
},
33+
};
34+
const validated = serverlessStepFunctions.httpValidate();
35+
expect(validated.events).to.be.an('Array').with.length(0);
36+
});
37+
38+
it('should reject an invalid http event', () => {
39+
serverlessStepFunctions.serverless.service.stepFunctions = {
40+
stateMachines: {
41+
first: {
42+
events: [
43+
{
44+
http: true,
45+
},
46+
],
47+
},
48+
},
49+
};
50+
expect(() => serverlessStepFunctions.httpValidate()).to.throw(Error);
51+
});
52+
53+
it('should throw an error if http event type is not a string or an object', () => {
54+
serverlessStepFunctions.serverless.service.stepFunctions = {
55+
stateMachines: {
56+
first: {
57+
events: [
58+
{
59+
http: 42,
60+
},
61+
],
62+
},
63+
},
64+
};
65+
66+
expect(() => serverlessStepFunctions.httpValidate()).to.throw(Error);
67+
});
68+
69+
it('should validate the http events "path" property', () => {
70+
serverlessStepFunctions.serverless.service.stepFunctions = {
71+
stateMachines: {
72+
first: {
73+
events: [
74+
{
75+
http: {
76+
method: 'POST',
77+
},
78+
},
79+
],
80+
},
81+
},
82+
};
83+
84+
expect(() => serverlessStepFunctions.httpValidate()).to.throw(Error);
85+
});
86+
87+
it('should validate the http events "method" property', () => {
88+
serverlessStepFunctions.serverless.service.stepFunctions = {
89+
stateMachines: {
90+
first: {
91+
events: [
92+
{
93+
http: {
94+
path: 'foo/bar',
95+
},
96+
},
97+
],
98+
},
99+
},
100+
};
101+
102+
expect(() => serverlessStepFunctions.httpValidate()).to.throw(Error);
103+
});
104+
105+
it('should validate the http events object syntax method is case insensitive', () => {
106+
serverlessStepFunctions.serverless.service.stepFunctions = {
107+
stateMachines: {
108+
first: {
109+
events: [
110+
{
111+
http: {
112+
method: 'POST',
113+
path: 'foo/bar',
114+
},
115+
},
116+
{
117+
http: {
118+
method: 'post',
119+
path: 'foo/bar',
120+
},
121+
},
122+
],
123+
},
124+
},
125+
};
126+
127+
const validated = serverlessStepFunctions.httpValidate();
128+
expect(validated.events).to.be.an('Array').with.length(2);
129+
});
130+
131+
it('should validate the http events string syntax method is case insensitive', () => {
132+
serverlessStepFunctions.serverless.service.stepFunctions = {
133+
stateMachines: {
134+
first: {
135+
events: [
136+
{
137+
http: 'POST foo/bar',
138+
},
139+
{
140+
http: 'post foo/bar',
141+
},
142+
],
143+
},
144+
},
145+
};
146+
147+
const validated = serverlessStepFunctions.httpValidate();
148+
expect(validated.events).to.be.an('Array').with.length(2);
149+
});
150+
151+
it('should throw an error if the method is invalid', () => {
152+
serverlessStepFunctions.serverless.service.stepFunctions = {
153+
stateMachines: {
154+
first: {
155+
events: [
156+
{
157+
http: {
158+
path: 'foo/bar',
159+
method: 'INVALID',
160+
},
161+
},
162+
],
163+
},
164+
},
165+
};
166+
167+
expect(() => serverlessStepFunctions.httpValidate()).to.throw(Error);
168+
});
169+
170+
it('should filter non-http events', () => {
171+
serverlessStepFunctions.serverless.service.stepFunctions = {
172+
stateMachines: {
173+
first: {
174+
events: [
175+
{
176+
http: {
177+
method: 'GET',
178+
path: 'foo/bar',
179+
},
180+
},
181+
{},
182+
],
183+
},
184+
second: {
185+
events: [
186+
{
187+
other: {},
188+
},
189+
],
190+
},
191+
},
192+
};
193+
194+
const validated = serverlessStepFunctions.httpValidate();
195+
expect(validated.events).to.be.an('Array').with.length(1);
196+
});
197+
198+
it('should discard a starting slash from paths', () => {
199+
serverlessStepFunctions.serverless.service.stepFunctions = {
200+
stateMachines: {
201+
first: {
202+
events: [
203+
{
204+
http: {
205+
method: 'POST',
206+
path: '/foo/bar',
207+
},
208+
},
209+
{
210+
http: 'GET /foo/bar',
211+
},
212+
],
213+
},
214+
},
215+
};
216+
const validated = serverlessStepFunctions.httpValidate();
217+
expect(validated.events).to.be.an('Array').with.length(2);
218+
expect(validated.events[0].http).to.have.property('path', 'foo/bar');
219+
expect(validated.events[1].http).to.have.property('path', 'foo/bar');
220+
});
221+
});

lib/naming.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,18 @@ describe('#naming', () => {
5959
.equal('IamRoleStateMachineExecution');
6060
});
6161
});
62+
63+
describe('#getRestApiLogicalId()', () => {
64+
it('should return restApiLogicalId', () => {
65+
expect(serverlessStepFunctions.getRestApiLogicalId()).to
66+
.equal('ApiGatewayRestApiStepFunctions');
67+
});
68+
});
69+
70+
describe('#getApiGatewayName()', () => {
71+
it('should return apiGatewayName', () => {
72+
expect(serverlessStepFunctions.getApiGatewayName()).to
73+
.equal('dev-step-functions-stepfunctions');
74+
});
75+
});
6276
});

0 commit comments

Comments
 (0)