RegisteredOAuth2AuthorizedClientControllerTests.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2002-2020 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;
  17. import okhttp3.mockwebserver.MockResponse;
  18. import okhttp3.mockwebserver.MockWebServer;
  19. import org.junit.AfterClass;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import sample.config.SecurityConfig;
  23. import sample.web.RegisteredOAuth2AuthorizedClientController;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  26. import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  27. import org.springframework.boot.test.mock.mockito.MockBean;
  28. import org.springframework.context.annotation.Bean;
  29. import org.springframework.context.annotation.Configuration;
  30. import org.springframework.context.annotation.Import;
  31. import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
  32. import org.springframework.test.context.junit4.SpringRunner;
  33. import org.springframework.test.web.servlet.MockMvc;
  34. import org.springframework.web.reactive.function.client.WebClient;
  35. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Client;
  36. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
  37. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  38. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  39. @WebMvcTest
  40. @Import({ SecurityConfig.class, RegisteredOAuth2AuthorizedClientController.class })
  41. @AutoConfigureMockMvc
  42. @RunWith(SpringRunner.class)
  43. public class RegisteredOAuth2AuthorizedClientControllerTests {
  44. private static MockWebServer web = new MockWebServer();
  45. @Autowired
  46. private MockMvc mockMvc;
  47. @MockBean
  48. ClientRegistrationRepository clientRegistrationRepository;
  49. @AfterClass
  50. public static void shutdown() throws Exception {
  51. web.shutdown();
  52. }
  53. @Test
  54. public void annotationExplicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
  55. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  56. this.mockMvc.perform(get("/annotation/explicit")
  57. .with(oauth2Login())
  58. .with(oauth2Client("client-id")))
  59. .andExpect(status().isOk());
  60. }
  61. @Test
  62. public void annotationImplicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
  63. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  64. this.mockMvc.perform(get("/annotation/implicit")
  65. .with(oauth2Login()))
  66. .andExpect(status().isOk());
  67. }
  68. @Test
  69. public void publicAnnotationExplicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
  70. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  71. this.mockMvc.perform(get("/public/annotation/explicit")
  72. .with(oauth2Client("client-id")))
  73. .andExpect(status().isOk());
  74. }
  75. @Test
  76. public void publicAnnotationImplicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
  77. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  78. this.mockMvc.perform(get("/public/annotation/implicit")
  79. .with(oauth2Login()))
  80. .andExpect(status().isOk());
  81. }
  82. @Configuration
  83. static class WebClientConfig {
  84. @Bean
  85. WebClient web() {
  86. return WebClient.create(web.url("/").toString());
  87. }
  88. }
  89. }