Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sjtu-meow/web
Browse files Browse the repository at this point in the history
  • Loading branch information
chuzhe-as-a-dev committed Sep 13, 2017
2 parents 678a25f + 0b02d0c commit 5e6ad70
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 37 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/me/sjtumeow/meow/model/Answer.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/me/sjtumeow/meow/model/Article.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package me.sjtumeow.meow.model;

import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LocalDateTime, Timestamp> {

@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());
}
}
Original file line number Diff line number Diff line change
@@ -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<LocalDate, Date> {

@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());
}
}
16 changes: 5 additions & 11 deletions src/main/java/me/sjtumeow/meow/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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("<body>" + html + "</body>"));
return xPath.evaluate("/body/p", source);
text = Jsoup.parse("<body>" + html + "</body>", "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) {
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/me/sjtumeow/meow/ApiTest.java
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 23 additions & 6 deletions src/test/java/me/sjtumeow/meow/service/PushServiceTest.java
Original file line number Diff line number Diff line change
@@ -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<PushArchive> 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);
}

}
37 changes: 33 additions & 4 deletions src/test/java/me/sjtumeow/meow/util/FormatValidatorTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
Loading

0 comments on commit 5e6ad70

Please sign in to comment.