Skip to content

Commit 5a095ea

Browse files
committed
add tests for sqs proxy
1 parent 9db6132 commit 5a095ea

8 files changed

+450
-8
lines changed

lib/package/kinesis/compileIamRoleToKinesis.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('#compileIamRoleToKinesis()', () => {
3737
]
3838
}
3939

40-
await serverlessApigatewayServiceProxy.compileIamRoleToKinesis()
40+
await expect(serverlessApigatewayServiceProxy.compileIamRoleToKinesis()).to.be.fulfilled
4141
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
4242
ApigatewayToKinesisRole: {
4343
Type: 'AWS::IAM::Role',
@@ -85,7 +85,7 @@ describe('#compileIamRoleToKinesis()', () => {
8585
]
8686
}
8787

88-
await serverlessApigatewayServiceProxy.compileIamRoleToKinesis()
88+
await expect(serverlessApigatewayServiceProxy.compileIamRoleToKinesis()).to.be.fulfilled
8989
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.be.empty
9090
})
9191
})

lib/package/kinesis/compileMethodsToKinesis.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,29 @@ describe('#compileMethodsToKinesis()', () => {
342342
resource.Properties.Integration.RequestTemplates['application/x-www-form-urlencoded']
343343
).to.be.equal('custom template')
344344
})
345+
346+
it('should not create corresponding resources when other proxies are given', async () => {
347+
serverlessApigatewayServiceProxy.validated = {
348+
events: [
349+
{
350+
serviceName: 'sqs',
351+
http: {
352+
streamName: 'myStream',
353+
path: 'kinesis',
354+
method: 'post'
355+
}
356+
}
357+
]
358+
}
359+
serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'
360+
serverlessApigatewayServiceProxy.apiGatewayResources = {
361+
kinesis: {
362+
name: 'kinesis',
363+
resourceLogicalId: 'ApiGatewayResourceKinesis'
364+
}
365+
}
366+
367+
await expect(serverlessApigatewayServiceProxy.compileMethodsToKinesis()).to.be.fulfilled
368+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.be.empty
369+
})
345370
})

lib/package/kinesis/validateKinesisServiceProxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = {
3333
!_.has(serviceProxy.http.streamName, 'Ref')
3434
) {
3535
const errorMessage = [
36-
'the AWS intrinsic function need "Ref" property like',
36+
'the AWS intrinsic function needs "Ref" property like',
3737
" { Ref: 'KinesisStreamResourceId' } as a streamName"
3838
].join('')
3939
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))

