16
16
17
17
package org .springframework .security .authentication ;
18
18
19
+ import static org .assertj .core .api .Assertions .*;
20
+ import static org .mockito .Mockito .*;
21
+
19
22
import org .junit .Before ;
20
23
import org .junit .Test ;
21
24
import org .junit .runner .RunWith ;
22
25
import org .mockito .Mock ;
23
26
import org .mockito .junit .MockitoJUnitRunner ;
27
+
28
+ import reactor .core .publisher .Mono ;
29
+ import reactor .core .scheduler .Scheduler ;
30
+ import reactor .core .scheduler .Schedulers ;
31
+
24
32
import org .springframework .security .core .Authentication ;
25
33
import org .springframework .security .core .userdetails .ReactiveUserDetailsPasswordService ;
26
34
import org .springframework .security .core .userdetails .ReactiveUserDetailsService ;
27
35
import org .springframework .security .core .userdetails .User ;
28
36
import org .springframework .security .core .userdetails .UserDetails ;
37
+ import org .springframework .security .core .userdetails .UserDetailsChecker ;
29
38
import 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 ;
40
39
41
40
/**
42
41
* @author Rob Winch
@@ -56,6 +55,9 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
56
55
@ Mock
57
56
private Scheduler scheduler ;
58
57
58
+ @ Mock
59
+ private UserDetailsChecker postAuthenticationChecks ;
60
+
59
61
private UserDetails user = User .withUsername ("user" )
60
62
.password ("password" )
61
63
.roles ("USER" )
@@ -140,4 +142,33 @@ public void authenticateWhenPasswordServiceAndUpgradeFalseThenNotUpdated() {
140
142
141
143
verifyZeroInteractions (this .userDetailsPasswordService );
142
144
}
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
+ }
143
174
}
0 commit comments