3

Suppose a API request fetches a users id, email address and their designated role. Sample API Request below:

GET: /v1/users HTTP/1.1
Content-Type: application/json
Authorization: bearer {access_token}

For the above request, the following is the response:

{
    "content": [
        {
            "id": 1,
            "email": "random@random.com",
            "full_name": "AlbusDumbledore",
            "role": "OWNER"
        },
        {
            "id": 40,
            "email": "random1@random1.com",
            "role": "OWNER"
        }
],
    "last": false,
    "total_elements": 2,
    "total_pages": 1,
    "sort": null,
    "first": true,
    "number_of_elements": 2,
    "size": 20,
    "number": 0
}

Now, what will be the test in postman to make sure that all the returned values under role node is equal to OWNER?

0

1 Answer 1

5

You could add something like a loop to check for it?

pm.test("Check role equals OWNER", () => {
    var jsonData = pm.response.json()
    for (i = 0; i < jsonData.content.length; i++) { 
        pm.expect(jsonData.content[i].role).to.equal('OWNER')
    }
})

This should work to check the value of each role property, if the schema response you posted is correct.

I changed one of the values and ran the test again to show you it working - This will show the failure in Postman if the property is not equal to 'OWNER'.

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.