lib/package/kinesis/validateKinesisServiceProxy.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('#validateKinesisServiceProxy()', () => {
7777
}
7878

7979
await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.rejectedWith(
80-
'the AWS intrinsic function need "Ref" property like { Ref: \'KinesisStreamResourceId\' } as a streamName'
80+
'the AWS intrinsic function needs "Ref" property like { Ref: \'KinesisStreamResourceId\' } as a streamName'
8181
)
8282
})
8383

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'use strict'
2+
3+
const chai = require('chai')
4+
const Serverless = require('serverless/lib/Serverless')
5+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
6+
const ServerlessApigatewayServiceProxy = require('./../../index')
7+
8+
chai.use(require('chai-as-promised'))
9+
const expect = require('chai').expect
10+
11+
describe('#compileIamRoleToSqs()', () => {
12+
let serverless
13+
let serverlessApigatewayServiceProxy
14+
15+
beforeEach(() => {
16+
serverless = new Serverless()
17+
serverless.servicePath = true
18+
serverless.service.service = 'apigw-service-proxy'
19+
const options = {
20+
stage: 'dev',
21+
region: 'us-east-1'
22+
}
23+
serverless.setProvider('aws', new AwsProvider(serverless))
24+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} }
25+
serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options)
26+
})
27+
28+
it('should create corresponding resources when kinesis proxies are given', async () => {
29+
serverlessApigatewayServiceProxy.serverless.service.custom = {
30+
apiGatewayServiceProxies: [
31+
{
32+
sqs: {
33+
path: '/sqs',
34+
method: 'post'
35+
}
36+
}
37+
]
38+
}
39+
40+
await expect(serverlessApigatewayServiceProxy.compileIamRoleToSqs()).to.be.fulfilled
41+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
42+
ApigatewayToSqsRole: {
43+
Type: 'AWS::IAM::Role',
44+
Properties: {
45+
AssumeRolePolicyDocument: {
46+
Version: '2012-10-17',
47+
Statement: [
48+
{
49+
Effect: 'Allow',
50+
Principal: {
51+
Service: 'apigateway.amazonaws.com'
52+
},
53+
Action: 'sts:AssumeRole'
54+
}
55+
]
56+
},
57+
Policies: [
58+
{
59+
PolicyName: 'apigatewaytosqs',
60+
PolicyDocument: {
61+
Version: '2012-10-17',
62+
Statement: [
63+
{
64+
Effect: 'Allow',
65+
Action: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'],
66+
Resource: '*'
67+
},
68+
{
69+
Effect: 'Allow',
70+
Action: ['sqs:SendMessage'],
71+
Resource: '*'
72+
}
73+
]
74+
}
75+
}
76+
]
77+
}
78+
}
79+
})
80+
})
81+
82+
it('should not create corresponding resources when other proxies are given', async () => {
83+
serverlessApigatewayServiceProxy.serverless.service.custom = {
84+
apiGatewayServiceProxies: [
85+
{
86+
kinesis: {
87+
path: '/sqs',
88+
method: 'post'
89+
}
90+
}
91+
]
92+
}
93+
94+
await expect(serverlessApigatewayServiceProxy.compileIamRoleToSqs()).to.be.fulfilled
95+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.be.empty
96+
})
97+
})
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
'use strict'
2+
3+
const chai = require('chai')
4+
const Serverless = require('serverless/lib/Serverless')
5+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
6+
const ServerlessApigatewayServiceProxy = require('./../../index')
7+
8+
chai.use(require('chai-as-promised'))
9+
const expect = require('chai').expect
10+
11+
describe('#compileIamRoleToSqs()', () => {
12+
let serverless
13+
let serverlessApigatewayServiceProxy
14+
15+
beforeEach(() => {
16+
serverless = new Serverless()
17+
serverless.servicePath = true
18+
serverless.service.service = 'apigw-service-proxy'
19+
const options = {
20+
stage: 'dev',
21+
region: 'us-east-1'
22+
}
23+
serverless.setProvider('aws', new AwsProvider(serverless))
24+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} }
25+
serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options)
26+
})
27+
28+
it('should create corresponding resources when sqs proxies are given', async () => {
29+
serverlessApigatewayServiceProxy.validated = {
30+
events: [
31+
{
32+
serviceName: 'sqs',
33+
http: {
34+
queueName: 'myQueue',
35+
path: 'sqs',
36+
method: 'post'
37+
}
38+
}
39+
]
40+
}
41+
serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'
42+
serverlessApigatewayServiceProxy.apiGatewayResources = {
43+
sqs: {
44+
name: 'sqs',
45+
resourceLogicalId: 'ApiGatewayResourceSqs'
46+
}
47+
}
48+
49+
await expect(serverlessApigatewayServiceProxy.compileMethodsToSqs()).to.be.fulfilled
50+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
51+
ApiGatewayMethodsqsPost: {
52+
Type: 'AWS::ApiGateway::Method',
53+
Properties: {
54+
HttpMethod: 'POST',
55+
RequestParameters: {},
56+
AuthorizationType: 'NONE',
57+
ApiKeyRequired: false,
58+
ResourceId: { Ref: 'ApiGatewayResourceSqs' },
59+
RestApiId: { Ref: 'ApiGatewayRestApi' },
60+
Integration: {
61+
IntegrationHttpMethod: 'POST',
62+
Type: 'AWS',
63+
Credentials: { 'Fn::GetAtt': ['ApigatewayToSqsRole', 'Arn'] },
64+
Uri: {
65+
'Fn::Join': [
66+
'',
67+
[
68+
'arn:aws:apigateway:',
69+
{ Ref: 'AWS::Region' },
70+
':sqs:path//',
71+
{ Ref: 'AWS::AccountId' },
72+
'/',
73+
'"myQueue"'
74+
]
75+
]
76+
},
77+
RequestParameters: {
78+
'integration.request.querystring.Action': "'SendMessage'",
79+
'integration.request.querystring.MessageBody': 'method.request.body.message'
80+
},
81+
RequestTemplates: { 'application/json': '{statusCode:200}' },
82+
IntegrationResponses: [
83+
{
84+
StatusCode: 200,
85+
SelectionPattern: 200,
86+
ResponseParameters: {},
87+
ResponseTemplates: {}
88+
},
89+
{
90+
StatusCode: 400,
91+
SelectionPattern: 400,
92+
ResponseParameters: {},
93+
ResponseTemplates: {}
94+
}
95+
]
96+
},
97+
MethodResponses: [
98+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 200 },
99+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 400 }
100+
]
101+
}
102+
}
103+
})
104+
})
105+
106+
it('should create corresponding resources when sqs proxies are given with cors', async () => {
107+
serverlessApigatewayServiceProxy.validated = {
108+
events: [
109+
{
110+
serviceName: 'sqs',
111+
http: {
112+
queueName: 'myQueue',
113+
path: 'sqs',
114+
method: 'post',
115+
cors: {
116+
origins: ['*'],
117+
origin: '*',
118+
methods: ['OPTIONS', 'POST'],
119+
headers: [
120+
'Content-Type',
121+
'X-Amz-Date',
122+
'Authorization',
123+
'X-Api-Key',
124+
'X-Amz-Security-Token',
125+
'X-Amz-User-Agent'
126+
],
127+
allowCredentials: false
128+
}
129+
}
130+
}
131+
]
132+
}
133+
serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'
134+
serverlessApigatewayServiceProxy.apiGatewayResources = {
135+
sqs: {
136+
name: 'sqs',
137+
resourceLogicalId: 'ApiGatewayResourceSqs'
138+
}
139+
}
140+
141+
await expect(serverlessApigatewayServiceProxy.compileMethodsToSqs()).to.be.fulfilled
142+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
143+
ApiGatewayMethodsqsPost: {
144+
Type: 'AWS::ApiGateway::Method',
145+
Properties: {
146+
HttpMethod: 'POST',
147+
RequestParameters: {},
148+
AuthorizationType: 'NONE',
149+
ApiKeyRequired: false,
150+
ResourceId: { Ref: 'ApiGatewayResourceSqs' },
151+
RestApiId: { Ref: 'ApiGatewayRestApi' },
152+
Integration: {
153+
IntegrationHttpMethod: 'POST',
154+
Type: 'AWS',
155+
Credentials: { 'Fn::GetAtt': ['ApigatewayToSqsRole', 'Arn'] },
156+
Uri: {
157+
'Fn::Join': [
158+
'',
159+
[
160+
'arn:aws:apigateway:',
161+
{ Ref: 'AWS::Region' },
162+
':sqs:path//',
163+
{ Ref: 'AWS::AccountId' },
164+
'/',
165+
'"myQueue"'
166+
]
167+
]
168+
},
169+
RequestParameters: {
170+
'integration.request.querystring.Action': "'SendMessage'",
171+
'integration.request.querystring.MessageBody': 'method.request.body.message'
172+
},
173+
RequestTemplates: { 'application/json': '{statusCode:200}' },
174+
IntegrationResponses: [
175+
{
176+
StatusCode: 200,
177+
SelectionPattern: 200,
178+
ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" },
179+
ResponseTemplates: {}
180+
},
181+
{
182+
StatusCode: 400,
183+
SelectionPattern: 400,
184+
ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" },
185+
ResponseTemplates: {}
186+
}
187+
]
188+
},
189+
MethodResponses: [
190+
{
191+
ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" },
192+
ResponseModels: {},
193+
StatusCode: 200
194+
},
195+
{
196+
ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" },
197+
ResponseModels: {},
198+
StatusCode: 400
199+
}
200+
]
201+
}
202+
}
203+
})
204+
})
205+
})

lib/package/sqs/validateSqsServiceProxy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module.exports = {
1515
}
1616

1717
if (
18-
typeof serviceProxy.http.queueName != 'object' &&
19-
typeof serviceProxy.http.queueName != 'string'
18+
!_.isPlainObject(serviceProxy.http.queueName) &&
19+
!_.isString(serviceProxy.http.queueName)
2020
) {
2121
const errorMessage = [
2222
'You can only set "string" or the AWS "Fn::GetAtt" intrinsic function',
@@ -27,11 +27,11 @@ module.exports = {
2727
}
2828

2929
if (
30-
typeof serviceProxy.http.queueName == 'object' &&
30+
_.isPlainObject(serviceProxy.http.queueName) &&
3131
!_.has(serviceProxy.http.queueName, 'Fn::GetAtt')
3232
) {
3333
const errorMessage = [
34-
'You can only set "string" or the AWS "Fn::GetAtt" intrinsic function',
34+
'the AWS intrinsic function needs "Fn::GetAtt"',
3535
' like { Fn::GetAtt: [ "SQSQueueResourceId", "QueueName" ] }}',
3636
' as a queueName property'
3737
].join('')

0 commit comments

Comments
 (0)