@@ -7,12 +7,20 @@ const expect = require('chai').expect
77
88const validOpenAPI = require ( '../json/valid-openAPI.json' )
99
10+ const basicDocumentation = require ( '../models/BasicDocumentation.json' )
11+ const basicValidFunction = require ( '../models/BasicValidFunction.json' )
12+
1013const OpenAPIGenerator = require ( '../../src/openAPIGenerator' )
1114
1215describe ( 'OpenAPIGenerator' , ( ) => {
1316 let sls , logOutput
1417 beforeEach ( function ( ) {
1518 sls = {
19+ service : {
20+ service : 'test-service' ,
21+ getAllFunctions : ( ) => { } ,
22+ getFunction : ( ) => { }
23+ } ,
1624 version : '3.0.0' ,
1725 variables : {
1826 service : {
@@ -32,7 +40,7 @@ describe('OpenAPIGenerator', () => {
3240 options : {
3341 postmanCollection : 'postman.json'
3442 }
35- }
43+ } ,
3644 }
3745
3846 logOutput = {
@@ -43,6 +51,119 @@ describe('OpenAPIGenerator', () => {
4351 }
4452 }
4553 } ) ;
54+
55+ describe ( 'generationAndValidation' , ( ) => {
56+ it ( 'should correctly generate a valid openAPI document' , async function ( ) {
57+ const succSpy = sinon . spy ( logOutput . log , 'success' )
58+ const errSpy = sinon . spy ( logOutput . log , 'error' )
59+
60+ Object . assign ( sls . service , basicDocumentation )
61+ const getAllFuncsStub = sinon . stub ( sls . service , 'getAllFunctions' ) . returns ( [ 'createUser' ] )
62+
63+ const getFuncStub = sinon . stub ( sls . service , 'getFunction' ) . returns ( basicValidFunction . createUser )
64+
65+ const openAPIGenerator = new OpenAPIGenerator ( sls , { } , logOutput )
66+ openAPIGenerator . processCliInput ( )
67+
68+ const validOpenAPIDocument = await openAPIGenerator . generationAndValidation ( )
69+ . catch ( err => {
70+ expect ( err ) . to . be . undefined
71+ } )
72+
73+ expect ( succSpy . called ) . to . be . true
74+ expect ( errSpy . called ) . to . be . false
75+
76+ succSpy . restore ( )
77+ errSpy . restore ( )
78+ getAllFuncsStub . reset ( )
79+ getFuncStub . reset ( )
80+ } ) ;
81+
82+ it ( 'should throw an error when trying to generate an invalid openAPI document' , async function ( ) {
83+ const succSpy = sinon . spy ( logOutput . log , 'success' )
84+ const errSpy = sinon . spy ( logOutput . log , 'error' )
85+
86+ Object . assign ( sls . service , basicDocumentation )
87+ const getAllFuncsStub = sinon . stub ( sls . service , 'getAllFunctions' ) . returns ( [ 'createUser' ] )
88+ const basicInvalidFunction = JSON . parse ( JSON . stringify ( basicValidFunction ) )
89+
90+ delete basicInvalidFunction . createUser . events [ 0 ] . http . documentation . methodResponses [ 0 ] . responseModels
91+ const getFuncStub = sinon . stub ( sls . service , 'getFunction' ) . returns ( basicInvalidFunction . createUser )
92+
93+ const openAPIGenerator = new OpenAPIGenerator ( sls , { } , logOutput )
94+ openAPIGenerator . processCliInput ( )
95+
96+ const validOpenAPIDocument = await openAPIGenerator . generationAndValidation ( )
97+ . catch ( err => {
98+ expect ( err . message ) . to . be . equal ( 'Error: createUser is missing a Response Model for statusCode 200' )
99+ } )
100+
101+ expect ( succSpy . called ) . to . be . false
102+ expect ( errSpy . called ) . to . be . true
103+
104+ succSpy . restore ( )
105+ errSpy . restore ( )
106+ getAllFuncsStub . reset ( )
107+ getFuncStub . reset ( )
108+ } ) ;
109+
110+ it ( 'should correctly validate a valid openAPI document' , async function ( ) {
111+ const succSpy = sinon . spy ( logOutput . log , 'success' )
112+ const errSpy = sinon . spy ( logOutput . log , 'error' )
113+
114+ Object . assign ( sls . service , basicDocumentation )
115+ const getAllFuncsStub = sinon . stub ( sls . service , 'getAllFunctions' ) . returns ( [ 'createUser' ] )
116+ const basicInvalidFunction = JSON . parse ( JSON . stringify ( basicValidFunction ) )
117+
118+ const getFuncStub = sinon . stub ( sls . service , 'getFunction' ) . returns ( basicInvalidFunction . createUser )
119+
120+ const openAPIGenerator = new OpenAPIGenerator ( sls , { } , logOutput )
121+ openAPIGenerator . processCliInput ( )
122+
123+ const validOpenAPIDocument = await openAPIGenerator . generationAndValidation ( )
124+ . catch ( err => {
125+ expect ( err ) . to . be . undefined
126+ } )
127+
128+ expect ( succSpy . called ) . to . be . true
129+ expect ( errSpy . called ) . to . be . false
130+ expect ( validOpenAPIDocument ) . to . have . property ( 'openapi' )
131+
132+ succSpy . restore ( )
133+ errSpy . restore ( )
134+ getAllFuncsStub . reset ( )
135+ getFuncStub . reset ( )
136+ } ) ;
137+
138+ it ( 'should throw an error when trying to validate an invalid openAPI document' , async function ( ) {
139+ const succSpy = sinon . spy ( logOutput . log , 'success' )
140+ const errSpy = sinon . spy ( logOutput . log , 'error' )
141+
142+ Object . assign ( sls . service , basicDocumentation )
143+ const getAllFuncsStub = sinon . stub ( sls . service , 'getAllFunctions' ) . returns ( [ 'createUser' ] )
144+ const basicInvalidFunction = JSON . parse ( JSON . stringify ( basicValidFunction ) )
145+
146+ delete basicInvalidFunction . createUser . events [ 0 ] . http . documentation . pathParams
147+ const getFuncStub = sinon . stub ( sls . service , 'getFunction' ) . returns ( basicInvalidFunction . createUser )
148+
149+ const openAPIGenerator = new OpenAPIGenerator ( sls , { } , logOutput )
150+ openAPIGenerator . processCliInput ( )
151+
152+ const validOpenAPIDocument = await openAPIGenerator . generationAndValidation ( )
153+ . catch ( err => {
154+ expect ( err . message ) . to . be . equal ( 'AssertionError: Templated parameter name not found' )
155+ } )
156+
157+ expect ( succSpy . called ) . to . be . false
158+ expect ( errSpy . called ) . to . be . true
159+
160+ succSpy . restore ( )
161+ errSpy . restore ( )
162+ getAllFuncsStub . reset ( )
163+ getFuncStub . reset ( )
164+ } ) ;
165+ } ) ;
166+
46167 describe ( 'createPostman' , ( ) => {
47168 it ( 'should generate a postman collection when a valid openAPI file is generated' , function ( ) {
48169 const fsStub = sinon . stub ( fs , 'writeFileSync' ) . returns ( true )
0 commit comments