Skip to content

Commit

Permalink
testResponseBodyContent - prevent PM variables as string (#651)
Browse files Browse the repository at this point in the history
* testResponseBodyContent - prevent PM variables as string

* testResponseBodyContent - use the internal var instead of the full json path

* Updated openapi-format to version 1.23.0

* testResponseHeaderContent - support using PM variables in oneOf check
  • Loading branch information
thim81 authored Sep 11, 2024
1 parent f11d176 commit 0f6b365
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

- testResponseBodyContent - prevent PM variables as string (#650)
- testResponseHeaderContent - support using PM variables in oneOf check
- overwrite - handle overwrite of non-empty objects with plain values (#646)
- Bumped dependencies: openapi-format 1.23.0

## v1.30.1 - (2024-08-27)

- Improved OpenAPI to JSON schema conversion to handle OpenAPI-specific properties (#642 #494)
Expand Down
34 changes: 21 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
"fp-ts": "^2.16.2",
"fs-extra": "^10.1.0",
"lodash": "^4.17.21",
"neotraverse": "^0.6.15",
"neotraverse": "^0.6.18",
"newman": "^6.2.1",
"node-emoji": "^1.11.0",
"openapi-format": "^1.22.3",
"openapi-format": "^1.23.0",
"openapi-to-postmanv2": "4.24.0",
"openapi-types": "9.1.0",
"ora": "^5.4.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if 'status_code' exists\\",
"// Response body should be one of the values \\"200,201\\" for \\"status_code\\"
if (_resStatusCode !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'status_code' is matching one of: '200,201'\\", function() {
pm.expect(jsonData.status_code).to.be.oneOf([200,201]);
pm.expect(_resStatusCode).to.be.oneOf([200,201]);
})};
",
]
Expand Down Expand Up @@ -385,7 +385,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if 'data[0].monetary_amount
"// Response body should contain value \\"{{postman_env_variable}}\\" for \\"data[0].monetary_amount\\"
if (_resData0MonetaryAmount !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'data[0].monetary_amount' contains '{{postman_env_variable}}'\\", function() {
pm.expect(jsonData.data[0].monetary_amount).to.include(pm.collectionVariables.get(\\"postman_env_variable\\"));
pm.expect(_resData0MonetaryAmount).to.include(pm.collectionVariables.get(\\"postman_env_variable\\"));
})};
",
]
Expand All @@ -408,7 +408,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if 'data[0].name' exists\\"
"// Response body should contain value \\"Musk\\" for \\"data[0].name\\"
if (_resData0Name !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'data[0].name' contains 'Musk'\\", function() {
pm.expect(jsonData.data[0].name).to.include(\\"Musk\\");
pm.expect(_resData0Name).to.include(\\"Musk\\");
})};
",
]
Expand Down Expand Up @@ -672,7 +672,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if '[0].data.monetary_amoun
"// Response body should contain value \\"{{postman_env_variable}}\\" for \\"[0].data.monetary_amount\\"
if (_res0DataMonetaryAmount !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for '[0].data.monetary_amount' contains '{{postman_env_variable}}'\\", function() {
pm.expect(jsonData[0].data.monetary_amount).to.include(pm.collectionVariables.get(\\"postman_env_variable\\"));
pm.expect(_res0DataMonetaryAmount).to.include(pm.collectionVariables.get(\\"postman_env_variable\\"));
})};
",
]
Expand All @@ -695,7 +695,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if '[0].data.name' exists\\
"// Response body should contain value \\"Musk\\" for \\"[0].data.name\\"
if (_res0DataName !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for '[0].data.name' contains 'Musk'\\", function() {
pm.expect(jsonData[0].data.name).to.include(\\"Musk\\");
pm.expect(_res0DataName).to.include(\\"Musk\\");
})};
",
]
Expand Down Expand Up @@ -787,7 +787,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if '[0].data.name' exists\\
"// Response body should be one of the values \\"Spacex,Tesla\\" for \\"[0].data.name\\"
if (_res0DataName !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for '[0].data.name' is matching one of: 'Spacex,Tesla'\\", function() {
pm.expect(jsonData[0].data.name).to.be.oneOf([\\"Spacex\\",\\"Tesla\\"]);
pm.expect(_res0DataName).to.be.oneOf([\\"Spacex\\",\\"Tesla\\"]);
})};
",
]
Expand Down Expand Up @@ -850,7 +850,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if 'data[0].company_name' e
"// Response body should be one of the values \\"true,false\\" for \\"data[0].company_name\\"
if (_resData0CompanyName !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'data[0].company_name' is matching one of: 'true,false'\\", function() {
pm.expect(jsonData.data[0].company_name).to.be.oneOf([true,false]);
pm.expect(_resData0CompanyName).to.be.oneOf([true,false]);
})};
",
]
Expand All @@ -873,7 +873,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if 'data[0].company_name' e
"// Response body should be one of the values \\"{{postman_env_variable}},{{postman_env_variable_two}}\\" for \\"data[0].company_name\\"
if (_resData0CompanyName !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'data[0].company_name' is matching one of: '{{postman_env_variable}},{{postman_env_variable_two}}'\\", function() {
pm.expect(jsonData.data[0].company_name).to.be.oneOf([\\"pm.collectionVariables.get(\\"postman_env_variable\\")\\",\\"pm.collectionVariables.get(\\"postman_env_variable_two\\")\\"]);
pm.expect(_resData0CompanyName).to.be.oneOf([pm.collectionVariables.get(\\"postman_env_variable\\"),pm.collectionVariables.get(\\"postman_env_variable_two\\")]);
})};
",
]
Expand All @@ -896,7 +896,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if 'data[0].company_name' e
"// Response body should be one of the values \\"Spacex,Tesla\\" for \\"data[0].company_name\\"
if (_resData0CompanyName !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'data[0].company_name' is matching one of: 'Spacex,Tesla'\\", function() {
pm.expect(jsonData.data[0].company_name).to.be.oneOf([\\"Spacex\\",\\"Tesla\\"]);
pm.expect(_resData0CompanyName).to.be.oneOf([\\"Spacex\\",\\"Tesla\\"]);
})};
",
]
Expand Down Expand Up @@ -930,7 +930,7 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if 'data[0].company_name' e
"// Response body should contain value \\"Spacex\\" for \\"data[0].company_name\\"
if (_resData0CompanyName !== undefined) {
pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'data[0].company_name' contains 'Spacex'\\", function() {
pm.expect(jsonData.data[0].company_name).to.include(\\"Spacex\\");
pm.expect(_resData0CompanyName).to.include(\\"Spacex\\");
})};
",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if header value for 'Operat
]
`;

exports[`testResponseHeaderContent should add content header test for property check & oneOf pm variable values 1`] = `
Array [
"// Validate if response header is present
pm.test(\\"[GET]::/crm/companies/:id - Response header Operation-Location is present\\", function () {
pm.response.to.have.header(\\"Operation-Location\\");
});
",
"// Response header should be one of the values \\"{{postman_env_variable}},{{postman_env_variable_two}}\\" for \\"Operation-Location\\"
pm.test(\\"[GET]::/crm/companies/:id - Content check if header value for 'Operation-Location' is matching one of: '{{postman_env_variable}},{{postman_env_variable_two}}'\\", function() {
pm.expect(pm.response.headers.get(\\"Operation-Location\\")).to.be.oneOf([pm.collectionVariables.get(\\"postman_env_variable\\"),pm.collectionVariables.get(\\"postman_env_variable_two\\")]);
});
",
]
`;

exports[`testResponseHeaderContent should add content header test for property check & pm variable value 1`] = `
Array [
"// Validate if response header is present
Expand Down
5 changes: 3 additions & 2 deletions src/application/tests/testResponseBodyContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const testResponseBodyContent = (
`if (${pathVarName} !== undefined) {\n`,
`pm.test("[${pmOperation.method.toUpperCase()}]${split}${pmOperation.path}`,
` - Content check if value for '${keyLabel}' contains '${check.contains}'", function() {\n`,
` pm.expect(${sourceData}${keyValue}).to.include(${checkContains});\n`,
` pm.expect(${pathVarName}).to.include(${checkContains});\n`,
`})};\n`
].join('')
}
Expand All @@ -160,6 +160,7 @@ export const testResponseBodyContent = (
/{{|}}/g,
''
)}")`
return checkOneOfItem
}
// Quote string value
return `"${checkOneOfItem}"`
Expand All @@ -172,7 +173,7 @@ export const testResponseBodyContent = (
`if (${pathVarName} !== undefined) {\n`,
`pm.test("[${pmOperation.method.toUpperCase()}]${split}${pmOperation.path}`,
` - Content check if value for '${keyLabel}' is matching one of: '${check.oneOf}'", function() {\n`,
` pm.expect(${sourceData}${keyValue}).to.be.oneOf([${safeOneOf}]);\n`,
` pm.expect(${pathVarName}).to.be.oneOf([${safeOneOf}]);\n`,
`})};\n`
].join('')
}
Expand Down
12 changes: 12 additions & 0 deletions src/application/tests/testResponseHeaderContent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ describe('testResponseHeaderContent', () => {
expect(pmTest.script.exec).toMatchSnapshot()
})

