only use rest-assured for tests

This commit is contained in:
Athou
2024-08-14 16:00:47 +02:00
parent 857736adad
commit 69cd90edd8
13 changed files with 248 additions and 245 deletions

View File

@@ -15,7 +15,6 @@
<quarkus.version>3.13.1</quarkus.version> <quarkus.version>3.13.1</quarkus.version>
<querydsl.version>6.6</querydsl.version> <querydsl.version>6.6</querydsl.version>
<rome.version>2.1.0</rome.version> <rome.version>2.1.0</rome.version>
<jersey.version>3.1.8</jersey.version>
<properties-plugin.version>1.2.1</properties-plugin.version> <properties-plugin.version>1.2.1</properties-plugin.version>
<quarkus.datasource.db-kind>h2</quarkus.datasource.db-kind> <quarkus.datasource.db-kind>h2</quarkus.datasource.db-kind>
@@ -441,24 +440,6 @@
<version>5.15.0</version> <version>5.15.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>io.rest-assured</groupId> <groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId> <artifactId>rest-assured</artifactId>

View File

@@ -11,8 +11,6 @@ import java.util.Objects;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.HttpStatus;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.mockserver.client.MockServerClient; import org.mockserver.client.MockServerClient;
@@ -20,19 +18,15 @@ import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse; import org.mockserver.model.HttpResponse;
import com.commafeed.JacksonCustomizer;
import com.commafeed.frontend.model.Entries; import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.Subscription; import com.commafeed.frontend.model.Subscription;
import com.commafeed.frontend.model.request.SubscribeRequest; import com.commafeed.frontend.model.request.SubscribeRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.http.Header; import io.restassured.http.Header;
import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.MediaType;
import lombok.Getter; import lombok.Getter;
@Getter @Getter
@@ -47,17 +41,10 @@ public abstract class BaseIT {
private String apiBaseUrl; private String apiBaseUrl;
private String webSocketUrl; private String webSocketUrl;
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) {
return base;
}
@BeforeEach @BeforeEach
void init() throws IOException { void init() throws IOException {
this.mockServerClient = ClientAndServer.startClientAndServer(0); this.mockServerClient = ClientAndServer.startClientAndServer(0);
ObjectMapper mapper = new ObjectMapper();
new JacksonCustomizer().customize(mapper);
this.client = configureClientBuilder(new JerseyClientBuilder().register(new JacksonJsonProvider(mapper))).build();
this.feedUrl = "http://localhost:" + mockServerClient.getPort() + "/"; this.feedUrl = "http://localhost:" + mockServerClient.getPort() + "/";
this.baseUrl = "http://localhost:8085/"; this.baseUrl = "http://localhost:8085/";
this.apiBaseUrl = this.baseUrl + "rest/"; this.apiBaseUrl = this.baseUrl + "rest/";
@@ -87,13 +74,11 @@ public abstract class BaseIT {
} }
protected List<HttpCookie> login() { protected List<HttpCookie> login() {
Form form = new Form();
form.param("j_username", "admin");
form.param("j_password", "admin");
List<Header> setCookieHeaders = RestAssured.given() List<Header> setCookieHeaders = RestAssured.given()
.auth()
.none()
.formParams("j_username", "admin", "j_password", "admin") .formParams("j_username", "admin", "j_password", "admin")
.post(baseUrl + "j_security_check") .post("j_security_check")
.then() .then()
.statusCode(HttpStatus.SC_OK) .statusCode(HttpStatus.SC_OK)
.extract() .extract()
@@ -106,7 +91,14 @@ public abstract class BaseIT {
SubscribeRequest subscribeRequest = new SubscribeRequest(); SubscribeRequest subscribeRequest = new SubscribeRequest();
subscribeRequest.setUrl(feedUrl); subscribeRequest.setUrl(feedUrl);
subscribeRequest.setTitle("my title for this feed"); subscribeRequest.setTitle("my title for this feed");
return client.target(apiBaseUrl + "feed/subscribe").request().post(Entity.json(subscribeRequest), Long.class); return RestAssured.given()
.body(subscribeRequest)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/feed/subscribe")
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.as(Long.class);
} }
protected Long subscribeAndWaitForEntries(String feedUrl) { protected Long subscribeAndWaitForEntries(String feedUrl) {
@@ -116,19 +108,24 @@ public abstract class BaseIT {
} }
protected Subscription getSubscription(Long subscriptionId) { protected Subscription getSubscription(Long subscriptionId) {
return client.target(apiBaseUrl + "feed/get/{id}").resolveTemplate("id", subscriptionId).request().get(Subscription.class); return RestAssured.given()
.get("rest/feed/get/{id}", subscriptionId)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.as(Subscription.class);
} }
protected Entries getFeedEntries(long subscriptionId) { protected Entries getFeedEntries(long subscriptionId) {
Response response = client.target(apiBaseUrl + "feed/entries") return RestAssured.given()
.queryParam("id", subscriptionId) .get("rest/feed/entries?id={id}&readType=all", subscriptionId)
.queryParam("readType", "all") .then()
.request() .statusCode(HttpStatus.SC_OK)
.get(); .extract()
return response.readEntity(Entries.class); .as(Entries.class);
} }
protected void forceRefreshAllFeeds() { protected void forceRefreshAllFeeds() {
client.target(apiBaseUrl + "feed/refreshAll").request().get(Void.class); RestAssured.given().get("rest/feed/refreshAll").then().statusCode(HttpStatus.SC_OK);
} }
} }

View File

@@ -1,7 +1,6 @@
package com.commafeed.integration; package com.commafeed.integration;
import java.net.HttpCookie; import java.net.HttpCookie;
import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -16,114 +15,117 @@ import com.commafeed.frontend.model.request.ProfileModificationRequest;
import com.commafeed.frontend.model.request.SubscribeRequest; import com.commafeed.frontend.model.request.SubscribeRequest;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.client.Entity; import io.restassured.RestAssured;
import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.MediaType;
@QuarkusTest @QuarkusTest
class SecurityIT extends BaseIT { class SecurityIT extends BaseIT {
@Test @Test
void notLoggedIn() { void notLoggedIn() {
try (Response response = getClient().target(getApiBaseUrl() + "user/profile").request().get()) { RestAssured.given().get("rest/user/profile").then().statusCode(HttpStatus.SC_UNAUTHORIZED);
Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatus());
}
} }
@Test @Test
void formLogin() { void formLogin() {
List<HttpCookie> cookies = login(); List<HttpCookie> cookies = login();
cookies.forEach(c -> Assertions.assertTrue(c.getMaxAge() > 0));
try (Response response = getClient().target(getApiBaseUrl() + "user/profile") RestAssured.given()
.request()
.header(HttpHeaders.COOKIE, cookies.stream().map(HttpCookie::toString).collect(Collectors.joining(";"))) .header(HttpHeaders.COOKIE, cookies.stream().map(HttpCookie::toString).collect(Collectors.joining(";")))
.get()) { .get("rest/user/profile")
Assertions.assertEquals(HttpStatus.SC_OK, response.getStatus()); .then()
cookies.forEach(c -> Assertions.assertTrue(c.getMaxAge() > 0)); .statusCode(HttpStatus.SC_OK);
}
} }
@Test @Test
void basicAuthLogin() { void basicAuthLogin() {
String auth = "Basic " + Base64.getEncoder().encodeToString("admin:admin".getBytes()); RestAssured.given().auth().preemptive().basic("admin", "admin").get("rest/user/profile").then().statusCode(HttpStatus.SC_OK);
try (Response response = getClient().target(getApiBaseUrl() + "user/profile")
.request()
.header(HttpHeaders.AUTHORIZATION, auth)
.get()) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getStatus());
}
} }
@Test @Test
void wrongPassword() { void wrongPassword() {
String auth = "Basic " + Base64.getEncoder().encodeToString("admin:wrong-password".getBytes()); RestAssured.given()
try (Response response = getClient().target(getApiBaseUrl() + "user/profile") .auth()
.request() .preemptive()
.header(HttpHeaders.AUTHORIZATION, auth) .basic("admin", "wrong-password")
.get()) { .get("rest/user/profile")
Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatus()); .then()
} .statusCode(HttpStatus.SC_UNAUTHORIZED);
} }
@Test @Test
void missingRole() { void missingRole() {
String auth = "Basic " + Base64.getEncoder().encodeToString("demo:demo".getBytes()); RestAssured.given().auth().preemptive().basic("demo", "demo").get("rest/admin/metrics").then().statusCode(HttpStatus.SC_FORBIDDEN);
try (Response response = getClient().target(getApiBaseUrl() + "admin/metrics")
.request()
.header(HttpHeaders.AUTHORIZATION, auth)
.get()) {
Assertions.assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatus());
}
} }
@Test @Test
void apiKey() { void apiKey() {
String auth = "Basic " + Base64.getEncoder().encodeToString("admin:admin".getBytes());
// create api key // create api key
ProfileModificationRequest req = new ProfileModificationRequest(); ProfileModificationRequest req = new ProfileModificationRequest();
req.setCurrentPassword("admin"); req.setCurrentPassword("admin");
req.setNewApiKey(true); req.setNewApiKey(true);
getClient().target(getApiBaseUrl() + "user/profile") RestAssured.given()
.request() .auth()
.header(HttpHeaders.AUTHORIZATION, auth) .preemptive()
.post(Entity.json(req)) .basic("admin", "admin")
.close(); .body(req)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/user/profile")
.then()
.statusCode(HttpStatus.SC_OK);
// fetch api key // fetch api key
String apiKey = getClient().target(getApiBaseUrl() + "user/profile") String apiKey = RestAssured.given()
.request() .auth()
.header(HttpHeaders.AUTHORIZATION, auth) .preemptive()
.get(UserModel.class) .basic("admin", "admin")
.get("rest/user/profile")
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.as(UserModel.class)
.getApiKey(); .getApiKey();
// subscribe to a feed // subscribe to a feed
SubscribeRequest subscribeRequest = new SubscribeRequest(); SubscribeRequest subscribeRequest = new SubscribeRequest();
subscribeRequest.setUrl(getFeedUrl()); subscribeRequest.setUrl(getFeedUrl());
subscribeRequest.setTitle("my title for this feed"); subscribeRequest.setTitle("my title for this feed");
long subscriptionId = getClient().target(getApiBaseUrl() + "feed/subscribe") long subscriptionId = RestAssured.given()
.request() .auth()
.header(HttpHeaders.AUTHORIZATION, auth) .preemptive()
.post(Entity.json(subscribeRequest), Long.class); .basic("admin", "admin")
.body(subscribeRequest)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/feed/subscribe")
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.as(Long.class);
// get entries with api key // get entries with api key
Entries entries = getClient().target(getApiBaseUrl() + "feed/entries") Entries entries = RestAssured.given()
.queryParam("id", subscriptionId) .queryParam("id", subscriptionId)
.queryParam("readType", "unread") .queryParam("readType", "unread")
.queryParam("apiKey", apiKey) .queryParam("apiKey", apiKey)
.request() .get("rest/feed/entries")
.get(Entries.class); .then()
.statusCode(HttpStatus.SC_OK)
.extract()
.as(Entries.class);
Assertions.assertEquals("my title for this feed", entries.getName()); Assertions.assertEquals("my title for this feed", entries.getName());
// mark entry as read and expect it won't work because it's not a GET request // mark entry as read and expect it won't work because it's not a GET request
MarkRequest markRequest = new MarkRequest(); MarkRequest markRequest = new MarkRequest();
markRequest.setId("1"); markRequest.setId("1");
markRequest.setRead(true); markRequest.setRead(true);
try (Response markResponse = getClient().target(getApiBaseUrl() + "entry/mark") RestAssured.given()
.body(markRequest)
.contentType(MediaType.APPLICATION_JSON)
.queryParam("apiKey", apiKey) .queryParam("apiKey", apiKey)
.request() .post("rest/entry/mark")
.post(Entity.json(markRequest))) { .then()
Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, markResponse.getStatus()); .statusCode(HttpStatus.SC_UNAUTHORIZED);
}
} }
} }

