-
Notifications
You must be signed in to change notification settings - Fork 6k
OAuth2ResourceServerConfigurerTests should avoid MockWebServer #6104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
if this is a trivial change, I would like to work on it. |
Thanks @govi20! This should be pretty easy. The issue is yours. Please let us know if you have any questions. |
I had a look into
Which one should I go for? Also, if possible please give me an overview of change. |
@govi20 Many of these tests use a common spring configuration, declared there in the tests class itself. In most cases, the reference to For example, in the case of this test: @Test
public void getWhenUsingDefaultsWithValidBearerTokenThenAcceptsRequest()
throws Exception {
this.spring.register(WebServerConfig.class, DefaultConfig.class, BasicController.class).autowire();
this.authz.enqueue(this.jwks("Default"));
String token = this.token("ValidNoScopes");
this.mvc.perform(get("/").with(bearerToken(token)))
.andExpect(status().isOk())
.andExpect(content().string("ok"));
} I would change this to: @Test
public void getWhenUsingDefaultsWithValidBearerTokenThenAcceptsRequest()
throws Exception {
this.spring.register(SingleKeyConfig.class, BasicController.class).autowire();
String token = this.token("ValidNoScopes");
this.mvc.perform(get("/").with(bearerToken(token)))
.andExpect(status().isOk())
.andExpect(content().string("ok"));
} Many of the tests don't actually need to be configured with a JWK Set URI to confirm their functionality since that is not what they are testing. In those cases, a single key is preferred. When the JWK Set endpoint is being tested, then it should use a For example, this test: @Test
public void getWhenUsingDefaultsWithBadJwkEndpointThenInvalidToken()
throws Exception {
this.spring.register(WebServerConfig.class, DefaultConfig.class).autowire();
this.authz.enqueue(new MockResponse().setBody("malformed"));
String token = this.token("ValidNoScopes");
this.mvc.perform(get("/").with(bearerToken(token)))
.andExpect(status().isUnauthorized())
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt: Malformed Jwk set"));
} Could change to: @Test
public void getWhenUsingDefaultsWithBadJwkEndpointThenInvalidToken()
throws Exception {
this.spring.register(RestOperationsConfig.class).autowire();
mockRestOperationsToHaveResponse("malformed");
String token = this.token("ValidNoScopes");
this.mvc.perform(get("/").with(bearerToken(token)))
.andExpect(status().isUnauthorized())
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt: Malformed Jwk set"));
} |
@govi20 Wanted to see if you had any other questions. And are you still wanting to work on this? |
Yes. I will give it a try on upcoming Saturday, Sunday. |
@jzheaux I have started working on it. are we planning to remove |
@govi20 Nice! Yes, let's keep it for just one "happy path" Looking through the tests just now, after your changes you might not have a "happy path" |
I have removed |
@govi20 Your pace is just fine, thanks for checking in. What I'd do is mock |
@govi20 How are things coming? Are you able to mock Here is an idea if you are stuck: private void mockRestOperationsToHaveResponse(String response) {
RestOperations rest = this.spring.getContext().getBean(RestOperations.class);
when(rest.exchange(any(RequestEntity.class), eq(String.class)))
.thenReturn(new ResponseEntity<>(response, HttpStatus.OK));
}
static class RestOperationsConfig extends WebSecurityConfigurerAdapter {
private final RestOperations rest = mock(RestOperations.class);
// ...
@Bean
RestOperations rest() {
return this.rest;
}
} |
Now that there is support to create a
JWTProcessor
using aRestOperations
, several of the tests inOAuth2ResourceServerConfigurerTests
that relied on aMockWebServer
can now rely on a mockRestOperations
, making the tests faster.Or, as an alternative, they could be configured to use a single key instead of a JWK Set, where applicable.
The text was updated successfully, but these errors were encountered: