Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: oneOf pm variables #520

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,29 @@ pm.test(\\"[GET]::/crm/companies/:id - Content check if value for 'data[0].compa
]
`;

exports[`testResponseBodyContent should add content test for string for property check & oneOf pm variable values 1`] = `
Array [
"// Set response object as internal variable
let jsonData = {};
try {jsonData = pm.response.json();}catch(e){}
",
"// Set property value as variable
const _resData0CompanyName = jsonData?.data?.[0]?.company_name;
",
"// Response body should have \\"data[0].company_name\\"
pm.test(\\"[GET]::/crm/companies/:id - Content check if 'data[0].company_name' exists\\", function() {
pm.expect(_resData0CompanyName !== undefined).to.be.true;
});
",
"// 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\\")\\"]);
})};
",
]
`;

exports[`testResponseBodyContent should add content test for string for property check & oneOf string values 1`] = `
Array [
"// Set response object as internal variable
Expand Down
12 changes: 12 additions & 0 deletions src/application/tests/testResponseBodyContent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,18 @@ describe('testResponseBodyContent', () => {
expect(pmTest.script.exec).toMatchSnapshot()
})

it('should add content test for string for property check & oneOf pm variable values', async () => {
const contentTests = [
{
key: 'data[0].company_name',
oneOf: ['{{postman_env_variable}}', '{{postman_env_variable_two}}']
}
]
pmOperation = testResponseBodyContent(contentTests, pmOperation)
const pmTest = pmOperation.getTests()
expect(pmTest.script.exec).toMatchSnapshot()
})

it('should add content test for string for property check & oneOf boolean values', async () => {
const contentTests = [
{
Expand Down
9 changes: 8 additions & 1 deletion src/application/tests/testResponseBodyContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,15 @@ export const testResponseBodyContent = (
// Make items safe to inject into test
const safeOneOf = check.oneOf.map(item => {
if (typeof item === 'string') {
let checkOneOfItem = item
if (checkOneOfItem.includes('{{') && checkOneOfItem.includes('}}')) {
checkOneOfItem = `pm.collectionVariables.get("${checkOneOfItem.replace(
/{{|}}/g,
''
)}")`
}
// Quote string value
return `"${item}"`
return `"${checkOneOfItem}"`
}
return item
})
Expand Down
Loading