use of org.springframework.security.test.context.support.WithMockUser in project spring-security by spring-projects.
the class OAuth2LoginBeanDefinitionParserTests method requestWhenAuthorizedClientFoundThenMethodArgumentResolved.
@WithMockUser
@Test
public void requestWhenAuthorizedClientFoundThenMethodArgumentResolved() throws Exception {
this.spring.configLocations(xml("AuthorizedClientArgumentResolver")).autowire();
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google-login");
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(clientRegistration, "user", TestOAuth2AccessTokens.noScopes());
given(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).willReturn(authorizedClient);
// @formatter:off
this.mvc.perform(get("/authorized-client")).andExpect(status().isOk()).andExpect(content().string("resolved"));
// @formatter:on
}
use of org.springframework.security.test.context.support.WithMockUser in project spring-security by spring-projects.
the class CsrfConfigTests method postWhenCsrfMismatchesThenForbidden.
@Test
@WithMockUser
public void postWhenCsrfMismatchesThenForbidden() throws Exception {
this.spring.configLocations(this.xml("shared-controllers"), this.xml("AutoConfig")).autowire();
MvcResult result = this.mvc.perform(get("/ok")).andReturn();
MockHttpSession session = (MockHttpSession) result.getRequest().getSession();
// @formatter:off
MockHttpServletRequestBuilder postOk = post("/ok").session(session).with(csrf().useInvalidToken());
this.mvc.perform(postOk).andExpect(status().isForbidden());
// @formatter:on
}
use of org.springframework.security.test.context.support.WithMockUser in project spring-security by spring-projects.
the class SecurityMockMvcResultHandlersTest method withTestSecurityContextNotCopiedToSecurityContextHolder.
@Test
@WithMockUser
public void withTestSecurityContextNotCopiedToSecurityContextHolder() throws Exception {
this.mockMvc.perform(get("/"));
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
assertThat(authentication).isNull();
}
use of org.springframework.security.test.context.support.WithMockUser in project spring-security by spring-projects.
the class SecurityMockServerConfigurersClassAnnotatedTests method withMockUserWhenClassAndMethodAnnotationThenMethodOverrides.
@Test
@WithMockUser("method-user")
public void withMockUserWhenClassAndMethodAnnotationThenMethodOverrides() {
this.client.get().exchange().expectStatus().isOk().expectBody(String.class).consumeWith((response) -> assertThat(response.getResponseBody()).contains("\"username\":\"method-user\""));
Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication();
this.controller.assertPrincipalIsEqualTo(authentication);
}
use of org.springframework.security.test.context.support.WithMockUser in project spring-security by spring-projects.
the class SecurityMockServerConfigurersAnnotatedTests method withMockUserWhenOnMethodAndRequestIsExecutedOnDifferentThreadThenSuccess.
@Test
@WithMockUser
public void withMockUserWhenOnMethodAndRequestIsExecutedOnDifferentThreadThenSuccess() {
Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication();
ForkJoinPool.commonPool().submit(() -> this.client.get().exchange().expectStatus().isOk()).join();
this.controller.assertPrincipalIsEqualTo(authentication);
}
Aggregations