|
@@ -20,13 +20,10 @@ package sample;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseCookie;
|
|
|
-import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers;
|
|
|
import org.springframework.security.web.server.WebFilterChainFilter;
|
|
|
-import org.springframework.security.web.server.header.ContentTypeOptionsHttpHeadersWriter;
|
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
|
import org.springframework.test.context.ContextConfiguration;
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
@@ -35,9 +32,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
|
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
|
|
|
|
-import java.nio.charset.Charset;
|
|
|
-import java.util.Base64;
|
|
|
-
|
|
|
+import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
|
|
|
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
|
|
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
|
|
|
|
@@ -59,188 +54,91 @@ public class HelloWebfluxFnApplicationTests {
|
|
|
@Before
|
|
|
public void setup() {
|
|
|
this.rest = WebTestClient
|
|
|
- .bindToRouterFunction(routerFunction)
|
|
|
- .webFilter(springSecurityFilterChain)
|
|
|
+ .bindToRouterFunction(this.routerFunction)
|
|
|
+ .webFilter(this.springSecurityFilterChain)
|
|
|
+ .apply(springSecurity())
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void basicRequired() throws Exception {
|
|
|
+ public void basicWhenNoCredentialsThenUnauthorized() throws Exception {
|
|
|
this.rest
|
|
|
.get()
|
|
|
- .uri("/principal")
|
|
|
+ .uri("/")
|
|
|
.exchange()
|
|
|
.expectStatus().isUnauthorized();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void basicWorks() throws Exception {
|
|
|
+ public void basicWhenValidCredentialsThenOk() throws Exception {
|
|
|
this.rest
|
|
|
.mutate()
|
|
|
- .filter(robsCredentials())
|
|
|
+ .filter(userCredentials())
|
|
|
.build()
|
|
|
.get()
|
|
|
- .uri("/principal")
|
|
|
+ .uri("/")
|
|
|
.exchange()
|
|
|
.expectStatus().isOk()
|
|
|
- .expectBody().json("{\"username\":\"rob\"}");
|
|
|
+ .expectBody().json("{\"message\":\"Hello user!\"}");
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void basicWhenPasswordInvalid401() throws Exception {
|
|
|
+ public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
|
|
this.rest
|
|
|
.mutate()
|
|
|
.filter(invalidPassword())
|
|
|
.build()
|
|
|
.get()
|
|
|
- .uri("/principal")
|
|
|
+ .uri("/")
|
|
|
.exchange()
|
|
|
.expectStatus().isUnauthorized()
|
|
|
.expectBody().isEmpty();
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void authorizationAdmin403() throws Exception {
|
|
|
- this.rest
|
|
|
- .mutate()
|
|
|
- .filter(robsCredentials())
|
|
|
- .build()
|
|
|
- .get()
|
|
|
- .uri("/admin")
|
|
|
- .exchange()
|
|
|
- .expectStatus().isEqualTo(HttpStatus.FORBIDDEN)
|
|
|
- .expectBody().isEmpty();
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void authorizationAdmin200() throws Exception {
|
|
|
- this.rest
|
|
|
- .mutate()
|
|
|
- .filter(adminCredentials())
|
|
|
- .build()
|
|
|
- .get()
|
|
|
- .uri("/admin")
|
|
|
- .exchange()
|
|
|
- .expectStatus().isOk();
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void basicMissingUser401() throws Exception {
|
|
|
- this.rest
|
|
|
- .mutate()
|
|
|
- .filter(basicAuthentication("missing-user", "password"))
|
|
|
- .build()
|
|
|
- .get()
|
|
|
- .uri("/admin")
|
|
|
- .exchange()
|
|
|
- .expectStatus().isUnauthorized();
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void basicInvalidPassword401() throws Exception {
|
|
|
- this.rest
|
|
|
- .mutate()
|
|
|
- .filter(invalidPassword())
|
|
|
- .build()
|
|
|
- .get()
|
|
|
- .uri("/admin")
|
|
|
- .exchange()
|
|
|
- .expectStatus().isUnauthorized();
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void basicInvalidParts401() throws Exception {
|
|
|
- this.rest
|
|
|
- .get()
|
|
|
- .uri("/admin")
|
|
|
- .header("Authorization", "Basic " + base64Encode("no colon"))
|
|
|
- .exchange()
|
|
|
- .expectStatus().isUnauthorized();
|
|
|
- }
|
|
|
-
|
|
|
@Test
|
|
|
public void sessionWorks() throws Exception {
|
|
|
ExchangeResult result = this.rest
|
|
|
.mutate()
|
|
|
- .filter(robsCredentials())
|
|
|
+ .filter(userCredentials())
|
|
|
.build()
|
|
|
.get()
|
|
|
- .uri("/principal")
|
|
|
+ .uri("/")
|
|
|
.exchange()
|
|
|
+ .expectStatus().isOk()
|
|
|
.returnResult(String.class);
|
|
|
|
|
|
ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
|
|
|
|
|
|
this.rest
|
|
|
.get()
|
|
|
- .uri("/principal")
|
|
|
+ .uri("/")
|
|
|
.cookie(session.getName(), session.getValue())
|
|
|
.exchange()
|
|
|
.expectStatus().isOk();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void mockSupport() throws Exception {
|
|
|
- WebTestClient mockRest = WebTestClient.bindToRouterFunction(this.routerFunction)
|
|
|
- .webFilter(springSecurityFilterChain)
|
|
|
- .apply(springSecurity())
|
|
|
- .build();
|
|
|
-
|
|
|
- mockRest
|
|
|
- .mutateWith(SecurityMockServerConfigurers.mockUser())
|
|
|
- .get()
|
|
|
- .uri("/principal")
|
|
|
- .exchange()
|
|
|
- .expectStatus().isOk();
|
|
|
-
|
|
|
- mockRest
|
|
|
- .get()
|
|
|
- .uri("/principal")
|
|
|
- .exchange()
|
|
|
- .expectStatus().isUnauthorized();
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void principal() throws Exception {
|
|
|
+ public void mockSupportWhenValidMockUserThenOk() throws Exception {
|
|
|
this.rest
|
|
|
- .mutate()
|
|
|
- .filter(robsCredentials())
|
|
|
- .build()
|
|
|
+ .mutateWith(mockUser())
|
|
|
.get()
|
|
|
- .uri("/principal")
|
|
|
+ .uri("/")
|
|
|
.exchange()
|
|
|
.expectStatus().isOk()
|
|
|
- .expectBody().json("{\"username\" : \"rob\"}");
|
|
|
- }
|
|
|
+ .expectBody().json("{\"message\":\"Hello user!\"}");
|
|
|
|
|
|
- @Test
|
|
|
- public void headers() throws Exception {
|
|
|
this.rest
|
|
|
- .mutate()
|
|
|
- .filter(robsCredentials())
|
|
|
- .build()
|
|
|
.get()
|
|
|
- .uri("/principal")
|
|
|
+ .uri("/")
|
|
|
.exchange()
|
|
|
- .expectHeader().valueEquals(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate")
|
|
|
- .expectHeader().valueEquals(HttpHeaders.EXPIRES, "0")
|
|
|
- .expectHeader().valueEquals(HttpHeaders.PRAGMA, "no-cache")
|
|
|
- .expectHeader().valueEquals(ContentTypeOptionsHttpHeadersWriter.X_CONTENT_OPTIONS, ContentTypeOptionsHttpHeadersWriter.NOSNIFF);
|
|
|
+ .expectStatus().isUnauthorized();
|
|
|
}
|
|
|
|
|
|
- private ExchangeFilterFunction robsCredentials() {
|
|
|
- return basicAuthentication("rob","rob");
|
|
|
+ private ExchangeFilterFunction userCredentials() {
|
|
|
+ return basicAuthentication("user","user");
|
|
|
}
|
|
|
|
|
|
private ExchangeFilterFunction invalidPassword() {
|
|
|
- return basicAuthentication("rob","INVALID");
|
|
|
- }
|
|
|
-
|
|
|
- private ExchangeFilterFunction adminCredentials() {
|
|
|
- return basicAuthentication("admin","admin");
|
|
|
- }
|
|
|
-
|
|
|
- private String base64Encode(String value) {
|
|
|
- return Base64.getEncoder().encodeToString(value.getBytes(Charset.defaultCharset()));
|
|
|
+ return basicAuthentication("user","INVALID");
|
|
|
}
|
|
|
}
|