Skip to content

Commit 8890c72

Browse files
committed
add test related to kinesis
1 parent 2327a08 commit 8890c72

8 files changed

+260
-55
lines changed

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class ServerlessApigatewayServiceProxy {
5555
'package:compileEvents': async () => {
5656
if (this.getAllServiceProxies().length > 0) {
5757
this.validated = await this.validateServiceProxies()
58+
5859
await this.compileRestApi()
5960
await this.compileResources()
6061
await this.compileCors()

lib/package/kinesis/compileIamRoleToKinesis.js

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,54 @@ module.exports = {
66
async compileIamRoleToKinesis() {
77
await BbPromise.all(
88
this.getAllServiceProxies().map(async (serviceProxy) => {
9-
Object.keys(serviceProxy).forEach(async (serviceName) => {
10-
if (serviceName == 'kinesis') {
11-
const template = {
12-
Type: 'AWS::IAM::Role',
13-
Properties: {
14-
AssumeRolePolicyDocument: {
15-
Version: '2012-10-17',
16-
Statement: [
17-
{
18-
Effect: 'Allow',
19-
Principal: {
20-
Service: 'apigateway.amazonaws.com'
21-
},
22-
Action: 'sts:AssumeRole'
23-
}
24-
]
25-
},
26-
Policies: [
9+
const serviceName = this.getServiceName(serviceProxy)
10+
if (serviceName == 'kinesis') {
11+
const template = {
12+
Type: 'AWS::IAM::Role',
13+
Properties: {
14+
AssumeRolePolicyDocument: {
15+
Version: '2012-10-17',
16+
Statement: [
2717
{
28-
PolicyName: 'apigatewaytokinesis',
29-
PolicyDocument: {
30-
Version: '2012-10-17',
31-
Statement: [
32-
{
33-
Effect: 'Allow',
34-
Action: [
35-
'logs:CreateLogGroup',
36-
'logs:CreateLogStream',
37-
'logs:PutLogEvents'
38-
],
39-
Resource: '*'
40-
},
41-
{
42-
Effect: 'Allow',
43-
Action: ['kinesis:PutRecord'],
44-
Resource: '*'
45-
}
46-
]
47-
}
18+
Effect: 'Allow',
19+
Principal: {
20+
Service: 'apigateway.amazonaws.com'
21+
},
22+
Action: 'sts:AssumeRole'
4823
}
4924
]
50-
}
25+
},
26+
Policies: [
27+
{
28+
PolicyName: 'apigatewaytokinesis',
29+
PolicyDocument: {
30+
Version: '2012-10-17',
31+
Statement: [
32+
{
33+
Effect: 'Allow',
34+
Action: [
35+
'logs:CreateLogGroup',
36+
'logs:CreateLogStream',
37+
'logs:PutLogEvents'
38+
],
39+
Resource: '*'
40+
},
41+
{
42+
Effect: 'Allow',
43+
Action: ['kinesis:PutRecord'],
44+
Resource: '*'
45+
}
46+
]
47+
}
48+
}
49+
]
5150
}
52-
53-
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
54-
ApigatewayToKinesisRole: template
55-
})
5651
}
57-
})
52+
53+
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
54+
ApigatewayToKinesisRole: template
55+
})
56+
}
5857
})
5958
)
6059
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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('#compileIamRoleToKinesis()', () => {
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+
kinesis: {
33+
path: '/kinesis',
34+
method: 'post'
35+
}
36+
}
37+
]
38+
}
39+
40+
await serverlessApigatewayServiceProxy.compileIamRoleToKinesis()
41+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
42+
ApigatewayToKinesisRole: {
43+
Type: 'AWS::IAM::Role',
44+
Properties: {
45+
AssumeRolePolicyDocument: {
46+
Version: '2012-10-17',
47+
Statement: [
48+
{
49+
Effect: 'Allow',
50+
Principal: { Service: 'apigateway.amazonaws.com' },
51+
Action: 'sts:AssumeRole'
52+
}
53+
]
54+
},
55+
Policies: [
56+
{
57+
PolicyName: 'apigatewaytokinesis',
58+
PolicyDocument: {
59+
Version: '2012-10-17',
60+
Statement: [
61+
{
62+
Effect: 'Allow',
63+
Action: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'],
64+
Resource: '*'
65+
},
66+
{ Effect: 'Allow', Action: ['kinesis:PutRecord'], Resource: '*' }
67+
]
68+
}
69+
}
70+
]
71+
}
72+
}
73+
})
74+
})
75+
76+
it('should not create corresponding resources when other proxies are given', async () => {
77+
serverlessApigatewayServiceProxy.serverless.service.custom = {
78+
apiGatewayServiceProxies: [
79+
{
80+
sqs: {
81+
path: '/kinesis',
82+
method: 'post'
83+
}
84+
}
85+
]
86+
}
87+
88+
await serverlessApigatewayServiceProxy.compileIamRoleToKinesis()
89+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.be.empty
90+
})
91+
})

