UNPKG

@apideck/portman

Version:

Port OpenAPI Spec to Postman Collection, with contract & variation tests included

232 lines 13.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testResponseBodyContent = void 0; var application_1 = require("../../application"); var utils_1 = require("../../utils"); var openapi_format_1 = require("openapi-format"); var testResponseBodyContent = function (responseBodyTests, pmOperation, config) { responseBodyTests.map(function (check) { var _a; var pmJsonData = ''; var pmMappedData = ''; var pmHelperContains = ''; var pmTestKey = ''; var pmTestValue = ''; var pmTestContains = ''; var pmTestOneOf = ''; var pmTestLength = ''; var pmTestMinLength = ''; var pmTestMaxLength = ''; var pmTestAssert = ''; var split = (_a = config === null || config === void 0 ? void 0 : config.separatorSymbol) !== null && _a !== void 0 ? _a : '::'; var checkKey = check.key; if (!pmOperation.testJsonDataInjected) { pmJsonData = [ "// Set response object as internal variable\n", "let jsonData = {};\n", "try {jsonData = pm.response.json();}catch(e){}\n" ].join(''); pmOperation.testJsonDataInjected = true; } var sourceVarName = ''; if (check.key.includes('[*]') && check.value) { var keyPaths = check.key.split('[*].'); if (keyPaths.length === 2) { var keyArrayPath = "".concat((0, utils_1.renderChainPath)("jsonData.".concat(keyPaths[0]))); var safeKeyPart = (0, utils_1.sanitizeKeyForVar)(keyPaths[0].replace(/\[/g, '')); var pathArrayVarName = "_resArray".concat((0, openapi_format_1.changeCase)(safeKeyPart, 'pascalCase')); var safeKeyFull = (0, utils_1.sanitizeKeyForVar)(check.key.replace(/\[/g, '')); sourceVarName = "_resArray".concat((0, openapi_format_1.changeCase)(safeKeyFull, 'pascalCase')); if (!pmOperation.mappedVars.includes(pathArrayVarName)) { pmOperation.registerVar(pathArrayVarName); pmOperation.registerVar(sourceVarName); pmHelperContains = [ "const ".concat(pathArrayVarName, " = ").concat(keyArrayPath, ";\n"), "const ".concat(sourceVarName, " = ").concat(pathArrayVarName, ".find(c => c.").concat(keyPaths[1], " === \"").concat(check.value, "\");\n") ].join(''); checkKey = keyPaths[1]; } } } var isRoot = checkKey === '.'; var isArray = checkKey.startsWith('['); var sourceData = sourceVarName ? "".concat(sourceVarName) : "jsonData"; var keyLabel = isRoot ? "ROOT" : "".concat(checkKey); var keySafeValue = (0, utils_1.renderBracketPath)(checkKey); var keyValue = isRoot ? "" : isArray || keySafeValue.startsWith('[') ? "".concat(keySafeValue) : ".".concat(keySafeValue); var keyPath = "".concat((0, utils_1.renderChainPath)("".concat(sourceData).concat(keyValue))); var safeKeyForVar = (0, utils_1.sanitizeKeyForVar)(keyValue.replace(/\[/g, '')); var pathVarName = "_".concat((0, openapi_format_1.changeCase)("res".concat(safeKeyForVar), 'camelCase')); if (check.hasOwnProperty('key')) { if (!pmOperation.mappedVars.includes(pathVarName)) { pmOperation.registerVar(pathVarName); pmMappedData = [ "// Set property value as variable\n", "const ".concat(pathVarName, " = ").concat(keyPath, ";\n") ].join(''); } var negate = check.notExist === true ? '===' : '!=='; var negateLabel = check.notExist === true ? 'not exists' : 'exists'; var label = check.notExist === true ? 'not have' : 'have'; pmTestKey = [ "// Response body should ".concat(label, " \"").concat(keyLabel, "\"\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if '".concat(keyLabel, "' ").concat(negateLabel, "\", function() {\n"), " pm.expect(".concat(pathVarName, " ").concat(negate, " undefined).to.be.true;\n"), "});\n" ].join(''); } if (check.hasOwnProperty('value')) { var checkValue = check.value; if (typeof check.value === 'string') { checkValue = "\"".concat(check.value, "\""); if (check.value.includes('{{') && check.value.includes('}}')) { checkValue = "pm.collectionVariables.get(\"".concat(check.value.replace(/{{|}}/g, ''), "\")"); } } pmTestValue = [ "// Response body should have value \"".concat(check.value, "\" for \"").concat(keyLabel, "\"\n"), "if (".concat(pathVarName, " !== undefined) {\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if value for '".concat(keyLabel, "' matches '").concat(check.value, "'\", function() {\n"), " pm.expect(".concat(pathVarName, ").to.eql(").concat(checkValue, ");\n"), "})};\n" ].join(''); } if (check.hasOwnProperty('contains')) { var checkContains = check.contains; if (typeof check.contains === 'string') { checkContains = "\"".concat(check.contains, "\""); if (check.contains.includes('{{') && check.contains.includes('}}')) { checkContains = "pm.collectionVariables.get(\"".concat(check.contains.replace(/{{|}}/g, ''), "\")"); } } pmTestContains = [ "// Response body should contain value \"".concat(check.contains, "\" for \"").concat(keyLabel, "\"\n"), "if (".concat(pathVarName, " !== undefined) {\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if value for '".concat(keyLabel, "' contains '").concat(check.contains, "'\", function() {\n"), " pm.expect(".concat(pathVarName, ").to.include(").concat(checkContains, ");\n"), "})};\n" ].join(''); } if (check.hasOwnProperty('oneOf')) { if (Array.isArray(check.oneOf)) { var safeOneOf = check.oneOf.map(function (item) { if (typeof item === 'string') { var checkOneOfItem = item; if (checkOneOfItem.includes('{{') && checkOneOfItem.includes('}}')) { checkOneOfItem = "pm.collectionVariables.get(\"".concat(checkOneOfItem.replace(/{{|}}/g, ''), "\")"); return checkOneOfItem; } return "\"".concat(checkOneOfItem, "\""); } return item; }); pmTestOneOf = [ "// Response body should be one of the values \"".concat(check.oneOf, "\" for \"").concat(keyLabel, "\"\n"), "if (".concat(pathVarName, " !== undefined) {\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if value for '".concat(keyLabel, "' is matching one of: '").concat(check.oneOf, "'\", function() {\n"), " pm.expect(".concat(pathVarName, ").to.be.oneOf([").concat(safeOneOf, "]);\n"), "})};\n" ].join(''); } } if (check.hasOwnProperty('length')) { var checkLength = check.length; if (typeof check.length === 'string') { checkLength = "\"".concat(check.length, "\""); if (check.length.includes('{{') && check.length.includes('}}')) { checkLength = "pm.collectionVariables.get(\"".concat(check.length.replace(/{{|}}/g, ''), "\")"); } } pmTestLength = [ "// Response body should have a length of \"".concat(check.length, "\" for \"").concat(keyLabel, "\"\n"), "if (".concat(pathVarName, " !== undefined) {\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if value of '".concat(keyLabel, "' has a length of '").concat(check.length, "'\", function() {\n"), " pm.expect(".concat(sourceData).concat(keyValue, ".length).to.equal(").concat(checkLength, ");\n"), "})};\n" ].join(''); } if (check.hasOwnProperty('minLength')) { var checkMinLength = check.minLength; if (typeof check.minLength === 'string') { checkMinLength = "\"".concat(check.minLength, "\""); if (check.minLength.includes('{{') && check.minLength.includes('}}')) { checkMinLength = "pm.collectionVariables.get(\"".concat(check.minLength.replace(/{{|}}/g, ''), "\")"); } } pmTestMinLength = [ "// Response body should have a minimum length of \"".concat(check.minLength, "\" for \"").concat(keyLabel, "\"\n"), "if (".concat(pathVarName, " !== undefined) {\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if value of '".concat(keyLabel, "' has a minimum length of '").concat(check.minLength, "'\", function() {\n"), " pm.expect(".concat(sourceData).concat(keyValue, ".length).is.at.least(").concat(checkMinLength, ");\n"), "})};\n" ].join(''); } if (check.hasOwnProperty('maxLength')) { var checkMaxLength = check.maxLength; if (typeof check.maxLength === 'string') { checkMaxLength = "\"".concat(check.maxLength, "\""); if (check.maxLength.includes('{{') && check.maxLength.includes('}}')) { checkMaxLength = "pm.collectionVariables.get(\"".concat(check.maxLength.replace(/{{|}}/g, ''), "\")"); } } pmTestMaxLength = [ "// Response body should have a maximum length of \"".concat(check.maxLength, "\" for \"").concat(keyLabel, "\"\n"), "if (".concat(pathVarName, " !== undefined) {\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if value of '".concat(keyLabel, "' has a maximum length of '").concat(check.maxLength, "'\", function() {\n"), " pm.expect(".concat(sourceData).concat(keyValue, ".length).is.at.most(").concat(checkMaxLength, ");\n"), "})};\n" ].join(''); } if (check.hasOwnProperty('assert') && check.assert) { var cleanAssert = check.assert .replace(/\.\./g, '.') .replace(/^\.|\.$/g, '') .replace(/'/g, '"'); var cleanAssertLabel = cleanAssert.replace(/"/g, "'"); pmTestAssert = [ "// Response body value for \"".concat(keyLabel, "\" \"").concat(cleanAssert, "\"\n"), "if (".concat(pathVarName, " !== undefined) {\n"), "pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path), " - Content check if value for '".concat(keyLabel, "' '").concat(cleanAssertLabel, "'\", function() {\n"), " pm.expect(".concat(sourceData).concat(keyValue, ").").concat(cleanAssert, ";\n"), "})};\n" ].join(''); } if (pmJsonData !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmJsonData); if (pmHelperContains !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmHelperContains); if (pmMappedData !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmMappedData); if (pmTestKey !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestKey); if (pmTestValue !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestValue); if (pmTestContains !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestContains); if (pmTestOneOf !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestOneOf); if (pmTestLength !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestLength); if (pmTestMinLength !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestMinLength); if (pmTestMaxLength !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestMaxLength); if (pmTestAssert !== '') (0, application_1.writeOperationTestScript)(pmOperation, pmTestAssert); }); return pmOperation; }; exports.testResponseBodyContent = testResponseBodyContent; //# sourceMappingURL=testResponseBodyContent.js.map