Skip to content

Commit bdab427

Browse files
committed
Update unit tests
1 parent 730fc6c commit bdab427

File tree

8 files changed

+1471
-395
lines changed

8 files changed

+1471
-395
lines changed

lib/deploy/events/apiGateway/cors.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
const corsMethodLogicalId = this.provider.naming
1313
.getMethodLogicalId(resourceName, 'options');
1414

15+
// TODO remove once "origins" config is deprecated
1516
let origin = config.origin;
1617
if (config.origins && config.origins.length) {
1718
origin = config.origins.join(',');
@@ -24,6 +25,16 @@ module.exports = {
2425
'Access-Control-Allow-Credentials': `'${config.allowCredentials}'`,
2526
};
2627

28+
// Enable CORS Max Age usage if set
29+
if (_.has(config, 'maxAge')) {
30+
if (_.isInteger(config.maxAge) && config.maxAge > 0) {
31+
preflightHeaders['Access-Control-Max-Age'] = `'${config.maxAge}'`;
32+
} else {
33+
const errorMessage = 'maxAge should be an integer over 0';
34+
throw new this.serverless.classes.Error(errorMessage);
35+
}
36+
}
37+
2738
if (_.includes(config.methods, 'ANY')) {
2839
preflightHeaders['Access-Control-Allow-Methods'] =
2940
preflightHeaders['Access-Control-Allow-Methods']
@@ -43,10 +54,11 @@ module.exports = {
4354
RequestTemplates: {
4455
'application/json': '{statusCode:200}',
4556
},
57+
ContentHandling: 'CONVERT_TO_TEXT',
4658
IntegrationResponses: this.generateCorsIntegrationResponses(preflightHeaders),
4759
},
4860
ResourceId: resourceRef,
49-
RestApiId: { Ref: this.apiGatewayRestApiLogicalId },
61+
RestApiId: this.provider.getApiGatewayRestApiId(),
5062
},
5163
},
5264
});

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

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

