更新Tag

This commit is contained in:
小海
2019-12-04 16:30:51 +08:00
parent 1745a5d821
commit 7ba287d261
3 changed files with 65 additions and 50 deletions

View File

@@ -8,10 +8,12 @@ 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.mock.web.MockHttpSession;
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.MvcResult;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
@@ -20,6 +22,8 @@ import org.springframework.web.context.WebApplicationContext;
import javax.servlet.http.Cookie;
import java.util.UUID;
import static org.junit.Assert.*;
/**
@@ -39,10 +43,12 @@ public class BaseTest {
@Autowired
private WebApplicationContext wac;
protected MockHttpSession session;
@Before
public void before() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
session = new MockHttpSession();
System.out.println("==========> 开始测试 <=========");
}
@@ -60,7 +66,7 @@ public class BaseTest {
req.setIsRememberMe(false);
JSONObject loginReq = JSONObject.fromObject(req);
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
// .andDo(MockMvcResultHandlers.print())
// .andDo(MockMvcResultHandlers.print())
.andReturn().getResponse().getContentAsString();
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
assertNotNull(token);
@@ -79,7 +85,7 @@ public class BaseTest {
req.setIsRememberMe(false);
JSONObject loginReq = JSONObject.fromObject(req);
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
// .andDo(MockMvcResultHandlers.print())
// .andDo(MockMvcResultHandlers.print())
.andReturn().getResponse().getContentAsString();
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
assertNotNull(token);
@@ -94,4 +100,8 @@ public class BaseTest {
public void test() {
}
protected String randomStr(int len) {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
}
}

View File

@@ -13,8 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.Arrays;
import java.util.UUID;
import java.util.*;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@@ -139,32 +138,17 @@ public class ArticleControllerTest extends BaseTest {
articleReq.setCategory("test");
articleReq.setMdContent("test-" + article.getMdContent());
articleReq.setOpen(!article.getOpen());
articleReq.setTags("tag");
String tag1 = randomStr(4);
String tag2 = randomStr(4);
String tag = "test," + tag1 + "," + tag2;
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))
.header("Authorization", adminLogin()))
.andExpect(status().isOk())
.andDo(result -> {
JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString());
@@ -175,8 +159,11 @@ public class ArticleControllerTest extends BaseTest {
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("]", ""))));
// Tag
List<String> asList = Arrays.asList(a.getTags());
assertTrue(asList.contains("test"));
assertTrue(asList.contains(tag1));
assertTrue(asList.contains(tag2));
assertEquals(articleReq.getOpen(), a.getOpen());
assertEquals(articleReq.getId(), a.getId());
});