|
40 | 40 | import com.cloud.user.UserVO; |
41 | 41 |
|
42 | 42 | import org.apache.cloudstack.acl.RolePermissionEntity.Permission; |
| 43 | +import org.apache.cloudstack.utils.cache.LazyCache; |
| 44 | +import com.cloud.utils.Pair; |
43 | 45 |
|
44 | 46 | import junit.framework.TestCase; |
45 | 47 |
|
@@ -195,4 +197,134 @@ public void getApisAllowedToUserTestPermissionDenyForGivenApiShouldReturnEmptyLi |
195 | 197 | List<String> apisReceived = apiAccessCheckerSpy.getApisAllowedToUser(getTestRole(), getTestUser(), apiNames); |
196 | 198 | Assert.assertEquals(0, apisReceived.size()); |
197 | 199 | } |
| 200 | + |
| 201 | + // --- Tests for checkAccess(Account, String) --- |
| 202 | + |
| 203 | + @Test(expected = PermissionDeniedException.class) |
| 204 | + public void testCheckAccessAccountNullRoleShouldThrow() { |
| 205 | + Mockito.when(roleServiceMock.findRole(Mockito.anyLong())).thenReturn(null); |
| 206 | + apiAccessCheckerSpy.checkAccess(getTestAccount(), "someApi"); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void testCheckAccessAccountAdminShouldAllow() { |
| 211 | + Account adminAccount = new AccountVO("root admin", 1L, null, Account.Type.ADMIN, "admin-uuid"); |
| 212 | + Mockito.when(roleServiceMock.findRole(Mockito.anyLong())).thenReturn(new RoleVO(1L, "Admin", RoleType.Admin, "default admin role")); |
| 213 | + assertTrue(apiAccessCheckerSpy.checkAccess(adminAccount, "anyApi")); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void testCheckAccessAccountAllowedApi() { |
| 218 | + final String allowedApiName = "someAllowedApi"; |
| 219 | + final RolePermission permission = new RolePermissionVO(1L, allowedApiName, Permission.ALLOW, null); |
| 220 | + Mockito.when(roleServiceMock.findAllPermissionsBy(Mockito.anyLong())).thenReturn(Collections.singletonList(permission)); |
| 221 | + assertTrue(apiAccessCheckerSpy.checkAccess(getTestAccount(), allowedApiName)); |
| 222 | + } |
| 223 | + |
| 224 | + @Test(expected = PermissionDeniedException.class) |
| 225 | + public void testCheckAccessAccountDeniedApi() { |
| 226 | + final String deniedApiName = "someDeniedApi"; |
| 227 | + final RolePermission permission = new RolePermissionVO(1L, deniedApiName, Permission.DENY, null); |
| 228 | + Mockito.when(roleServiceMock.findAllPermissionsBy(Mockito.anyLong())).thenReturn(Collections.singletonList(permission)); |
| 229 | + apiAccessCheckerSpy.checkAccess(getTestAccount(), deniedApiName); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void testCheckAccessAccountUsesCachedPermissions() throws Exception { |
| 234 | + // Enable caching by setting a positive cachePeriod |
| 235 | + Field cachePeriodField = DynamicRoleBasedAPIAccessChecker.class.getDeclaredField("cachePeriod"); |
| 236 | + cachePeriodField.setAccessible(true); |
| 237 | + cachePeriodField.set(apiAccessCheckerSpy, 1); |
| 238 | + |
| 239 | + Field rpCacheField = DynamicRoleBasedAPIAccessChecker.class.getDeclaredField("rolePermissionsCache"); |
| 240 | + rpCacheField.setAccessible(true); |
| 241 | + rpCacheField.set(apiAccessCheckerSpy, new LazyCache<Long, Pair<Role, List<RolePermission>>>(32, 1, apiAccessCheckerSpy::getRolePermissions)); |
| 242 | + |
| 243 | + final String allowedApiName = "someAllowedApi"; |
| 244 | + final RolePermission permission = new RolePermissionVO(1L, allowedApiName, Permission.ALLOW, null); |
| 245 | + Mockito.when(roleServiceMock.findAllPermissionsBy(Mockito.anyLong())).thenReturn(Collections.singletonList(permission)); |
| 246 | + |
| 247 | + // First call should populate the cache |
| 248 | + apiAccessCheckerSpy.checkAccess(getTestAccount(), allowedApiName); |
| 249 | + // Second call should use cached permissions and not hit the DAO again |
| 250 | + apiAccessCheckerSpy.checkAccess(getTestAccount(), allowedApiName); |
| 251 | + |
| 252 | + Mockito.verify(roleServiceMock, Mockito.times(1)).findAllPermissionsBy(Mockito.anyLong()); |
| 253 | + } |
| 254 | + |
| 255 | + // --- Tests for getApisAllowedToAccount --- |
| 256 | + |
| 257 | + @Test |
| 258 | + public void testGetApisAllowedToAccountDisabledShouldReturnAll() { |
| 259 | + Mockito.doReturn(false).when(apiAccessCheckerSpy).isEnabled(); |
| 260 | + List<String> input = new ArrayList<>(Arrays.asList("api1", "api2", "api3")); |
| 261 | + List<String> result = apiAccessCheckerSpy.getApisAllowedToAccount(getTestAccount(), input); |
| 262 | + Assert.assertEquals(3, result.size()); |
| 263 | + } |
| 264 | + |
| 265 | + @Test(expected = PermissionDeniedException.class) |
| 266 | + public void testGetApisAllowedToAccountNullRoleShouldThrow() { |
| 267 | + Mockito.when(roleServiceMock.findRole(Mockito.anyLong())).thenReturn(null); |
| 268 | + apiAccessCheckerSpy.getApisAllowedToAccount(getTestAccount(), new ArrayList<>(Arrays.asList("api1"))); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void testGetApisAllowedToAccountAdminShouldReturnAll() { |
| 273 | + Account adminAccount = new AccountVO("root admin", 1L, null, Account.Type.ADMIN, "admin-uuid"); |
| 274 | + Mockito.when(roleServiceMock.findRole(Mockito.anyLong())).thenReturn(new RoleVO(1L, "Admin", RoleType.Admin, "default admin role")); |
| 275 | + List<String> input = new ArrayList<>(Arrays.asList("api1", "api2", "api3")); |
| 276 | + List<String> result = apiAccessCheckerSpy.getApisAllowedToAccount(adminAccount, input); |
| 277 | + Assert.assertEquals(3, result.size()); |
| 278 | + Assert.assertEquals(input, result); |
| 279 | + } |
| 280 | + |
| 281 | + @Test |
| 282 | + public void testGetApisAllowedToAccountFiltersCorrectly() { |
| 283 | + final RolePermission allowPermission = new RolePermissionVO(1L, "allowedApi", Permission.ALLOW, null); |
| 284 | + final RolePermission denyPermission = new RolePermissionVO(1L, "deniedApi", Permission.DENY, null); |
| 285 | + Mockito.when(roleServiceMock.findAllPermissionsBy(Mockito.anyLong())).thenReturn(Arrays.asList(allowPermission, denyPermission)); |
| 286 | + List<String> input = new ArrayList<>(Arrays.asList("allowedApi", "deniedApi", "unknownApi")); |
| 287 | + List<String> result = apiAccessCheckerSpy.getApisAllowedToAccount(getTestAccount(), input); |
| 288 | + Assert.assertEquals(1, result.size()); |
| 289 | + Assert.assertEquals("allowedApi", result.get(0)); |
| 290 | + } |
| 291 | + |
| 292 | + @Test |
| 293 | + public void testGetApisAllowedToAccountAnnotationFallback() { |
| 294 | + Mockito.when(roleServiceMock.findAllPermissionsBy(Mockito.anyLong())).thenReturn(Collections.emptyList()); |
| 295 | + apiAccessCheckerSpy.addApiToRoleBasedAnnotationsMap(RoleType.User, "annotatedApi"); |
| 296 | + List<String> input = new ArrayList<>(Arrays.asList("annotatedApi", "unknownApi")); |
| 297 | + List<String> result = apiAccessCheckerSpy.getApisAllowedToAccount(getTestAccount(), input); |
| 298 | + Assert.assertEquals(1, result.size()); |
| 299 | + Assert.assertEquals("annotatedApi", result.get(0)); |
| 300 | + } |
| 301 | + |
| 302 | + @Test |
| 303 | + public void testGetApisAllowedToAccountUsesCachedPermissions() { |
| 304 | + try { |
| 305 | + // Ensure caching is enabled by setting a positive cachePeriod |
| 306 | + Field cachePeriodField = DynamicRoleBasedAPIAccessChecker.class.getDeclaredField("cachePeriod"); |
| 307 | + cachePeriodField.setAccessible(true); |
| 308 | + cachePeriodField.set(apiAccessCheckerSpy, 1); |
| 309 | + |
| 310 | + Field rpCacheField = DynamicRoleBasedAPIAccessChecker.class.getDeclaredField("rolePermissionsCache"); |
| 311 | + rpCacheField.setAccessible(true); |
| 312 | + rpCacheField.set(apiAccessCheckerSpy, new LazyCache<Long, Pair<Role, List<RolePermission>>>(32, 1, apiAccessCheckerSpy::getRolePermissions)); |
| 313 | + |
| 314 | + final RolePermission permission = new RolePermissionVO(1L, "api1", Permission.ALLOW, null); |
| 315 | + Mockito.when(roleServiceMock.findAllPermissionsBy(Mockito.anyLong())).thenReturn(Collections.singletonList(permission)); |
| 316 | + |
| 317 | + Account account = getTestAccount(); |
| 318 | + List<String> apis = new ArrayList<>(Arrays.asList("api1")); |
| 319 | + |
| 320 | + // First call should load permissions from the DAO and populate the cache |
| 321 | + apiAccessCheckerSpy.getApisAllowedToAccount(account, apis); |
| 322 | + // Second call should use cached permissions and not hit the DAO again |
| 323 | + apiAccessCheckerSpy.getApisAllowedToAccount(account, apis); |
| 324 | + |
| 325 | + Mockito.verify(roleServiceMock, Mockito.times(1)).findAllPermissionsBy(Mockito.anyLong()); |
| 326 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 327 | + Assert.fail("Failed to set cachePeriod for test: " + e.getMessage()); |
| 328 | + } |
| 329 | + } |
198 | 330 | } |
0 commit comments