Test Results Showing Incorrect Status Codes

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:

  1. I have a collection of tests where I modify permissions using PUT requests and then make a PATCH request to update user information.
  2. 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.
  3. 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:

Hey @hooper7775 :wave:

Are you expecting the status code from the main request or the async requests?


This would fail as it’s using pm.response.to.have.status(500) which isn’t the status code of the main request.

This would pass as it’s using pm.expect(response.code).to.eql(500) which is the status code of the async request.

I want my test results match my logs, it happens as is it shows tests for the last request. Attached images above. There is a bug, but it’s okay, i just don’t know why there is a mismatch. And how i better use async. Btw, i don’t know why i use async :slight_smile:

Yes, i want async request results, as shown in logs)) Understood. But still don’t know how to use right

The async requests are checking the status code of the main request though.

What happens if you change those to pm.expect(res.code).to.eql(200) and pm.expect(res.code).to.eql(403)?

Thanks, you saved me, I’ve been racking my brain for 2 hours. Now it works


Do u know how i can simplify my code, or what i should know about async functions? And how do i skip my initial request in query? I would be really glad to hear, you always know how to help :slight_smile: :wink:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.