add more tests

This commit is contained in:
Athou
2023-12-16 09:02:56 +01:00
parent 7ef865506f
commit cb4a8df0d2
10 changed files with 218 additions and 20 deletions

View File

@@ -0,0 +1,82 @@
package com.commafeed.integration.rest;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import com.commafeed.CommaFeedConfiguration.ApplicationSettings;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.UserModel;
import com.commafeed.frontend.model.request.IDRequest;
import com.commafeed.integration.BaseIT;
class AdminIT extends BaseIT {
@Test
void getApplicationSettings() {
try (Response response = getClient().target(getApiBaseUrl() + "admin/settings").request().get()) {
ApplicationSettings settings = response.readEntity(ApplicationSettings.class);
Assertions.assertTrue(settings.getAllowRegistrations());
}
}
@Nested
class Users {
@Test
void saveThenDeleteNewUser() {
List<UserModel> existingUsers = getAllUsers();
User user = new User();
user.setName("test");
user.setPassword("test".getBytes());
user.setEmail("test@test.com");
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
List<UserModel> newUsers = getAllUsers();
Assertions.assertEquals(existingUsers.size() + 1, newUsers.size());
UserModel newUser = newUsers.stream().filter(u -> u.getName().equals("test")).findFirst().get();
user.setId(newUser.getId());
}
IDRequest req = new IDRequest();
req.setId(user.getId());
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/delete").request().post(Entity.json(req))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
List<UserModel> newUsers = getAllUsers();
Assertions.assertEquals(existingUsers.size(), newUsers.size());
}
}
@Test
void editExistingUser() {
List<UserModel> existingUsers = getAllUsers();
UserModel user = existingUsers.stream().filter(u -> u.getName().equals("admin")).findFirst().get();
user.setEmail("new-email@provider.com");
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
List<UserModel> newUsers = getAllUsers();
Assertions.assertEquals(existingUsers.size(), newUsers.size());
}
}
private List<UserModel> getAllUsers() {
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/getAll").request().get()) {
return Arrays.asList(response.readEntity(UserModel[].class));
}
}
}
}

View File

