|
1 |
| -from unittest.mock import MagicMock |
| 1 | +from unittest.mock import MagicMock, patch |
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | from django.apps import apps
|
5 | 5 | from django.test.utils import override_settings
|
6 | 6 |
|
7 | 7 | from ansible_base.rbac.models import ObjectRole, RoleEvaluation, RoleTeamAssignment, RoleUserAssignment
|
8 | 8 | from ansible_base.rbac.permission_registry import permission_registry
|
9 |
| -from ansible_base.rbac.triggers import dab_post_migrate, post_migration_rbac_setup |
| 9 | +from ansible_base.rbac.triggers import bulk_rbac_caching, dab_post_migrate, post_migration_rbac_setup |
10 | 10 | from test_app.models import Inventory, Organization
|
11 | 11 |
|
12 | 12 |
|
@@ -172,3 +172,177 @@ def test_delete_signals_team_organization(organization, inventory, team, org_inv
|
172 | 172 | assert not RoleEvaluation.objects.filter(**org_gfk).exists()
|
173 | 173 |
|
174 | 174 | assert not RoleEvaluation.objects.filter(**inv_gfk).exists()
|
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.django_db |
| 178 | +class TestBulkRBACCaching: |
| 179 | + """Tests for the bulk_rbac_caching context manager""" |
| 180 | + |
| 181 | + def test_bulk_caching_defers_updates(self, rando, inv_rd, inventory): |
| 182 | + """Test that updates are deferred during bulk operations""" |
| 183 | + with ( |
| 184 | + patch('ansible_base.rbac.triggers.compute_team_member_roles') as mock_team_update, |
| 185 | + patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update, |
| 186 | + ): |
| 187 | + |
| 188 | + with bulk_rbac_caching(): |
| 189 | + # Multiple permission assignments |
| 190 | + inv_rd.give_permission(rando, inventory) |
| 191 | + # During bulk mode, the expensive cache functions should not be called |
| 192 | + mock_team_update.assert_not_called() |
| 193 | + mock_obj_update.assert_not_called() |
| 194 | + |
| 195 | + # After exiting context, they should be called once |
| 196 | + mock_obj_update.assert_called_once() |
| 197 | + |
| 198 | + def test_bulk_caching_collects_object_roles(self, rando, team, inv_rd, org_inv_rd, inventory, organization): |
| 199 | + """Test that object roles are properly collected and updated in bulk""" |
| 200 | + with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update: |
| 201 | + |
| 202 | + with bulk_rbac_caching(): |
| 203 | + # Multiple assignments that affect different object roles |
| 204 | + assignment1 = inv_rd.give_permission(rando, inventory) |
| 205 | + assignment2 = org_inv_rd.give_permission(team, organization) |
| 206 | + |
| 207 | + # Should not be called during bulk mode |
| 208 | + mock_obj_update.assert_not_called() |
| 209 | + |
| 210 | + # Should be called once with collected object roles |
| 211 | + mock_obj_update.assert_called_once() |
| 212 | + call_args = mock_obj_update.call_args |
| 213 | + object_roles = call_args.kwargs['object_roles'] |
| 214 | + |
| 215 | + # Should contain both object roles |
| 216 | + assert assignment1.object_role in object_roles |
| 217 | + assert assignment2.object_role in object_roles |
| 218 | + |
| 219 | + def test_bulk_caching_handles_team_updates(self, rando, team, member_rd): |
| 220 | + """Test that team updates are properly deferred and executed""" |
| 221 | + with ( |
| 222 | + patch('ansible_base.rbac.triggers.compute_team_member_roles') as mock_team_update, |
| 223 | + patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update, |
| 224 | + ): |
| 225 | + |
| 226 | + with bulk_rbac_caching(): |
| 227 | + # Assignment that affects team membership |
| 228 | + member_rd.give_permission(rando, team) |
| 229 | + |
| 230 | + # Should not be called during bulk mode |
| 231 | + mock_team_update.assert_not_called() |
| 232 | + mock_obj_update.assert_not_called() |
| 233 | + |
| 234 | + # Both should be called after exiting context |
| 235 | + mock_team_update.assert_called_once() |
| 236 | + mock_obj_update.assert_called_once() |
| 237 | + |
| 238 | + def test_bulk_caching_nested_contexts(self, rando, inv_rd, inventory): |
| 239 | + """Test that nested bulk contexts work correctly""" |
| 240 | + with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update: |
| 241 | + |
| 242 | + with bulk_rbac_caching(): |
| 243 | + inv_rd.give_permission(rando, inventory) |
| 244 | + |
| 245 | + # Nested context |
| 246 | + with bulk_rbac_caching(): |
| 247 | + # Another assignment in nested context |
| 248 | + # Should still not trigger updates |
| 249 | + pass |
| 250 | + |
| 251 | + # Still in outer context, should not be called yet |
| 252 | + mock_obj_update.assert_not_called() |
| 253 | + |
| 254 | + # Only called once when exiting outermost context |
| 255 | + mock_obj_update.assert_called_once() |
| 256 | + |
| 257 | + def test_bulk_caching_with_multiple_assignments(self, rando, team, inv_rd, org_inv_rd, inventory, organization): |
| 258 | + """Test bulk caching works with multiple assignments that require updates""" |
| 259 | + with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update: |
| 260 | + |
| 261 | + with bulk_rbac_caching(): |
| 262 | + # Multiple operations that create new object roles |
| 263 | + inv_rd.give_permission(rando, inventory) |
| 264 | + org_inv_rd.give_permission(team, organization) |
| 265 | + mock_obj_update.assert_not_called() |
| 266 | + |
| 267 | + # Should be called once after all operations |
| 268 | + mock_obj_update.assert_called_once() |
| 269 | + |
| 270 | + def test_bulk_caching_with_object_role_deletion(self, rando, inv_rd, inventory): |
| 271 | + """Test bulk caching when object role gets deleted during removal""" |
| 272 | + # First give permission normally |
| 273 | + inv_rd.give_permission(rando, inventory) |
| 274 | + |
| 275 | + with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update: |
| 276 | + |
| 277 | + with bulk_rbac_caching(): |
| 278 | + # Remove permission in bulk mode - this will delete the object role |
| 279 | + inv_rd.remove_permission(rando, inventory) |
| 280 | + mock_obj_update.assert_not_called() |
| 281 | + |
| 282 | + # Should not be called since object role was deleted (nothing to update) |
| 283 | + mock_obj_update.assert_not_called() |
| 284 | + |
| 285 | + def test_bulk_caching_performance_benefit(self, rando, team, inv_rd, inventory): |
| 286 | + """Test that bulk operations actually reduce cache update calls""" |
| 287 | + with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update: |
| 288 | + |
| 289 | + # Without bulk caching - each assignment triggers an update |
| 290 | + inv_rd.give_permission(rando, inventory) |
| 291 | + inv_rd.remove_permission(rando, inventory) |
| 292 | + |
| 293 | + # Should have been called multiple times |
| 294 | + assert mock_obj_update.call_count >= 2 |
| 295 | + |
| 296 | + mock_obj_update.reset_mock() |
| 297 | + |
| 298 | + # With bulk caching - should only be called once |
| 299 | + with bulk_rbac_caching(): |
| 300 | + inv_rd.give_permission(rando, inventory) |
| 301 | + inv_rd.give_permission(team, inventory) |
| 302 | + inv_rd.remove_permission(rando, inventory) |
| 303 | + |
| 304 | + # Should only be called once |
| 305 | + mock_obj_update.assert_called_once() |
| 306 | + |
| 307 | + def test_bulk_caching_exception_handling(self, rando, inv_rd, inventory): |
| 308 | + """Test that bulk caching state is reset even if exception occurs""" |
| 309 | + with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update: |
| 310 | + |
| 311 | + try: |
| 312 | + with bulk_rbac_caching(): |
| 313 | + inv_rd.give_permission(rando, inventory) |
| 314 | + raise ValueError("Test exception") |
| 315 | + except ValueError: |
| 316 | + pass |
| 317 | + |
| 318 | + # Should still be called even though exception occurred |
| 319 | + mock_obj_update.assert_called_once() |
| 320 | + |
| 321 | + mock_obj_update.reset_mock() |
| 322 | + |
| 323 | + # State should be reset, next operation should work normally |
| 324 | + inv_rd.give_permission(rando, inventory) |
| 325 | + mock_obj_update.assert_called() |
| 326 | + |
| 327 | + def test_no_bulk_caching_without_context(self, rando, inv_rd, inventory): |
| 328 | + """Test that normal operations still work when not in bulk context""" |
| 329 | + with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update: |
| 330 | + |
| 331 | + # Normal assignment should trigger immediate update |
| 332 | + inv_rd.give_permission(rando, inventory) |
| 333 | + mock_obj_update.assert_called() |
| 334 | + |
| 335 | + def test_bulk_caching_empty_context(self): |
| 336 | + """Test that empty bulk context doesn't call cache functions""" |
| 337 | + with ( |
| 338 | + patch('ansible_base.rbac.triggers.compute_team_member_roles') as mock_team_update, |
| 339 | + patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update, |
| 340 | + ): |
| 341 | + |
| 342 | + with bulk_rbac_caching(): |
| 343 | + # No operations performed |
| 344 | + pass |
| 345 | + |
| 346 | + # Should not call expensive functions if no changes were made |
| 347 | + mock_team_update.assert_not_called() |
| 348 | + mock_obj_update.assert_not_called() |
0 commit comments