Search in sources :

Example 66 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class WebSessionOAuth2ServerAuthorizationRequestRepositoryDoNotAllowMultipleAuthorizationRequestsTests method removeAuthorizationRequestWhenMultipleThenSessionAttributeRemoved.

// gh-5145
@Test
public void removeAuthorizationRequestWhenMultipleThenSessionAttributeRemoved() {
    String oldState = "state0";
    // @formatter:off
    MockServerHttpRequest oldRequest = MockServerHttpRequest.get("/").queryParam(OAuth2ParameterNames.STATE, oldState).build();
    OAuth2AuthorizationRequest oldAuthorizationRequest = OAuth2AuthorizationRequest.authorizationCode().authorizationUri("https://example.com/oauth2/authorize").clientId("client-id").redirectUri("http://localhost/client-1").state(oldState).build();
    // @formatter:on
    Map<String, Object> sessionAttrs = spy(new HashMap<>());
    WebSession session = mock(WebSession.class);
    given(session.getAttributes()).willReturn(sessionAttrs);
    WebSessionManager sessionManager = (e) -> Mono.just(session);
    this.exchange = new DefaultServerWebExchange(this.exchange.getRequest(), new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    ServerWebExchange oldExchange = new DefaultServerWebExchange(oldRequest, new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    // @formatter:off
    Mono<OAuth2AuthorizationRequest> saveAndSaveAndRemove = this.repository.saveAuthorizationRequest(oldAuthorizationRequest, oldExchange).then(this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange)).then(this.repository.removeAuthorizationRequest(this.exchange));
    StepVerifier.create(saveAndSaveAndRemove).expectNext(this.authorizationRequest).verifyComplete();
    StepVerifier.create(this.repository.loadAuthorizationRequest(this.exchange)).verifyComplete();
    // @formatter:on
    verify(sessionAttrs, times(2)).put(anyString(), any());
    verify(sessionAttrs).remove(anyString());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.spy(org.mockito.Mockito.spy) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) WebSessionManager(org.springframework.web.server.session.WebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) WebSession(org.springframework.web.server.WebSession) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) WebSession(org.springframework.web.server.WebSession) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebSessionManager(org.springframework.web.server.session.WebSessionManager) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 67 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class WebSessionOAuth2ServerAuthorizationRequestRepositoryAllowMultipleAuthorizationRequestsTests method removeAuthorizationRequestWhenMultipleThenRemovedAndSessionAttributeUpdated.

// gh-7327
@Test
public void removeAuthorizationRequestWhenMultipleThenRemovedAndSessionAttributeUpdated() {
    String oldState = "state0";
    // @formatter:off
    MockServerHttpRequest oldRequest = MockServerHttpRequest.get("/").queryParam(OAuth2ParameterNames.STATE, oldState).build();
    OAuth2AuthorizationRequest oldAuthorizationRequest = OAuth2AuthorizationRequest.authorizationCode().authorizationUri("https://example.com/oauth2/authorize").clientId("client-id").redirectUri("http://localhost/client-1").state(oldState).build();
    // @formatter:on
    Map<String, Object> sessionAttrs = spy(new HashMap<>());
    WebSession session = mock(WebSession.class);
    given(session.getAttributes()).willReturn(sessionAttrs);
    WebSessionManager sessionManager = (e) -> Mono.just(session);
    this.exchange = new DefaultServerWebExchange(this.exchange.getRequest(), new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    ServerWebExchange oldExchange = new DefaultServerWebExchange(oldRequest, new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    // @formatter:off
    Mono<OAuth2AuthorizationRequest> saveAndSaveAndRemove = this.repository.saveAuthorizationRequest(oldAuthorizationRequest, oldExchange).then(this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange)).then(this.repository.removeAuthorizationRequest(this.exchange));
    StepVerifier.create(saveAndSaveAndRemove).expectNext(this.authorizationRequest).verifyComplete();
    StepVerifier.create(this.repository.loadAuthorizationRequest(this.exchange)).verifyComplete();
    // @formatter:on
    verify(sessionAttrs, times(3)).put(any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.spy(org.mockito.Mockito.spy) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) WebSessionManager(org.springframework.web.server.session.WebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) WebSession(org.springframework.web.server.WebSession) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Mockito.mock(org.mockito.Mockito.mock) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) WebSession(org.springframework.web.server.WebSession) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebSessionManager(org.springframework.web.server.session.WebSessionManager) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 68 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class WebSessionOAuth2ServerAuthorizationRequestRepositoryAllowMultipleAuthorizationRequestsTests method removeAuthorizationRequestWhenMultipleThenOnlyOneRemoved.

@Test
public void removeAuthorizationRequestWhenMultipleThenOnlyOneRemoved() {
    String oldState = "state0";
    // @formatter:off
    MockServerHttpRequest oldRequest = MockServerHttpRequest.get("/").queryParam(OAuth2ParameterNames.STATE, oldState).build();
    OAuth2AuthorizationRequest oldAuthorizationRequest = OAuth2AuthorizationRequest.authorizationCode().authorizationUri("https://example.com/oauth2/authorize").clientId("client-id").redirectUri("http://localhost/client-1").state(oldState).build();
    // @formatter:on
    WebSessionManager sessionManager = (e) -> this.exchange.getSession();
    this.exchange = new DefaultServerWebExchange(this.exchange.getRequest(), new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    ServerWebExchange oldExchange = new DefaultServerWebExchange(oldRequest, new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    // @formatter:off
    Mono<OAuth2AuthorizationRequest> saveAndSaveAndRemove = this.repository.saveAuthorizationRequest(oldAuthorizationRequest, oldExchange).then(this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange)).then(this.repository.removeAuthorizationRequest(this.exchange));
    StepVerifier.create(saveAndSaveAndRemove).expectNext(this.authorizationRequest).verifyComplete();
    StepVerifier.create(this.repository.loadAuthorizationRequest(this.exchange)).verifyComplete();
    StepVerifier.create(this.repository.loadAuthorizationRequest(oldExchange)).expectNext(oldAuthorizationRequest).verifyComplete();
// @formatter:on
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.spy(org.mockito.Mockito.spy) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) WebSessionManager(org.springframework.web.server.session.WebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) WebSession(org.springframework.web.server.WebSession) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Mockito.mock(org.mockito.Mockito.mock) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebSessionManager(org.springframework.web.server.session.WebSessionManager) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 69 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class WebSessionOAuth2ServerAuthorizationRequestRepositoryAllowMultipleAuthorizationRequestsTests method loadAuthorizationRequestWhenMultipleSavedThenAuthorizationRequest.

@Test
public void loadAuthorizationRequestWhenMultipleSavedThenAuthorizationRequest() {
    String oldState = "state0";
    // @formatter:off
    MockServerHttpRequest oldRequest = MockServerHttpRequest.get("/").queryParam(OAuth2ParameterNames.STATE, oldState).build();
    OAuth2AuthorizationRequest oldAuthorizationRequest = OAuth2AuthorizationRequest.authorizationCode().authorizationUri("https://example.com/oauth2/authorize").clientId("client-id").redirectUri("http://localhost/client-1").state(oldState).build();
    // @formatter:on
    WebSessionManager sessionManager = (e) -> this.exchange.getSession();
    this.exchange = new DefaultServerWebExchange(this.exchange.getRequest(), new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    ServerWebExchange oldExchange = new DefaultServerWebExchange(oldRequest, new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    // @formatter:off
    Mono<OAuth2AuthorizationRequest> saveAndSaveAndLoad = this.repository.saveAuthorizationRequest(oldAuthorizationRequest, oldExchange).then(this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange)).then(this.repository.loadAuthorizationRequest(oldExchange));
    StepVerifier.create(saveAndSaveAndLoad).expectNext(oldAuthorizationRequest).verifyComplete();
    StepVerifier.create(this.repository.loadAuthorizationRequest(this.exchange)).expectNext(this.authorizationRequest).verifyComplete();
// @formatter:on
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.spy(org.mockito.Mockito.spy) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) WebSessionManager(org.springframework.web.server.session.WebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) WebSession(org.springframework.web.server.WebSession) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Mockito.mock(org.mockito.Mockito.mock) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebSessionManager(org.springframework.web.server.session.WebSessionManager) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 70 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class ServerWebExchangeDelegatingReactiveAuthenticationManagerResolverTests method resolveWhenMatchesThenReturnsReactiveAuthenticationManager.

@Test
public void resolveWhenMatchesThenReturnsReactiveAuthenticationManager() {
    ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver resolver = ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver.builder().add(new PathPatternParserServerWebExchangeMatcher("/one/**"), this.one).add(new PathPatternParserServerWebExchangeMatcher("/two/**"), this.two).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/one/location").build();
    assertThat(resolver.resolve(MockServerWebExchange.from(request)).block()).isEqualTo(this.one);
}
Also used : PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)75 Test (org.junit.jupiter.api.Test)40 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)35 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)26 URI (java.net.URI)16 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)11 HttpHeaders (org.springframework.http.HttpHeaders)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ResponseStatusException (org.springframework.web.server.ResponseStatusException)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)6 Mono (reactor.core.publisher.Mono)6 InetSocketAddress (java.net.InetSocketAddress)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)5 Mockito.mock (org.mockito.Mockito.mock)5 ObjectError (org.springframework.validation.ObjectError)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4