0

I am trying to run a few automated testing using the Postman tool. For regular scenarios, I understand how to write pre-test and test scripts. What I do not know (and trying to understand) is, how to write scripts for checking 409 error (let us call it duplicate resource check).

I want to run a create resource api like below, then run it again and ensure that the 2nd invocation really returns 409 error.

POST  /myservice/books

Is there a way to run the same api twice and check the return value for 2nd invocation. If yes, how do I do that. One crude way of achieving this could be to create a dependency between two tests, where the first one creates a resource, and the second one uses the same payload once again to create the same resource. I am looking for a single test to do an end-to-end testing.

Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78

2 Answers2

0

Postman doesn't really provide a standard way, but is still flexible. I realized that we have to write javascript code in the pre-request tab, to do our own http request (using sendRequest method) and store the resulting data into env vars for use by the main api call.

Here is a sample:

var phone = pm.variables.replaceIn("{{$randomPhoneNumber}}");
console.log("phone:", phone)
var baseURL = pm.variables.replaceIn("{{ROG_SERVER}}:{{ROG_PORT}}{{ROG_BASE_URL}}")
var usersURL = pm.variables.replaceIn("{{ROG_SERVICE}}/users")
var otpURL = `${baseURL}/${phone}/_otp_x`

// Payload for partner creation
const payload = {
    "name": pm.variables.replaceIn("{{username}}"),
    "phone":phone,
    "password": pm.variables.replaceIn("{{$randomPassword}}"),
}
console.log("user payload:", payload)

function getOTP (a, callback) {
    // Get an OTP
    pm.sendRequest(otpURL, function(err, response) {
        if (err) throw err
        var jsonDaata = response.json()
        pm.expect(jsonDaata).to.haveOwnProperty('otp')
        pm.environment.set("otp", jsonDaata.otp)
        pm.environment.set("phone", phone);
        pm.environment.set("username", "{{$randomUserName}}")
        if (callback) callback(jsonDaata.otp)
    }) 
}


// Get an OTP
getOTP("a", otp => {
    console.log("OTP received:", otp)
    payload.partnerRef = pm.variables.replaceIn("{{$randomPassword}}")
    payload.otp = otp

    //create a partner user with the otp.
    let reqOpts = {
        url: usersURL,
        method: 'POST',
        headers: { 'Content-Type': 'application/json'},
        body: JSON.stringify(payload)
    }
    pm.sendRequest(reqOpts, (err, response) => {
        console.log("response?", response)
        pm.expect(response).to.have.property('code', 201)
    })

   // Get a new OTP for the main request to be executed.
   getOTP() 
})


Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78
0

I did it in my test block. Create your normal request as you would send it, then in your tests, validate the original works, and then you can send the second command and validate the response.

You can also use the pre and post scripting to do something similar, or have one test after the other in the file (they run sequentially) to do the same testing.

For instance, I sent an API call here to create records. As I need the Key_ to delete them, I can make a call to GET /foo at my API

pm.test("Response should be 200", function () { 
    pm.response.to.be.ok;
    pm.response.to.have.status(200);
});

pm.test("Parse Key_ values and send DELETE from original request response", function () {
    var jsonData = JSON.parse(responseBody);
    jsonData.forEach(function (TimeEntryRecord) {
        console.log(TimeEntryRecord.Key_);
        const DeleteURL = pm.variables.get('APIHost') + '/bar/' + TimeEntryRecord.Key_;

        pm.sendRequest({
            url: DeleteURL, 
            method: 'DELETE',
            header: { 'Content-Type': 'application/json' },
            body: { TimeEntryRecord }
        }, function (err, res) {
            console.log("Sent Delete: " + DeleteURL );
        });
    });
});
Steven Scott
  • 10,234
  • 9
  • 69
  • 117