22
22
import org .mockito .Mock ;
23
23
import org .mockito .junit .MockitoJUnitRunner ;
24
24
import org .springframework .security .core .Authentication ;
25
+ import org .springframework .security .core .userdetails .ReactiveUserDetailsPasswordService ;
25
26
import org .springframework .security .core .userdetails .ReactiveUserDetailsService ;
26
27
import org .springframework .security .core .userdetails .User ;
27
28
import org .springframework .security .core .userdetails .UserDetails ;
32
33
33
34
import static org .assertj .core .api .Assertions .*;
34
35
import static org .mockito .ArgumentMatchers .any ;
36
+ import static org .mockito .ArgumentMatchers .eq ;
35
37
import static org .mockito .Mockito .verify ;
38
+ import static org .mockito .Mockito .verifyZeroInteractions ;
36
39
import static org .mockito .Mockito .when ;
37
40
38
41
/**
@@ -47,6 +50,9 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
47
50
@ Mock
48
51
private PasswordEncoder encoder ;
49
52
53
+ @ Mock
54
+ private ReactiveUserDetailsPasswordService userDetailsPasswordService ;
55
+
50
56
@ Mock
51
57
private Scheduler scheduler ;
52
58
@@ -79,10 +85,61 @@ public void authentiateWhenCustomSchedulerThenUsed() {
79
85
this .manager .setScheduler (this .scheduler );
80
86
this .manager .setPasswordEncoder (this .encoder );
81
87
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken (
82
- this .user , this .user .getPassword ());
88
+ this .user , this .user .getPassword ());
83
89
84
90
Authentication result = this .manager .authenticate (token ).block ();
85
91
86
92
verify (this .scheduler ).schedule (any ());
87
93
}
94
+
95
+ @ Test
96
+ public void authenticateWhenPasswordServiceThenUpdated () {
97
+ String encodedPassword = "encoded" ;
98
+ when (this .userDetailsService .findByUsername (any ())).thenReturn (Mono .just (this .user ));
99
+ when (this .encoder .matches (any (), any ())).thenReturn (true );
100
+ when (this .encoder .upgradeEncoding (any ())).thenReturn (true );
101
+ when (this .encoder .encode (any ())).thenReturn (encodedPassword );
102
+ when (this .userDetailsPasswordService .updatePassword (any (), any ())).thenReturn (Mono .just (this .user ));
103
+ this .manager .setPasswordEncoder (this .encoder );
104
+ this .manager .setUserDetailsPasswordService (this .userDetailsPasswordService );
105
+ UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken (
106
+ this .user , this .user .getPassword ());
107
+
108
+ Authentication result = this .manager .authenticate (token ).block ();
109
+
110
+ verify (this .encoder ).encode (this .user .getPassword ());
111
+ verify (this .userDetailsPasswordService ).updatePassword (eq (this .user ), eq (encodedPassword ));
112
+ }
113
+
114
+ @ Test
115
+ public void authenticateWhenPasswordServiceAndBadCredentialsThenNotUpdated () {
116
+ when (this .userDetailsService .findByUsername (any ())).thenReturn (Mono .just (this .user ));
117
+ when (this .encoder .matches (any (), any ())).thenReturn (false );
118
+ when (this .userDetailsPasswordService .updatePassword (any (), any ())).thenReturn (Mono .just (this .user ));
119
+ this .manager .setPasswordEncoder (this .encoder );
120
+ this .manager .setUserDetailsPasswordService (this .userDetailsPasswordService );
121
+ UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken (
122
+ this .user , this .user .getPassword ());
123
+
124
+ assertThatThrownBy (() -> this .manager .authenticate (token ).block ())
125
+ .isInstanceOf (BadCredentialsException .class );
126
+
127
+ verifyZeroInteractions (this .userDetailsPasswordService );
128
+ }
129
+
130
+ @ Test
131
+ public void authenticateWhenPasswordServiceAndUpgradeFalseThenNotUpdated () {
132
+ when (this .userDetailsService .findByUsername (any ())).thenReturn (Mono .just (this .user ));
133
+ when (this .encoder .matches (any (), any ())).thenReturn (true );
134
+ when (this .encoder .upgradeEncoding (any ())).thenReturn (false );
135
+ when (this .userDetailsPasswordService .updatePassword (any (), any ())).thenReturn (Mono .just (this .user ));
136
+ this .manager .setPasswordEncoder (this .encoder );
137
+ this .manager .setUserDetailsPasswordService (this .userDetailsPasswordService );
138
+ UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken (
139
+ this .user , this .user .getPassword ());
140
+
141
+ Authentication result = this .manager .authenticate (token ).block ();
142
+
143
+ verifyZeroInteractions (this .userDetailsPasswordService );
144
+ }
88
145
}
0 commit comments