28
28
import org .springframework .security .oauth2 .core .endpoint .OAuth2ParameterNames ;
29
29
import reactor .core .publisher .Mono ;
30
30
import reactor .test .StepVerifier ;
31
+ import reactor .test .publisher .PublisherProbe ;
31
32
33
+ import java .util .Map ;
32
34
import java .util .function .Function ;
33
35
34
36
import static org .assertj .core .api .Assertions .assertThat ;
35
37
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
36
38
import static org .mockito .ArgumentMatchers .any ;
37
39
import static org .mockito .ArgumentMatchers .eq ;
38
- import static org .mockito .Mockito .*;
40
+ import static org .mockito .Mockito .mock ;
41
+ import static org .mockito .Mockito .never ;
42
+ import static org .mockito .Mockito .verify ;
43
+ import static org .mockito .Mockito .when ;
39
44
40
45
/**
41
- * Tests for {@link OAuth2AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager }.
46
+ * Tests for {@link AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager }.
42
47
*
43
48
* @author Ankur Pathak
49
+ * @author Phil Clay
44
50
*/
45
- public class OAuth2AuthorizedClientServiceReactiveOAuth2AuthorizedClientManagerTests {
51
+ public class AuthorizedClientServiceReactiveOAuth2AuthorizedClientManagerTests {
46
52
private ReactiveClientRegistrationRepository clientRegistrationRepository ;
47
53
private ReactiveOAuth2AuthorizedClientService authorizedClientService ;
48
54
private ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider ;
49
- private Function contextAttributesMapper ;
50
- private OAuth2AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager ;
55
+ private Function < OAuth2AuthorizeRequest , Mono < Map < String , Object >>> contextAttributesMapper ;
56
+ private AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager ;
51
57
private ClientRegistration clientRegistration ;
52
58
private Authentication principal ;
53
59
private OAuth2AuthorizedClient authorizedClient ;
54
60
private ArgumentCaptor <OAuth2AuthorizationContext > authorizationContextCaptor ;
61
+ private PublisherProbe <Void > saveAuthorizedClientProbe ;
55
62
56
63
@ SuppressWarnings ("unchecked" )
57
64
@ Before
58
65
public void setup () {
59
66
this .clientRegistrationRepository = mock (ReactiveClientRegistrationRepository .class );
60
67
this .authorizedClientService = mock (ReactiveOAuth2AuthorizedClientService .class );
68
+ this .saveAuthorizedClientProbe = PublisherProbe .empty ();
69
+ when (this .authorizedClientService .saveAuthorizedClient (any (), any ())).thenReturn (this .saveAuthorizedClientProbe .mono ());
61
70
this .authorizedClientProvider = mock (ReactiveOAuth2AuthorizedClientProvider .class );
62
71
this .contextAttributesMapper = mock (Function .class );
63
- this .authorizedClientManager = new OAuth2AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager (
72
+ when (this .contextAttributesMapper .apply (any ())).thenReturn (Mono .empty ());
73
+ this .authorizedClientManager = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager (
64
74
this .clientRegistrationRepository , this .authorizedClientService );
65
75
this .authorizedClientManager .setAuthorizedClientProvider (this .authorizedClientProvider );
66
76
this .authorizedClientManager .setContextAttributesMapper (this .contextAttributesMapper );
@@ -73,23 +83,23 @@ public void setup() {
73
83
74
84
@ Test
75
85
public void constructorWhenClientRegistrationRepositoryIsNullThenThrowIllegalArgumentException () {
76
- assertThatThrownBy (() -> new OAuth2AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager (null , this .authorizedClientService ))
86
+ assertThatThrownBy (() -> new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager (null , this .authorizedClientService ))
77
87
.isInstanceOf (IllegalArgumentException .class )
78
- .hasMessage ("reactiveClientRegistrationRepository cannot be null" );
88
+ .hasMessage ("clientRegistrationRepository cannot be null" );
79
89
}
80
90
81
91
@ Test
82
92
public void constructorWhenOAuth2AuthorizedClientServiceIsNullThenThrowIllegalArgumentException () {
83
- assertThatThrownBy (() -> new OAuth2AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager (this .clientRegistrationRepository , null ))
93
+ assertThatThrownBy (() -> new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager (this .clientRegistrationRepository , null ))
84
94
.isInstanceOf (IllegalArgumentException .class )
85
- .hasMessage ("reactiveAuthorizedClientService cannot be null" );
95
+ .hasMessage ("authorizedClientService cannot be null" );
86
96
}
87
97
88
98
@ Test
89
99
public void setAuthorizedClientProviderWhenNullThenThrowIllegalArgumentException () {
90
100
assertThatThrownBy (() -> this .authorizedClientManager .setAuthorizedClientProvider (null ))
91
101
.isInstanceOf (IllegalArgumentException .class )
92
- .hasMessage ("reactiveAuthorizedClientProvider cannot be null" );
102
+ .hasMessage ("authorizedClientProvider cannot be null" );
93
103
}
94
104
95
105
@ Test
@@ -132,7 +142,7 @@ public void authorizeWhenNotAuthorizedAndUnsupportedProviderThenNotAuthorized()
132
142
.build ();
133
143
Mono <OAuth2AuthorizedClient > authorizedClient = this .authorizedClientManager .authorize (authorizeRequest );
134
144
135
- authorizedClient . subscribe ();
145
+ StepVerifier . create ( authorizedClient ). verifyComplete ();
136
146
137
147
verify (this .authorizedClientProvider ).authorize (this .authorizationContextCaptor .capture ());
138
148
verify (this .contextAttributesMapper ).apply (eq (authorizeRequest ));
@@ -142,7 +152,6 @@ public void authorizeWhenNotAuthorizedAndUnsupportedProviderThenNotAuthorized()
142
152
assertThat (authorizationContext .getAuthorizedClient ()).isNull ();
143
153
assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
144
154
145
- StepVerifier .create (authorizedClient ).expectComplete ();
146
155
verify (this .authorizedClientService , never ()).saveAuthorizedClient (
147
156
any (OAuth2AuthorizedClient .class ), eq (this .principal ));
148
157
}
@@ -163,7 +172,9 @@ public void authorizeWhenNotAuthorizedAndSupportedProviderThenAuthorized() {
163
172
.build ();
164
173
Mono <OAuth2AuthorizedClient > authorizedClient = this .authorizedClientManager .authorize (authorizeRequest );
165
174
166
- authorizedClient .subscribe ();
175
+ StepVerifier .create (authorizedClient )
176
+ .expectNext (this .authorizedClient )
177
+ .verifyComplete ();
167
178
168
179
verify (this .authorizedClientProvider ).authorize (this .authorizationContextCaptor .capture ());
169
180
verify (this .contextAttributesMapper ).apply (eq (authorizeRequest ));
@@ -173,9 +184,9 @@ public void authorizeWhenNotAuthorizedAndSupportedProviderThenAuthorized() {
173
184
assertThat (authorizationContext .getAuthorizedClient ()).isNull ();
174
185
assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
175
186
176
- StepVerifier .create (authorizedClient ).expectNextCount (1 ).assertNext (x -> assertThat (x ).isSameAs (this .authorizedClient ));
177
187
verify (this .authorizedClientService ).saveAuthorizedClient (
178
188
eq (this .authorizedClient ), eq (this .principal ));
189
+ this .saveAuthorizedClientProbe .assertWasSubscribed ();
179
190
}
180
191
181
192
@ SuppressWarnings ("unchecked" )
@@ -197,8 +208,9 @@ public void authorizeWhenAuthorizedAndSupportedProviderThenReauthorized() {
197
208
.build ();
198
209
Mono <OAuth2AuthorizedClient > authorizedClient = this .authorizedClientManager .authorize (authorizeRequest );
199
210
200
- authorizedClient .subscribe ();
201
-
211
+ StepVerifier .create (authorizedClient )
212
+ .expectNext (reauthorizedClient )
213
+ .verifyComplete ();
202
214
verify (this .authorizedClientProvider ).authorize (this .authorizationContextCaptor .capture ());
203
215
verify (this .contextAttributesMapper ).apply (eq (authorizeRequest ));
204
216
@@ -207,9 +219,9 @@ public void authorizeWhenAuthorizedAndSupportedProviderThenReauthorized() {
207
219
assertThat (authorizationContext .getAuthorizedClient ()).isSameAs (this .authorizedClient );
208
220
assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
209
221
210
- StepVerifier .create (authorizedClient ).expectNextCount (1 ).assertNext (x -> assertThat (x ).isSameAs (this .authorizedClient ));
211
222
verify (this .authorizedClientService ).saveAuthorizedClient (
212
223
eq (reauthorizedClient ), eq (this .principal ));
224
+ this .saveAuthorizedClientProbe .assertWasSubscribed ();
213
225
}
214
226
215
227
@ SuppressWarnings ("unchecked" )
@@ -221,8 +233,9 @@ public void reauthorizeWhenUnsupportedProviderThenNotReauthorized() {
221
233
.build ();
222
234
Mono <OAuth2AuthorizedClient > authorizedClient = this .authorizedClientManager .authorize (reauthorizeRequest );
223
235
224
- authorizedClient .subscribe ();
225
-
236
+ StepVerifier .create (authorizedClient )
237
+ .expectNext (this .authorizedClient )
238
+ .verifyComplete ();
226
239
verify (this .authorizedClientProvider ).authorize (this .authorizationContextCaptor .capture ());
227
240
verify (this .contextAttributesMapper ).apply (eq (reauthorizeRequest ));
228
241
@@ -231,7 +244,6 @@ public void reauthorizeWhenUnsupportedProviderThenNotReauthorized() {
231
244
assertThat (authorizationContext .getAuthorizedClient ()).isSameAs (this .authorizedClient );
232
245
assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
233
246
234
- StepVerifier .create (authorizedClient ).expectNextCount (1 ).assertNext (x -> assertThat (x ).isSameAs (this .authorizedClient ));
235
247
verify (this .authorizedClientService , never ()).saveAuthorizedClient (
236
248
any (OAuth2AuthorizedClient .class ), eq (this .principal ));
237
249
}
@@ -250,7 +262,9 @@ public void reauthorizeWhenSupportedProviderThenReauthorized() {
250
262
.build ();
251
263
Mono <OAuth2AuthorizedClient > authorizedClient = this .authorizedClientManager .authorize (reauthorizeRequest );
252
264
253
- authorizedClient .subscribe ();
265
+ StepVerifier .create (authorizedClient )
266
+ .expectNext (reauthorizedClient )
267
+ .verifyComplete ();
254
268
255
269
verify (this .authorizedClientProvider ).authorize (this .authorizationContextCaptor .capture ());
256
270
verify (this .contextAttributesMapper ).apply (eq (reauthorizeRequest ));
@@ -260,9 +274,9 @@ public void reauthorizeWhenSupportedProviderThenReauthorized() {
260
274
assertThat (authorizationContext .getAuthorizedClient ()).isSameAs (this .authorizedClient );
261
275
assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
262
276
263
- StepVerifier .create (authorizedClient ).expectNextCount (1 ).assertNext (x -> assertThat (x ).isSameAs (this .authorizedClient ));
264
277
verify (this .authorizedClientService ).saveAuthorizedClient (
265
278
eq (reauthorizedClient ), eq (this .principal ));
279
+ this .saveAuthorizedClientProbe .assertWasSubscribed ();
266
280
}
267
281
268
282
@ SuppressWarnings ("unchecked" )
@@ -274,14 +288,20 @@ public void reauthorizeWhenRequestAttributeScopeThenMappedToContext() {
274
288
275
289
when (this .authorizedClientProvider .authorize (any (OAuth2AuthorizationContext .class ))).thenReturn (Mono .just (reauthorizedClient ));
276
290
277
-
278
291
OAuth2AuthorizeRequest reauthorizeRequest = OAuth2AuthorizeRequest .withAuthorizedClient (this .authorizedClient )
279
292
.principal (this .principal )
280
293
.attribute (OAuth2ParameterNames .SCOPE , "read write" )
281
294
.build ();
295
+
296
+ this .authorizedClientManager .setContextAttributesMapper (new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager .DefaultContextAttributesMapper ());
282
297
Mono <OAuth2AuthorizedClient > authorizedClient = this .authorizedClientManager .authorize (reauthorizeRequest );
283
298
284
- authorizedClient .subscribe ();
299
+ StepVerifier .create (authorizedClient )
300
+ .expectNext (reauthorizedClient )
301
+ .verifyComplete ();
302
+ verify (this .authorizedClientService ).saveAuthorizedClient (
303
+ eq (reauthorizedClient ), eq (this .principal ));
304
+ this .saveAuthorizedClientProbe .assertWasSubscribed ();
285
305
286
306
verify (this .authorizedClientProvider ).authorize (this .authorizationContextCaptor .capture ());
287
307
@@ -293,8 +313,5 @@ public void reauthorizeWhenRequestAttributeScopeThenMappedToContext() {
293
313
String [] requestScopeAttribute = authorizationContext .getAttribute (OAuth2AuthorizationContext .REQUEST_SCOPE_ATTRIBUTE_NAME );
294
314
assertThat (requestScopeAttribute ).contains ("read" , "write" );
295
315
296
- StepVerifier .create (authorizedClient ).expectNextCount (1 ).assertNext (x -> assertThat (x ).isSameAs (this .authorizedClient ));
297
- verify (this .authorizedClientService ).saveAuthorizedClient (
298
- eq (reauthorizedClient ), eq (this .principal ));
299
316
}
300
317
}
0 commit comments