Don't Mock OAuth2AuthorizedClientRepository

Issue gh-13542
Issue gh-13546
This commit is contained in:
Josh Cummings 2023-07-14 18:32:17 -06:00
parent b0022a0ae8
commit b3ad2b0dc5
No known key found for this signature in database
GPG Key ID: A306A51F43B8E5A5
4 changed files with 18 additions and 11 deletions

View File

@ -24,6 +24,9 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestContextExtension;
@ -219,8 +222,11 @@ public class OAuth2ClientBeanDefinitionParserTests {
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google");
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(clientRegistration, "user",
TestOAuth2AccessTokens.noScopes());
given(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).willReturn(authorizedClient);
this.mvc.perform(get("/authorized-client")).andExpect(status().isOk()).andExpect(content().string("resolved"));
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, request, response);
this.mvc.perform(get("/authorized-client").session((MockHttpSession) request.getSession()))
.andExpect(status().isOk()).andExpect(content().string("resolved"));
}
private static OAuth2AuthorizationRequest createAuthorizationRequest(ClientRegistration clientRegistration) {

View File

@ -27,6 +27,9 @@ import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestContextExtension;
@ -522,9 +525,11 @@ public class OAuth2LoginBeanDefinitionParserTests {
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google-login");
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(clientRegistration, "user",
TestOAuth2AccessTokens.noScopes());
given(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).willReturn(authorizedClient);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, null, request, response);
// @formatter:off
this.mvc.perform(get("/authorized-client"))
this.mvc.perform(get("/authorized-client").session((MockHttpSession) request.getSession()))
.andExpect(status().isOk())
.andExpect(content().string("resolved"));
// @formatter:on
@ -539,7 +544,7 @@ public class OAuth2LoginBeanDefinitionParserTests {
@GetMapping("/authorized-client")
String authorizedClient(Model model,
@RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
@RegisteredOAuth2AuthorizedClient("google-login") OAuth2AuthorizedClient authorizedClient) {
return (authorizedClient != null) ? "resolved" : "not-resolved";
}

View File

@ -43,9 +43,7 @@
provider-id="google"/>
</client-registrations>
<b:bean id="authorizedClientRepository" class="org.mockito.Mockito" factory-method="mock">
<b:constructor-arg value="org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository" type="java.lang.Class"/>
</b:bean>
<b:bean id="authorizedClientRepository" class="org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository"/>
<b:bean name="authorizedClientController" class="org.springframework.security.config.http.OAuth2ClientBeanDefinitionParserTests.AuthorizedClientController" />

View File

@ -34,9 +34,7 @@
<mvc:annotation-driven />
<b:bean id="authorizedClientRepository" class="org.mockito.Mockito" factory-method="mock">
<b:constructor-arg value="org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository" type="java.lang.Class"/>
</b:bean>
<b:bean id="authorizedClientRepository" class="org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository"/>
<b:bean name="authorizedClientController" class="org.springframework.security.config.http.OAuth2LoginBeanDefinitionParserTests.AuthorizedClientController" />