OAuth2LoginControllerTests.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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;
  17. import org.junit.Before;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. import org.mockito.Mock;
  21. import sample.web.OAuth2LoginController;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
  24. import org.springframework.core.ReactiveAdapterRegistry;
  25. import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
  26. import org.springframework.security.oauth2.client.web.reactive.result.method.annotation.OAuth2AuthorizedClientArgumentResolver;
  27. import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
  28. import org.springframework.security.oauth2.client.web.server.WebSessionServerOAuth2AuthorizedClientRepository;
  29. import org.springframework.security.web.reactive.result.method.annotation.AuthenticationPrincipalArgumentResolver;
  30. import org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter;
  31. import org.springframework.test.context.junit4.SpringRunner;
  32. import org.springframework.test.web.reactive.server.WebTestClient;
  33. import org.springframework.web.reactive.result.view.ViewResolver;
  34. import static org.hamcrest.Matchers.containsString;
  35. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOidcLogin;
  36. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
  37. /**
  38. * @author Josh Cummings
  39. */
  40. @RunWith(SpringRunner.class)
  41. @WebFluxTest(OAuth2LoginController.class)
  42. public class OAuth2LoginControllerTests {
  43. @Autowired
  44. OAuth2LoginController controller;
  45. @Autowired
  46. ViewResolver viewResolver;
  47. @Mock
  48. ReactiveClientRegistrationRepository clientRegistrationRepository;
  49. WebTestClient rest;
  50. @Before
  51. public void setup() {
  52. ServerOAuth2AuthorizedClientRepository authorizedClientRepository =
  53. new WebSessionServerOAuth2AuthorizedClientRepository();
  54. this.rest = WebTestClient
  55. .bindToController(this.controller)
  56. .apply(springSecurity())
  57. .webFilter(new SecurityContextServerWebExchangeWebFilter())
  58. .argumentResolvers(c -> {
  59. c.addCustomResolver(new AuthenticationPrincipalArgumentResolver(new ReactiveAdapterRegistry()));
  60. c.addCustomResolver(new OAuth2AuthorizedClientArgumentResolver
  61. (this.clientRegistrationRepository, authorizedClientRepository));
  62. })
  63. .viewResolvers(c -> c.viewResolver(this.viewResolver))
  64. .build();
  65. }
  66. @Test
  67. public void indexGreetsAuthenticatedUser() {
  68. this.rest.mutateWith(mockOidcLogin())
  69. .get().uri("/").exchange()
  70. .expectBody(String.class).value(containsString("test-subject"));
  71. }
  72. }