OAuth2LoginControllerTests.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2002-2019 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample.web;
  17. import java.util.Collections;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  22. import org.springframework.boot.test.context.TestConfiguration;
  23. import org.springframework.boot.test.mock.mockito.MockBean;
  24. import org.springframework.context.annotation.Bean;
  25. import org.springframework.security.oauth2.client.registration.ClientRegistration;
  26. import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
  27. import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository;
  28. import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
  29. import org.springframework.security.oauth2.core.AuthorizationGrantType;
  30. import org.springframework.test.context.junit4.SpringRunner;
  31. import org.springframework.test.web.servlet.MockMvc;
  32. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
  33. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  34. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
  35. /**
  36. * Tests for {@link OAuth2LoginController}
  37. *
  38. * @author Josh Cummings
  39. */
  40. @RunWith(SpringRunner.class)
  41. @WebMvcTest(OAuth2LoginController.class)
  42. public class OAuth2LoginControllerTests {
  43. @Autowired
  44. MockMvc mvc;
  45. @MockBean
  46. ClientRegistrationRepository clientRegistrationRepository;
  47. @TestConfiguration
  48. static class AuthorizedClient {
  49. @Bean
  50. public OAuth2AuthorizedClientRepository authorizedClientRepository() {
  51. return new HttpSessionOAuth2AuthorizedClientRepository();
  52. }
  53. }
  54. @Test
  55. public void rootWhenAuthenticatedReturnsUserAndClient() throws Exception {
  56. this.mvc.perform(get("/").with(oauth2Login()))
  57. .andExpect(model().attribute("userName", "user"))
  58. .andExpect(model().attribute("clientName", "test"))
  59. .andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "user")));
  60. }
  61. @Test
  62. public void rootWhenOverridingClientRegistrationReturnsAccordingly() throws Exception {
  63. ClientRegistration clientRegistration = ClientRegistration.withRegistrationId("test")
  64. .authorizationGrantType(AuthorizationGrantType.PASSWORD)
  65. .clientId("my-client-id")
  66. .clientName("my-client-name")
  67. .tokenUri("https://token-uri.example.org")
  68. .build();
  69. this.mvc.perform(get("/").with(oauth2Login()
  70. .clientRegistration(clientRegistration)
  71. .attributes(a -> a.put("sub", "spring-security"))))
  72. .andExpect(model().attribute("userName", "spring-security"))
  73. .andExpect(model().attribute("clientName", "my-client-name"))
  74. .andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "spring-security")));
  75. }
  76. }