HelloWebfluxMethodApplicationTests.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2002-2017 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 static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
  18. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
  19. import java.util.function.Consumer;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.boot.test.context.SpringBootTest;
  24. import org.springframework.context.ApplicationContext;
  25. import org.springframework.http.HttpHeaders;
  26. import org.springframework.http.HttpStatus;
  27. import org.springframework.security.test.context.support.WithMockUser;
  28. import org.springframework.test.context.junit4.SpringRunner;
  29. import org.springframework.test.web.reactive.server.WebTestClient;
  30. /**
  31. * @author Rob Winch
  32. * @since 5.0
  33. */
  34. @RunWith(SpringRunner.class)
  35. @SpringBootTest
  36. public class HelloWebfluxMethodApplicationTests {
  37. WebTestClient rest;
  38. @Autowired
  39. public void setup(ApplicationContext context) {
  40. this.rest = WebTestClient
  41. .bindToApplicationContext(context)
  42. .apply(springSecurity())
  43. .configureClient()
  44. .build();
  45. }
  46. @Test
  47. public void messageWhenNotAuthenticated() {
  48. this.rest
  49. .get()
  50. .uri("/message")
  51. .exchange()
  52. .expectStatus().isUnauthorized();
  53. }
  54. @Test
  55. public void messageWhenUserThenForbidden() {
  56. this.rest
  57. .get()
  58. .uri("/message")
  59. .headers(robsCredentials())
  60. .exchange()
  61. .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
  62. }
  63. @Test
  64. public void messageWhenAdminThenOk() {
  65. this.rest
  66. .get()
  67. .uri("/message")
  68. .headers(adminCredentials())
  69. .exchange()
  70. .expectStatus().isOk()
  71. .expectBody(String.class).isEqualTo("Hello World!");
  72. }
  73. @Test
  74. @WithMockUser
  75. public void messageWhenWithMockUserThenForbidden() {
  76. this.rest
  77. .get()
  78. .uri("/message")
  79. .exchange()
  80. .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
  81. }
  82. @Test
  83. @WithMockUser(roles = "ADMIN")
  84. public void messageWhenWithMockAdminThenOk() {
  85. this.rest
  86. .get()
  87. .uri("/message")
  88. .exchange()
  89. .expectStatus().isOk()
  90. .expectBody(String.class).isEqualTo("Hello World!");
  91. }
  92. @Test
  93. public void messageWhenMutateWithMockUserThenForbidden() {
  94. this.rest
  95. .mutateWith(mockUser())
  96. .get()
  97. .uri("/message")
  98. .exchange()
  99. .expectStatus().isEqualTo(HttpStatus.FORBIDDEN);
  100. }
  101. @Test
  102. public void messageWhenMutateWithMockAdminThenOk() {
  103. this.rest
  104. .mutateWith(mockUser().roles("ADMIN"))
  105. .get()
  106. .uri("/message")
  107. .exchange()
  108. .expectStatus().isOk()
  109. .expectBody(String.class).isEqualTo("Hello World!");
  110. }
  111. private Consumer<HttpHeaders> robsCredentials() {
  112. return (httpHeaders) -> httpHeaders.setBasicAuth("rob", "rob");
  113. }
  114. private Consumer<HttpHeaders> adminCredentials() {
  115. return (httpHeaders) -> httpHeaders.setBasicAuth("admin", "admin");
  116. }
  117. }