Skip to content

Commit cd38347

Browse files
committed
Merge pull request #31453 from izeye
* pr/31453: Polish OAuth2ResourceServerAutoConfigurationTests Closes gh-31453
2 parents f68d051 + 11a07a9 commit cd38347

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ void autoConfigurationShouldMatchDefaultJwsAlgorithm() {
114114
.withPropertyValues("spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://jwk-set-uri.com")
115115
.run((context) -> {
116116
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
117-
Object processor = ReflectionTestUtils.getField(jwtDecoder, "jwtProcessor");
118-
Object keySelector = ReflectionTestUtils.getField(processor, "jwsKeySelector");
119-
assertThat(keySelector).hasFieldOrPropertyWithValue("jwsAlgs",
120-
Collections.singleton(JWSAlgorithm.RS256));
117+
assertThat(jwtDecoder).extracting("jwtProcessor.jwsKeySelector.jwsAlgs")
118+
.asInstanceOf(InstanceOfAssertFactories.collection(JWSAlgorithm.class))
119+
.containsExactlyInAnyOrder(JWSAlgorithm.RS256);
121120
});
122121
}
123122

@@ -144,9 +143,7 @@ void autoConfigurationShouldConfigureResourceServerWithSingleJwsAlgorithm() {
144143
"spring.security.oauth2.resourceserver.jwt.jws-algorithms=RS384")
145144
.run((context) -> {
146145
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
147-
Object processor = ReflectionTestUtils.getField(jwtDecoder, "jwtProcessor");
148-
Object keySelector = ReflectionTestUtils.getField(processor, "jwsKeySelector");
149-
assertThat(keySelector).extracting("jwsAlgs")
146+
assertThat(jwtDecoder).extracting("jwtProcessor.jwsKeySelector.jwsAlgs")
150147
.asInstanceOf(InstanceOfAssertFactories.collection(JWSAlgorithm.class))
151148
.containsExactlyInAnyOrder(JWSAlgorithm.RS384);
152149
assertThat(getBearerTokenFilter(context)).isNotNull();
@@ -160,9 +157,7 @@ void autoConfigurationShouldConfigureResourceServerWithMultipleJwsAlgorithms() {
160157
"spring.security.oauth2.resourceserver.jwt.jws-algorithms=RS256, RS384, RS512")
161158
.run((context) -> {
162159
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
163-
Object processor = ReflectionTestUtils.getField(jwtDecoder, "jwtProcessor");
164-
Object keySelector = ReflectionTestUtils.getField(processor, "jwsKeySelector");
165-
assertThat(keySelector).extracting("jwsAlgs")
160+
assertThat(jwtDecoder).extracting("jwtProcessor.jwsKeySelector.jwsAlgs")
166161
.asInstanceOf(InstanceOfAssertFactories.collection(JWSAlgorithm.class))
167162
.containsExactlyInAnyOrder(JWSAlgorithm.RS256, JWSAlgorithm.RS384, JWSAlgorithm.RS512);
168163
assertThat(getBearerTokenFilter(context)).isNotNull();
@@ -472,11 +467,9 @@ void autoConfigurationShouldConfigureResourceServerUsingJwkSetUriAndIssuerUri()
472467
.run((context) -> {
473468
assertThat(context).hasSingleBean(JwtDecoder.class);
474469
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
475-
DelegatingOAuth2TokenValidator<Jwt> jwtValidator = (DelegatingOAuth2TokenValidator<Jwt>) ReflectionTestUtils
476-
.getField(jwtDecoder, "jwtValidator");
477-
Collection<OAuth2TokenValidator<Jwt>> tokenValidators = (Collection<OAuth2TokenValidator<Jwt>>) ReflectionTestUtils
478-
.getField(jwtValidator, "tokenValidators");
479-
assertThat(tokenValidators).hasAtLeastOneElementOfType(JwtIssuerValidator.class);
470+
assertThat(jwtDecoder).extracting("jwtValidator.tokenValidators")
471+
.asInstanceOf(InstanceOfAssertFactories.collection(OAuth2TokenValidator.class))
472+
.hasAtLeastOneElementOfType(JwtIssuerValidator.class);
480473
});
481474
}
482475

@@ -494,13 +487,11 @@ void autoConfigurationShouldNotConfigureIssuerUriAndAudienceJwtValidatorIfProper
494487
.run((context) -> {
495488
assertThat(context).hasSingleBean(JwtDecoder.class);
496489
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
497-
DelegatingOAuth2TokenValidator<Jwt> jwtValidator = (DelegatingOAuth2TokenValidator<Jwt>) ReflectionTestUtils
498-
.getField(jwtDecoder, "jwtValidator");
499-
Collection<OAuth2TokenValidator<Jwt>> tokenValidators = (Collection<OAuth2TokenValidator<Jwt>>) ReflectionTestUtils
500-
.getField(jwtValidator, "tokenValidators");
501-
assertThat(tokenValidators).hasExactlyElementsOfTypes(JwtTimestampValidator.class);
502-
assertThat(tokenValidators).doesNotHaveAnyElementsOfTypes(JwtClaimValidator.class);
503-
assertThat(tokenValidators).doesNotHaveAnyElementsOfTypes(JwtIssuerValidator.class);
490+
assertThat(jwtDecoder).extracting("jwtValidator.tokenValidators")
491+
.asInstanceOf(InstanceOfAssertFactories.collection(OAuth2TokenValidator.class))
492+
.hasExactlyElementsOfTypes(JwtTimestampValidator.class)
493+
.doesNotHaveAnyElementsOfTypes(JwtClaimValidator.class)
494+
.doesNotHaveAnyElementsOfTypes(JwtIssuerValidator.class);
504495
});
505496
}
506497

@@ -565,10 +556,10 @@ private void validateDelegates(String issuerUri, Collection<OAuth2TokenValidator
565556
assertThat(delegates).hasAtLeastOneElementOfType(JwtClaimValidator.class);
566557
OAuth2TokenValidator<Jwt> delegatingValidator = delegates.stream()
567558
.filter((v) -> v instanceof DelegatingOAuth2TokenValidator).findFirst().get();
568-
Collection<OAuth2TokenValidator<Jwt>> nestedDelegates = (Collection<OAuth2TokenValidator<Jwt>>) ReflectionTestUtils
569-
.getField(delegatingValidator, "tokenValidators");
570559
if (issuerUri != null) {
571-
assertThat(nestedDelegates).hasAtLeastOneElementOfType(JwtIssuerValidator.class);
560+
assertThat(delegatingValidator).extracting("tokenValidators")
561+
.asInstanceOf(InstanceOfAssertFactories.collection(OAuth2TokenValidator.class))
562+
.hasAtLeastOneElementOfType(JwtIssuerValidator.class);
572563
}
573564
}
574565

0 commit comments

Comments
 (0)