skip to Main Content

In ‘before’ hook I have a bunch of API requests setting up my test user for tests, one of method is responsible for setting up a password and it can have to possible out comes:

  1. 204 if the user do not have any password yet, so it creating a new one
  2. 400 if the user already have the password

I would like test not to fail in both cases, is it possible to add 2 possible outcomes to response?

I have tried something like this but it doesn’t work, second value in eq() is completely ignored

cy.request({
method: 'PUT',
url: "url/endpoint"
headers: {
  Authorization: "Bearer " + Cypress.env("access_token_val"),
  Connection: "keep-alive",
  Host: "host",
},
body: {
  "type": "password",
  "value": password,
  "temporary": false
}
}).then((response) => {
  expect(response.status).to.eq(204||400) })

EDIT: Actually it fails even if I only have

expect(response.status).to.eq(400) 

I have also tried to use this, same results

expect(response.body).to.have.property("error") 

Is there any way to make the test pass with the 400 response? That’s the whole response, the scenario is to use the same password on each environment rebuild.

Status: 400 – Bad Request

Headers: {
"x-xss-protection": "1; mode=block",
"strict-transport-security": "max-age=31536000; includeSubDomains",
"x-content-type-options": "nosniff",
"content-type": "application/json",
"referrer-policy": "no-referrer",
"content-length": "128",
"date": "Fri, 26 Apr 2024 18:12:43 GMT"
}
Body: {
"error": "invalidPasswordHistoryMessage",
"error_description": "Invalid password: must not be equal to any of last 24 passwords."
}

2

Answers


  1. Your code doesn’t work because 204||400 returns 204.

    console.log(204||400);

    expect(response.status).to.eq(204||400) is the same as expect(response.status).to.eq(204).

    You can manually check the expected result

    expect(response.status === 204 || response.status === 400).to.be.true
    
    Login or Signup to reply.
  2. you can create a temporary flag that determines if the expect should pass and have a simple conditional check in your callback where you can check if either of your conditions are met and then flip the flag to pass or fail your test.

    .then((response) => {
       let shouldPass = false;
       if(response.status === 204 || response.status === 400) {
          shouldPass = true
       }
       expect(shouldPass).to.be.true;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search