@@ -60,13 +60,17 @@ public class HealthMvcEndpointTests {
60
60
61
61
private MockEnvironment environment ;
62
62
63
- private UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken (
64
- "user" , "password" ,
65
- AuthorityUtils .commaSeparatedStringToAuthorityList ("ROLE_USER" ));
63
+ private UsernamePasswordAuthenticationToken user = createAuthenticationToken ("ROLE_USER" );
66
64
67
- private UsernamePasswordAuthenticationToken admin = new UsernamePasswordAuthenticationToken (
68
- "user" , "password" ,
69
- AuthorityUtils .commaSeparatedStringToAuthorityList ("ROLE_ADMIN" ));
65
+ private UsernamePasswordAuthenticationToken admin = createAuthenticationToken ("ROLE_ADMIN" );
66
+
67
+ private UsernamePasswordAuthenticationToken hero = createAuthenticationToken ("ROLE_HERO" );
68
+
69
+ private UsernamePasswordAuthenticationToken createAuthenticationToken (String authority ) {
70
+ return new UsernamePasswordAuthenticationToken (
71
+ "user" , "password" ,
72
+ AuthorityUtils .commaSeparatedStringToAuthorityList (authority ));
73
+ }
70
74
71
75
@ Before
72
76
public void init () {
@@ -147,17 +151,26 @@ public void secureNonAdmin() {
147
151
148
152
@ Test
149
153
public void secureCustomRole () {
150
- this .mvc = new HealthMvcEndpoint (this .endpoint , false );
151
- this .mvc .setEnvironment (this .environment );
152
154
this .environment .getPropertySources ().addLast (SECURITY_ROLES );
153
155
given (this .endpoint .invoke ())
154
156
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
155
- Object result = this .mvc .invoke (this .user );
157
+ Object result = this .mvc .invoke (this .hero );
156
158
assertThat (result instanceof Health ).isTrue ();
157
159
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
158
160
assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
159
161
}
160
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
+
161
174
@ Test
162
175
public void healthIsCached () {
163
176
given (this .endpoint .getTimeToLive ()).willReturn (10000L );
0 commit comments