|
| 1 | +""" |
| 2 | +Test coverage for RuntimeError raises in sync.py when RBAC is not installed. |
| 3 | +Generated by Claude Sonnet 4.5 |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from django.test import override_settings |
| 8 | + |
| 9 | + |
| 10 | +class TestSyncWithoutRBAC: |
| 11 | + """Test that sync functions raise appropriate errors when rbac is NOT installed.""" |
| 12 | + |
| 13 | + @pytest.fixture(autouse=True) |
| 14 | + def setup_without_rbac(self): |
| 15 | + """Override settings to remove rbac from INSTALLED_APPS for these tests.""" |
| 16 | + # Create a copy of INSTALLED_APPS without rbac |
| 17 | + from django.conf import settings |
| 18 | + |
| 19 | + apps_without_rbac = [app for app in settings.INSTALLED_APPS if 'rbac' not in app] |
| 20 | + |
| 21 | + with override_settings(INSTALLED_APPS=apps_without_rbac): |
| 22 | + yield |
| 23 | + |
| 24 | + def test_get_local_assignments_raises_error(self): |
| 25 | + """Test that get_local_assignments raises error when rbac not available.""" |
| 26 | + # Force reload to pick up the modified INSTALLED_APPS |
| 27 | + import importlib |
| 28 | + |
| 29 | + from ansible_base.resource_registry.tasks import sync |
| 30 | + |
| 31 | + importlib.reload(sync) |
| 32 | + |
| 33 | + with pytest.raises(RuntimeError, match="get_local_assignments requires ansible_base.rbac to be installed"): |
| 34 | + sync.get_local_assignments() |
| 35 | + |
| 36 | + def test_delete_local_assignment_raises_error(self): |
| 37 | + """Test that delete_local_assignment raises error when rbac not available.""" |
| 38 | + import importlib |
| 39 | + |
| 40 | + from ansible_base.resource_registry.tasks import sync |
| 41 | + |
| 42 | + importlib.reload(sync) |
| 43 | + |
| 44 | + assignment = sync.AssignmentTuple( |
| 45 | + actor_ansible_id="test-id", |
| 46 | + ansible_id_or_pk="obj-id", |
| 47 | + role_definition_name="test-role", |
| 48 | + assignment_type="user", |
| 49 | + ) |
| 50 | + |
| 51 | + with pytest.raises(RuntimeError, match="delete_local_assignment requires ansible_base.rbac to be installed"): |
| 52 | + sync.delete_local_assignment(assignment) |
| 53 | + |
| 54 | + def test_create_local_assignment_raises_error(self): |
| 55 | + """Test that create_local_assignment raises error when rbac not available.""" |
| 56 | + import importlib |
| 57 | + |
| 58 | + from ansible_base.resource_registry.tasks import sync |
| 59 | + |
| 60 | + importlib.reload(sync) |
| 61 | + |
| 62 | + assignment = sync.AssignmentTuple( |
| 63 | + actor_ansible_id="test-id", |
| 64 | + ansible_id_or_pk="obj-id", |
| 65 | + role_definition_name="test-role", |
| 66 | + assignment_type="user", |
| 67 | + ) |
| 68 | + |
| 69 | + with pytest.raises(RuntimeError, match="create_local_assignment requires ansible_base.rbac to be installed"): |
| 70 | + sync.create_local_assignment(assignment) |
| 71 | + |
| 72 | + def test_get_ansible_id_or_pk_raises_error(self): |
| 73 | + """Test that get_ansible_id_or_pk raises error when rbac not available.""" |
| 74 | + import importlib |
| 75 | + |
| 76 | + from ansible_base.resource_registry.tasks import sync |
| 77 | + |
| 78 | + importlib.reload(sync) |
| 79 | + |
| 80 | + # Create a mock assignment object |
| 81 | + class MockAssignment: |
| 82 | + content_type = None |
| 83 | + object_id = None |
| 84 | + |
| 85 | + with pytest.raises(RuntimeError, match="get_ansible_id_or_pk requires ansible_base.rbac to be installed"): |
| 86 | + sync.get_ansible_id_or_pk(MockAssignment()) |
| 87 | + |
| 88 | + def test_get_content_object_raises_error(self): |
| 89 | + """Test that get_content_object raises error when rbac not available.""" |
| 90 | + import importlib |
| 91 | + |
| 92 | + from ansible_base.resource_registry.tasks import sync |
| 93 | + |
| 94 | + importlib.reload(sync) |
| 95 | + |
| 96 | + # Create a mock role_definition |
| 97 | + class MockRoleDefinition: |
| 98 | + class content_type: |
| 99 | + model = 'organization' |
| 100 | + |
| 101 | + assignment = sync.AssignmentTuple( |
| 102 | + actor_ansible_id="test-id", |
| 103 | + ansible_id_or_pk="obj-id", |
| 104 | + role_definition_name="test-role", |
| 105 | + assignment_type="user", |
| 106 | + ) |
| 107 | + |
| 108 | + with pytest.raises(RuntimeError, match="get_content_object requires ansible_base.rbac to be installed"): |
| 109 | + sync.get_content_object(MockRoleDefinition(), assignment) |
0 commit comments