|
3 | 3 | validateOperationId,
|
4 | 4 | validateServerSecurity,
|
5 | 5 | validateChannels,
|
| 6 | + validateMessageId, |
6 | 7 | } = require('../lib/customValidators.js');
|
7 | 8 | const { checkErrorWrapper } = require('./testsUtils');
|
8 | 9 |
|
@@ -1076,6 +1077,273 @@ describe('validateOperationId()', function () {
|
1076 | 1077 | });
|
1077 | 1078 | });
|
1078 | 1079 |
|
| 1080 | +describe('validateMessageId()', function () { |
| 1081 | + const operations = ['subscribe', 'publish']; |
| 1082 | + |
| 1083 | + it('should successfully validate messageId', async function () { |
| 1084 | + const inputString = `{ |
| 1085 | + "asyncapi": "2.0.0", |
| 1086 | + "info": { |
| 1087 | + "version": "1.0.0" |
| 1088 | + }, |
| 1089 | + "channels": { |
| 1090 | + "test/1": { |
| 1091 | + "publish": { |
| 1092 | + "message": { |
| 1093 | + "messageId": "test1" |
| 1094 | + } |
| 1095 | + } |
| 1096 | + }, |
| 1097 | + "test/2": { |
| 1098 | + "subscribe": { |
| 1099 | + "message": { |
| 1100 | + "messageId": "test2" |
| 1101 | + } |
| 1102 | + } |
| 1103 | + } |
| 1104 | + } |
| 1105 | + }`; |
| 1106 | + const parsedInput = JSON.parse(inputString); |
| 1107 | + |
| 1108 | + expect( |
| 1109 | + validateMessageId(parsedInput, inputString, input, operations) |
| 1110 | + ).to.equal(true); |
| 1111 | + }); |
| 1112 | + |
| 1113 | + it('should throw error that messageIds are duplicated and that they duplicate', async function () { |
| 1114 | + const inputString = `{ |
| 1115 | + "asyncapi": "2.0.0", |
| 1116 | + "info": { |
| 1117 | + "version": "1.0.0" |
| 1118 | + }, |
| 1119 | + "channels": { |
| 1120 | + "test/1": { |
| 1121 | + "publish": { |
| 1122 | + "message": { |
| 1123 | + "messageId": "test" |
| 1124 | + } |
| 1125 | + } |
| 1126 | + }, |
| 1127 | + "test/2": { |
| 1128 | + "subscribe": { |
| 1129 | + "message": { |
| 1130 | + "messageId": "test" |
| 1131 | + } |
| 1132 | + } |
| 1133 | + }, |
| 1134 | + "test/3": { |
| 1135 | + "subscribe": { |
| 1136 | + "message": { |
| 1137 | + "messageId": "test" |
| 1138 | + } |
| 1139 | + } |
| 1140 | + }, |
| 1141 | + "test/4": { |
| 1142 | + "subscribe": { |
| 1143 | + "operationId": "test4" |
| 1144 | + } |
| 1145 | + } |
| 1146 | + } |
| 1147 | + }`; |
| 1148 | + const parsedInput = JSON.parse(inputString); |
| 1149 | + |
| 1150 | + const expectedErrorObject = { |
| 1151 | + type: 'https://github.com/asyncapi/parser-js/validation-errors', |
| 1152 | + title: 'messageId must be unique across all the messages.', |
| 1153 | + parsedJSON: parsedInput, |
| 1154 | + validationErrors: [ |
| 1155 | + { |
| 1156 | + title: |
| 1157 | + 'test/2/subscribe/message/messageId is a duplicate of: test/1/publish/message/messageId', |
| 1158 | + location: { |
| 1159 | + jsonPointer: '/channels/test~12/subscribe/message/messageId', |
| 1160 | + startLine: 17, |
| 1161 | + startColumn: 29, |
| 1162 | + startOffset: 337, |
| 1163 | + endLine: 17, |
| 1164 | + endColumn: 35, |
| 1165 | + endOffset: 343, |
| 1166 | + }, |
| 1167 | + }, |
| 1168 | + { |
| 1169 | + title: |
| 1170 | + 'test/3/subscribe/message/messageId is a duplicate of: test/1/publish/message/messageId', |
| 1171 | + location: { |
| 1172 | + jsonPointer: '/channels/test~13/subscribe/message/messageId', |
| 1173 | + startLine: 24, |
| 1174 | + startColumn: 29, |
| 1175 | + startOffset: 478, |
| 1176 | + endLine: 24, |
| 1177 | + endColumn: 35, |
| 1178 | + endOffset: 484, |
| 1179 | + }, |
| 1180 | + }, |
| 1181 | + ], |
| 1182 | + }; |
| 1183 | + |
| 1184 | + await checkErrorWrapper(async () => { |
| 1185 | + await validateMessageId(parsedInput, inputString, input, operations); |
| 1186 | + }, expectedErrorObject); |
| 1187 | + }); |
| 1188 | + |
| 1189 | + it('should throw error that messageIds are duplicated and that they duplicate with oneOf', async function () { |
| 1190 | + const inputString = `{ |
| 1191 | + "asyncapi": "2.0.0", |
| 1192 | + "info": { |
| 1193 | + "version": "1.0.0" |
| 1194 | + }, |
| 1195 | + "channels": { |
| 1196 | + "test/1": { |
| 1197 | + "publish": { |
| 1198 | + "message": { |
| 1199 | + "oneOf": [{ |
| 1200 | + "messageId": "test1" |
| 1201 | + }] |
| 1202 | + } |
| 1203 | + } |
| 1204 | + }, |
| 1205 | + "test/2": { |
| 1206 | + "subscribe": { |
| 1207 | + "message": { |
| 1208 | + "messageId": "test1" |
| 1209 | + } |
| 1210 | + } |
| 1211 | + }, |
| 1212 | + "test/3": { |
| 1213 | + "subscribe": { |
| 1214 | + "message": { |
| 1215 | + "oneOf": [ |
| 1216 | + { |
| 1217 | + "messageId": "test2" |
| 1218 | + }, |
| 1219 | + { |
| 1220 | + "messageId": "test2" |
| 1221 | + } |
| 1222 | + ] |
| 1223 | + } |
| 1224 | + } |
| 1225 | + }, |
| 1226 | + "test/4": { |
| 1227 | + "subscribe": { |
| 1228 | + "operationId": "test4" |
| 1229 | + } |
| 1230 | + } |
| 1231 | + } |
| 1232 | + }`; |
| 1233 | + const parsedInput = JSON.parse(inputString); |
| 1234 | + |
| 1235 | + const expectedErrorObject = { |
| 1236 | + type: 'https://github.com/asyncapi/parser-js/validation-errors', |
| 1237 | + title: 'messageId must be unique across all the messages.', |
| 1238 | + parsedJSON: parsedInput, |
| 1239 | + validationErrors: [ |
| 1240 | + { |
| 1241 | + title: |
| 1242 | + 'test/2/subscribe/message/messageId is a duplicate of: test/1/publish/message/oneOf/messageId', |
| 1243 | + location: { |
| 1244 | + jsonPointer: '/channels/test~12/subscribe/message/messageId', |
| 1245 | + startLine: 19, |
| 1246 | + startColumn: 29, |
| 1247 | + startOffset: 383, |
| 1248 | + endLine: 19, |
| 1249 | + endColumn: 36, |
| 1250 | + endOffset: 390, |
| 1251 | + }, |
| 1252 | + }, |
| 1253 | + { |
| 1254 | + title: |
| 1255 | + 'test/3/subscribe/message/oneOf/messageId is a duplicate of: test/3/subscribe/message/oneOf/messageId', |
| 1256 | + location: { |
| 1257 | + jsonPointer: '/channels/test~13/subscribe/message/oneOf/messageId', |
| 1258 | + startLine: 28, |
| 1259 | + startColumn: 33, |
| 1260 | + startOffset: 572, |
| 1261 | + endLine: 28, |
| 1262 | + endColumn: 40, |
| 1263 | + endOffset: 579, |
| 1264 | + }, |
| 1265 | + }, |
| 1266 | + ], |
| 1267 | + }; |
| 1268 | + |
| 1269 | + await checkErrorWrapper(async () => { |
| 1270 | + await validateMessageId(parsedInput, inputString, input, operations); |
| 1271 | + }, expectedErrorObject); |
| 1272 | + }); |
| 1273 | + |
| 1274 | + it('should only throw error for message that have defined duplicate messageIds', async function () { |
| 1275 | + const inputString = `{ |
| 1276 | + "asyncapi": "2.0.0", |
| 1277 | + "info": { |
| 1278 | + "version": "1.0.0" |
| 1279 | + }, |
| 1280 | + "channels": { |
| 1281 | + "test/1": { |
| 1282 | + "publish": { |
| 1283 | + "message": { |
| 1284 | + "oneOf": [{ |
| 1285 | + "messageId": "test1" |
| 1286 | + }] |
| 1287 | + } |
| 1288 | + } |
| 1289 | + }, |
| 1290 | + "test/2": { |
| 1291 | + "subscribe": { |
| 1292 | + "message": { |
| 1293 | + "name": "test1" |
| 1294 | + } |
| 1295 | + } |
| 1296 | + }, |
| 1297 | + "test/3": { |
| 1298 | + "subscribe": { |
| 1299 | + "message": { |
| 1300 | + "oneOf": [ |
| 1301 | + { |
| 1302 | + "messageId": "test2" |
| 1303 | + }, |
| 1304 | + { |
| 1305 | + "messageId": "test2" |
| 1306 | + } |
| 1307 | + ] |
| 1308 | + } |
| 1309 | + } |
| 1310 | + }, |
| 1311 | + "test/4": { |
| 1312 | + "subscribe": { |
| 1313 | + "operationId": "test4" |
| 1314 | + } |
| 1315 | + } |
| 1316 | + } |
| 1317 | + }`; |
| 1318 | + const parsedInput = JSON.parse(inputString); |
| 1319 | + |
| 1320 | + const expectedErrorObject = { |
| 1321 | + type: 'https://github.com/asyncapi/parser-js/validation-errors', |
| 1322 | + title: 'messageId must be unique across all the messages.', |
| 1323 | + parsedJSON: parsedInput, |
| 1324 | + validationErrors: [ |
| 1325 | + { |
| 1326 | + title: |
| 1327 | + 'test/3/subscribe/message/oneOf/messageId is a duplicate of: test/3/subscribe/message/oneOf/messageId', |
| 1328 | + location: { |
| 1329 | + jsonPointer: '/channels/test~13/subscribe/message/oneOf/messageId', |
| 1330 | + startLine: 28, |
| 1331 | + startColumn: 33, |
| 1332 | + startOffset: 567, |
| 1333 | + endLine: 28, |
| 1334 | + endColumn: 40, |
| 1335 | + endOffset: 574, |
| 1336 | + }, |
| 1337 | + }, |
| 1338 | + ], |
| 1339 | + }; |
| 1340 | + |
| 1341 | + await checkErrorWrapper(async () => { |
| 1342 | + await validateMessageId(parsedInput, inputString, input, operations); |
| 1343 | + }, expectedErrorObject); |
| 1344 | + }); |
| 1345 | +}); |
| 1346 | + |
1079 | 1347 | describe('validateServerSecurity()', function () {
|
1080 | 1348 | const specialSecTypes = ['oauth2', 'openIdConnect'];
|
1081 | 1349 |
|
|
0 commit comments