spring-security/samples/javaconfig/hellowebfluxfn/src/test/java/sample/HelloWebfluxFnApplicationTe...

121 lines
3.3 KiB
Java
Raw Normal View History

2017-05-17 04:38:07 +08:00
/*
*
* * Copyright 2002-2017 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
2017-05-17 04:38:07 +08:00
*
*/
package sample;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
2017-05-24 05:29:30 +08:00
import org.springframework.security.web.server.WebFilterChainFilter;
import org.springframework.test.context.ActiveProfiles;
2017-05-17 04:38:07 +08:00
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
2017-05-24 05:29:30 +08:00
import org.springframework.web.reactive.function.server.RouterFunction;
2017-05-17 04:38:07 +08:00
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
2017-05-17 04:38:07 +08:00
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
/**
* @author Rob Winch
* @since 5.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = HelloWebfluxFnApplication.class)
@ActiveProfiles("test")
2017-05-17 04:38:07 +08:00
public class HelloWebfluxFnApplicationTests {
@Autowired
2017-05-24 05:29:30 +08:00
RouterFunction<?> routerFunction;
@Autowired
WebFilterChainFilter springSecurityFilterChain;
2017-05-17 04:38:07 +08:00
WebTestClient rest;
@Before
public void setup() {
2017-05-24 05:29:30 +08:00
this.rest = WebTestClient
.bindToRouterFunction(this.routerFunction)
.webFilter(this.springSecurityFilterChain)
.apply(springSecurity())
2017-05-24 05:29:30 +08:00
.build();
2017-05-17 04:38:07 +08:00
}
@Test
public void basicWhenNoCredentialsThenUnauthorized() throws Exception {
2017-05-17 04:38:07 +08:00
this.rest
.get()
.uri("/")
2017-05-17 04:38:07 +08:00
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void basicWhenValidCredentialsThenOk() throws Exception {
2017-05-17 04:38:07 +08:00
this.rest
.mutate()
.filter(userCredentials())
.build()
2017-05-17 04:38:07 +08:00
.get()
.uri("/")
2017-05-17 04:38:07 +08:00
.exchange()
.expectStatus().isOk()
.expectBody().json("{\"message\":\"Hello user!\"}");
2017-05-17 04:38:07 +08:00
}
@Test
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
2017-05-17 04:38:07 +08:00
this.rest
.mutate()
2017-05-17 04:38:07 +08:00
.filter(invalidPassword())
.build()
2017-05-17 04:38:07 +08:00
.get()
.uri("/")
2017-05-17 04:38:07 +08:00
.exchange()
.expectStatus().isUnauthorized()
.expectBody().isEmpty();
}
@Test
public void mockSupportWhenValidMockUserThenOk() throws Exception {
2017-05-17 04:38:07 +08:00
this.rest
.mutateWith(mockUser())
2017-05-17 04:38:07 +08:00
.get()
.uri("/")
2017-05-17 04:38:07 +08:00
.exchange()
.expectStatus().isOk()
.expectBody().json("{\"message\":\"Hello user!\"}");
2017-05-17 04:38:07 +08:00
this.rest
.get()
.uri("/")
.exchange()
.expectStatus().isUnauthorized();
2017-05-17 04:38:07 +08:00
}
private ExchangeFilterFunction userCredentials() {
return basicAuthentication("user","user");
2017-05-17 04:38:07 +08:00
}
private ExchangeFilterFunction invalidPassword() {
return basicAuthentication("user","INVALID");
2017-05-17 04:38:07 +08:00
}
}