Skip to content

Commit accc9af

Browse files
committed
test(coverage): remove unnecessary usage of async, add tests coverage
1 parent 15a0209 commit accc9af

24 files changed

+465
-117
lines changed

lib/index.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict'
22
const chalk = require('chalk')
3-
const BbPromise = require('bluebird')
4-
const _ = require('lodash')
53

64
const utils = require('./utils')
75
const validate = require('./apiGateway/validate')
@@ -97,7 +95,7 @@ class ServerlessApigatewayServiceProxy {
9795
async mergeDeployment() {
9896
let exists = false
9997
Object.keys(this.serverless.service.provider.compiledCloudFormationTemplate.Resources).forEach(
100-
async (resource) => {
98+
(resource) => {
10199
if (
102100
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[resource][
103101
'Type'
@@ -118,35 +116,34 @@ class ServerlessApigatewayServiceProxy {
118116
}
119117
}
120118

121-
async display() {
119+
display() {
120+
const proxies = this.getAllServiceProxies()
121+
if (proxies.length <= 0) {
122+
return ''
123+
}
124+
122125
let message = ''
123126
let serviceProxyMessages = ''
124127

125128
const endpointInfo = this.gatheredData.info.endpoints
126129
message += `${chalk.yellow.underline('Serverless APIGateway Service Proxy OutPuts')}\n`
127130
message += `${chalk.yellow('endpoints:')}`
128131

129-
await BbPromise.all(
130-
this.getAllServiceProxies().map(async (serviceProxy) => {
131-
Object.keys(serviceProxy).forEach(async (serviceName) => {
132-
let path
133-
const method = serviceProxy[serviceName].method.toUpperCase()
134-
path = serviceProxy[serviceName].path
135-
path =
136-
path !== '/'
137-
? `/${path
138-
.split('/')
139-
.filter((p) => p !== '')
140-
.join('/')}`
141-
: ''
142-
serviceProxyMessages += `\n ${method} - ${endpointInfo}${path}`
143-
})
132+
proxies.forEach((serviceProxy) => {
133+
Object.keys(serviceProxy).forEach((serviceName) => {
134+
let path
135+
const method = serviceProxy[serviceName].method.toUpperCase()
136+
path = serviceProxy[serviceName].path
137+
path =
138+
path !== '/'
139+
? `/${path
140+
.split('/')
141+
.filter((p) => p !== '')
142+
.join('/')}`
143+
: ''
144+
serviceProxyMessages += `\n ${method} - ${endpointInfo}${path}`
144145
})
145-
)
146-
147-
if (_.isEmpty(serviceProxyMessages)) {
148-
return ''
149-
}
146+
})
150147

151148
message += serviceProxyMessages
152149
message += '\n'

lib/index.test.js

Lines changed: 121 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
2+
const chalk = require('chalk')
33
const sinon = require('sinon')
44
const chai = require('chai')
55
const BbPromise = require('bluebird')
@@ -24,31 +24,47 @@ describe('#index()', () => {
2424
stage: 'dev',
2525
region: 'us-east-1'
2626
}
27+
serverless.cli = {
28+
consoleLog: () => {}
29+
}
2730
serverless.setProvider('aws', new AwsProvider(serverless))
2831
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} }
2932
serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options)
3033
})
3134

3235
describe('#constructor()', () => {
33-
it('should have hooks', () => expect(serverlessApigatewayServiceProxy.hooks).to.be.not.empty)
36+
it('should initialize options to empty object', () => {
37+
serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless)
38+
39+
expect(serverlessApigatewayServiceProxy.options).to.be.empty
40+
})
41+
42+
it('should have hooks', () => {
43+
expect(serverlessApigatewayServiceProxy.hooks).to.be.not.empty
44+
})
3445

35-
it('should set the provider variable to an instance of AwsProvider', () =>
36-
expect(serverlessApigatewayServiceProxy.provider).to.be.instanceof(AwsProvider))
46+
it('should set the provider variable to an instance of AwsProvider', () => {
47+
expect(serverlessApigatewayServiceProxy.provider).to.be.instanceof(AwsProvider)
48+
})
3749

38-
it('should have access to the serverless instance', () =>
39-
expect(serverlessApigatewayServiceProxy.serverless).to.deep.equal(serverless))
50+
it('should have access to the serverless instance', () => {
51+
expect(serverlessApigatewayServiceProxy.serverless).to.deep.equal(serverless)
52+
})
4053

41-
it('should set the options variable', () =>
54+
it('should set the options variable', () => {
4255
expect(serverlessApigatewayServiceProxy.options).to.deep.equal({
4356
stage: 'dev',
4457
region: 'us-east-1'
45-
}))
58+
})
59+
})
4660

47-
it('should set the region variable', () =>
48-
expect(serverlessApigatewayServiceProxy.region).to.be.equal('us-east-1'))
61+
it('should set the region variable', () => {
62+
expect(serverlessApigatewayServiceProxy.region).to.be.equal('us-east-1')
63+
})
4964

50-
it('should set the stage variable', () =>
51-
expect(serverlessApigatewayServiceProxy.stage).to.be.equal('dev'))
65+
it('should set the stage variable', () => {
66+
expect(serverlessApigatewayServiceProxy.stage).to.be.equal('dev')
67+
})
5268

5369
it('should run package:compileEvents in order', async () => {
5470
serverlessApigatewayServiceProxy.serverless.service.custom = {
@@ -273,5 +289,98 @@ describe('#index()', () => {
273289
})
274290
})
275291
})
292+
293+
it('should create deployment resource if core do not have "AWS::ApiGateway::Deployment" resource', async () => {
294+
serverlessApigatewayServiceProxy.apiGatewayMethodLogicalIds = 'apiResource'
295+
serverlessApigatewayServiceProxy.serverless.instanceId = 'xxx'
296+
serverless.service.provider.compiledCloudFormationTemplate.Resources = {
297+
SomeOtherResource: {
298+
Type: 'SomeOtherResourceType'
299+
}
300+
}
301+
302+
await expect(serverlessApigatewayServiceProxy.mergeDeployment()).to.be.fulfilled.then(() => {
303+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
304+
SomeOtherResource: {
305+
Type: 'SomeOtherResourceType'
306+
},
307+
ApiGatewayDeploymentxxx: {
308+
Type: 'AWS::ApiGateway::Deployment',
309+
Properties: {
310+
RestApiId: { Ref: 'ApiGatewayRestApi' },
311+
StageName: 'dev',
312+
Description: undefined
313+
},
314+
DependsOn: 'apiResource'
315+
}
316+
})
317+
})
318+
})
319+
})
320+
321+
describe('display', () => {
322+
it('should call serverless.cli.consoleLog with message when proxies are set', () => {
323+
serverlessApigatewayServiceProxy.serverless.service.custom = {
324+
apiGatewayServiceProxies: [
325+
{
326+
kinesis: {
327+
path: '/kinesis',
328+
method: 'post'
329+
}
330+
},
331+
{
332+
sqs: {
333+
path: '/sqs',
334+
method: 'post'
335+
}
336+
},
337+
{
338+
s3: {
339+
path: '/s3',
340+
method: 'post'
341+
}
342+
},
343+
{
344+
sns: {
345+
path: '/',
346+
method: 'post'
347+
}
348+
}
349+
]
350+
}
351+
serverlessApigatewayServiceProxy.gatheredData = {
352+
info: { endpoints: 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev' }
353+
}
354+
355+
const expectedMessage = `${chalk.yellow.underline(
356+
'Serverless APIGateway Service Proxy OutPuts'
357+
)}
358+
${chalk.yellow('endpoints:')}
359+
POST - https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/kinesis
360+
POST - https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/sqs
361+
POST - https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/s3
362+
POST - https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev
363+
`
364+
365+
const logStub = sinon.stub(serverless.cli, 'consoleLog')
366+
367+
expect(serverlessApigatewayServiceProxy.display()).to.equal(expectedMessage)
368+
369+
expect(logStub).to.have.been.calledOnceWith(expectedMessage)
370+
371+
serverless.cli.consoleLog.restore()
372+
})
373+
374+
it('should not call serverless.cli.consoleLog when proxies are not set', async () => {
375+
serverlessApigatewayServiceProxy.serverless.service.custom = {}
376+
377+
const logStub = sinon.stub(serverless.cli, 'consoleLog')
378+
379+
expect(serverlessApigatewayServiceProxy.display()).to.equal('')
380+
381+
expect(logStub).to.not.have.been.called
382+
383+
serverless.cli.consoleLog.restore()
384+
})
276385
})
277386
})

