11'use strict'
2-
2+ const chalk = require ( 'chalk' )
33const sinon = require ( 'sinon' )
44const chai = require ( 'chai' )
55const 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} )
0 commit comments