Fix and test data migration error from DAB RBAC (#15138)

* Fix and test data migration error from DAB RBAC

* Fix up migration test

* Fix custom method bug

* Fix another fat fingered bug
This commit is contained in:
Alan Rominger
2024-04-24 15:14:03 -04:00
committed by GitHub
parent c760577855
commit 47a061eb39
2 changed files with 29 additions and 1 deletions

View File

@@ -140,6 +140,17 @@ def get_permissions_for_role(role_field, children_map, apps):
return perm_list 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): def migrate_to_new_rbac(apps, schema_editor):
""" """
This method moves the assigned permissions from the old rbac.py models This method moves the assigned permissions from the old rbac.py models
@@ -197,7 +208,7 @@ def migrate_to_new_rbac(apps, schema_editor):
role_definition = managed_definitions[permissions] role_definition = managed_definitions[permissions]
else: else:
action = role.role_field.rsplit('_', 1)[0] # remove the _field ending of the name 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] description = role_descriptions[role.role_field]
if type(description) == dict: if type(description) == dict:

View File

@@ -1,6 +1,7 @@
import pytest import pytest
from django_test_migrations.plan import all_migrations, nodes_to_tuples 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 Most tests that live in here can probably be deleted at some point. They are mainly
@@ -68,3 +69,19 @@ class TestMigrationSmoke:
bar_peers = bar.peers.all() bar_peers = bar.peers.all()
assert len(bar_peers) == 1 assert len(bar_peers) == 1
assert fooaddr in bar_peers 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()