Search in sources :

Example 36 with WithMockUser

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
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 37 with WithMockUser

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
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockHttpSession(org.springframework.mock.web.MockHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 38 with WithMockUser

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();
}
Also used : Authentication(org.springframework.security.core.Authentication) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 39 with WithMockUser

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);
}
Also used : Authentication(org.springframework.security.core.Authentication) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 40 with WithMockUser

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);
}
Also used : Authentication(org.springframework.security.core.Authentication) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Aggregations

WithMockUser (org.springframework.security.test.context.support.WithMockUser)3415 Test (org.junit.jupiter.api.Test)2107 Test (org.junit.Test)1226 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)865 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)427 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)409 MvcResult (org.springframework.test.web.servlet.MvcResult)304 ResultActions (org.springframework.test.web.servlet.ResultActions)278 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)275 Transactional (org.springframework.transaction.annotation.Transactional)220 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)179 User (io.github.jhipster.sample.domain.User)149 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)136 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)130 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)130 Course (de.tum.in.www1.artemis.domain.Course)124 ArrayList (java.util.ArrayList)124 Exam (de.tum.in.www1.artemis.domain.exam.Exam)114 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)103 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)102