1616
1717package org .springframework .security .authentication ;
1818
19+ import static org .assertj .core .api .Assertions .*;
20+ import static org .mockito .Mockito .*;
21+
1922import org .junit .Before ;
2023import org .junit .Test ;
2124import org .junit .runner .RunWith ;
2225import org .mockito .Mock ;
2326import org .mockito .junit .MockitoJUnitRunner ;
27+
28+ import reactor .core .publisher .Mono ;
29+ import reactor .core .scheduler .Scheduler ;
30+ import reactor .core .scheduler .Schedulers ;
31+
2432import org .springframework .security .core .Authentication ;
2533import org .springframework .security .core .userdetails .ReactiveUserDetailsPasswordService ;
2634import org .springframework .security .core .userdetails .ReactiveUserDetailsService ;
2735import org .springframework .security .core .userdetails .User ;
2836import org .springframework .security .core .userdetails .UserDetails ;
37+ import org .springframework .security .core .userdetails .UserDetailsChecker ;
2938import org .springframework .security .crypto .password .PasswordEncoder ;
30- import reactor .core .publisher .Mono ;
31- import reactor .core .scheduler .Scheduler ;
32- import reactor .core .scheduler .Schedulers ;
33-
34- import static org .assertj .core .api .Assertions .*;
35- import static org .mockito .ArgumentMatchers .any ;
36- import static org .mockito .ArgumentMatchers .eq ;
37- import static org .mockito .Mockito .verify ;
38- import static org .mockito .Mockito .verifyZeroInteractions ;
39- import static org .mockito .Mockito .when ;
4039
4140/**
4241 * @author Rob Winch
@@ -56,6 +55,9 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
5655 @ Mock
5756 private Scheduler scheduler ;
5857
58+ @ Mock
59+ private UserDetailsChecker postAuthenticationChecks ;
60+
5961 private UserDetails user = User .withUsername ("user" )
6062 .password ("password" )
6163 .roles ("USER" )
@@ -140,4 +142,33 @@ public void authenticateWhenPasswordServiceAndUpgradeFalseThenNotUpdated() {
140142
141143 verifyZeroInteractions (this .userDetailsPasswordService );
142144 }
145+
146+ @ Test
147+ public void authenticateWhenPostAuthenticationChecksFail () {
148+ when (this .userDetailsService .findByUsername (any ())).thenReturn (Mono .just (this .user ));
149+ doThrow (new LockedException ("account is locked" )).when (this .postAuthenticationChecks ).check (any ());
150+ when (this .encoder .matches (any (), any ())).thenReturn (true );
151+ this .manager .setPasswordEncoder (this .encoder );
152+ this .manager .setPostAuthenticationChecks (this .postAuthenticationChecks );
153+
154+ assertThatExceptionOfType (LockedException .class )
155+ .isThrownBy (() -> this .manager .authenticate (new UsernamePasswordAuthenticationToken (this .user , this .user .getPassword ())).block ())
156+ .withMessage ("account is locked" );
157+
158+ verify (this .postAuthenticationChecks ).check (eq (this .user ));
159+ }
160+
161+ @ Test
162+ public void authenticateWhenPostAuthenticationChecksNotSet () {
163+ when (this .userDetailsService .findByUsername (any ())).thenReturn (Mono .just (this .user ));
164+ when (this .encoder .matches (any (), any ())).thenReturn (true );
165+ this .manager .setPasswordEncoder (this .encoder );
166+
167+ UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken (
168+ this .user , this .user .getPassword ());
169+
170+ this .manager .authenticate (token ).block ();
171+
172+ verifyZeroInteractions (this .postAuthenticationChecks );
173+ }
143174}
0 commit comments