View File

@@ -11,15 +11,16 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.hc.core5.http.HttpStatus;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.commafeed.frontend.model.request.FeedModificationRequest; import com.commafeed.frontend.model.request.FeedModificationRequest;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import jakarta.websocket.ClientEndpointConfig; import jakarta.websocket.ClientEndpointConfig;
import jakarta.websocket.CloseReason; import jakarta.websocket.CloseReason;
import jakarta.websocket.ContainerProvider; import jakarta.websocket.ContainerProvider;
@@ -27,17 +28,17 @@ import jakarta.websocket.DeploymentException;
import jakarta.websocket.Endpoint; import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig; import jakarta.websocket.EndpointConfig;
import jakarta.websocket.Session; import jakarta.websocket.Session;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@QuarkusTest @QuarkusTest
@Slf4j @Slf4j
class WebSocketIT extends BaseIT { class WebSocketIT extends BaseIT {
@Override @BeforeEach
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) { void setup() {
return base.register(HttpAuthenticationFeature.basic("admin", "admin")); RestAssured.authentication = RestAssured.preemptive().basic("admin", "admin");
} }
@Test @Test
@@ -94,7 +95,7 @@ class WebSocketIT extends BaseIT {
req.setId(subscriptionId); req.setId(subscriptionId);
req.setName("feed-name"); req.setName("feed-name");
req.setFilter("!title.contains('item 4')"); req.setFilter("!title.contains('item 4')");
getClient().target(getApiBaseUrl() + "feed/modify").request().post(Entity.json(req), Void.class); RestAssured.given().body(req).contentType(MediaType.APPLICATION_JSON).post("rest/feed/modify").then().statusCode(HttpStatus.SC_OK);
AtomicBoolean connected = new AtomicBoolean(); AtomicBoolean connected = new AtomicBoolean();
AtomicReference<String> messageRef = new AtomicReference<>(); AtomicReference<String> messageRef = new AtomicReference<>();

View File

@@ -1,11 +1,10 @@
package com.commafeed.integration.rest; package com.commafeed.integration.rest;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.glassfish.jersey.client.JerseyClientBuilder; import org.apache.hc.core5.http.HttpStatus;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -15,14 +14,15 @@ import com.commafeed.frontend.model.request.IDRequest;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.client.Entity; import io.restassured.RestAssured;
import jakarta.ws.rs.core.MediaType;
@QuarkusTest @QuarkusTest
class AdminIT extends BaseIT { class AdminIT extends BaseIT {
@Override @BeforeEach
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) { void setup() {
return base.register(HttpAuthenticationFeature.basic("admin", "admin")); RestAssured.authentication = RestAssured.preemptive().basic("admin", "admin");
} }
@Nested @Nested
@@ -46,7 +46,12 @@ class AdminIT extends BaseIT {
user.setName("test"); user.setName("test");
user.setPassword("test".getBytes()); user.setPassword("test".getBytes());
user.setEmail("test@test.com"); user.setEmail("test@test.com");
getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user), Void.TYPE); RestAssured.given()
.body(user)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/admin/user/save")
.then()
.statusCode(HttpStatus.SC_OK);
} }
private void modifyUser() { private void modifyUser() {
@@ -56,7 +61,12 @@ class AdminIT extends BaseIT {
.findFirst() .findFirst()
.orElseThrow(() -> new NullPointerException("User not found")); .orElseThrow(() -> new NullPointerException("User not found"));
user.setEmail("new-email@provider.com"); user.setEmail("new-email@provider.com");
getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user), Void.TYPE); RestAssured.given()
.body(user)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/admin/user/save")
.then()
.statusCode(HttpStatus.SC_OK);
} }
private void deleteUser() { private void deleteUser() {
@@ -68,11 +78,17 @@ class AdminIT extends BaseIT {
IDRequest req = new IDRequest(); IDRequest req = new IDRequest();
req.setId(user.getId()); req.setId(user.getId());
getClient().target(getApiBaseUrl() + "admin/user/delete").request().post(Entity.json(req), Void.TYPE); RestAssured.given()
.body(req)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/admin/user/delete")
.then()
.statusCode(HttpStatus.SC_OK);
} }
private List<UserModel> getAllUsers() { private List<UserModel> getAllUsers() {
return Arrays.asList(getClient().target(getApiBaseUrl() + "admin/user/getAll").request().get(UserModel[].class)); return List.of(
RestAssured.given().get("rest/admin/user/getAll").then().statusCode(HttpStatus.SC_OK).extract().as(UserModel[].class));
} }
} }

