Search in sources :

Example 11 with MockServerHttpResponse

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);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 12 with MockServerHttpResponse

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);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 13 with MockServerHttpResponse

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);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChain(org.springframework.web.server.WebFilterChain) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with MockServerHttpResponse

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
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) Authentication(org.springframework.security.core.Authentication) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 15 with MockServerHttpResponse

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\""));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpResponse (org.springframework.mock.http.server.reactive.MockServerHttpResponse)15 Test (org.junit.jupiter.api.Test)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)9 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)8 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)7 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Mono (reactor.core.publisher.Mono)6 Mockito.mock (org.mockito.Mockito.mock)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 Mockito.spy (org.mockito.Mockito.spy)4 Mockito.times (org.mockito.Mockito.times)4 Mockito.verify (org.mockito.Mockito.verify)4 ServerCodecConfigurer (org.springframework.http.codec.ServerCodecConfigurer)4 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)4 OAuth2ParameterNames (org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames)4 WebSession (org.springframework.web.server.WebSession)4 DefaultServerWebExchange (org.springframework.web.server.adapter.DefaultServerWebExchange)4