8-
describe('#methods()', () => {
8+
// 'use strict';
9+
//
10+
// const expect = require('chai').expect;
11+
// const ServerlessStepFunctions = require('../index');
12+
// const Serverless = require('../../../../../../../Serverless');
13+
// const AwsProvider = require('../../../../../provider/awsProvider');
14+
15+
describe('#compileCors()', () => {
916
let serverless;
10-
let serverlessStepFunctions;
17+
let awsCompileApigEvents;
1118

1219
beforeEach(() => {
13-
serverless = new Serverless();
14-
serverless.setProvider('aws', new AwsProvider(serverless));
15-
serverless.service.provider.compiledCloudFormationTemplate = {
16-
Resources: {},
17-
};
18-
1920
const options = {
2021
stage: 'dev',
2122
region: 'us-east-1',
2223
};
23-
serverlessStepFunctions = new ServerlessStepFunctions(serverless, options);
24-
serverlessStepFunctions.serverless.service.stepFunctions = {
25-
stateMachines: {
26-
first: {},
24+
serverless = new Serverless();
25+
serverless.setProvider('aws', new AwsProvider(serverless, options));
26+
serverless.service.service = 'first-service';
27+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
28+
serverless.service.environment = {
29+
stages: {
30+
dev: {
31+
regions: {
32+
'us-east-1': {
33+
vars: {
34+
IamRoleLambdaExecution:
35+
'arn:aws:iam::12345678:role/service-dev-IamRoleLambdaExecution-FOO12345678',
36+
},
37+
},
38+
},
39+
},
2740
},
2841
};
29-
serverlessStepFunctions.apiGatewayResourceLogicalIds = {
30-
'users/create': 'ApiGatewayResourceUsersCreate',
31-
'users/list': 'ApiGatewayResourceUsersList',
32-
'users/update': 'ApiGatewayResourceUsersUpdate',
33-
'users/delete': 'ApiGatewayResourceUsersDelete',
34-
'users/any': 'ApiGatewayResourceUsersAny',
35-
};
36-
serverlessStepFunctions.apiGatewayResourceNames = {
37-
'users/create': 'UsersCreate',
38-
'users/list': 'UsersList',
39-
'users/update': 'UsersUpdate',
40-
'users/delete': 'UsersDelete',
41-
'users/any': 'UsersAny',
42+
awsCompileApigEvents = new ServerlessStepFunctions(serverless, options);
43+
awsCompileApigEvents.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
44+
awsCompileApigEvents.apiGatewayResources = {
45+
'users/create': {
46+
name: 'UsersCreate',
47+
resourceLogicalId: 'ApiGatewayResourceUsersCreate',
48+
},
49+
50+
'users/list': {
51+
name: 'UsersList',
52+
resourceLogicalId: 'ApiGatewayResourceUsersList',
53+
},
54+
'users/update': {
55+
name: 'UsersUpdate',
56+
resourceLogicalId: 'ApiGatewayResourceUsersUpdate',
57+
},
58+
'users/delete': {
59+
name: 'UsersDelete',
60+
resourceLogicalId: 'ApiGatewayResourceUsersDelete',
61+
},
62+
'users/any': {
63+
name: 'UsersAny',
64+
resourceLogicalId: 'ApiGatewayResourceUsersAny',
65+
},
4266
};
43-
serverlessStepFunctions.pluginhttpValidated = {};
67+
awsCompileApigEvents.pluginhttpValidated = {};
4468
});
4569

4670
it('should create preflight method for CORS enabled resource', () => {
47-
serverlessStepFunctions.pluginhttpValidated.corsPreflight = {
71+
awsCompileApigEvents.pluginhttpValidated.corsPreflight = {
4872
'users/update': {
4973
origin: 'http://example.com',
5074
headers: ['*'],
5175
methods: ['OPTIONS', 'PUT'],
5276
allowCredentials: false,
77+
maxAge: 86400,
5378
},
5479
'users/create': {
5580
origins: ['*', 'http://example.com'],
5681
headers: ['*'],
5782
methods: ['OPTIONS', 'POST'],
5883
allowCredentials: true,
84+
maxAge: 86400,
5985
},
6086
'users/delete': {
6187
origins: ['*'],
6288
headers: ['CustomHeaderA', 'CustomHeaderB'],
6389
methods: ['OPTIONS', 'DELETE'],
6490
allowCredentials: false,
91+
maxAge: 86400,
6592
},
6693
'users/any': {
6794
origins: ['http://example.com'],
@@ -70,115 +97,166 @@ describe('#methods()', () => {
7097
allowCredentials: false,
7198
},
7299
};
73-
return serverlessStepFunctions.compileCors().then(() => {
100+
return awsCompileApigEvents.compileCors().then(() => {
74101
// users/create
75102
expect(
76-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
103+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
77104
.Resources.ApiGatewayMethodUsersCreateOptions
78105
.Properties.Integration.IntegrationResponses[0]
79106
.ResponseParameters['method.response.header.Access-Control-Allow-Origin']
80107
).to.equal('\'*,http://example.com\'');
81108

82109
expect(
83-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
110+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
84111
.Resources.ApiGatewayMethodUsersCreateOptions
85112
.Properties.Integration.IntegrationResponses[0]
86113
.ResponseParameters['method.response.header.Access-Control-Allow-Headers']
87114
).to.equal('\'*\'');
88115

89116
expect(
90-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
117+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
91118
.Resources.ApiGatewayMethodUsersCreateOptions
92119
.Properties.Integration.IntegrationResponses[0]
93120
.ResponseParameters['method.response.header.Access-Control-Allow-Methods']
94121
).to.equal('\'OPTIONS,POST\'');
95122

96123
expect(
97-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
124+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
98125
.Resources.ApiGatewayMethodUsersCreateOptions
99126
.Properties.Integration.IntegrationResponses[0]
100127
.ResponseParameters['method.response.header.Access-Control-Allow-Credentials']
101128
).to.equal('\'true\'');
102129

130+
expect(
131+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
132+
.Resources.ApiGatewayMethodUsersCreateOptions
133+
.Properties.Integration.IntegrationResponses[0]
134+
.ResponseParameters['method.response.header.Access-Control-Max-Age']
135+
).to.equal('\'86400\'');
136+
103137
// users/update
104138
expect(
105-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
139+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
106140
.Resources.ApiGatewayMethodUsersUpdateOptions
107141
.Properties.Integration.IntegrationResponses[0]
108142
.ResponseParameters['method.response.header.Access-Control-Allow-Origin']
109143
).to.equal('\'http://example.com\'');
110144

111145
expect(
112-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
146+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
113147
.Resources.ApiGatewayMethodUsersUpdateOptions
114148
.Properties.Integration.IntegrationResponses[0]
115149
.ResponseParameters['method.response.header.Access-Control-Allow-Methods']
116150
).to.equal('\'OPTIONS,PUT\'');
117151

118152
expect(
119-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
153+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
120154
.Resources.ApiGatewayMethodUsersUpdateOptions
121155
.Properties.Integration.IntegrationResponses[0]
122156
.ResponseParameters['method.response.header.Access-Control-Allow-Credentials']
123157
).to.equal('\'false\'');
124158

159+
expect(
160+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
161+
.Resources.ApiGatewayMethodUsersUpdateOptions
162+
.Properties.Integration.IntegrationResponses[0]
163+
.ResponseParameters['method.response.header.Access-Control-Max-Age']
164+
).to.equal('\'86400\'');
165+
125166
// users/delete
126167
expect(
127-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
168+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
128169
.Resources.ApiGatewayMethodUsersDeleteOptions
129170
.Properties.Integration.IntegrationResponses[0]
130171
.ResponseParameters['method.response.header.Access-Control-Allow-Origin']
131172
).to.equal('\'*\'');
132173

133174
expect(
134-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
175+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
135176
.Resources.ApiGatewayMethodUsersDeleteOptions
136177
.Properties.Integration.IntegrationResponses[0]
137178
.ResponseParameters['method.response.header.Access-Control-Allow-Headers']
138179
).to.equal('\'CustomHeaderA,CustomHeaderB\'');
139180

140181
expect(
141-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
182+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
142183
.Resources.ApiGatewayMethodUsersDeleteOptions
143184
.Properties.Integration.IntegrationResponses[0]
144185
.ResponseParameters['method.response.header.Access-Control-Allow-Methods']
145186
).to.equal('\'OPTIONS,DELETE\'');
146187

147188
expect(
148-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
189+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
149190
.Resources.ApiGatewayMethodUsersDeleteOptions
150191
.Properties.Integration.IntegrationResponses[0]
151192
.ResponseParameters['method.response.header.Access-Control-Allow-Credentials']
152193
).to.equal('\'false\'');
153194

195+
expect(
196+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
197+
.Resources.ApiGatewayMethodUsersDeleteOptions
198+
.Properties.Integration.IntegrationResponses[0]
199+
.ResponseParameters['method.response.header.Access-Control-Max-Age']
200+
).to.equal('\'86400\'');
201+
154202
// users/any
155203
expect(
156-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
204+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
157205
.Resources.ApiGatewayMethodUsersAnyOptions
158206
.Properties.Integration.IntegrationResponses[0]
159207
.ResponseParameters['method.response.header.Access-Control-Allow-Origin']
160208
).to.equal('\'http://example.com\'');
161209

162210
expect(
163-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
211+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
164212
.Resources.ApiGatewayMethodUsersAnyOptions
165213
.Properties.Integration.IntegrationResponses[0]
166214
.ResponseParameters['method.response.header.Access-Control-Allow-Headers']
167215
).to.equal('\'*\'');
168216

169217
expect(
170-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
218+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
171219
.Resources.ApiGatewayMethodUsersAnyOptions
172220
.Properties.Integration.IntegrationResponses[0]
173221
.ResponseParameters['method.response.header.Access-Control-Allow-Methods']
174222
).to.equal('\'OPTIONS,DELETE,GET,HEAD,PATCH,POST,PUT\'');
175223

176224
expect(
177-
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
225+
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
178226
.Resources.ApiGatewayMethodUsersAnyOptions
179227
.Properties.Integration.IntegrationResponses[0]
180228
.ResponseParameters['method.response.header.Access-Control-Allow-Credentials']
181229
).to.equal('\'false\'');
182230
});
183231
});
232+
233+
it('should throw error if maxAge is not an integer greater than 0', () => {
234+
awsCompileApigEvents.pluginhttpValidated.corsPreflight = {
235+
'users/update': {
236+
origin: 'http://example.com',
237+
headers: ['*'],
238+
methods: ['OPTIONS', 'PUT'],
239+
allowCredentials: false,
240+
maxAge: -1,
241+
},
242+
};
243+
244+
expect(() => awsCompileApigEvents.compileCors())
245+
.to.throw(Error, 'maxAge should be an integer over 0');
246+
});
247+
248+
it('should throw error if maxAge is not an integer', () => {
249+
awsCompileApigEvents.pluginhttpValidated.corsPreflight = {
250+
'users/update': {
251+
origin: 'http://example.com',
252+
headers: ['*'],
253+
methods: ['OPTIONS', 'PUT'],
254+
allowCredentials: false,
255+
maxAge: 'five',
256+
},
257+
};
258+
259+
expect(() => awsCompileApigEvents.compileCors())
260+
.to.throw(Error, 'maxAge should be an integer over 0');
261+
});
184262
});

0 commit comments

Comments
 (0)