diff --git a/.travis.yml b/.travis.yml index 9974add..0bf31fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,9 +14,7 @@ before_install: - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list install: - sudo apt-get update && sudo apt-get install yarn - - cd src/main/web - - yarn && yarn run build - - cd - + - ./gradlew build script: - ./gradlew check sudo: required diff --git a/README.md b/README.md index 9af1c65..5cb331e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # Meow Web Project [![Build Status](https://travis-ci.org/sjtu-meow/web.svg?branch=master)](https://travis-ci.org/sjtu-meow/web) + +## Deploy +``` +./gradlew buildDocker +docker-compose up +``` diff --git a/build.gradle b/build.gradle index 7c35890..7857869 100644 --- a/build.gradle +++ b/build.gradle @@ -81,6 +81,7 @@ dependencies { compile group: 'com.googlecode.owasp-java-html-sanitizer', name: 'owasp-java-html-sanitizer', version: 'r239' + compile 'org.jsoup:jsoup:1.10.3' } diff --git a/src/main/java/me/sjtumeow/meow/model/Answer.java b/src/main/java/me/sjtumeow/meow/model/Answer.java index d0c66a4..d06ae67 100644 --- a/src/main/java/me/sjtumeow/meow/model/Answer.java +++ b/src/main/java/me/sjtumeow/meow/model/Answer.java @@ -1,17 +1,20 @@ package me.sjtumeow.meow.model; -import com.fasterxml.jackson.annotation.JsonBackReference; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import org.hibernate.annotations.Formula; +import com.fasterxml.jackson.annotation.JsonBackReference; + @Entity public class Answer extends Item { private static final long serialVersionUID = 1L; + @Column(columnDefinition = "TEXT") private String content; @JsonBackReference diff --git a/src/main/java/me/sjtumeow/meow/model/Article.java b/src/main/java/me/sjtumeow/meow/model/Article.java index b72f2bc..6c33141 100644 --- a/src/main/java/me/sjtumeow/meow/model/Article.java +++ b/src/main/java/me/sjtumeow/meow/model/Article.java @@ -1,5 +1,6 @@ package me.sjtumeow.meow.model; +import javax.persistence.Column; import javax.persistence.Entity; @Entity @@ -7,7 +8,10 @@ public class Article extends Item { private static final long serialVersionUID = 1L; - String title, summary, content, cover; + String title, summary, cover; + + @Column(columnDefinition = "TEXT") + String content; public Article() { type = Item.ITEM_TYPE_ARTICLE; diff --git a/src/main/java/me/sjtumeow/meow/util/LocalDateAttributeConverter.java b/src/main/java/me/sjtumeow/meow/util/LocalDateAttributeConverter.java new file mode 100644 index 0000000..01b900a --- /dev/null +++ b/src/main/java/me/sjtumeow/meow/util/LocalDateAttributeConverter.java @@ -0,0 +1,21 @@ +package me.sjtumeow.meow.util; + +import java.sql.Timestamp; +import java.time.LocalDateTime; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +@Converter(autoApply = true) +public class LocalDateAttributeConverter implements AttributeConverter { + + @Override + public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) { + return (localDateTime == null ? null : Timestamp.valueOf(localDateTime)); + } + + @Override + public LocalDateTime convertToEntityAttribute(Timestamp timestamp) { + return (timestamp == null ? null : timestamp.toLocalDateTime()); + } +} \ No newline at end of file diff --git a/src/main/java/me/sjtumeow/meow/util/LocalDateTimeAttributeConverter.java b/src/main/java/me/sjtumeow/meow/util/LocalDateTimeAttributeConverter.java new file mode 100644 index 0000000..e0062e4 --- /dev/null +++ b/src/main/java/me/sjtumeow/meow/util/LocalDateTimeAttributeConverter.java @@ -0,0 +1,25 @@ +package me.sjtumeow.meow.util; + +import java.sql.Date; +import java.time.LocalDate; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +/** + * Custom converter class that enables usage of LocalDateTime with the DB + */ + +@Converter(autoApply = true) +public class LocalDateTimeAttributeConverter implements AttributeConverter { + + @Override + public Date convertToDatabaseColumn(LocalDate localDate) { + return (localDate == null ? null : Date.valueOf(localDate)); + } + + @Override + public LocalDate convertToEntityAttribute(Date date) { + return (date == null ? null : date.toLocalDate()); + } +} \ No newline at end of file diff --git a/src/main/java/me/sjtumeow/meow/util/StringUtil.java b/src/main/java/me/sjtumeow/meow/util/StringUtil.java index 9c829c0..58d80b5 100644 --- a/src/main/java/me/sjtumeow/meow/util/StringUtil.java +++ b/src/main/java/me/sjtumeow/meow/util/StringUtil.java @@ -1,13 +1,8 @@ package me.sjtumeow.meow.util; -import java.io.StringReader; - -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathFactory; - +import org.jsoup.Jsoup; import org.owasp.html.PolicyFactory; import org.owasp.html.Sanitizers; -import org.xml.sax.InputSource; public class StringUtil { @@ -24,14 +19,13 @@ public static String filterRichText(String html) { } public static String extractHTMLSummary(String html) { + String text = ""; try { - XPathFactory factory = XPathFactory.newInstance(); - XPath xPath = factory.newXPath(); - InputSource source = new InputSource(new StringReader("" + html + "")); - return xPath.evaluate("/body/p", source); + text = Jsoup.parse("" + html + "", "UTF-8").select("body").text(); } catch (Exception e) { - return ""; + e.printStackTrace(); } + return text.length() > 100 ? text.substring(0, 100) : text; } public static String wrapLikeSubstr(String keyword) { diff --git a/src/test/java/me/sjtumeow/meow/ApiTest.java b/src/test/java/me/sjtumeow/meow/ApiTest.java new file mode 100644 index 0000000..1e7c088 --- /dev/null +++ b/src/test/java/me/sjtumeow/meow/ApiTest.java @@ -0,0 +1,29 @@ +package me.sjtumeow.meow; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class ApiTest { + + /*private MockMvc mockMvc; + @Autowired + private WebApplicationContext context;*/ + + @Before + public void setupMockMvc() { + //mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); + + } + + @Test + public void testLogin() throws Exception { + //MockHttpServletRequestBuilder builder = new MockHttpServletRequestBuilder(null, null); + //mockMvc.perform(builder).andReturn(); + // TODO + } +} diff --git a/src/test/java/me/sjtumeow/meow/authorization/manager/impl/RedisTokenManagerTest.java b/src/test/java/me/sjtumeow/meow/authorization/manager/impl/RedisTokenManagerTest.java new file mode 100644 index 0000000..922ae96 --- /dev/null +++ b/src/test/java/me/sjtumeow/meow/authorization/manager/impl/RedisTokenManagerTest.java @@ -0,0 +1,41 @@ +package me.sjtumeow.meow.authorization.manager.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import me.sjtumeow.meow.authorization.manager.TokenManager; +import me.sjtumeow.meow.authorization.model.TokenModel; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class RedisTokenManagerTest { + @Autowired + TokenManager tokenManager; + + @Test + public void checkToken() throws Exception { + Long userId = 1L; + TokenModel token = tokenManager.createToken(userId); + assertTrue(tokenManager.checkToken(token)); + tokenManager.deleteToken(userId); + assertFalse(tokenManager.checkToken(token)); + } + + @Test + public void getToken() throws Exception { + String userId = "123"; + String tokenString = "tokenString"; + String auth = userId + '_' + tokenString; + TokenModel token = tokenManager.getToken(auth); + assertEquals(Long.valueOf(userId), Long.valueOf(token.getUserId())); + assertEquals(tokenString, token.getToken()); + } + +} \ No newline at end of file diff --git a/src/test/java/me/sjtumeow/meow/service/InteractionServiceTest.java b/src/test/java/me/sjtumeow/meow/service/InteractionServiceTest.java index 6ad52c8..19c74f6 100644 --- a/src/test/java/me/sjtumeow/meow/service/InteractionServiceTest.java +++ b/src/test/java/me/sjtumeow/meow/service/InteractionServiceTest.java @@ -4,8 +4,6 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - public class InteractionServiceTest { @Before public void setUp() throws Exception { diff --git a/src/test/java/me/sjtumeow/meow/service/PushServiceTest.java b/src/test/java/me/sjtumeow/meow/service/PushServiceTest.java index e981330..c137669 100644 --- a/src/test/java/me/sjtumeow/meow/service/PushServiceTest.java +++ b/src/test/java/me/sjtumeow/meow/service/PushServiceTest.java @@ -1,22 +1,39 @@ package me.sjtumeow.meow.service; -import org.junit.Assert; +import static org.junit.Assert.assertNotNull; + import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; -import static org.junit.Assert.*; +import me.sjtumeow.meow.model.form.PushForm; +import me.sjtumeow.meow.model.result.CreateResult; +@SpringBootTest +@RunWith(SpringRunner.class) public class PushServiceTest { - @Test - public void findAll() throws Exception { - } + @Autowired + PushService pushService; @Test - public void findAllPageable() throws Exception { + public void findAll() throws Exception { + /*List result = pushService.findAll("99"); + assertEquals(4, result.length());*/ } @Test public void create() throws Exception { + PushForm pf = new PushForm(); + pf.setItemId(1L); + pf.setItemType(1); + String message = "Test Push"; + pf.setText(message); + CreateResult result = pushService.create(pf); + + assertNotNull(result); } } \ No newline at end of file diff --git a/src/test/java/me/sjtumeow/meow/util/FormatValidatorTest.java b/src/test/java/me/sjtumeow/meow/util/FormatValidatorTest.java index 47e4b57..4df4be3 100644 --- a/src/test/java/me/sjtumeow/meow/util/FormatValidatorTest.java +++ b/src/test/java/me/sjtumeow/meow/util/FormatValidatorTest.java @@ -1,42 +1,71 @@ package me.sjtumeow.meow.util; -import java.text.Normalizer; -import javax.validation.constraints.AssertTrue; -import org.junit.Test; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; -import static org.junit.Assert.*; +import org.junit.Test; public class FormatValidatorTest { @Test public void checkNonNegativeInt() throws Exception { + assertFalse(FormatValidator.checkNonNegativeInt(null)); + assertTrue(FormatValidator.checkNonNegativeInt(100)); + assertTrue(FormatValidator.checkNonNegativeInt(0)); + assertFalse(FormatValidator.checkNonNegativeInt(-33)); } @Test public void checkPositiveInt() throws Exception { + assertFalse(FormatValidator.checkPositiveInt(null)); + assertTrue(FormatValidator.checkPositiveInt(100)); + assertFalse(FormatValidator.checkPositiveInt(0)); + assertFalse(FormatValidator.checkPositiveInt(-33)); } @Test public void checkPhone() throws Exception { + assertFalse(FormatValidator.checkPhone(null)); assertFalse(FormatValidator.checkPhone("123")); + assertFalse(FormatValidator.checkPhone("123xxxxyyyy")); assertTrue(FormatValidator.checkPhone("12333333333")); } @Test public void checkPassword() throws Exception { + assertFalse(FormatValidator.checkPassword(null)); assertTrue(FormatValidator.checkPassword("password")); + assertTrue(FormatValidator.checkPassword("666666")); assertFalse(FormatValidator.checkPassword("123")); } @Test public void checkSmsCode() throws Exception { + assertFalse(FormatValidator.checkSmsCode(null)); + assertFalse(FormatValidator.checkSmsCode("hacode")); + assertTrue(FormatValidator.checkSmsCode("666666")); } @Test public void checkMainItemType() throws Exception { + assertFalse(FormatValidator.checkMainItemType(null)); + assertFalse(FormatValidator.checkMainItemType(-1)); + assertTrue(FormatValidator.checkMainItemType(0)); + assertTrue(FormatValidator.checkMainItemType(1)); + assertTrue(FormatValidator.checkMainItemType(2)); + assertTrue(FormatValidator.checkMainItemType(3)); + assertFalse(FormatValidator.checkMainItemType(4)); } @Test public void checkItemType() throws Exception { + assertFalse(FormatValidator.checkItemType(null)); + assertFalse(FormatValidator.checkItemType(-1)); + assertTrue(FormatValidator.checkItemType(0)); + assertTrue(FormatValidator.checkItemType(1)); + assertTrue(FormatValidator.checkItemType(2)); + assertTrue(FormatValidator.checkItemType(3)); + assertTrue(FormatValidator.checkItemType(4)); + assertFalse(FormatValidator.checkItemType(5)); } } \ No newline at end of file diff --git a/src/test/java/me/sjtumeow/meow/util/StringUtilTest.java b/src/test/java/me/sjtumeow/meow/util/StringUtilTest.java index 8a0f08b..f377cca 100644 --- a/src/test/java/me/sjtumeow/meow/util/StringUtilTest.java +++ b/src/test/java/me/sjtumeow/meow/util/StringUtilTest.java @@ -1,9 +1,11 @@ package me.sjtumeow.meow.util; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; -import static org.junit.Assert.*; +import org.junit.Test; public class StringUtilTest { @Test @@ -15,20 +17,32 @@ public void replaceNull() throws Exception { @Test public void filterRichText() throws Exception { - String html = ""; - assertEquals("", StringUtil.filterRichText(html)); - String str = "str"; - assertEquals(str, StringUtil.filterRichText(str)); + assertEquals("", StringUtil.filterRichText("")); + assertEquals("text", StringUtil.filterRichText("text")); + assertEquals("

blocks

", StringUtil.filterRichText("

blocks

")); + assertEquals("formatting", StringUtil.filterRichText("formatting")); + assertEquals("", + StringUtil.filterRichText("")); + assertEquals("
styled div
", + StringUtil.filterRichText("
styled div
")); } @Test public void extractHTMLSummary() throws Exception { + assertEquals("paragraph 1 paragraph 2", StringUtil.extractHTMLSummary("

paragraph 1

paragraph 2

")); + assertEquals( + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", + StringUtil.extractHTMLSummary( + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890aaaabbbb")); + assertEquals("some text without tags", + StringUtil.extractHTMLSummary("some text without tags")); + assertEquals("malformed data", StringUtil.extractHTMLSummary("

malformed data")); } @Test public void wrapLikeSubstr() throws Exception { - String keyword = "str"; - assertEquals("%str%",StringUtil.wrapLikeSubstr(keyword)); + assertEquals("%str%", StringUtil.wrapLikeSubstr("str")); + assertEquals("%\\_99\\%%", StringUtil.wrapLikeSubstr("_99%")); } @Test