Skip to content

Commit

Permalink
Fix and test data migration error from DAB RBAC (#15138)
Browse files Browse the repository at this point in the history
* Fix and test data migration error from DAB RBAC

* Fix up migration test

* Fix custom method bug

* Fix another fat fingered bug
  • Loading branch information
AlanCoding authored Apr 24, 2024
1 parent c760577 commit 47a061e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 12 additions & 1 deletion awx/main/migrations/_dab_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ def get_permissions_for_role(role_field, children_map, apps):
return perm_list


def model_class(ct, apps):
"""
You can not use model methods in migrations, so this duplicates
what ContentType.model_class does, using current apps
"""
try:
return apps.get_model(ct.app_label, ct.model)
except LookupError:
return None


def migrate_to_new_rbac(apps, schema_editor):
"""
This method moves the assigned permissions from the old rbac.py models
Expand Down Expand Up @@ -197,7 +208,7 @@ def migrate_to_new_rbac(apps, schema_editor):
role_definition = managed_definitions[permissions]
else:
action = role.role_field.rsplit('_', 1)[0] # remove the _field ending of the name
role_definition_name = f'{role.content_type.model_class().__name__} {action.title()}'
role_definition_name = f'{model_class(role.content_type, apps).__name__} {action.title()}'

description = role_descriptions[role.role_field]
if type(description) == dict:
Expand Down
17 changes: 17 additions & 0 deletions awx/main/tests/functional/test_migrations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from django_test_migrations.plan import all_migrations, nodes_to_tuples
from django.utils.timezone import now

"""
Most tests that live in here can probably be deleted at some point. They are mainly
Expand Down Expand Up @@ -68,3 +69,19 @@ def test_receptor_address(self, migrator):
bar_peers = bar.peers.all()
assert len(bar_peers) == 1
assert fooaddr in bar_peers

def test_migrate_DAB_RBAC(self, migrator):
old_state = migrator.apply_initial_migration(('main', '0190_alter_inventorysource_source_and_more'))
Organization = old_state.apps.get_model('main', 'Organization')
User = old_state.apps.get_model('auth', 'User')

org = Organization.objects.create(name='arbitrary-org', created=now(), modified=now())
user = User.objects.create(username='random-user')
org.read_role.members.add(user)

new_state = migrator.apply_tested_migration(
('main', '0192_custom_roles'),
)

RoleUserAssignment = new_state.apps.get_model('dab_rbac', 'RoleUserAssignment')
assert RoleUserAssignment.objects.filter(user=user.id, object_id=org.id).exists()

0 comments on commit 47a061e

Please sign in to comment.