From 94f85e2ec113efee03def8ae9539cdaee21cccf9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 28 Mar 2018 11:27:29 +0300 Subject: drop sphinx search and use datasource autoconfiguration --- .../main/java/com/juick/service/SearchService.java | 31 ++ .../com/juick/service/search/SearchService.java | 31 -- .../com/juick/configuration/DataConfiguration.java | 300 ---------------- .../juick/configuration/SearchConfiguration.java | 58 ---- .../com/juick/service/FTSSearchServiceImpl.java | 85 +++++ .../com/juick/service/MessagesServiceImpl.java | 1 - .../service/search/SphinxSearchServiceImpl.java | 102 ------ .../java/com/juick/service/MessageServiceTest.java | 2 - .../server/configuration/ApiAppConfiguration.java | 3 - .../java/com/juick/server/tests/ServerTests.java | 27 +- juick-www/src/main/java/com/juick/Application.java | 22 ++ .../com/juick/configuration/SapeConfiguration.java | 37 ++ .../com/juick/configuration/WebSecurityConfig.java | 140 ++++++++ .../juick/configuration/WwwAppConfiguration.java | 123 +++++++ .../com/juick/configuration/XMPPConfiguration.java | 43 +++ .../src/main/java/com/juick/www/Application.java | 20 -- .../juick/www/configuration/SapeConfiguration.java | 37 -- .../juick/www/configuration/WebSecurityConfig.java | 140 -------- .../www/configuration/WwwAppConfiguration.java | 125 ------- .../juick/www/configuration/XMPPConfiguration.java | 43 --- juick-www/src/test/java/com/juick/WebAppTests.java | 379 ++++++++++++++++++++ .../src/test/java/com/juick/www/WebAppTests.java | 383 --------------------- 22 files changed, 873 insertions(+), 1259 deletions(-) create mode 100644 juick-common/src/main/java/com/juick/service/SearchService.java delete mode 100644 juick-common/src/main/java/com/juick/service/search/SearchService.java delete mode 100644 juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java delete mode 100644 juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/FTSSearchServiceImpl.java delete mode 100644 juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java create mode 100644 juick-www/src/main/java/com/juick/Application.java create mode 100644 juick-www/src/main/java/com/juick/configuration/SapeConfiguration.java create mode 100644 juick-www/src/main/java/com/juick/configuration/WebSecurityConfig.java create mode 100644 juick-www/src/main/java/com/juick/configuration/WwwAppConfiguration.java create mode 100644 juick-www/src/main/java/com/juick/configuration/XMPPConfiguration.java delete mode 100644 juick-www/src/main/java/com/juick/www/Application.java delete mode 100644 juick-www/src/main/java/com/juick/www/configuration/SapeConfiguration.java delete mode 100644 juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java delete mode 100644 juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java delete mode 100644 juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java create mode 100644 juick-www/src/test/java/com/juick/WebAppTests.java delete mode 100644 juick-www/src/test/java/com/juick/www/WebAppTests.java diff --git a/juick-common/src/main/java/com/juick/service/SearchService.java b/juick-common/src/main/java/com/juick/service/SearchService.java new file mode 100644 index 00000000..cd57eb5b --- /dev/null +++ b/juick-common/src/main/java/com/juick/service/SearchService.java @@ -0,0 +1,31 @@ +/* + * 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 . + */ + +package com.juick.service; + +import java.util.List; + +/** + * Created by aalexeev on 11/18/16. + */ +public interface SearchService { + void setMaxResult(int maxResult); + + List searchInAllMessages(String searchString, int messageIdBefore); + + List searchByStringAndUser(String searchString, final int userId, int messageIdBefore); +} diff --git a/juick-common/src/main/java/com/juick/service/search/SearchService.java b/juick-common/src/main/java/com/juick/service/search/SearchService.java deleted file mode 100644 index b1ea9374..00000000 --- a/juick-common/src/main/java/com/juick/service/search/SearchService.java +++ /dev/null @@ -1,31 +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 . - */ - -package com.juick.service.search; - -import java.util.List; - -/** - * Created by aalexeev on 11/18/16. - */ -public interface SearchService { - void setMaxResult(int maxResult); - - List searchInAllMessages(String searchString, int messageIdBefore); - - List searchByStringAndUser(String searchString, final int userId, int messageIdBefore); -} diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java deleted file mode 100644 index 9f59f965..00000000 --- a/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java +++ /dev/null @@ -1,300 +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 . - */ - -package com.juick.configuration; - -import com.juick.service.search.SearchService; -import org.apache.commons.dbcp2.BasicDataSource; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; -import org.springframework.dao.DuplicateKeyException; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; -import org.springframework.transaction.annotation.TransactionManagementConfigurer; - -import javax.sql.DataSource; -import java.util.Collections; -import java.util.List; - -/** - * Created by aalexeev on 11/11/16. - */ -@Configuration -@EnableTransactionManagement -@ComponentScan(basePackages = {"com.juick.service"}) -public class DataConfiguration implements TransactionManagementConfigurer { - @Value("${datasource_driver:org.h2.Driver}") - private String datasourceDriver; - @Value("${datasource_url:jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;mode=MYSQL}") - private String datasourceUrl; - @Value("${datasource_user:juick}") - private String datasourceUser; - @Value("${datasource_password:secret}") - private String datasourcePassword; - - // NOTE: The close() method will be called automatically with default @Bean settings - // But Datasource interface has no close() method - @Bean(destroyMethod = "") - public DataSource dataSource() { - BasicDataSource dataSource = new BasicDataSource(); - - dataSource.setDriverClassName(datasourceDriver); - dataSource.setUrl(datasourceUrl); - dataSource.setUsername(datasourceUser); - dataSource.setPassword(datasourcePassword); - - dataSource.setValidationQuery("select 1"); - - return dataSource; - } - - @Bean - public PlatformTransactionManager transactionManager() { - return new DataSourceTransactionManager(dataSource()); - } - - @Override - public PlatformTransactionManager annotationDrivenTransactionManager() { - return transactionManager(); - } - - @Bean - public JdbcTemplate jdbcTemplate() { - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); - if (datasourceDriver.equals("org.h2.Driver")) { - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_users (user_id int(10) unsigned NOT NULL, " + - "bl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_tags (user_id int(10) unsigned NOT NULL, " + - "tag_id int(10) unsigned NOT NULL)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS wl_users (user_id int(10) unsigned NOT NULL, " + - "wl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages (" + - "message_id int(10) unsigned NOT NULL AUTO_INCREMENT," + - "user_id int(10) unsigned NOT NULL," + - "place_id int(10) unsigned DEFAULT NULL," + - "lat decimal(10,7) DEFAULT NULL," + - "lon decimal(10,7) DEFAULT NULL," + - "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," + - "replies smallint(5) unsigned NOT NULL DEFAULT 0," + - "maxreplyid smallint(5) unsigned NOT NULL DEFAULT 0," + - "privacy tinyint(4) NOT NULL DEFAULT '1'," + - "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," + - "readonly tinyint(1) NOT NULL DEFAULT 0," + - "likes smallint(6) NOT NULL DEFAULT 0," + - "`popular` tinyint(4) NOT NULL DEFAULT '0'," + - "hidden tinyint(3) unsigned NOT NULL DEFAULT 0," + - "updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_tags (" + - "message_id int(10) unsigned NOT NULL," + - "tag_id int(10) unsigned NOT NULL" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS users (" + - "id int(10) unsigned NOT NULL AUTO_INCREMENT," + - "nick char(64) NOT NULL," + - "`lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__'," + - "passw char(32) NOT NULL," + - "banned tinyint(3) unsigned NOT NULL DEFAULT 0, " + - "PRIMARY KEY(id), UNIQUE KEY(nick))"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS useroptions (" + - "user_id int(10) unsigned NOT NULL," + - "jnotify tinyint(1) NOT NULL DEFAULT 1," + - "`repliesview` tinyint(1) NOT NULL DEFAULT '0'," + - "`subscr_notify` tinyint(1) NOT NULL DEFAULT '1'," + - "`recommendations` tinyint(1) NOT NULL DEFAULT '1'," + - "subscr_active tinyint(1) NOT NULL DEFAULT 1)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_users (" + - "user_id int(10) unsigned NOT NULL," + - "suser_id int(10) unsigned NOT NULL," + - "jid char(64) DEFAULT NULL," + - "active tinyint(1) NOT NULL DEFAULT 1," + - "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " + - "PRIMARY KEY (suser_id, user_id))"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_messages (" + - "message_id int(10) unsigned NOT NULL," + - "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, message_id))"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_tags (" + - "tag_id int(10) unsigned NOT NULL," + - "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, tag_id))"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_txt (" + - "message_id int(10) unsigned NOT NULL," + - "tags varchar(255)," + - "repliesby varchar(96)," + - "txt TEXT)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS tags(" + - "tag_id int(10) unsigned NOT NULL AUTO_INCREMENT," + - "synonym_id int(10) unsigned DEFAULT NULL," + - "name varchar_ignorecase(48) NOT NULL," + - "top tinyint(1) unsigned NOT NULL DEFAULT 0," + - "stat_messages int(10) unsigned NOT NULL DEFAULT 0," + - "stat_users smallint(5) unsigned NOT NULL DEFAULT 0)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS jids(" + - "user_id int(10) unsigned NOT NULL AUTO_INCREMENT," + - "jid char(64) NOT NULL," + - "active tinyint(1) unsigned NOT NULL DEFAULT '1'," + - "loginhash char(36) unsigned DEFAULT NULL," + - "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS replies (" + - "message_id int(10) unsigned NOT NULL," + - "reply_id smallint(5) unsigned NOT NULL," + - "user_id int(10) unsigned NOT NULL," + - "replyto smallint(5) unsigned NOT NULL DEFAULT 0," + - "ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + - "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," + - "txt text)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS twitter(" + - "user_id int(10) unsigned NOT NULL," + - "access_token char(64) NOT NULL," + - "access_token_secret char(64) NOT NULL," + - "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + - "uname char(64) NOT NULL," + - "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS facebook(" + - "user_id int(10) unsigned NOT NULL," + - "fb_id int(20) unsigned NOT NULL," + - "access_token char(255) NOT NULL," + - "loginhash char(36) NOT NULL," + - "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + - "fb_name char(64) NOT NULL," + - "fb_link char(64) NOT NULL," + - "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS vk(" + - "user_id int(10) unsigned NOT NULL," + - "vk_id int(20) unsigned NOT NULL," + - "access_token char(128) NOT NULL," + - "loginhash char(36) NOT NULL," + - "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + - "vk_name char(64) NOT NULL," + - "vk_link char(64) NOT NULL," + - "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS emails (" + - " user_id int(10) unsigned NOT NULL," + - " email char(64) NOT NULL," + - " subscr_hour tinyint(4) DEFAULT NULL," + - " PRIMARY KEY (email)" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS favorites (" + - " user_id int(10) unsigned NOT NULL," + - " message_id int(10) unsigned NOT NULL," + - " ts datetime NOT NULL" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS logins (" + - " user_id int(10) unsigned NOT NULL," + - " hash char(16) NOT NULL," + - " PRIMARY KEY (user_id)" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `telegram` (" + - " `user_id` int(10) unsigned DEFAULT NULL," + - " `tg_id` bigint(20) NOT NULL," + - " `tg_name` char(64) NOT NULL," + - " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + - " `loginhash` char(36) DEFAULT NULL" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `pm` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `user_id_to` int(10) unsigned NOT NULL," + - " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + - " `txt` text NOT NULL" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `pm_streams` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `user_id_to` int(10) unsigned NOT NULL," + - " `lastmessage` datetime NOT NULL," + - " `lastview` datetime DEFAULT NULL," + - " `unread` smallint(5) unsigned NOT NULL DEFAULT '0'" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `android` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `regid` char(255) NOT NULL," + - " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `ios` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `token` char(64) NOT NULL," + - " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `winphone` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `url` char(255) NOT NULL," + - " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `tags_ignore` (" + - " `tag_id` int(10) unsigned NOT NULL" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `users_subscr` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `cnt` smallint(5) unsigned NOT NULL DEFAULT '0'," + - " PRIMARY KEY (`user_id`)" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `auth` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `protocol` enum('xmpp','email','sms') NOT NULL," + - " `account` char(64) NOT NULL," + - " `authcode` char(8) NOT NULL" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `mail` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `hash` char(16) NOT NULL," + - " PRIMARY KEY (`user_id`)" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `usersinfo` (" + - " `user_id` int(10) unsigned NOT NULL," + - " `jid` char(32) DEFAULT NULL," + - " `fullname` char(32) DEFAULT NULL," + - " `country` char(32) DEFAULT NULL," + - " `url` char(64) DEFAULT NULL," + - " `gender` char(32) DEFAULT NULL," + - " `bday` char(10) DEFAULT NULL," + - " `descr` varchar(255) DEFAULT NULL," + - " PRIMARY KEY (`user_id`)" + - ")"); - jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS `telegram_chats` (\n" + - " `chat_id` bigint(20) DEFAULT NULL,\n" + - " UNIQUE KEY `chat_id` (`chat_id`)\n" + - ")"); - } - return jdbcTemplate; - } - - @Bean - public SearchService emptySearchService() { - return new SearchService() { - @Override - public void setMaxResult(int maxResult) { - } - - @Override - public List searchInAllMessages(String searchString, int messageIdBefore) { - return Collections.emptyList(); - } - - @Override - public List searchByStringAndUser(String searchString, int userId, int messageIdBefore) { - return Collections.emptyList(); - } - }; - } - @Bean - public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { - return new PropertySourcesPlaceholderConfigurer(); - } -} diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java deleted file mode 100644 index 26b5f888..00000000 --- a/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java +++ /dev/null @@ -1,58 +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 . - */ - -package com.juick.configuration; - -import com.juick.service.search.SearchService; -import com.juick.service.search.SphinxSearchServiceImpl; -import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.commons.lang3.StringUtils; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; -import org.springframework.core.env.Environment; - -import javax.annotation.Resource; - -/** - * Created by aalexeev on 11/18/16. - */ -@Configuration -public class SearchConfiguration { - @Resource - private Environment env; - - // NOTE: The close() method will be called automatically with default @Bean settings - // But Datasource interface has no close() method - @Bean(destroyMethod = "") - public BasicDataSource searchDataSource() { - BasicDataSource dataSource = new BasicDataSource(); - - dataSource.setDriverClassName(env.getProperty("sphinx_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(env.getProperty("sphinx_url")); - dataSource.setUsername(env.getProperty("sphinx_user", StringUtils.EMPTY)); - dataSource.setPassword(env.getProperty("sphinx_password", StringUtils.EMPTY)); - - return dataSource; - } - - @Bean - @DependsOn("searchDataSource") - public SearchService searchService() { - return new SphinxSearchServiceImpl(searchDataSource()); - } -} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/FTSSearchServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/FTSSearchServiceImpl.java new file mode 100644 index 00000000..8d2ffbdb --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/FTSSearchServiceImpl.java @@ -0,0 +1,85 @@ +/* + * 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 . + */ + +package com.juick.service; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collections; +import java.util.List; + +/** + * Created by aalexeev on 11/18/16. + */ + +@Repository +@Transactional(readOnly = true) +public class FTSSearchServiceImpl extends BaseJdbcService implements SearchService { + private static final int DEFAULT_MAX_RESULT = 20; + + private int maxResult = DEFAULT_MAX_RESULT; + + @Override + public List searchInAllMessages(final String searchString, final int messageIdBefore) { + if (StringUtils.isBlank(searchString)) + return Collections.emptyList(); + + MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("search", searchString) + .addValue("before", messageIdBefore) + .addValue("limit", maxResult); + + return getNamedParameterJdbcTemplate().queryForList( + "SELECT message_id FROM messages_txt WHERE MATCH(txt) AGAINST(:search) " + + (messageIdBefore > 0 ? + " AND message_id < :before " : StringUtils.EMPTY) + + " ORDER BY message_id DESC LIMIT :limit", + sqlParameterSource, + Integer.class); + } + + @Override + public List searchByStringAndUser(final String searchString, final int userId, int messageIdBefore) { + if (StringUtils.isBlank(searchString)) + return Collections.emptyList(); + + MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("search", searchString) + .addValue("userId", userId) + .addValue("before", messageIdBefore) + .addValue("limit", maxResult); + + return getNamedParameterJdbcTemplate().queryForList( + "SELECT messages.message_id AS message_id FROM messages INNER JOIN messages_txt ON messages_txt.message_id=messages.message_id WHERE messages.user_id = :userId AND MATCH(messages_txt.txt) AGAINST (:search) " + + (messageIdBefore > 0 ? + " AND messages.message_id < :before " : StringUtils.EMPTY) + + " ORDER BY messages.message_id DESC LIMIT :limit", + sqlParameterSource, + Integer.class); + } + + @Override + public void setMaxResult(int maxResult) { + if (maxResult <= 0) + throw new IllegalArgumentException("maxResult value (" + maxResult + ") must be greater then 0"); + + this.maxResult = maxResult; + } +} \ No newline at end of file diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java index 4398259c..31731466 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -22,7 +22,6 @@ import com.juick.Tag; import com.juick.User; import com.juick.server.helpers.PrivacyOpts; import com.juick.server.helpers.ResponseReply; -import com.juick.service.search.SearchService; import com.juick.util.MessageUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; diff --git a/juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java deleted file mode 100644 index e4287502..00000000 --- a/juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java +++ /dev/null @@ -1,102 +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 . - */ - -package com.juick.service.search; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.util.Assert; - -import javax.sql.DataSource; -import java.util.Collections; -import java.util.List; - -/** - * Created by aalexeev on 11/18/16. - */ - -/* Note - * Don't use any spring's component annotation (such as @Repository, @Service, @Component, etc). - * This class directly used by spring's search configuration class - */ -public class SphinxSearchServiceImpl implements SearchService { - private static final int DEFAULT_MAX_RESULT = 25; - - private final NamedParameterJdbcTemplate namedParameterSearchJdbcTemplate; - - private int maxResult = DEFAULT_MAX_RESULT; - - - public SphinxSearchServiceImpl(JdbcTemplate searchJdbcTemplate) { - Assert.notNull(searchJdbcTemplate, "JdbcTemplate must be initialized"); - this.namedParameterSearchJdbcTemplate = new NamedParameterJdbcTemplate(searchJdbcTemplate); - } - - public SphinxSearchServiceImpl(DataSource searchDataSource) { - Assert.notNull(searchDataSource, "DataSource must be initialized"); - this.namedParameterSearchJdbcTemplate = new NamedParameterJdbcTemplate(searchDataSource); - } - - @Override - public List searchInAllMessages(final String searchString, final int messageIdBefore) { - if (StringUtils.isBlank(searchString)) - return Collections.emptyList(); - - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("search", searchString) - .addValue("before", messageIdBefore) - .addValue("limit", maxResult); - - return namedParameterSearchJdbcTemplate.queryForList( - "SELECT id AS message_id FROM messages WHERE MATCH(:search) " + - (messageIdBefore > 0 ? - " AND id < :before " : StringUtils.EMPTY) + - " ORDER BY id DESC LIMIT :limit", - sqlParameterSource, - Integer.class); - } - - @Override - public List searchByStringAndUser(final String searchString, final int userId, int messageIdBefore) { - if (StringUtils.isBlank(searchString)) - return Collections.emptyList(); - - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("search", searchString) - .addValue("userId", userId) - .addValue("before", messageIdBefore) - .addValue("limit", maxResult); - - return namedParameterSearchJdbcTemplate.queryForList( - "SELECT id AS message_id FROM messages WHERE user_id = :userId AND MATCH(:search) " + - (messageIdBefore > 0 ? - " AND id < :before " : StringUtils.EMPTY) + - " ORDER BY id DESC LIMIT :limit", - sqlParameterSource, - Integer.class); - } - - @Override - public void setMaxResult(int maxResult) { - if (maxResult <= 0) - throw new IllegalArgumentException("maxResult value (" + maxResult + ") must be greater then 0"); - - this.maxResult = maxResult; - } -} \ No newline at end of file diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java index 7b321496..5cbc2ca0 100644 --- a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java +++ b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java @@ -20,7 +20,6 @@ package com.juick.service; import com.juick.Message; import com.juick.Tag; import com.juick.User; -import com.juick.configuration.DataConfiguration; import com.juick.server.helpers.AnonymousUser; import com.juick.server.helpers.TagStats; import com.juick.util.MessageUtils; @@ -50,7 +49,6 @@ import static org.junit.Assert.assertEquals; * Created by aalexeev on 11/25/16. */ @RunWith(SpringRunner.class) -@ContextConfiguration(classes = { DataConfiguration.class }) public class MessageServiceTest extends AbstractJUnit4SpringContextTests { @Inject private MessagesService messagesService; 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 78838117..973c31fd 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 @@ -18,7 +18,6 @@ package com.juick.server.configuration; import com.google.common.base.Predicates; -import com.juick.configuration.DataConfiguration; import com.juick.server.WebsocketManager; import com.juick.server.api.rss.MessagesView; import com.juick.server.api.rss.RepliesView; @@ -51,8 +50,6 @@ import java.util.Collections; @EnableSwagger2 @EnableScheduling @EnableWebSocket -@Import({ApiSecurityConfig.class, BaseWebConfiguration.class, DataConfiguration.class, StorageConfiguration.class}) -@ComponentScan(basePackages = "com.juick.server") public class ApiAppConfiguration implements WebMvcConfigurer, WebSocketConfigurer { @Inject private WebsocketManager websocketManager; 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 b8c9d524..7220deeb 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 @@ -24,7 +24,6 @@ import com.juick.ExternalToken; import com.juick.Message; import com.juick.Tag; import com.juick.User; -import com.juick.configuration.DataConfiguration; import com.juick.server.EmailManager; import com.juick.server.XMPPBot; import com.juick.server.XMPPServer; @@ -36,13 +35,15 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; -import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; @@ -51,7 +52,6 @@ import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; @@ -59,11 +59,9 @@ 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.MessageEvent; import rocks.xmpp.core.stanza.model.Stanza; import rocks.xmpp.core.stanza.model.server.ServerMessage; -import javax.annotation.Nonnull; import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; @@ -125,14 +123,8 @@ public class ServerTests { private static boolean isSetUp = false; - @Import({ApiAppConfiguration.class, DataConfiguration.class}) - @Configuration - static class TestConfig { - @Bean - ApplicationListener listener() { - return event -> latch.countDown(); - } - } + @Mock + ApplicationListener listener; @Before public void setUp() { @@ -156,6 +148,13 @@ public class ServerTests { msg = messagesService.getMessage(mid); tagService.createTag("тест"); juickTagId = tagService.createTag("juick"); + Mockito.doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + latch.countDown(); + return null; + } + }).when(listener).onApplicationEvent(Mockito.any()); isSetUp = true; } } diff --git a/juick-www/src/main/java/com/juick/Application.java b/juick-www/src/main/java/com/juick/Application.java new file mode 100644 index 00000000..f8e5d333 --- /dev/null +++ b/juick-www/src/main/java/com/juick/Application.java @@ -0,0 +1,22 @@ +package com.juick; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication +@EnableTransactionManagement +public class Application extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { + setRegisterErrorPageFilter(false); + return builder.sources(Application.class); + } + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/juick-www/src/main/java/com/juick/configuration/SapeConfiguration.java b/juick-www/src/main/java/com/juick/configuration/SapeConfiguration.java new file mode 100644 index 00000000..2630c05b --- /dev/null +++ b/juick-www/src/main/java/com/juick/configuration/SapeConfiguration.java @@ -0,0 +1,37 @@ +/* + * 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 . + */ + +package com.juick.configuration; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import ru.sape.Sape; + +/** + * Created by vitalyster on 29.03.2017. + */ +@Configuration +public class SapeConfiguration { + @Value("${sape_user:secret}") + private String token; + + @Bean + public Sape sape() { + return new Sape(token, "juick.com", 2000, 3600); + } +} diff --git a/juick-www/src/main/java/com/juick/configuration/WebSecurityConfig.java b/juick-www/src/main/java/com/juick/configuration/WebSecurityConfig.java new file mode 100644 index 00000000..92f43e64 --- /dev/null +++ b/juick-www/src/main/java/com/juick/configuration/WebSecurityConfig.java @@ -0,0 +1,140 @@ +/* + * 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 . + */ + +package com.juick.configuration; + +import com.juick.service.UserService; +import com.juick.service.security.HashParamAuthenticationFilter; +import com.juick.service.security.JuickUserDetailsService; +import com.juick.service.security.entities.JuickUser; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.web.authentication.RememberMeServices; +import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices; +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; + +import javax.annotation.Resource; + +/** + * Created by aalexeev on 11/21/16. + */ +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Value("${auth_remember_me_key:secret}") + private String rememberMeKey; + @Value("${web_domain:localhost}") + private String webDomain; + @Resource + private UserService userService; + + private final String COOKIE_NAME = "juick-remember-me"; + + @Bean("userDetailsService") + @Override + public UserDetailsService userDetailsServiceBean() throws Exception { + return super.userDetailsServiceBean(); + } + + @Override + public UserDetailsService userDetailsService() { + return new JuickUserDetailsService(userService); + } + + @Bean("authenticationManager") + @Override + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.addFilterAfter(hashParamAuthenticationFilter(), BasicAuthenticationFilter.class); + http + .authorizeRequests() + .antMatchers("/settings", "/pm/**", "/**/bl", "/_twitter", "/post", "/comment") + .authenticated() + .anyRequest().permitAll() + .and() + .anonymous().principal(JuickUser.ANONYMOUS_USER).authorities(JuickUser.ANONYMOUS_AUTHORITY) + .and() + .sessionManagement().invalidSessionUrl("/") + .and() + .logout() + .invalidateHttpSession(true) + .logoutUrl("/logout") + .logoutSuccessUrl("/login?logout") + .deleteCookies("hash", COOKIE_NAME) + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .defaultSuccessUrl("/") + .loginProcessingUrl("/login") + .usernameParameter("username") + .passwordParameter("password") + .failureUrl("/login?error=1") + .and() + .rememberMe() + .rememberMeCookieDomain(webDomain).key(rememberMeKey) + .rememberMeServices(rememberMeServices()) + .and() + .csrf().disable() + .authenticationProvider(authenticationProvider()) + .headers().defaultsDisabled().cacheControl(); + } + + @Bean + public DaoAuthenticationProvider authenticationProvider() throws Exception { + DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(); + + authenticationProvider.setUserDetailsService(userDetailsService()); + + return authenticationProvider; + } + + @Bean + public HashParamAuthenticationFilter hashParamAuthenticationFilter() throws Exception { + return new HashParamAuthenticationFilter(userService, rememberMeServices()); + } + + @Bean + public RememberMeServices rememberMeServices() throws Exception { + TokenBasedRememberMeServices services = new TokenBasedRememberMeServices( + rememberMeKey, userDetailsService()); + + services.setCookieName(COOKIE_NAME); + services.setCookieDomain(webDomain); + services.setAlwaysRemember(true); + services.setTokenValiditySeconds(6 * 30 * 24 * 3600); + services.setUseSecureCookie(false); // TODO set true if https is supports + + return services; + } + + @Override + public void configure(WebSecurity web) throws Exception { + web.debug(false); + web.ignoring().antMatchers("/style.css*", "/scripts.js*"); + } +} diff --git a/juick-www/src/main/java/com/juick/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/configuration/WwwAppConfiguration.java new file mode 100644 index 00000000..ea585a6f --- /dev/null +++ b/juick-www/src/main/java/com/juick/configuration/WwwAppConfiguration.java @@ -0,0 +1,123 @@ +/* + * 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 . + */ + +package com.juick.configuration; + +import com.juick.server.configuration.BaseWebConfiguration; +import com.juick.server.configuration.StorageConfiguration; +import com.juick.service.CloudflareCache; +import com.juick.service.TagService; +import com.juick.service.UserService; +import com.juick.www.HelpService; +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.extension.FormatterExtension; +import com.mitchellbosecke.pebble.loader.ClasspathLoader; +import com.mitchellbosecke.pebble.loader.Loader; +import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; +import com.mitchellbosecke.pebble.spring4.extension.SpringExtension; +import org.apache.commons.codec.CharEncoding; +import org.commonmark.ext.autolink.AutolinkExtension; +import org.commonmark.node.Link; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.inject.Inject; +import java.util.Collections; + +/** + * Created by aalexeev on 11/22/16. + */ +@Configuration +@EnableCaching +@Import({ BaseWebConfiguration.class, WebSecurityConfig.class, SapeConfiguration.class, + StorageConfiguration.class, XMPPConfiguration.class}) +public class WwwAppConfiguration implements WebMvcConfigurer { + @Inject + private UserService userService; + @Inject + private TagService tagService; + @Bean + public CaffeineCacheManager cacheManager() { + return new CaffeineCacheManager("help"); + } + + @Bean + public HelpService helpService() { + return new HelpService("help"); + } + + @Bean + public Parser cmParser() { + return Parser.builder().extensions(Collections.singletonList(AutolinkExtension.create())).build(); + } + @Bean + public HtmlRenderer helpRenderer() { + return HtmlRenderer.builder() + .attributeProviderFactory(context -> (node, tagName, attributes) -> { + if (node instanceof Link) { + Link link = (Link) node; + if (link.getDestination().startsWith("/")) { + String destination = "/" + helpService().getHelpPath() + link.getDestination(); + link.setDestination(destination); + attributes.put("href", destination); + } + } + }) + .build(); + } + @Bean + public CloudflareCache cloudflareCache() { + return new CloudflareCache(); + } + @Bean + public Loader templateLoader() { + return new ClasspathLoader(); + } + + @Bean + public SpringExtension springExtension() { + return new SpringExtension(); + } + + @Bean + public PebbleEngine pebbleEngine() { + return new PebbleEngine.Builder() + .loader(this.templateLoader()) + .extension(springExtension()) + .extension(new FormatterExtension()) + .strictVariables(true) + .build(); + } + + @Bean + public ViewResolver viewResolver() { + PebbleViewResolver viewResolver = new PebbleViewResolver(); + viewResolver.setPrefix("templates"); + viewResolver.setSuffix(".html"); + viewResolver.setPebbleEngine(pebbleEngine()); + viewResolver.setCharacterEncoding(CharEncoding.UTF_8); + return viewResolver; + } + +} diff --git a/juick-www/src/main/java/com/juick/configuration/XMPPConfiguration.java b/juick-www/src/main/java/com/juick/configuration/XMPPConfiguration.java new file mode 100644 index 00000000..91f55759 --- /dev/null +++ b/juick-www/src/main/java/com/juick/configuration/XMPPConfiguration.java @@ -0,0 +1,43 @@ +package com.juick.configuration; + +import com.juick.Message; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.core.session.debug.LogbackDebugger; +import rocks.xmpp.extensions.component.accept.ExternalComponent; + +@Configuration +public class XMPPConfiguration { + private static Logger logger = LoggerFactory.getLogger(XMPPConfiguration.class); + @Value("${xmpp_host:localhost}") + private String xmppHost; + @Value("${xmpp_password:secret}") + private String xmppPassword; + @Value("${www_xmpp_jid:www.juick.local}") + private String xmppJid; + @Value("${xmpp_port:5347}") + private int xmppPort; + @Value("${xmpp_disabled:false}") + private boolean isXmppDisabled; + @Bean + public ExternalComponent xmpp() { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(Message.class)) + .debugger(LogbackDebugger.class) + .build(); + ExternalComponent xmpp = ExternalComponent.create(xmppJid, xmppPassword, configuration, xmppHost, xmppPort); + xmpp.addConnectionListener(e -> logger.error(e.toString(), e.getCause())); + if (!isXmppDisabled) try { + xmpp.connect(); + } catch (XmppException e) { + logger.error("xmpp extension", e); + } + return xmpp; + } +} diff --git a/juick-www/src/main/java/com/juick/www/Application.java b/juick-www/src/main/java/com/juick/www/Application.java deleted file mode 100644 index b5a83f49..00000000 --- a/juick-www/src/main/java/com/juick/www/Application.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.juick.www; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; - -@SpringBootApplication -public class Application extends SpringBootServletInitializer { - - @Override - protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { - setRegisterErrorPageFilter(false); - return builder.sources(Application.class); - } - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} diff --git a/juick-www/src/main/java/com/juick/www/configuration/SapeConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/SapeConfiguration.java deleted file mode 100644 index 68ff28d2..00000000 --- a/juick-www/src/main/java/com/juick/www/configuration/SapeConfiguration.java +++ /dev/null @@ -1,37 +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 . - */ - -package com.juick.www.configuration; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import ru.sape.Sape; - -/** - * Created by vitalyster on 29.03.2017. - */ -@Configuration -public class SapeConfiguration { - @Value("${sape_user:secret}") - private String token; - - @Bean - public Sape sape() { - return new Sape(token, "juick.com", 2000, 3600); - } -} diff --git a/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java b/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java deleted file mode 100644 index 65871088..00000000 --- a/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java +++ /dev/null @@ -1,140 +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 . - */ - -package com.juick.www.configuration; - -import com.juick.service.UserService; -import com.juick.service.security.HashParamAuthenticationFilter; -import com.juick.service.security.JuickUserDetailsService; -import com.juick.service.security.entities.JuickUser; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.dao.DaoAuthenticationProvider; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.builders.WebSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.web.authentication.RememberMeServices; -import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices; -import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; - -import javax.annotation.Resource; - -/** - * Created by aalexeev on 11/21/16. - */ -@EnableWebSecurity -public class WebSecurityConfig extends WebSecurityConfigurerAdapter { - @Value("${auth_remember_me_key:secret}") - private String rememberMeKey; - @Value("${web_domain:localhost}") - private String webDomain; - @Resource - private UserService userService; - - private final String COOKIE_NAME = "juick-remember-me"; - - @Bean("userDetailsService") - @Override - public UserDetailsService userDetailsServiceBean() throws Exception { - return super.userDetailsServiceBean(); - } - - @Override - public UserDetailsService userDetailsService() { - return new JuickUserDetailsService(userService); - } - - @Bean("authenticationManager") - @Override - public AuthenticationManager authenticationManagerBean() throws Exception { - return super.authenticationManagerBean(); - } - - @Override - protected void configure(HttpSecurity http) throws Exception { - http.addFilterAfter(hashParamAuthenticationFilter(), BasicAuthenticationFilter.class); - http - .authorizeRequests() - .antMatchers("/settings", "/pm/**", "/**/bl", "/_twitter", "/post", "/comment") - .authenticated() - .anyRequest().permitAll() - .and() - .anonymous().principal(JuickUser.ANONYMOUS_USER).authorities(JuickUser.ANONYMOUS_AUTHORITY) - .and() - .sessionManagement().invalidSessionUrl("/") - .and() - .logout() - .invalidateHttpSession(true) - .logoutUrl("/logout") - .logoutSuccessUrl("/login?logout") - .deleteCookies("hash", COOKIE_NAME) - .and() - .formLogin() - .loginPage("/login") - .permitAll() - .defaultSuccessUrl("/") - .loginProcessingUrl("/login") - .usernameParameter("username") - .passwordParameter("password") - .failureUrl("/login?error=1") - .and() - .rememberMe() - .rememberMeCookieDomain(webDomain).key(rememberMeKey) - .rememberMeServices(rememberMeServices()) - .and() - .csrf().disable() - .authenticationProvider(authenticationProvider()) - .headers().defaultsDisabled().cacheControl(); - } - - @Bean - public DaoAuthenticationProvider authenticationProvider() throws Exception { - DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(); - - authenticationProvider.setUserDetailsService(userDetailsService()); - - return authenticationProvider; - } - - @Bean - public HashParamAuthenticationFilter hashParamAuthenticationFilter() throws Exception { - return new HashParamAuthenticationFilter(userService, rememberMeServices()); - } - - @Bean - public RememberMeServices rememberMeServices() throws Exception { - TokenBasedRememberMeServices services = new TokenBasedRememberMeServices( - rememberMeKey, userDetailsService()); - - services.setCookieName(COOKIE_NAME); - services.setCookieDomain(webDomain); - services.setAlwaysRemember(true); - services.setTokenValiditySeconds(6 * 30 * 24 * 3600); - services.setUseSecureCookie(false); // TODO set true if https is supports - - return services; - } - - @Override - public void configure(WebSecurity web) throws Exception { - web.debug(false); - web.ignoring().antMatchers("/style.css*", "/scripts.js*"); - } -} diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java deleted file mode 100644 index 34720c33..00000000 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java +++ /dev/null @@ -1,125 +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 . - */ - -package com.juick.www.configuration; - -import com.juick.configuration.DataConfiguration; -import com.juick.configuration.SearchConfiguration; -import com.juick.server.configuration.BaseWebConfiguration; -import com.juick.server.configuration.StorageConfiguration; -import com.juick.service.CloudflareCache; -import com.juick.service.TagService; -import com.juick.service.UserService; -import com.juick.www.HelpService; -import com.mitchellbosecke.pebble.PebbleEngine; -import com.mitchellbosecke.pebble.extension.FormatterExtension; -import com.mitchellbosecke.pebble.loader.ClasspathLoader; -import com.mitchellbosecke.pebble.loader.Loader; -import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; -import com.mitchellbosecke.pebble.spring4.extension.SpringExtension; -import org.apache.commons.codec.CharEncoding; -import org.commonmark.ext.autolink.AutolinkExtension; -import org.commonmark.node.Link; -import org.commonmark.parser.Parser; -import org.commonmark.renderer.html.HtmlRenderer; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.caffeine.CaffeineCacheManager; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -import javax.inject.Inject; -import java.util.Collections; - -/** - * Created by aalexeev on 11/22/16. - */ -@Configuration -@EnableCaching -@Import({ BaseWebConfiguration.class, WebSecurityConfig.class, SapeConfiguration.class, SearchConfiguration.class, - DataConfiguration.class, StorageConfiguration.class, XMPPConfiguration.class}) -public class WwwAppConfiguration implements WebMvcConfigurer { - @Inject - private UserService userService; - @Inject - private TagService tagService; - @Bean - public CaffeineCacheManager cacheManager() { - return new CaffeineCacheManager("help"); - } - - @Bean - public HelpService helpService() { - return new HelpService("help"); - } - - @Bean - public Parser cmParser() { - return Parser.builder().extensions(Collections.singletonList(AutolinkExtension.create())).build(); - } - @Bean - public HtmlRenderer helpRenderer() { - return HtmlRenderer.builder() - .attributeProviderFactory(context -> (node, tagName, attributes) -> { - if (node instanceof Link) { - Link link = (Link) node; - if (link.getDestination().startsWith("/")) { - String destination = "/" + helpService().getHelpPath() + link.getDestination(); - link.setDestination(destination); - attributes.put("href", destination); - } - } - }) - .build(); - } - @Bean - public CloudflareCache cloudflareCache() { - return new CloudflareCache(); - } - @Bean - public Loader templateLoader() { - return new ClasspathLoader(); - } - - @Bean - public SpringExtension springExtension() { - return new SpringExtension(); - } - - @Bean - public PebbleEngine pebbleEngine() { - return new PebbleEngine.Builder() - .loader(this.templateLoader()) - .extension(springExtension()) - .extension(new FormatterExtension()) - .strictVariables(true) - .build(); - } - - @Bean - public ViewResolver viewResolver() { - PebbleViewResolver viewResolver = new PebbleViewResolver(); - viewResolver.setPrefix("templates"); - viewResolver.setSuffix(".html"); - viewResolver.setPebbleEngine(pebbleEngine()); - viewResolver.setCharacterEncoding(CharEncoding.UTF_8); - return viewResolver; - } - -} diff --git a/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java deleted file mode 100644 index 1396f9f9..00000000 --- a/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.juick.www.configuration; - -import com.juick.Message; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import rocks.xmpp.core.XmppException; -import rocks.xmpp.core.session.Extension; -import rocks.xmpp.core.session.XmppSessionConfiguration; -import rocks.xmpp.core.session.debug.LogbackDebugger; -import rocks.xmpp.extensions.component.accept.ExternalComponent; - -@Configuration -public class XMPPConfiguration { - private static Logger logger = LoggerFactory.getLogger(XMPPConfiguration.class); - @Value("${xmpp_host:localhost}") - private String xmppHost; - @Value("${xmpp_password:secret}") - private String xmppPassword; - @Value("${www_xmpp_jid:www.juick.local}") - private String xmppJid; - @Value("${xmpp_port:5347}") - private int xmppPort; - @Value("${xmpp_disabled:false}") - private boolean isXmppDisabled; - @Bean - public ExternalComponent xmpp() { - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() - .extensions(Extension.of(Message.class)) - .debugger(LogbackDebugger.class) - .build(); - ExternalComponent xmpp = ExternalComponent.create(xmppJid, xmppPassword, configuration, xmppHost, xmppPort); - xmpp.addConnectionListener(e -> logger.error(e.toString(), e.getCause())); - if (!isXmppDisabled) try { - xmpp.connect(); - } catch (XmppException e) { - logger.error("xmpp extension", e); - } - return xmpp; - } -} diff --git a/juick-www/src/test/java/com/juick/WebAppTests.java b/juick-www/src/test/java/com/juick/WebAppTests.java new file mode 100644 index 00000000..ca318738 --- /dev/null +++ b/juick-www/src/test/java/com/juick/WebAppTests.java @@ -0,0 +1,379 @@ +/* + * 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 . + */ + +package com.juick; + +import com.gargoylesoftware.htmlunit.CookieManager; +import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.css.StyleElement; +import com.gargoylesoftware.htmlunit.html.DomElement; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +import com.juick.service.*; +import com.juick.util.MessageUtils; +import com.juick.www.WebApp; +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.error.PebbleException; +import com.mitchellbosecke.pebble.template.PebbleTemplate; +import org.apache.commons.text.StringEscapeUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.mock.web.MockMultipartFile; +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 javax.inject.Inject; +import javax.servlet.http.Cookie; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.StreamSupport; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +/** + * Created by vitalyster on 12.01.2017. + */ +@RunWith(SpringRunner.class) +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@TestPropertySource(properties = {"xmpp_disabled=true"}) + +public class WebAppTests { + @MockBean + private ImagesService imagesService; + @Inject + private WebApp webApp; + + @Inject + private MockMvc mockMvc; + @Inject + private WebClient webClient; + + @Inject + private UserService userService; + @Inject + private MessagesService messagesService; + @Inject + private PrivacyQueriesService privacyQueriesService; + @Inject + private JdbcTemplate jdbcTemplate; + @Inject + private SubscriptionService subscriptionService; + + @Inject + private PebbleEngine pebbleEngine; + @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") + private String imgPath; + + private static User ugnich, freefd; + private static String ugnichName, ugnichPassword, freefdName, freefdPassword; + + private static boolean isSetUp = false; + + @Before + public void setup() throws IOException { + if (!isSetUp) { + webClient.getOptions().setJavaScriptEnabled(false); + webClient.getOptions().setCssEnabled(false); + webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); + + ugnichName = "ugnich"; + ugnichPassword = "secret"; + freefdName = "freefd"; + freefdPassword = "MyPassw0rd!"; + + userService.createUser(ugnichName, ugnichPassword); + ugnich = userService.getUserByName(ugnichName); + int freefdId = userService.createUser(freefdName, freefdPassword); + freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); + + isSetUp = true; + } + Files.createDirectory(Paths.get(imgPath, "p")); + Files.createDirectory(Paths.get(imgPath, "photos-1024")); + Files.createDirectory(Paths.get(imgPath, "photos-512")); + Files.createDirectory(Paths.get(imgPath, "ps")); + } + + @After + public void teardown() throws IOException { + FileSystemUtils.deleteRecursively(Paths.get(imgPath, "p")); + FileSystemUtils.deleteRecursively(Paths.get(imgPath, "photos-1024")); + FileSystemUtils.deleteRecursively(Paths.get(imgPath, "photos-512")); + FileSystemUtils.deleteRecursively(Paths.get(imgPath, "ps")); + } + + @Test + public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception { + String msgText = "Привет, я - Угнич"; + int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); + HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); + assertThat(threadPage.getTitleText(), equalTo("ugnich:")); + } + @Test + public void bannedUserBlogandPostShouldReturn404() throws IOException { + String userName = "isilmine"; + String userPassword = "secret"; + String msgText = "автор этого поста был забанен"; + String hash = "12345678"; + + User isilmine = userService.getUserByUID(userService.createUser(userName, userPassword)).orElseThrow(IllegalStateException::new); + int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, null); + jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", isilmine.getUid()); + Page blogPage = webClient.getPage("http://localhost:8080/isilmine"); + Page threadPage = webClient.getPage(String.format("http://localhost:8080/isilmine/%d", mid)); + assertThat(blogPage.getWebResponse().getStatusCode(), equalTo(404)); + assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(404)); + } + @Test + public void repliesList() throws IOException { + int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null); + IntStream.range(1, 15).forEach(i -> + messagesService.createReply(mid, i-1, freefd.getUid(), String.valueOf(i-1), null )); + + HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); + assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); + Long visibleItems = StreamSupport.stream(threadPage.getHtmlElementById("replies") + .getChildElements().spliterator(), false).filter(e -> { + StyleElement display = e.getStyleElement("display"); + return display == null || !display.getValue().equals("none"); + }).count(); + assertThat(visibleItems, equalTo(14L)); + } + @Test + public void userShouldNotSeeReplyButtonToBannedUser() throws Exception { + int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, null); + messagesService.createReply(mid, 0, ugnich.getUid(), "yo", null); + MvcResult loginResult = mockMvc.perform(post("/login") + .param("username", freefdName) + .param("password", freefdPassword)).andReturn(); + Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me"); + webClient.setCookieManager(new CookieManager()); + webClient.getCookieManager().addCookie( + new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), + loginCookie.getName(), + loginCookie.getValue())); + HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); + assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); + assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(false)); + assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(false)); + privacyQueriesService.blacklistUser(freefd, ugnich); + assertThat(userService.isInBLAny(freefd.getUid(), ugnich.getUid()), equalTo(true)); + int renhaId = userService.createUser("renha", "secret"); + messagesService.createReply(mid, 0, renhaId, "people", null); + threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); + assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); + assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(true)); + assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(true)); + } + @Test + public void correctTagsEscaping() throws PebbleException, IOException { + PebbleTemplate template = pebbleEngine.getTemplate("views/test"); + Writer writer = new StringWriter(); + template.evaluate(writer, + Collections.singletonMap("tagsList", + Collections.singletonList(StringEscapeUtils.escapeHtml4(new Tag(">_<").getName())))); + String output = writer.toString().trim(); + assertThat(output, equalTo(">_<")); + } + + public DomElement fetchMeta(String url, String name) throws IOException { + HtmlPage page = webClient.getPage(url); + DomElement emptyMeta = new DomElement("", "meta", null, null); + return page.getElementsByTagName("meta").stream() + .filter(t -> t.getAttribute("name").equals(name)).findFirst().orElse(emptyMeta); + } + @Test + public void testTwitterCards() throws Exception { + + int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, null); + + assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card") + .getAttribute("content"), equalTo("summary")); + int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null); + Message message = messagesService.getMessage(mid2); + assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card") + .getAttribute("content"), equalTo("summary_large_image")); + assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "og:description") + .getAttribute("content"), + startsWith(StringEscapeUtils.escapeHtml4(MessageUtils.getMessageHashTags(message)))); + } + @Test + public void postMessageTests() throws Exception { + mockMvc.perform(post("/post").param("body", "yo")).andExpect(redirectedUrl("http://localhost/login")); + MvcResult loginResult = mockMvc.perform(post("/login") + .param("username", ugnichName) + .param("password", ugnichPassword)).andReturn(); + mockMvc.perform(post("/post") + .cookie(loginResult.getResponse().getCookies()) + .param("wrong_param", "yo")).andExpect(status().isBadRequest()); + mockMvc.perform(post("/post") + .cookie(loginResult.getResponse().getCookies()) + .param("body", "yo")).andExpect(status().isOk()); + mockMvc.perform(post("/post") + .cookie(loginResult.getResponse().getCookies()) + .param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isOk()); + mockMvc.perform(post("/post") + .cookie(loginResult.getResponse().getCookies()) + .param("img", "bad_url")).andExpect(status().isBadRequest()); + FileInputStream fi = new FileInputStream(new ClassPathResource("tagscloud.png").getFile()); + MockMultipartFile file = new MockMultipartFile("attach", fi); + mockMvc.perform(multipart("/post") + .file(file) + .cookie(loginResult.getResponse().getCookies())).andExpect(status().isOk()); + int mid = messagesService.createMessage(ugnich.getUid(), "dummy message", null, null); + mockMvc.perform(post("/comment") + .param("mid", String.valueOf(mid)) + .param("body", "yo")).andExpect(redirectedUrl("http://localhost/login")); + mockMvc.perform(post("/comment") + .cookie(loginResult.getResponse().getCookies()) + .param("wrong_param", "yo")).andExpect(status().isBadRequest()); + mockMvc.perform(post("/comment") + .cookie(loginResult.getResponse().getCookies()) + .param("mid", String.valueOf(mid)) + .param("wrong_param", "yo")).andExpect(status().isBadRequest()); + mockMvc.perform(post("/comment") + .cookie(loginResult.getResponse().getCookies()) + .param("mid", String.valueOf(mid)) + .param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isFound()); + mockMvc.perform(multipart("/comment") + .file(file) + .cookie(loginResult.getResponse().getCookies()) + .param("mid", String.valueOf(mid))).andExpect(status().isFound()); + mockMvc.perform(post("/comment") + .cookie(loginResult.getResponse().getCookies()) + .param("mid", String.valueOf(mid)) + .param("body", "yo")).andExpect(redirectedUrl(String.format("/%s/%d#%d", ugnichName, mid, 3))); + } + @Test + public void hashLoginShouldNotUseSession() throws Exception { + String hash = userService.getHashByUID(ugnich.getUid()); + MvcResult hashLoginResult = mockMvc.perform(get("/?show=my&hash=" + hash)) + .andExpect(status().isOk()) + .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) + .andExpect(content().string(containsString(hash))) + .andReturn(); + Cookie rememberMeFromHash = hashLoginResult.getResponse().getCookie("juick-remember-me"); + MvcResult formLoginResult = mockMvc.perform(post("/login") + .param("username", ugnichName) + .param("password", ugnichPassword)).andReturn(); + Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me"); + mockMvc.perform(get("/?show=my").cookie(rememberMeFromForm)).andExpect(status().isOk()) + .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) + .andExpect(content().string(containsString(hash))); + mockMvc.perform(get("/?show=my").cookie(rememberMeFromHash)).andExpect(status().isOk()) + .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) + .andExpect(content().string(containsString(hash))); + } + @Test + public void nonExistentBlogShouldReturn404() throws Exception { + mockMvc.perform(get("/ololoe/")).andExpect(status().isNotFound()); + } + @Test + public void discussionsShouldBePageableByTimestamp() throws Exception { + String msgText = "Привет, я снова Угнич"; + int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); + int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, null); + MvcResult loginResult = mockMvc.perform(post("/login") + .param("username", freefdName) + .param("password", freefdPassword)).andReturn(); + Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me"); + webClient.setCookieManager(new CookieManager()); + webClient.getCookieManager().addCookie( + new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), + loginCookie.getName(), + loginCookie.getValue())); + String discussionsUrl = "http://localhost:8080/?show=discuss"; + HtmlPage discussions = webClient.getPage(discussionsUrl); + assertThat(discussions.querySelectorAll("article").size(), is(0)); + subscriptionService.subscribeMessage(mid, freefd.getUid()); + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article").size(), is(1)); + subscriptionService.subscribeMessage(midNew, freefd.getUid()); + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article").size(), is(2)); + assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); + messagesService.createReply(mid, 0, freefd.getUid(), "I'm replied", null); + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article").size(), is(2)); + assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); + Message msg = messagesService.getMessage(mid); + HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "&to=" + msg.getUpdated().toEpochMilli()); + assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); + assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); + List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); + for (Integer m : newMids) { + subscriptionService.subscribeMessage(m, freefd.getUid()); + } + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article").size(), is(20)); + assertThat(discussions.querySelectorAll("article") + .get(19).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); + messagesService.createReply(midNew, 0, freefd.getUid(), "I'm replied", null); + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article") + .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); + Message old = messagesService.getMessage(newMids.get(0)); + discussionsOld = webClient.getPage(discussionsUrl + "&to=" + old.getUpdated().toEpochMilli()); + assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); + assertThat(discussionsOld.querySelectorAll("article") + .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); + } + @Test + public void redirectParamShouldCorrectlyRedirectLoggedUser() throws Exception { + MvcResult formLoginResult = mockMvc.perform(post("/login") + .param("username", ugnichName) + .param("password", ugnichPassword)).andReturn(); + Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me"); + mockMvc.perform(get("/login").cookie(rememberMeFromForm)) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/")); + mockMvc.perform(get("/login?redirect=false").cookie(rememberMeFromForm)) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/login/success")); + } + @Test + public void anythingRedirects() throws Exception { + int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null); + mockMvc.perform(get(String.format("/%d", mid))).andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid))); + } +} diff --git a/juick-www/src/test/java/com/juick/www/WebAppTests.java b/juick-www/src/test/java/com/juick/www/WebAppTests.java deleted file mode 100644 index c34fcea8..00000000 --- a/juick-www/src/test/java/com/juick/www/WebAppTests.java +++ /dev/null @@ -1,383 +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 . - */ - -package com.juick.www; - -import com.gargoylesoftware.htmlunit.CookieManager; -import com.gargoylesoftware.htmlunit.Page; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.css.StyleElement; -import com.gargoylesoftware.htmlunit.html.DomElement; -import com.gargoylesoftware.htmlunit.html.HtmlPage; -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; -import com.juick.configuration.DataConfiguration; -import com.juick.service.*; -import com.juick.util.MessageUtils; -import com.juick.www.configuration.SapeConfiguration; -import com.juick.www.configuration.WebSecurityConfig; -import com.juick.www.configuration.WwwAppConfiguration; -import com.mitchellbosecke.pebble.PebbleEngine; -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; -import org.apache.commons.text.StringEscapeUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.core.io.ClassPathResource; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.context.ContextConfiguration; -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 javax.inject.Inject; -import javax.servlet.http.Cookie; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.StreamSupport; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -/** - * Created by vitalyster on 12.01.2017. - */ -@RunWith(SpringRunner.class) -@AutoConfigureMockMvc -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -@TestPropertySource(properties = {"xmpp_disabled=true"}) - -public class WebAppTests { - @MockBean - private ImagesService imagesService; - @Inject - private WebApp webApp; - - @Inject - private MockMvc mockMvc; - @Inject - private WebClient webClient; - - @Inject - private UserService userService; - @Inject - private MessagesService messagesService; - @Inject - private PrivacyQueriesService privacyQueriesService; - @Inject - private JdbcTemplate jdbcTemplate; - @Inject - private SubscriptionService subscriptionService; - - @Inject - private PebbleEngine pebbleEngine; - @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") - private String imgPath; - - private static User ugnich, freefd; - private static String ugnichName, ugnichPassword, freefdName, freefdPassword; - - private static boolean isSetUp = false; - - @Before - public void setup() throws IOException { - if (!isSetUp) { - webClient.getOptions().setJavaScriptEnabled(false); - webClient.getOptions().setCssEnabled(false); - webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); - - ugnichName = "ugnich"; - ugnichPassword = "secret"; - freefdName = "freefd"; - freefdPassword = "MyPassw0rd!"; - - userService.createUser(ugnichName, ugnichPassword); - ugnich = userService.getUserByName(ugnichName); - int freefdId = userService.createUser(freefdName, freefdPassword); - freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); - - isSetUp = true; - } - Files.createDirectory(Paths.get(imgPath, "p")); - Files.createDirectory(Paths.get(imgPath, "photos-1024")); - Files.createDirectory(Paths.get(imgPath, "photos-512")); - Files.createDirectory(Paths.get(imgPath, "ps")); - } - - @After - public void teardown() throws IOException { - FileSystemUtils.deleteRecursively(Paths.get(imgPath, "p")); - FileSystemUtils.deleteRecursively(Paths.get(imgPath, "photos-1024")); - FileSystemUtils.deleteRecursively(Paths.get(imgPath, "photos-512")); - FileSystemUtils.deleteRecursively(Paths.get(imgPath, "ps")); - } - - @Test - public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception { - String msgText = "Привет, я - Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); - HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); - assertThat(threadPage.getTitleText(), equalTo("ugnich:")); - } - @Test - public void bannedUserBlogandPostShouldReturn404() throws IOException { - String userName = "isilmine"; - String userPassword = "secret"; - String msgText = "автор этого поста был забанен"; - String hash = "12345678"; - - User isilmine = userService.getUserByUID(userService.createUser(userName, userPassword)).orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, null); - jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", isilmine.getUid()); - Page blogPage = webClient.getPage("http://localhost:8080/isilmine"); - Page threadPage = webClient.getPage(String.format("http://localhost:8080/isilmine/%d", mid)); - assertThat(blogPage.getWebResponse().getStatusCode(), equalTo(404)); - assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(404)); - } - @Test - public void repliesList() throws IOException { - int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null); - IntStream.range(1, 15).forEach(i -> - messagesService.createReply(mid, i-1, freefd.getUid(), String.valueOf(i-1), null )); - - HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); - assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); - Long visibleItems = StreamSupport.stream(threadPage.getHtmlElementById("replies") - .getChildElements().spliterator(), false).filter(e -> { - StyleElement display = e.getStyleElement("display"); - return display == null || !display.getValue().equals("none"); - }).count(); - assertThat(visibleItems, equalTo(14L)); - } - @Test - public void userShouldNotSeeReplyButtonToBannedUser() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, null); - messagesService.createReply(mid, 0, ugnich.getUid(), "yo", null); - MvcResult loginResult = mockMvc.perform(post("/login") - .param("username", freefdName) - .param("password", freefdPassword)).andReturn(); - Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me"); - webClient.setCookieManager(new CookieManager()); - webClient.getCookieManager().addCookie( - new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), - loginCookie.getName(), - loginCookie.getValue())); - HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); - assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); - assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(false)); - assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(false)); - privacyQueriesService.blacklistUser(freefd, ugnich); - assertThat(userService.isInBLAny(freefd.getUid(), ugnich.getUid()), equalTo(true)); - int renhaId = userService.createUser("renha", "secret"); - messagesService.createReply(mid, 0, renhaId, "people", null); - threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); - assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); - assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(true)); - assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(true)); - } - @Test - public void correctTagsEscaping() throws PebbleException, IOException { - PebbleTemplate template = pebbleEngine.getTemplate("views/test"); - Writer writer = new StringWriter(); - template.evaluate(writer, - Collections.singletonMap("tagsList", - Collections.singletonList(StringEscapeUtils.escapeHtml4(new Tag(">_<").getName())))); - String output = writer.toString().trim(); - assertThat(output, equalTo(">_<")); - } - - public DomElement fetchMeta(String url, String name) throws IOException { - HtmlPage page = webClient.getPage(url); - DomElement emptyMeta = new DomElement("", "meta", null, null); - return page.getElementsByTagName("meta").stream() - .filter(t -> t.getAttribute("name").equals(name)).findFirst().orElse(emptyMeta); - } - @Test - public void testTwitterCards() throws Exception { - - int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, null); - - assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card") - .getAttribute("content"), equalTo("summary")); - int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null); - Message message = messagesService.getMessage(mid2); - assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card") - .getAttribute("content"), equalTo("summary_large_image")); - assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "og:description") - .getAttribute("content"), - startsWith(StringEscapeUtils.escapeHtml4(MessageUtils.getMessageHashTags(message)))); - } - @Test - public void postMessageTests() throws Exception { - mockMvc.perform(post("/post").param("body", "yo")).andExpect(redirectedUrl("http://localhost/login")); - MvcResult loginResult = mockMvc.perform(post("/login") - .param("username", ugnichName) - .param("password", ugnichPassword)).andReturn(); - mockMvc.perform(post("/post") - .cookie(loginResult.getResponse().getCookies()) - .param("wrong_param", "yo")).andExpect(status().isBadRequest()); - mockMvc.perform(post("/post") - .cookie(loginResult.getResponse().getCookies()) - .param("body", "yo")).andExpect(status().isOk()); - mockMvc.perform(post("/post") - .cookie(loginResult.getResponse().getCookies()) - .param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isOk()); - mockMvc.perform(post("/post") - .cookie(loginResult.getResponse().getCookies()) - .param("img", "bad_url")).andExpect(status().isBadRequest()); - FileInputStream fi = new FileInputStream(new ClassPathResource("tagscloud.png").getFile()); - MockMultipartFile file = new MockMultipartFile("attach", fi); - mockMvc.perform(multipart("/post") - .file(file) - .cookie(loginResult.getResponse().getCookies())).andExpect(status().isOk()); - int mid = messagesService.createMessage(ugnich.getUid(), "dummy message", null, null); - mockMvc.perform(post("/comment") - .param("mid", String.valueOf(mid)) - .param("body", "yo")).andExpect(redirectedUrl("http://localhost/login")); - mockMvc.perform(post("/comment") - .cookie(loginResult.getResponse().getCookies()) - .param("wrong_param", "yo")).andExpect(status().isBadRequest()); - mockMvc.perform(post("/comment") - .cookie(loginResult.getResponse().getCookies()) - .param("mid", String.valueOf(mid)) - .param("wrong_param", "yo")).andExpect(status().isBadRequest()); - mockMvc.perform(post("/comment") - .cookie(loginResult.getResponse().getCookies()) - .param("mid", String.valueOf(mid)) - .param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isFound()); - mockMvc.perform(multipart("/comment") - .file(file) - .cookie(loginResult.getResponse().getCookies()) - .param("mid", String.valueOf(mid))).andExpect(status().isFound()); - mockMvc.perform(post("/comment") - .cookie(loginResult.getResponse().getCookies()) - .param("mid", String.valueOf(mid)) - .param("body", "yo")).andExpect(redirectedUrl(String.format("/%s/%d#%d", ugnichName, mid, 3))); - } - @Test - public void hashLoginShouldNotUseSession() throws Exception { - String hash = userService.getHashByUID(ugnich.getUid()); - MvcResult hashLoginResult = mockMvc.perform(get("/?show=my&hash=" + hash)) - .andExpect(status().isOk()) - .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) - .andExpect(content().string(containsString(hash))) - .andReturn(); - Cookie rememberMeFromHash = hashLoginResult.getResponse().getCookie("juick-remember-me"); - MvcResult formLoginResult = mockMvc.perform(post("/login") - .param("username", ugnichName) - .param("password", ugnichPassword)).andReturn(); - Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me"); - mockMvc.perform(get("/?show=my").cookie(rememberMeFromForm)).andExpect(status().isOk()) - .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) - .andExpect(content().string(containsString(hash))); - mockMvc.perform(get("/?show=my").cookie(rememberMeFromHash)).andExpect(status().isOk()) - .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) - .andExpect(content().string(containsString(hash))); - } - @Test - public void nonExistentBlogShouldReturn404() throws Exception { - mockMvc.perform(get("/ololoe/")).andExpect(status().isNotFound()); - } - @Test - public void discussionsShouldBePageableByTimestamp() throws Exception { - String msgText = "Привет, я снова Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); - int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, null); - MvcResult loginResult = mockMvc.perform(post("/login") - .param("username", freefdName) - .param("password", freefdPassword)).andReturn(); - Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me"); - webClient.setCookieManager(new CookieManager()); - webClient.getCookieManager().addCookie( - new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), - loginCookie.getName(), - loginCookie.getValue())); - String discussionsUrl = "http://localhost:8080/?show=discuss"; - HtmlPage discussions = webClient.getPage(discussionsUrl); - assertThat(discussions.querySelectorAll("article").size(), is(0)); - subscriptionService.subscribeMessage(mid, freefd.getUid()); - discussions = (HtmlPage) discussions.refresh(); - assertThat(discussions.querySelectorAll("article").size(), is(1)); - subscriptionService.subscribeMessage(midNew, freefd.getUid()); - discussions = (HtmlPage) discussions.refresh(); - assertThat(discussions.querySelectorAll("article").size(), is(2)); - assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); - messagesService.createReply(mid, 0, freefd.getUid(), "I'm replied", null); - discussions = (HtmlPage) discussions.refresh(); - assertThat(discussions.querySelectorAll("article").size(), is(2)); - assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); - Message msg = messagesService.getMessage(mid); - HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "&to=" + msg.getUpdated().toEpochMilli()); - assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); - assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); - List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); - for (Integer m : newMids) { - subscriptionService.subscribeMessage(m, freefd.getUid()); - } - discussions = (HtmlPage) discussions.refresh(); - assertThat(discussions.querySelectorAll("article").size(), is(20)); - assertThat(discussions.querySelectorAll("article") - .get(19).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); - messagesService.createReply(midNew, 0, freefd.getUid(), "I'm replied", null); - discussions = (HtmlPage) discussions.refresh(); - assertThat(discussions.querySelectorAll("article") - .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); - Message old = messagesService.getMessage(newMids.get(0)); - discussionsOld = webClient.getPage(discussionsUrl + "&to=" + old.getUpdated().toEpochMilli()); - assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); - assertThat(discussionsOld.querySelectorAll("article") - .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); - } - @Test - public void redirectParamShouldCorrectlyRedirectLoggedUser() throws Exception { - MvcResult formLoginResult = mockMvc.perform(post("/login") - .param("username", ugnichName) - .param("password", ugnichPassword)).andReturn(); - Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me"); - mockMvc.perform(get("/login").cookie(rememberMeFromForm)) - .andExpect(status().is3xxRedirection()) - .andExpect(redirectedUrl("/")); - mockMvc.perform(get("/login?redirect=false").cookie(rememberMeFromForm)) - .andExpect(status().is3xxRedirection()) - .andExpect(redirectedUrl("/login/success")); - } - @Test - public void anythingRedirects() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null); - mockMvc.perform(get(String.format("/%d", mid))).andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid))); - } -} -- cgit v1.2.3