diff options
author | Vitaly Takmazov | 2018-05-18 15:33:38 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-07-24 14:35:02 +0300 |
commit | 8a0fbbd35c13054a947ea0d27ca117542bc452b9 (patch) | |
tree | 4266fbfaac3f1e268e780bc1cfa3cdc23ccd955c /juick-server/src/test/java | |
parent | 3fa18888d5ca53b59c778de3f829870614d05c27 (diff) |
JPA WIP
Diffstat (limited to 'juick-server/src/test/java')
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/ServerTests.java | 322 |
1 files changed, 159 insertions, 163 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 e4e5f298..b463765a 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 @@ -112,7 +112,7 @@ public class ServerTests { @Inject private MockMvc mockMvc; @Inject - private TestRestTemplate restTemplate; + private TestRestTemplate rest; @Inject private MessagesService messagesService; @Inject @@ -175,12 +175,12 @@ public class ServerTests { freefdPassword = "MyPassw0rd!"; juickName = "juick"; juickPassword = "demo"; - int ugnichId = userService.createUser(ugnichName, ugnichPassword); - ugnich = userService.getUserByUID(ugnichId).orElseThrow(IllegalStateException::new); - int freefdId = userService.createUser(freefdName, freefdPassword); - freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); - int juickId = userService.createUser(juickName, juickPassword); - juick = userService.getUserByUID(juickId).orElseThrow(IllegalStateException::new); + ugnich = userService.createUser(ugnichName, ugnichPassword); + assertThat(ugnich, not(AnonymousUser.INSTANCE)); + freefd = userService.createUser(freefdName, freefdPassword); + assertThat(freefd, not(AnonymousUser.INSTANCE)); + juick = userService.createUser(juickName, juickPassword); + assertThat(juick, not(AnonymousUser.INSTANCE)); isSetUp = true; } } @@ -193,130 +193,123 @@ public class ServerTests { } @Test public void getMyFeed() { - int mid0 = messagesService.createMessage(ugnich.getUid(), "test", null, null); - int mid2 = messagesService.createMessage(ugnich.getUid(), "test2", null, null); + Message msg0 = messagesService.createMessage(ugnich, "test", null, null); + Message msg2 = messagesService.createMessage(ugnich, "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)); + assertThat(freefdFeed.get(0), equalTo(msg2.getMid())); + User tonya = userService.createUser("Tonya", "secret"); + assertThat(tonya, not(AnonymousUser.INSTANCE)); + Message msg3 = messagesService.createMessage(tonya, "test3", null, null); + messagesService.recommendMessage(msg3.getMid(), ugnich.getUid()); + assertThat(messagesService.getMyFeed(freefd.getUid(), 0, false).get(0), equalTo(msg2.getMid())); + assertThat(messagesService.getMyFeed(freefd.getUid(), 0, true).get(0), equalTo(msg3.getMid())); + assertThat(messagesService.getMyFeed(freefd.getUid(), msg2.getMid(), true).get(0), equalTo(msg0.getMid())); + assertThat(messagesService.recommendMessage(msg0.getMid(), ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Added)); + assertThat(msg0.getLikes(), equalTo(1)); + assertThat(messagesService.recommendMessage(msg0.getMid(), ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Deleted)); + assertThat(msg0.getLikes(), equalTo(0)); + assertThat(messagesService.getAll(ugnich.getUid(), 0).get(0), equalTo(msg3.getMid())); 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); + Message msg = messagesService.createMessage(ugnich, "yo", null, Collections.singletonList(yoTag)); + List<User> subscribers = subscriptionService.getSubscribedUsers(ugnich.getUid(), msg.getMid()); telegramService.createTelegramUser(12345, "freefd"); String loginhash = jdbcTemplate.queryForObject("SELECT loginhash FROM telegram where tg_id=?", String.class, 12345); - crosspostService.setTelegramUser(loginhash, freefd.getUid()); + crosspostService.setTelegramUser(loginhash, freefd); 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); + List<User> subscribers2 = subscriptionService.getSubscribedUsers(ugnich.getUid(), msg.getMid()); 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)); + assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), msg.getMid()).size(), equalTo(1)); subscriptionService.unSubscribeUser(freefd, ugnich); - assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), mid).size(), equalTo(0)); + assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), msg.getMid()).size(), equalTo(0)); } @Test public void pmTests() { - pmQueriesService.createPM(freefd.getUid(), ugnich.getUid(), "hello"); - Message pm = pmQueriesService.getPMMessages(ugnich.getUid(), freefd.getUid()).get(0); + pmQueriesService.createPM(freefd, ugnich, "hello"); + Message pm = pmQueriesService.getPMMessages(ugnich, freefd).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(AnonymousUser.INSTANCE); - assertEquals("it should be me", "mmmme", user.getName()); - int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); - Message msg = messagesService.getMessage(mid); + User mmmme = userService.createUser("mmmme", "secret"); + assertEquals("it should be me", "mmmme", mmmme.getName()); + Message msg = messagesService.createMessage(mmmme, "yo", null, new ArrayList<>()); assertEquals("yo", msg.getText()); User me = msg.getUser(); assertEquals("mmmme", me.getName()); - assertEquals("mmmme", messagesService.getMessageAuthor(mid).getName()); + assertEquals("mmmme", messagesService.getMessageAuthor(msg.getMid()).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); + Message msg2 = messagesService.createMessage(mmmme, "yo2", null, tagList); 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(AnonymousUser.INSTANCE); - int rid = messagesService.createReply(msg2.getMid(), 0, ugnich, "bla-bla", null); + assertEquals("we already have ugnich", AnonymousUser.INSTANCE, userService.createUser("ugnich", "x")); + User hugnich = userService.createUser("hugnich", "x"); + int rid = messagesService.createReply(msg2.getMid(), 0, hugnich, "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(user, msg2.getMid()); + assertThat(reply.getTo().getName(), equalTo(mmmme.getName())); + List<Message> replies = messagesService.getReplies(mmmme, msg2.getMid()); assertThat(replies.size(), equalTo(1)); assertThat(replies.get(0), equalTo(reply)); int ridToReply = messagesService.createReply(msg2.getMid(), 1, ugnich, "blax2", null); Message reply2 = messagesService.getReply(msg2.getMid(), ridToReply); - assertThat(reply.getTo().getName(), equalTo(user.getName())); - List<Message> replies2 = messagesService.getReplies(user, msg2.getMid()); + assertThat(reply.getTo().getName(), equalTo(mmmme.getName())); + List<Message> replies2 = messagesService.getReplies(mmmme, 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")); + assertEquals(2, msg2.getReplies()); + assertEquals("weather", msg2.getTags().get(0).getName()); + assertEquals(hugnich, userService.checkPassword(hugnich.getName(), "x")); + assertEquals(AnonymousUser.INSTANCE, userService.checkPassword(hugnich.getName(), "xy")); - subscriptionService.subscribeMessage(msg, user); + subscriptionService.subscribeMessage(msg, mmmme); subscriptionService.subscribeMessage(msg, ugnich); int reply_id = messagesService.createReply(msg.getMid(), 0, ugnich, "comment", null); assertEquals(1, subscriptionService.getUsersSubscribedToComments(msg, messagesService.getReply(msg.getMid(), reply_id)).size()); assertThat(messagesService.getDiscussions(ugnich.getUid(), 0L).get(0), equalTo(msg.getMid())); - messagesService.deleteMessage(user_id, mid); - messagesService.deleteMessage(user_id, mid2); + messagesService.deleteMessage(mmmme.getUid(), msg.getMid()); + messagesService.deleteMessage(mmmme.getUid(), msg2.getMid()); 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); + Message msg4 = messagesService.createMessage(mmmme, "yoyoyo", null, null); assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4)); - messagesService.deleteMessage(user_id, mid4); + messagesService.deleteMessage(mmmme.getUid(), msg4.getMid()); } public ExpectedException exception = ExpectedException.none(); @Test public void likeTypeStatsTests(){ - int user_id = userService.createUser("dsdss", "secret"); + User dsdss = userService.createUser("dsdss", "secret"); final int freefdId = freefd.getUid(); - int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); - messagesService.likeMessage(mid, freefdId , 2); - messagesService.likeMessage(mid, freefdId,2); - messagesService.likeMessage(mid, freefdId,3); - messagesService.likeMessage(mid, freefdId,1); + Message msg4 = messagesService.createMessage(dsdss, "yo", null, new ArrayList<>()); + messagesService.likeMessage(msg4.getMid(), freefdId , 2); + messagesService.likeMessage(msg4.getMid(), freefdId,2); + messagesService.likeMessage(msg4.getMid(), freefdId,3); + messagesService.likeMessage(msg4.getMid(), freefdId,1); - Message msg4 = messagesService.getMessage(mid); assertThat(msg4.getLikes(), equalTo(1)); Assert.assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2) @@ -326,34 +319,34 @@ public class ServerTests { } @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)); + User hugnich2 = userService.createUser("hugnich2", "x"); + jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", hugnich2.getUid(), "firstjid@localhost", 1); + jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", hugnich2.getUid(), "secondjid@localhost", 1); + assertThat(userService.deleteJID(hugnich2.getUid(), "secondjid@localhost"), equalTo(true)); + assertThat(userService.deleteJID(hugnich2.getUid(), "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)); + User hugnich3 = userService.createUser("hugnich3", "x"); + jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", hugnich3.getUid(), "first@localhost"); + jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", hugnich3.getUid(), "second@localhost"); + assertThat(emailService.deleteEmail(hugnich3, "second@localhost"), equalTo(true)); + assertThat(emailService.deleteEmail(hugnich3, "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); + User hugnich4 = userService.createUser("hugnich4", "x"); + Message msg = messagesService.createMessage(hugnich4, "yo", null, null); Instant ts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?", - Timestamp.class, mid).toInstant(); + Timestamp.class, msg.getMid()).toInstant(); Thread.sleep(1000); - int rid = messagesService.createReply(mid, 0, ugnich, "people", null); + int rid = messagesService.createReply(msg.getMid(), 0, ugnich, "people", null); Instant rts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?", - Timestamp.class, mid).toInstant(); + Timestamp.class, msg.getMid()).toInstant(); assertThat(rts, greaterThan(ts)); - Message msg = messagesService.getReply(mid, rid); - assertThat(rts, equalTo(msg.getTimestamp())); - messagesService.deleteMessage(ugnich_id, mid); + Message reply = messagesService.getReply(msg.getMid(), rid); + assertThat(rts, equalTo(reply.getTimestamp())); + messagesService.deleteMessage(hugnich4.getUid(), msg.getMid()); } @Test @@ -399,7 +392,7 @@ public class ServerTests { @Test public void homeTestWithMessagesAndRememberMe() throws Exception { - String ugnichHash = userService.getHashByUID(ugnich.getUid()); + String ugnichHash = userService.getHashForUser(ugnich); mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword))) @@ -452,16 +445,16 @@ public class ServerTests { @Test public void messagesUrlTest() throws Exception { - int user_id = userService.createUser("dsds4345", "secret"); + User dsds4345 = userService.createUser("dsds4345", "secret"); - String freefdHash = userService.getHashByUID(freefd.getUid()); - System.out.println("user_id"+ user_id); - String userIdHash = userService.getHashByUID(user_id); + String freefdHash = userService.getHashForUser(freefd); + System.out.println("user_id"+ dsds4345.getUid()); + String userIdHash = userService.getHashForUser(dsds4345); final int freefdId = freefd.getUid(); - int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); - messagesService.likeMessage(mid, freefdId, 2 ); - messagesService.likeMessage(mid, freefdId, 2 ); - messagesService.likeMessage(mid, freefdId, 3 ); + Message msg = messagesService.createMessage(dsds4345, "yo", null, new ArrayList<>()); + messagesService.likeMessage(msg.getMid(), freefdId, 2 ); + messagesService.likeMessage(msg.getMid(), freefdId, 2 ); + messagesService.likeMessage(msg.getMid(), freefdId, 3 ); mockMvc.perform(get("/messages?"+ "hash=" + userIdHash)) .andDo(print()) @@ -482,8 +475,8 @@ public class ServerTests { public void tags() throws Exception { Tag weather = tagService.getTag("weather", true); Tag yo = tagService.getTag("yo", true); - messagesService.createMessage(ugnich.getUid(), "text", null, Arrays.asList(yo, weather)); - messagesService.createMessage(freefd.getUid(), "text2", null, Collections.singletonList(yo)); + messagesService.createMessage(ugnich, "text", null, Arrays.asList(yo, weather)); + messagesService.createMessage(freefd, "text2", null, Collections.singletonList(yo)); MvcResult result = mockMvc.perform(get("/tags")) .andExpect(status().isOk()) .andReturn(); @@ -515,7 +508,7 @@ public class ServerTests { } @Test public void performRequestsWithIssuedToken() throws Exception { - String ugnichHash = userService.getHashByUID(ugnich.getUid()); + String ugnichHash = userService.getHashForUser(ugnich); mockMvc.perform(get("/home")).andExpect(status().isUnauthorized()); mockMvc.perform(get("/auth")) .andExpect(status().isUnauthorized()); @@ -573,26 +566,26 @@ public class ServerTests { } @Test public void topTest() { - int topmid = messagesService.createMessage(ugnich.getUid(), "top message", null, null); + Message topmsg = messagesService.createMessage(ugnich, "top message", null, null); IntStream.rangeClosed(6, 12).forEach(i -> { User next = new User(); next.setUid(i); - messagesService.createReply(topmid, 0, next, "yo", null); + messagesService.createReply(topmsg.getMid(), 0, next, "yo", null); }); List<Integer> topCandidates = messagesService.getPopularCandidates(); assertThat(topCandidates.size(), is(1)); - assertThat(topCandidates.get(0), is(topmid)); + assertThat(topCandidates.get(0), is(topmsg.getMid())); Tag juickTag = tagService.getTag("juick", false); assertThat(juickTag.TID, is(2)); - tagService.updateTags(topmid, Collections.singletonList(juickTag)); + tagService.updateTags(topmsg.getMid(), Collections.singletonList(juickTag)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(true)); - tagService.updateTags(topmid, Collections.singletonList(juickTag)); + tagService.updateTags(topmsg.getMid(), Collections.singletonList(juickTag)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(false)); jdbcTemplate.update("INSERT INTO tags(tag_id, name) VALUES(805, 'NSFW')"); Tag nsfw = tagService.getTag("NSFW", false); assertThat(nsfw.TID, equalTo(805)); - tagService.updateTags(topmid, Collections.singletonList(nsfw)); + tagService.updateTags(topmsg.getMid(), Collections.singletonList(nsfw)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(true)); } @Test @@ -619,10 +612,10 @@ public class ServerTests { } @Test public void botIsUpAndProcessingResourceConstraints() throws Exception { - int renhaId = userService.createUser("renha", "umnnbt"); + User renha = 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); + renha.getUid(), from.toEscapedString(), 1); rocks.xmpp.core.stanza.model.Message xmppMessage = new rocks.xmpp.core.stanza.model.Message(); xmppMessage.setType(rocks.xmpp.core.stanza.model.Message.Type.ERROR); xmppMessage.setFrom(from); @@ -630,16 +623,16 @@ public class ServerTests { StanzaError err = new StanzaError(StanzaError.Type.CANCEL, Condition.RESOURCE_CONSTRAINT); xmppMessage.setError(err); Function<Integer, Boolean> isActive = f -> jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, f) == 1; - assertThat(isActive.apply(renhaId), equalTo(true)); + assertThat(isActive.apply(renha.getUid()), equalTo(true)); ClientMessage result = router.incomingMessage(xmppMessage); assertNull(result); - assertThat(isActive.apply(renhaId), equalTo(false)); + assertThat(isActive.apply(renha.getUid()), equalTo(false)); xmppMessage.setError(null); xmppMessage.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT); xmppMessage.setBody("On"); result = router.incomingMessage(xmppMessage); assertThat(result.getBody(), equalTo("XMPP notifications are activated")); - assertTrue(isActive.apply(renhaId)); + assertTrue(isActive.apply(renha.getUid())); xmppMessage.setBody("*test test"); result = router.incomingMessage(xmppMessage); assertThat(result.getBody(), startsWith("New message posted")); @@ -647,7 +640,7 @@ public class ServerTests { xmppMessage.setBody("PING"); result = router.incomingMessage(xmppMessage); assertThat(result.getBody(), equalTo("PONG")); - int secretlySadId = userService.createUser("secretlysad", "bbk"); + User secretlySad = userService.createUser("secretlysad", "bbk"); xmppMessage.setTo(botJid.withLocal("secretlysad")); xmppMessage.setBody("What's up?"); result = router.incomingMessage(xmppMessage); @@ -672,8 +665,7 @@ public class ServerTests { @Test public void protocolTests() throws Exception { - int uid = userService.createUser("me", "secret"); - User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE); + User user = userService.createUser("me", "secret"); Tag yo = tagService.getTag("yo", true); Message msg = commandsManager.processCommand(user, "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getNewMessage().get(); assertThat(msg.getAttachmentType(), is("png")); @@ -691,8 +683,7 @@ public class ServerTests { assertThat(last.toInstant(), equalTo(yoyoMsg.getNewMessage().get().getTimestamp())); assertEquals("should be message", true, commandsManager.processCommand(user, String.format("#%d", mid), emptyUri).getText().startsWith("@me")); - int readerUid = userService.createUser("dummyReader", "dummySecret"); - User readerUser = userService.getUserByUID(readerUid).orElse(AnonymousUser.INSTANCE); + User readerUser = userService.createUser("dummyReader", "dummySecret"); assertThat(commandsManager.processCommand(readerUser, "s", emptyUri).getText().startsWith("You are subscribed to"), is(true)); assertThat(commandsManager.processCommand(readerUser, "S", emptyUri).getText().startsWith("You are subscribed to"), is(true)); assertEquals("should be subscribed", "Subscribed", @@ -716,11 +707,11 @@ public class ServerTests { assertEquals("should be subscribed", "Subscribed to @" + user.getName(), commandsManager.processCommand(readerUser, "S @" + user.getName(), emptyUri) .getText()); - List<User> friends = userService.getUserFriends(readerUid); + List<User> friends = userService.getUserFriends(readerUser.getUid()); assertEquals("number of friend users should match", 2, friends.size()); assertEquals("number of reader users should match", 1, - userService.getUserReaders(uid).size()); + userService.getUserReaders(user.getUid()).size()); String expectedSecondReply = "Reply posted.\n#" + mid + "/2 " + "https://juick.com/m/" + mid + "#2"; String expectedThirdReply = "Reply posted.\n#" + mid + "/3 " @@ -746,32 +737,31 @@ public class ServerTests { assertEquals("should be blacklisted", "Tag added to your blacklist", commandsManager.processCommand(readerUser, "BL *there", emptyUri).getText()); assertEquals("number of subscribed users should match", 0, - subscriptionService.getSubscribedUsers(uid, mid).size()); + subscriptionService.getSubscribedUsers(user.getUid(), mid).size()); assertEquals("tags should be updated", "Tags are updated", commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText()); assertEquals("number of tags should match", 1, tagService.getMessageTags(mid).size()); - int taggerUid = userService.createUser("dummyTagger", "dummySecret"); - User taggerUser = userService.getUserByUID(taggerUid).orElse(AnonymousUser.INSTANCE); + User taggerUser = userService.createUser("dummyTagger", "dummySecret"); assertEquals("should be subscribed", "Subscribed", commandsManager.processCommand(taggerUser, "S *yo", emptyUri).getText()); assertEquals("number of subscribed users should match", 2, - subscriptionService.getSubscribedUsers(uid, mid).size()); + subscriptionService.getSubscribedUsers(user.getUid(), mid).size()); assertEquals("should be unsubscribed", "Unsubscribed from yo", commandsManager.processCommand(taggerUser, "U *yo", emptyUri).getText()); assertEquals("number of subscribed users should match", 1, - subscriptionService.getSubscribedUsers(uid, mid).size()); + subscriptionService.getSubscribedUsers(user.getUid(), mid).size()); assertEquals("number of readers should match", 1, - userService.getUserReaders(uid).size()); + userService.getUserReaders(user.getUid()).size()); String readerFeed = commandsManager.processCommand(readerUser, "#", emptyUri).getText(); assertTrue("description should match", readerFeed.startsWith("Your feed")); assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(), commandsManager.processCommand(readerUser, "U @" + user.getName(), emptyUri) .getText()); assertEquals("number of readers should match", 0, - userService.getUserReaders(uid).size()); + userService.getUserReaders(user.getUid()).size()); assertEquals("number of friends should match", 1, - userService.getUserFriends(uid).size()); + userService.getUserFriends(user.getUid()).size()); assertEquals("should be unsubscribed", "Unsubscribed from #" + mid, commandsManager.processCommand(readerUser, "u #" + mid, emptyUri).getText()); assertEquals("number of subscribed users should match", 0, @@ -867,83 +857,82 @@ public class ServerTests { @Test public void recommendTests() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "to be liked", null, null); - String freefdHash = userService.getHashByUID(freefd.getUid()); - int freefdMid = messagesService.createMessage(freefd.getUid(), "to be not liked", null, null); + Message msg = messagesService.createMessage(ugnich, "to be liked", null, null); + String freefdHash = userService.getHashForUser(freefd); + Message freefdMsg = messagesService.createMessage(freefd, "to be not liked", null, null); - mockMvc.perform(post("/like?mid=" + mid + "&hash=" + freefdHash)) + mockMvc.perform(post("/like?mid=" + msg.getMid() + "&hash=" + freefdHash)) .andExpect(status().isOk()) .andExpect(jsonPath("$.status", is("Message is added to your recommendations"))); - mockMvc.perform(get("/thread?mid=" + mid + "&hash=" + freefdHash)) + mockMvc.perform(get("/thread?mid=" + msg.getMid() + "&hash=" + freefdHash)) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].recommendations.length()", is(1))) .andExpect(jsonPath("$[0].recommendations[0]", is(freefdName))); - mockMvc.perform(post("/like?mid=" + freefdMid + "&hash=" + freefdHash)) + mockMvc.perform(post("/like?mid=" + freefdMsg.getMid() + "&hash=" + freefdHash)) .andExpect(status().isForbidden()); } @Test public void likesTests() throws Exception{ - int user_id = userService.createUser("dsds", "secret"); - String freefdHash = userService.getHashByUID(freefd.getUid()); - int mid1 = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); + User dsds = userService.createUser("dsds", "secret"); + String freefdHash = userService.getHashForUser(freefd); + Message msg1 = messagesService.createMessage(dsds, "yo", null, new ArrayList<>()); - mockMvc.perform(post("/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=2")) + mockMvc.perform(post("/react?mid=" + msg1.getMid() + "&hash=" + freefdHash+ "&reactionId=2")) .andExpect(status().isOk()); - Message msg4 = messagesService.getMessage(mid1); - assertThat(msg4.getLikes(), is(0)); - assertThat(messagesService.getMessages(Collections.singletonList(mid1)).get(0).getLikes(), is(0)); - Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2) + assertThat(msg1.getLikes(), is(0)); + assertThat(messagesService.getMessages(Collections.singletonList(msg1.getMid())).get(0).getLikes(), is(0)); + Assert.assertEquals(1, msg1.getReactions().stream().filter(r -> r.getId() == 2) .findFirst().orElseThrow(IllegalStateException::new).getCount()); - mockMvc.perform(post("/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=1")) + mockMvc.perform(post("/react?mid=" + msg1.getMid() + "&hash=" + freefdHash+ "&reactionId=1")) .andExpect(status().isOk()); - mockMvc.perform(post("/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=1")) + mockMvc.perform(post("/react?mid=" + msg1.getMid() + "&hash=" + freefdHash+ "&reactionId=1")) .andExpect(status().isOk()); - assertThat(messagesService.getMessage(mid1).getLikes(), is(1)); + assertThat(msg1.getLikes(), is(1)); } @Test public void lastReadTests() throws Exception { 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); - messagesService.createReply(mid, 0, freefd, "new reply", null); + Message msg = messagesService.createMessage(ugnich, "to be watched", null, null); + subscriptionService.subscribeMessage(msg, ugnich); + messagesService.createReply(msg.getMid(), 0, freefd, "new reply", null); BiFunction<User, Integer, Integer> lastRead = (user, m) -> jdbcTemplate.queryForObject( "SELECT last_read_rid FROM subscr_messages WHERE suser_id=? AND message_id=?", Integer.class, user.getUid(), m); - assertThat(lastRead.apply(ugnich, mid), is(0)); + assertThat(lastRead.apply(ugnich, msg.getMid()), is(0)); assertThat(messagesService.getUnread(ugnich).size(), is(1)); - assertThat(messagesService.getUnread(ugnich).get(0), is(mid)); - messagesService.getReplies(ugnich, mid); - assertThat(lastRead.apply(ugnich, mid), is(1)); + assertThat(messagesService.getUnread(ugnich).get(0), is(msg.getMid())); + messagesService.getReplies(ugnich, msg.getMid()); + assertThat(lastRead.apply(ugnich, msg.getMid()), is(1)); assertThat(messagesService.getUnread(ugnich).size(), is(0)); - messagesService.setLastReadComment(ugnich, mid, 0); - assertThat(lastRead.apply(ugnich, mid), is(1)); - String ugnichHash = userService.getHashByUID(ugnich.getUid()); - int freefdrid = messagesService.createReply(mid, 0, freefd, "again", null); - mockMvc.perform(get(String.format("/thread/mark_read/%d-%d.gif?hash=%s", mid, freefdrid, ugnichHash))) + messagesService.setLastReadComment(ugnich, msg.getMid(), 0); + assertThat(lastRead.apply(ugnich, msg.getMid()), is(1)); + String ugnichHash = userService.getHashForUser(ugnich); + int freefdrid = messagesService.createReply(msg.getMid(), 0, freefd, "again", null); + mockMvc.perform(get(String.format("/thread/mark_read/%d-%d.gif?hash=%s", msg.getMid(), freefdrid, ugnichHash))) .andExpect(status().isOk()) .andExpect(content().bytes(IOUtils.toByteArray( Objects.requireNonNull(getClass().getClassLoader().getResource("Transparent.gif"))))); - assertThat(lastRead.apply(ugnich, mid), is(freefdrid)); + assertThat(lastRead.apply(ugnich, msg.getMid()), is(freefdrid)); privacyQueriesService.blacklistUser(ugnich, freefd); - int newfreefdrid = messagesService.createReply(mid, 0, freefd, "from ban", null); - serverManager.processMessageEvent(new MessageEvent(this, messagesService.getReply(mid, newfreefdrid), + int newfreefdrid = messagesService.createReply(msg.getMid(), 0, freefd, "from ban", null); + serverManager.processMessageEvent(new MessageEvent(this, messagesService.getReply(msg.getMid(), newfreefdrid), Collections.emptyList())); - assertThat(userService.isReplyToBL(ugnich, messagesService.getReply(mid, newfreefdrid)), is(true)); - assertThat(lastRead.apply(ugnich, mid), is(newfreefdrid)); + assertThat(userService.isReplyToBL(ugnich, messagesService.getReply(msg.getMid(), newfreefdrid)), is(true)); + assertThat(lastRead.apply(ugnich, msg.getMid()), is(newfreefdrid)); privacyQueriesService.blacklistUser(ugnich, freefd); - newfreefdrid = messagesService.createReply(mid, 0, freefd, "after ban", null); - assertThat(lastRead.apply(ugnich, mid), lessThan(newfreefdrid)); - mockMvc.perform(get(String.format("/thread?mid=%d&hash=%s", mid, ugnichHash))) + newfreefdrid = messagesService.createReply(msg.getMid(), 0, freefd, "after ban", null); + assertThat(lastRead.apply(ugnich, msg.getMid()), lessThan(newfreefdrid)); + mockMvc.perform(get(String.format("/thread?mid=%d&hash=%s", msg.getMid(), ugnichHash))) .andExpect(status().isOk()); - assertThat(lastRead.apply(ugnich, mid), is(newfreefdrid)); + assertThat(lastRead.apply(ugnich, msg.getMid()), is(newfreefdrid)); } @Test public void feedsShouldNotContainMessagesWithBannedTags() { Tag banned = tagService.getTag("banned", true); - int mid = messagesService.createMessage(ugnich.getUid(), "yo", "jpg", + Message msg = messagesService.createMessage(ugnich, "yo", "jpg", Collections.singletonList(banned)); privacyQueriesService.blacklistTag(freefd, banned); assertTrue(messagesService.getMessages(messagesService.getAll(freefd.getUid(), 0)) @@ -954,7 +943,7 @@ public class ServerTests { .stream().noneMatch(m -> m.getTags().contains(banned))); assertFalse(messagesService.getMessages(messagesService.getPhotos(ugnich.getUid(), 0)) .stream().noneMatch(m -> m.getTags().contains(banned))); - jdbcTemplate.update("UPDATE messages SET popular=1 WHERE message_id=?", mid); + jdbcTemplate.update("UPDATE messages SET popular=1 WHERE message_id=?", msg.getMid()); assertTrue(messagesService.getMessages(messagesService.getPopular(freefd.getUid(), 0)) .stream().noneMatch(m -> m.getTags().contains(banned))); assertFalse(messagesService.getMessages(messagesService.getPopular(ugnich.getUid(), 0)) @@ -1024,8 +1013,8 @@ public class ServerTests { HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); map.add("body", "yo"); - map.add("hash", userService.getHashByUID(ugnich.getUid())); - ResponseEntity<CommandResult> result = restTemplate.postForEntity( + map.add("hash", userService.getHashForUser(ugnich)); + ResponseEntity<CommandResult> result = rest.postForEntity( "/post", request, CommandResult.class); assertThat(result.getStatusCode(), is(HttpStatus.OK)); @@ -1051,7 +1040,7 @@ public class ServerTests { commandsManager.processCommand(freefd, "S @ugnich", emptyUri); commandsManager.processCommand(ugnich, "S @freefd", emptyUri); assertThat(userService.getUserReaders(ugnich.getUid()).size(), is(1)); - String hash = userService.getHashByUID(ugnich.getUid()); + String hash = userService.getHashForUser(ugnich); mockMvc.perform(get("/me") .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(jsonPath("$.hash", is(hash))) @@ -1111,4 +1100,11 @@ public class ServerTests { assertThat(replyJpgCmyk.getNewMessage().isPresent(), is(true)); assertThat(replyJpgCmyk.getNewMessage().get().getAttachmentType(), is("jpg")); } + @Test + public void emailTest() { + User tezzt = userService.createUser("tezzt", "12346"); + jdbcTemplate.update("INSERT INTO emails(user_id, email) VALUES(?, 'yo@example.com')", tezzt.getUid()); + User user = userService.getUserByEmail("yo@example.com"); + assertThat(user, is(tezzt)); + } } |