Mate25
Mate25

Reputation: 65

How can I test authentication with Postman if status code is 200 even if I give wrong inputs?

I'm learning Postman, and I tried authentication with valid and invalid data. (I'm using basic auth). If I give valid data, the login redirects me to the index page as is should, giving 200 OK status code. However if I pass wrong parameters, the status code remains 200 OK aswell, only the page tells me, that me username and password is incorrect. I tried using the following method I found somewhere:

pm.test("Request is successful with a status code of 200", function () {
  pm.response.to.have.status(200);
});

I think postman gives me 200 OK since the post-request is done, and the failed-login option is handled properly. (I have to mention that this API I'm testing is pre-defined, I didn't make it.) So basically how can I test this?

Upvotes: 0

Views: 962

Answers (2)

Robert Green MBA
Robert Green MBA

Reputation: 1876

You can just test for a different status code (i.e., 400 BadRequest)

tests["Status code is 400"] = responseCode.code === 400;

Upvotes: 0

Christian Baumann
Christian Baumann

Reputation: 3444

You could check the response body to not contain a certain string:

pm.test("Body does not contain invalid login message",() => {
    pm.expect(pm.response.text()).to.not.include("invalid login");
});

Replace invalid login by te string you want to check for.

Upvotes: 1

Related Questions