use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenConvertAndAuthenticationSuccessThenSuccess.
@Test
public void filterWhenConvertAndAuthenticationSuccessThenSuccess() {
Mono<Authentication> authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER"));
given(this.authenticationConverter.convert(any())).willReturn(authentication);
given(this.authenticationManager.authenticate(any())).willReturn(authentication);
given(this.successHandler.onAuthenticationSuccess(any(), any())).willReturn(Mono.empty());
given(this.securityContextRepository.save(any(), any())).willAnswer((a) -> Mono.just(a.getArguments()[0]));
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
client.get().uri("/").exchange().expectStatus().isOk().expectBody().isEmpty();
verify(this.successHandler).onAuthenticationSuccess(any(), eq(authentication.block()));
verify(this.securityContextRepository).save(any(), any());
verifyZeroInteractions(this.failureHandler);
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenNotMatchAndConvertAndAuthenticationSuccessThenContinues.
@Test
public void filterWhenNotMatchAndConvertAndAuthenticationSuccessThenContinues() {
this.filter.setRequiresAuthenticationMatcher((e) -> ServerWebExchangeMatcher.MatchResult.notMatch());
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
EntityExchangeResult<String> result = client.get().uri("/").headers((headers) -> headers.setBasicAuth("test", "this")).exchange().expectStatus().isOk().expectBody(String.class).consumeWith((b) -> assertThat(b.getResponseBody()).isEqualTo("ok")).returnResult();
assertThat(result.getResponseCookies()).isEmpty();
verifyZeroInteractions(this.authenticationConverter, this.authenticationManager, this.successHandler);
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenConvertErrorThenServerError.
@Test
public void filterWhenConvertErrorThenServerError() {
given(this.authenticationConverter.convert(any())).willReturn(Mono.error(new RuntimeException("Unexpected")));
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
client.get().uri("/").exchange().expectStatus().is5xxServerError().expectBody().isEmpty();
verify(this.securityContextRepository, never()).save(any(), any());
verifyZeroInteractions(this.authenticationManager, this.successHandler, this.failureHandler);
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenDefaultsAndNoAuthenticationThenContinues.
@Test
public void filterWhenDefaultsAndNoAuthenticationThenContinues() {
this.filter = new AuthenticationWebFilter(this.authenticationManager);
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
EntityExchangeResult<String> result = client.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).consumeWith((b) -> assertThat(b.getResponseBody()).isEqualTo("ok")).returnResult();
verifyZeroInteractions(this.authenticationManager);
assertThat(result.getResponseCookies()).isEmpty();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenDefaultsAndAuthenticationSuccessThenContinues.
@Test
public void filterWhenDefaultsAndAuthenticationSuccessThenContinues() {
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("test", "this", "ROLE")));
this.filter = new AuthenticationWebFilter(this.authenticationManager);
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
EntityExchangeResult<String> result = client.get().uri("/").headers((headers) -> headers.setBasicAuth("test", "this")).exchange().expectStatus().isOk().expectBody(String.class).consumeWith((b) -> assertThat(b.getResponseBody()).isEqualTo("ok")).returnResult();
assertThat(result.getResponseCookies()).isEmpty();
}
Aggregations