aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/test/java/com/juick/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-02-17 22:13:12 +0300
committerGravatar Vitaly Takmazov2018-02-17 22:13:12 +0300
commitdd8a51e0f342c183116d8b2af3f12394b0ac53dd (patch)
treeea88db8f0c79256f9527d1220023f2ba5d24994d /juick-server/src/test/java/com/juick/server
parentae265f84efc37619411fb21d92b51183a4744478 (diff)
server: merge xmpp
Diffstat (limited to 'juick-server/src/test/java/com/juick/server')
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/XMPPServerTests.java190
1 files changed, 190 insertions, 0 deletions
diff --git a/juick-server/src/test/java/com/juick/server/tests/XMPPServerTests.java b/juick-server/src/test/java/com/juick/server/tests/XMPPServerTests.java
new file mode 100644
index 00000000..5a3c8e69
--- /dev/null
+++ b/juick-server/src/test/java/com/juick/server/tests/XMPPServerTests.java
@@ -0,0 +1,190 @@
+package com.juick.server.tests;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.configuration.RepositoryConfiguration;
+import com.juick.server.XMPPBot;
+import com.juick.server.XMPPServer;
+import com.juick.server.configuration.ApiAppConfiguration;
+import com.juick.server.configuration.ApiSecurityConfig;
+import com.juick.service.MessagesService;
+import com.juick.service.SubscriptionService;
+import com.juick.service.TagService;
+import com.juick.service.UserService;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+import rocks.xmpp.addr.Jid;
+import rocks.xmpp.core.stanza.model.Stanza;
+import rocks.xmpp.core.stanza.model.server.ServerMessage;
+
+import javax.inject.Inject;
+import java.lang.reflect.InvocationTargetException;
+import java.text.ParseException;
+import java.util.Collections;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@WebAppConfiguration
+@ContextConfiguration(classes = {
+ ApiAppConfiguration.class, ApiSecurityConfig.class, RepositoryConfiguration.class
+})
+@TestPropertySource(properties = {"broken_ssl_hosts=localhost,serverstorageisfull.tld"})
+public class XMPPServerTests extends AbstractJUnit4SpringContextTests {
+ @Inject
+ private WebApplicationContext wac;
+ @Inject
+ private XMPPServer server;
+ @Inject
+ private XMPPBot bot;
+ @Inject
+ private UserService userService;
+ @Inject
+ private MessagesService messagesService;
+ @Inject
+ private TagService tagService;
+ @Inject
+ private SubscriptionService subscriptionService;
+ @Inject
+ private JdbcTemplate jdbcTemplate;
+ @Value("${hostname}")
+ private Jid jid;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() {
+ mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
+ }
+ @Test
+ public void statusPageIsUp() throws Exception {
+ mockMvc.perform(get("http://localhost:8080/status")).andExpect(status().isOk());
+ assertThat(server.getJid(), equalTo(jid));
+ }
+ @Test
+ public void botIsUpAndProcessingResourceConstraints() {
+ int renhaId;
+ renhaId = userService.createUser("renha", "umnnbt");
+ Jid from = Jid.of("renha@serverstorageisfull.tld");
+ jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", renhaId, from.toEscapedString(), 1);
+ String xmlMessage = "<message xmlns=\"jabber:server\" from=\"renha@serverstorageisfull.tld\" to=\"juick@juick.com/Juick\" type=\"error\"><body>Reply by @LexX</body><juick xmlns=\"http://juick.com/message\" mid=\"2885759\" privacy=\"1\" replyto=\"0\" rid=\"8\" ts=\"2017-10-10 07:41:10\"><body>Похоже нынче можно публично заявлять о своем веганстве. </body><user xmlns=\"http://juick.com/user\" uname=\"LexX\" uid=\"6340\"></user></juick><error type=\"wait\"><resource-constraint xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"></resource-constraint><text xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">Your contact offline message queue is full. The message has been discarded.</text></error></message>";
+ Stanza msg = server.parse(xmlMessage);
+ assertThat(from, equalTo(msg.getFrom()));
+ boolean isActive = jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, renhaId) == 1;
+ assertThat(isActive, equalTo(true));
+ bot.incomingMessage((ServerMessage)msg);
+ isActive = jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, renhaId) == 1;
+ assertThat(isActive, equalTo(false));
+ }
+ @Test
+ public void botCommandsTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
+ assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "PING").get(), is("PONG"));
+ // subscription commands have two lines, others have 1
+ assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(23));
+ }
+
+ @Test
+ public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException, JsonProcessingException {
+ int uid = userService.createUser("me", "secret");
+ User user = userService.getUserByUID(uid).orElse(new User());
+ Tag yo = tagService.getTag("yo", true);
+ int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo));
+ assertEquals("should be message", true,
+ bot.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid)).get().startsWith("@me"));
+ mid = messagesService.getUserBlog(user.getUid(), -1, 0).stream().reduce((first, second) -> second).get();
+ assertEquals("text should match", "yoyo",
+ messagesService.getMessage(mid).getText());
+ assertEquals("tag should match", "yo",
+ tagService.getMessageTags(mid).get(0).getTag().getName());
+ int readerUid = userService.createUser("dummyReader", "dummySecret");
+ User readerUser = userService.getUserByUID(readerUid).orElse(new User());
+ assertEquals("should be subscribed", "Subscribed",
+ bot.processCommand(readerUser, Jid.of("dummy@localhost"), "S #" + mid).get());
+ /* TODO: move from juick-legacy
+ assertEquals("should be favorited", "Message added to your recommendations",
+ juickProtocol.getReply(readerUser, "! #" + mid));
+ */
+ assertEquals("number of subscribed users should match", 1,
+ subscriptionService.getUsersSubscribedToComments(mid, uid).size());
+ /*
+ assertEquals("should be subscribed", "Subscribed",
+ bot.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName()).get());
+ List<User> friends = userService.getUserFriends(readerUid);
+ assertEquals("number of friend users should match", 2,
+ friends.size());
+ assertEquals("number of reader users should match", 1,
+ userService.getUserReaders(uid).size());
+ String expectedReply = "Reply posted.\n#" + mid + "/1 "
+ + "http://juick.com/" + mid + "#1";
+ String expectedSecondReply = "Reply posted.\n#" + mid + "/2 "
+ + "http://juick.com/" + mid + "#2";
+ assertEquals("should be reply", expectedReply,
+ bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo").get());
+ assertEquals("should be second reply", expectedSecondReply,
+ bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/1 yoyo").get());
+ Message reply = messagesService.getReplies(mid).stream().filter(m -> m.getRid() == 2).findFirst()
+ .orElse(new Message());
+ assertEquals("should be reply to first comment", 1, reply.getReplyto());
+ assertNotEquals("tags should NOT be updated", "Tags are updated",
+ bot.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there").get());
+ assertEquals("tags should be updated", "Tags are updated",
+ bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get());
+ assertEquals("number of tags should match", 2,
+ tagService.getMessageTags(mid).size());
+ assertEquals("should be blacklisted", "Tag added to your blacklist",
+ bot.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there").get());
+ assertEquals("number of subscribed users should match", 0,
+ subscriptionService.getSubscribedUsers(uid, mid).size());
+ assertEquals("tags should be updated", "Tags are updated",
+ bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get());
+ assertEquals("number of tags should match", 1,
+ tagService.getMessageTags(mid).size());
+ int taggerUid = userService.createUser("dummyTagger", "dummySecret");
+ User taggerUser = userService.getUserByUID(taggerUid).orElse(new User());
+ assertEquals("should be subscribed", "Subscribed",
+ bot.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo").get());
+ assertEquals("number of subscribed users should match", 2,
+ subscriptionService.getSubscribedUsers(uid, mid).size());
+ assertEquals("should be unsubscribed", "Unsubscribed from yo",
+ bot.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo").get());
+ assertEquals("number of subscribed users should match", 1,
+ subscriptionService.getSubscribedUsers(uid, mid).size());
+ assertEquals("number of readers should match", 1,
+ userService.getUserReaders(uid).size());
+ String readerFeed = bot.processCommand(readerUser, Jid.of("dummy@localhost"), "#").get();
+ assertEquals("description should match", true, readerFeed.startsWith("Your feed"));
+ assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(),
+ bot.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName()).get());
+ assertEquals("number of readers should match", 0,
+ userService.getUserReaders(uid).size());
+ assertEquals("number of friends should match", 1,
+ userService.getUserFriends(uid).size());
+ assertEquals("should be unsubscribed", "Unsubscribed from #" + mid,
+ bot.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid).get());
+ assertEquals("number of subscribed users should match", 0,
+ subscriptionService.getUsersSubscribedToComments(mid, uid).size());
+ assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid),
+ bot.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid).get());
+ assertEquals("should be deleted", String.format("Message %s deleted", mid),
+ bot.processCommand(user, Jid.of("test@localhost"), "D #" + mid).get());
+ assertEquals("should not have messages", 0, messagesService.getAll(user.getUid(), 0).size());
+ */
+ }
+}