it('should add content header test for property check & oneOf pm variable values', async () => {
const contentTests = [
{
key: 'Operation-Location',
oneOf: ['{{postman_env_variable}}', '{{postman_env_variable_two}}']
}
]
pmOperation = testResponseHeaderContent(contentTests, pmOperation)
const pmTest = pmOperation.getTests()
expect(pmTest.script.exec).toMatchSnapshot()
})

it('should not add content header test for a property & custom assert', async () => {
const contentTests = [
{
Expand Down
15 changes: 13 additions & 2 deletions src/application/tests/testResponseHeaderContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,19 @@ export const testResponseHeaderContent = (
if (Array.isArray(check.oneOf)) {
// Make items safe to inject into test
const safeOneOf = check.oneOf.map(item => {
// Quote string value
return `"${item}"`
if (typeof item === 'string') {
let checkOneOfItem = item
if (checkOneOfItem.includes('{{') && checkOneOfItem.includes('}}')) {
checkOneOfItem = `pm.collectionVariables.get("${checkOneOfItem.replace(
/{{|}}/g,
''
)}")`
return checkOneOfItem
}
// Quote string value
return `"${checkOneOfItem}"`
}
return item
})

pmTestOneOf = [
Expand Down

0 comments on commit 0f6b365

Please sign in to comment.