@@ -0,0 +1,237 @@
package com.commafeed.integration.rest;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.Date;
import java.util.Objects;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.awaitility.Awaitility;
import org.eclipse.jetty.http.HttpStatus;
import org.glassfish.jersey.client.ClientProperties;
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.Nested;
import org.junit.jupiter.api.Test;
import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.Entry;
import com.commafeed.frontend.model.FeedInfo;
import com.commafeed.frontend.model.Subscription;
import com.commafeed.frontend.model.request.FeedInfoRequest;
import com.commafeed.frontend.model.request.FeedModificationRequest;
import com.commafeed.frontend.model.request.IDRequest;
import com.commafeed.frontend.model.request.MarkRequest;
import com.commafeed.integration.BaseIT;
class FeedIT extends BaseIT {
@Nested
class Fetch {
@Test
void fetchFeed() {
FeedInfoRequest req = new FeedInfoRequest();
req.setUrl(getFeedUrl());
try (Response response = getClient().target(getApiBaseUrl() + "feed/fetch").request().post(Entity.json(req))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
FeedInfo feedInfo = response.readEntity(FeedInfo.class);
Assertions.assertEquals("CommaFeed test feed", feedInfo.getTitle());
Assertions.assertEquals(getFeedUrl(), feedInfo.getUrl());
}
}
}
@Nested
class Subscribe {
@Test
void subscribeAndReadEntries() {
long subscriptionId = subscribe(getFeedUrl());
Awaitility.await().atMost(Duration.ofSeconds(15)).until(() -> getFeedEntries(subscriptionId), e -> e.getEntries().size() == 2);
}
@Test
void subscribeFromUrl() {
try (Response response = getClient().target(getApiBaseUrl() + "feed/subscribe")
.queryParam("url", getFeedUrl())
.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE)
.request()
.get()) {
Assertions.assertEquals(HttpStatus.TEMPORARY_REDIRECT_307, response.getStatus());
}
}
@Test
void unsubscribeFromUnknownFeed() {
Assertions.assertEquals(HttpStatus.NOT_FOUND_404, unsubsribe(1L));
}
@Test
void unsubscribeFromKnownFeed() {
long subscriptionId = subscribe(getFeedUrl());
Assertions.assertEquals(HttpStatus.OK_200, unsubsribe(subscriptionId));
}
private int unsubsribe(long subscriptionId) {
IDRequest request = new IDRequest();
request.setId(subscriptionId);
try (Response response = getClient().target(getApiBaseUrl() + "feed/unsubscribe").request().post(Entity.json(request))) {
return response.getStatus();
}
}
}
@Nested
class Mark {
@Test
void mark() {
long subscriptionId = subscribe(getFeedUrl());
Entries entries = Awaitility.await()
.atMost(Duration.ofSeconds(15))
.until(() -> getFeedEntries(subscriptionId), e -> e.getEntries().size() == 2);
Assertions.assertTrue(entries.getEntries().stream().noneMatch(Entry::isRead));
markFeedEntries(subscriptionId);
Awaitility.await()
.atMost(Duration.ofSeconds(15))
.until(() -> getFeedEntries(subscriptionId), e -> e.getEntries().stream().allMatch(Entry::isRead));
}
private void markFeedEntries(long subscriptionId) {
MarkRequest request = new MarkRequest();
request.setId(String.valueOf(subscriptionId));
try (Response response = getClient().target(getApiBaseUrl() + "feed/mark").request().post(Entity.json(request))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
}
}
}
@Nested
class Refresh {
@Test
void refresh() {
Long subscriptionId = subscribeAndWaitForEntries(getFeedUrl());
Date now = new Date();
refreshFeed(subscriptionId);
Awaitility.await()
.atMost(Duration.ofSeconds(15))
.until(() -> getSubscription(subscriptionId), f -> f.getLastRefresh().after(now));
}
@Test
void refreshAll() {
Long subscriptionId = subscribeAndWaitForEntries(getFeedUrl());
Date now = new Date();
refreshAllFeeds();
Awaitility.await()
.atMost(Duration.ofSeconds(15))
.until(() -> getSubscription(subscriptionId), f -> f.getLastRefresh().after(now));
}
private void refreshFeed(Long subscriptionId) {
IDRequest request = new IDRequest();
request.setId(subscriptionId);
try (Response response = getClient().target(getApiBaseUrl() + "feed/refresh").request().post(Entity.json(request))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
}
}
private void refreshAllFeeds() {
try (Response response = getClient().target(getApiBaseUrl() + "feed/refreshAll").request().get()) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
}
}
}
@Nested
class Modify {
@Test
void modify() {
Long subscriptionId = subscribe(getFeedUrl());
Subscription subscription = getSubscription(subscriptionId);
FeedModificationRequest req = new FeedModificationRequest();
req.setId(subscriptionId);
req.setName("new name");
req.setCategoryId(subscription.getCategoryId());
try (Response response = getClient().target(getApiBaseUrl() + "feed/modify").request().post(Entity.json(req))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
}
subscription = getSubscription(subscriptionId);
Assertions.assertEquals("new name", subscription.getName());
}
}
@Nested
class Favicon {
@Test
void favicon() throws IOException {
Long subscriptionId = subscribe(getFeedUrl());
try (Response response = getClient().target(getApiBaseUrl() + "feed/favicon/{id}")
.resolveTemplate("id", subscriptionId)
.request()
.get()) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
byte[] icon = response.readEntity(byte[].class);
byte[] defaultFavicon = IOUtils.toByteArray(Objects.requireNonNull(getClass().getResource("/images/default_favicon.gif")));
Assertions.assertArrayEquals(defaultFavicon, icon);
}
}
}
@Nested
class Opml {
@Test
void importExportOpml() throws IOException {
importOpml();
String opml = exportOpml();
String expextedOpml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<opml version=\"1.0\">\n" + " <head>\n"
+ " <title>admin subscriptions in CommaFeed</title>\n" + " </head>\n" + " <body>\n"
+ " <outline text=\"out1\" title=\"out1\">\n"
+ " <outline text=\"feed1\" type=\"rss\" title=\"feed1\" xmlUrl=\"https://hostname.local/commafeed/feed1.xml\" />\n"
+ " </outline>\n" + " </body>\n" + "</opml>\n";
Assertions.assertEquals(StringUtils.normalizeSpace(expextedOpml), StringUtils.normalizeSpace(opml));
}
void importOpml() {
InputStream stream = Objects.requireNonNull(getClass().getResourceAsStream("/opml/opml_v2.0.xml"));
MultiPart multiPart = new MultiPart().bodyPart(new StreamDataBodyPart("file", stream));
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
try (Response response = getClient().target(getApiBaseUrl() + "feed/import")
.request()
.post(Entity.entity(multiPart, multiPart.getMediaType()))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
}
}
String exportOpml() {
try (Response response = getClient().target(getApiBaseUrl() + "feed/export").request().get()) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
return response.readEntity(String.class);
}
}
}
}

