-
Notifications
You must be signed in to change notification settings - Fork 3.6k
AAP-52518: Fix Role User Assignment Cleanup on Inventory Deletion #16090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
awx/main/tests/functional/dab_rbac/test_inventory_role_assignments.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Copyright (c) 2024 Ansible, Inc. | ||
| # All Rights Reserved. | ||
|
|
||
| import pytest | ||
|
|
||
| from awx.main.models import Inventory, User | ||
| from awx.main.tasks.system import delete_inventory | ||
| from ansible_base.rbac.models import RoleDefinition, RoleUserAssignment | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_inventory_role_assignments_deleted_sync(inventory, alice, setup_managed_roles): | ||
| """Test role assignments are properly cleaned up when inventory is deleted synchronously.""" | ||
| # Get the managed Inventory Admin role definition | ||
| inv_rd = RoleDefinition.objects.get(name='Inventory Admin') | ||
|
|
||
| # Create a role assignment for the inventory | ||
| inv_rd.give_permission(alice, inventory) | ||
|
|
||
| # Verify assignment exists | ||
| assert RoleUserAssignment.objects.filter(user=alice, object_id=str(inventory.pk), role_definition=inv_rd).exists() | ||
|
|
||
| inventory_id = str(inventory.pk) | ||
|
|
||
| # Delete inventory synchronously | ||
| inventory.delete() | ||
|
|
||
| # Verify role assignment is cleaned up | ||
| assert not RoleUserAssignment.objects.filter(object_id=inventory_id, role_definition=inv_rd).exists() | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_inventory_role_assignments_deleted_async(inventory, alice, setup_managed_roles): | ||
| """Test role assignments are properly cleaned up via schedule_deletion.""" | ||
| from unittest.mock import patch | ||
|
|
||
| # Get the managed Inventory Admin role definition | ||
| inv_rd = RoleDefinition.objects.get(name='Inventory Admin') | ||
|
|
||
| # Create a role assignment for the inventory | ||
| inv_rd.give_permission(alice, inventory) | ||
|
|
||
| inventory_id = inventory.pk | ||
| user_id = alice.pk | ||
|
|
||
| # Verify assignment exists before deletion | ||
| assert RoleUserAssignment.objects.filter(user=alice, object_id=str(inventory_id), role_definition=inv_rd).exists() | ||
|
|
||
| # Mock the WebSocket notification to avoid Redis dependency | ||
| with patch('awx.main.tasks.system.emit_channel_notification'): | ||
| # Call delete_inventory directly (simulating Celery task) | ||
| delete_inventory(inventory_id, user_id) | ||
|
|
||
| # Verify role assignment is cleaned up | ||
| assert not RoleUserAssignment.objects.filter(user_id=user_id, object_id=str(inventory_id), role_definition=inv_rd).exists() | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_multiple_inventory_role_assignments_cleanup(inventory, alice, bob, setup_managed_roles): | ||
| """Test multiple role assignments are cleaned up when inventory is deleted.""" | ||
| # Get managed role definitions | ||
| inv_rd = RoleDefinition.objects.get(name='Inventory Admin') | ||
| use_rd = RoleDefinition.objects.get(name='Inventory Use') | ||
|
|
||
| # Create multiple role assignments | ||
| inv_rd.give_permission(alice, inventory) | ||
| use_rd.give_permission(bob, inventory) | ||
|
|
||
| inventory_id = str(inventory.pk) | ||
|
|
||
| # Verify assignments exist | ||
| assert RoleUserAssignment.objects.filter(object_id=inventory_id).count() == 2 | ||
|
|
||
| # Delete inventory | ||
| inventory.delete() | ||
|
|
||
| # Verify all assignments are cleaned up | ||
| assert RoleUserAssignment.objects.filter(object_id=inventory_id).count() == 0 | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_aap_52518_reproduction(organization, setup_managed_roles): | ||
| """Test the exact scenario described in AAP-52518.""" | ||
| # Create user and inventory per Jira scenario | ||
| user = User.objects.create(username='testuser', email='[email protected]', is_active=True) | ||
| inventory = Inventory.objects.create(name='Test Controller Inventory', organization=organization, description='Test inventory for AAP-52518') | ||
|
|
||
| # Step 3: Create role user assignment for user to become Inventory Admin | ||
| inv_admin_role = RoleDefinition.objects.get(name='Inventory Admin') | ||
| inv_admin_role.give_permission(user, inventory) | ||
|
|
||
| # Verify assignment was created | ||
| assert RoleUserAssignment.objects.filter(user=user, object_id=str(inventory.pk), role_definition=inv_admin_role).exists() | ||
|
|
||
| inventory_id = inventory.pk | ||
| user_id = user.pk | ||
|
|
||
| # Step 4: Delete the inventory | ||
| inventory.delete() | ||
|
|
||
| # Step 5: Verify no orphaned role assignments remain | ||
| assert RoleUserAssignment.objects.filter(user_id=user_id, object_id=str(inventory_id)).count() == 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider this to be redundant with the existing signals.
But for the background task, that makes sense to me.