diff options
Diffstat (limited to 'juick-server/src')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/CrosspostManager.java | 4 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/TelegramBotManager.java | 2 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/XMPPBot.java | 2 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/XMPPServer.java | 2 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java | 4 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/configuration/ApiInitializer.java | 3 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/configuration/ApiSecurityConfig.java | 2 | ||||
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/RSSTests.java | 143 | ||||
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/ServerTests.java (renamed from juick-server/src/test/java/com/juick/server/tests/MessagesTests.java) | 155 | ||||
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/XMPPServerTests.java | 190 |
10 files changed, 158 insertions, 349 deletions
diff --git a/juick-server/src/main/java/com/juick/server/CrosspostManager.java b/juick-server/src/main/java/com/juick/server/CrosspostManager.java index 1752e1ca..631e98ae 100644 --- a/juick-server/src/main/java/com/juick/server/CrosspostManager.java +++ b/juick-server/src/main/java/com/juick/server/CrosspostManager.java @@ -53,9 +53,9 @@ public class CrosspostManager implements ApplicationListener<MessageEvent> { @Inject private CrosspostService crosspostService; - @Value("${twitter_consumer_key:}") + @Value("${twitter_consumer_key:12345678}") private String twitter_consumer_key; - @Value("${twitter_consumer_secret:}") + @Value("${twitter_consumer_secret:secret}") private String twitter_consumer_secret; @Override diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java index 5743a15e..0d765b71 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -70,7 +70,7 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> { private TelegramBot bot; - @Value("${telegram_token}") + @Value("${telegram_token:12345678}") private String telegramToken; @Inject private TelegramService telegramService; diff --git a/juick-server/src/main/java/com/juick/server/XMPPBot.java b/juick-server/src/main/java/com/juick/server/XMPPBot.java index 46a83777..1c17a0aa 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPBot.java +++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java @@ -60,7 +60,7 @@ public class XMPPBot implements StanzaListener, AutoCloseable { private XMPPServer xmpp; @Inject private XMPPConnection router; - @Value("${xmppbot_jid}") + @Value("${xmppbot_jid:juick@localhost}") private Jid jid; private PrettyTime pt; diff --git a/juick-server/src/main/java/com/juick/server/XMPPServer.java b/juick-server/src/main/java/com/juick/server/XMPPServer.java index 7562d265..358860f7 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPServer.java +++ b/juick-server/src/main/java/com/juick/server/XMPPServer.java @@ -63,7 +63,7 @@ public class XMPPServer implements ConnectionListener, AutoCloseable { @Inject public ExecutorService service; - @Value("${hostname}") + @Value("${hostname:localhost}") private Jid jid; @Value("${s2s_port:5269}") private int s2sPort; diff --git a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java index db9dab17..146e277c 100644 --- a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java +++ b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java @@ -68,7 +68,7 @@ import java.util.concurrent.Executors; @EnableSwagger2 @EnableScheduling @EnableWebSocket -@PropertySource("classpath:juick.conf") +@PropertySource(value = "classpath:juick.conf", ignoreResourceNotFound = true) @ComponentScan(basePackages = "com.juick.server") public class ApiAppConfiguration extends BaseWebConfiguration implements WebSocketConfigurer { @Inject @@ -132,7 +132,7 @@ public class ApiAppConfiguration extends BaseWebConfiguration implements WebSock container.setMaxBinaryMessageBufferSize(8192); return container; } - @Value("${hostname}") + @Value("${hostname:localhost}") private String hostname; @Bean diff --git a/juick-server/src/main/java/com/juick/server/configuration/ApiInitializer.java b/juick-server/src/main/java/com/juick/server/configuration/ApiInitializer.java index b25edd9a..b789fc50 100644 --- a/juick-server/src/main/java/com/juick/server/configuration/ApiInitializer.java +++ b/juick-server/src/main/java/com/juick/server/configuration/ApiInitializer.java @@ -18,8 +18,6 @@ package com.juick.server.configuration; import com.juick.configuration.DataConfiguration; -import com.juick.server.configuration.JuickServerWebsocketConfiguration; -import com.juick.server.configuration.StorageConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -35,6 +33,7 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{ + ApiAppConfiguration.class, ApiSecurityConfig.class, DataConfiguration.class, StorageConfiguration.class diff --git a/juick-server/src/main/java/com/juick/server/configuration/ApiSecurityConfig.java b/juick-server/src/main/java/com/juick/server/configuration/ApiSecurityConfig.java index ed79c5f1..58fd4231 100644 --- a/juick-server/src/main/java/com/juick/server/configuration/ApiSecurityConfig.java +++ b/juick-server/src/main/java/com/juick/server/configuration/ApiSecurityConfig.java @@ -48,8 +48,6 @@ import java.util.concurrent.TimeUnit; */ @Configuration @EnableWebSecurity -@PropertySource("classpath:juick.conf") -@Import(ApiAppConfiguration.class) public class ApiSecurityConfig extends WebSecurityConfigurerAdapter { @Value("${auth_remember_me_key}") private String rememberMeKey; diff --git a/juick-server/src/test/java/com/juick/server/tests/RSSTests.java b/juick-server/src/test/java/com/juick/server/tests/RSSTests.java deleted file mode 100644 index 6a19123b..00000000 --- a/juick-server/src/test/java/com/juick/server/tests/RSSTests.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright (C) 2008-2017, 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 - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.server.tests; - -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; -import com.juick.configuration.RepositoryConfiguration; -import com.juick.server.configuration.ApiAppConfiguration; -import com.juick.server.configuration.ApiSecurityConfig; -import com.juick.service.*; -import org.apache.commons.text.RandomStringGenerator; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.Primary; -import org.springframework.test.context.ContextConfiguration; -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 javax.inject.Inject; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -/** - * Created by vitalyster on 13.12.2016. - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {ApiAppConfiguration.class, ApiSecurityConfig.class, RepositoryConfiguration.class}) -@WebAppConfiguration -public class RSSTests { - - private MockMvc mockMvc; - @Inject - private WebApplicationContext webApplicationContext; - - @Inject - private MessagesService messagesService; - @Inject - private UserService userService; - @Inject - private TagService tagService; - - private User ugnich, freefd; - String ugnichName, ugnichPassword, freefdName, freefdPassword; - - final static RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('a', 'z').build(); - - private static Message getMessage(final User user, final String messageText) { - Message msg = new Message(); - - msg.setMid(1); - msg.setUser(user); - msg.setText(messageText == null ? generator.generate(24) : messageText); - msg.setTags(Collections.singletonList(new Tag(generator.generate(4)))); - - return msg; - } - - private static User getUser(final int uid, final String name, final String password) { - User user = new User(); - - user.setName(name); - user.setUid(uid); - user.setCredentials(password); - user.setBanned(false); - - return user; - } - - @Before - public void setUp() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) - .dispatchOptions(true) - .build(); - ugnichName = "ugnich"; - ugnichPassword = "MyPassw0rd!"; - freefdName = "freefd"; - freefdPassword = "MyPassw0rd!"; - - ugnich = getUser(1, ugnichName, ugnichPassword); - freefd = getUser(2, freefdName, freefdPassword); - - List<String> users = new ArrayList<>(2); - users.add(ugnichName); - users.add(freefdName); - - when(userService.getUsersByName(users)) - .thenReturn(Arrays.asList(ugnich, freefd)); - when(userService.getUserByName(ugnichName)) - .thenReturn(ugnich); - when(userService.getFullyUserByName(ugnichName)) - .thenReturn(ugnich); - when(userService.getUserByName(null)) - .thenReturn(new User()); - } - - @Test - public void lastMessagesTest() throws Exception { - String msgText = "Привет, я - Угнич"; - - Message msg = getMessage(ugnich, msgText); - - when(messagesService.getMyFeed(1, 0, false)) - .thenReturn(Collections.singletonList(1)); - when(messagesService.getMessages(Collections.singletonList(1))) - .thenReturn(Collections.singletonList(msg)); - - mockMvc.perform( - get("/rss/")) - .andExpect(status().isOk()) - .andExpect(content().contentType("application/rss+xml")) - .andExpect(xpath("/rss/channel/description").string("The latest messages at Juick")); - } -} diff --git a/juick-server/src/test/java/com/juick/server/tests/MessagesTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index b9bed8f1..b1848986 100644 --- a/juick-server/src/test/java/com/juick/server/tests/MessagesTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -17,6 +17,7 @@ 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; @@ -25,20 +26,22 @@ import com.juick.Tag; import com.juick.User; import com.juick.configuration.RepositoryConfiguration; 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.configuration.ApiSecurityConfig; import com.juick.server.helpers.TagStats; -import com.juick.service.ImagesService; -import com.juick.service.MessagesService; -import com.juick.service.TagService; -import com.juick.service.UserService; +import com.juick.service.*; import com.juick.util.DateFormattersHolder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; 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; @@ -48,8 +51,13 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; +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.Arrays; import java.util.Collections; import java.util.List; @@ -58,6 +66,8 @@ import java.util.stream.IntStream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -67,8 +77,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {ApiAppConfiguration.class, ApiSecurityConfig.class, RepositoryConfiguration.class}) +@TestPropertySource(properties = {"broken_ssl_hosts=localhost,serverstorageisfull.tld"}) @WebAppConfiguration -public class MessagesTests extends AbstractJUnit4SpringContextTests { +public class ServerTests extends AbstractJUnit4SpringContextTests { private MockMvc mockMvc; @Inject @@ -84,6 +95,18 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests { private ObjectMapper jsonMapper; @Inject private ImagesService imagesService; + @Inject + private WebApplicationContext wac; + @Inject + private XMPPServer server; + @Inject + private XMPPBot bot; + @Inject + private SubscriptionService subscriptionService; + @Inject + private JdbcTemplate jdbcTemplate; + @Value("${hostname:localhost}") + private Jid jid; private static User ugnich, freefd, juick; static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword; @@ -323,4 +346,126 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests { assertThat(mid, equalTo(123456)); assertThat(rid, equalTo(56)); } + @Test + public void lastMessagesTest() throws Exception { + mockMvc.perform( + get("/rss/")) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/rss+xml")) + .andExpect(xpath("/rss/channel/description").string("The latest messages at Juick")); + } + @Test + public void statusPageIsUp() throws Exception { + mockMvc.perform(get("http://localhost:8080/status").with(httpBasic(ugnichName, ugnichPassword))).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()); + */ + } } 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 deleted file mode 100644 index 5a3c8e69..00000000 --- a/juick-server/src/test/java/com/juick/server/tests/XMPPServerTests.java +++ /dev/null @@ -1,190 +0,0 @@ -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()); - */ - } -} |