1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import org .springframework .security .oauth2 .client .registration .TestClientRegistrations ;
29
29
import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
30
30
31
- import static org .assertj .core .api .Assertions .assertThat ;
32
- import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
33
- import static org .assertj .core .api .Assertions .assertThatObject ;
34
- import static org .mockito .ArgumentMatchers .eq ;
35
- import static org .mockito .BDDMockito .given ;
36
- import static org .mockito .Mockito .mock ;
31
+ import static org .assertj .core .api .Assertions .*;
32
+ import static org .mockito .ArgumentMatchers .*;
33
+ import static org .mockito .BDDMockito .*;
37
34
38
35
/**
39
36
* Tests for {@link InMemoryOAuth2AuthorizedClientService}.
@@ -52,9 +49,9 @@ public class InMemoryOAuth2AuthorizedClientServiceTests {
52
49
private ClientRegistration registration2 = TestClientRegistrations .clientRegistration2 ().build ();
53
50
54
51
private ClientRegistration registration3 = TestClientRegistrations .clientRegistration ()
55
- .clientId ("client-3" )
56
- .registrationId ("registration-3" )
57
- .build ();
52
+ .clientId ("client-3" )
53
+ .registrationId ("registration-3" )
54
+ .build ();
58
55
59
56
private ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository (
60
57
this .registration1 , this .registration2 , this .registration3 );
@@ -79,9 +76,11 @@ public void constructorWhenAuthorizedClientsIsNullThenThrowIllegalArgumentExcept
79
76
@ Test
80
77
public void constructorWhenAuthorizedClientsProvidedThenUseProvidedAuthorizedClients () {
81
78
String registrationId = this .registration3 .getRegistrationId ();
79
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration3 , this .principalName1 ,
80
+ mock (OAuth2AccessToken .class ));
82
81
Map <OAuth2AuthorizedClientId , OAuth2AuthorizedClient > authorizedClients = Collections .singletonMap (
83
82
new OAuth2AuthorizedClientId (this .registration3 .getRegistrationId (), this .principalName1 ),
84
- mock ( OAuth2AuthorizedClient . class ) );
83
+ authorizedClient );
85
84
ClientRegistrationRepository clientRegistrationRepository = mock (ClientRegistrationRepository .class );
86
85
given (clientRegistrationRepository .findByRegistrationId (eq (registrationId ))).willReturn (this .registration3 );
87
86
InMemoryOAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService (
@@ -92,7 +91,7 @@ public void constructorWhenAuthorizedClientsProvidedThenUseProvidedAuthorizedCli
92
91
@ Test
93
92
public void loadAuthorizedClientWhenClientRegistrationIdIsNullThenThrowIllegalArgumentException () {
94
93
assertThatIllegalArgumentException ()
95
- .isThrownBy (() -> this .authorizedClientService .loadAuthorizedClient (null , this .principalName1 ));
94
+ .isThrownBy (() -> this .authorizedClientService .loadAuthorizedClient (null , this .principalName1 ));
96
95
}
97
96
98
97
@ Test
@@ -104,14 +103,14 @@ public void loadAuthorizedClientWhenPrincipalNameIsNullThenThrowIllegalArgumentE
104
103
@ Test
105
104
public void loadAuthorizedClientWhenClientRegistrationNotFoundThenReturnNull () {
106
105
OAuth2AuthorizedClient authorizedClient = this .authorizedClientService
107
- .loadAuthorizedClient ("registration-not-found" , this .principalName1 );
106
+ .loadAuthorizedClient ("registration-not-found" , this .principalName1 );
108
107
assertThat (authorizedClient ).isNull ();
109
108
}
110
109
111
110
@ Test
112
111
public void loadAuthorizedClientWhenClientRegistrationFoundButNotAssociatedToPrincipalThenReturnNull () {
113
112
OAuth2AuthorizedClient authorizedClient = this .authorizedClientService
114
- .loadAuthorizedClient (this .registration1 .getRegistrationId (), "principal-not-found" );
113
+ .loadAuthorizedClient (this .registration1 .getRegistrationId (), "principal-not-found" );
115
114
assertThat (authorizedClient ).isNull ();
116
115
}
117
116
@@ -123,14 +122,42 @@ public void loadAuthorizedClientWhenClientRegistrationFoundAndAssociatedToPrinci
123
122
mock (OAuth2AccessToken .class ));
124
123
this .authorizedClientService .saveAuthorizedClient (authorizedClient , authentication );
125
124
OAuth2AuthorizedClient loadedAuthorizedClient = this .authorizedClientService
126
- .loadAuthorizedClient (this .registration1 .getRegistrationId (), this .principalName1 );
127
- assertThat (loadedAuthorizedClient ).isEqualTo (authorizedClient );
125
+ .loadAuthorizedClient (this .registration1 .getRegistrationId (), this .principalName1 );
126
+ assertAuthorizedClientEquals (authorizedClient , loadedAuthorizedClient );
127
+ }
128
+
129
+ @ Test
130
+ public void loadAuthorizedClientWhenClientRegistrationIsUpdatedThenReturnAuthorizedClientWithUpdatedClientRegistration () {
131
+ ClientRegistration updatedRegistration = ClientRegistration .withClientRegistration (this .registration1 )
132
+ .clientSecret ("updated secret" )
133
+ .build ();
134
+ ClientRegistrationRepository repository = mock (ClientRegistrationRepository .class );
135
+ given (repository .findByRegistrationId (this .registration1 .getRegistrationId ())).willReturn (this .registration1 ,
136
+ updatedRegistration );
137
+
138
+ Authentication authentication = mock (Authentication .class );
139
+ given (authentication .getName ()).willReturn (this .principalName1 );
140
+
141
+ InMemoryOAuth2AuthorizedClientService service = new InMemoryOAuth2AuthorizedClientService (repository );
142
+
143
+ OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration1 , this .principalName1 ,
144
+ mock (OAuth2AccessToken .class ));
145
+ service .saveAuthorizedClient (authorizedClient , authentication );
146
+
147
+ OAuth2AuthorizedClient authorizedClientWithUpdatedRegistration = new OAuth2AuthorizedClient (updatedRegistration ,
148
+ this .principalName1 , mock (OAuth2AccessToken .class ));
149
+ OAuth2AuthorizedClient firstLoadedClient = service .loadAuthorizedClient (this .registration1 .getRegistrationId (),
150
+ this .principalName1 );
151
+ OAuth2AuthorizedClient secondLoadedClient = service .loadAuthorizedClient (this .registration1 .getRegistrationId (),
152
+ this .principalName1 );
153
+ assertAuthorizedClientEquals (authorizedClient , firstLoadedClient );
154
+ assertAuthorizedClientEquals (authorizedClientWithUpdatedRegistration , secondLoadedClient );
128
155
}
129
156
130
157
@ Test
131
158
public void saveAuthorizedClientWhenAuthorizedClientIsNullThenThrowIllegalArgumentException () {
132
159
assertThatIllegalArgumentException ()
133
- .isThrownBy (() -> this .authorizedClientService .saveAuthorizedClient (null , mock (Authentication .class )));
160
+ .isThrownBy (() -> this .authorizedClientService .saveAuthorizedClient (null , mock (Authentication .class )));
134
161
}
135
162
136
163
@ Test
@@ -147,20 +174,20 @@ public void saveAuthorizedClientWhenSavedThenCanLoad() {
147
174
mock (OAuth2AccessToken .class ));
148
175
this .authorizedClientService .saveAuthorizedClient (authorizedClient , authentication );
149
176
OAuth2AuthorizedClient loadedAuthorizedClient = this .authorizedClientService
150
- .loadAuthorizedClient (this .registration3 .getRegistrationId (), this .principalName2 );
151
- assertThat ( loadedAuthorizedClient ). isEqualTo ( authorizedClient );
177
+ .loadAuthorizedClient (this .registration3 .getRegistrationId (), this .principalName2 );
178
+ assertAuthorizedClientEquals ( authorizedClient , loadedAuthorizedClient );
152
179
}
153
180
154
181
@ Test
155
182
public void removeAuthorizedClientWhenClientRegistrationIdIsNullThenThrowIllegalArgumentException () {
156
183
assertThatIllegalArgumentException ()
157
- .isThrownBy (() -> this .authorizedClientService .removeAuthorizedClient (null , this .principalName2 ));
184
+ .isThrownBy (() -> this .authorizedClientService .removeAuthorizedClient (null , this .principalName2 ));
158
185
}
159
186
160
187
@ Test
161
188
public void removeAuthorizedClientWhenPrincipalNameIsNullThenThrowIllegalArgumentException () {
162
189
assertThatIllegalArgumentException ().isThrownBy (() -> this .authorizedClientService
163
- .removeAuthorizedClient (this .registration3 .getRegistrationId (), null ));
190
+ .removeAuthorizedClient (this .registration3 .getRegistrationId (), null ));
164
191
}
165
192
166
193
@ Test
@@ -171,13 +198,38 @@ public void removeAuthorizedClientWhenSavedThenRemoved() {
171
198
mock (OAuth2AccessToken .class ));
172
199
this .authorizedClientService .saveAuthorizedClient (authorizedClient , authentication );
173
200
OAuth2AuthorizedClient loadedAuthorizedClient = this .authorizedClientService
174
- .loadAuthorizedClient (this .registration2 .getRegistrationId (), this .principalName2 );
201
+ .loadAuthorizedClient (this .registration2 .getRegistrationId (), this .principalName2 );
175
202
assertThat (loadedAuthorizedClient ).isNotNull ();
176
203
this .authorizedClientService .removeAuthorizedClient (this .registration2 .getRegistrationId (),
177
204
this .principalName2 );
178
205
loadedAuthorizedClient = this .authorizedClientService
179
- .loadAuthorizedClient (this .registration2 .getRegistrationId (), this .principalName2 );
206
+ .loadAuthorizedClient (this .registration2 .getRegistrationId (), this .principalName2 );
180
207
assertThat (loadedAuthorizedClient ).isNull ();
181
208
}
182
209
210
+ private static void assertAuthorizedClientEquals (OAuth2AuthorizedClient expected , OAuth2AuthorizedClient actual ) {
211
+ assertThat (actual ).isNotNull ();
212
+ assertThat (actual .getClientRegistration ().getRegistrationId ())
213
+ .isEqualTo (expected .getClientRegistration ().getRegistrationId ());
214
+ assertThat (actual .getClientRegistration ().getClientName ())
215
+ .isEqualTo (expected .getClientRegistration ().getClientName ());
216
+ assertThat (actual .getClientRegistration ().getRedirectUri ())
217
+ .isEqualTo (expected .getClientRegistration ().getRedirectUri ());
218
+ assertThat (actual .getClientRegistration ().getAuthorizationGrantType ())
219
+ .isEqualTo (expected .getClientRegistration ().getAuthorizationGrantType ());
220
+ assertThat (actual .getClientRegistration ().getClientAuthenticationMethod ())
221
+ .isEqualTo (expected .getClientRegistration ().getClientAuthenticationMethod ());
222
+ assertThat (actual .getClientRegistration ().getClientId ())
223
+ .isEqualTo (expected .getClientRegistration ().getClientId ());
224
+ assertThat (actual .getClientRegistration ().getClientSecret ())
225
+ .isEqualTo (expected .getClientRegistration ().getClientSecret ());
226
+ assertThat (actual .getPrincipalName ()).isEqualTo (expected .getPrincipalName ());
227
+ assertThat (actual .getAccessToken ().getTokenType ()).isEqualTo (expected .getAccessToken ().getTokenType ());
228
+ assertThat (actual .getAccessToken ().getTokenValue ()).isEqualTo (expected .getAccessToken ().getTokenValue ());
229
+ assertThat (actual .getAccessToken ().getIssuedAt ()).isEqualTo (expected .getAccessToken ().getIssuedAt ());
230
+ assertThat (actual .getAccessToken ().getExpiresAt ()).isEqualTo (expected .getAccessToken ().getExpiresAt ());
231
+ assertThat (actual .getAccessToken ().getScopes ()).isEqualTo (expected .getAccessToken ().getScopes ());
232
+ assertThat (actual .getRefreshToken ()).isEqualTo (expected .getRefreshToken ());
233
+ }
234
+
183
235
}
0 commit comments