Fix spring security tests.
This commit is contained in:
@@ -16,21 +16,15 @@
|
||||
|
||||
package org.springframework.cloud.netflix.feign.valid;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient;
|
||||
@@ -38,9 +32,10 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
import org.springframework.cloud.netflix.ribbon.StaticServerList;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
@@ -51,21 +46,27 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
|
||||
import feign.Client;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Objects;
|
||||
import feign.Client;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = FeignHttpClientTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
|
||||
"spring.application.name=feignclienttest", "feign.hystrix.enabled=false",
|
||||
"feign.okhttp.enabled=false" })
|
||||
@DirtiesContext
|
||||
public class FeignHttpClientTests {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
@LocalServerPort
|
||||
private int port = 0;
|
||||
|
||||
@Autowired
|
||||
@@ -82,7 +83,7 @@ public class FeignHttpClientTests {
|
||||
}
|
||||
|
||||
protected interface BaseTestClient {
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/hello")
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
Hello getHello();
|
||||
|
||||
@RequestMapping(method = RequestMethod.PATCH, value = "/hellop", consumes = "application/json")
|
||||
@@ -90,7 +91,7 @@ public class FeignHttpClientTests {
|
||||
}
|
||||
|
||||
protected interface UserService {
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/{id}")
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
User getUser(@PathVariable("id") long id);
|
||||
}
|
||||
|
||||
@@ -226,7 +227,7 @@ public class FeignHttpClientTests {
|
||||
@Configuration
|
||||
static class LocalRibbonClientConfiguration {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
@LocalServerPort
|
||||
private int port = 0;
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.netflix.hystrix;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -32,28 +28,36 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = HystrixOnlyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles("proxysecurity")
|
||||
public class HystrixOnlyTests {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Value("${security.user.username}")
|
||||
@@ -64,24 +68,27 @@ public class HystrixOnlyTests {
|
||||
|
||||
@Test
|
||||
public void testNormalExecution() {
|
||||
String s = new TestRestTemplate()
|
||||
.getForObject("http://localhost:" + this.port + "/", String.class);
|
||||
assertEquals("incorrect response", "Hello world", s);
|
||||
ResponseEntity<String> res = new TestRestTemplate()
|
||||
.getForEntity("http://localhost:" + this.port + "/", String.class);
|
||||
assertEquals("incorrect response", "Hello world", res.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailureFallback() {
|
||||
String s = new TestRestTemplate()
|
||||
.getForObject("http://localhost:" + this.port + "/fail", String.class);
|
||||
assertEquals("incorrect fallback", "Fallback Hello world", s);
|
||||
ResponseEntity<String> res = new TestRestTemplate()
|
||||
.getForEntity("http://localhost:" + this.port + "/fail", String.class);
|
||||
assertEquals("incorrect fallback", "Fallback Hello world", res.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testHystrixHealth() {
|
||||
Map<?, ?> map = getHealth();
|
||||
assertTrue("Missing hystrix health key", map.containsKey("hystrix"));
|
||||
Map<?, ?> hystrix = (Map<?, ?>) map.get("hystrix");
|
||||
assertEquals("Wrong hystrix status", "UP", hystrix.get("status"));
|
||||
Map map = getHealth();
|
||||
assertThat(map).containsKeys("details");
|
||||
Map details = (Map) map.get("details");
|
||||
assertThat(details).containsKeys("hystrix");
|
||||
Map hystrix = (Map) details.get("hystrix");
|
||||
assertThat(hystrix).containsEntry("status", "UP");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -94,7 +101,7 @@ public class HystrixOnlyTests {
|
||||
|
||||
|
||||
|
||||
private Map<?, ?> getHealth() {
|
||||
private Map getHealth() {
|
||||
return new TestRestTemplate().exchange(
|
||||
"http://localhost:" + this.port + "/admin/health", HttpMethod.GET,
|
||||
new HttpEntity<Void>(createBasicAuthHeader(username, password)),
|
||||
|
||||
@@ -18,23 +18,25 @@ package org.springframework.cloud.netflix.hystrix.security;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
import com.netflix.hystrix.strategy.HystrixPlugins;
|
||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.cloud.netflix.hystrix.security.app.CustomConcurrenyStrategy;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.netflix.hystrix.strategy.HystrixPlugins;
|
||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -42,23 +44,23 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* the security context from a hystrix command.
|
||||
* @author Daniel Lavoie
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringBootTest(classes = HystrixSecurityApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT,
|
||||
properties = { "username.ribbon.listOfServers=localhost:${local.server.port}",
|
||||
"feign.hystrix.enabled=true"})
|
||||
@ActiveProfiles("proxysecurity")
|
||||
public class HystrixSecurityTests {
|
||||
@Autowired
|
||||
private CustomConcurrenyStrategy customConcurrenyStrategy;
|
||||
|
||||
@Value("${local.server.port}")
|
||||
@LocalServerPort
|
||||
private String serverPort;
|
||||
|
||||
@Value("${security.user.username}")
|
||||
private String username;
|
||||
//TODOO: move to constants in TestAutoConfiguration
|
||||
private String username = "user";
|
||||
|
||||
@Value("${security.user.password}")
|
||||
private String password;
|
||||
private String password = "password";
|
||||
|
||||
@Test
|
||||
public void testSecurityConcurrencyStrategyInstalled() {
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2015 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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.netflix.test;
|
||||
|
||||
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@Import(NoopDiscoveryClientAutoConfiguration.class)
|
||||
public class NoopDiscoveryClientConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2013-2015 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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.netflix.test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@Import({NoopDiscoveryClientAutoConfiguration.class})
|
||||
@AutoConfigureBefore(SecurityAutoConfiguration.class)
|
||||
public class TestAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
protected static class TestSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
|
||||
|
||||
TestSecurityConfiguration() {
|
||||
super(true);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UserDetailsService userDetailsService() {
|
||||
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
|
||||
manager.createUser(User.withUsername("user").password("password").roles("USER").build());
|
||||
return manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
// super.configure(http);
|
||||
http.antMatcher("/proxy-username")
|
||||
.httpBasic()
|
||||
.and()
|
||||
.authorizeRequests().antMatchers("/**").permitAll();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
org.springframework.cloud.client.discovery.EnableDiscoveryClient=\
|
||||
org.springframework.cloud.netflix.test.NoopDiscoveryClientConfiguration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.netflix.test.TestAutoConfiguration
|
||||
|
||||
@@ -56,9 +56,4 @@ feignClient:
|
||||
localappName: localapp
|
||||
methodLevelRequestMappingPath: /hello2
|
||||
myPlaceholderHeader: myPlaceholderHeaderValue
|
||||
security:
|
||||
basic:
|
||||
path: /proxy-username
|
||||
user:
|
||||
username: user
|
||||
password: password
|
||||
endpoints.default.web.enabled: true
|
||||
|
||||
Reference in New Issue
Block a user