从"Blog"仓库中分离出来
This commit is contained in:
97
src/test/java/cn/celess/blog/BaseTest.java
Normal file
97
src/test/java/cn/celess/blog/BaseTest.java
Normal file
@@ -0,0 +1,97 @@
|
||||
package cn.celess.blog;
|
||||
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
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.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultHandler;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
* @Date: 2019/08/22 12:46
|
||||
* @Description: 测试基类
|
||||
*/
|
||||
@SpringBootTest
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("test")
|
||||
public class BaseTest {
|
||||
|
||||
protected MockMvc mockMvc;
|
||||
protected final static String Code = "code";
|
||||
protected final static String Result = "result";
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
System.out.println("==========> 开始测试 <=========");
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
System.out.println("==========> 测试结束 <=========");
|
||||
}
|
||||
|
||||
|
||||
protected String adminLogin() {
|
||||
try {
|
||||
LoginReq req = new LoginReq();
|
||||
req.setEmail("a@celess.cn");
|
||||
req.setPassword("123456789");
|
||||
req.setIsRememberMe(false);
|
||||
JSONObject loginReq = JSONObject.fromObject(req);
|
||||
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
|
||||
// .andDo(MockMvcResultHandlers.print())
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||
assertNotNull(token);
|
||||
return token;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected String userLogin() {
|
||||
try {
|
||||
LoginReq req = new LoginReq();
|
||||
req.setEmail("zh56462271@qq.com");
|
||||
req.setPassword("123456789");
|
||||
req.setIsRememberMe(false);
|
||||
JSONObject loginReq = JSONObject.fromObject(req);
|
||||
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
|
||||
// .andDo(MockMvcResultHandlers.print())
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||
assertNotNull(token);
|
||||
return token;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Article;
|
||||
import cn.celess.blog.entity.Response;
|
||||
import cn.celess.blog.entity.model.ArticleModel;
|
||||
import cn.celess.blog.entity.request.ArticleReq;
|
||||
import cn.celess.blog.mapper.ArticleMapper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
|
||||
public class ArticleControllerTest extends BaseTest {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
|
||||
@Test
|
||||
public void create() {
|
||||
ArticleReq articleReq = new ArticleReq();
|
||||
// 应该正常通过
|
||||
articleReq.setTitle("test-" + UUID.randomUUID().toString());
|
||||
articleReq.setMdContent("# test title");
|
||||
articleReq.setCategory("随笔");
|
||||
articleReq.setTags("test,SpringMvc");
|
||||
articleReq.setOpen(true);
|
||||
articleReq.setType(true);
|
||||
articleReq.setUrl("http://xxxx.com");
|
||||
JSONObject jsonObject = JSONObject.fromObject(articleReq);
|
||||
|
||||
try {
|
||||
// 未登录
|
||||
mockMvc.perform(post("/admin/article/create")
|
||||
.content(jsonObject.toString())
|
||||
.contentType("application/json"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(),
|
||||
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)
|
||||
);
|
||||
});
|
||||
// User权限
|
||||
String token = userLogin();
|
||||
mockMvc.perform(post("/admin/article/create")
|
||||
.content(jsonObject.toString())
|
||||
.contentType("application/json")
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(PERMISSION_ERROR.getCode(),
|
||||
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)
|
||||
);
|
||||
});
|
||||
|
||||
// Admin权限
|
||||
token = adminLogin();
|
||||
mockMvc.perform(post("/admin/article/create")
|
||||
.content(jsonObject.toString())
|
||||
.contentType("application/json")
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
ArticleModel articleModel = (ArticleModel) JSONObject.toBean(object.getJSONObject(Result), ArticleModel.class);
|
||||
assertNotNull(articleModel.getId());
|
||||
assertNotNull(articleModel.getTitle());
|
||||
assertNotNull(articleModel.getSummary());
|
||||
assertNotNull(articleModel.getOriginal());
|
||||
assertNotNull(articleModel.getTags());
|
||||
assertNotNull(articleModel.getCategory());
|
||||
assertNotNull(articleModel.getPublishDateFormat());
|
||||
assertNotNull(articleModel.getMdContent());
|
||||
assertNotNull(articleModel.getNextArticleId());
|
||||
assertNotNull(articleModel.getNextArticleTitle());
|
||||
assertNotNull(articleModel.getPreArticleId());
|
||||
assertNotNull(articleModel.getPreArticleTitle());
|
||||
assertNotNull(articleModel.getOpen());
|
||||
assertNotNull(articleModel.getReadingNumber());
|
||||
assertNotNull(articleModel.getAuthorName());
|
||||
assertNotNull(articleModel.getUrl());
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
long articleId = articleMapper.getLastestArticleId();
|
||||
|
||||
try {
|
||||
// 未登录删除文章
|
||||
mockMvc.perform(MockMvcRequestBuilders.delete("/admin/article/del?articleID=" + articleId)
|
||||
).andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(),
|
||||
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)
|
||||
);
|
||||
});
|
||||
// user 权限删除文章
|
||||
String token = userLogin();
|
||||
mockMvc.perform(MockMvcRequestBuilders.delete("/admin/article/del?articleID=" + articleId)
|
||||
.header("Authorization", token))
|
||||
.andDo(result -> assertEquals(PERMISSION_ERROR.getCode(),
|
||||
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
||||
);
|
||||
// admin 权限删除文章
|
||||
token = adminLogin();
|
||||
mockMvc.perform(MockMvcRequestBuilders.delete("/admin/article/del?articleID=" + articleId)
|
||||
.header("Authorization", token))
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
// 断言删除成功
|
||||
assertTrue(object.getBoolean(Result));
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Article article = articleMapper.getLastestArticle();
|
||||
ArticleReq articleReq = new ArticleReq();
|
||||
articleReq.setId(article.getId());
|
||||
articleReq.setUrl("http://www.test.test");
|
||||
articleReq.setType(!article.getType());
|
||||
articleReq.setCategory("test");
|
||||
articleReq.setMdContent("test-" + article.getMdContent());
|
||||
articleReq.setOpen(!article.getOpen());
|
||||
articleReq.setTags("tag");
|
||||
articleReq.setTitle("test-" + article.getTitle());
|
||||
try {
|
||||
mockMvc.perform(put("/admin/article/update")
|
||||
.content(JSONObject.fromObject(articleReq).toString())
|
||||
.contentType("application/json"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// User 权限
|
||||
String token = userLogin();
|
||||
mockMvc.perform(put("/admin/article/update")
|
||||
.content(JSONObject.fromObject(articleReq).toString())
|
||||
.contentType("application/json")
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// Admin 权限
|
||||
token = adminLogin();
|
||||
mockMvc.perform(put("/admin/article/update")
|
||||
.content(JSONObject.fromObject(articleReq).toString())
|
||||
.contentType("application/json")
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), jsonObject.getInt(Code));
|
||||
ArticleModel a = (ArticleModel) JSONObject.toBean(jsonObject.getJSONObject(Result), ArticleModel.class);
|
||||
assertEquals(articleReq.getCategory(), a.getCategory());
|
||||
assertEquals(articleReq.getUrl(), a.getUrl());
|
||||
assertEquals(articleReq.getMdContent(), a.getMdContent());
|
||||
assertEquals(articleReq.getTitle(), a.getTitle());
|
||||
assertEquals(articleReq.getType(), a.getOriginal());
|
||||
// Tag 暂时不支持更新
|
||||
// assertEquals(articleReq.getTags(), Arrays.toString(a.getTags()).replaceAll(" ", "".replace("[", "".replace("]", ""))));
|
||||
assertEquals(articleReq.getOpen(), a.getOpen());
|
||||
assertEquals(articleReq.getId(), a.getId());
|
||||
});
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveOneById() {
|
||||
try {
|
||||
long articleID = 3;
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/article/articleID/" + articleID))
|
||||
.andExpect(status().is(200));
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/article/articleID/" + articleID + "?update=true"))
|
||||
.andExpect(status().is(200));
|
||||
|
||||
// 文章不存在
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/article/articleID/-1"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(ARTICLE_NOT_EXIST.getCode(), jsonObject.getInt(Code));
|
||||
});
|
||||
|
||||
// 正常情况
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/article/articleID/" + articleID + "?update=false"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
JSONObject articleJson = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
// 断言获取数据成功
|
||||
assertEquals(SUCCESS.getCode(), articleJson.getInt(Code));
|
||||
assertNotNull(articleJson.getJSONObject(Result));
|
||||
|
||||
ArticleModel a = (ArticleModel) JSONObject.toBean(articleJson.getJSONObject(Result), ArticleModel.class);
|
||||
assertNotNull(a.getTitle());
|
||||
assertNotNull(a.getId());
|
||||
assertNotNull(a.getSummary());
|
||||
assertNotNull(a.getMdContent());
|
||||
assertNotNull(a.getUrl());
|
||||
assertNotNull(a.getUpdateDateFormat());
|
||||
assertNotNull(a.getPreArticleId());
|
||||
assertNotNull(a.getPreArticleId());
|
||||
assertNotNull(a.getNextArticleId());
|
||||
assertNotNull(a.getNextArticleTitle());
|
||||
assertNotNull(a.getReadingNumber());
|
||||
// assertNotNull(a.getOpen());
|
||||
assertNotNull(a.getOriginal());
|
||||
assertNotNull(a.getPublishDateFormat());
|
||||
assertNotNull(a.getCategory());
|
||||
assertNotNull(a.getTags());
|
||||
assertNotNull(a.getAuthorName());
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void articles() {
|
||||
try {
|
||||
// 测试不带参数访问
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/articles"))
|
||||
.andExpect(status().is(200));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/articles?page=1&count=5"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
JSONObject articlesJSON = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
Response response = (Response) JSONObject.toBean(articlesJSON, Response.class);
|
||||
// 断言获取数据成功
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
// 结果集非空
|
||||
assertNotNull(response.getResult());
|
||||
// 判断pageInfo是否包装完全
|
||||
JSONObject resultJson = JSONObject.fromObject(response.getResult());
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(resultJson, PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getTotal());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(5, pageInfo.getPageSize());
|
||||
// 内容完整
|
||||
for (Object arc : pageInfo.getList()) {
|
||||
ArticleModel a = (ArticleModel) JSONObject.toBean(JSONObject.fromObject(arc), ArticleModel.class);
|
||||
assertNotNull(a.getTitle());
|
||||
assertNotNull(a.getId());
|
||||
assertNotNull(a.getSummary());
|
||||
assertNotNull(a.getOriginal());
|
||||
assertNotNull(a.getPublishDateFormat());
|
||||
assertNotNull(a.getCategory());
|
||||
assertNotNull(a.getTags());
|
||||
assertNotNull(a.getAuthorName());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adminArticles() {
|
||||
String token;
|
||||
try {
|
||||
// 未登录
|
||||
mockMvc.perform(get("/admin/articles?page=1&count=10"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(),
|
||||
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)
|
||||
);
|
||||
});
|
||||
|
||||
// User权限登陆
|
||||
token = userLogin();
|
||||
mockMvc.perform(get("/admin/articles?page=1&count=10")
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(PERMISSION_ERROR.getCode(), object.getInt(Code));
|
||||
});
|
||||
|
||||
token = adminLogin();
|
||||
// admin权限登陆
|
||||
mockMvc.perform(get("/admin/articles?page=1&count=10")
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject adminLogin = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), adminLogin.getInt(Code));
|
||||
assertNotNull(adminLogin.getString(Result));
|
||||
// 判断pageInfo是否包装完全
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(adminLogin.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getTotal());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
// 内容完整
|
||||
for (Object arc : pageInfo.getList()) {
|
||||
ArticleModel a = (ArticleModel) JSONObject.toBean(JSONObject.fromObject(arc), ArticleModel.class);
|
||||
assertNotNull(a.getTitle());
|
||||
assertNotNull(a.getId());
|
||||
assertNotNull(a.getOriginal());
|
||||
assertNotNull(a.getPublishDateFormat());
|
||||
assertNotNull(a.getOpen());
|
||||
assertNotNull(a.getReadingNumber());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByCategory() {
|
||||
try {
|
||||
// 分类不存在
|
||||
String categoryName = "NoSuchCategory";
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/articles/category/" + categoryName + "?page=1&count=10"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
assertEquals(CATEGORY_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// 正常查询
|
||||
categoryName = "linux";
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/articles/category/" + categoryName + "?page=1&count=10"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), jsonObject.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(jsonObject.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getTotal());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
for (Object arc : pageInfo.getList()) {
|
||||
JSONObject jsonObject1 = JSONObject.fromObject(arc);
|
||||
assertNotEquals(0, jsonObject1.getInt("id"));
|
||||
assertNotNull(jsonObject1.getString("title"));
|
||||
assertNotNull(jsonObject1.getString("summary"));
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByTag() {
|
||||
try {
|
||||
// 分类不存在
|
||||
String tagName = "NoSuchTag";
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/articles/tag/" + tagName + "?page=1&count=10"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
assertEquals(TAG_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// 正常查询
|
||||
tagName = "linux";
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/articles/tag/" + tagName + "?page=1&count=10"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), jsonObject.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(jsonObject.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getTotal());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
|
||||
for (Object arc : pageInfo.getList()) {
|
||||
JSONObject jsonObject1 = JSONObject.fromObject(arc);
|
||||
assertNotEquals(0, jsonObject1.getInt("id"));
|
||||
assertNotNull(jsonObject1.getString("title"));
|
||||
assertNotNull(jsonObject1.getString("summary"));
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Category;
|
||||
import cn.celess.blog.mapper.CategoryMapper;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
|
||||
public class CategoryControllerTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
|
||||
@Test
|
||||
public void addOne() throws Exception {
|
||||
String categoryName = UUID.randomUUID().toString().substring(0, 4);
|
||||
System.out.println("categoryName: ==> " + categoryName);
|
||||
// 未登录
|
||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)).andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// User权限
|
||||
String token = userLogin();
|
||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// Admin权限
|
||||
token = adminLogin();
|
||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
Category category = (Category) JSONObject.toBean(object.getJSONObject(Result), Category.class);
|
||||
assertEquals(categoryName, category.getName());
|
||||
assertNotNull(category.getId());
|
||||
assertNotNull(category.getArticles());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteOne() throws Exception {
|
||||
Category category = categoryMapper.getLastestCategory();
|
||||
// 未登录
|
||||
mockMvc.perform(delete("/admin/category/del?id=" + category.getId())).andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// User权限
|
||||
String token = userLogin();
|
||||
mockMvc.perform(delete("/admin/category/del?id=" + category.getId())
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// Admin权限
|
||||
token = adminLogin();
|
||||
mockMvc.perform(delete("/admin/category/del?id=" + category.getId())
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertTrue(object.getBoolean(Result));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateOne() throws Exception {
|
||||
Category category = categoryMapper.getLastestCategory();
|
||||
String name = UUID.randomUUID().toString().substring(0, 4);
|
||||
// 未登录
|
||||
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)).andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// User权限
|
||||
String token = userLogin();
|
||||
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// Admin权限
|
||||
token = adminLogin();
|
||||
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
Category c = (Category) JSONObject.toBean(object.getJSONObject(Result), Category.class);
|
||||
assertEquals(name, c.getName());
|
||||
assertNotNull(c.getArticles());
|
||||
assertNotNull(c.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPage() throws Exception {
|
||||
mockMvc.perform(get("/categories")).andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
JSONArray jsonArray = object.getJSONArray(Result);
|
||||
assertNotNull(jsonArray);
|
||||
jsonArray.forEach(o -> {
|
||||
Category c = (Category) JSONObject.toBean(JSONObject.fromObject(o), Category.class);
|
||||
assertNotNull(c.getName());
|
||||
assertNotNull(c.getId());
|
||||
assertNotNull(c.getArticles());
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Article;
|
||||
import cn.celess.blog.entity.Comment;
|
||||
import cn.celess.blog.entity.model.CommentModel;
|
||||
import cn.celess.blog.entity.request.CommentReq;
|
||||
import cn.celess.blog.mapper.ArticleMapper;
|
||||
import cn.celess.blog.mapper.CommentMapper;
|
||||
import cn.celess.blog.mapper.UserMapper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
public class CommentControllerTest extends BaseTest {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
@Autowired
|
||||
CommentMapper commentMapper;
|
||||
|
||||
@Test
|
||||
public void addOne() throws Exception {
|
||||
CommentReq commentReq = new CommentReq();
|
||||
// 测试留言
|
||||
commentReq.setArticleID(null);
|
||||
commentReq.setComment(false);
|
||||
commentReq.setContent(UUID.randomUUID().toString());
|
||||
commentReq.setPid(-1L);
|
||||
commentReq.setResponseId(null);
|
||||
String token = userLogin();
|
||||
CommentModel PC = null;
|
||||
mockMvc.perform(post("/user/comment/create")
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.content(JSONObject.fromObject(commentReq).toString())
|
||||
.header("Authorization", token)
|
||||
).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
|
||||
assertNotEquals(0, model.getId());
|
||||
assertEquals(commentReq.getPid().longValue(), model.getPid());
|
||||
assertEquals(-1, model.getPid());
|
||||
assertEquals(commentReq.getComment(), model.isComment());
|
||||
assertEquals(commentReq.getContent(), model.getContent());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
});
|
||||
|
||||
Article article = articleMapper.getLastestArticle();
|
||||
// 测试评论
|
||||
commentReq.setArticleID(article.getId());
|
||||
commentReq.setComment(true);
|
||||
commentReq.setContent(UUID.randomUUID().toString());
|
||||
commentReq.setPid(-1L);
|
||||
commentReq.setResponseId(null);
|
||||
mockMvc.perform(post("/user/comment/create")
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.content(JSONObject.fromObject(commentReq).toString())
|
||||
.header("Authorization", token)
|
||||
).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
|
||||
// 响应数据的完整性
|
||||
assertNotEquals(0, model.getId());
|
||||
assertEquals(commentReq.getPid().longValue(), model.getPid());
|
||||
assertEquals(-1, model.getPid());
|
||||
assertEquals(commentReq.getComment(), model.isComment());
|
||||
assertEquals(commentReq.getContent(), model.getContent());
|
||||
assertEquals(commentReq.getArticleID().longValue(), model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
});
|
||||
|
||||
// 测试二级回复
|
||||
Comment lastestComment = commentMapper.getLastestComment();
|
||||
commentReq.setArticleID(lastestComment.getArticleID());
|
||||
commentReq.setComment(lastestComment.getType());
|
||||
commentReq.setContent(UUID.randomUUID().toString());
|
||||
commentReq.setPid(lastestComment.getId());
|
||||
commentReq.setResponseId(null);
|
||||
mockMvc.perform(post("/user/comment/create")
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.content(JSONObject.fromObject(commentReq).toString())
|
||||
.header("Authorization", token)
|
||||
).andDo(MockMvcResultHandlers.print()).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
|
||||
// 重新获取父评论信息
|
||||
Comment pCommon = commentMapper.findCommentById(lastestComment.getId());
|
||||
assertEquals(pCommon.getId().longValue(), model.getPid());
|
||||
// 判断父评论中是否有写入当前新增的评论的id
|
||||
String[] ids = pCommon.getResponseId().split(",");
|
||||
boolean contain = false;
|
||||
for (String id : ids) {
|
||||
if (!id.isEmpty() && Long.parseLong(id) == model.getId()) {
|
||||
contain = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(contain);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteTest() throws Exception {
|
||||
// 准备数据
|
||||
Comment c = new Comment();
|
||||
c.setArticleID(-1L);
|
||||
c.setType(true);
|
||||
c.setAuthorID(2L);
|
||||
c.setDate(new Date());
|
||||
c.setPid(-1L);
|
||||
commentMapper.insert(c);
|
||||
Comment comment = commentMapper.getLastestComment();
|
||||
// 接口测试
|
||||
long id = comment.getId();
|
||||
assertNotEquals(0, id);
|
||||
String token = userLogin();
|
||||
mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertTrue(object.getBoolean(Result));
|
||||
});
|
||||
mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(COMMENT_NOT_EXIST.getCode(), object.getInt(Code));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
Comment comment = commentMapper.getLastestComment();
|
||||
CommentReq commentReq = new CommentReq();
|
||||
commentReq.setId(comment.getId());
|
||||
commentReq.setPid(comment.getPid());
|
||||
commentReq.setContent(UUID.randomUUID().toString());
|
||||
commentReq.setArticleID(comment.getArticleID());
|
||||
// 不合法数据 setResponseId
|
||||
commentReq.setResponseId("xxxx");
|
||||
commentReq.setComment(comment.getType());
|
||||
mockMvc.perform(put("/user/comment/update")
|
||||
.content(JSONObject.fromObject(commentReq).toString())
|
||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.header("Authorization", userLogin())
|
||||
).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
CommentModel c = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
|
||||
assertEquals(commentReq.getContent(), c.getContent());
|
||||
assertEquals(commentReq.getResponseId(), c.getResponseId());
|
||||
assertNotNull(c.getAuthorAvatarImgUrl());
|
||||
assertNotNull(c.getAuthorName());
|
||||
assertNotNull(c.getDate());
|
||||
assertNotEquals(0, c.getPid());
|
||||
assertNotEquals(0, c.getArticleID());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commentsOfArticle() throws Exception {
|
||||
mockMvc.perform(get("/comments?articleId=3&page=1&count=10")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
|
||||
assertEquals(3, model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
assertNotNull(model.getArticleTitle());
|
||||
assertNotNull(model.getContent());
|
||||
assertNotNull(model.getResponseId());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievePage() throws Exception {
|
||||
long pid = 17;
|
||||
mockMvc.perform(get("/comment/pid/" + pid + "?articleId=3&page=1&count=10")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
|
||||
assertEquals(3, model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
assertNotNull(model.getArticleTitle());
|
||||
assertNotNull(model.getContent());
|
||||
assertNotNull(model.getResponseId());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievePageOfLeaveMsg() throws Exception {
|
||||
mockMvc.perform(get("/leaveMsg?page=1&count=10")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
|
||||
assertEquals(-1, model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
assertNotNull(model.getContent());
|
||||
assertNotNull(model.getResponseId());
|
||||
assertFalse(model.isComment());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievePageAdmin() throws Exception {
|
||||
mockMvc.perform(get("/admin/comment/type/1?page=1&count=10").header("Authorization", adminLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
|
||||
assertNotEquals(-1, model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
assertNotNull(model.getContent());
|
||||
assertNotNull(model.getResponseId());
|
||||
assertTrue(model.isComment());
|
||||
});
|
||||
});
|
||||
mockMvc.perform(get("/admin/comment/type/0?page=1&count=10").header("Authorization", adminLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
|
||||
assertEquals(-1, model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
assertNotNull(model.getContent());
|
||||
assertNotNull(model.getResponseId());
|
||||
assertFalse(model.isComment());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievePageByAuthor() throws Exception {
|
||||
mockMvc.perform(get("/user/comment/type/1?page=1&count=10").header("Authorization", userLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
|
||||
assertNotEquals(-1, model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
assertNotNull(model.getContent());
|
||||
assertNotNull(model.getResponseId());
|
||||
assertTrue(model.isComment());
|
||||
});
|
||||
});
|
||||
mockMvc.perform(get("/user/comment/type/0?page=1&count=10").header("Authorization", userLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
|
||||
assertEquals(-1, model.getArticleID());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getAuthorName());
|
||||
assertNotNull(model.getAuthorAvatarImgUrl());
|
||||
assertNotNull(model.getContent());
|
||||
assertNotNull(model.getResponseId());
|
||||
assertFalse(model.isComment());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
178
src/test/java/cn/celess/blog/controller/LinksControllerTest.java
Normal file
178
src/test/java/cn/celess/blog/controller/LinksControllerTest.java
Normal file
@@ -0,0 +1,178 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.PartnerSite;
|
||||
import cn.celess.blog.entity.request.LinkReq;
|
||||
import cn.celess.blog.mapper.PartnerMapper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
public class LinksControllerTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
PartnerMapper mapper;
|
||||
|
||||
@Test
|
||||
public void create() throws Exception {
|
||||
LinkReq linkReq = new LinkReq();
|
||||
linkReq.setName(UUID.randomUUID().toString().substring(0, 4));
|
||||
linkReq.setOpen(false);
|
||||
linkReq.setUrl("https://example.com");
|
||||
String token = adminLogin();
|
||||
mockMvc.perform(
|
||||
post("/admin/links/create")
|
||||
.content(JSONObject.fromObject(linkReq).toString())
|
||||
.header("Authorization", token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PartnerSite site = (PartnerSite) JSONObject.toBean(object.getJSONObject(Result), PartnerSite.class);
|
||||
assertNotNull(site.getId());
|
||||
assertEquals(linkReq.getName(), site.getName());
|
||||
assertEquals(linkReq.getUrl(), site.getUrl());
|
||||
assertEquals(linkReq.isOpen(), site.getOpen());
|
||||
});
|
||||
|
||||
// https/http
|
||||
linkReq.setName(UUID.randomUUID().toString().substring(0, 4));
|
||||
linkReq.setOpen(false);
|
||||
linkReq.setUrl("example.com");
|
||||
mockMvc.perform(
|
||||
post("/admin/links/create")
|
||||
.content(JSONObject.fromObject(linkReq).toString())
|
||||
.header("Authorization", token)
|
||||
.contentType("application/json")
|
||||
).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PartnerSite site = (PartnerSite) JSONObject.toBean(object.getJSONObject(Result), PartnerSite.class);
|
||||
assertEquals("http://example.com", site.getUrl());
|
||||
});
|
||||
|
||||
// 测试已存在的数据
|
||||
mockMvc.perform(
|
||||
post("/admin/links/create")
|
||||
.content(JSONObject.fromObject(linkReq).toString())
|
||||
.header("Authorization", token)
|
||||
.contentType("application/json")
|
||||
).andDo(result -> assertEquals(DATA_HAS_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void del() throws Exception {
|
||||
PartnerSite partnerSite = new PartnerSite();
|
||||
partnerSite.setName(UUID.randomUUID().toString().substring(0, 4));
|
||||
partnerSite.setOpen(true);
|
||||
partnerSite.setUrl("https://www.celess.cn");
|
||||
mapper.insert(partnerSite);
|
||||
PartnerSite lastest = mapper.getLastest();
|
||||
assertNotNull(lastest.getId());
|
||||
String token = adminLogin();
|
||||
mockMvc.perform(delete("/admin/links/del/" + lastest.getId()).header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertTrue(object.getBoolean(Result));
|
||||
});
|
||||
long id = lastest.getId();
|
||||
do {
|
||||
id += 1;
|
||||
} while (mapper.existsById(id));
|
||||
System.out.println("删除ID=" + id + "的数据");
|
||||
mockMvc.perform(delete("/admin/links/del/" + id).header("Authorization", token)).andDo(result ->
|
||||
assertEquals(DATA_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
// 增数据
|
||||
PartnerSite partnerSite = new PartnerSite();
|
||||
partnerSite.setName(UUID.randomUUID().toString().substring(0, 4));
|
||||
partnerSite.setOpen(true);
|
||||
partnerSite.setUrl("https://www.celess.cn");
|
||||
mapper.insert(partnerSite);
|
||||
// 查数据
|
||||
PartnerSite lastest = mapper.getLastest();
|
||||
assertNotNull(lastest.getId());
|
||||
String token = adminLogin();
|
||||
// 构建请求
|
||||
LinkReq linkReq = new LinkReq();
|
||||
linkReq.setUrl(lastest.getUrl());
|
||||
linkReq.setOpen(!lastest.getOpen());
|
||||
linkReq.setName(UUID.randomUUID().toString().substring(0, 4));
|
||||
linkReq.setId(lastest.getId());
|
||||
mockMvc.perform(
|
||||
put("/admin/links/update")
|
||||
.content(JSONObject.fromObject(linkReq).toString())
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.header("Authorization", token)
|
||||
).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PartnerSite site = (PartnerSite) JSONObject.toBean(object.getJSONObject(Result), PartnerSite.class);
|
||||
assertNotNull(site.getId());
|
||||
assertEquals(linkReq.getId(), site.getId().longValue());
|
||||
assertEquals(linkReq.getUrl(), site.getUrl());
|
||||
assertEquals(linkReq.getName(), site.getName());
|
||||
assertEquals(linkReq.isOpen(), site.getOpen());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allForOpen() throws Exception {
|
||||
mockMvc.perform(get("/links")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
object.getJSONArray(Result).forEach(o -> {
|
||||
PartnerSite site = (PartnerSite) JSONObject.toBean(JSONObject.fromObject(o), PartnerSite.class);
|
||||
assertNotNull(site.getUrl());
|
||||
assertNull(site.getOpen());
|
||||
assertNotNull(site.getName());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void all() throws Exception {
|
||||
mockMvc.perform(get("/admin/links?page=1&count=10").header("Authorization", adminLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
PartnerSite site = (PartnerSite) JSONObject.toBean(JSONObject.fromObject(o), PartnerSite.class);
|
||||
assertNotNull(site.getUrl());
|
||||
assertNotNull(site.getName());
|
||||
assertNotNull(site.getOpen());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply() throws Exception {
|
||||
long l = System.currentTimeMillis();
|
||||
String url = "https://www.example.com";
|
||||
mockMvc.perform(post("/apply?name=小海博客Api测试,请忽略&url=" + url)).andDo(result -> {
|
||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
System.out.println("耗时:" + (System.currentTimeMillis() - l) / 1000 + "s");
|
||||
url = "xxxxxxxxxm";
|
||||
mockMvc.perform(post("/apply?name=小海博客Api测试,请忽略&url=" + url)).andDo(result -> {
|
||||
assertEquals(PARAMETERS_URL_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
143
src/test/java/cn/celess/blog/controller/TagControllerTest.java
Normal file
143
src/test/java/cn/celess/blog/controller/TagControllerTest.java
Normal file
@@ -0,0 +1,143 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Tag;
|
||||
import cn.celess.blog.mapper.TagMapper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
|
||||
public class TagControllerTest extends BaseTest {
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
|
||||
@Test
|
||||
public void addOne() throws Exception {
|
||||
String name = UUID.randomUUID().toString().substring(0, 4);
|
||||
mockMvc.perform(post("/admin/tag/create?name=" + name)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(post("/admin/tag/create?name=" + name).header("authorization", userLogin())).andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(post("/admin/tag/create?name=" + name).header("authorization", adminLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
JSONObject resJson = object.getJSONObject(Result);
|
||||
Tag tag = (Tag) JSONObject.toBean(resJson, Tag.class);
|
||||
assertNotNull(tag.getId());
|
||||
assertEquals(name, tag.getName());
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delOne() throws Exception {
|
||||
Tag lastestTag = tagMapper.getLastestTag();
|
||||
assertNotNull(lastestTag.getId());
|
||||
String token = adminLogin();
|
||||
mockMvc.perform(delete("/admin/tag/del?id=" + lastestTag.getId()).header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertTrue(object.getBoolean(Result));
|
||||
});
|
||||
long id = lastestTag.getId() * 2;
|
||||
mockMvc.perform(delete("/admin/tag/del?id=" + id).header("Authorization", token)).andDo(result ->
|
||||
assertEquals(TAG_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateOne() throws Exception {
|
||||
Tag tag = tagMapper.getLastestTag();
|
||||
assertNotNull(tag.getId());
|
||||
String name = UUID.randomUUID().toString().substring(0, 4);
|
||||
mockMvc.perform(put("/admin/tag/update?id=" + tag.getId() + "&name=" + name).header("Authorization", adminLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertNotNull(object.getJSONObject(Result));
|
||||
Tag t = (Tag) JSONObject.toBean(object.getJSONObject(Result), Tag.class);
|
||||
assertEquals(name, t.getName());
|
||||
assertEquals(tag.getArticles(), t.getArticles());
|
||||
assertEquals(tag.getId(), t.getId());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveOneById() throws Exception {
|
||||
Tag tag = tagMapper.getLastestTag();
|
||||
assertNotNull(tag.getId());
|
||||
mockMvc.perform(get("/tag/id/" + tag.getId())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertNotNull(object.getJSONObject(Result));
|
||||
Tag t = (Tag) JSONObject.toBean(object.getJSONObject(Result), Tag.class);
|
||||
assertEquals(tag.getId(), t.getId());
|
||||
assertNotNull(t.getName());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveOneByName() throws Exception {
|
||||
Tag tag = tagMapper.getLastestTag();
|
||||
assertNotNull(tag.getName());
|
||||
mockMvc.perform(get("/tag/name/" + tag.getName())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertNotNull(object.getJSONObject(Result));
|
||||
Tag t = (Tag) JSONObject.toBean(object.getJSONObject(Result), Tag.class);
|
||||
assertEquals(tag.getName(), t.getName());
|
||||
assertNotNull(t.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPage() throws Exception {
|
||||
mockMvc.perform(get("/tags?page=1&count=5"))
|
||||
.andExpect(status().is(200))
|
||||
.andDo(result -> {
|
||||
JSONObject articlesJSON = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
// 断言获取数据成功
|
||||
assertEquals(SUCCESS.getCode(), articlesJSON.getInt(Code));
|
||||
// 结果集非空
|
||||
assertNotNull(articlesJSON.getJSONObject(Result));
|
||||
// 判断pageInfo是否包装完全
|
||||
JSONObject resultJson = JSONObject.fromObject(articlesJSON.getJSONObject(Result));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(resultJson, PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getTotal());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(5, pageInfo.getPageSize());
|
||||
// 内容完整
|
||||
for (Object tag : pageInfo.getList()) {
|
||||
Tag t = (Tag) JSONObject.toBean(JSONObject.fromObject(tag), Tag.class);
|
||||
assertNotNull(t.getId());
|
||||
assertNotNull(t.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTagNameAndCount() throws Exception {
|
||||
mockMvc.perform(get("/tags/nac")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
JSONArray jsonArray = object.getJSONArray(Result);
|
||||
assertNotNull(jsonArray);
|
||||
jsonArray.forEach(o -> {
|
||||
JSONObject json = JSONObject.fromObject(o);
|
||||
assertTrue(json.containsKey("size"));
|
||||
assertTrue(json.containsKey("name"));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
247
src/test/java/cn/celess/blog/controller/UserControllerTest.java
Normal file
247
src/test/java/cn/celess/blog/controller/UserControllerTest.java
Normal file
@@ -0,0 +1,247 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.User;
|
||||
import cn.celess.blog.entity.model.UserModel;
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import cn.celess.blog.entity.request.UserReq;
|
||||
import cn.celess.blog.mapper.UserMapper;
|
||||
import cn.celess.blog.util.MD5Util;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
|
||||
public class UserControllerTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
public void login() throws Exception {
|
||||
assertNotNull(userLogin());
|
||||
assertNotNull(adminLogin());
|
||||
// 用户不存在
|
||||
LoginReq req = new LoginReq();
|
||||
req.setEmail("zh@celess.cn");
|
||||
req.setPassword("123456789");
|
||||
req.setIsRememberMe(false);
|
||||
JSONObject loginReq = JSONObject.fromObject(req);
|
||||
mockMvc.perform(post("/login").content(loginReq.toString()).contentType("application/json"))
|
||||
.andDo(result ->
|
||||
assertEquals(USER_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registration() {
|
||||
// 自行手动测试!
|
||||
// TODO :
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logout() throws Exception {
|
||||
mockMvc.perform(get("/logout")).andDo(result -> assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(get("/logout").header("Authorization", userLogin())).andDo(result -> assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateInfo() throws Exception {
|
||||
String desc = UUID.randomUUID().toString().substring(0, 4);
|
||||
String disPlayName = UUID.randomUUID().toString().substring(0, 4);
|
||||
mockMvc.perform(put("/user/userInfo/update?desc=" + desc + "&displayName=" + disPlayName).header("Authorization", userLogin()))
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
UserModel u = (UserModel) JSONObject.toBean(object.getJSONObject(Result), UserModel.class);
|
||||
assertEquals(desc, u.getDesc());
|
||||
assertEquals(disPlayName, u.getDisplayName());
|
||||
assertNotNull(u.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUserInfo() throws Exception {
|
||||
mockMvc.perform(get("/user/userInfo").header("Authorization", userLogin()))
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
UserModel u = (UserModel) JSONObject.toBean(object.getJSONObject(Result), UserModel.class);
|
||||
assertNotNull(u.getId());
|
||||
assertNotNull(u.getEmail());
|
||||
assertNotNull(u.getDisplayName());
|
||||
assertNotNull(u.getEmailStatus());
|
||||
assertNotNull(u.getAvatarImgUrl());
|
||||
assertNotNull(u.getDesc());
|
||||
assertNotNull(u.getRecentlyLandedDate());
|
||||
assertNotNull(u.getRole());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upload() throws Exception {
|
||||
File logoFile = new File("C:\\Users\\zh564\\Pictures\\logo.png");
|
||||
MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, new FileInputStream(logoFile));
|
||||
mockMvc.perform(multipart("/user/imgUpload").file(file)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(multipart("/user/imgUpload").file(file).header("Authorization", userLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertNotNull(object.getString(Result));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendResetPwdEmail() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendVerifyEmail() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emailVerify() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resetPwd() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleDelete() throws Exception {
|
||||
List<User> userList = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
String s = UUID.randomUUID().toString();
|
||||
String email = s.substring(s.length() - 4) + "@celess.cn";
|
||||
String pwd = MD5Util.getMD5("123456789");
|
||||
int i1 = userMapper.addUser(email, pwd);
|
||||
if (i1 == 0) {
|
||||
continue;
|
||||
}
|
||||
userList.add(userMapper.findByEmail(email));
|
||||
if (i == 9) {
|
||||
//设置一个管理员
|
||||
userMapper.setUserRole(userMapper.findByEmail(email).getId(), "admin");
|
||||
}
|
||||
}
|
||||
List<Long> idList = new ArrayList<>();
|
||||
userList.forEach(user -> idList.add(user.getId()));
|
||||
System.out.println("id :: == > " + idList.toString());
|
||||
mockMvc.perform(delete("/admin/user/delete").content(idList.toString()).contentType("application/json"))
|
||||
.andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(delete("/admin/user/delete").content(idList.toString()).contentType("application/json").header("Authorization", userLogin()))
|
||||
.andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(delete("/admin/user/delete").content(idList.toString()).contentType("application/json").header("Authorization", adminLogin()))
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
JSONArray jsonArray = object.getJSONArray(Result);
|
||||
jsonArray.forEach(o -> {
|
||||
JSONObject json = JSONObject.fromObject(o);
|
||||
// 判断响应数据中是否包含输入的id
|
||||
assertTrue(idList.contains((long) json.getInt("id")));
|
||||
// 判断处理状态
|
||||
boolean status = json.getBoolean("status");
|
||||
if (json.containsKey("msg"))
|
||||
assertFalse(status);
|
||||
else
|
||||
assertTrue(status);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateInfoByAdmin() throws Exception {
|
||||
UserReq userReq = new UserReq();
|
||||
String email = UUID.randomUUID().toString().substring(0, 4) + "@celess.cn";
|
||||
userMapper.addUser(email, MD5Util.getMD5("123456789"));
|
||||
User userByDb = userMapper.findByEmail(email);
|
||||
userReq.setId(userByDb.getId());
|
||||
userReq.setPwd(UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10));
|
||||
userReq.setDesc(UUID.randomUUID().toString());
|
||||
userReq.setEmailStatus(new Random().nextBoolean());
|
||||
userReq.setRole("admin");
|
||||
userReq.setDisplayName(UUID.randomUUID().toString().substring(0, 4));
|
||||
userReq.setEmail(UUID.randomUUID().toString().substring(0, 5) + "@celess.cn");
|
||||
mockMvc.perform(put("/admin/user").contentType("application/json").content(JSONObject.fromObject(userReq).toString()))
|
||||
.andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(put("/admin/user").contentType("application/json").header("Authorization", userLogin()).content(JSONObject.fromObject(userReq).toString()))
|
||||
.andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(put("/admin/user").contentType("application/json").header("Authorization", adminLogin()).content(JSONObject.fromObject(userReq).toString()))
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
UserModel user = (UserModel) JSONObject.toBean(object.getJSONObject(Result), UserModel.class);
|
||||
assertEquals(userReq.getId(), user.getId());
|
||||
assertEquals(userReq.getRole(), user.getRole());
|
||||
assertEquals(userReq.getEmail(), user.getEmail());
|
||||
assertEquals(userReq.getDesc(), user.getDesc());
|
||||
assertEquals(userReq.getDisplayName(), user.getDisplayName());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllUser() throws Exception {
|
||||
mockMvc.perform(get("/admin/users?page=1&count=10"))
|
||||
.andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(get("/admin/users?page=1&count=10").header("authorization", userLogin()))
|
||||
.andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
mockMvc.perform(get("/admin/users?page=1&count=10").header("Authorization", adminLogin()))
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
// 结果集非空
|
||||
assertNotNull(object.getJSONObject(Result));
|
||||
// 判断pageInfo是否包装完全
|
||||
JSONObject resultJson = JSONObject.fromObject(object.getJSONObject(Result));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(resultJson, PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getTotal());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
// 内容完整
|
||||
for (Object user : pageInfo.getList()) {
|
||||
UserModel u = (UserModel) JSONObject.toBean(JSONObject.fromObject(user), UserModel.class);
|
||||
assertNotNull(u.getId());
|
||||
assertNotNull(u.getEmail());
|
||||
assertNotNull(u.getRole());
|
||||
assertNotNull(u.getEmailStatus());
|
||||
assertNotNull(u.getDisplayName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEmailStatus() throws Exception {
|
||||
String email = UUID.randomUUID().toString().substring(0, 4) + "@celess.cn";
|
||||
mockMvc.perform(get("/emailStatus/" + email)).andDo(result -> {
|
||||
String content = result.getResponse().getContentAsString();
|
||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(content).getInt(Code));
|
||||
assertFalse(JSONObject.fromObject(content).getBoolean(Result));
|
||||
});
|
||||
email = "a@celess.cn";
|
||||
mockMvc.perform(get("/emailStatus/" + email)).andDo(result -> {
|
||||
String content = result.getResponse().getContentAsString();
|
||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(content).getInt(Code));
|
||||
assertTrue(JSONObject.fromObject(content).getBoolean(Result));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.model.VisitorModel;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
|
||||
public class VisitorControllerTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void getVisitorCount() throws Exception {
|
||||
mockMvc.perform(get("/visitor/count")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertTrue(object.containsKey(Result));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void page() throws Exception {
|
||||
int count = 10;
|
||||
int page = 1;
|
||||
mockMvc.perform(get("/admin/visitor/page?count=" + count + "&page=" + page).header("Authorization", adminLogin()))
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
JSONObject resultJson = JSONObject.fromObject(object.getJSONObject(Result));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(resultJson, PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getTotal());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
for (Object ver : pageInfo.getList()) {
|
||||
VisitorModel v = (VisitorModel) JSONObject.toBean(JSONObject.fromObject(ver), VisitorModel.class);
|
||||
assertNotEquals(0, v.getId());
|
||||
assertNotNull(v.getDate());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void add() throws Exception {
|
||||
mockMvc.perform(post("/visit")).andDo(MockMvcResultHandlers.print()).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
VisitorModel visitorModel = (VisitorModel) JSONObject.toBean(object.getJSONObject(Result), VisitorModel.class);
|
||||
assertNotEquals(0, visitorModel.getId());
|
||||
assertNotNull(visitorModel.getIp());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dayVisitCount() throws Exception {
|
||||
mockMvc.perform(get("/dayVisitCount")).andDo(MockMvcResultHandlers.print()).andDo(result ->
|
||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ipLocation() throws Exception {
|
||||
String ip = "127.0.0.1";
|
||||
mockMvc.perform(get("/ip/" + ip)).andDo(MockMvcResultHandlers.print()).andDo(result -> {
|
||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
assertTrue(JSONObject.fromObject(result.getResponse().getContentAsString()).containsKey(Result));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIp() throws Exception {
|
||||
mockMvc.perform(get("/ip")).andDo(MockMvcResultHandlers.print()).andDo(result -> {
|
||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
assertEquals("127.0.0.1", JSONObject.fromObject(result.getResponse().getContentAsString()).getString(Result));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.WebUpdate;
|
||||
import cn.celess.blog.entity.model.WebUpdateModel;
|
||||
import cn.celess.blog.mapper.WebUpdateInfoMapper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
public class WebUpdateInfoControllerTest extends BaseTest {
|
||||
@Autowired
|
||||
WebUpdateInfoMapper mapper;
|
||||
|
||||
@Test
|
||||
public void create() throws Exception {
|
||||
String info = UUID.randomUUID().toString();
|
||||
mockMvc.perform(post("/admin/webUpdate/create?info=" + info).header("Authorization", adminLogin())).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertTrue(object.containsKey(Result));
|
||||
WebUpdateModel webUpdateModel = (WebUpdateModel) JSONObject.toBean(object.getJSONObject(Result), WebUpdateModel.class);
|
||||
assertEquals(info, webUpdateModel.getInfo());
|
||||
assertNotNull(webUpdateModel.getTime());
|
||||
assertNotEquals(0, webUpdateModel.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void del() throws Exception {
|
||||
// 新增数据
|
||||
WebUpdate webUpdate = new WebUpdate();
|
||||
webUpdate.setUpdateInfo(UUID.randomUUID().toString());
|
||||
webUpdate.setUpdateTime(new Date());
|
||||
mapper.insert(webUpdate);
|
||||
// 接口测试
|
||||
List<WebUpdate> updateList = mapper.findAll();
|
||||
WebUpdate update = updateList.get(updateList.size() - 1);
|
||||
assertNotEquals(0, update.getId());
|
||||
|
||||
long id = update.getId();
|
||||
mockMvc.perform(delete("/admin/webUpdate/del/" + id).header("Authorization", adminLogin())).andDo(result -> {
|
||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
assertTrue(JSONObject.fromObject(result.getResponse().getContentAsString()).getBoolean(Result));
|
||||
});
|
||||
do {
|
||||
id += 2;
|
||||
} while (mapper.existsById(id));
|
||||
System.out.println("准备删除ID=" + id + "的不存在记录");
|
||||
mockMvc.perform(delete("/admin/webUpdate/del/" + id).header("Authorization", adminLogin())).andDo(result ->
|
||||
assertEquals(DATA_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
// 新增数据
|
||||
WebUpdate webUpdate = new WebUpdate();
|
||||
webUpdate.setUpdateInfo(UUID.randomUUID().toString());
|
||||
webUpdate.setUpdateTime(new Date());
|
||||
mapper.insert(webUpdate);
|
||||
List<WebUpdate> all = mapper.findAll();
|
||||
WebUpdate update = all.get(all.size() - 1);
|
||||
assertNotEquals(0, update.getId());
|
||||
assertNotNull(update.getUpdateInfo());
|
||||
String info = UUID.randomUUID().toString();
|
||||
mockMvc.perform(put("/admin/webUpdate/update?id=" + update.getId() + "&info=" + info).header("Authorization", adminLogin())).andDo(result -> {
|
||||
List<WebUpdate> list = mapper.findAll();
|
||||
WebUpdate up = list.get(list.size() - 1);
|
||||
assertEquals(update.getId(), up.getId());
|
||||
assertEquals(update.getUpdateTime(), up.getUpdateTime());
|
||||
assertEquals(info, up.getUpdateInfo());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() throws Exception {
|
||||
mockMvc.perform(get("/webUpdate")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
JSONArray jsonArray = object.getJSONArray(Result);
|
||||
jsonArray.forEach(o -> {
|
||||
WebUpdateModel webUpdate = (WebUpdateModel) JSONObject.toBean(JSONObject.fromObject(o), WebUpdateModel.class);
|
||||
assertNotEquals(0, webUpdate.getId());
|
||||
assertNotNull(webUpdate.getTime());
|
||||
assertNotNull(webUpdate.getInfo());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void page() throws Exception {
|
||||
mockMvc.perform(get("/webUpdate/pages?page=1&count=10")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
assertNotNull(object.getJSONObject(Result));
|
||||
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
|
||||
assertNotEquals(0, pageInfo.getEndRow());
|
||||
assertNotEquals(0, pageInfo.getStartRow());
|
||||
assertEquals(1, pageInfo.getPageNum());
|
||||
assertEquals(10, pageInfo.getPageSize());
|
||||
pageInfo.getList().forEach(o -> {
|
||||
WebUpdateModel model = (WebUpdateModel) JSONObject.toBean(JSONObject.fromObject(o), WebUpdateModel.class);
|
||||
assertNotEquals(0, model.getId());
|
||||
assertNotNull(model.getTime());
|
||||
assertNotNull(model.getInfo());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastestUpdateTime() throws Exception {
|
||||
mockMvc.perform(get("/lastestUpdateTime")).andDo(result -> assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
||||
}
|
||||
}
|
||||
64
src/test/java/cn/celess/blog/filter/AuthorizationFilter.java
Normal file
64
src/test/java/cn/celess/blog/filter/AuthorizationFilter.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package cn.celess.blog.filter;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.junit.Assert.*;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
* @Date: 2019/11/28 16:05
|
||||
* @Description: 授权拦截器的测试类
|
||||
*/
|
||||
public class AuthorizationFilter extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void UserAccess() throws Exception {
|
||||
String token = "";
|
||||
// 未登录
|
||||
mockMvc.perform(get("/user/userInfo").header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), object.getInt(Code));
|
||||
});
|
||||
token = userLogin();
|
||||
mockMvc.perform(get("/user/userInfo").header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void AdminAccess() throws Exception {
|
||||
String token = "";
|
||||
// 未登录
|
||||
mockMvc.perform(get("/admin/articles?page=1&count=1").header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), object.getInt(Code));
|
||||
});
|
||||
token = userLogin();
|
||||
mockMvc.perform(get("/admin/articles?page=1&count=1").header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(PERMISSION_ERROR.getCode(), object.getInt(Code));
|
||||
});
|
||||
token = adminLogin();
|
||||
mockMvc.perform(get("/admin/articles?page=1&count=1").header("Authorization", token)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void VisitorAccess() throws Exception {
|
||||
mockMvc.perform(get("/user/userInfo")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), object.getInt(Code));
|
||||
});
|
||||
mockMvc.perform(get("/admin/articles?page=1&count=1")).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), object.getInt(Code));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.celess.blog.filter;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
* @Date: 2019/11/28 16:17
|
||||
* @Description: 测试重复请求
|
||||
*/
|
||||
public class MultipleSubmitFilter extends BaseTest {
|
||||
|
||||
private MockHttpSession session = null;
|
||||
|
||||
@Test
|
||||
public void submitTest() throws Exception {
|
||||
session = new MockHttpSession();
|
||||
sendRequest(ResponseEnum.SUCCESS);
|
||||
sendRequest(ResponseEnum.FAILURE, "重复请求");
|
||||
}
|
||||
|
||||
|
||||
private void sendRequest(ResponseEnum expectResponse, String... msg) throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/counts").session(session)).andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
Assert.assertEquals(expectResponse.getCode(), object.getInt(Code));
|
||||
if (msg.length != 0) {
|
||||
Assert.assertEquals(msg[0], object.getString("msg"));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
52
src/test/java/cn/celess/blog/util/JwtUtilTest.java
Normal file
52
src/test/java/cn/celess/blog/util/JwtUtilTest.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package cn.celess.blog.util;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.User;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class JwtUtilTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
JwtUtil jwtUtil;
|
||||
|
||||
|
||||
@Test
|
||||
public void generateToken() {
|
||||
User user = new User();
|
||||
user.setEmail("a@celess.cn");
|
||||
String s = jwtUtil.generateToken(user, true);
|
||||
System.out.println(s);
|
||||
assertNotNull(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateToken() {
|
||||
User user = new User();
|
||||
user.setEmail("a@celess.cn");
|
||||
assertTrue(jwtUtil.validateToken(createToken(), user));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTokenExpired() {
|
||||
assertFalse(jwtUtil.isTokenExpired(createToken()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUsernameFromToken() {
|
||||
assertEquals("a@celess.cn", jwtUtil.getUsernameFromToken(createToken()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExpirationDateFromToken() {
|
||||
assertNotNull(jwtUtil.getExpirationDateFromToken(createToken()));
|
||||
}
|
||||
|
||||
private String createToken() {
|
||||
User user = new User();
|
||||
user.setEmail("a@celess.cn");
|
||||
return jwtUtil.generateToken(user, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user