Запускается два запроса на пост, чтобы я не делал.. Как исправить?
Помогите пожалуйста разобраться. Есть класс с шагами:
import io.qameta.allure.Step;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
public class Steps {
UserPODJ staticUser = new UserPODJ(Constants.email_static, Constants.password_static, Constants.name_static);
UserPODJ user = new UserPODJ(Constants.email, Constants.password, Constants.name);
UserPODJ userUpdated = new UserPODJ(Constants.email, Constants.password, Constants.name);
UserPODJ userUpdatedOne = new UserPODJ(Constants.email, Constants.password, Constants.name);
UserPODJ userUpdatedTwo = new UserPODJ(Constants.email, Constants.password, Constants.name);
UserPODJ userUpdatedTree = new UserPODJ(Constants.email, Constants.password, Constants.name);
UserPODJ emptyFieldName = new UserPODJ(Constants.email, Constants.password, null);
UserPODJ emptyFieldPassword = new UserPODJ(Constants.email, null, Constants.name);
UserPODJ emptyFieldEmail = new UserPODJ(null, Constants.password, Constants.name);
@Step("Создание пользователя")
public Response creationOfUser(UserPODJ user) {
return
given()
.header("Content-type", "application/json")
.and()
.body(user)
.when()
.post("/api/auth/register");
}
@Step("Авторизация пользователя")
public Response authOfUser(UserPODJ user) {
return
given()
.header("Content-type", "application/json")
.and()
.body(user)
.when()
.post("/api/auth/login");
}
@Step("Создание юзера и получение accessToken")
public String getAccessToken(UserPODJ user) {
Response response = given()
.header("Content-type", "application/json")
.and()
.body(user)
.when()
.post("/api/auth/register").getBody().path("accessToken");
return response.toString();
// .replaceAll("Bearer ", "")
}
public ValidatableResponse getToken(UserPODJ user) {
return RestAssured.given()
.header("Content-type", "application/json")
.and()
.body(user)
.when()
.post("/api/auth/register").then();
// .replaceAll("Bearer ", "")
}
@Step("Создание юзера и получение accessToken")
public String getBAccessToken(UserPODJ user) {
Tokens response = given()
.header("Content-type", "application/json")
.and()
.body(user)
.when()
.post("/api/auth/register").as(Tokens.class);
return response.getAccessToken();
}
@Step("Логин статичного юзера и получение токена")
public String successLogin() {
return given()
.header("Content-type", "application/json")
.and()
.body(staticUser)
.when()
.post("/api/auth/login").as(Tokens.class).getAccessToken().replaceAll("Bearer ", "");
}
@Step("Получение списка ингридиентов")
public Response getListOfIngredients() {
return given()
.header("Content-type", "application/json")
.and()
.get("/api/ingredients");
}
}
и есть класс с тестами:
import io.qameta.allure.Step;
import io.qameta.allure.junit4.DisplayName;
import io.restassured.RestAssured;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static io.restassured.RestAssured.given;
public class CreationOfOrderTest {
Steps step = new Steps();
UserPODJ userUpdatedTree = new UserPODJ(Constants.email, Constants.password, Constants.name);
@Before
public void setUp() {
RestAssured.baseURI = Constants.URL;
RestAssured.filters(new RequestLoggingFilter(), new ResponseLoggingFilter());
}
@Test
@DisplayName("Проверка добавления ордера на юзера с авторизацией")
public void successCreationOfOrderTestWithAuth() {
List<String> listOfOrders = step.getListOfIngredients().getBody().path("data._id");
IngredientsPODJ jsonOne = new IngredientsPODJ(listOfOrders.subList(1, 3));
given()
.header("Authorization",
step.getToken(step.userUpdatedOne).extract().path("accessToken").toString())
.header("Content-type", "application/json")
.body(jsonOne)
.when()
.post("/api/orders")
.then().statusCode(200)
.body("success", Matchers.is(true));
}
@Test
@DisplayName("Проверка добавления заказа на юзера без авторизации")
public void failedCreationOfOrderTestWithoutAuth() {
List<String> listOfOrders = step.getListOfIngredients().getBody().path("data._id");
IngredientsPODJ jsonOne = new IngredientsPODJ(listOfOrders.subList(2, 4));
given()
.header("Content-type", "application/json")
.body(jsonOne)
.when()
.post("/api/orders")
.then().statusCode(200)
.body("success", Matchers.is(true));
}
@Test
@DisplayName("Проверка добавления заказа с пустыми ингредиентами на пользователя")
public void creationOfOrderTestWithAuthAndWithEmptyIngredients() {
List<String> listOfOrders = step.getListOfIngredients().getBody().path("data._id");
IngredientsPODJ jsonOne = new IngredientsPODJ(listOfOrders.subList(2, 4));
jsonOne = new IngredientsPODJ();
given()
.header("Authorization",
step.getToken(step.userUpdatedTwo).extract().path("accessToken").toString())
.header("Content-type", "application/json")
.body(jsonOne)
.when()
.post("/api/orders")
.then().statusCode(400)
.body("success", Matchers.is(false));
}
@Test
@DisplayName("Проверка добавления заказа с неправильными ингредиентами на пользователя")
public void creationOfOrderTestWithAuthAndWithWrongIngredients() {
// IngredientsPODJ jsonOne = new IngredientsPODJ(Constants.list);
// File json = new File("/src/test/resources/orderWithWrongIngredients.json");
Response response = given()
// .header("Content-type", "application/json")
.header("Authorization",
given()
.header("Content-type", "application/json")
.and()
.body("{\n" +
"\"email\": \"[email protected]\",\n" +
"\"password\": \"45454545\",\n" +
"\"name\": \"Vdfdf\"\n" +
"}")
.when()
.post("/api/auth/register").then().extract().path("accessToken").toString())
.body("{\n" +
" \"ingredients\": [\"61c0c5a71d1f82001bdaaa6v\",\"61c0c5a71d1f82001bdaaa6v\"]\n" +
"}")
.when()
.post("/api/orders");
response
.then().statusCode(500);
}
}
Как бы я не пытался, при балковом запуске методы creationOfOrderTestWithAuthAndWithWrongIngredients и creationOfOrderTestWithAuthAndWithEmptyIngredients выполняют два метода пост:
Request method: POST
Request URI: https://stellarburgers.nomoreparties.site/api/auth/register
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Content-Type=application/json
Cookies: <none>
Multiparts: <none>
Body:
{
"email": "[email protected]",
"password": "45454545",
"name": "Vdfdf"
}
Request method: POST
Request URI: https://stellarburgers.nomoreparties.site/api/auth/register
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Content-Type=application/json
Cookies: <none>
Multiparts: <none>
Body:
{
"email": "[email protected]",
"password": "45454545",
"name": "Vdfdf"
}
HTTP/1.1 403 Forbidden
Date: Wed, 01 May 2024 15:14:56 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: *
ETag: W/"31-WKEMg8zALB1Ko0WjUHi/6RBGHVc"
CF-Cache-Status: DYNAMIC
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v4?s=ZmRXBu3XmTUjMM5YWC8R9vGNOA8wMncXh5eBRKozPtX%2BxhA7dHFPGdfuZxlzFFYxZ2Ga7MjIk%2FLhjn%2FkOnVe9sShXDLa57725iubHrdDNSD187A0BC%2FAi0CaVsPwYRFispd2cc1HLoRlZyVvpLhXlAAlfV0%3D"}],"group":"cf-nel","max_age":604800}
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 87d0b95f3b9135d8-WAW
Content-Encoding: gzip
alt-svc: h3=":443"; ma=86400
{
"success": false,
"message": "User already exists"
}
Хотя в коде этого явно нет.. как убрать эту дубликацию?
Причём если запускать их по очереди они все зелёные...