@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2019 the original author or authors .
* Copyright 2002 - 2024 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -33,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.assertj.core.api.Assertions.assertThatObject ;
import static org.assertj.core.api.Assertions.assertThatObject ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.BDDMockito.given ;
import static org.mockito.BDDMockito.given ;
import static org.mockito.Mockito.mock ;
import static org.mockito.BDD Mockito.mock ;
/ * *
/ * *
* Tests for { @link InMemoryOAuth2AuthorizedClientService } .
* Tests for { @link InMemoryOAuth2AuthorizedClientService } .
@ -79,9 +79,11 @@ public class InMemoryOAuth2AuthorizedClientServiceTests {
@Test
@Test
public void constructorWhenAuthorizedClientsProvidedThenUseProvidedAuthorizedClients ( ) {
public void constructorWhenAuthorizedClientsProvidedThenUseProvidedAuthorizedClients ( ) {
String registrationId = this . registration3 . getRegistrationId ( ) ;
String registrationId = this . registration3 . getRegistrationId ( ) ;
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient ( this . registration3 , this . principalName1 ,
mock ( OAuth2AccessToken . class ) ) ;
Map < OAuth2AuthorizedClientId , OAuth2AuthorizedClient > authorizedClients = Collections . singletonMap (
Map < OAuth2AuthorizedClientId , OAuth2AuthorizedClient > authorizedClients = Collections . singletonMap (
new OAuth2AuthorizedClientId ( this . registration3 . getRegistrationId ( ) , this . principalName1 ) ,
new OAuth2AuthorizedClientId ( this . registration3 . getRegistrationId ( ) , this . principalName1 ) ,
mock ( OAuth2A uthorizedClient. class ) ) ;
a uthorizedClient) ;
ClientRegistrationRepository clientRegistrationRepository = mock ( ClientRegistrationRepository . class ) ;
ClientRegistrationRepository clientRegistrationRepository = mock ( ClientRegistrationRepository . class ) ;
given ( clientRegistrationRepository . findByRegistrationId ( eq ( registrationId ) ) ) . willReturn ( this . registration3 ) ;
given ( clientRegistrationRepository . findByRegistrationId ( eq ( registrationId ) ) ) . willReturn ( this . registration3 ) ;
InMemoryOAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService (
InMemoryOAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService (
@ -124,7 +126,35 @@ public class InMemoryOAuth2AuthorizedClientServiceTests {
this . authorizedClientService . saveAuthorizedClient ( authorizedClient , authentication ) ;
this . authorizedClientService . saveAuthorizedClient ( authorizedClient , authentication ) ;
OAuth2AuthorizedClient loadedAuthorizedClient = this . authorizedClientService
OAuth2AuthorizedClient loadedAuthorizedClient = this . authorizedClientService
. loadAuthorizedClient ( this . registration1 . getRegistrationId ( ) , this . principalName1 ) ;
. loadAuthorizedClient ( this . registration1 . getRegistrationId ( ) , this . principalName1 ) ;
assertThat ( loadedAuthorizedClient ) . isEqualTo ( authorizedClient ) ;
assertAuthorizedClientEquals ( authorizedClient , loadedAuthorizedClient ) ;
}
@Test
public void loadAuthorizedClientWhenClientRegistrationIsUpdatedThenReturnAuthorizedClientWithUpdatedClientRegistration ( ) {
ClientRegistration updatedRegistration = ClientRegistration . withClientRegistration ( this . registration1 )
. clientSecret ( "updated secret" )
. build ( ) ;
ClientRegistrationRepository repository = mock ( ClientRegistrationRepository . class ) ;
given ( repository . findByRegistrationId ( this . registration1 . getRegistrationId ( ) ) ) . willReturn ( this . registration1 ,
updatedRegistration ) ;
Authentication authentication = mock ( Authentication . class ) ;
given ( authentication . getName ( ) ) . willReturn ( this . principalName1 ) ;
InMemoryOAuth2AuthorizedClientService service = new InMemoryOAuth2AuthorizedClientService ( repository ) ;
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient ( this . registration1 , this . principalName1 ,
mock ( OAuth2AccessToken . class ) ) ;
service . saveAuthorizedClient ( authorizedClient , authentication ) ;
OAuth2AuthorizedClient authorizedClientWithUpdatedRegistration = new OAuth2AuthorizedClient ( updatedRegistration ,
this . principalName1 , mock ( OAuth2AccessToken . class ) ) ;
OAuth2AuthorizedClient firstLoadedClient = service . loadAuthorizedClient ( this . registration1 . getRegistrationId ( ) ,
this . principalName1 ) ;
OAuth2AuthorizedClient secondLoadedClient = service . loadAuthorizedClient ( this . registration1 . getRegistrationId ( ) ,
this . principalName1 ) ;
assertAuthorizedClientEquals ( authorizedClient , firstLoadedClient ) ;
assertAuthorizedClientEquals ( authorizedClientWithUpdatedRegistration , secondLoadedClient ) ;
}
}
@Test
@Test
@ -148,7 +178,7 @@ public class InMemoryOAuth2AuthorizedClientServiceTests {
this . authorizedClientService . saveAuthorizedClient ( authorizedClient , authentication ) ;
this . authorizedClientService . saveAuthorizedClient ( authorizedClient , authentication ) ;
OAuth2AuthorizedClient loadedAuthorizedClient = this . authorizedClientService
OAuth2AuthorizedClient loadedAuthorizedClient = this . authorizedClientService
. loadAuthorizedClient ( this . registration3 . getRegistrationId ( ) , this . principalName2 ) ;
. loadAuthorizedClient ( this . registration3 . getRegistrationId ( ) , this . principalName2 ) ;
assertThat ( loadedAuthorizedClient ) . isEqualTo ( a uthorizedClient ) ;
assertAuthorizedClientEquals ( authorizedClient , loadedA uthorizedClient ) ;
}
}
@Test
@Test
@ -180,4 +210,29 @@ public class InMemoryOAuth2AuthorizedClientServiceTests {
assertThat ( loadedAuthorizedClient ) . isNull ( ) ;
assertThat ( loadedAuthorizedClient ) . isNull ( ) ;
}
}
private static void assertAuthorizedClientEquals ( OAuth2AuthorizedClient expected , OAuth2AuthorizedClient actual ) {
assertThat ( actual ) . isNotNull ( ) ;
assertThat ( actual . getClientRegistration ( ) . getRegistrationId ( ) )
. isEqualTo ( expected . getClientRegistration ( ) . getRegistrationId ( ) ) ;
assertThat ( actual . getClientRegistration ( ) . getClientName ( ) )
. isEqualTo ( expected . getClientRegistration ( ) . getClientName ( ) ) ;
assertThat ( actual . getClientRegistration ( ) . getRedirectUri ( ) )
. isEqualTo ( expected . getClientRegistration ( ) . getRedirectUri ( ) ) ;
assertThat ( actual . getClientRegistration ( ) . getAuthorizationGrantType ( ) )
. isEqualTo ( expected . getClientRegistration ( ) . getAuthorizationGrantType ( ) ) ;
assertThat ( actual . getClientRegistration ( ) . getClientAuthenticationMethod ( ) )
. isEqualTo ( expected . getClientRegistration ( ) . getClientAuthenticationMethod ( ) ) ;
assertThat ( actual . getClientRegistration ( ) . getClientId ( ) )
. isEqualTo ( expected . getClientRegistration ( ) . getClientId ( ) ) ;
assertThat ( actual . getClientRegistration ( ) . getClientSecret ( ) )
. isEqualTo ( expected . getClientRegistration ( ) . getClientSecret ( ) ) ;
assertThat ( actual . getPrincipalName ( ) ) . isEqualTo ( expected . getPrincipalName ( ) ) ;
assertThat ( actual . getAccessToken ( ) . getTokenType ( ) ) . isEqualTo ( expected . getAccessToken ( ) . getTokenType ( ) ) ;
assertThat ( actual . getAccessToken ( ) . getTokenValue ( ) ) . isEqualTo ( expected . getAccessToken ( ) . getTokenValue ( ) ) ;
assertThat ( actual . getAccessToken ( ) . getIssuedAt ( ) ) . isEqualTo ( expected . getAccessToken ( ) . getIssuedAt ( ) ) ;
assertThat ( actual . getAccessToken ( ) . getExpiresAt ( ) ) . isEqualTo ( expected . getAccessToken ( ) . getExpiresAt ( ) ) ;
assertThat ( actual . getAccessToken ( ) . getScopes ( ) ) . isEqualTo ( expected . getAccessToken ( ) . getScopes ( ) ) ;
assertThat ( actual . getRefreshToken ( ) ) . isEqualTo ( expected . getRefreshToken ( ) ) ;
}
}
}