diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/juick/MessageTest.java | 16 | ||||
-rw-r--r-- | src/test/java/com/juick/server/MockDeleteListener.java | 2 | ||||
-rw-r--r-- | src/test/java/com/juick/server/MockNotificationListener.java | 2 | ||||
-rw-r--r-- | src/test/java/com/juick/server/MockUpdateListener.java | 2 | ||||
-rw-r--r-- | src/test/java/com/juick/server/tests/ServerTests.java | 184 | ||||
-rw-r--r-- | src/test/resources/application-sqlite.yml | 3 | ||||
-rw-r--r-- | src/test/resources/sample1.dng | bin | 0 -> 6372698 bytes |
7 files changed, 124 insertions, 85 deletions
diff --git a/src/test/java/com/juick/MessageTest.java b/src/test/java/com/juick/MessageTest.java index 14d6175a..e750f652 100644 --- a/src/test/java/com/juick/MessageTest.java +++ b/src/test/java/com/juick/MessageTest.java @@ -213,8 +213,8 @@ public class MessageTest { assertThat(MessageUtils.formatMessage(msg), is("<blockquote>quote</blockquote>message")); String brokenComment = "<!-- read next"; assertThat(MessageUtils.formatMessage(brokenComment), is("<!-- read next")); - String url = "[ya](http://ya.ru)"; - assertThat(MessageUtils.formatMessage(url), is("<a href=\"http://ya.ru\" rel=\"nofollow\">ya</a>")); + String url = "test [ya](https://juick.com/a/1)? hh"; + assertThat(MessageUtils.formatMessage(url), is("test <a href=\"https://juick.com/a/1\" rel=\"nofollow\">ya</a>? hh")); String complexMessage = "У футболистов нет мозгов. Что в России, что в Беларуси:\n" + "\n" + ">Отец футболиста Лухвича, объехавшего пробку по тротуару: «Сына задержали, Infiniti арестовали» https://auto.onliner.by/2019/01/23/probka-9\n" + @@ -238,4 +238,16 @@ public class MessageTest { testMessage.setTags(MessageUtils.parseTags("NSFW test")); assertThat(MessageUtils.isSensitive(testMessage), is(true)); } + @Test + public void stripHashesTest() { + var safeMessage = "And that is a [odd](https://juick.com/a/1)? aaapaa"; + var nonsafeMessage = "[Here is my link](https://juick.com?hash=12345)"; + var filteredMessage = "[Here is my link](https://juick.com)"; + var pidginMessage = "I'm very smart to post my login url there<https://juick.com/settings?hash=VTYZkKV8FWkmu6g1>"; + var westernMessage = "«Please, verify your account at https://juick.com/settings?hash=12345»"; + assertThat(MessageUtils.stripNonSafeUrls(safeMessage), is(safeMessage)); + assertThat(MessageUtils.stripNonSafeUrls(nonsafeMessage), is(filteredMessage)); + assertThat(MessageUtils.stripNonSafeUrls(pidginMessage), not(containsString("VTYZkKV8FWkmu6g1"))); + assertThat(MessageUtils.stripNonSafeUrls(westernMessage), containsString("»")); + } } diff --git a/src/test/java/com/juick/server/MockDeleteListener.java b/src/test/java/com/juick/server/MockDeleteListener.java index d060d865..20ee99bf 100644 --- a/src/test/java/com/juick/server/MockDeleteListener.java +++ b/src/test/java/com/juick/server/MockDeleteListener.java @@ -20,7 +20,7 @@ package com.juick.server; import com.juick.service.activities.DeleteUserEvent; import org.springframework.context.ApplicationListener; -import javax.annotation.Nonnull; +import jakarta.annotation.Nonnull; public class MockDeleteListener implements ApplicationListener<DeleteUserEvent> { @Override diff --git a/src/test/java/com/juick/server/MockNotificationListener.java b/src/test/java/com/juick/server/MockNotificationListener.java index fd91c3fd..ee9f1696 100644 --- a/src/test/java/com/juick/server/MockNotificationListener.java +++ b/src/test/java/com/juick/server/MockNotificationListener.java @@ -20,7 +20,7 @@ package com.juick.server; import com.juick.service.component.SystemEvent; import org.springframework.context.ApplicationListener; -import javax.annotation.Nonnull; +import jakarta.annotation.Nonnull; public class MockNotificationListener implements ApplicationListener<SystemEvent> { @Override diff --git a/src/test/java/com/juick/server/MockUpdateListener.java b/src/test/java/com/juick/server/MockUpdateListener.java index 08742b12..21c9b673 100644 --- a/src/test/java/com/juick/server/MockUpdateListener.java +++ b/src/test/java/com/juick/server/MockUpdateListener.java @@ -20,7 +20,7 @@ package com.juick.server; import com.juick.service.activities.UpdateEvent; import org.springframework.context.ApplicationListener; -import javax.annotation.Nonnull; +import jakarta.annotation.Nonnull; public class MockUpdateListener implements ApplicationListener<UpdateEvent> { @Override diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index d61725e6..84034428 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2019, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -30,6 +30,10 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.github.scribejava.apis.AppleClientSecretGenerator; import com.jayway.jsonpath.JsonPath; import com.juick.*; +import com.juick.data.MessagesRepository; +import com.juick.data.UsersRepository; +import com.juick.data.entities.MessageEntity; +import com.juick.data.entities.ReplyEntity; import com.juick.model.Tag; import com.juick.model.*; import com.juick.server.MockNotificationListener; @@ -38,7 +42,10 @@ import com.juick.service.*; import com.juick.service.activities.UpdateEvent; import com.juick.service.component.SystemEvent; import com.juick.test.util.MockUtils; -import com.juick.util.*; +import com.juick.util.DateFormattersHolder; +import com.juick.util.HttpUtils; +import com.juick.util.MessageUtils; +import com.juick.util.WebUtils; import com.juick.util.formatters.PlainTextFormatter; import com.juick.www.WebApp; import com.juick.www.ad.models.Site; @@ -111,7 +118,7 @@ import rocks.xmpp.core.session.XmppSession; import rocks.xmpp.core.session.XmppSessionConfiguration; import ru.sape.SapePageLinks; -import javax.inject.Inject; +import jakarta.inject.Inject; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -119,9 +126,6 @@ import java.io.*; import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.*; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.spec.InvalidKeySpecException; import java.time.Instant; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; @@ -196,6 +200,10 @@ public class ServerTests { @Inject private ProfileUriBuilder profileUriBuilder; @Inject + private MessagesRepository messagesRepository; + @Inject + private UsersRepository usersRepository; + @Inject private WebApp webApp; @Value("${ap_base_uri:http://localhost:8080/}") private String baseUri; @@ -278,6 +286,12 @@ public class ServerTests { .orElseThrow(IllegalStateException::new); freefd = userService.createUser(freefdName, freefdPassword) .orElseThrow(IllegalStateException::new); + ugnich = makeUserVerified(ugnich); + freefd = makeUserVerified(freefd); + assertThat(ugnich.getLanguage(), is("__")); + ugnich.setLanguage("en"); + userService.updateLanguage(ugnich); + assertThat(ugnich.getLanguage(), is("en")); webClient.getOptions().setJavaScriptEnabled(false); webClient.getOptions().setCssEnabled(false); isSetUp = true; @@ -660,8 +674,9 @@ public class ServerTests { new TypeReference<>() { }); assertThat(users.size(), is(1)); - assertThat(users.get(0).getTokens().size(), is(1)); - assertThat(users.get(0).getTokens().get(0).token(), equalTo(token)); + // ugnich has durov and apns token + assertThat(users.get(0).getTokens().size(), is(2)); + assertThat(users.get(0).getTokens().stream().filter(t -> t.type().equals("apns")).findFirst().get().token(), equalTo(token)); } @Test @@ -775,6 +790,7 @@ public class ServerTests { public void protocolTests() throws Exception { String tmpDir = storageService.getTemporaryDirectory(); User user = userService.createUser("me", "secret").orElseThrow(IllegalStateException::new); + user = makeUserVerified(user); Tag yo = tagService.getTag("yo", true); Message msg = commandsManager .processCommand(user, "*yo yoyo", @@ -846,6 +862,7 @@ public class ServerTests { last = list.get(0); assertThat(last, equalTo(reply.getCreated())); assertEquals(2, reply.getReplyto()); + readerUser = makeUserVerified(readerUser); assertThat(commandsManager.processCommand(readerUser, "#" + mid + " *yo *there", emptyUri).getText(), startsWith("Reply posted")); assertEquals("Tags are updated", @@ -900,12 +917,6 @@ public class ServerTests { assertThat(result.getNewMessage(), is(Optional.empty())); assertThat(result.getText(), is("Tags are NOT updated (5 tags maximum?)")); result = commandsManager.processCommand(user, - "I'm very smart to post my login url there" - + "<https://juick.com/settings?hash=VTYZkKV8FWkmu6g1>", - emptyUri); - assertThat(result.getNewMessage().isPresent(), is(true)); - assertFalse(result.getNewMessage().get().getText().contains("VTYZkKV8FWkmu6g1")); - result = commandsManager.processCommand(user, "*корм *juick_ppl *рационализм *? *мюсли а сколько микроморт в дневной порции сверхмюслей?", emptyUri); assertThat(result.getNewMessage().isPresent(), is(true)); @@ -1230,8 +1241,9 @@ public class ServerTests { CommandResult result = commandsManager.processCommand(ugnich, "freefd - dick", emptyUri); int mid = result.getNewMessage().get().getMid(); commandsManager.processCommand(freefd, String.format("#%d ugnich - dick too", mid), emptyUri); - commandsManager.processCommand(serviceUser, String.format("#%d/1 ban for a hour!", mid), emptyUri); - commandsManager.processCommand(serviceUser, + var juick = makeUserVerified(serviceUser); + commandsManager.processCommand(juick, String.format("#%d/1 ban for a hour!", mid), emptyUri); + commandsManager.processCommand(juick, String.format("#%d freefd is here but it is hidden from you", mid), emptyUri); assertThat(messagesService.getMessage(mid).get().getReplies(), is(3)); @@ -1246,7 +1258,7 @@ public class ServerTests { mockMvc.perform(get("/api/thread").with(httpBasic(ugnichName, ugnichPassword)).param("mid", String.valueOf(mid))) .andExpect(jsonPath("$[0].replies", is(1))); - commandsManager.processCommand(serviceUser, String.format("#%d/4 mmm?!", mid), emptyUri); + commandsManager.processCommand(juick, String.format("#%d/4 mmm?!", mid), emptyUri); assertThat(messagesService.getMessage(mid).get().getReplies(), is(5)); replies = messagesService.getReplies(ugnich, mid); reply = messagesService.getReply(mid, 5); @@ -1264,8 +1276,8 @@ public class ServerTests { int freefdMsg = messagesService.createMessage(freefd.getUid(), "sux", null, Set.of(tag)); assertThat(messagesService.getTag(tag.getId(), freefd.getUid(), 0, 10).size(), is(1)); assertThat(messagesService.getTag(tag.getId(), ugnich.getUid(), 0, 10).size(), is(0)); - messagesService.recommendMessage(freefdMsg, serviceUser.getUid()); - assertThat(messagesService.getUserBlogWithRecommendations(serviceUser, ugnich, 0, 0) + messagesService.recommendMessage(freefdMsg, juick.getUid()); + assertThat(messagesService.getUserBlogWithRecommendations(juick, ugnich, 0, 0) .contains(freefdMsg), is(false)); commandsManager.processCommand(ugnich, "BL @freefd", emptyUri); @@ -1516,8 +1528,8 @@ public class ServerTests { jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); String jsonUser = jsonMapper.writeValueAsString(yyy); Map<String, Object> user = JsonPath.read(jsonUser, "$"); - // only uid, name and uri - assertThat(user.keySet().size(), is(3)); + // only uid, name, lang and uri + assertThat(user.keySet().size(), is(4)); JAXBContext context = JAXBContext.newInstance(User.class); Marshaller m = context.createMarshaller(); @@ -1591,14 +1603,14 @@ public class ServerTests { assertThat(CollectionUtils.isEqualCollection( messagesService.getMessagesRecommendations(monstreek.getUid(), Collections.singletonList(mid)).stream() .map(Pair::getRight).map(User::getName).toList(), - Arrays.asList("ermine", "pogo", "test@example.com")), is(true)); + Arrays.asList("ermine", "pogo", "test")), is(true)); privacyQueriesService.blacklistUser(userService.getUserByName("monstreek"), userService.getUserByName("pogo")); assertThat(messagesService.getMessage(mid).get().getRecommendations().size(), is(3)); assertThat(CollectionUtils.isEqualCollection( messagesService.getMessagesRecommendations(monstreek.getUid(), Collections.singletonList(mid)).stream() .map(Pair::getRight).map(User::getName).toList(), - Arrays.asList("ermine", "test@example.com")), is(true)); + Arrays.asList("ermine", "test")), is(true)); jdbcTemplate.execute("DELETE FROM favorites"); } @@ -1629,19 +1641,6 @@ public class ServerTests { } @Test - public void accountUrlShouldBeExposedOverWebfinger() throws Exception { - mockMvc.perform(get("/.well-known/webfinger?resource=acct:ugnich@" + webDomain) - .accept("application/jrd+json")).andExpect(status().isOk()) - .andExpect(jsonPath("$.subject", is("acct:ugnich@" + webDomain))) - .andExpect(jsonPath("$.links", hasSize(1))) - .andExpect(jsonPath("$.links[0].href", is(baseUri + "u/ugnich"))); - mockMvc.perform(get("/.well-known/webfinger?resource=acct:durov@" + webDomain)) - .andExpect(status().isNotFound()); - mockMvc.perform(get("/.well-known/webfinger?resource=acct:@" + webDomain)) - .andExpect(status().isNotFound()); - } - - @Test public void userProfileAndBlogShouldBeExposedAsActivityStream() throws Exception { ClassPathResource defaultAvatar = new ClassPathResource("static/av-96.png"); String hash = DigestUtils.md5DigestAsHex(IOUtils.toByteArray(defaultAvatar.getInputStream())); @@ -2078,14 +2077,6 @@ public class ServerTests { } @Test - public void hostmeta() throws Exception { - MvcResult result = mockMvc.perform(get("/.well-known/host-meta")).andExpect(status().isOk()) - .andReturn(); - String xrd = result.getResponse().getContentAsString(); - result = mockMvc.perform(get("/.well-known/x-nodeinfo2")).andExpect(status().isOk()).andReturn(); - } - - @Test public void pms() throws Exception { jdbcTemplate.execute("DELETE FROM pm"); jdbcTemplate.execute("DELETE FROM bl_users"); @@ -2110,12 +2101,14 @@ public class ServerTests { } @Test + @Disabled("FIXME: rewrite signup flow") public void signupTest() throws Exception { emailService.addVerificationCode(null, "demo@email.com", "123456"); - MvcResult result = mockMvc.perform(post("/api/signup").param("username", "testuser") + MvcResult result = mockMvc.perform(post("/signup").param("username", "testuser") + .param("type", "email") .param("password", "demopassword").param("verificationCode", "123456")) - .andExpect(status().isOk()) - .andReturn(); + .andExpect(status().isOk()) + .andReturn(); User testuser = jsonMapper.readValue(result.getResponse().getContentAsString(), User.class); assertThat(testuser.getName(), is("testuser")); } @@ -2167,19 +2160,30 @@ public class ServerTests { assertThat(top.size(), is(1)); } + private User makeUserVerified(User user) { + var id = System.currentTimeMillis(); + telegramService.createTelegramUser(id, user.getName()); + var hash = userService.getSignUpHashByTelegramID(id, user.getName()); + userService.setTelegramUser(hash, user.getUid()); + return userService.getUserByName(user.getName()); + } + @Test public void verifiedUsersTest() { - assertThat(userService.getUserByName("ugnich").isVerified(), is(false)); - jdbcTemplate.update("INSERT INTO telegram(user_id, tg_id, tg_name) VALUES(?, ?, ?)", ugnich.getUid(), - 100001866137681L, "tg_test"); - assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false)); + var user = userService.createUser("new_unverified_user", "evil").orElseThrow(); + assertThat(userService.getUserByName(user.getName()).isVerified(), is(false)); + user = makeUserVerified(user); + assertThat(userService.getUserByName(user.getName()).isVerified(), is(true)); + assertThat(userService.canDeleteTelegramUser(userService.getUserByName(user.getName())), is(false)); userService.addFacebookState("12345", "http://localhost"); - userService.createFacebookUser(12345, "12345", "5678", "ugnich"); - userService.setFacebookUser("12345", ugnich.getUid()); - assertThat(userService.getUserByName("ugnich").isVerified(), is(true)); - assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(true)); + userService.createFacebookUser(12345, "12345", "5678", user.getName()); + userService.setFacebookUser("12345", user.getUid()); + assertThat(userService.canDeleteTelegramUser(userService.getUserByName(user.getName())), is(true)); jdbcTemplate.update("DELETE FROM facebook"); - assertThat(userService.canDeleteTelegramUser(userService.getUserByName("ugnich")), is(false)); + assertThat(userService.canDeleteTelegramUser(userService.getUserByName(user.getName())), is(false)); + assertThat(userService.getUserByName(user.getName()).isVerified(), is(true)); + telegramService.deleteTelegramUser(user.getUid()); + assertThat(userService.getUserByName(user.getName()).isVerified(), is(false)); } @Test @@ -2298,27 +2302,6 @@ public class ServerTests { } @Test - public void nodeinfo() throws Exception { - MvcResult nodeinfoXRD = mockMvc - .perform(get("/.well-known/nodeinfo").contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()).andReturn(); - JsonNode node = jsonMapper.readTree(nodeinfoXRD.getResponse().getContentAsString()); - assertThat(node.get("links"), notNullValue()); - String nodeinfoUrl = node.get("links").get(0).get("href").textValue(); - MvcResult nodeinfoData = mockMvc.perform(get(nodeinfoUrl).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()).andReturn(); - JsonNode nodeinfo = jsonMapper.readTree(nodeinfoData.getResponse().getContentAsString()); - assertThat(nodeinfo.get("software"), notNullValue()); - assertThat(nodeinfo.get("server"), nullValue()); - MvcResult xnodeinfoData = mockMvc - .perform(get("/.well-known/x-nodeinfo2").contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()).andReturn(); - JsonNode xnodeinfo = jsonMapper.readTree(xnodeinfoData.getResponse().getContentAsString()); - assertThat(xnodeinfo.get("server"), notNullValue()); - assertThat(xnodeinfo.get("software"), nullValue()); - } - - @Test public void anonymousUserFromZero() { User user = userService.getUserByUID(0).orElse(AnonymousUser.INSTANCE); assertThat(user.isAnonymous(), is(true)); @@ -2696,8 +2679,51 @@ public class ServerTests { @Test public void textTruncationShouldNotBreakEmojis() { var text = "Так ты же написал, чтоб я сам поправил отступ \uD83D\uDE00\n"; - var expected = "Так ты же написал, чтоб я сам поправил отступ"; - var truncated = PlainTextFormatter.truncateText(text, 47); + var expected = "Так ты же написал, чтоб я сам поправил отступ …"; + var truncated = StringUtils.abbreviate(text, "…", 47); assertThat(truncated, is(expected)); } + @Test + @Transactional + public void jpaMessageTests() { + var user = usersRepository.findByName(ugnichName); + var message = new MessageEntity(); + message.setUser(user); + message.setText("Hello, JPA!"); + message.setTimestamp(Instant.now()); + message = messagesRepository.save(message); + var msg = messagesService.getMessage(message.getId()); + assertThat(msg.get().getText(), is(notNullValue())); + assertThat(msg.get().getText(), is (message.getText())); + } + @Test + @Transactional + public void jpaReplyTests() { + var mid = messagesService.createMessage(ugnich.getUid(), "new message", null, Set.of()); + var rid = messagesService.createReply(mid, 0, ugnich, "reply 1", null); + var message = messagesRepository.getReferenceById(mid); + var replies = message.getReplies(); + assertThat(replies.size(), is(1)); + } + @Test + @Transactional + public void unsupportedMediaShouldBeHandledCorrectly() throws Exception { + ClassPathResource newMedia = new ClassPathResource("sample1.dng"); + byte[] newMediaData = IOUtils.toByteArray(newMedia.getInputStream()); + var response = mockMvc.perform(MockMvcRequestBuilders.multipart("/api/post") + .file(new MockMultipartFile("attach", "sample1.dng", "image/dng", newMediaData)) + .param("body", "test") + .with(httpBasic(freefdName, freefdPassword))).andExpect(status().isBadRequest()).andReturn(); + var result = jsonMapper.readValue(response.getResponse().getContentAsString(), CommandResult.class); + assertThat(result.getText(), is("Wrong file type: tif")); + var r = commandsManager.processCommand(freefd, "tst", emptyUri); + response = mockMvc.perform(MockMvcRequestBuilders.multipart("/api/comment") + .file(new MockMultipartFile("attach", "sample1.dng", "image/dng", newMediaData)) + .param("body", "test") + .param("mid", String.valueOf(r.getNewMessage().get().getMid())) + .with(httpBasic(freefdName, freefdPassword))).andExpect(status().isBadRequest()).andReturn(); + result = jsonMapper.readValue(response.getResponse().getContentAsString(), CommandResult.class); + assertThat(result.getText(), is("Wrong file type: tif")); + + } } diff --git a/src/test/resources/application-sqlite.yml b/src/test/resources/application-sqlite.yml index 9ffa0833..60cd1392 100644 --- a/src/test/resources/application-sqlite.yml +++ b/src/test/resources/application-sqlite.yml @@ -8,4 +8,5 @@ spring: init: platform: sqlite mode: always - + jpa: + database-platform: org.hibernate.community.dialect.SQLiteDialect diff --git a/src/test/resources/sample1.dng b/src/test/resources/sample1.dng Binary files differnew file mode 100644 index 00000000..e732e009 --- /dev/null +++ b/src/test/resources/sample1.dng |