My test case raises a django.core.exceptions.PermissionDenied
exception even though permission_required
and user permissions seem to be set right.
Here is the test case:
def test_create(self):
""" Test view for creating a new category """
response = self.client.get(reverse('category-create'), {'category': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
Here are the relevant views:
class AjaxMixin(PermissionRequiredMixin):
""" AjaxMixin provides basic functionality for rendering a Django form to JSON. """
# By default, allow *any* permissions
permission_required = '*'
def has_permission(self):
""" Override the default behaviour of has_permission from PermissionRequiredMixin. """
if self.permission_required == '*':
return True
else:
return super().has_permission()
class CategoryCreate(AjaxMixin, CreateView):
""" Create view to make a new PartCategory """
# The problem is with 5 or more permissions, TestCase fails... no idea why!
permission_required = ('part.add_part',
'part.add_bomitem',
'part.add_partcategory',
'part.add_partattachment',
'part.add_partsellpricebreak',
# 'part.add_parttesttemplate',
# 'part.add_partparametertemplate',
# 'part.add_partparameter'
)
If I comment 'part.add_partsellpricebreak',
in the CategoryCreate
view, the get request response code is 200 and no exception is raised by the test case.
If I re-enable it, the test case breaks as stated above.
The CategoryCreate
view works exactly as intended though the project's UI (eg. permissions are well managed, independently of the size of permission_required
).
Any leads why is the test case failing when permission_required
has a size of 5 or more?
Any idea how to troubleshoot this issue?
Any help would be greatly appreciated!