@@ -60,13 +60,17 @@ public class HealthMvcEndpointTests {
6060
6161 private MockEnvironment environment ;
6262
63- private UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken (
64- "user" , "password" ,
65- AuthorityUtils .commaSeparatedStringToAuthorityList ("ROLE_USER" ));
63+ private UsernamePasswordAuthenticationToken user = createAuthenticationToken ("ROLE_USER" );
6664
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+ }
7074
7175 @ Before
7276 public void init () {
@@ -147,17 +151,26 @@ public void secureNonAdmin() {
147151
148152 @ Test
149153 public void secureCustomRole () {
150- this .mvc = new HealthMvcEndpoint (this .endpoint , false );
151- this .mvc .setEnvironment (this .environment );
152154 this .environment .getPropertySources ().addLast (SECURITY_ROLES );
153155 given (this .endpoint .invoke ())
154156 .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 );
156158 assertThat (result instanceof Health ).isTrue ();
157159 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
158160 assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
159161 }
160162
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+
161174 @ Test
162175 public void healthIsCached () {
163176 given (this .endpoint .getTimeToLive ()).willReturn (10000L );
0 commit comments