5656 * @author Ovidiu Popa
5757 */
5858public class JdbcOAuth2AuthorizationConsentServiceTests {
59-
6059 private static final String OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql" ;
6160 private static final String CUSTOM_OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE = "org/springframework/security/oauth2/server/authorization/custom-oauth2-authorization-consent-schema.sql" ;
6261 private static final String PRINCIPAL_NAME = "principal-name" ;
6362 private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients .registeredClient ().build ();
6463
6564 private static final OAuth2AuthorizationConsent AUTHORIZATION_CONSENT =
6665 OAuth2AuthorizationConsent .withId (REGISTERED_CLIENT .getId (), PRINCIPAL_NAME )
67- .authority (new SimpleGrantedAuthority ("some.authority" ))
66+ .authority (new SimpleGrantedAuthority ("SCOPE_scope1" ))
67+ .authority (new SimpleGrantedAuthority ("SCOPE_scope2" ))
68+ .authority (new SimpleGrantedAuthority ("SCOPE_scope3" ))
69+ .authority (new SimpleGrantedAuthority ("authority-a" ))
70+ .authority (new SimpleGrantedAuthority ("authority-b" ))
6871 .build ();
6972
7073 private EmbeddedDatabase db ;
7174 private JdbcOperations jdbcOperations ;
7275 private RegisteredClientRepository registeredClientRepository ;
7376 private JdbcOAuth2AuthorizationConsentService authorizationConsentService ;
7477
78+ @ Before
79+ public void setUp () {
80+ this .db = createDb ();
81+ this .jdbcOperations = new JdbcTemplate (this .db );
82+ this .registeredClientRepository = mock (RegisteredClientRepository .class );
83+ this .authorizationConsentService = new JdbcOAuth2AuthorizationConsentService (this .jdbcOperations , this .registeredClientRepository );
84+ }
85+
86+ @ After
87+ public void tearDown () {
88+ this .db .shutdown ();
89+ }
90+
7591 @ Test
7692 public void constructorWhenJdbcOperationsIsNullThenThrowIllegalArgumentException () {
7793 // @formatter:off
@@ -127,7 +143,7 @@ public void saveWhenAuthorizationConsentNewThenSaved() {
127143 RegisteredClient newRegisteredClient = TestRegisteredClients .registeredClient ()
128144 .id ("new-client" ).build ();
129145
130- when (registeredClientRepository .findById (eq (newRegisteredClient .getId ())))
146+ when (this . registeredClientRepository .findById (eq (newRegisteredClient .getId ())))
131147 .thenReturn (newRegisteredClient );
132148
133149 this .authorizationConsentService .save (expectedAuthorizationConsent );
@@ -143,7 +159,7 @@ public void saveWhenAuthorizationConsentExistsThenUpdated() {
143159 OAuth2AuthorizationConsent .from (AUTHORIZATION_CONSENT )
144160 .authority (new SimpleGrantedAuthority ("new.authority" ))
145161 .build ();
146- when (registeredClientRepository .findById (eq (REGISTERED_CLIENT .getId ())))
162+ when (this . registeredClientRepository .findById (eq (REGISTERED_CLIENT .getId ())))
147163 .thenReturn (REGISTERED_CLIENT );
148164
149165 this .authorizationConsentService .save (expectedAuthorizationConsent );
@@ -157,7 +173,7 @@ public void saveWhenAuthorizationConsentExistsThenUpdated() {
157173
158174 @ Test
159175 public void saveLoadAuthorizationConsentWhenCustomStrategiesSetThenCalled () throws Exception {
160- when (registeredClientRepository .findById (eq (REGISTERED_CLIENT .getId ())))
176+ when (this . registeredClientRepository .findById (eq (REGISTERED_CLIENT .getId ())))
161177 .thenReturn (REGISTERED_CLIENT );
162178
163179 JdbcOAuth2AuthorizationConsentService .OAuth2AuthorizationConsentRowMapper authorizationConsentRowMapper = spy (
@@ -205,6 +221,17 @@ public void findByIdWhenPrincipalNameNullThenThrowIllegalArgumentException() {
205221 .withMessage ("principalName cannot be empty" );
206222 }
207223
224+ @ Test
225+ public void findByIdWhenAuthorizationConsentExistsThenFound () {
226+ when (this .registeredClientRepository .findById (eq (REGISTERED_CLIENT .getId ())))
227+ .thenReturn (REGISTERED_CLIENT );
228+
229+ this .authorizationConsentService .save (AUTHORIZATION_CONSENT );
230+ OAuth2AuthorizationConsent authorizationConsent = this .authorizationConsentService .findById (
231+ AUTHORIZATION_CONSENT .getRegisteredClientId (), AUTHORIZATION_CONSENT .getPrincipalName ());
232+ assertThat (authorizationConsent ).isNotNull ();
233+ }
234+
208235 @ Test
209236 public void findByIdWhenAuthorizationConsentDoesNotExistThenNull () {
210237 this .authorizationConsentService .save (AUTHORIZATION_CONSENT );
@@ -221,27 +248,16 @@ public void tableDefinitionWhenCustomThenAbleToOverride() {
221248 OAuth2AuthorizationConsentService authorizationConsentService =
222249 new CustomJdbcOAuth2AuthorizationConsentService (new JdbcTemplate (db ), this .registeredClientRepository );
223250 authorizationConsentService .save (AUTHORIZATION_CONSENT );
224- OAuth2AuthorizationConsent foundAuthorizationConsent1 = authorizationConsentService .findById (AUTHORIZATION_CONSENT .getRegisteredClientId (), AUTHORIZATION_CONSENT .getPrincipalName ());
251+ OAuth2AuthorizationConsent foundAuthorizationConsent1 = authorizationConsentService .findById (
252+ AUTHORIZATION_CONSENT .getRegisteredClientId (), AUTHORIZATION_CONSENT .getPrincipalName ());
225253 assertThat (foundAuthorizationConsent1 ).isEqualTo (AUTHORIZATION_CONSENT );
226254 authorizationConsentService .remove (AUTHORIZATION_CONSENT );
227- OAuth2AuthorizationConsent foundAuthorizationConsent2 = authorizationConsentService .findById (REGISTERED_CLIENT .getClientId (), AUTHORIZATION_CONSENT .getPrincipalName ());
255+ OAuth2AuthorizationConsent foundAuthorizationConsent2 = authorizationConsentService .findById (
256+ AUTHORIZATION_CONSENT .getRegisteredClientId (), AUTHORIZATION_CONSENT .getPrincipalName ());
228257 assertThat (foundAuthorizationConsent2 ).isNull ();
229258 db .shutdown ();
230259 }
231260
232- @ Before
233- public void setUp () {
234- this .db = createDb ();
235- this .registeredClientRepository = mock (RegisteredClientRepository .class );
236- this .jdbcOperations = new JdbcTemplate (this .db );
237- this .authorizationConsentService = new JdbcOAuth2AuthorizationConsentService (this .jdbcOperations , this .registeredClientRepository );
238- }
239-
240- @ After
241- public void tearDown () {
242- this .db .shutdown ();
243- }
244-
245261 private static EmbeddedDatabase createDb () {
246262 return createDb (OAUTH2_AUTHORIZATION_CONSENT_SCHEMA_SQL_RESOURCE );
247263 }
@@ -282,7 +298,7 @@ private static final class CustomJdbcOAuth2AuthorizationConsentService extends J
282298
283299 private static final String REMOVE_AUTHORIZATION_CONSENT_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + PK_FILTER ;
284300
285- CustomJdbcOAuth2AuthorizationConsentService (JdbcOperations jdbcOperations , RegisteredClientRepository registeredClientRepository ) {
301+ private CustomJdbcOAuth2AuthorizationConsentService (JdbcOperations jdbcOperations , RegisteredClientRepository registeredClientRepository ) {
286302 super (jdbcOperations , registeredClientRepository );
287303 setAuthorizationConsentRowMapper (new CustomOAuth2AuthorizationConsentRowMapper (registeredClientRepository ));
288304 }
@@ -317,16 +333,14 @@ public OAuth2AuthorizationConsent findById(String registeredClientId, String pri
317333
318334 private static final class CustomOAuth2AuthorizationConsentRowMapper extends JdbcOAuth2AuthorizationConsentService .OAuth2AuthorizationConsentRowMapper {
319335
320- CustomOAuth2AuthorizationConsentRowMapper (RegisteredClientRepository registeredClientRepository ) {
336+ private CustomOAuth2AuthorizationConsentRowMapper (RegisteredClientRepository registeredClientRepository ) {
321337 super (registeredClientRepository );
322338 }
323339
324340 @ Override
325341 public OAuth2AuthorizationConsent mapRow (ResultSet rs , int rowNum ) throws SQLException {
326342 String registeredClientId = rs .getString ("registeredClientId" );
327-
328- RegisteredClient registeredClient = getRegisteredClientRepository ()
329- .findById (registeredClientId );
343+ RegisteredClient registeredClient = getRegisteredClientRepository ().findById (registeredClientId );
330344 if (registeredClient == null ) {
331345 throw new DataRetrievalFailureException (
332346 "The RegisteredClient with id '" + registeredClientId + "' was not found in the RegisteredClientRepository." );
0 commit comments