|
30 | 30 | package org.hisp.dhis.webapi.controller.security; |
31 | 31 |
|
32 | 32 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
33 | 36 |
|
34 | 37 | import java.util.Properties; |
35 | 38 | import org.hisp.dhis.external.conf.ConfigurationKey; |
36 | 39 | import org.hisp.dhis.external.conf.DhisConfigurationProvider; |
37 | 40 | import org.hisp.dhis.http.HttpStatus; |
| 41 | +import org.hisp.dhis.jsontree.JsonObject; |
38 | 42 | import org.hisp.dhis.test.config.H2DhisConfigurationProvider; |
39 | 43 | import org.hisp.dhis.test.webapi.H2ControllerIntegrationTestBase; |
40 | 44 | import org.hisp.dhis.test.webapi.json.domain.JsonImpersonateUserResponse; |
@@ -209,4 +213,51 @@ void testImpersonateWithAuthorityNotAllowedToBecomeSuperuser() { |
209 | 213 | assertEquals("Forbidden", response.getHttpStatus()); |
210 | 214 | assertEquals("ERROR", response.getStatus()); |
211 | 215 | } |
| 216 | + |
| 217 | + /** |
| 218 | + * PIN: the {@code /api/me} {@code impersonation} field carries the impersonator USERNAME (from |
| 219 | + * {@code Authentication#getName()}), never the display name. Frontends match it against the |
| 220 | + * username; PR #24124 accidentally changed it to first+surname which this test would catch. |
| 221 | + */ |
| 222 | + @Test |
| 223 | + void testMeImpersonationFieldsDuringAndAfterImpersonation() { |
| 224 | + User impersonator = createUserWithAuth("impuser", "F_IMPERSONATE_USER"); |
| 225 | + createUserWithAuth("target", "NONE"); |
| 226 | + injectSecurityContextUser(impersonator); |
| 227 | + |
| 228 | + POST("/auth/impersonate?username=target").content(HttpStatus.OK); |
| 229 | + |
| 230 | + JsonObject during = |
| 231 | + GET("/me?fields=username,impersonation,canImpersonate").content(HttpStatus.OK); |
| 232 | + assertEquals("target", during.getString("username").string()); |
| 233 | + assertEquals("impuser", during.getString("impersonation").string()); |
| 234 | + assertNotEquals(impersonator.getName(), during.getString("impersonation").string()); |
| 235 | + // the impersonated user itself holds no impersonation authority |
| 236 | + assertFalse(during.get("canImpersonate").exists()); |
| 237 | + |
| 238 | + POST("/auth/impersonateExit").content(HttpStatus.OK); |
| 239 | + |
| 240 | + JsonObject after = |
| 241 | + GET("/me?fields=username,impersonation,canImpersonate").content(HttpStatus.OK); |
| 242 | + assertEquals("impuser", after.getString("username").string()); |
| 243 | + assertFalse(after.get("impersonation").exists()); |
| 244 | + assertTrue(after.getBoolean("canImpersonate").booleanValue()); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + void testMeCanImpersonateForAdminWhenFeatureEnabled() { |
| 249 | + JsonObject me = GET("/me?fields=canImpersonate,impersonation").content(HttpStatus.OK); |
| 250 | + assertTrue(me.getBoolean("canImpersonate").booleanValue()); |
| 251 | + assertFalse(me.get("impersonation").exists()); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + void testMeCanImpersonateAbsentWithoutAuthority() { |
| 256 | + User guest = createUserWithAuth("guestimp", "NONE"); |
| 257 | + injectSecurityContextUser(guest); |
| 258 | + |
| 259 | + JsonObject me = GET("/me?fields=canImpersonate,impersonation").content(HttpStatus.OK); |
| 260 | + assertFalse(me.get("canImpersonate").exists()); |
| 261 | + assertFalse(me.get("impersonation").exists()); |
| 262 | + } |
212 | 263 | } |
0 commit comments