mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-28 14:52:24 +00:00
Polish AuthenticationWebFilterTests
This commit is contained in:
parent
3e399f03a4
commit
0a5116ba76
@ -52,30 +52,30 @@ import static org.springframework.web.reactive.function.client.ExchangeFilterFun
|
|||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class AuthenticationWebFilterTests {
|
public class AuthenticationWebFilterTests {
|
||||||
@Mock
|
@Mock
|
||||||
AuthenticationSuccessHandler successHandler;
|
private AuthenticationSuccessHandler successHandler;
|
||||||
@Mock
|
@Mock
|
||||||
Function<ServerWebExchange,Mono<Authentication>> authenticationConverter;
|
private Function<ServerWebExchange,Mono<Authentication>> authenticationConverter;
|
||||||
@Mock
|
@Mock
|
||||||
ReactiveAuthenticationManager authenticationManager;
|
private ReactiveAuthenticationManager authenticationManager;
|
||||||
@Mock
|
@Mock
|
||||||
AuthenticationEntryPoint entryPoint;
|
private AuthenticationEntryPoint entryPoint;
|
||||||
|
|
||||||
AuthenticationWebFilter filter;
|
private AuthenticationWebFilter filter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
filter = new AuthenticationWebFilter(authenticationManager);
|
this.filter = new AuthenticationWebFilter(this.authenticationManager);
|
||||||
filter.setAuthenticationSuccessHandler(successHandler);
|
this.filter.setAuthenticationSuccessHandler(this.successHandler);
|
||||||
filter.setAuthenticationConverter(authenticationConverter);
|
this.filter.setAuthenticationConverter(this.authenticationConverter);
|
||||||
filter.setEntryPoint(entryPoint);
|
this.filter.setEntryPoint(this.entryPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenDefaultsAndNoAuthenticationThenContinues() {
|
public void filterWhenDefaultsAndNoAuthenticationThenContinues() {
|
||||||
filter = new AuthenticationWebFilter(authenticationManager);
|
this.filter = new AuthenticationWebFilter(this.authenticationManager);
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
EntityExchangeResult<String> result = client.get()
|
EntityExchangeResult<String> result = client.get()
|
||||||
@ -85,17 +85,17 @@ public class AuthenticationWebFilterTests {
|
|||||||
.expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok"))
|
.expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok"))
|
||||||
.returnResult();
|
.returnResult();
|
||||||
|
|
||||||
verifyZeroInteractions(authenticationManager);
|
verifyZeroInteractions(this.authenticationManager);
|
||||||
assertThat(result.getResponseCookies()).isEmpty();
|
assertThat(result.getResponseCookies()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenDefaultsAndAuthenticationSuccessThenContinues() {
|
public void filterWhenDefaultsAndAuthenticationSuccessThenContinues() {
|
||||||
when(authenticationManager.authenticate(any())).thenReturn(Mono.just(new TestingAuthenticationToken("test","this", "ROLE")));
|
when(this.authenticationManager.authenticate(any())).thenReturn(Mono.just(new TestingAuthenticationToken("test","this", "ROLE")));
|
||||||
filter = new AuthenticationWebFilter(authenticationManager);
|
this.filter = new AuthenticationWebFilter(this.authenticationManager);
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
EntityExchangeResult<String> result = client
|
EntityExchangeResult<String> result = client
|
||||||
@ -114,11 +114,11 @@ public class AuthenticationWebFilterTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenDefaultsAndAuthenticationFailThenUnauthorized() {
|
public void filterWhenDefaultsAndAuthenticationFailThenUnauthorized() {
|
||||||
when(authenticationManager.authenticate(any())).thenReturn(Mono.error(new BadCredentialsException("failed")));
|
when(this.authenticationManager.authenticate(any())).thenReturn(Mono.error(new BadCredentialsException("failed")));
|
||||||
filter = new AuthenticationWebFilter(authenticationManager);
|
this.filter = new AuthenticationWebFilter(this.authenticationManager);
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
EntityExchangeResult<Void> result = client
|
EntityExchangeResult<Void> result = client
|
||||||
@ -137,10 +137,10 @@ public class AuthenticationWebFilterTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenConvertEmptyThenOk() {
|
public void filterWhenConvertEmptyThenOk() {
|
||||||
when(authenticationConverter.apply(any())).thenReturn(Mono.empty());
|
when(this.authenticationConverter.apply(any())).thenReturn(Mono.empty());
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
EntityExchangeResult<String> result = client
|
EntityExchangeResult<String> result = client
|
||||||
@ -151,15 +151,16 @@ public class AuthenticationWebFilterTests {
|
|||||||
.expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok"))
|
.expectBody(String.class).consumeWith(b -> assertThat(b.getResponseBody()).isEqualTo("ok"))
|
||||||
.returnResult();
|
.returnResult();
|
||||||
|
|
||||||
verifyZeroInteractions(authenticationManager, successHandler, entryPoint);
|
verifyZeroInteractions(this.authenticationManager, this.successHandler,
|
||||||
|
this.entryPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenConvertErrorThenServerError() {
|
public void filterWhenConvertErrorThenServerError() {
|
||||||
when(authenticationConverter.apply(any())).thenReturn(Mono.error(new RuntimeException("Unexpected")));
|
when(this.authenticationConverter.apply(any())).thenReturn(Mono.error(new RuntimeException("Unexpected")));
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
client
|
client
|
||||||
@ -169,18 +170,19 @@ public class AuthenticationWebFilterTests {
|
|||||||
.expectStatus().is5xxServerError()
|
.expectStatus().is5xxServerError()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
|
|
||||||
verifyZeroInteractions(authenticationManager, successHandler, entryPoint);
|
verifyZeroInteractions(this.authenticationManager, this.successHandler,
|
||||||
|
this.entryPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenConvertAndAuthenticationSuccessThenSuccessHandler() {
|
public void filterWhenConvertAndAuthenticationSuccessThenSuccessHandler() {
|
||||||
Mono<Authentication> authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER"));
|
Mono<Authentication> authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER"));
|
||||||
when(authenticationConverter.apply(any())).thenReturn(authentication);
|
when(this.authenticationConverter.apply(any())).thenReturn(authentication);
|
||||||
when(authenticationManager.authenticate(any())).thenReturn(authentication);
|
when(this.authenticationManager.authenticate(any())).thenReturn(authentication);
|
||||||
when(successHandler.success(any(),any(),any())).thenReturn(Mono.empty());
|
when(this.successHandler.success(any(),any(),any())).thenReturn(Mono.empty());
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
client
|
client
|
||||||
@ -190,19 +192,19 @@ public class AuthenticationWebFilterTests {
|
|||||||
.expectStatus().isOk()
|
.expectStatus().isOk()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
|
|
||||||
verify(successHandler).success(eq(authentication.block()), any(), any());
|
verify(this.successHandler).success(eq(authentication.block()), any(), any());
|
||||||
verifyZeroInteractions(entryPoint);
|
verifyZeroInteractions(this.entryPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenConvertAndAuthenticationFailThenEntryPoint() {
|
public void filterWhenConvertAndAuthenticationFailThenEntryPoint() {
|
||||||
Mono<Authentication> authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER"));
|
Mono<Authentication> authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER"));
|
||||||
when(authenticationConverter.apply(any())).thenReturn(authentication);
|
when(this.authenticationConverter.apply(any())).thenReturn(authentication);
|
||||||
when(authenticationManager.authenticate(any())).thenReturn(Mono.error(new BadCredentialsException("Failed")));
|
when(this.authenticationManager.authenticate(any())).thenReturn(Mono.error(new BadCredentialsException("Failed")));
|
||||||
when(entryPoint.commence(any(),any())).thenReturn(Mono.empty());
|
when(this.entryPoint.commence(any(),any())).thenReturn(Mono.empty());
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
client
|
client
|
||||||
@ -212,19 +214,19 @@ public class AuthenticationWebFilterTests {
|
|||||||
.expectStatus().isOk()
|
.expectStatus().isOk()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
|
|
||||||
verify(entryPoint).commence(any(),any());
|
verify(this.entryPoint).commence(any(),any());
|
||||||
verifyZeroInteractions(successHandler);
|
verifyZeroInteractions(this.successHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWhenConvertAndAuthenticationExceptionThenServerError() {
|
public void filterWhenConvertAndAuthenticationExceptionThenServerError() {
|
||||||
Mono<Authentication> authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER"));
|
Mono<Authentication> authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER"));
|
||||||
when(authenticationConverter.apply(any())).thenReturn(authentication);
|
when(this.authenticationConverter.apply(any())).thenReturn(authentication);
|
||||||
when(authenticationManager.authenticate(any())).thenReturn(Mono.error(new RuntimeException("Failed")));
|
when(this.authenticationManager.authenticate(any())).thenReturn(Mono.error(new RuntimeException("Failed")));
|
||||||
when(entryPoint.commence(any(),any())).thenReturn(Mono.empty());
|
when(this.entryPoint.commence(any(),any())).thenReturn(Mono.empty());
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(filter)
|
.bindToWebFilters(this.filter)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
client
|
client
|
||||||
@ -234,6 +236,6 @@ public class AuthenticationWebFilterTests {
|
|||||||
.expectStatus().is5xxServerError()
|
.expectStatus().is5xxServerError()
|
||||||
.expectBody().isEmpty();
|
.expectBody().isEmpty();
|
||||||
|
|
||||||
verifyZeroInteractions(successHandler, entryPoint);
|
verifyZeroInteractions(this.successHandler, this.entryPoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user