aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/test/java/com/juick
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/test/java/com/juick')
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java191
1 files changed, 158 insertions, 33 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 7220deeb..52504f4a 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
@@ -17,7 +17,6 @@
package com.juick.server.tests;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.ExternalToken;
@@ -27,28 +26,23 @@ import com.juick.User;
import com.juick.server.EmailManager;
import com.juick.server.XMPPBot;
import com.juick.server.XMPPServer;
-import com.juick.server.configuration.ApiAppConfiguration;
+import com.juick.server.helpers.AnonymousUser;
import com.juick.server.helpers.TagStats;
import com.juick.service.*;
import com.juick.util.DateFormattersHolder;
+import com.juick.util.MessageUtils;
+import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.context.ApplicationListener;
+import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
@@ -64,12 +58,9 @@ import rocks.xmpp.core.stanza.model.server.ServerMessage;
import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
-import java.text.ParseException;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Scanner;
-import java.util.concurrent.CountDownLatch;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.util.*;
import java.util.stream.IntStream;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -100,8 +91,6 @@ public class ServerTests {
@Inject
private ObjectMapper jsonMapper;
@Inject
- private ImagesService imagesService;
- @Inject
private WebApplicationContext wac;
@Inject
private XMPPServer server;
@@ -113,6 +102,16 @@ public class ServerTests {
private PrivacyQueriesService privacyQueriesService;
@Inject
private JdbcTemplate jdbcTemplate;
+ @Inject
+ private EmailService emailService;
+ @Inject
+ private PMQueriesService pmQueriesService;
+ @Inject
+ private TelegramService telegramService;
+ @Inject
+ private CrosspostService crosspostService;
+ @Inject
+ private ImagesService imagesService;
@Value("${hostname:localhost}")
private Jid jid;
@@ -123,9 +122,6 @@ public class ServerTests {
private static boolean isSetUp = false;
- @Mock
- ApplicationListener<com.juick.server.component.MessageEvent> listener;
-
@Before
public void setUp() {
if (!isSetUp) {
@@ -148,16 +144,148 @@ public class ServerTests {
msg = messagesService.getMessage(mid);
tagService.createTag("ั‚ะตัั‚");
juickTagId = tagService.createTag("juick");
- Mockito.doAnswer(new Answer() {
- @Override
- public Object answer(InvocationOnMock invocation) throws Throwable {
- latch.countDown();
- return null;
- }
- }).when(listener).onApplicationEvent(Mockito.any());
isSetUp = true;
}
}
+ @Test
+ public void getMyFeed() {
+ int mid0 = messagesService.createMessage(ugnich.getUid(), "test", null, null);
+ int mid2 = messagesService.createMessage(ugnich.getUid(), "test2", null, null);
+ subscriptionService.subscribeUser(userService.getUserByUID(freefd.getUid()).orElse(AnonymousUser.INSTANCE),
+ userService.getUserByUID(ugnich.getUid()).orElse(AnonymousUser.INSTANCE));
+ List<Integer> freefdFeed = messagesService.getMyFeed(freefd.getUid(), 0, false);
+ assertThat(freefdFeed.get(0), equalTo(mid2));
+ int tonyaid = userService.createUser("Tonya", "secret");
+ int mid3 = messagesService.createMessage(tonyaid, "test3", null, null);
+ messagesService.recommendMessage(mid3, ugnich.getUid());
+ assertThat(messagesService.getMyFeed(freefd.getUid(), 0, false).get(0), equalTo(mid2));
+ assertThat(messagesService.getMyFeed(freefd.getUid(), 0, true).get(0), equalTo(mid3));
+ assertThat(messagesService.getMyFeed(freefd.getUid(), mid2, true).get(0), equalTo(mid0));
+ assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Added));
+ assertThat(messagesService.getMessage(mid0).getLikes(), equalTo(1));
+ assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Deleted));
+ assertThat(messagesService.getMessage(mid0).getLikes(), equalTo(0));
+ assertThat(messagesService.getAll(ugnich.getUid(), 0).get(0), equalTo(mid3));
+ Tag yoTag = tagService.getTag("yoyo", true);
+ assertThat(tagService.getTag("YOYO", false), equalTo(yoTag));
+ int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, Collections.singletonList(yoTag));
+ List<User> subscribers = subscriptionService.getSubscribedUsers(ugnich.getUid(), mid);
+
+ telegramService.createTelegramUser(12345, "freefd");
+ String loginhash = jdbcTemplate.queryForObject("SELECT loginhash FROM telegram where tg_id=?",
+ String.class, 12345);
+ crosspostService.setTelegramUser(loginhash, freefd.getUid());
+
+ List<Long> telegramSubscribers = telegramService.getTelegramIdentifiers(subscribers);
+ assertThat(subscribers.size(), equalTo(1));
+ assertThat(subscribers.size(), equalTo(telegramSubscribers.size()));
+ assertThat(subscribers.get(0).getUid(), equalTo(freefd.getUid()));
+ tagService.blacklistTag(freefd, yoTag);
+ List<User> subscribers2 = subscriptionService.getSubscribedUsers(ugnich.getUid(), mid);
+ assertThat(subscribers2.size(), equalTo(0));
+ assertThat(telegramService.getTelegramIdentifiers(subscribers2).size(), equalTo(0));
+ tagService.blacklistTag(freefd, yoTag);
+ assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), mid).size(), equalTo(1));
+ }
+ @Test
+ public void pmTests() {
+ pmQueriesService.createPM(freefd.getUid(), ugnich.getUid(), "hello");
+ Message pm = pmQueriesService.getPMMessages(ugnich.getUid(), freefd.getUid()).get(0);
+ assertThat(pm.getText(), equalTo("hello"));
+ assertThat(pm.getUser().getUid(), equalTo(freefd.getUid()));
+ }
+
+ @Test
+ public void messageTests() {
+ int user_id = userService.createUser("mmmme", "secret");
+ User user = userService.getUserByUID(user_id).orElse(new User());
+ assertEquals("it should be me", "mmmme", user.getName());
+ int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
+ Message msg = messagesService.getMessage(mid);
+ assertEquals("yo", msg.getText());
+ User me = msg.getUser();
+ assertEquals("mmmme", me.getName());
+ assertEquals("mmmme", messagesService.getMessageAuthor(mid).getName());
+ int tagID = tagService.createTag("weather");
+ Tag tag = tagService.getTag(tagID);
+ List<Tag> tagList = new ArrayList<>();
+ tagList.add(tag);
+ int mid2 = messagesService.createMessage(user_id, "yo2", null, tagList);
+ Message msg2 = messagesService.getMessage(mid2);
+ assertEquals(1, msg2.getTags().size());
+ assertEquals("we already have ugnich", -1, userService.createUser("ugnich", "x"));
+ int ugnich_id = userService.createUser("hugnich", "x");
+ User ugnich = userService.getUserByUID(ugnich_id).orElse(new User());
+ int rid = messagesService.createReply(msg2.getMid(), 0, ugnich.getUid(), "bla-bla", null);
+ assertEquals(1, rid);
+ assertThat(msg2.getTo(), equalTo(null));
+ Message reply = messagesService.getReply(msg2.getMid(), rid);
+ assertThat(reply.getTo().getName(), equalTo(user.getName()));
+ List<Message> replies = messagesService.getReplies(msg2.getMid());
+ assertThat(replies.size(), equalTo(1));
+ assertThat(replies.get(0), equalTo(reply));
+ int ridToReply = messagesService.createReply(msg2.getMid(), 1, ugnich_id, "blax2", null);
+ Message reply2 = messagesService.getReply(msg2.getMid(), ridToReply);
+ assertThat(reply.getTo().getName(), equalTo(user.getName()));
+ List<Message> replies2 = messagesService.getReplies(msg2.getMid());
+ assertThat(replies2.size(), equalTo(2));
+ assertThat(replies2.get(1), equalTo(reply2));
+ Message msg3 = messagesService.getMessage(mid2);
+ assertEquals(2, msg3.getReplies());
+ assertEquals("weather", msg3.getTags().get(0).getName());
+ assertEquals(ugnich.getUid(), userService.checkPassword(ugnich.getName(), "x"));
+ assertEquals(-1, userService.checkPassword(ugnich.getName(), "xy"));
+ subscriptionService.subscribeMessage(msg.getMid(), user.getUid());
+ subscriptionService.subscribeMessage(msg.getMid(), ugnich.getUid());
+ int reply_id = messagesService.createReply(msg.getMid(), 0, ugnich_id, "comment", null);
+ assertEquals(1, subscriptionService.getUsersSubscribedToComments(msg,
+ messagesService.getReply(msg.getMid(), reply_id)).size());
+ assertThat(messagesService.getDiscussions(ugnich.getUid(), Instant.now().toEpochMilli()).get(0),
+ equalTo(msg.getMid()));
+ messagesService.deleteMessage(user_id, mid);
+ messagesService.deleteMessage(user_id, mid2);
+ String htmlTagName = ">_<";
+ Tag htmlTag = tagService.getTag(htmlTagName, true);
+ TagStats htmlTagStats = new TagStats();
+ htmlTagStats.setTag(htmlTag);
+ String dbTagName = jdbcTemplate.queryForObject("select name from tags where name=?", String.class, htmlTagName);
+ assertEquals("db tags should not be escaped", dbTagName, htmlTag.getName());
+ int mid4 = messagesService.createMessage(user_id, "yoyoyo", null, null);
+ Message msg4 = messagesService.getMessage(mid4);
+ assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4));
+ messagesService.deleteMessage(user_id, mid4);
+ }
+ @Test
+ public void lastJidShouldNotBeDeleted() {
+ int ugnich_id = userService.createUser("hugnich2", "x");
+ jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", ugnich_id, "firstjid@localhost", 1);
+ jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", ugnich_id, "secondjid@localhost", 1);
+ assertThat(userService.deleteJID(ugnich_id, "secondjid@localhost"), equalTo(true));
+ assertThat(userService.deleteJID(ugnich_id, "firstjid@localhost"), equalTo(false));
+ }
+ @Test
+ public void lastEmailShouldNotBeDeleted() {
+ int ugnich_id = userService.createUser("hugnich3", "x");
+ jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", ugnich_id, "first@localhost");
+ jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", ugnich_id, "second@localhost");
+ assertThat(emailService.deleteEmail(ugnich_id, "second@localhost"), equalTo(true));
+ assertThat(emailService.deleteEmail(ugnich_id, "first@localhost"), equalTo(false));
+ }
+ @Test
+ public void messageUpdatedTimeShouldMatchLastReplyTime() throws InterruptedException {
+ int ugnich_id = userService.createUser("hugnich4", "x");
+ int mid = messagesService.createMessage(ugnich_id, "yo", null, null);
+ Instant ts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?",
+ Timestamp.class, mid).toInstant();
+ Thread.sleep(1000);
+ int rid = messagesService.createReply(mid, 0, ugnich_id, "people", null);
+ Instant rts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?",
+ Timestamp.class, mid).toInstant();
+ assertThat(rts, greaterThan(ts));
+ Message msg = messagesService.getReply(mid, rid);
+ assertThat(rts, equalTo(msg.getTimestamp()));
+ messagesService.deleteMessage(ugnich_id, mid);
+ }
@Test
public void testAllUnAuthorized() throws Exception {
@@ -291,7 +419,7 @@ public class ServerTests {
MvcResult result = mockMvc.perform(get("/auth").with(httpBasic(ugnichName, ugnichPassword)))
.andExpect(status().isOk())
.andReturn();
- String authHash = jsonMapper.readValue(result.getResponse().getContentAsString(), String.class);
+ String authHash = result.getResponse().getContentAsString();
assertThat(authHash, equalTo(ugnichHash));
mockMvc.perform(get("/home").param("hash", ugnichHash)).andExpect(status().isOk());
}
@@ -394,7 +522,7 @@ public class ServerTests {
}
@Test
- public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException, JsonProcessingException {
+ public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
int uid = userService.createUser("me", "secret");
User user = userService.getUserByUID(uid).orElse(new User());
Tag yo = tagService.getTag("yo", true);
@@ -487,7 +615,6 @@ public class ServerTests {
bot.processCommand(user, Jid.of("test@localhost"), "D #" + mid).get());
assertEquals("should be not found", "Message not found",
bot.processCommand(user, Jid.of("test@localhost"), "#" + mid).get());
- assertThat(latch.getCount(), equalTo(0L));
}
@Test
public void mailParserTest() throws Exception {
@@ -527,6 +654,4 @@ public class ServerTests {
Assert.assertThat(count, equalTo(0L));
mockMvc.perform(get("/ws/_all")).andExpect(status().isBadRequest());
}
-
- private static CountDownLatch latch = new CountDownLatch(1);
}