From 809e045c0b44dc49390a6a55452f89d1c3f1f663 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sat, 24 Aug 2024 09:12:54 +0200 Subject: [PATCH] I made a test but I don't know how to test --- pom.xml | 11 ++++++ src/test/java/eu/m724/TokenTest.java | 50 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/test/java/eu/m724/TokenTest.java diff --git a/pom.xml b/pom.xml index ddc3b84..0efdcfa 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,17 @@ io.quarkus quarkus-rest-jackson + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + diff --git a/src/test/java/eu/m724/TokenTest.java b/src/test/java/eu/m724/TokenTest.java new file mode 100644 index 0000000..daf11d3 --- /dev/null +++ b/src/test/java/eu/m724/TokenTest.java @@ -0,0 +1,50 @@ +package eu.m724; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.json.JsonPath; +import jakarta.json.Json; +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.given; + +@QuarkusTest +public class TokenTest { + @Test + void anonymousAccessTest() { + get("/api/tokens/me").then().statusCode(HttpStatus.SC_UNAUTHORIZED); + get("/api/tokens/create").then().statusCode(HttpStatus.SC_UNAUTHORIZED); + } + + @Test + void createTokenTest() { + String tokenEncoded = given() + .header("X-Token", "AAAAAAAAAAAAAAAAAAAAAAAA") + .body(Json.createObjectBuilder().add("accessLimits", "kilo").build().toString()) + .when() + .get("/api/tokens/create") + .then() + .statusCode(HttpStatus.SC_OK) + .extract().jsonPath().getString("token"); + + System.out.println("New token: " + tokenEncoded); + + given() + .header("X-Token", tokenEncoded) + .when() + .get("/api/tokens/create") + .then() + .statusCode(HttpStatus.SC_FORBIDDEN); + + JsonPath response = given() + .header("X-Token", tokenEncoded) + .when() + .get("/api/tokens/me") + .then() + .statusCode(HttpStatus.SC_OK) + .extract().jsonPath(); + + System.out.println(response.toString()); + } +}