|
1 | 1 | "use strict"; |
2 | 2 |
|
| 3 | +const expect = require("chai").expect; |
| 4 | +const sinon = require('sinon'); |
| 5 | + |
| 6 | + |
3 | 7 | const fs = require("fs").promises; |
4 | 8 | const path = require("path"); |
5 | | -const expect = require("chai").expect; |
6 | 9 |
|
7 | 10 | const serverlessMock = require("../helpers/serverless"); |
8 | 11 | const modelsDocument = require("../models/models/models.json"); |
| 12 | + |
| 13 | +const schemaHandler = require('../../src/schemaHandler'); |
| 14 | + |
9 | 15 | const DefinitionGenerator = require("../../src/definitionGenerator"); |
10 | 16 |
|
11 | 17 | describe("DefinitionGenerator", () => { |
@@ -1020,31 +1026,233 @@ describe("DefinitionGenerator", () => { |
1020 | 1026 | }); |
1021 | 1027 | }); |
1022 | 1028 |
|
1023 | | - describe(`createResponses`, async function () { |
1024 | | - it(`handles creating headers with pragma as a default`, async function () { |
1025 | | - const description = "this is a description"; |
1026 | | - const responseMock = { |
1027 | | - methodResponses: [ |
1028 | | - { |
1029 | | - responseBody: { description: description }, |
1030 | | - statusCode: 200, |
1031 | | - owasp: { pragma: true }, |
1032 | | - }, |
1033 | | - ], |
1034 | | - }; |
| 1029 | + describe(`createRequestBody`, function () { |
| 1030 | + it(`required should default to false if required is not true`, async function () { |
| 1031 | + const requestBody = { |
| 1032 | + models: { |
| 1033 | + 'application/json': 'PostRequestBody' |
| 1034 | + } |
| 1035 | + } |
| 1036 | + |
| 1037 | + const definitionGenerator = new DefinitionGenerator( |
| 1038 | + mockServerless, |
| 1039 | + logger |
| 1040 | + ); |
| 1041 | + |
| 1042 | + const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/PostRequestBody') |
| 1043 | + |
| 1044 | + const expected = await definitionGenerator.createRequestBody(requestBody) |
| 1045 | + |
| 1046 | + expect(expected).to.be.an('object'); |
| 1047 | + expect(expected).to.not.have.property('description'); |
| 1048 | + expect(expected).to.have.property('required', false); |
| 1049 | + expect(expected).to.have.property('content'); |
| 1050 | + |
| 1051 | + stub.restore(); |
| 1052 | + }); |
| 1053 | + |
| 1054 | + it(`required should be true if required is true`, async function () { |
| 1055 | + const requestBody = { |
| 1056 | + required: true, |
| 1057 | + models: { |
| 1058 | + 'application/json': 'PostRequestBody' |
| 1059 | + } |
| 1060 | + } |
| 1061 | + |
| 1062 | + const definitionGenerator = new DefinitionGenerator( |
| 1063 | + mockServerless, |
| 1064 | + logger |
| 1065 | + ); |
| 1066 | + |
| 1067 | + const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/PostRequestBody') |
| 1068 | + |
| 1069 | + const expected = await definitionGenerator.createRequestBody(requestBody) |
| 1070 | + |
| 1071 | + expect(expected).to.be.an('object'); |
| 1072 | + expect(expected).to.not.have.property('description'); |
| 1073 | + expect(expected).to.have.property('required', true); |
| 1074 | + expect(expected).to.have.property('content'); |
| 1075 | + |
| 1076 | + stub.restore(); |
| 1077 | + }); |
| 1078 | + |
| 1079 | + it(`should have a description when description is passed through`, async function () { |
| 1080 | + const description = 'a description' |
| 1081 | + const requestBody = { |
| 1082 | + description, |
| 1083 | + models: { |
| 1084 | + 'application/json': 'PostRequestBody' |
| 1085 | + } |
| 1086 | + } |
1035 | 1087 |
|
1036 | 1088 | const definitionGenerator = new DefinitionGenerator( |
1037 | 1089 | mockServerless, |
1038 | 1090 | logger |
1039 | 1091 | ); |
1040 | 1092 |
|
1041 | | - const response = await definitionGenerator.createResponses(responseMock); |
| 1093 | + const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/PostRequestBody') |
| 1094 | + |
| 1095 | + const expected = await definitionGenerator.createRequestBody(requestBody) |
| 1096 | + |
| 1097 | + expect(expected).to.be.an('object'); |
| 1098 | + expect(expected).to.have.property('description', description); |
| 1099 | + expect(expected).to.have.property('required', false); |
| 1100 | + expect(expected).to.have.property('content'); |
| 1101 | + |
| 1102 | + stub.restore(); |
| 1103 | + }); |
| 1104 | + |
| 1105 | + it(`should handle more than one requestModel type`, async function () { |
| 1106 | + const description = 'a description' |
| 1107 | + const requestBody = { |
| 1108 | + description, |
| 1109 | + models: { |
| 1110 | + 'application/json': 'PostRequestBody', |
| 1111 | + 'application/xml': 'PostRequestBodyXML', |
| 1112 | + } |
| 1113 | + } |
| 1114 | + |
| 1115 | + const definitionGenerator = new DefinitionGenerator( |
| 1116 | + mockServerless, |
| 1117 | + logger |
| 1118 | + ); |
| 1119 | + |
| 1120 | + const stub = sinon.stub(schemaHandler.prototype, 'createSchema').onFirstCall().resolves('#components/schemas/PostRequestBody').onSecondCall().resolves('#components/schemas/PostRequestBodyXML'); |
| 1121 | + |
| 1122 | + const expected = await definitionGenerator.createRequestBody(requestBody) |
| 1123 | + |
| 1124 | + expect(expected).to.be.an('object'); |
| 1125 | + expect(expected).to.have.property('description', description); |
| 1126 | + expect(expected).to.have.property('required', false); |
| 1127 | + expect(expected).to.have.property('content'); |
| 1128 | + expect(expected.content).to.be.an('object'); |
| 1129 | + expect(Object.keys(expected.content)).to.have.lengthOf(2); |
| 1130 | + expect(Object.keys(expected.content)).to.be.eql(['application/json', 'application/xml']); |
| 1131 | + |
| 1132 | + stub.restore(); |
| 1133 | + }); |
| 1134 | + }); |
| 1135 | + |
| 1136 | + describe(`createResponses`, async function () { |
| 1137 | + describe(`responseHeaders`, function () { |
| 1138 | + it(`does not create headers when headers are not specified`, async function () { |
| 1139 | + const description = "this is a description"; |
| 1140 | + const responseMock = { |
| 1141 | + methodResponses: [ |
| 1142 | + { |
| 1143 | + responseBody: { description: description }, |
| 1144 | + statusCode: 200, |
| 1145 | + }, |
| 1146 | + ], |
| 1147 | + }; |
| 1148 | + |
| 1149 | + const definitionGenerator = new DefinitionGenerator( |
| 1150 | + mockServerless, |
| 1151 | + logger |
| 1152 | + ); |
| 1153 | + |
| 1154 | + const response = await definitionGenerator.createResponses(responseMock); |
| 1155 | + |
| 1156 | + expect(response).to.be.an("object"); |
| 1157 | + expect(response).to.have.property("200"); |
| 1158 | + expect(response["200"]).to.have.property("description", description); |
| 1159 | + expect(response["200"]).to.not.have.property('headers'); |
| 1160 | + }); |
| 1161 | + |
| 1162 | + it(`only creates headers when headers are specified`, async function () { |
| 1163 | + const description = "this is a description"; |
| 1164 | + const responseMock = { |
| 1165 | + methodResponses: [ |
| 1166 | + { |
| 1167 | + responseBody: { description: description }, |
| 1168 | + statusCode: 200, |
| 1169 | + responseHeaders: { |
| 1170 | + 'x-rate-limit': { |
| 1171 | + description: 'The number of allowed requests in the current period', |
| 1172 | + schema: { |
| 1173 | + type: 'integer' |
| 1174 | + } |
| 1175 | + } |
| 1176 | + } |
| 1177 | + }, |
| 1178 | + ], |
| 1179 | + }; |
| 1180 | + |
| 1181 | + const definitionGenerator = new DefinitionGenerator( |
| 1182 | + mockServerless, |
| 1183 | + logger |
| 1184 | + ); |
| 1185 | + |
| 1186 | + const response = await definitionGenerator.createResponses(responseMock); |
| 1187 | + |
| 1188 | + expect(response).to.be.an("object"); |
| 1189 | + expect(response).to.have.property("200"); |
| 1190 | + expect(response["200"]).to.have.property("description", description); |
| 1191 | + expect(response["200"]).to.have.property('headers'); |
| 1192 | + expect(response["200"].headers).to.be.an("object"); |
| 1193 | + expect(response["200"].headers).to.have.property("x-rate-limit"); |
| 1194 | + }); |
| 1195 | + |
| 1196 | + it(`handles creating headers with pragma as a default`, async function () { |
| 1197 | + const description = "this is a description"; |
| 1198 | + const responseMock = { |
| 1199 | + methodResponses: [ |
| 1200 | + { |
| 1201 | + responseBody: { description: description }, |
| 1202 | + statusCode: 200, |
| 1203 | + owasp: { pragma: true }, |
| 1204 | + }, |
| 1205 | + ], |
| 1206 | + }; |
| 1207 | + |
| 1208 | + const definitionGenerator = new DefinitionGenerator( |
| 1209 | + mockServerless, |
| 1210 | + logger |
| 1211 | + ); |
| 1212 | + |
| 1213 | + const response = await definitionGenerator.createResponses(responseMock); |
| 1214 | + |
| 1215 | + expect(response).to.be.an("object"); |
| 1216 | + expect(response).to.have.property("200"); |
| 1217 | + expect(response["200"]).to.have.property("description", description); |
| 1218 | + expect(response["200"].headers).to.be.an("object"); |
| 1219 | + expect(response["200"].headers).to.have.property("Pragma"); |
| 1220 | + }); |
1042 | 1221 |
|
1043 | | - expect(response).to.be.an("object"); |
1044 | | - expect(response).to.have.property("200"); |
1045 | | - expect(response["200"]).to.have.property("description", description); |
1046 | | - expect(response["200"].headers).to.be.an("object"); |
1047 | | - expect(response["200"].headers).to.have.property("Pragma"); |
| 1222 | + it(`handles creating headers with owasp and user specified headers`, async function () { |
| 1223 | + const description = "this is a description"; |
| 1224 | + const responseMock = { |
| 1225 | + methodResponses: [ |
| 1226 | + { |
| 1227 | + responseBody: { description: description }, |
| 1228 | + statusCode: 200, |
| 1229 | + responseHeaders: { |
| 1230 | + 'x-rate-limit': { |
| 1231 | + description: 'The number of allowed requests in the current period', |
| 1232 | + schema: { |
| 1233 | + type: 'integer' |
| 1234 | + } |
| 1235 | + } |
| 1236 | + }, |
| 1237 | + owasp: { pragma: true }, |
| 1238 | + }, |
| 1239 | + ], |
| 1240 | + }; |
| 1241 | + |
| 1242 | + const definitionGenerator = new DefinitionGenerator( |
| 1243 | + mockServerless, |
| 1244 | + logger |
| 1245 | + ); |
| 1246 | + |
| 1247 | + const response = await definitionGenerator.createResponses(responseMock); |
| 1248 | + |
| 1249 | + expect(response).to.be.an("object"); |
| 1250 | + expect(response).to.have.property("200"); |
| 1251 | + expect(response["200"]).to.have.property("description", description); |
| 1252 | + expect(response["200"].headers).to.be.an("object"); |
| 1253 | + expect(response["200"].headers).to.have.property("Pragma"); |
| 1254 | + expect(response["200"].headers).to.have.property("x-rate-limit"); |
| 1255 | + }); |
1048 | 1256 | }); |
1049 | 1257 | }); |
1050 | 1258 | }); |
0 commit comments