View File

@@ -0,0 +1,92 @@
package com.commafeed.integration.rest;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;
import org.apache.commons.codec.digest.DigestUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.commafeed.frontend.model.UserModel;
import com.commafeed.frontend.model.request.ProfileModificationRequest;
import com.commafeed.frontend.resource.fever.FeverResponse;
import com.commafeed.integration.BaseIT;
class FeverIT extends BaseIT {
private Long userId;
private String apiKey;
@BeforeEach
void init() {
// create api key
ProfileModificationRequest req = new ProfileModificationRequest();
req.setCurrentPassword("admin");
req.setNewApiKey(true);
try (Response response = getClient().target(getApiBaseUrl() + "user/profile").request().post(Entity.json(req))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
}
// retrieve api key
UserModel user = getClient().target(getApiBaseUrl() + "user/profile").request().get(UserModel.class);
this.apiKey = user.getApiKey();
this.userId = user.getId();
}
@Test
void get() {
try (Response response = getClient().target(getApiBaseUrl() + "fever/user/${userId}")
.resolveTemplate("userId", 1)
.request()
.get()) {
Assertions.assertEquals("Welcome to the CommaFeed Fever API. Add this URL to your Fever-compatible reader.",
response.readEntity(String.class));
}
}
@Test
void invalidApiKey() {
FeverResponse response = fetch("feeds", "invalid-key");
Assertions.assertFalse(response.isAuth());
}
@Test
void validApiKey() {
FeverResponse response = fetch("feeds", apiKey);
Assertions.assertTrue(response.isAuth());
}
@Test
void feeds() {
subscribe(getFeedUrl());
FeverResponse feverResponse = fetch("feeds");
Assertions.assertEquals(1, feverResponse.getFeeds().size());
}
@Test
void unreadEntries() {
subscribeAndWaitForEntries(getFeedUrl());
FeverResponse feverResponse = fetch("unread_item_ids");
Assertions.assertEquals(2, feverResponse.getUnreadItemIds().size());
}
private FeverResponse fetch(String what) {
return fetch(what, apiKey);
}
private FeverResponse fetch(String what, String apiKey) {
Form form = new Form();
form.param("api_key", DigestUtils.md5Hex("admin:" + apiKey));
form.param(what, "1");
try (Response response = getClient().target(getApiBaseUrl() + "fever/user/{userId}")
.resolveTemplate("userId", userId)
.request()
.post(Entity.form(form))) {
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus());
return response.readEntity(FeverResponse.class);
}
}
}