1818
1919import java .util .Arrays ;
2020import java .util .Collections ;
21+ import java .util .Set ;
2122
2223import javax .servlet .http .HttpServletRequest ;
2324
3435import org .springframework .mock .env .MockEnvironment ;
3536import org .springframework .mock .web .MockHttpServletRequest ;
3637import org .springframework .mock .web .MockServletContext ;
38+ import org .springframework .security .core .Authentication ;
39+ import org .springframework .security .core .authority .SimpleGrantedAuthority ;
3740
3841import static org .assertj .core .api .Assertions .assertThat ;
3942import static org .mockito .BDDMockito .given ;
43+ import static org .mockito .Mockito .doReturn ;
4044import static org .mockito .Mockito .mock ;
4145
4246/**
@@ -52,7 +56,7 @@ public class HealthMvcEndpointTests {
5256
5357 private static final PropertySource <?> SECURITY_ROLES = new MapPropertySource ("test" ,
5458 Collections .<String , Object >singletonMap ("management.security.roles" ,
55- "HERO, USER " ));
59+ "HERO" ));
5660
5761 private HttpServletRequest request = new MockHttpServletRequest ();
5862
@@ -62,13 +66,11 @@ public class HealthMvcEndpointTests {
6266
6367 private MockEnvironment environment ;
6468
65- private HttpServletRequest user = createAuthenticationToken ( "ROLE_USER " );
69+ private HttpServletRequest defaultUser = createAuthenticationRequest ( "ROLE_ACTUATOR " );
6670
67- private HttpServletRequest actuator = createAuthenticationToken ( "ROLE_ACTUATOR " );
71+ private HttpServletRequest hero = createAuthenticationRequest ( "HERO " );
6872
69- private HttpServletRequest hero = createAuthenticationToken ("ROLE_HERO" );
70-
71- private HttpServletRequest createAuthenticationToken (String role ) {
73+ private HttpServletRequest createAuthenticationRequest (String role ) {
7274 MockServletContext servletContext = new MockServletContext ();
7375 servletContext .declareRoles (role );
7476 return new MockHttpServletRequest (servletContext );
@@ -86,7 +88,7 @@ public void init() {
8688 @ Test
8789 public void up () {
8890 given (this .endpoint .invoke ()).willReturn (new Health .Builder ().up ().build ());
89- Object result = this .mvc .invoke (this .request );
91+ Object result = this .mvc .invoke (this .request , null );
9092 assertThat (result instanceof Health ).isTrue ();
9193 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
9294 }
@@ -95,7 +97,7 @@ public void up() {
9597 @ Test
9698 public void down () {
9799 given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
98- Object result = this .mvc .invoke (this .request );
100+ Object result = this .mvc .invoke (this .request , null );
99101 assertThat (result instanceof ResponseEntity ).isTrue ();
100102 ResponseEntity <Health > response = (ResponseEntity <Health >) result ;
101103 assertThat (response .getBody ().getStatus () == Status .DOWN ).isTrue ();
@@ -109,7 +111,7 @@ public void customMapping() {
109111 .willReturn (new Health .Builder ().status ("OK" ).build ());
110112 this .mvc .setStatusMapping (
111113 Collections .singletonMap ("OK" , HttpStatus .INTERNAL_SERVER_ERROR ));
112- Object result = this .mvc .invoke (this .request );
114+ Object result = this .mvc .invoke (this .request , null );
113115 assertThat (result instanceof ResponseEntity ).isTrue ();
114116 ResponseEntity <Health > response = (ResponseEntity <Health >) result ;
115117 assertThat (response .getBody ().getStatus ().equals (new Status ("OK" ))).isTrue ();
@@ -123,7 +125,7 @@ public void customMappingWithRelaxedName() {
123125 .willReturn (new Health .Builder ().outOfService ().build ());
124126 this .mvc .setStatusMapping (Collections .singletonMap ("out-of-service" ,
125127 HttpStatus .INTERNAL_SERVER_ERROR ));
126- Object result = this .mvc .invoke (this .request );
128+ Object result = this .mvc .invoke (this .request , null );
127129 assertThat (result instanceof ResponseEntity ).isTrue ();
128130 ResponseEntity <Health > response = (ResponseEntity <Health >) result ;
129131 assertThat (response .getBody ().getStatus ().equals (Status .OUT_OF_SERVICE )).isTrue ();
@@ -134,7 +136,7 @@ public void customMappingWithRelaxedName() {
134136 public void presenceOfRightRoleShouldExposeDetails () {
135137 given (this .endpoint .invoke ())
136138 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
137- Object result = this .mvc .invoke (this .actuator );
139+ Object result = this .mvc .invoke (this .defaultUser , null );
138140 assertThat (result instanceof Health ).isTrue ();
139141 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
140142 assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
@@ -145,7 +147,7 @@ public void managementSecurityDisabledShouldExposeDetails() throws Exception {
145147 this .mvc = new HealthMvcEndpoint (this .endpoint , false );
146148 given (this .endpoint .invoke ())
147149 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
148- Object result = this .mvc .invoke (this .user );
150+ Object result = this .mvc .invoke (this .defaultUser , null );
149151 assertThat (result instanceof Health ).isTrue ();
150152 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
151153 assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
@@ -155,18 +157,32 @@ public void managementSecurityDisabledShouldExposeDetails() throws Exception {
155157 public void rightRoleNotPresentShouldNotExposeDetails () {
156158 given (this .endpoint .invoke ())
157159 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
158- Object result = this .mvc .invoke (this .user );
160+ Object result = this .mvc .invoke (this .hero , null );
159161 assertThat (result instanceof Health ).isTrue ();
160162 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
161163 assertThat (((Health ) result ).getDetails ().get ("foo" )).isNull ();
162164 }
163165
166+ @ Test
167+ public void rightAuthorityPresentShouldExposeDetails () throws Exception {
168+ this .environment .getPropertySources ().addLast (SECURITY_ROLES );
169+ Authentication principal = mock (Authentication .class );
170+ Set <SimpleGrantedAuthority > authorities = Collections .singleton (new SimpleGrantedAuthority ("HERO" ));
171+ doReturn (authorities ).when (principal ).getAuthorities ();
172+ given (this .endpoint .invoke ())
173+ .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
174+ Object result = this .mvc .invoke (this .defaultUser , principal );
175+ assertThat (result instanceof Health ).isTrue ();
176+ assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
177+ assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
178+ }
179+
164180 @ Test
165181 public void customRolePresentShouldExposeDetails () {
166182 this .environment .getPropertySources ().addLast (SECURITY_ROLES );
167183 given (this .endpoint .invoke ())
168184 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
169- Object result = this .mvc .invoke (this .hero );
185+ Object result = this .mvc .invoke (this .hero , null );
170186 assertThat (result instanceof Health ).isTrue ();
171187 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
172188 assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
@@ -177,38 +193,51 @@ public void customRoleShouldNotExposeDetailsForDefaultRole() {
177193 this .environment .getPropertySources ().addLast (SECURITY_ROLES );
178194 given (this .endpoint .invoke ())
179195 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
180- Object result = this .mvc .invoke (this .actuator );
196+ Object result = this .mvc .invoke (this .defaultUser , null );
181197 assertThat (result instanceof Health ).isTrue ();
182198 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
183199 assertThat (((Health ) result ).getDetails ().get ("foo" )).isNull ();
184200 }
185201
186202 @ Test
187- public void customRoleFromListShouldNotExposeDetailsForDefaultRole () {
203+ public void customRoleFromListShouldExposeDetails () {
188204 // gh-8314
189205 this .mvc = new HealthMvcEndpoint (this .endpoint , true ,
190206 Arrays .asList ("HERO" , "USER" ));
191207 given (this .endpoint .invoke ())
192208 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
193- Object result = this .mvc .invoke (this .hero );
209+ Object result = this .mvc .invoke (this .hero , null );
194210 assertThat (result instanceof Health ).isTrue ();
195211 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
196212 assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
197213 }
198214
215+ @ Test
216+ public void customRoleFromListShouldNotExposeDetailsForDefaultRole () {
217+ // gh-8314
218+ this .mvc = new HealthMvcEndpoint (this .endpoint , true ,
219+ Arrays .asList ("HERO" , "USER" ));
220+ given (this .endpoint .invoke ())
221+ .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
222+ Object result = this .mvc .invoke (this .defaultUser , null );
223+ assertThat (result instanceof Health ).isTrue ();
224+ assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
225+ assertThat (((Health ) result ).getDetails ().get ("foo" )).isNull ();
226+ }
227+
199228 @ Test
200229 public void healthIsCached () {
201230 given (this .endpoint .getTimeToLive ()).willReturn (10000L );
202231 given (this .endpoint .invoke ())
203232 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
204- Object result = this .mvc .invoke (this .actuator );
233+ Object result = this .mvc .invoke (this .defaultUser , null );
205234 assertThat (result instanceof Health ).isTrue ();
206235 Health health = (Health ) result ;
207236 assertThat (health .getStatus () == Status .UP ).isTrue ();
208237 assertThat (health .getDetails ()).hasSize (1 );
209238 assertThat (health .getDetails ().get ("foo" )).isEqualTo ("bar" );
210239 given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
211- result = this .mvc .invoke (this .request ); // insecure now
240+ result = this .mvc .invoke (this .request , null ); // insecure now
212241 assertThat (result instanceof Health ).isTrue ();
213242 health = (Health ) result ;
214243 // so the result is cached
@@ -222,11 +251,11 @@ public void noCachingWhenTimeToLiveIsZero() {
222251 given (this .endpoint .getTimeToLive ()).willReturn (0L );
223252 given (this .endpoint .invoke ())
224253 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
225- Object result = this .mvc .invoke (this .request );
254+ Object result = this .mvc .invoke (this .request , null );
226255 assertThat (result instanceof Health ).isTrue ();
227256 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
228257 given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
229- result = this .mvc .invoke (this .request );
258+ result = this .mvc .invoke (this .request , null );
230259 @ SuppressWarnings ("unchecked" )
231260 Health health = ((ResponseEntity <Health >) result ).getBody ();
232261 assertThat (health .getStatus () == Status .DOWN ).isTrue ();
@@ -237,12 +266,12 @@ public void newValueIsReturnedOnceTtlExpires() throws InterruptedException {
237266 given (this .endpoint .getTimeToLive ()).willReturn (50L );
238267 given (this .endpoint .invoke ())
239268 .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
240- Object result = this .mvc .invoke (this .request );
269+ Object result = this .mvc .invoke (this .request , null );
241270 assertThat (result instanceof Health ).isTrue ();
242271 assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
243272 Thread .sleep (100 );
244273 given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
245- result = this .mvc .invoke (this .request );
274+ result = this .mvc .invoke (this .request , null );
246275 @ SuppressWarnings ("unchecked" )
247276 Health health = ((ResponseEntity <Health >) result ).getBody ();
248277 assertThat (health .getStatus () == Status .DOWN ).isTrue ();
0 commit comments