HelloWebfluxMethodApplicationTests.java 4.0 KB

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