I am encountering an issue with the test results in Postman. I have a collection of tests that include sending PUT and PATCH requests to modify permissions for a user and then make another request. However, the test results are not matching the actual responses received in the console.
Here are the steps I’ve taken to resolve the issue:
- I have a collection of tests where I modify permissions using PUT requests and then make a PATCH request to update user information.
- I expect the responses from the PATCH requests to have a status code of 403, but the test results show a status code of 500 for these requests.
- The console, on the other hand, displays the correct status codes for the requests (403 for PATCH requests).
My test:
// Set the desired URL, method, request body, and expected permissions
const membersUrl = pm.environment.replaceIn("{{members_url}}");
const accountId = pm.environment.get("account_id");
const requestUrl = `${membersUrl}/${accountId}`;
const requestMethod = "PATCH";
const requestBody = { "key": "value" };
const expectedPermissions = [1, 3, 5, 50, 52];
// Call the reusable function
modifyRoles.modifyPermissionsAndSendRequest(requestUrl, requestMethod, requestBody, expectedPermissions, pm);
My collection:
//function itself
modifyRoles = {
modifyPermissionsAndSendRequest: async function (url, method, requestBody, expectedPermissions, pm) {
// Fetch the authorization token from the environment variables
const authToken = pm.environment.get("authorization_token");
// Gather environment variables and constants
const baseUrl = pm.environment.get("base_url");
const userRoleId = pm.environment.get("user_role_id");
const permissions_url = pm.environment.get("modify_permissions_url");
const modifyPermissionsUrl = baseUrl + "/" + permissions_url + userRoleId;
const scope = 1; // Organizations
// Initialize the permissions dictionary with an empty object
let dictionary = {};
// Function to set permissions based on an array of permission IDs
function setPermissions(permissionIds) {
dictionary = {};
permissionIds.forEach(id => {
dictionary[id] = scope;
});
}
// Function to check if permissions match expectedPermissions
function permissionsMatch() {
for (const permissionId of expectedPermissions) {
if (dictionary[permissionId] !== scope) {
return false;
}
}
return true;
}
// Function to send the PUT request
async function sendPutRequest() {
return new Promise((resolve, reject) => {
// Initiate the PUT request to modify permissions
pm.sendRequest({
url: modifyPermissionsUrl,
method: 'PUT',
header: {
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify(dictionary)
}
}, function (err, res) {
if (err) {
console.log(err);
reject(err);
} else {
console.log(res);
resolve();
}
});
});
}
// Function to send the PATCH request
async function sendPatchRequest() {
return new Promise((resolve, reject) => {
// After modifying permissions, make the specified request
pm.sendRequest({
url: url,
method: method,
header: {
'Content-Type': 'application/json',
'Authorization': `${authToken}`
},
body: {
mode: 'raw',
raw: JSON.stringify(requestBody)
}
}, function (err, res) {
if (err) {
console.log(err);
reject(err);
} else {
console.log(res);
// Add your tests here
if (permissionsMatch()) {
pm.test("Permissions are as expected", function () {
pm.response.to.be.success;
});
} else {
pm.test("Permissions do not match expected list", function () {
pm.response.to.have.status(403);
});
}
resolve(res);
}
});
});
}
// Perform multiple iterations to set permissions incrementally
for (let i = 0; i < expectedPermissions.length; i++) {
setPermissions(expectedPermissions.slice(0, i + 1));
await sendPutRequest(); // Wait for the PUT request to complete
await sendPatchRequest(); // Wait for the PATCH request to complete
}
}
};
Test results:
Logs: