use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.
the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenAuthenticationManagerThrowsOAuth2AuthorizationExceptionThenMappedToOAuth2AuthenticationException.
// gh-8609
@Test
public void filterWhenAuthenticationManagerThrowsOAuth2AuthorizationExceptionThenMappedToOAuth2AuthenticationException() {
ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
given(this.clientRegistrationRepository.findByRegistrationId(any())).willReturn(Mono.just(clientRegistration));
MockServerHttpRequest authorizationRequest = createAuthorizationRequest("/authorization/callback");
OAuth2AuthorizationRequest oauth2AuthorizationRequest = createOAuth2AuthorizationRequest(authorizationRequest, clientRegistration);
given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
given(this.authorizationRequestRepository.removeAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
given(this.authenticationManager.authenticate(any())).willReturn(Mono.error(new OAuth2AuthorizationException(new OAuth2Error("authorization_error"))));
MockServerHttpRequest authorizationResponse = createAuthorizationResponse(authorizationRequest);
MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.filter.filter(exchange, chain).block()).satisfies((ex) -> assertThat(ex.getError()).extracting("errorCode").isEqualTo("authorization_error"));
}
use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.
the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenMatchThenAuthorizedClientSaved.
@Test
public void filterWhenMatchThenAuthorizedClientSaved() {
ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
given(this.clientRegistrationRepository.findByRegistrationId(any())).willReturn(Mono.just(clientRegistration));
MockServerHttpRequest authorizationRequest = createAuthorizationRequest("/authorization/callback");
OAuth2AuthorizationRequest oauth2AuthorizationRequest = createOAuth2AuthorizationRequest(authorizationRequest, clientRegistration);
given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
given(this.authorizationRequestRepository.removeAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
given(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).willReturn(Mono.empty());
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(TestOAuth2AuthorizationCodeAuthenticationTokens.authenticated()));
MockServerHttpRequest authorizationResponse = createAuthorizationResponse(authorizationRequest);
MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
this.filter.filter(exchange, chain).block();
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), any(AnonymousAuthenticationToken.class), any());
}
use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.
the class ServerWebExchangeDelegatingReactiveAuthenticationManagerResolverTests method resolveWhenDoesNotMatchThenReturnsDefaultReactiveAuthenticationManager.
@Test
public void resolveWhenDoesNotMatchThenReturnsDefaultReactiveAuthenticationManager() {
ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver resolver = ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver.builder().add(new PathPatternParserServerWebExchangeMatcher("/one/**"), this.one).add(new PathPatternParserServerWebExchangeMatcher("/two/**"), this.two).build();
MockServerHttpRequest request = MockServerHttpRequest.get("/wrong/location").build();
ReactiveAuthenticationManager authenticationManager = resolver.resolve(MockServerWebExchange.from(request)).block();
Authentication authentication = new TestingAuthenticationToken("principal", "creds");
assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(() -> authenticationManager.authenticate(authentication).block());
}
use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.
the class AuthenticationConverterServerWebExchangeMatcherTests method setup.
@BeforeEach
public void setup() {
MockServerHttpRequest request = MockServerHttpRequest.get("/path").build();
this.exchange = MockServerWebExchange.from(request);
this.matcher = new AuthenticationConverterServerWebExchangeMatcher(this.converter);
}
use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.
the class PathMatcherServerWebExchangeMatcherTests method setup.
@BeforeEach
public void setup() {
MockServerHttpRequest request = MockServerHttpRequest.post("/path").build();
MockServerHttpResponse response = new MockServerHttpResponse();
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
this.exchange = MockServerWebExchange.from(request);
this.path = "/path";
this.matcher = new PathPatternParserServerWebExchangeMatcher(this.pattern);
}
Aggregations