0

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!

eeintech
  • 111
  • 5
  • 1
    as per the doc of [**`has_perms(perm_list, obj=None)`**](https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.has_perms) there is no limit for the ***permissions*** to be checked. So, I suspect, the user doesn't have sufficient permissions. – JPG Oct 06 '20 at 03:31
  • 1
    I recommend to check whether the ***requested use*** has sufficient permissions by this way, [How to get user permissions in Django?](https://stackoverflow.com/questions/16573174/how-to-get-user-permissions) – JPG Oct 06 '20 at 03:33
  • 1
    Thanks @ArakkalAbu, I actually made a false assumption that any permission in the set should match user's permissions to be allowed... I was a little tired and didn't realized ALL permissions required need to match the user. So you are right and thanks for your comment! – eeintech Oct 06 '20 at 12:55
  • I am glad that helped !!! @eeintech – JPG Oct 06 '20 at 12:59

0 Answers0