View File

@@ -11,12 +11,8 @@ import java.util.Objects;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.HttpStatus;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -30,16 +26,15 @@ import com.commafeed.frontend.model.request.MarkRequest;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.client.Entity; import io.restassured.RestAssured;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
@QuarkusTest @QuarkusTest
class FeedIT extends BaseIT { class FeedIT extends BaseIT {
@Override @BeforeEach
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) { void setup() {
return base.register(HttpAuthenticationFeature.basic("admin", "admin")); RestAssured.authentication = RestAssured.preemptive().basic("admin", "admin");
} }
@Nested @Nested
@@ -49,7 +44,14 @@ class FeedIT extends BaseIT {
FeedInfoRequest req = new FeedInfoRequest(); FeedInfoRequest req = new FeedInfoRequest();
req.setUrl(getFeedUrl()); req.setUrl(getFeedUrl());
FeedInfo feedInfo = getClient().target(getApiBaseUrl() + "feed/fetch").request().post(Entity.json(req), FeedInfo.class); FeedInfo feedInfo = RestAssured.given()
.body(req)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/feed/fetch")
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.as(FeedInfo.class);
Assertions.assertEquals("CommaFeed test feed", feedInfo.getTitle()); Assertions.assertEquals("CommaFeed test feed", feedInfo.getTitle());
Assertions.assertEquals(getFeedUrl(), feedInfo.getUrl()); Assertions.assertEquals(getFeedUrl(), feedInfo.getUrl());
} }
@@ -65,13 +67,13 @@ class FeedIT extends BaseIT {
@Test @Test
void subscribeFromUrl() { void subscribeFromUrl() {
try (Response response = getClient().target(getApiBaseUrl() + "feed/subscribe") RestAssured.given()
.queryParam("url", getFeedUrl()) .queryParam("url", getFeedUrl())
.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE) .redirects()
.request() .follow(false)
.get()) { .get("rest/feed/subscribe")
Assertions.assertEquals(HttpStatus.SC_TEMPORARY_REDIRECT, response.getStatus()); .then()
} .statusCode(HttpStatus.SC_TEMPORARY_REDIRECT);
} }
@Test @Test
@@ -89,9 +91,13 @@ class FeedIT extends BaseIT {
IDRequest request = new IDRequest(); IDRequest request = new IDRequest();
request.setId(subscriptionId); request.setId(subscriptionId);
try (Response response = getClient().target(getApiBaseUrl() + "feed/unsubscribe").request().post(Entity.json(request))) { return RestAssured.given()
return response.getStatus(); .body(request)
} .contentType(MediaType.APPLICATION_JSON)
.post("rest/feed/unsubscribe")
.then()
.extract()
.statusCode();
} }
} }
@@ -137,7 +143,13 @@ class FeedIT extends BaseIT {
request.setId(String.valueOf(subscriptionId)); request.setId(String.valueOf(subscriptionId));
request.setOlderThan(olderThan == null ? null : olderThan.toEpochMilli()); request.setOlderThan(olderThan == null ? null : olderThan.toEpochMilli());
request.setInsertedBefore(insertedBefore == null ? null : insertedBefore.toEpochMilli()); request.setInsertedBefore(insertedBefore == null ? null : insertedBefore.toEpochMilli());
getClient().target(getApiBaseUrl() + "feed/mark").request().post(Entity.json(request), Void.TYPE);
RestAssured.given()
.body(request)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/feed/mark")
.then()
.statusCode(HttpStatus.SC_OK);
} }
} }
@@ -152,7 +164,12 @@ class FeedIT extends BaseIT {
IDRequest request = new IDRequest(); IDRequest request = new IDRequest();
request.setId(subscriptionId); request.setId(subscriptionId);
getClient().target(getApiBaseUrl() + "feed/refresh").request().post(Entity.json(request), Void.TYPE); RestAssured.given()
.body(request)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/feed/refresh")
.then()
.statusCode(HttpStatus.SC_OK);
Awaitility.await() Awaitility.await()
.atMost(Duration.ofSeconds(15)) .atMost(Duration.ofSeconds(15))
@@ -165,7 +182,7 @@ class FeedIT extends BaseIT {
// mariadb/mysql timestamp precision is 1 second // mariadb/mysql timestamp precision is 1 second
Instant threshold = Instant.now().minus(Duration.ofSeconds(1)); Instant threshold = Instant.now().minus(Duration.ofSeconds(1));
getClient().target(getApiBaseUrl() + "feed/refreshAll").request().get(Void.TYPE); forceRefreshAllFeeds();
Awaitility.await() Awaitility.await()
.atMost(Duration.ofSeconds(15)) .atMost(Duration.ofSeconds(15))
@@ -185,7 +202,12 @@ class FeedIT extends BaseIT {
req.setId(subscriptionId); req.setId(subscriptionId);
req.setName("new name"); req.setName("new name");
req.setCategoryId(subscription.getCategoryId()); req.setCategoryId(subscription.getCategoryId());
getClient().target(getApiBaseUrl() + "feed/modify").request().post(Entity.json(req), Void.TYPE); RestAssured.given()
.body(req)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/feed/modify")
.then()
.statusCode(HttpStatus.SC_OK);
subscription = getSubscription(subscriptionId); subscription = getSubscription(subscriptionId);
Assertions.assertEquals("new name", subscription.getName()); Assertions.assertEquals("new name", subscription.getName());
@@ -198,10 +220,13 @@ class FeedIT extends BaseIT {
void favicon() throws IOException { void favicon() throws IOException {
Long subscriptionId = subscribe(getFeedUrl()); Long subscriptionId = subscribe(getFeedUrl());
byte[] icon = getClient().target(getApiBaseUrl() + "feed/favicon/{id}") byte[] icon = RestAssured.given()
.resolveTemplate("id", subscriptionId) .get("rest/feed/favicon/{id}", subscriptionId)
.request() .then()
.get(byte[].class); .statusCode(HttpStatus.SC_OK)
.extract()
.response()
.asByteArray();
byte[] defaultFavicon = IOUtils.toByteArray(Objects.requireNonNull(getClass().getResource("/images/default_favicon.gif"))); byte[] defaultFavicon = IOUtils.toByteArray(Objects.requireNonNull(getClass().getResource("/images/default_favicon.gif")));
Assertions.assertArrayEquals(defaultFavicon, icon); Assertions.assertArrayEquals(defaultFavicon, icon);
} }
@@ -210,22 +235,20 @@ class FeedIT extends BaseIT {
@Nested @Nested
class Opml { class Opml {
@Test @Test
void importExportOpml() throws IOException { void importExportOpml() {
importOpml(); importOpml();
String opml = getClient().target(getApiBaseUrl() + "feed/export").request().get(String.class); String opml = RestAssured.given().get("rest/feed/export").then().statusCode(HttpStatus.SC_OK).extract().asString();
Assertions.assertTrue(opml.contains("<title>admin subscriptions in CommaFeed</title>")); Assertions.assertTrue(opml.contains("<title>admin subscriptions in CommaFeed</title>"));
} }
void importOpml() throws IOException { void importOpml() {
InputStream stream = Objects.requireNonNull(getClass().getResourceAsStream("/opml/opml_v2.0.xml")); InputStream stream = Objects.requireNonNull(getClass().getResourceAsStream("/opml/opml_v2.0.xml"));
try (MultiPart multiPart = new MultiPart()) {
multiPart.bodyPart(new StreamDataBodyPart("file", stream));
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
getClient().target(getApiBaseUrl() + "feed/import") RestAssured.given()
.request() .multiPart("file", "opml_v2.0.xml", stream, MediaType.MULTIPART_FORM_DATA)
.post(Entity.entity(multiPart, multiPart.getMediaType()), Void.TYPE); .post("rest/feed/import")
} .then()
.statusCode(HttpStatus.SC_OK);
} }
} }

View File

@@ -1,7 +1,6 @@
package com.commafeed.integration.rest; package com.commafeed.integration.rest;
import org.glassfish.jersey.client.JerseyClientBuilder; import org.apache.hc.core5.http.HttpStatus;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -13,8 +12,8 @@ import com.commafeed.frontend.resource.fever.FeverResponse;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.client.Entity; import io.restassured.RestAssured;
import jakarta.ws.rs.core.Form; import jakarta.ws.rs.core.MediaType;
@QuarkusTest @QuarkusTest
class FeverIT extends BaseIT { class FeverIT extends BaseIT {
@@ -22,21 +21,18 @@ class FeverIT extends BaseIT {
private Long userId; private Long userId;
private String apiKey; private String apiKey;
@Override
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) {
return base.register(HttpAuthenticationFeature.basic("admin", "admin"));
}
@BeforeEach @BeforeEach
void setup() { void setup() {
RestAssured.authentication = RestAssured.preemptive().basic("admin", "admin");
// create api key // create api key
ProfileModificationRequest req = new ProfileModificationRequest(); ProfileModificationRequest req = new ProfileModificationRequest();
req.setCurrentPassword("admin"); req.setCurrentPassword("admin");
req.setNewApiKey(true); req.setNewApiKey(true);
getClient().target(getApiBaseUrl() + "user/profile").request().post(Entity.json(req), Void.TYPE); RestAssured.given().body(req).contentType(MediaType.APPLICATION_JSON).post("rest/user/profile").then().statusCode(HttpStatus.SC_OK);
// retrieve api key // retrieve api key
UserModel user = getClient().target(getApiBaseUrl() + "user/profile").request().get(UserModel.class); UserModel user = RestAssured.given().get("rest/user/profile").then().statusCode(HttpStatus.SC_OK).extract().as(UserModel.class);
this.apiKey = user.getApiKey(); this.apiKey = user.getApiKey();
this.userId = user.getId(); this.userId = user.getId();
} }
@@ -72,13 +68,15 @@ class FeverIT extends BaseIT {
} }
private FeverResponse fetch(String what, String apiKey) { private FeverResponse fetch(String what, String apiKey) {
Form form = new Form(); return RestAssured.given()
form.param("api_key", Digests.md5Hex("admin:" + apiKey)); .auth()
form.param(what, "1"); .none()
.formParam("api_key", Digests.md5Hex("admin:" + apiKey))
return getClient().target(getApiBaseUrl() + "fever/user/{userId}") .formParam(what, 1)
.resolveTemplate("userId", userId) .post("rest/fever/user/{userId}", userId)
.request() .then()
.post(Entity.form(form), FeverResponse.class); .statusCode(HttpStatus.SC_OK)
.extract()
.as(FeverResponse.class);
} }
} }

View File

@@ -7,13 +7,14 @@ import com.commafeed.frontend.model.ServerInfo;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
@QuarkusTest @QuarkusTest
class ServerIT extends BaseIT { class ServerIT extends BaseIT {
@Test @Test
void getServerInfos() { void getServerInfos() {
ServerInfo serverInfos = getClient().target(getApiBaseUrl() + "server/get").request().get(ServerInfo.class); ServerInfo serverInfos = RestAssured.given().get("/rest/server/get").then().statusCode(200).extract().as(ServerInfo.class);
Assertions.assertTrue(serverInfos.isAllowRegistrations()); Assertions.assertTrue(serverInfos.isAllowRegistrations());
Assertions.assertTrue(serverInfos.isSmtpEnabled()); Assertions.assertTrue(serverInfos.isSmtpEnabled());
Assertions.assertTrue(serverInfos.isDemoAccountEnabled()); Assertions.assertTrue(serverInfos.isDemoAccountEnabled());

View File

@@ -11,9 +11,10 @@ import com.commafeed.integration.BaseIT;
import io.quarkus.mailer.MockMailbox; import io.quarkus.mailer.MockMailbox;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.vertx.ext.mail.MailMessage; import io.vertx.ext.mail.MailMessage;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.MediaType;
@QuarkusTest @QuarkusTest
class UserIT extends BaseIT { class UserIT extends BaseIT {
@@ -23,6 +24,8 @@ class UserIT extends BaseIT {
@BeforeEach @BeforeEach
void setup() { void setup() {
RestAssured.authentication = RestAssured.preemptive().basic("admin", "admin");
mailbox.clear(); mailbox.clear();
} }
@@ -30,8 +33,7 @@ class UserIT extends BaseIT {
void resetPassword() { void resetPassword() {
PasswordResetRequest req = new PasswordResetRequest(); PasswordResetRequest req = new PasswordResetRequest();
req.setEmail("admin@commafeed.com"); req.setEmail("admin@commafeed.com");
RestAssured.given().body(req).contentType(MediaType.APPLICATION_JSON).post("rest/user/passwordReset").then().statusCode(200);
getClient().target(getApiBaseUrl() + "user/passwordReset").request().post(Entity.json(req), Void.TYPE);
List<MailMessage> mails = mailbox.getMailMessagesSentTo("admin@commafeed.com"); List<MailMessage> mails = mailbox.getMailMessagesSentTo("admin@commafeed.com");
Assertions.assertEquals(1, mails.size()); Assertions.assertEquals(1, mails.size());

View File

@@ -1,53 +1,42 @@
package com.commafeed.integration.servlet; package com.commafeed.integration.servlet;
import java.net.HttpCookie; import org.apache.hc.core5.http.HttpStatus;
import java.util.List; import org.hamcrest.CoreMatchers;
import java.util.stream.Collectors; import org.junit.jupiter.api.BeforeEach;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.commafeed.frontend.model.Settings; import com.commafeed.frontend.model.Settings;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.client.Entity; import io.restassured.RestAssured;
import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
@QuarkusTest @QuarkusTest
class CustomCodeIT extends BaseIT { class CustomCodeIT extends BaseIT {
@Override @BeforeEach
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) { void setup() {
return base.register(HttpAuthenticationFeature.basic("admin", "admin")); RestAssured.authentication = RestAssured.preemptive().basic("admin", "admin");
} }
@Test @Test
void test() { void test() {
// get settings // get settings
Settings settings = getClient().target(getApiBaseUrl() + "user/settings").request().get(Settings.class); Settings settings = RestAssured.given().get("rest/user/settings").then().statusCode(200).extract().as(Settings.class);
// update settings // update settings
settings.setCustomJs("custom-js"); settings.setCustomJs("custom-js");
settings.setCustomCss("custom-css"); settings.setCustomCss("custom-css");
getClient().target(getApiBaseUrl() + "user/settings").request().post(Entity.json(settings), Void.TYPE); RestAssured.given()
.body(settings)
.contentType(MediaType.APPLICATION_JSON)
.post("rest/user/settings")
.then()
.statusCode(HttpStatus.SC_OK);
// check custom code servlets // check custom code servlets
List<HttpCookie> cookies = login(); RestAssured.given().get("custom_js.js").then().statusCode(HttpStatus.SC_OK).body(CoreMatchers.is("custom-js"));
try (Response response = getClient().target(getBaseUrl() + "custom_js.js") RestAssured.given().get("custom_css.css").then().statusCode(HttpStatus.SC_OK).body(CoreMatchers.is("custom-css"));
.request()
.header(HttpHeaders.COOKIE, cookies.stream().map(HttpCookie::toString).collect(Collectors.joining(";")))
.get()) {
Assertions.assertEquals("custom-js", response.readEntity(String.class));
}
try (Response response = getClient().target(getBaseUrl() + "custom_css.css")
.request()
.header(HttpHeaders.COOKIE, cookies.stream().map(HttpCookie::toString).collect(Collectors.joining(";")))
.get()) {
Assertions.assertEquals("custom-css", response.readEntity(String.class));
}
} }
} }

View File

@@ -5,15 +5,15 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.HttpStatus;
import org.glassfish.jersey.client.ClientProperties;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.Headers;
import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
@QuarkusTest @QuarkusTest
class LogoutIT extends BaseIT { class LogoutIT extends BaseIT {
@@ -21,14 +21,17 @@ class LogoutIT extends BaseIT {
@Test @Test
void test() { void test() {
List<HttpCookie> cookies = login(); List<HttpCookie> cookies = login();
try (Response response = getClient().target(getBaseUrl() + "logout") Headers responseHeaders = RestAssured.given()
.request()
.header(HttpHeaders.COOKIE, cookies.stream().map(HttpCookie::toString).collect(Collectors.joining(";"))) .header(HttpHeaders.COOKIE, cookies.stream().map(HttpCookie::toString).collect(Collectors.joining(";")))
.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE) .redirects()
.get()) { .follow(false)
Assertions.assertEquals(HttpStatus.SC_TEMPORARY_REDIRECT, response.getStatus()); .get("logout")
List<String> setCookieHeaders = response.getStringHeaders().get(HttpHeaders.SET_COOKIE); .then()
Assertions.assertTrue(setCookieHeaders.stream().flatMap(c -> HttpCookie.parse(c).stream()).allMatch(c -> c.getMaxAge() == 0)); .statusCode(HttpStatus.SC_TEMPORARY_REDIRECT)
} .extract()
.headers();
List<String> setCookieHeaders = responseHeaders.getValues(HttpHeaders.SET_COOKIE);
Assertions.assertTrue(setCookieHeaders.stream().flatMap(c -> HttpCookie.parse(c).stream()).allMatch(c -> c.getMaxAge() == 0));
} }
} }

View File

@@ -1,42 +1,34 @@
package com.commafeed.integration.servlet; package com.commafeed.integration.servlet;
import java.net.HttpCookie;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.HttpStatus;
import org.glassfish.jersey.client.ClientProperties; import org.junit.jupiter.api.BeforeEach;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
@QuarkusTest @QuarkusTest
class NextUnreadIT extends BaseIT { class NextUnreadIT extends BaseIT {
@Override @BeforeEach
protected JerseyClientBuilder configureClientBuilder(JerseyClientBuilder base) { void setup() {
return base.register(HttpAuthenticationFeature.basic("admin", "admin")); RestAssured.authentication = RestAssured.preemptive().basic("admin", "admin");
} }
@Test @Test
void test() { void test() {
subscribeAndWaitForEntries(getFeedUrl()); subscribeAndWaitForEntries(getFeedUrl());
List<HttpCookie> cookies = login(); RestAssured.given()
Response response = getClient().target(getBaseUrl() + "next") .redirects()
.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE) .follow(false)
.request() .get("next")
.header(HttpHeaders.COOKIE, cookies.stream().map(HttpCookie::toString).collect(Collectors.joining(";"))) .then()
.get(); .statusCode(HttpStatus.SC_TEMPORARY_REDIRECT)
Assertions.assertEquals(HttpStatus.SC_TEMPORARY_REDIRECT, response.getStatus()); .header(HttpHeaders.LOCATION, "https://hostname.local/commafeed/2");
Assertions.assertEquals("https://hostname.local/commafeed/2", response.getHeaderString(HttpHeaders.LOCATION));
} }
} }

View File

@@ -1,19 +1,17 @@
package com.commafeed.integration.servlet; package com.commafeed.integration.servlet;
import org.junit.jupiter.api.Assertions; import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.commafeed.integration.BaseIT; import com.commafeed.integration.BaseIT;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.core.Response; import io.restassured.RestAssured;
@QuarkusTest @QuarkusTest
class RobotsTxtIT extends BaseIT { class RobotsTxtIT extends BaseIT {
@Test @Test
void test() { void test() {
try (Response response = getClient().target(getBaseUrl() + "robots.txt").request().get()) { RestAssured.given().get("robots.txt").then().statusCode(200).body(CoreMatchers.is("User-agent: *\nDisallow: /"));
Assertions.assertEquals("User-agent: *\nDisallow: /", response.readEntity(String.class));
}
} }
} }