aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-09-09 21:17:58 +0300
committerGravatar Vitaly Takmazov2018-09-09 21:17:58 +0300
commita0dbddf1f4a5a8049602de69875868fcb14beba8 (patch)
treee6d3397649a30e65cccd794ee2a964df783164e6 /juick-server/src/test/java/com/juick/server/tests/ServerTests.java
parent12567412b0b8a091240b4b014c08c6e16936d75b (diff)
merge webapp tests
Diffstat (limited to 'juick-server/src/test/java/com/juick/server/tests/ServerTests.java')
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java296
1 files changed, 295 insertions, 1 deletions
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index fc18dbf5..9fe877f3 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -20,6 +20,12 @@ package com.juick.server.tests;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.gargoylesoftware.htmlunit.CookieManager;
+import com.gargoylesoftware.htmlunit.Page;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.css.StyleElement;
+import com.gargoylesoftware.htmlunit.html.DomElement;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.jayway.jsonpath.JsonPath;
import com.juick.*;
import com.juick.server.*;
@@ -30,16 +36,21 @@ import com.juick.server.helpers.CommandResult;
import com.juick.server.helpers.TagStats;
import com.juick.server.util.HttpUtils;
import com.juick.server.util.ImageUtils;
+import com.juick.server.www.Utils;
import com.juick.server.xmpp.helpers.XMPPStatus;
import com.juick.server.xmpp.s2s.ConnectionIn;
import com.juick.service.*;
import com.juick.util.DateFormattersHolder;
import com.juick.util.MessageUtils;
+import com.mitchellbosecke.pebble.PebbleEngine;
+import com.mitchellbosecke.pebble.error.PebbleException;
+import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringEscapeUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -50,8 +61,10 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.core.io.ClassPathResource;
import org.springframework.http.*;
import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -76,6 +89,7 @@ import rocks.xmpp.core.stanza.model.client.ClientMessage;
import rocks.xmpp.core.stanza.model.errors.Condition;
import javax.inject.Inject;
+import javax.servlet.http.Cookie;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
@@ -100,7 +114,9 @@ import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
+import java.util.stream.StreamSupport;
+import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
@@ -114,13 +130,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
-@TestPropertySource(properties = {"broken_ssl_hosts=localhost,serverstorageisfull.tld"})
+@TestPropertySource(properties = {"broken_ssl_hosts=localhost,serverstorageisfull.tld", "ios_app_id=12345678.com.juick.ExampleApp"})
@AutoConfigureMockMvc
public class ServerTests {
@Inject
private MockMvc mockMvc;
@Inject
+ private WebClient webClient;
+ @Inject
private TestRestTemplate restTemplate;
@Inject
private MessagesService messagesService;
@@ -166,6 +184,10 @@ public class ServerTests {
private String tmpDir;
@Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
private String imgDir;
+ @Inject
+ private PebbleEngine pebbleEngine;
+ @Value("${ios_app_id:}")
+ private String appId;
private static User ugnich, freefd, juick;
static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword;
@@ -629,6 +651,7 @@ public class ServerTests {
}
@Test
public void botIsUpAndProcessingResourceConstraints() throws Exception {
+ jdbcTemplate.execute("DELETE FROM users WHERE nick='renha'");
int renhaId = userService.createUser("renha", "umnnbt");
Jid from = Jid.of("renha@serverstorageisfull.tld");
jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)",
@@ -903,6 +926,7 @@ public class ServerTests {
@Test
public void lastReadTests() throws Exception {
+ jdbcTemplate.execute("DELETE FROM bl_users");
assertThat(userService.isInBLAny(ugnich.getUid(), freefd.getUid()), is(false));
int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, null);
subscriptionService.subscribeMessage(messagesService.getMessage(mid), ugnich);
@@ -1294,6 +1318,10 @@ public class ServerTests {
userService.createUser(userName, userPassword);
mockMvc.perform(get("/api/auth").with(httpBasic(userName, userPassword))).andExpect(status().isUnauthorized());
+ mockMvc.perform(post("/login")
+ .param("username", userName)
+ .param("password", userPassword)).andExpect(status().is3xxRedirection())
+ .andExpect(redirectedUrl("/login?error=1"));
}
@Test
public void bannedUserShouldBeShadowedFromRecommendationsList() throws IOException {
@@ -1357,4 +1385,270 @@ public class ServerTests {
.andExpect(jsonPath("$.orderedItems", hasSize(20)))
.andExpect(jsonPath("$.next", is("http://localhost:8080/u/ugnich/blog?before=" + midsPage.get(midsPage.size() - 1))));
}
+ @Test
+ public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception {
+ String msgText = "Привет, я - Угнич";
+ int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null);
+ HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
+ assertThat(threadPage.getTitleText(), equalTo("ugnich:"));
+ }
+ @Test
+ public void repliesList() throws IOException {
+ int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null);
+ IntStream.range(1, 15).forEach(i ->
+ messagesService.createReply(mid, i-1, freefd, String.valueOf(i-1), null ));
+
+ HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
+ assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200));
+ Long visibleItems = StreamSupport.stream(threadPage.getHtmlElementById("replies")
+ .getChildElements().spliterator(), false).filter(e -> {
+ StyleElement display = e.getStyleElement("display");
+ return display == null || !display.getValue().equals("none");
+ }).count();
+ assertThat(visibleItems, equalTo(14L));
+ }
+ @Test
+ public void userShouldNotSeeReplyButtonToBannedUser() throws Exception {
+ int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, null);
+ messagesService.createReply(mid, 0, ugnich, "yo", null);
+ MvcResult loginResult = mockMvc.perform(post("/login")
+ .param("username", freefdName)
+ .param("password", freefdPassword))
+ .andExpect(status().isFound()).andReturn();
+ Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me");
+ webClient.setCookieManager(new CookieManager());
+ webClient.getCookieManager().addCookie(
+ new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(),
+ loginCookie.getName(),
+ loginCookie.getValue()));
+ HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
+ assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200));
+ assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(false));
+ assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(false));
+ privacyQueriesService.blacklistUser(freefd, ugnich);
+ assertThat(userService.isInBLAny(freefd.getUid(), ugnich.getUid()), equalTo(true));
+ int renhaId = userService.createUser("renha", "secret");
+ messagesService.createReply(mid, 0, userService.getUserByUID(renhaId).orElseThrow(IllegalStateException::new),
+ "people", null);
+ threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
+ assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200));
+ assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(true));
+ assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(true));
+ }
+ @Test
+ public void correctTagsEscaping() throws PebbleException, IOException {
+ PebbleTemplate template = pebbleEngine.getTemplate("views/test");
+ Writer writer = new StringWriter();
+ template.evaluate(writer,
+ Collections.singletonMap("tagsList",
+ Collections.singletonList(StringEscapeUtils.escapeHtml4(new Tag(">_<").getName()))));
+ String output = writer.toString().trim();
+ assertThat(output, equalTo("<a href=\"/ugnich/?tag=%26gt%3B_%26lt%3B\">&gt;_&lt;</a>"));
+ }
+
+ public DomElement fetchMeta(String url, String name) throws IOException {
+ HtmlPage page = webClient.getPage(url);
+ DomElement emptyMeta = new DomElement("", "meta", null, null);
+ return page.getElementsByTagName("meta").stream()
+ .filter(t -> t.getAttribute("name").equals(name)).findFirst().orElse(emptyMeta);
+ }
+ @Test
+ public void testTwitterCards() throws Exception {
+
+ int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, null);
+
+ assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card")
+ .getAttribute("content"), equalTo("summary"));
+ int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null);
+ Message message = messagesService.getMessage(mid2);
+ assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card")
+ .getAttribute("content"), equalTo("summary_large_image"));
+ assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "og:description")
+ .getAttribute("content"),
+ startsWith(StringEscapeUtils.escapeHtml4(MessageUtils.getMessageHashTags(message))));
+ }
+ @Test
+ public void postMessageTests() throws Exception {
+ mockMvc.perform(post("/post2").param("body", "yo"))
+ .andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("http://localhost/login"));
+ MvcResult loginResult = mockMvc.perform(post("/login")
+ .param("username", ugnichName)
+ .param("password", ugnichPassword)).andReturn();
+ String msgText = "yoppppl";
+ mockMvc.perform(post("/post2")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("body", msgText)).andExpect(status().isFound());
+ Message lastMessage = messagesService.getMessage(messagesService.getMyFeed(ugnich.getUid(), 0, false).get(0));
+ assertThat(lastMessage.getText(), equalTo(msgText));
+ mockMvc.perform(post("/post2")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isFound());
+ lastMessage = messagesService.getMessage(messagesService.getMyFeed(ugnich.getUid(), 0, false).get(0));
+ assertThat(lastMessage.getAttachmentType(), equalTo("png"));
+ mockMvc.perform(post("/post2")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("img", "bad_url")).andExpect(status().isBadRequest());
+ FileInputStream fi = new FileInputStream(new ClassPathResource("static/tagscloud.png").getFile());
+ MockMultipartFile file = new MockMultipartFile("attach", fi);
+ mockMvc.perform(multipart("/post2")
+ .file(file)
+ .cookie(loginResult.getResponse().getCookies())).andExpect(status().isFound());
+ int mid = messagesService.createMessage(ugnich.getUid(), "dummy message", null, null);
+ mockMvc.perform(post("/comment")
+ .param("mid", String.valueOf(mid))
+ .param("body", "yo")).andExpect(redirectedUrl("http://localhost/login"));
+ mockMvc.perform(post("/comment")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("wrong_param", "yo")).andExpect(status().isBadRequest());
+ mockMvc.perform(post("/comment")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("mid", String.valueOf(mid))
+ .param("wrong_param", "yo")).andExpect(status().isBadRequest());
+ mockMvc.perform(post("/comment")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("mid", String.valueOf(mid))
+ .param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isFound());
+ mockMvc.perform(multipart("/comment")
+ .file(file)
+ .cookie(loginResult.getResponse().getCookies())
+ .param("mid", String.valueOf(mid))).andExpect(status().isFound());
+ mockMvc.perform(post("/comment")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("mid", String.valueOf(mid))
+ .param("body", "yo")).andExpect(redirectedUrl(String.format("/%s/%d#%d", ugnichName, mid, 3)));
+ mockMvc.perform(post("/post2")
+ .cookie(loginResult.getResponse().getCookies())
+ .param("body", String.format("D #%d/%d", mid, 3)))
+ .andExpect(status().isFound());
+ assertThat(messagesService.getReplies(ugnich, mid).size(), equalTo(2));
+ }
+ @Test
+ public void hashLoginShouldNotUseSession() throws Exception {
+ String hash = userService.getHashByUID(ugnich.getUid());
+ MvcResult hashLoginResult = mockMvc.perform(get("/?show=my&hash=" + hash))
+ .andExpect(status().isOk())
+ .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash))))
+ .andExpect(content().string(containsString(hash)))
+ .andReturn();
+ Cookie rememberMeFromHash = hashLoginResult.getResponse().getCookie("juick-remember-me");
+ MvcResult formLoginResult = mockMvc.perform(post("/login")
+ .param("username", ugnichName)
+ .param("password", ugnichPassword))
+ .andExpect(status().is3xxRedirection()).andReturn();
+ Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me");
+ mockMvc.perform(get("/?show=my").cookie(rememberMeFromForm)).andExpect(status().isOk())
+ .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash))))
+ .andExpect(content().string(containsString(hash)));
+ mockMvc.perform(get("/?show=my").cookie(rememberMeFromHash)).andExpect(status().isOk())
+ .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash))))
+ .andExpect(content().string(containsString(hash)));
+ }
+ @Test
+ public void nonExistentBlogShouldReturn404() throws Exception {
+ mockMvc.perform(get("/ololoe/")).andExpect(status().isNotFound());
+ }
+ @Test
+ public void discussionsShouldBePageableByTimestamp() throws Exception {
+ String msgText = "Привет, я снова Угнич";
+ int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null);
+ int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, null);
+ MvcResult loginResult = mockMvc.perform(post("/login")
+ .param("username", freefdName)
+ .param("password", freefdPassword))
+ .andExpect(status().is3xxRedirection()).andReturn();
+ Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me");
+ webClient.setCookieManager(new CookieManager());
+ webClient.getCookieManager().addCookie(
+ new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(),
+ loginCookie.getName(),
+ loginCookie.getValue()));
+ String discussionsUrl = "http://localhost:8080/?show=discuss";
+ HtmlPage discussions = webClient.getPage(discussionsUrl);
+ assertThat(discussions.querySelectorAll("article").size(), is(0));
+ subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd);
+ discussions = (HtmlPage) discussions.refresh();
+ assertThat(discussions.querySelectorAll("article").size(), is(1));
+ subscriptionService.subscribeMessage(messagesService.getMessage(midNew), freefd);
+ discussions = (HtmlPage) discussions.refresh();
+ assertThat(discussions.querySelectorAll("article").size(), is(2));
+ assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
+ messagesService.createReply(mid, 0, freefd, "I'm replied", null);
+ discussions = (HtmlPage) discussions.refresh();
+ assertThat(discussions.querySelectorAll("article").size(), is(2));
+ assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid)));
+ Message msg = messagesService.getMessage(mid);
+ HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "&to=" + msg.getUpdated().toEpochMilli());
+ assertThat(discussionsOld.querySelectorAll("article").size(), is(1));
+ assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
+ List<Integer> newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList());
+ for (Integer m : newMids) {
+ subscriptionService.subscribeMessage(messagesService.getMessage(m), freefd);
+ }
+ discussions = (HtmlPage) discussions.refresh();
+ assertThat(discussions.querySelectorAll("article").size(), is(20));
+ assertThat(discussions.querySelectorAll("article")
+ .get(19).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid)));
+ messagesService.createReply(midNew, 0, freefd, "I'm replied", null);
+ discussions = (HtmlPage) discussions.refresh();
+ assertThat(discussions.querySelectorAll("article")
+ .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
+ Message old = messagesService.getMessage(newMids.get(0));
+ discussionsOld = webClient.getPage(discussionsUrl + "&to=" + old.getUpdated().toEpochMilli());
+ assertThat(discussionsOld.querySelectorAll("article").size(), is(1));
+ assertThat(discussionsOld.querySelectorAll("article")
+ .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid)));
+ }
+ @Test
+ public void redirectParamShouldCorrectlyRedirectLoggedUser() throws Exception {
+ MvcResult formLoginResult = mockMvc.perform(post("/login")
+ .param("username", ugnichName)
+ .param("password", ugnichPassword))
+ .andExpect(status().isFound()).andReturn();
+ Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me");
+ mockMvc.perform(get("/login").cookie(rememberMeFromForm))
+ .andExpect(status().is3xxRedirection())
+ .andExpect(redirectedUrl("/"));
+ mockMvc.perform(get("/login?redirect=false").cookie(rememberMeFromForm))
+ .andExpect(status().is3xxRedirection())
+ .andExpect(redirectedUrl("/login/success"));
+ }
+ @Test
+ public void anythingRedirects() throws Exception {
+ int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null);
+ mockMvc.perform(get(String.format("/%d", mid)))
+ .andExpect(status().isMovedPermanently())
+ .andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid)));
+ }
+ @Test
+ public void appAssociationsTest() throws Exception {
+ mockMvc.perform((get("/.well-known/apple-app-site-association")))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
+ .andExpect(jsonPath("$.webcredentials.apps[0]", is(appId)));
+ }
+ @Test
+ public void notificationsTests() throws Exception {
+ MvcResult loginResult = mockMvc.perform(post("/login")
+ .param("username", freefdName)
+ .param("password", freefdPassword))
+ .andExpect(status().is3xxRedirection()).andReturn();
+ Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me");
+ webClient.setCookieManager(new CookieManager());
+ webClient.getCookieManager().addCookie(
+ new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(),
+ loginCookie.getName(),
+ loginCookie.getValue()));
+ int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, null);
+ subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd);
+ int rid = messagesService.createReply(mid, 0, ugnich, "new reply", null);
+ HtmlPage discussionsPage = webClient.getPage("http://localhost:8080/?show=discuss");
+ assertThat(discussionsPage.querySelectorAll("#global a .badge").size(), is(1));
+ HtmlPage unreadThread = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
+ assertThat(unreadThread.querySelectorAll("#global a .badge").size(), is(0));
+ }
+ @Test
+ public void escapeSqlTests() {
+ String sql = String.format("SELECT * FROM table WHERE data='%s'", Utils.encodeSphinx("';-- DROP TABLE table"));
+ assertThat(sql, is("SELECT * FROM table WHERE data='\\';-- DROP TABLE table\'"));
+ }
}