Junit test with csrf sample.

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.alibaba.fastjson.JSONObject;
import com.xxxxxxxxxxxx.dao.tables.ConferenceDTO;
import com.xxxxxxxxxxxx.dao.tables.ConferenceSearchDTO;
import com.xxxxxxxxxxxx.pf.xxxxxxxxxxxx.data.ConferenceData;
import com.xxxxxxxxxxxx.pf.xxxxxxxxxxxx.service.LogDataService;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.test.context.support.WithMockUser;
// import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

 

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class LoginControllerTest {

@Autowired
protected MockMvc mockMvc;

@Autowired
private WebApplicationContext wac;

@InjectMocks
LogApiController logApiController;

@MockBean
private LogDataService logDataService;

/**
* This method prepare for testing.
*
* @throws IOException
*
*/
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).apply(springSecurity()).build();
}

/**
* This method test case normal login client not send authiden parameter to server.
*
* @throws Exception when error
*/
@Test
// @WithUserDetails(value = "admin", userDetailsServiceBeanName = "userConfig")
@WithMockUser(value = "admin", roles = {"USER"})
public void menuTest() throws Exception {
MvcResult result = mockMvc.perform(get("/menu/")).andExpect(status().is(200)).andReturn();
MockHttpServletResponse mockResponse = result.getResponse();
String content = mockResponse.getContentAsString();
assertThat(content, containsString("html"));
String redirectUrl = mockResponse.getRedirectedUrl();
assertEquals(redirectUrl, null);
}

@Test
@WithMockUser(value = "admin", roles = {"USER"})
public void logTest() throws Exception {

String confid = "b46812c6-3569-42e0-8e21-d9237212b083";
ConferenceDTO dto = new ConferenceDTO();
dto.setActual_start_time(new Date());
ConferenceData conferenceData = new ConferenceData(dto);
conferenceData.setDisplay("junit test");
when(logDataService.selectConference(confid)).thenReturn(conferenceData);
Map<String, Object> map = new HashMap<>();
map.put("confid", confid);

ConferenceSearchDTO conferenceSearchDto = new ConferenceSearchDTO();
conferenceSearchDto.setConfid(confid);

MvcResult result = mockMvc.perform(post("/ui/log").with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(JSONObject.toJSONString(map))).andReturn();
MockHttpServletResponse mockResponse = result.getResponse();
String content = mockResponse.getContentAsString();
assertThat(content, containsString("result"));
assertEquals(mockResponse.getStatus(), 200);

// .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))//
// .andReturn();

}

}

posted @ 2020-08-11 15:31  dunkbird  阅读(218)  评论(0编辑  收藏  举报