package com.bfwg.rest; 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.security.test.context.support.WithAnonymousUser; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; 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.result.MockMvcResultMatchers.status; /** * Created by fanjin on 2017-09-01. */ @RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTest { private MockMvc mvc; @Autowired private WebApplicationContext context; @Before public void setup() { mvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) .build(); } @Test @WithAnonymousUser public void shouldGetUnauthorizedWithoutRole() throws Exception { this.mvc.perform(get("/user")) .andExpect(status().isUnauthorized()); } @Test @WithMockUser(roles = "USER") public void getPersonsSuccessfullyWithUserRole() throws Exception { this.mvc.perform(get("/api/whoami")) .andExpect(status().is2xxSuccessful()); } @Test @WithAnonymousUser public void getPersonsFailWithAnonymousUser() throws Exception { this.mvc.perform(get("/api/whoami")) .andExpect(status().is4xxClientError()); } @Test @WithMockUser(roles = "ADMIN") public void getAllUserSuccessWithAdminRole() throws Exception { this.mvc.perform(get("/api/user/all")) .andExpect(status().is2xxSuccessful()); } @Test @WithMockUser(roles = "USER") public void getAllUserFailWithUserRole() throws Exception { this.mvc.perform(get("/api/user/all")) .andExpect(status().is4xxClientError()); } }