UNPKG

@apideck/portman

Version:

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

394 lines 24.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestSuite = void 0; var tslib_1 = require("tslib"); var _1 = require("."); var types_1 = require("../types"); var utils_1 = require("../utils"); var inOperations_1 = require("../utils/inOperations"); var utils_2 = require("../utils"); var getRequestBodyExample_1 = require("../utils/getRequestBodyExample"); var TestSuite = (function () { function TestSuite(testSuiteOptions) { var _this = this; this.track = { openApiOperationIds: [], openApiOperations: [] }; this.setupTests = function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; if (!((_a = _this.config) === null || _a === void 0 ? void 0 : _a.tests)) return; _this.contractTests = (_c = (_b = _this.config) === null || _b === void 0 ? void 0 : _b.tests) === null || _c === void 0 ? void 0 : _c.contractTests; _this.contentTests = (_e = (_d = _this.config) === null || _d === void 0 ? void 0 : _d.tests) === null || _e === void 0 ? void 0 : _e.contentTests; _this.variationTests = (_g = (_f = _this.config) === null || _f === void 0 ? void 0 : _f.tests) === null || _g === void 0 ? void 0 : _g.variationTests; _this.integrationTests = (_j = (_h = _this.config) === null || _h === void 0 ? void 0 : _h.tests) === null || _j === void 0 ? void 0 : _j.integrationTests; _this.extendTests = (_l = (_k = _this.config) === null || _k === void 0 ? void 0 : _k.tests) === null || _l === void 0 ? void 0 : _l.extendTests; }; this.generateContractTests = function (pmOperations, oaOperation, contractTests, openApiResponseCode, openApiContentType) { var _a; var tests = contractTests || _this.contractTests; if (!tests || ((_a = _this.options) === null || _a === void 0 ? void 0 : _a.includeTests) === false) return; tests.map(function (contractTest) { var operations = pmOperations || _this.getOperationsFromSetting(contractTest); operations.map(function (pmOperation) { var _a, _b, _c; var operation = oaOperation || _this.oasParser.getOperationByPath(pmOperation.pathRef); if (operation) { if (contractTest.openApiRequest) { var reqInfo = (0, utils_2.parseOpenApiRequest)(contractTest.openApiRequest); var reqContentType_1 = reqInfo === null || reqInfo === void 0 ? void 0 : reqInfo.contentType; if (reqContentType_1 && reqContentType_1.includes('*') && ((_a = operation.schema) === null || _a === void 0 ? void 0 : _a.requestBody)) { var reqObj = operation.schema.requestBody; var matchCt = Object.keys(reqObj.content || {}).find(function (ct) { return (0, utils_2.matchWildcard)(ct, reqContentType_1); }); if (matchCt) reqContentType_1 = matchCt; } if (reqContentType_1) { pmOperation.item.request.upsertHeader({ key: 'Content-Type', value: reqContentType_1 }); var reqBodyObj = operation.schema.requestBody; var example = (0, utils_2.getRequestBodyExample)(reqBodyObj, reqContentType_1); if (example && pmOperation.item.request.body) { pmOperation.item.request.body.mode = 'raw'; pmOperation.item.request.body.raw = example; var lang = (0, getRequestBodyExample_1.getRawLanguageFromContentType)(reqContentType_1); pmOperation.item.request.body.options = { raw: { language: lang, headerFamily: lang } }; } } } var respCode = openApiResponseCode; var respContentType_1 = openApiContentType; if (contractTest.openApiResponse) { var respInfo = (0, utils_2.parseOpenApiResponse)(contractTest.openApiResponse); if (respInfo) { respCode = respInfo.code; respContentType_1 = respInfo.contentType; if (respContentType_1 && respContentType_1.includes('*') && ((_c = (_b = operation.schema) === null || _b === void 0 ? void 0 : _b.responses) === null || _c === void 0 ? void 0 : _c[respCode])) { var respObj = operation.schema.responses[respCode]; var matchCt = Object.keys(respObj.content || {}).find(function (ct) { return (0, utils_2.matchWildcard)(ct, respContentType_1); }); if (matchCt) respContentType_1 = matchCt; } if (respContentType_1) { pmOperation.item.request.upsertHeader({ key: 'Accept', value: respContentType_1 }); } } } _this.injectContractTests(pmOperation, operation, contractTest, respCode, respContentType_1); _this.registerOperationTestType(pmOperation, types_1.PortmanTestTypes.contract, false); } }); }); }; this.generateVariationTests = function () { var variationTests = _this.variationTests; if (!variationTests) return; variationTests.map(function (variationTest) { var pmOperations = _this.getOperationsFromSetting(variationTest); pmOperations.map(function (pmOperation) { var oaOperation = _this.oasParser.getOperationByPath(pmOperation.pathRef); if (!variationTest.openApiResponse) { _this.variationWriter.add(pmOperation, oaOperation, variationTest); } else { var respInfo_1 = (0, utils_2.parseOpenApiResponse)(variationTest.openApiResponse); if (respInfo_1 && oaOperation) { var matchCodes = []; if (respInfo_1.code.includes('*')) { matchCodes = oaOperation.responseCodes.filter(function (code) { return (0, utils_2.matchWildcard)(code, respInfo_1.code); }); } else if (oaOperation.responseCodes.includes(respInfo_1.code)) { matchCodes = [respInfo_1.code]; } if (matchCodes.length > 0) { _this.variationWriter.add(pmOperation, oaOperation, variationTest); } } } }); }); _this.collection = _this.variationWriter.mergeToCollection(_this.collection); }; this.generateIntegrationTests = function () { var integrationTests = _this.integrationTests; if (!integrationTests) return; integrationTests.map(function (integrationTest) { _this.integrationTestWriter.add(integrationTest); }); _this.collection = _this.integrationTestWriter.mergeToCollection(_this.collection); }; this.injectContractTests = function (pmOperation, oaOperation, contractTest, openApiResponseCode, openApiContentType) { var e_1, _a; var _b, _c, _d, _e, _f; if (!((_b = oaOperation.schema) === null || _b === void 0 ? void 0 : _b.responses)) return pmOperation; var response = Object.entries(oaOperation.schema.responses).filter(function (res) { return (0, utils_1.inRange)(parseInt(res[0]), 200, 302); }); if (openApiResponseCode && typeof openApiResponseCode === 'string') { if (openApiResponseCode.toLowerCase() === 'default') { response = Object.entries(oaOperation.schema.responses).filter(function (res) { return res[0] === 'default'; }); } else { response = Object.entries(oaOperation.schema.responses).filter(function (res) { return parseInt(res[0]) === parseInt(openApiResponseCode); }); } } if (!((_c = response[0]) === null || _c === void 0 ? void 0 : _c[1])) return pmOperation; var responseKey = response[0][0]; var responseCode = parseInt(responseKey); var responseObject = response[0][1]; var optStatusSuccess = contractTest['statusSuccess']; var optStatusCode = contractTest['statusCode']; var optResponseTime = contractTest['responseTime']; var optContentType = contractTest['contentType']; var optJsonBody = contractTest['jsonBody']; var optSchemaValidation = contractTest['schemaValidation']; var optHeadersPresent = contractTest['headersPresent']; if (optStatusSuccess && optStatusSuccess.enabled && !(0, inOperations_1.inOperations)(pmOperation, optStatusSuccess === null || optStatusSuccess === void 0 ? void 0 : optStatusSuccess.excludeForOperations)) { pmOperation = (0, _1.testResponseStatusSuccess)(pmOperation, (_d = _this.config) === null || _d === void 0 ? void 0 : _d.globals); } if (optStatusCode && optStatusCode.enabled && !(0, inOperations_1.inOperations)(pmOperation, optStatusCode === null || optStatusCode === void 0 ? void 0 : optStatusCode.excludeForOperations)) { var statusCodeSetting = tslib_1.__assign({}, optStatusCode); if (!statusCodeSetting.code && responseCode) { statusCodeSetting.code = responseCode; } pmOperation = (0, _1.testResponseStatusCode)(statusCodeSetting, pmOperation); } if (optResponseTime && optResponseTime.enabled && !(0, inOperations_1.inOperations)(pmOperation, optResponseTime === null || optResponseTime === void 0 ? void 0 : optResponseTime.excludeForOperations)) { pmOperation = (0, _1.testResponseTime)(optResponseTime, pmOperation, (_e = _this.config) === null || _e === void 0 ? void 0 : _e.globals); } if (optJsonBody && optJsonBody.enabled && responseCode === 204 && !(0, inOperations_1.inOperations)(pmOperation, optJsonBody === null || optJsonBody === void 0 ? void 0 : optJsonBody.excludeForOperations)) { pmOperation = (0, _1.testResponseBodyEmpty)(pmOperation, oaOperation); } if (responseObject.content) { var processContent = function (contentType, content) { var _a, _b, _c, _d, _e; if (optContentType && optContentType.enabled && !(0, inOperations_1.inOperations)(pmOperation, optContentType === null || optContentType === void 0 ? void 0 : optContentType.excludeForOperations)) { pmOperation = (0, _1.testResponseContentType)(contentType, pmOperation, oaOperation, (_a = _this.config) === null || _a === void 0 ? void 0 : _a.globals); } if (optJsonBody && optJsonBody.enabled && (contentType === 'application/json' || contentType.includes('json')) && !(0, inOperations_1.inOperations)(pmOperation, optJsonBody === null || optJsonBody === void 0 ? void 0 : optJsonBody.excludeForOperations)) { pmOperation = (0, _1.testResponseJsonBody)(pmOperation, (_b = _this.config) === null || _b === void 0 ? void 0 : _b.globals); } if (optSchemaValidation && optSchemaValidation.enabled && (content === null || content === void 0 ? void 0 : content.schema) && (contentType === 'application/json' || contentType.includes('json')) && !(0, inOperations_1.inOperations)(pmOperation, optSchemaValidation === null || optSchemaValidation === void 0 ? void 0 : optSchemaValidation.excludeForOperations)) { pmOperation = (0, _1.testResponseJsonSchema)(optSchemaValidation, content.schema, pmOperation, oaOperation, (_d = (_c = _this.options) === null || _c === void 0 ? void 0 : _c.extraUnknownFormats) !== null && _d !== void 0 ? _d : [], (_e = _this.config) === null || _e === void 0 ? void 0 : _e.globals); } }; if (openApiContentType) { var content = responseObject.content[openApiContentType]; if (content) { processContent(openApiContentType, content); } } else { var contentTypesCounter = 0; try { for (var _g = tslib_1.__values(Object.entries(responseObject.content)), _h = _g.next(); !_h.done; _h = _g.next()) { var _j = tslib_1.__read(_h.value, 2), contentType = _j[0], content = _j[1]; if (!contentType) continue; if (contentTypesCounter > 0) continue; processContent(contentType, content); contentTypesCounter++; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_h && !_h.done && (_a = _g.return)) _a.call(_g); } finally { if (e_1) throw e_1.error; } } } } if (responseObject.headers) { for (var headerKey in responseObject.headers) { if (!headerKey) continue; var header = responseObject.headers[headerKey]; var headerRequired = header.required || false; var headerName = headerKey; if (optHeadersPresent && optHeadersPresent.enabled && headerRequired && !(0, inOperations_1.inOperations)(pmOperation, optHeadersPresent === null || optHeadersPresent === void 0 ? void 0 : optHeadersPresent.excludeForOperations)) { pmOperation = (0, _1.testResponseHeader)(headerName, pmOperation, oaOperation, (_f = _this.config) === null || _f === void 0 ? void 0 : _f.globals); } } } return pmOperation; }; this.injectContentTests = function (pmOperations, contentTests) { var tests = contentTests || _this.contentTests; if (!tests) return _this.postmanParser.mappedOperations; tests.map(function (contentTest) { var operations = pmOperations || _this.getOperationsFromSetting(contentTest); operations.map(function (pmOperation) { var _a, _b; if (contentTest === null || contentTest === void 0 ? void 0 : contentTest.responseBodyTests) { (0, _1.testResponseBodyContent)(contentTest.responseBodyTests, pmOperation, (_a = _this.config) === null || _a === void 0 ? void 0 : _a.globals); _this.registerOperationTestType(pmOperation, types_1.PortmanTestTypes.contract, false); } if (contentTest === null || contentTest === void 0 ? void 0 : contentTest.responseHeaderTests) { (0, _1.testResponseHeaderContent)(contentTest.responseHeaderTests, pmOperation, (_b = _this.config) === null || _b === void 0 ? void 0 : _b.globals); _this.registerOperationTestType(pmOperation, types_1.PortmanTestTypes.contract, false); } }); }); return _this.postmanParser.mappedOperations; }; this.injectAssignVariables = function (pmOperations, assignVariables) { var settings = assignVariables || _this.config.assignVariables; if (!settings) return _this.postmanParser.mappedOperations; settings.map(function (assignVarSetting) { if (!(assignVarSetting === null || assignVarSetting === void 0 ? void 0 : assignVarSetting.collectionVariables)) return; var operations = pmOperations || _this.getOperationsFromSetting(assignVarSetting); var fixedValueCounter = 0; operations.map(function (pmOperation) { var _a; var oaOperation = _this.oasParser.getOperationByPath(pmOperation.pathRef); fixedValueCounter = (0, _1.assignCollectionVariables)(pmOperation, oaOperation, assignVarSetting, fixedValueCounter, _this.options, (_a = _this.config) === null || _a === void 0 ? void 0 : _a.globals); }); }); return _this.postmanParser.mappedOperations; }; this.injectExtendedTests = function (pmOperations, extendedTestsSettings) { var settings = extendedTestsSettings || _this.extendTests; if (!settings) return _this.postmanParser.mappedOperations; settings.map(function (extendedTestsSetting) { var operations = pmOperations || _this.getOperationsFromSetting(extendedTestsSetting); operations.map(function (pmOperation) { if (extendedTestsSetting === null || extendedTestsSetting === void 0 ? void 0 : extendedTestsSetting.tests) { (0, _1.extendTest)(extendedTestsSetting, pmOperation); _this.registerOperationTestType(pmOperation, types_1.PortmanTestTypes.contract, false); } }); }); return _this.postmanParser.mappedOperations; }; this.injectOverwrites = function (pmOperations, overwriteSettings) { var settings = overwriteSettings || _this.config.overwrites; if (!settings) return _this.postmanParser.mappedOperations; settings.map(function (overwriteSetting) { var _a; var operations = pmOperations || _this.getOperationsFromSetting(overwriteSetting); (0, _1.applyOverwrites)(operations, overwriteSetting, _this.oasParser, (_a = _this.config) === null || _a === void 0 ? void 0 : _a.globals); }); return _this.postmanParser.mappedOperations; }; this.injectPreRequestScripts = function (pmOperations, preRequestSettings) { var settings = preRequestSettings || _this.config.operationPreRequestScripts; if (!settings) return _this.postmanParser.mappedOperations; settings.map(function (preRequestSetting) { var operations = pmOperations || _this.getOperationsFromSetting(preRequestSetting); (preRequestSetting === null || preRequestSetting === void 0 ? void 0 : preRequestSetting.scripts) && (0, _1.writeOperationPreRequestScripts)(operations, preRequestSetting.scripts); }); return _this.postmanParser.mappedOperations; }; this.registerOperationTestType = function (pmOperation, operationTestType, update) { if (update === void 0) { update = true; } var opTestType = { postmanItemId: pmOperation.item.id, postmanName: pmOperation.item.name, reqTestType: operationTestType }; var idx = _this.requestTestTypes.findIndex(function (x) { return x.postmanItemId == opTestType.postmanItemId; }); if (idx === -1) { _this.requestTestTypes.push(opTestType); } else if (update) { _this.requestTestTypes[idx] = opTestType; } }; var oasParser = testSuiteOptions.oasParser, postmanParser = testSuiteOptions.postmanParser, config = testSuiteOptions.config, options = testSuiteOptions.options; this.pmResponseJsonVarInjected = false; this.requestTestTypes = []; this.oasParser = oasParser; this.postmanParser = postmanParser; this.config = config; this.options = options; this.collection = postmanParser.collection; this.setupTests(); } TestSuite.prototype.getOperationsFromSetting = function (settings) { var _a; var openApiOperation = settings.openApiOperation, openApiOperationId = settings.openApiOperationId, openApiOperationIds = settings.openApiOperationIds; var pmOperations = []; if (openApiOperation) { pmOperations = this.postmanParser.getOperationsByPath(openApiOperation); if (pmOperations.length === 0) { this.track.openApiOperations.push(openApiOperation); } } else if (openApiOperationId) { pmOperations = this.postmanParser.getOperationsByIds([openApiOperationId]); if (pmOperations.length === 0) { this.track.openApiOperationIds.push(openApiOperationId); } } else if (openApiOperationIds) { pmOperations = this.postmanParser.getOperationsByIds(openApiOperationIds); if (pmOperations.length === 0) { (_a = this.track.openApiOperationIds).push.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(openApiOperationIds), false)); } } if (settings === null || settings === void 0 ? void 0 : settings.excludeForOperations) { var excludedOperations_1 = settings.excludeForOperations; pmOperations = pmOperations.filter(function (pmOperation) { return !excludedOperations_1.includes(pmOperation === null || pmOperation === void 0 ? void 0 : pmOperation.pathRef); }); pmOperations = pmOperations.filter(function (pmOperation) { if (pmOperation === null || pmOperation === void 0 ? void 0 : pmOperation.id) { return !excludedOperations_1.includes(pmOperation === null || pmOperation === void 0 ? void 0 : pmOperation.id); } return true; }); } return pmOperations; }; return TestSuite; }()); exports.TestSuite = TestSuite; //# sourceMappingURL=TestSuite.js.map