diff --git a/mayan/apps/acls/classes.py b/mayan/apps/acls/classes.py index 60acb4bbfa..e736faa4db 100644 --- a/mayan/apps/acls/classes.py +++ b/mayan/apps/acls/classes.py @@ -12,6 +12,12 @@ class ModelPermission(object): _inheritances = {} _registry = {} + @classmethod + def deregister(cls, model): + cls._registry.pop(model, None) + # TODO: Find method to revert the add_to_class('acls'...) + # delattr doesn't work. + @classmethod def register(cls, model, permissions): from django.contrib.contenttypes.fields import GenericRelation diff --git a/mayan/apps/acls/tests/mixins.py b/mayan/apps/acls/tests/mixins.py index 7bc0d970c1..bf72fe56f9 100644 --- a/mayan/apps/acls/tests/mixins.py +++ b/mayan/apps/acls/tests/mixins.py @@ -36,10 +36,6 @@ class ACLTestMixin(PermissionTestMixin, RoleTestMixin, TestModelTestMixin): auto_create_test_role = True auto_create_test_object = False - def _create_test_acl(self): - self.test_acl = AccessControlList.objects.create( - content_object=self.test_object, role=self.test_role - ) def setUp(self): super(ACLTestMixin, self).setUp() @@ -49,6 +45,22 @@ class ACLTestMixin(PermissionTestMixin, RoleTestMixin, TestModelTestMixin): if self.auto_create_test_object: self._setup_test_object() + def tearDown(self): + # Deregister the permissions of the ephimeral test models + # this avoids their Content Type from being looked up + # in subsequent tests where they don't exists due to the database + # transaction rollback. + for model in self._test_models: + ModelPermission.deregister(model=model) + self._test_models.remove(model) + + super(ACLTestMixin, self).tearDown() + + def _create_test_acl(self): + self.test_acl = AccessControlList.objects.create( + content_object=self.test_object, role=self.test_role + ) + def _inject_test_object_content_type(self): self.test_object_content_type = ContentType.objects.get_for_model( model=self.test_object diff --git a/mayan/apps/acls/tests/test_classes.py b/mayan/apps/acls/tests/test_classes.py index 483f0feb57..f1ab062709 100644 --- a/mayan/apps/acls/tests/test_classes.py +++ b/mayan/apps/acls/tests/test_classes.py @@ -5,7 +5,7 @@ from mayan.apps.common.tests import BaseTestCase from ..classes import ModelPermission -class ModelpermissionTestCase(BaseTestCase): +class ModelPermissionTestCase(BaseTestCase): def test_model_permission_get_classes_as_content_type(self): self.assertNotEqual( ModelPermission.get_classes(as_content_type=True).count(), 0 diff --git a/mayan/apps/common/tests/mixins.py b/mayan/apps/common/tests/mixins.py index 788fcc7a5d..ce1f198bcf 100644 --- a/mayan/apps/common/tests/mixins.py +++ b/mayan/apps/common/tests/mixins.py @@ -283,11 +283,13 @@ class TempfileCheckTestCasekMixin(object): class TestModelTestMixin(object): + _test_models = [] + def _create_test_model(self, fields=None, model_name='TestModel', options=None): if connection.vendor == 'mysql': self.skipTest( - reason='MySQL doesn\t support schema changes inside an ' + reason='MySQL doesn\'t support schema changes inside an ' 'atomic block.' ) @@ -340,6 +342,7 @@ class TestModelTestMixin(object): ) setattr(self, model_name, TestModel) + self._test_models.append(TestModel) with connection.schema_editor() as schema_editor: schema_editor.create_model(model=TestModel)