lib/package/kinesis/compileIamRoleToKinesis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const _ = require('lodash')
33

44
module.exports = {
5-
async compileIamRoleToKinesis() {
5+
compileIamRoleToKinesis() {
66
const kinesisStreamNames = this.getAllServiceProxies()
77
.filter((serviceProxy) => this.getServiceName(serviceProxy) === 'kinesis')
88
.map((serviceProxy) => {

lib/package/kinesis/compileIamRoleToKinesis.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict'
22

3-
const chai = require('chai')
43
const Serverless = require('serverless/lib/Serverless')
54
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
65
const ServerlessApigatewayServiceProxy = require('./../../index')
76

8-
chai.use(require('chai-as-promised'))
97
const expect = require('chai').expect
108

119
describe('#compileIamRoleToKinesis()', () => {
@@ -25,7 +23,7 @@ describe('#compileIamRoleToKinesis()', () => {
2523
serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options)
2624
})
2725

28-
it('should create corresponding resources when kinesis proxies are given', async () => {
26+
it('should create corresponding resources when kinesis proxies are given', () => {
2927
serverlessApigatewayServiceProxy.serverless.service.custom = {
3028
apiGatewayServiceProxies: [
3129
{
@@ -52,7 +50,8 @@ describe('#compileIamRoleToKinesis()', () => {
5250
]
5351
}
5452

55-
await expect(serverlessApigatewayServiceProxy.compileIamRoleToKinesis()).to.be.fulfilled
53+
serverlessApigatewayServiceProxy.compileIamRoleToKinesis()
54+
5655
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
5756
ApigatewayToKinesisRole: {
5857
Type: 'AWS::IAM::Role',
@@ -105,7 +104,7 @@ describe('#compileIamRoleToKinesis()', () => {
105104
})
106105
})
107106

108-
it('should not create corresponding resources when other proxies are given', async () => {
107+
it('should not create corresponding resources when other proxies are given', () => {
109108
serverlessApigatewayServiceProxy.serverless.service.custom = {
110109
apiGatewayServiceProxies: [
111110
{
@@ -117,7 +116,8 @@ describe('#compileIamRoleToKinesis()', () => {
117116
]
118117
}
119118

120-
await expect(serverlessApigatewayServiceProxy.compileIamRoleToKinesis()).to.be.fulfilled
119+
serverlessApigatewayServiceProxy.compileIamRoleToKinesis()
120+
121121
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.be.empty
122122
})
123123
})

lib/package/kinesis/compileKinesisServiceProxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
module.exports = {
3-
async compileKinesisServiceProxy() {
3+
compileKinesisServiceProxy() {
44
this.compileIamRoleToKinesis()
55
this.compileMethodsToKinesis()
66
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const sinon = require('sinon')
2+
const chai = require('chai')
3+
const Serverless = require('serverless/lib/Serverless')
4+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
5+
const ServerlessApigatewayServiceProxy = require('./../../index')
6+
7+
chai.use(require('sinon-chai'))
8+
9+
const expect = chai.expect
10+
11+
describe('#compileKinesisServiceProxy', () => {
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 call compileIamRoleToKinesis and compileMethodsToKinesis once', () => {
29+
const compileIamRoleToKinesisStub = sinon.stub(
30+
serverlessApigatewayServiceProxy,
31+
'compileIamRoleToKinesis'
32+
)
33+
const compileMethodsToKinesisStub = sinon.stub(
34+
serverlessApigatewayServiceProxy,
35+
'compileMethodsToKinesis'
36+
)
37+
38+
serverlessApigatewayServiceProxy.compileKinesisServiceProxy()
39+
40+
expect(compileIamRoleToKinesisStub).to.have.been.calledOnce
41+
expect(compileMethodsToKinesisStub).to.have.been.calledOnce
42+
43+
serverlessApigatewayServiceProxy.compileIamRoleToKinesis.restore()
44+
serverlessApigatewayServiceProxy.compileMethodsToKinesis.restore()
45+
})
46+
})

lib/package/kinesis/compileMethodsToKinesis.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,7 @@ module.exports = {
8080
]
8181
}
8282

83-
if (http && http.cors) {
84-
let origin = http.cors.origin
85-
if (http.cors.origins && http.cors.origins.length) {
86-
origin = http.cors.origins.join(',')
87-
}
88-
89-
integrationResponse.IntegrationResponses.forEach((val, i) => {
90-
integrationResponse.IntegrationResponses[i].ResponseParameters = {
91-
'method.response.header.Access-Control-Allow-Origin': `'${origin}'`
92-
}
93-
})
94-
}
83+
this.addCors(http, integrationResponse)
9584

9685
_.merge(integration, integrationResponse)
9786

lib/package/kinesis/compileMethodsToKinesis.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict'
22

3-
const chai = require('chai')
43
const Serverless = require('serverless/lib/Serverless')
54
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
65
const ServerlessApigatewayServiceProxy = require('./../../index')
76

8-
chai.use(require('chai-as-promised'))
97
const expect = require('chai').expect
108

119
describe('#compileMethodsToKinesis()', () => {

0 commit comments

Comments
 (0)