Skip to content

Commit b6c03d4

Browse files
authored
AAP-48392 Fix test failures due to uptream DAB ContentType changes (#1355)
This is another version of #1353, but with the dependency changes reverted. That linked PR (draft) shows that this is effective. I expect tests will pass here (which current DAB) as well. But that PR shows that, if we merge this, it will make downstream tests for ansible/django-ansible-base#749 will pass. ---- Technical summary: There are 2 structural changes in DAB RBAC that require adjustment by the app: - the `post_migrate` signal will now run _once_ as opposed to run _for every app_ and to do that, it can only run when the post migrate signal is called for its own app (the "dab_rbac" app), but doing this mucks with the assumptions around what order post_migrate methods run in, so this often requires other post-migrate methods to call the methods to create DAB types and permissions to resolve the ordering problem - A DAB RBAC-specific content type app is introduced, and this is clearly not the same as the proper `ContentType` model, and this will error any queries that pass a content type object as a python object. To do that, we'll just use the variable from DAB RBAC for the content type, which will give the correct model for whatever version of DAB we are using.
1 parent 90e0e48 commit b6c03d4

File tree

6 files changed

+47
-17
lines changed

6 files changed

+47
-17
lines changed

src/aap_eda/core/management/commands/create_initial_data.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
from ansible_base.rbac import permission_registry
1919
from ansible_base.rbac.models import DABPermission, RoleDefinition
20+
from django.apps import apps
2021
from django.conf import settings
21-
from django.contrib.contenttypes.models import ContentType
2222
from django.core.exceptions import ImproperlyConfigured
2323
from django.core.management import BaseCommand
2424
from django.db import transaction
@@ -1291,6 +1291,15 @@ def handle(self, *args, **options):
12911291
self._remove_deprecated_credential_kinds()
12921292
enable_redis_prefix()
12931293

1294+
@property
1295+
def content_type_model(self):
1296+
try:
1297+
# DAB RBAC migrated to a custom type model, try to use that here
1298+
return apps.get_model("dab_rbac", "DABContentType")
1299+
except LookupError:
1300+
# Fallback for older version of DAB, which just used ContentType
1301+
return apps.get_model("contenttypes", "ContentType")
1302+
12941303
def _remove_deprecated_credential_kinds(self):
12951304
"""Remove old credential types which are deprecated."""
12961305
for credential_type in models.CredentialType.objects.filter(
@@ -1356,7 +1365,7 @@ def _read_file(self, name: str, key: str):
13561365
return f.read()
13571366

13581367
def _create_org_roles(self):
1359-
org_ct = ContentType.objects.get(model="organization")
1368+
org_ct = self.content_type_model.objects.get(model="organization")
13601369
created = updated = 0
13611370
for role_data in ORG_ROLES:
13621371
data = {
@@ -1426,7 +1435,7 @@ def _create_permissions_for_content_type(self, ct=None) -> list:
14261435

14271436
def _create_obj_roles(self):
14281437
for cls in permission_registry.all_registered_models:
1429-
ct = ContentType.objects.get_for_model(cls)
1438+
ct = self.content_type_model.objects.get_for_model(cls)
14301439
parent_model = permission_registry.get_parent_model(cls)
14311440
# ignore if the model is organization, covered by org roles
14321441
# or child model, inherits permissions from parent model
@@ -1441,7 +1450,9 @@ def _create_obj_roles(self):
14411450
child_models = permission_registry.get_child_models(cls)
14421451
child_names = []
14431452
for _, child_model in child_models:
1444-
child_ct = ContentType.objects.get_for_model(child_model)
1453+
child_ct = self.content_type_model.objects.get_for_model(
1454+
child_model
1455+
)
14451456
permissions.extend(
14461457
self._create_permissions_for_content_type(child_ct)
14471458
)
@@ -1500,7 +1511,7 @@ def _create_obj_roles(self):
15001511
name=f"Organization {cls._meta.verbose_name.title()} Admin", # noqa: E501
15011512
defaults={
15021513
"description": f"Has all permissions to {cls._meta.verbose_name}s within an organization", # noqa: E501
1503-
"content_type": ContentType.objects.get(
1514+
"content_type": self.content_type_model.objects.get(
15041515
model="organization"
15051516
),
15061517
"managed": True,

tests/integration/api/test_user.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from typing import Any, Dict
1616

1717
import pytest
18+
from ansible_base.rbac import permission_registry
1819
from ansible_base.rbac.models import DABPermission, RoleDefinition
19-
from django.contrib.contenttypes.models import ContentType
2020
from rest_framework import status
2121
from rest_framework.reverse import reverse
2222
from rest_framework.test import APIClient
@@ -49,7 +49,9 @@ def org_admin_rd():
4949
"view_organization",
5050
"delete_organization",
5151
],
52-
content_type=ContentType.objects.get_for_model(models.Organization),
52+
content_type=permission_registry.content_type_model.objects.get_for_model( # noqa: E501
53+
models.Organization
54+
),
5355
managed=True, # custom roles can not include these permissions
5456
)
5557

@@ -62,7 +64,9 @@ def org_member_rd():
6264
"member_organization",
6365
"view_organization",
6466
],
65-
content_type=ContentType.objects.get_for_model(models.Organization),
67+
content_type=permission_registry.content_type_model.objects.get_for_model( # noqa: E501
68+
models.Organization
69+
),
6670
managed=True,
6771
)
6872

@@ -584,7 +588,9 @@ def test_resources_remain_after_user_delete(
584588
# Give default user permission to create resources
585589
admin_role = RoleDefinition.objects.create(
586590
name="Elevated User",
587-
content_type=ContentType.objects.get_for_model(default_organization),
591+
content_type=permission_registry.content_type_model.objects.get_for_model( # noqa: E501
592+
default_organization
593+
),
588594
)
589595
admin_role.permissions.add(*DABPermission.objects.all())
590596
admin_role.give_permission(default_user, default_organization)

tests/integration/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
from unittest.mock import MagicMock, create_autospec
2020

2121
import pytest
22+
from ansible_base.rbac import permission_registry
2223
from ansible_base.rbac.models import DABPermission, RoleDefinition
2324
from django.conf import settings
2425
from django.contrib.auth.models import AnonymousUser
25-
from django.contrib.contenttypes.models import ContentType
2626
from django.test import override_settings
2727
from rest_framework.test import APIClient
2828

@@ -92,7 +92,9 @@ def admin_user(default_organization, admin_info):
9292
)
9393
admin_role = RoleDefinition.objects.create(
9494
name="Test Admin",
95-
content_type=ContentType.objects.get_for_model(default_organization),
95+
content_type=permission_registry.content_type_model.objects.get_for_model( # noqa: E501
96+
default_organization
97+
),
9698
)
9799
admin_role.permissions.add(*DABPermission.objects.all())
98100
admin_role.give_permission(user, default_organization)

tests/integration/dab_rbac/conftest.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
import pytest
1515
from ansible_base.rbac.models import DABPermission, RoleDefinition
16-
from django.contrib.contenttypes.models import ContentType
16+
from django.apps import apps
1717
from django.db.models import ForeignKey
1818
from django.forms.models import model_to_dict
1919
from rest_framework.test import APIClient
@@ -91,7 +91,15 @@ def _rf(target_user, obj, action):
9191
this creates a role definition with that permission
9292
then it gives the specified user to the specified object
9393
"""
94-
ct = ContentType.objects.get_for_model(obj)
94+
95+
try:
96+
# DAB RBAC migrated to a custom type model, try to use that here
97+
ct_model = apps.get_model("dab_rbac", "DABContentType")
98+
except LookupError:
99+
# Fallback for older version of DAB, which just used ContentType
100+
ct_model = apps.get_model("contenttypes", "ContentType")
101+
102+
ct = ct_model.objects.get_for_model(obj)
95103
rd, _ = RoleDefinition.objects.get_or_create(
96104
name=f"{obj._meta.model_name}-{action}", content_type=ct
97105
)

tests/integration/dab_rbac/test_crud_permissions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import pytest
1616
from ansible_base.rbac import permission_registry
1717
from ansible_base.rbac.models import DABPermission, RoleDefinition
18-
from django.contrib.contenttypes.models import ContentType
1918
from django.test import override_settings
2019
from django.urls.exceptions import NoReverseMatch
2120
from rest_framework.reverse import reverse
@@ -71,7 +70,9 @@ def test_add_permissions(
7170
)
7271
add_rd = RoleDefinition.objects.create(
7372
name=f"add-{model._meta.model_name}",
74-
content_type=ContentType.objects.get_for_model(parent_obj),
73+
content_type=permission_registry.content_type_model.objects.get_for_model( # noqa: E501
74+
parent_obj
75+
),
7576
)
7677
add_rd.permissions.add(
7778
DABPermission.objects.get(codename=f"add_{model._meta.model_name}")

tests/integration/dab_rbac/test_role_permissions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414

1515
import pytest
16+
from ansible_base.rbac import permission_registry
1617
from ansible_base.rbac.models import RoleDefinition, RoleUserAssignment
17-
from django.contrib.contenttypes.models import ContentType
1818
from rest_framework.reverse import reverse
1919

2020
from aap_eda.core import models
@@ -24,7 +24,9 @@
2424
def view_activation_rd():
2525
return RoleDefinition.objects.create_from_permissions(
2626
name="view_act",
27-
content_type=ContentType.objects.get_for_model(models.Activation),
27+
content_type=permission_registry.content_type_model.objects.get_for_model( # noqa: E501
28+
models.Activation
29+
),
2830
permissions=["view_activation"],
2931
)
3032

0 commit comments

Comments
 (0)