use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-security by spring-projects.
the class ExceptionTranslationWebFilterTests method filterWhenDefaultsAndAccessDeniedExceptionAndNotAuthenticatedThenUnauthorized.
@Test
public void filterWhenDefaultsAndAccessDeniedExceptionAndNotAuthenticatedThenUnauthorized() {
given(this.exchange.getResponse()).willReturn(new MockServerHttpResponse());
this.filter = new ExceptionTranslationWebFilter();
given(this.exchange.getPrincipal()).willReturn(Mono.empty());
given(this.chain.filter(this.exchange)).willReturn(Mono.error(new AccessDeniedException("Not Authorized")));
StepVerifier.create(this.filter.filter(this.exchange, this.chain)).expectComplete().verify();
assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-security by spring-projects.
the class ExceptionTranslationWebFilterTests method filterWhenDefaultsAndAccessDeniedExceptionAndAuthenticatedThenForbidden.
@Test
public void filterWhenDefaultsAndAccessDeniedExceptionAndAuthenticatedThenForbidden() {
given(this.exchange.getResponse()).willReturn(new MockServerHttpResponse());
this.filter = new ExceptionTranslationWebFilter();
given(this.exchange.getPrincipal()).willReturn(Mono.just(this.principal));
given(this.chain.filter(this.exchange)).willReturn(Mono.error(new AccessDeniedException("Not Authorized")));
StepVerifier.create(this.filter.filter(this.exchange, this.chain)).expectComplete().verify();
assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
}
use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-security by spring-projects.
the class OidcClientInitiatedServerLogoutSuccessHandlerTests method setup.
@BeforeEach
public void setup() {
this.exchange = mock(ServerWebExchange.class);
given(this.exchange.getResponse()).willReturn(new MockServerHttpResponse());
given(this.exchange.getRequest()).willReturn(MockServerHttpRequest.get("/").build());
this.chain = mock(WebFilterChain.class);
this.handler = new OidcClientInitiatedServerLogoutSuccessHandler(this.repository);
}
use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-security by spring-projects.
the class BearerTokenServerAccessDeniedHandlerTests method handleWhenOAuth2AuthenticatedThenStatus403AndAuthHeaderWithInsufficientScopeErrorAttribute.
@Test
public void handleWhenOAuth2AuthenticatedThenStatus403AndAuthHeaderWithInsufficientScopeErrorAttribute() {
Authentication token = new TestingOAuth2TokenAuthenticationToken(Collections.emptyMap());
ServerWebExchange exchange = mock(ServerWebExchange.class);
given(exchange.getPrincipal()).willReturn(Mono.just(token));
given(exchange.getResponse()).willReturn(new MockServerHttpResponse());
this.accessDeniedHandler.handle(exchange, null).block();
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
// @formatter:off
assertThat(exchange.getResponse().getHeaders().get("WWW-Authenticate")).isEqualTo(Arrays.asList("Bearer error=\"insufficient_scope\", " + "error_description=\"The request requires higher privileges than provided by the access token.\", " + "error_uri=\"https://tools.ietf.org/html/rfc6750#section-3.1\""));
// @formatter:on
}
use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-security by spring-projects.
the class BearerTokenServerAccessDeniedHandlerTests method handleWhenNotOAuth2AuthenticatedAndRealmSetThenStatus403AndAuthHeaderWithRealm.
@Test
public void handleWhenNotOAuth2AuthenticatedAndRealmSetThenStatus403AndAuthHeaderWithRealm() {
Authentication token = new TestingAuthenticationToken("user", "pass");
ServerWebExchange exchange = mock(ServerWebExchange.class);
given(exchange.getPrincipal()).willReturn(Mono.just(token));
given(exchange.getResponse()).willReturn(new MockServerHttpResponse());
this.accessDeniedHandler.setRealmName("test");
this.accessDeniedHandler.handle(exchange, null).block();
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(exchange.getResponse().getHeaders().get("WWW-Authenticate")).isEqualTo(Arrays.asList("Bearer realm=\"test\""));
}
Aggregations