serverless-openapi-documenter 0.0.71 → 0.0.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,223 +1,264 @@
1
- 'use strict'
2
-
3
- const fs = require('fs')
4
- const PostmanGenerator = require('openapi-to-postmanv2')
5
- const sinon = require('sinon')
6
- const expect = require('chai').expect
7
-
8
- const validOpenAPI = require('../json/valid-openAPI.json')
9
-
10
- const basicDocumentation = require('../models/BasicDocumentation.json')
11
- const basicValidFunction = require('../models/BasicValidFunction.json')
12
-
13
- const OpenAPIGenerator = require('../../src/openAPIGenerator')
14
-
15
- describe('OpenAPIGenerator', () => {
16
- let sls, logOutput
17
- beforeEach(function() {
18
- sls = {
19
- service: {
20
- service: 'test-service',
21
- getAllFunctions: () => {},
22
- getFunction: () => {}
23
- },
24
- version: '3.0.0',
25
- variables: {
26
- service: {
27
- custom: {
28
-
29
- }
30
- }
31
- },
32
- configSchemaHandler: {
33
- defineFunctionEventProperties: () => {},
34
- defineFunctionProperties: () => {},
35
- defineCustomProperties: () => {},
36
- },
37
- classes: {
38
- Error: class ServerlessError {constructor(err) {return new Error(err)}}
39
- },
40
- processedInput: {
41
- options: {
42
- postmanCollection: 'postman.json'
43
- }
44
- },
45
- }
46
-
47
- logOutput = {
48
- log: {
49
- notice: (str) => {},
50
- error: (str) => {},
51
- success: (str) => {}
52
- }
53
- }
54
- });
55
-
56
- describe('generationAndValidation', () => {
57
- it('should correctly generate a valid openAPI document', async function() {
58
- const succSpy = sinon.spy(logOutput.log, 'success')
59
- const errSpy = sinon.spy(logOutput.log, 'error')
60
-
61
- Object.assign(sls.service, basicDocumentation)
62
- const getAllFuncsStub = sinon.stub(sls.service, 'getAllFunctions').returns(['createUser'])
63
-
64
- const getFuncStub = sinon.stub(sls.service, 'getFunction').returns(basicValidFunction.createUser)
65
-
66
- const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
67
- openAPIGenerator.processCliInput()
68
-
69
- const validOpenAPIDocument = await openAPIGenerator.generationAndValidation()
70
- .catch(err => {
71
- expect(err).to.be.undefined
72
- })
73
-
74
- expect(succSpy.called).to.be.true
75
- expect(errSpy.called).to.be.false
76
-
77
- succSpy.restore()
78
- errSpy.restore()
79
- getAllFuncsStub.reset()
80
- getFuncStub.reset()
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const PostmanGenerator = require("openapi-to-postmanv2");
5
+ const sinon = require("sinon");
6
+ const expect = require("chai").expect;
7
+
8
+ const validOpenAPI = require("../json/valid-openAPI.json");
9
+
10
+ const basicDocumentation = require("../models/BasicDocumentation.json");
11
+ const basicValidFunction = require("../models/BasicValidFunction.json");
12
+
13
+ const OpenAPIGenerator = require("../../src/openAPIGenerator");
14
+
15
+ describe("OpenAPIGenerator", () => {
16
+ let sls, logOutput;
17
+ beforeEach(function () {
18
+ sls = {
19
+ service: {
20
+ service: "test-service",
21
+ provider: {
22
+ stage: "test",
23
+ },
24
+ getAllFunctions: () => {},
25
+ getFunction: () => {},
26
+ },
27
+ version: "3.0.0",
28
+ variables: {
29
+ service: {
30
+ custom: {},
31
+ },
32
+ },
33
+ configSchemaHandler: {
34
+ defineFunctionEventProperties: () => {},
35
+ defineFunctionProperties: () => {},
36
+ defineCustomProperties: () => {},
37
+ },
38
+ classes: {
39
+ Error: class ServerlessError {
40
+ constructor(err) {
41
+ return new Error(err);
42
+ }
43
+ },
44
+ },
45
+ processedInput: {
46
+ options: {
47
+ postmanCollection: "postman.json",
48
+ },
49
+ },
50
+ };
51
+
52
+ logOutput = {
53
+ log: {
54
+ notice: (str) => {},
55
+ error: (str) => {},
56
+ success: (str) => {},
57
+ },
58
+ };
59
+ });
60
+
61
+ describe("generationAndValidation", () => {
62
+ it("should correctly generate a valid openAPI document", async function () {
63
+ const succSpy = sinon.spy(logOutput.log, "success");
64
+ const errSpy = sinon.spy(logOutput.log, "error");
65
+
66
+ Object.assign(sls.service, basicDocumentation);
67
+ const getAllFuncsStub = sinon
68
+ .stub(sls.service, "getAllFunctions")
69
+ .returns(["createUser"]);
70
+
71
+ const getFuncStub = sinon
72
+ .stub(sls.service, "getFunction")
73
+ .returns(basicValidFunction.createUser);
74
+
75
+ const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
76
+ openAPIGenerator.processCliInput();
77
+
78
+ const validOpenAPIDocument = await openAPIGenerator
79
+ .generationAndValidation()
80
+ .catch((err) => {
81
+ expect(err).to.be.undefined;
81
82
  });
82
83
 
83
- it('should throw an error when trying to generate an invalid openAPI document', async function() {
84
- const succSpy = sinon.spy(logOutput.log, 'success')
85
- const errSpy = sinon.spy(logOutput.log, 'error')
84
+ expect(succSpy.called).to.be.true;
85
+ expect(errSpy.called).to.be.false;
86
86
 
87
- Object.assign(sls.service, basicDocumentation)
88
- const getAllFuncsStub = sinon.stub(sls.service, 'getAllFunctions').returns(['createUser'])
89
- const basicInvalidFunction = JSON.parse(JSON.stringify(basicValidFunction))
90
-
91
- delete basicInvalidFunction.createUser.events[0].http.documentation.methodResponses[0].responseModels
92
- const getFuncStub = sinon.stub(sls.service, 'getFunction').returns(basicInvalidFunction.createUser)
93
-
94
- const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
95
- openAPIGenerator.processCliInput()
96
-
97
- const validOpenAPIDocument = await openAPIGenerator.generationAndValidation()
98
- .catch(err => {
99
- expect(err.message).to.be.equal('Error: createUser is missing a Response Model for statusCode 200')
100
- })
101
-
102
- expect(succSpy.called).to.be.false
103
- expect(errSpy.called).to.be.true
87
+ succSpy.restore();
88
+ errSpy.restore();
89
+ getAllFuncsStub.reset();
90
+ getFuncStub.reset();
91
+ });
104
92
 
105
- succSpy.restore()
106
- errSpy.restore()
107
- getAllFuncsStub.reset()
108
- getFuncStub.reset()
93
+ it("should throw an error when trying to generate an invalid openAPI document", async function () {
94
+ const succSpy = sinon.spy(logOutput.log, "success");
95
+ const errSpy = sinon.spy(logOutput.log, "error");
96
+
97
+ Object.assign(sls.service, basicDocumentation);
98
+ const getAllFuncsStub = sinon
99
+ .stub(sls.service, "getAllFunctions")
100
+ .returns(["createUser"]);
101
+ const basicInvalidFunction = JSON.parse(
102
+ JSON.stringify(basicValidFunction)
103
+ );
104
+
105
+ delete basicInvalidFunction.createUser.events[0].http.documentation
106
+ .methodResponses[0].responseModels;
107
+ const getFuncStub = sinon
108
+ .stub(sls.service, "getFunction")
109
+ .returns(basicInvalidFunction.createUser);
110
+
111
+ const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
112
+ openAPIGenerator.processCliInput();
113
+
114
+ const validOpenAPIDocument = await openAPIGenerator
115
+ .generationAndValidation()
116
+ .catch((err) => {
117
+ expect(err.message).to.be.equal(
118
+ "Error: createUser is missing a Response Model for statusCode 200"
119
+ );
109
120
  });
110
121
 
111
- it('should correctly validate a valid openAPI document', async function() {
112
- const succSpy = sinon.spy(logOutput.log, 'success')
113
- const errSpy = sinon.spy(logOutput.log, 'error')
114
-
115
- Object.assign(sls.service, basicDocumentation)
116
- const getAllFuncsStub = sinon.stub(sls.service, 'getAllFunctions').returns(['createUser'])
117
- const basicInvalidFunction = JSON.parse(JSON.stringify(basicValidFunction))
118
-
119
- const getFuncStub = sinon.stub(sls.service, 'getFunction').returns(basicInvalidFunction.createUser)
122
+ expect(succSpy.called).to.be.false;
123
+ expect(errSpy.called).to.be.true;
120
124
 
121
- const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
122
- openAPIGenerator.processCliInput()
123
-
124
- const validOpenAPIDocument = await openAPIGenerator.generationAndValidation()
125
- .catch(err => {
126
- expect(err).to.be.undefined
127
- })
128
-
129
- expect(succSpy.called).to.be.true
130
- expect(errSpy.called).to.be.false
131
- expect(validOpenAPIDocument).to.have.property('openapi')
125
+ succSpy.restore();
126
+ errSpy.restore();
127
+ getAllFuncsStub.reset();
128
+ getFuncStub.reset();
129
+ });
132
130
 
133
- succSpy.restore()
134
- errSpy.restore()
135
- getAllFuncsStub.reset()
136
- getFuncStub.reset()
131
+ it("should correctly validate a valid openAPI document", async function () {
132
+ const succSpy = sinon.spy(logOutput.log, "success");
133
+ const errSpy = sinon.spy(logOutput.log, "error");
134
+
135
+ Object.assign(sls.service, basicDocumentation);
136
+ const getAllFuncsStub = sinon
137
+ .stub(sls.service, "getAllFunctions")
138
+ .returns(["createUser"]);
139
+ const basicInvalidFunction = JSON.parse(
140
+ JSON.stringify(basicValidFunction)
141
+ );
142
+
143
+ const getFuncStub = sinon
144
+ .stub(sls.service, "getFunction")
145
+ .returns(basicInvalidFunction.createUser);
146
+
147
+ const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
148
+ openAPIGenerator.processCliInput();
149
+
150
+ const validOpenAPIDocument = await openAPIGenerator
151
+ .generationAndValidation()
152
+ .catch((err) => {
153
+ expect(err).to.be.undefined;
137
154
  });
138
155
 
139
- it('should throw an error when trying to validate an invalid openAPI document', async function() {
140
- const succSpy = sinon.spy(logOutput.log, 'success')
141
- const errSpy = sinon.spy(logOutput.log, 'error')
142
-
143
- Object.assign(sls.service, basicDocumentation)
144
- const getAllFuncsStub = sinon.stub(sls.service, 'getAllFunctions').returns(['createUser'])
145
- const basicInvalidFunction = JSON.parse(JSON.stringify(basicValidFunction))
156
+ expect(succSpy.called).to.be.true;
157
+ expect(errSpy.called).to.be.false;
158
+ expect(validOpenAPIDocument).to.have.property("openapi");
146
159
 
147
- delete basicInvalidFunction.createUser.events[0].http.documentation.pathParams
148
- const getFuncStub = sinon.stub(sls.service, 'getFunction').returns(basicInvalidFunction.createUser)
149
-
150
- const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
151
- openAPIGenerator.processCliInput()
160
+ succSpy.restore();
161
+ errSpy.restore();
162
+ getAllFuncsStub.reset();
163
+ getFuncStub.reset();
164
+ });
152
165
 
153
- const validOpenAPIDocument = await openAPIGenerator.generationAndValidation()
154
- .catch(err => {
155
- expect(err.message).to.be.equal('AssertionError: Templated parameter name not found')
156
- })
166
+ it("should throw an error when trying to validate an invalid openAPI document", async function () {
167
+ const succSpy = sinon.spy(logOutput.log, "success");
168
+ const errSpy = sinon.spy(logOutput.log, "error");
169
+
170
+ Object.assign(sls.service, basicDocumentation);
171
+ const getAllFuncsStub = sinon
172
+ .stub(sls.service, "getAllFunctions")
173
+ .returns(["createUser"]);
174
+ const basicInvalidFunction = JSON.parse(
175
+ JSON.stringify(basicValidFunction)
176
+ );
177
+
178
+ delete basicInvalidFunction.createUser.events[0].http.documentation
179
+ .pathParams;
180
+ const getFuncStub = sinon
181
+ .stub(sls.service, "getFunction")
182
+ .returns(basicInvalidFunction.createUser);
183
+
184
+ const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
185
+ openAPIGenerator.processCliInput();
186
+
187
+ const validOpenAPIDocument = await openAPIGenerator
188
+ .generationAndValidation()
189
+ .catch((err) => {
190
+ expect(err.message).to.be.equal(
191
+ "AssertionError: Templated parameter name not found"
192
+ );
193
+ });
157
194
 
158
- expect(succSpy.called).to.be.false
159
- expect(errSpy.called).to.be.true
195
+ expect(succSpy.called).to.be.false;
196
+ expect(errSpy.called).to.be.true;
160
197
 
161
- succSpy.restore()
162
- errSpy.restore()
163
- getAllFuncsStub.reset()
164
- getFuncStub.reset()
165
- });
198
+ succSpy.restore();
199
+ errSpy.restore();
200
+ getAllFuncsStub.reset();
201
+ getFuncStub.reset();
202
+ });
203
+ });
204
+
205
+ describe("createPostman", () => {
206
+ it("should generate a postman collection when a valid openAPI file is generated", function () {
207
+ const fsStub = sinon.stub(fs, "writeFileSync").returns(true);
208
+ const succSpy = sinon.spy(logOutput.log, "success");
209
+ const errSpy = sinon.spy(logOutput.log, "error");
210
+ const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
211
+ openAPIGenerator.processCliInput();
212
+
213
+ openAPIGenerator.createPostman(validOpenAPI);
214
+
215
+ expect(fsStub.called).to.be.true;
216
+ expect(succSpy.calledTwice).to.be.true;
217
+ expect(errSpy.called).to.be.false;
218
+ fsStub.restore();
219
+ succSpy.restore();
220
+ errSpy.restore();
166
221
  });
167
222
 
168
- describe('createPostman', () => {
169
- it('should generate a postman collection when a valid openAPI file is generated', function() {
170
- const fsStub = sinon.stub(fs, 'writeFileSync').returns(true)
171
- const succSpy = sinon.spy(logOutput.log, 'success')
172
- const errSpy = sinon.spy(logOutput.log, 'error')
173
- const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
174
- openAPIGenerator.processCliInput()
175
-
176
- openAPIGenerator.createPostman(validOpenAPI)
177
-
178
- expect(fsStub.called).to.be.true
179
- expect(succSpy.calledTwice).to.be.true
180
- expect(errSpy.called).to.be.false
181
- fsStub.restore()
182
- succSpy.restore()
183
- errSpy.restore()
184
- });
185
-
186
- it('should throw an error when writing a file fails', function() {
187
- const errStub = sinon.stub(logOutput.log, 'error').returns('')
188
- const succSpy = sinon.spy(logOutput.log, 'success')
189
- const fsStub = sinon.stub(fs, 'writeFileSync').throws(new Error())
190
- const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
191
- openAPIGenerator.processCliInput()
192
-
193
- expect(() => {openAPIGenerator.createPostman(validOpenAPI)}).to.throw()
194
-
195
- expect(fsStub.called).to.be.true
196
- expect(errStub.called).to.be.true
197
- expect(succSpy.calledOnce).to.be.true
198
- expect(succSpy.calledTwice).to.be.false
199
- fsStub.restore()
200
- succSpy.restore()
201
- errStub.restore()
202
- });
223
+ it("should throw an error when writing a file fails", function () {
224
+ const errStub = sinon.stub(logOutput.log, "error").returns("");
225
+ const succSpy = sinon.spy(logOutput.log, "success");
226
+ const fsStub = sinon.stub(fs, "writeFileSync").throws(new Error());
227
+ const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
228
+ openAPIGenerator.processCliInput();
229
+
230
+ expect(() => {
231
+ openAPIGenerator.createPostman(validOpenAPI);
232
+ }).to.throw();
233
+
234
+ expect(fsStub.called).to.be.true;
235
+ expect(errStub.called).to.be.true;
236
+ expect(succSpy.calledOnce).to.be.true;
237
+ expect(succSpy.calledTwice).to.be.false;
238
+ fsStub.restore();
239
+ succSpy.restore();
240
+ errStub.restore();
241
+ });
203
242
 
204
- it('should throw an error converting an OpenAPI fails', function() {
205
- const errStub = sinon.spy(logOutput.log, 'error')
206
- const succSpy = sinon.spy(logOutput.log, 'success')
207
- const pgStub = sinon.stub(PostmanGenerator, 'convert')
208
- pgStub.yields(new Error())
243
+ it("should throw an error converting an OpenAPI fails", function () {
244
+ const errStub = sinon.spy(logOutput.log, "error");
245
+ const succSpy = sinon.spy(logOutput.log, "success");
246
+ const pgStub = sinon.stub(PostmanGenerator, "convert");
247
+ pgStub.yields(new Error());
209
248
 
210
- const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput)
211
- openAPIGenerator.processCliInput()
249
+ const openAPIGenerator = new OpenAPIGenerator(sls, {}, logOutput);
250
+ openAPIGenerator.processCliInput();
212
251
 
213
- expect(() => {openAPIGenerator.createPostman(validOpenAPI)}).to.throw()
252
+ expect(() => {
253
+ openAPIGenerator.createPostman(validOpenAPI);
254
+ }).to.throw();
214
255
 
215
- expect(errStub.called).to.be.true
216
- expect(succSpy.calledOnce).to.be.false
217
- expect(succSpy.calledTwice).to.be.false
256
+ expect(errStub.called).to.be.true;
257
+ expect(succSpy.calledOnce).to.be.false;
258
+ expect(succSpy.calledTwice).to.be.false;
218
259
 
219
- succSpy.restore()
220
- errStub.restore()
221
- });
260
+ succSpy.restore();
261
+ errStub.restore();
222
262
  });
263
+ });
223
264
  });