@@ -65,6 +65,7 @@ public class DefaultReactiveOAuth2AuthorizedClientManagerTests {
65
65
private MockServerWebExchange serverWebExchange ;
66
66
private Context context ;
67
67
private ArgumentCaptor <OAuth2AuthorizationContext > authorizationContextCaptor ;
68
+ private PublisherProbe <OAuth2AuthorizedClient > loadAuthorizedClientProbe ;
68
69
private PublisherProbe <Void > saveAuthorizedClientProbe ;
69
70
70
71
@ SuppressWarnings ("unchecked" )
@@ -74,8 +75,9 @@ public void setup() {
74
75
when (this .clientRegistrationRepository .findByRegistrationId (
75
76
anyString ())).thenReturn (Mono .empty ());
76
77
this .authorizedClientRepository = mock (ServerOAuth2AuthorizedClientRepository .class );
78
+ this .loadAuthorizedClientProbe = PublisherProbe .empty ();
77
79
when (this .authorizedClientRepository .loadAuthorizedClient (
78
- anyString (), any (Authentication .class ), any (ServerWebExchange .class ))).thenReturn (Mono . empty ());
80
+ anyString (), any (Authentication .class ), any (ServerWebExchange .class ))).thenReturn (this . loadAuthorizedClientProbe . mono ());
79
81
this .saveAuthorizedClientProbe = PublisherProbe .empty ();
80
82
when (this .authorizedClientRepository .saveAuthorizedClient (
81
83
any (OAuth2AuthorizedClient .class ), any (Authentication .class ), any (ServerWebExchange .class ))).thenReturn (this .saveAuthorizedClientProbe .mono ());
@@ -131,6 +133,16 @@ public void authorizeWhenRequestIsNullThenThrowIllegalArgumentException() {
131
133
.hasMessage ("authorizeRequest cannot be null" );
132
134
}
133
135
136
+ @ Test
137
+ public void authorizeWhenExchangeIsNullThenThrowIllegalArgumentException () {
138
+ OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest .withClientRegistrationId (this .clientRegistration .getRegistrationId ())
139
+ .principal (this .principal )
140
+ .build ();
141
+ assertThatThrownBy (() -> this .authorizedClientManager .authorize (authorizeRequest ).block ())
142
+ .isInstanceOf (IllegalArgumentException .class )
143
+ .hasMessage ("serverWebExchange cannot be null" );
144
+ }
145
+
134
146
@ Test
135
147
public void authorizeWhenClientRegistrationNotFoundThenThrowIllegalArgumentException () {
136
148
OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest .withClientRegistrationId ("invalid-registration-id" )
@@ -162,7 +174,8 @@ public void authorizeWhenNotAuthorizedAndUnsupportedProviderThenNotAuthorized()
162
174
assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
163
175
164
176
assertThat (authorizedClient ).isNull ();
165
- verify (this .authorizedClientRepository , never ()).saveAuthorizedClient (any (), any (), any ());
177
+ this .loadAuthorizedClientProbe .assertWasSubscribed ();
178
+ this .saveAuthorizedClientProbe .assertWasNotSubscribed ();
166
179
}
167
180
168
181
@ SuppressWarnings ("unchecked" )
@@ -193,38 +206,14 @@ public void authorizeWhenNotAuthorizedAndSupportedProviderThenAuthorized() {
193
206
this .saveAuthorizedClientProbe .assertWasSubscribed ();
194
207
}
195
208
196
- @ Test
197
- public void authorizeWhenNotAuthorizedAndSupportedProviderAndExchangeUnavailableThenAuthorizedButNotSaved () {
198
- when (this .clientRegistrationRepository .findByRegistrationId (
199
- eq (this .clientRegistration .getRegistrationId ()))).thenReturn (Mono .just (this .clientRegistration ));
200
-
201
- when (this .authorizedClientProvider .authorize (
202
- any (OAuth2AuthorizationContext .class ))).thenReturn (Mono .just (this .authorizedClient ));
203
-
204
- OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest .withClientRegistrationId (this .clientRegistration .getRegistrationId ())
205
- .principal (this .principal )
206
- .build ();
207
- OAuth2AuthorizedClient authorizedClient = this .authorizedClientManager .authorize (authorizeRequest ).block ();
208
-
209
- verify (this .authorizedClientProvider ).authorize (this .authorizationContextCaptor .capture ());
210
- verify (this .contextAttributesMapper ).apply (eq (authorizeRequest ));
211
-
212
- OAuth2AuthorizationContext authorizationContext = this .authorizationContextCaptor .getValue ();
213
- assertThat (authorizationContext .getClientRegistration ()).isEqualTo (this .clientRegistration );
214
- assertThat (authorizationContext .getAuthorizedClient ()).isNull ();
215
- assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
216
-
217
- assertThat (authorizedClient ).isSameAs (this .authorizedClient );
218
- verify (this .authorizedClientRepository , never ()).saveAuthorizedClient (any (), any (), any ());
219
- }
220
-
221
209
@ SuppressWarnings ("unchecked" )
222
210
@ Test
223
211
public void authorizeWhenAuthorizedAndSupportedProviderThenReauthorized () {
224
212
when (this .clientRegistrationRepository .findByRegistrationId (
225
213
eq (this .clientRegistration .getRegistrationId ()))).thenReturn (Mono .just (this .clientRegistration ));
214
+ this .loadAuthorizedClientProbe = PublisherProbe .of (Mono .just (this .authorizedClient ));
226
215
when (this .authorizedClientRepository .loadAuthorizedClient (
227
- eq (this .clientRegistration .getRegistrationId ()), eq (this .principal ), eq (this .serverWebExchange ))).thenReturn (Mono . just ( this .authorizedClient ));
216
+ eq (this .clientRegistration .getRegistrationId ()), eq (this .principal ), eq (this .serverWebExchange ))).thenReturn (this .loadAuthorizedClientProbe . mono ( ));
228
217
229
218
OAuth2AuthorizedClient reauthorizedClient = new OAuth2AuthorizedClient (
230
219
this .clientRegistration , this .principal .getName (),
@@ -313,7 +302,7 @@ public void reauthorizeWhenUnsupportedProviderThenNotReauthorized() {
313
302
assertThat (authorizationContext .getPrincipal ()).isEqualTo (this .principal );
314
303
315
304
assertThat (authorizedClient ).isSameAs (this .authorizedClient );
316
- verify ( this .authorizedClientRepository , never ()). saveAuthorizedClient ( any (), any (), any () );
305
+ this .saveAuthorizedClientProbe . assertWasNotSubscribed ( );
317
306
}
318
307
319
308
@ SuppressWarnings ("unchecked" )
0 commit comments