|
42 | 42 | * @author Christian Dupuis
|
43 | 43 | * @author Dave Syer
|
44 | 44 | * @author Andy Wilkinson
|
| 45 | + * @author Eddú Meléndez |
45 | 46 | */
|
46 | 47 | public class HealthMvcEndpointTests {
|
47 | 48 |
|
48 | 49 | private static final PropertySource<?> NON_SENSITIVE = new MapPropertySource("test",
|
49 | 50 | Collections.<String, Object>singletonMap("endpoints.health.sensitive",
|
50 | 51 | "false"));
|
51 | 52 |
|
| 53 | + private static final PropertySource<?> SECURITY_ROLES = new MapPropertySource("test", |
| 54 | + Collections.<String, Object>singletonMap("management.security.roles", |
| 55 | + "HERO, USER")); |
| 56 | + |
52 | 57 | private HealthEndpoint endpoint = null;
|
53 | 58 |
|
54 | 59 | private HealthMvcEndpoint mvc = null;
|
55 | 60 |
|
56 | 61 | private MockEnvironment environment;
|
57 | 62 |
|
58 |
| - private UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken( |
59 |
| - "user", "password", |
60 |
| - AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); |
| 63 | + private UsernamePasswordAuthenticationToken user = createAuthenticationToken("ROLE_USER"); |
| 64 | + |
| 65 | + private UsernamePasswordAuthenticationToken admin = createAuthenticationToken("ROLE_ADMIN"); |
| 66 | + |
| 67 | + private UsernamePasswordAuthenticationToken hero = createAuthenticationToken("ROLE_HERO"); |
61 | 68 |
|
62 |
| - private UsernamePasswordAuthenticationToken admin = new UsernamePasswordAuthenticationToken( |
63 |
| - "user", "password", |
64 |
| - AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN")); |
| 69 | + private UsernamePasswordAuthenticationToken createAuthenticationToken(String authority) { |
| 70 | + return new UsernamePasswordAuthenticationToken( |
| 71 | + "user", "password", |
| 72 | + AuthorityUtils.commaSeparatedStringToAuthorityList(authority)); |
| 73 | + } |
65 | 74 |
|
66 | 75 | @Before
|
67 | 76 | public void init() {
|
@@ -140,6 +149,28 @@ public void secureNonAdmin() {
|
140 | 149 | assertThat(((Health) result).getDetails().get("foo")).isNull();
|
141 | 150 | }
|
142 | 151 |
|
| 152 | + @Test |
| 153 | + public void secureCustomRole() { |
| 154 | + this.environment.getPropertySources().addLast(SECURITY_ROLES); |
| 155 | + given(this.endpoint.invoke()) |
| 156 | + .willReturn(new Health.Builder().up().withDetail("foo", "bar").build()); |
| 157 | + Object result = this.mvc.invoke(this.hero); |
| 158 | + assertThat(result instanceof Health).isTrue(); |
| 159 | + assertThat(((Health) result).getStatus() == Status.UP).isTrue(); |
| 160 | + assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void secureCustomRoleNoAccess() { |
| 165 | + this.environment.getPropertySources().addLast(SECURITY_ROLES); |
| 166 | + given(this.endpoint.invoke()) |
| 167 | + .willReturn(new Health.Builder().up().withDetail("foo", "bar").build()); |
| 168 | + Object result = this.mvc.invoke(this.admin); |
| 169 | + assertThat(result instanceof Health).isTrue(); |
| 170 | + assertThat(((Health) result).getStatus() == Status.UP).isTrue(); |
| 171 | + assertThat(((Health) result).getDetails().get("foo")).isNull(); |
| 172 | + } |
| 173 | + |
143 | 174 | @Test
|
144 | 175 | public void healthIsCached() {
|
145 | 176 | given(this.endpoint.getTimeToLive()).willReturn(10000L);
|
|
0 commit comments