use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.
the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenAuthorizationSucceedsAndRequestCacheConfiguredThenRequestCacheUsed.
@Test
public void filterWhenAuthorizationSucceedsAndRequestCacheConfiguredThenRequestCacheUsed() {
ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
given(this.clientRegistrationRepository.findByRegistrationId(any())).willReturn(Mono.just(clientRegistration));
given(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).willReturn(Mono.empty());
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(TestOAuth2AuthorizationCodeAuthenticationTokens.authenticated()));
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));
MockServerHttpRequest authorizationResponse = createAuthorizationResponse(authorizationRequest);
MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
ServerRequestCache requestCache = mock(ServerRequestCache.class);
given(requestCache.getRedirectUri(any(ServerWebExchange.class))).willReturn(Mono.just(URI.create("/saved-request")));
this.filter.setRequestCache(requestCache);
this.filter.filter(exchange, chain).block();
verify(requestCache).getRedirectUri(exchange);
assertThat(exchange.getResponse().getHeaders().getLocation().toString()).isEqualTo("/saved-request");
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.
the class SwitchUserWebFilterTests method setSwitchUserMatcherWhenDefinedThenChangeDefaultValue.
@Test
public void setSwitchUserMatcherWhenDefinedThenChangeDefaultValue() {
final MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/login/impersonate"));
final ServerWebExchangeMatcher oldSwitchUserMatcher = (ServerWebExchangeMatcher) ReflectionTestUtils.getField(this.switchUserWebFilter, "switchUserMatcher");
assertThat(oldSwitchUserMatcher.matches(exchange).block().isMatch()).isTrue();
final ServerWebExchangeMatcher newSwitchUserMatcher = ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, "/switch-url");
this.switchUserWebFilter.setSwitchUserMatcher(newSwitchUserMatcher);
final ServerWebExchangeMatcher currentExitUserMatcher = (ServerWebExchangeMatcher) ReflectionTestUtils.getField(this.switchUserWebFilter, "switchUserMatcher");
assertThat(currentExitUserMatcher).isSameAs(newSwitchUserMatcher);
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.
the class SwitchUserWebFilterTests method switchUserWhenRequestNotMatchThenDoesNothing.
@Test
public void switchUserWhenRequestNotMatchThenDoesNothing() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/not/existing"));
WebFilterChain chain = mock(WebFilterChain.class);
given(chain.filter(exchange)).willReturn(Mono.empty());
this.switchUserWebFilter.filter(exchange, chain).block();
verifyNoInteractions(this.userDetailsService);
verifyNoInteractions(this.successHandler);
verifyNoInteractions(this.failureHandler);
verifyNoInteractions(this.serverSecurityContextRepository);
verify(chain).filter(exchange);
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.
the class SwitchUserWebFilterTests method setSwitchUserUrlWhenDefinedThenChangeDefaultValue.
@Test
public void setSwitchUserUrlWhenDefinedThenChangeDefaultValue() {
final MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/login/impersonate"));
final ServerWebExchangeMatcher oldSwitchUserMatcher = (ServerWebExchangeMatcher) ReflectionTestUtils.getField(this.switchUserWebFilter, "switchUserMatcher");
assertThat(oldSwitchUserMatcher.matches(exchange).block().isMatch()).isTrue();
this.switchUserWebFilter.setSwitchUserUrl("/switch-url");
final MockServerWebExchange newExchange = MockServerWebExchange.from(MockServerHttpRequest.post("/switch-url"));
final ServerWebExchangeMatcher newSwitchUserMatcher = (ServerWebExchangeMatcher) ReflectionTestUtils.getField(this.switchUserWebFilter, "switchUserMatcher");
assertThat(newSwitchUserMatcher.matches(newExchange).block().isMatch()).isTrue();
}
use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.
the class SwitchUserWebFilterTests method exitSwitchWhenNoCurrentUserThenThrowError.
@Test
public void exitSwitchWhenNoCurrentUserThenThrowError() {
final MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/logout/impersonate"));
final WebFilterChain chain = mock(WebFilterChain.class);
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class).isThrownBy(() -> this.switchUserWebFilter.filter(exchange, chain).block()).withMessage("No current user associated with this request");
verifyNoInteractions(chain);
}
Aggregations