ServerOAuth2ResourceServerApplicationITests.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 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 example;
  17. import java.util.function.Consumer;
  18. import org.junit.jupiter.api.Test;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
  21. import org.springframework.boot.test.context.SpringBootTest;
  22. import org.springframework.http.HttpHeaders;
  23. import org.springframework.test.web.reactive.server.WebTestClient;
  24. import static org.hamcrest.Matchers.containsString;
  25. /**
  26. * Integration tests for resource server.
  27. *
  28. * @author Rob Winch
  29. * @since 5.1
  30. */
  31. @SpringBootTest
  32. @AutoConfigureWebTestClient
  33. public class ServerOAuth2ResourceServerApplicationITests {
  34. Consumer<HttpHeaders> noScopesToken = (http) -> http.setBearerAuth(
  35. "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJzdWJqZWN0IiwiZXhwIjo0NjgzODA1MTI4fQ.ULEPdHG-MK5GlrTQMhgqcyug2brTIZaJIrahUeq9zaiwUSdW83fJ7W1IDd2Z3n4a25JY2uhEcoV95lMfccHR6y_2DLrNvfta22SumY9PEDF2pido54LXG6edIGgarnUbJdR4rpRe_5oRGVa8gDx8FnuZsNv6StSZHAzw5OsuevSTJ1UbJm4UfX3wiahFOQ2OI6G-r5TB2rQNdiPHuNyzG5yznUqRIZ7-GCoMqHMaC-1epKxiX8gYXRROuUYTtcMNa86wh7OVDmvwVmFioRcR58UWBRoO1XQexTtOQq_t8KYsrPZhb9gkyW8x2bAQF-d0J0EJY8JslaH6n4RBaZISww");
  36. Consumer<HttpHeaders> messageReadToken = (http) -> http.setBearerAuth(
  37. "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJzdWJqZWN0Iiwic2NvcGUiOiJtZXNzYWdlOnJlYWQiLCJleHAiOjQ2ODM4MDUxNDF9.h-j6FKRFdnTdmAueTZCdep45e6DPwqM68ZQ8doIJ1exi9YxAlbWzOwId6Bd0L5YmCmp63gGQgsBUBLzwnZQ8kLUgUOBEC3UzSWGRqMskCY9_k9pX0iomX6IfF3N0PaYs0WPC4hO1s8wfZQ-6hKQ4KigFi13G9LMLdH58PRMK0pKEvs3gCbHJuEPw-K5ORlpdnleUTQIwINafU57cmK3KocTeknPAM_L716sCuSYGvDl6xUTXO7oPdrXhS_EhxLP6KxrpI1uD4Ea_5OWTh7S0Wx5LLDfU6wBG1DowN20d374zepOIEkR-Jnmr_QlR44vmRqS5ncrF-1R0EGcPX49U6A");
  38. @Autowired
  39. private WebTestClient rest;
  40. @Test
  41. void getWhenValidBearerTokenThenAllows() {
  42. // @formatter:off
  43. this.rest.get().uri("/")
  44. .headers(this.noScopesToken)
  45. .exchange()
  46. .expectStatus().isOk()
  47. .expectBody(String.class).isEqualTo("Hello, subject!");
  48. // @formatter:on
  49. }
  50. // -- tests with scopes
  51. @Test
  52. void getWhenValidBearerTokenThenScopedRequestsAlsoWork() {
  53. // @formatter:off
  54. this.rest.get().uri("/message")
  55. .headers(this.messageReadToken)
  56. .exchange()
  57. .expectStatus().isOk()
  58. .expectBody(String.class).isEqualTo("secret message");
  59. // @formatter:on
  60. }
  61. @Test
  62. void getWhenInsufficientlyScopedBearerTokenThenDeniesScopedMethodAccess() {
  63. // @formatter:off
  64. this.rest.get().uri("/message")
  65. .headers(this.noScopesToken)
  66. .exchange()
  67. .expectStatus().isForbidden()
  68. .expectHeader().value(HttpHeaders.WWW_AUTHENTICATE, containsString("Bearer error=\"insufficient_scope\""));
  69. // @formatter:on
  70. }
  71. }