diff --git a/awx/main/models/__init__.py b/awx/main/models/__init__.py index b962456e48..d8b206e18a 100644 --- a/awx/main/models/__init__.py +++ b/awx/main/models/__init__.py @@ -76,7 +76,8 @@ User.add_to_class('auditor_of_organizations', user_get_auditor_of_organizations) @property def user_is_system_auditor(user): if not hasattr(user, '_is_system_auditor'): - user._is_system_auditor = Role.objects.filter(role_field='system_auditor', id=user.id).exists() + user._is_system_auditor = user.roles.filter( + singleton_name='system_auditor', role_field='system_auditor').exists() return user._is_system_auditor diff --git a/awx/main/tests/functional/conftest.py b/awx/main/tests/functional/conftest.py index 05f1941fab..165dfed0d6 100644 --- a/awx/main/tests/functional/conftest.py +++ b/awx/main/tests/functional/conftest.py @@ -41,7 +41,7 @@ from awx.main.models.organization import ( Permission, Team, ) - +from awx.main.models.rbac import Role from awx.main.models.notifications import ( NotificationTemplate, Notification @@ -262,6 +262,13 @@ def admin(user): return user('admin', True) +@pytest.fixture +def system_auditor(user): + u = user(False) + Role.singleton('system_auditor').members.add(u) + return u + + @pytest.fixture def alice(user): return user('alice', False) diff --git a/awx/main/tests/functional/test_rbac_user.py b/awx/main/tests/functional/test_rbac_user.py index 0b43ce2f1c..c7eaa8c0e9 100644 --- a/awx/main/tests/functional/test_rbac_user.py +++ b/awx/main/tests/functional/test_rbac_user.py @@ -9,7 +9,7 @@ from awx.main.models import Role, User, Organization, Inventory @pytest.mark.django_db -class TestSysAuditor(TransactionTestCase): +class TestSysAuditorTransactional(TransactionTestCase): def rando(self): return User.objects.create(username='rando', password='rando', email='rando@com.com') @@ -41,6 +41,10 @@ class TestSysAuditor(TransactionTestCase): assert not rando.is_system_auditor +@pytest.mark.django_db +def test_system_auditor_is_system_auditor(system_auditor): + assert system_auditor.is_system_auditor + @pytest.mark.django_db def test_user_admin(user_project, project, user):