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:
- 204 if the user do not have any password yet, so it creating a new one
- 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
Your code doesn’t work because
204||400
returns204
.expect(response.status).to.eq(204||400)
is the same asexpect(response.status).to.eq(204)
.You can manually check the expected result
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.