lib/package/kinesis/compileMethodsToKinesis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const _ = require('lodash')
66
module.exports = {
77
async compileMethodsToKinesis() {
88
this.validated.events.forEach(async (event) => {
9-
if (event.functionName == 'kinesis') {
9+
if (event.serviceName == 'kinesis') {
1010
const resourceId = this.getResourceId(event.http.path)
1111
const resourceName = this.getResourceName(event.http.path)
1212

lib/package/kinesis/validateKinesisServiceProxy.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
async validateKinesisServiceProxy() {
88
await BbPromise.all(
99
this.validated.events.map(async (serviceProxy) => {
10-
if (serviceProxy.functionName == 'kinesis') {
10+
if (serviceProxy.serviceName == 'kinesis') {
1111
if (!_.has(serviceProxy.http, 'streamName')) {
1212
return BbPromise.reject(
1313
new this.serverless.classes.Error(
@@ -17,25 +17,24 @@ module.exports = {
1717
}
1818

1919
if (
20-
typeof serviceProxy.http.streamName != 'object' &&
21-
typeof serviceProxy.http.streamName != 'string'
20+
!_.isPlainObject(serviceProxy.http.streamName) &&
21+
!_.isString(serviceProxy.http.streamName)
2222
) {
2323
const errorMessage = [
2424
'You can only set "string" or the AWS intrinsic function',
25-
' "Ref" like { Ref: \'KinesisStreamResourceId\' }}',
25+
' "Ref" like { Ref: \'KinesisStreamResourceId\' }',
2626
' as a streamName property'
2727
].join('')
2828
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
2929
}
3030

3131
if (
32-
typeof serviceProxy.http.streamName == 'object' &&
32+
_.isPlainObject(serviceProxy.http.streamName) &&
3333
!_.has(serviceProxy.http.streamName, 'Ref')
3434
) {
3535
const errorMessage = [
36-
'You can only set "string" or the AWS intrinsic function',
37-
' "Ref" like { Ref: \'KinesisStreamResourceId\' }}',
38-
' as a streamName property'
36+
'the AWS intrinsic function need "Ref" property like',
37+
" { Ref: 'KinesisStreamResourceId' } as a streamName"
3938
].join('')
4039
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
4140
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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('#validateKinesisServiceProxy()', () => {
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 validate the "streamName" property', async () => {
29+
serverlessApigatewayServiceProxy.validated = {
30+
events: [
31+
{
32+
serviceName: 'kinesis',
33+
http: {
34+
path: 'kinesis',
35+
method: 'post'
36+
}
37+
}
38+
]
39+
}
40+
41+
await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.rejectedWith(
42+
'Missing "streamName" property in Kinesis Service Proxy'
43+
)
44+
})
45+
46+
it('should validate the "streamName" property if it is string or AWS intrinsic function', async () => {
47+
serverlessApigatewayServiceProxy.validated = {
48+
events: [
49+
{
50+
serviceName: 'kinesis',
51+
http: {
52+
streamName: ['xx', 'yy'],
53+
path: 'kinesis',
54+
method: 'post'
55+
}
56+
}
57+
]
58+
}
59+
60+
await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.rejectedWith(
61+
'You can only set "string" or the AWS intrinsic function "Ref" like { Ref: \'KinesisStreamResourceId\' } as a streamName property'
62+
)
63+
})
64+
65+
it('should validate the "streamName" property if AWS intrinsic function is correct syntax', async () => {
66+
serverlessApigatewayServiceProxy.validated = {
67+
events: [
68+
{
69+
serviceName: 'kinesis',
70+
http: {
71+
streamName: { xxx: 'KinesisStreamResourceId' },
72+
path: 'kinesis',
73+
method: 'post'
74+
}
75+
}
76+
]
77+
}
78+
79+
await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.rejectedWith(
80+
'the AWS intrinsic function need "Ref" property like { Ref: \'KinesisStreamResourceId\' } as a streamName'
81+
)
82+
})
83+
84+
it('should not show error if streamName is correct', async () => {
85+
serverlessApigatewayServiceProxy.validated = {
86+
events: [
87+
{
88+
serviceName: 'kinesis',
89+
http: {
90+
streamName: { Ref: 'KinesisStreamResourceId' },
91+
path: 'kinesis',
92+
method: 'post'
93+
}
94+
}
95+
]
96+
}
97+
98+
await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.fulfilled
99+
100+
serverlessApigatewayServiceProxy.validated = {
101+
events: [
102+
{
103+
serviceName: 'kinesis',
104+
http: {
105+
streamName: 'yourStream',
106+
path: 'kinesis',
107+
method: 'post'
108+
}
109+
}
110+
]
111+
}
112+
113+
await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.fulfilled
114+
})
115+
})

lib/package/sqs/compileMethodsToSqs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const _ = require('lodash')
66
module.exports = {
77
async compileMethodsToSqs() {
88
this.validated.events.forEach(async (event) => {
9-
if (event.functionName == 'sqs') {
9+
if (event.serviceName == 'sqs') {
1010
const resourceId = this.getResourceId(event.http.path)
1111
const resourceName = this.getResourceName(event.http.path)
1212

lib/package/sqs/validateSqsServiceProxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
async validateSqsServiceProxy() {
88
await BbPromise.all(
99
this.validated.events.map(async (serviceProxy) => {
10-
if (serviceProxy.functionName == 'sqs') {
10+
if (serviceProxy.serviceName == 'sqs') {
1111
if (!_.has(serviceProxy.http, 'queueName')) {
1212
return BbPromise.reject(
1313
new this.serverless.classes.Error('Missing "queueName" property in SQS Service Proxy')

0 commit comments

Comments
 (0)