From 42e2a727c2bc15ac15c9a3af66f126dbefe508a2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 30 Mar 2018 12:47:51 +0300 Subject: server: process attachments correctly --- .../java/com/juick/server/tests/ServerTests.java | 76 ++++++++++++++-------- 1 file changed, 50 insertions(+), 26 deletions(-) (limited to 'juick-server/src/test/java/com/juick/server/tests/ServerTests.java') 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 6c232167..92d05828 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 @@ -23,16 +23,18 @@ import com.juick.ExternalToken; import com.juick.Message; import com.juick.Tag; import com.juick.User; -import com.juick.server.EmailManager; import com.juick.server.CommandsManager; +import com.juick.server.EmailManager; import com.juick.server.XMPPConnection; import com.juick.server.XMPPServer; import com.juick.server.helpers.AnonymousUser; import com.juick.server.helpers.TagStats; +import com.juick.server.util.HttpUtils; import com.juick.service.*; import com.juick.util.DateFormattersHolder; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -49,6 +51,7 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; +import org.springframework.util.FileSystemUtils; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; @@ -57,7 +60,11 @@ import rocks.xmpp.core.stanza.model.Stanza; import rocks.xmpp.core.stanza.model.server.ServerMessage; import javax.inject.Inject; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Paths; import java.sql.Timestamp; import java.time.Instant; import java.util.*; @@ -116,6 +123,10 @@ public class ServerTests { private ImagesService imagesService; @Value("${hostname:localhost}") private Jid jid; + @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") + private String tmpDir; + @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") + private String imgDir; private static User ugnich, freefd, juick; static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword; @@ -125,7 +136,11 @@ public class ServerTests { private static boolean isSetUp = false; @Before - public void setUp() { + public void setUp() throws IOException { + Files.createDirectory(Paths.get(imgDir, "p")); + Files.createDirectory(Paths.get(imgDir, "photos-1024")); + Files.createDirectory(Paths.get(imgDir, "photos-512")); + Files.createDirectory(Paths.get(imgDir, "ps")); if (!isSetUp) { ugnichName = "ugnich"; ugnichPassword = "secret"; @@ -149,6 +164,13 @@ public class ServerTests { isSetUp = true; } } + @After + public void teardown() throws IOException { + FileSystemUtils.deleteRecursively(Paths.get(imgDir, "p")); + FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-1024")); + FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-512")); + FileSystemUtils.deleteRecursively(Paths.get(imgDir, "ps")); + } @Test public void getMyFeed() { int mid0 = messagesService.createMessage(ugnich.getUid(), "test", null, null); @@ -242,7 +264,7 @@ public class ServerTests { 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), + assertThat(messagesService.getDiscussions(ugnich.getUid(), 0L).get(0), equalTo(msg.getMid())); messagesService.deleteMessage(user_id, mid); messagesService.deleteMessage(user_id, mid2); @@ -518,21 +540,23 @@ public class ServerTests { } @Test public void botCommandsTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "PING").get(), is("PONG")); + assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "PING", null).get(), is("PONG")); // subscription commands have two lines, others have 1 - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(32)); + assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "help", null).get().split("\n").length, is(32)); } @Test - public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void protocolTests() throws Exception { int uid = userService.createUser("me", "secret"); User user = userService.getUserByUID(uid).orElse(new User()); Tag yo = tagService.getTag("yo", true); - router.incomingMessageJuick(user, Jid.of("test@localhost"), "*yo yoyo"); + Message msg = router.incomingMessageJuick(user, Jid.of("test@localhost"), "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")); + assertThat(msg.getAttachmentType(), is("png")); + Message msgreply = router.incomingMessageJuick(user, Jid.of("test@localhost"), "#" + msg.getMid() + " yyy", URI.create("juick://" + HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir))); + assertThat(messagesService.getReply(msg.getMid(), 1).getAttachmentType(), equalTo("png")); int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo)); assertEquals("should be message", true, - commandsManager.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(); + commandsManager.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid), null).get().startsWith("@me")); assertEquals("text should match", "yoyo", messagesService.getMessage(mid).getText()); assertEquals("tag should match", "yo", @@ -541,9 +565,9 @@ public class ServerTests { User readerUser = userService.getUserByUID(readerUid).orElse(new User()); Jid dummyJid = Jid.of("dummy@localhost"); assertEquals("should be subscribed", "Subscribed", - commandsManager.processCommand(readerUser, dummyJid, "S #" + mid).get()); + commandsManager.processCommand(readerUser, dummyJid, "S #" + mid, null).get()); assertEquals("should be favorited", "Message is added to your recommendations", - commandsManager.processCommand(readerUser, dummyJid, "! #" + mid).get()); + commandsManager.processCommand(readerUser, dummyJid, "! #" + mid, null).get()); int rid = messagesService.createReply(mid, 0, uid, "comment", null); assertEquals("number of subscribed users should match", 1, subscriptionService.getUsersSubscribedToComments( @@ -555,7 +579,7 @@ public class ServerTests { messagesService.getMessage(mid), messagesService.getReply(mid, rid)).size()); assertEquals("should be subscribed", "Subscribed to @" + user.getName(), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName()).get()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName(), null).get()); List friends = userService.getUserFriends(readerUid); assertEquals("number of friend users should match", 2, friends.size()); @@ -566,57 +590,57 @@ public class ServerTests { String expectedThirdReply = "Reply posted.\n#" + mid + "/3 " + "https://juick.com/" + mid + "#3"; assertEquals("should be second reply", expectedSecondReply, - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo").get()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo", null).get()); assertEquals("should be third reply", expectedThirdReply, - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/2 yoyo").get()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/2 yoyo", null).get()); Message reply = messagesService.getReplies(mid).stream().filter(m -> m.getRid() == 3).findFirst() .orElse(new Message()); assertEquals("should be reply to second comment", 2, reply.getReplyto()); assertEquals("tags should NOT be updated", "It is not your message", - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there").get()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there", null).get()); assertEquals("tags should be updated", "Tags are updated", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", null).get()); assertEquals("number of tags should match", 2, tagService.getMessageTags(mid).size()); assertEquals("should be blacklisted", "Tag added to your blacklist", - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there").get()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there", null).get()); assertEquals("number of subscribed users should match", 0, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("tags should be updated", "Tags are updated", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", null).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", - commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo").get()); + commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo", null).get()); assertEquals("number of subscribed users should match", 2, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("should be unsubscribed", "Unsubscribed from yo", - commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo").get()); + commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo", null).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 = commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#").get(); + String readerFeed = commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#", null).get(); assertEquals("description should match", true, readerFeed.startsWith("Your feed")); assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName()).get()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName(), null).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, - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid).get()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid, null).get()); assertEquals("number of subscribed users should match", 0, subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid), messagesService.getReply(mid, rid)).size()); assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid).get()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid, null).get()); assertEquals("should be deleted", "Message deleted", - commandsManager.processCommand(user, Jid.of("test@localhost"), "D #" + mid).get()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "D #" + mid, null).get()); assertEquals("should be not found", "Message not found", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid).get()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid, null).get()); } @Test public void mailParserTest() throws Exception { -- cgit v1.2.3