Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RunWith(SpringRunner.class)
- @ContextConfiguration(classes = { CoreConfig.class, UserManagementConfig.class, UserManagementServiceConfig.class, LoginController.class }, initializers = ConfigFileApplicationContextInitializer.class)
- @WebAppConfiguration
- @WebMvcTest
- @ActiveProfiles({"core"})
- public class LoginControllerTest extends WithServiceInitializedTest {
- private MockMvc mockMvc;
- @Autowired
- private Filter springSecurityFilterChain;
- @Autowired
- private WebApplicationContext webApplicationContext;
- @Before
- public void setUp() {
- mockMvc = MockMvcBuilders
- .webAppContextSetup(webApplicationContext)
- .addFilters(springSecurityFilterChain)
- .alwaysDo(print())
- .build();
- }
- @Test
- @WithMockUser(value = "root", roles = { "AUTH", "USER_MANAGEMENT" })
- public void testCookieLogin() throws Exception {
- HttpSessionCsrfTokenRepository httpSessionCsrfTokenRepository = new HttpSessionCsrfTokenRepository();
- CsrfToken csrfToken = httpSessionCsrfTokenRepository.generateToken(new MockHttpServletRequest());
- MockHttpServletRequestBuilder req = get(CoreHttpPathStore.CONTEXT_PATH)
- .header("Accept", "*/*")
- .header("Cache-Control", "no-cache")
- .header("Connection", "keep-alive")
- .header("content-type", "application/json");
- MvcResult resultBefore = mockMvc.perform(
- req
- )
- .andDo(result -> setSessionBackOnRequestBuilder(req, result.getRequest()))
- // .andExpect(authenticated())
- .andExpect(status().isUnauthorized())
- // .andExpect(cookie().exists("XSRF-TOKEN"))
- // .andExpect(cookie().exists("JSESSIONID"))
- .andReturn();
- MockHttpServletRequestBuilder reqLogin = post(CoreHttpPathStore.LOGIN)
- .requestAttr("X-XSRF-TOKEN", csrfToken)
- .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
- .param("username", "root")
- .param("password", "password");
- MvcResult result = mockMvc.perform(
- reqLogin
- )
- .andDo(result2 -> setSessionBackOnRequestBuilder(req, result2.getRequest()))
- .andExpect(status().isOk())
- // .andExpect(cookie().exists("JSESSIONID"))
- .andReturn();
- Cookie sessionId = result.getResponse().getCookie("JSESSIONID");
- Cookie token = result.getResponse().getCookie("XSRF-TOKEN");
- assertThat(sessionId).isEqualTo("yes");
- assertThat(token).isEqualTo("ye2s");
- }
- private MockHttpServletRequest setSessionBackOnRequestBuilder(final MockHttpServletRequestBuilder requestBuilder,
- final MockHttpServletRequest request) {
- requestBuilder.session((MockHttpSession) request.getSession());
- return request;
- }
- @Test
- public void testClientHomeWithError() throws Exception {
- MvcResult result = this.mockMvc.perform(
- get(CoreHttpPathStore.LOGIN_OAUTH_CLIENT_CB, "dummyClientId")
- .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
- )
- .andExpect(status().isUnauthorized())
- .andReturn();
- assertThat(result.getResponse().getContentAsString()).isEqualTo(""" + HttpStatus.UNAUTHORIZED.getReasonPhrase() + """);
- }
- @Test
- @WithMockUser(value = "root", roles = { "AUTH", "USER_MANAGEMENT" })
- public void testClientHomeWithSuccess() throws Exception {
- MvcResult result = this.mockMvc.perform(
- get(CoreHttpPathStore.REST_PATH)
- .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
- .with(user("root").roles("AUTH", "USER_MANAGEMENT"))
- )
- .andExpect(status().isOk())
- .andReturn();
- assertThat(result.getResponse().getContentAsString()).isEqualTo(""" + HttpStatus.UNAUTHORIZED.getReasonPhrase() + """);
- }
- }
- MockHttpServletRequest:
- HTTP Method = GET
- Request URI = /
- Parameters = {}
- Headers = {Accept=[*/*], Cache-Control=[no-cache], Connection=[keep-alive], Content-Type=[application/json]}
- Handler:
- Type = null
- Async:
- Async started = false
- Async result = null
- Resolved Exception:
- Type = null
- ModelAndView:
- View name = null
- View = null
- Model = null
- FlashMap:
- Attributes = null
- MockHttpServletResponse:
- Status = 401
- Error message = null
- Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
- Content type = null
- Body = "Unauthorized"
- Forwarded URL = null
- Redirected URL = null
- Cookies = []
- MockHttpServletRequest:
- HTTP Method = POST
- Request URI = /login
- Parameters = {username=[root], password=[password]}
- Headers = {Content-Type=[application/x-www-form-urlencoded]}
- Handler:
- Type = null
- Async:
- Async started = false
- Async result = null
- Resolved Exception:
- Type = null
- ModelAndView:
- View name = null
- View = null
- Model = null
- FlashMap:
- Attributes = null
- MockHttpServletResponse:
- Status = 401
- Error message = null
- Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
- Content type = null
- Body = "Unauthorized"
- Forwarded URL = null
- Redirected URL = null
- Cookies = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement