【问题标题】:Django unittest handle PermissionDeniedDjango 单元测试句柄 PermissionDenied
【发布时间】:2021-05-08 07:02:31
【问题描述】:

正如文档所说 https://docs.djangoproject.com/en/2.2/topics/testing/tools/#exceptions;

测试客户端不可见的唯一例外是Http404PermissionDeniedSystemExitSuspiciousOperation。 Django 在内部捕获这些异常并将它们转换为适当的 HTTP 响应代码。在这些情况下,您可以在测试中检查response.status_code

我处理了一个自定义登录混合,用于向已登录但无权访问该页面的用户引发错误。

class LoginQuizMarkerMixin(LoginRequiredMixin):

    def dispatch(self, *args, **kwargs):
        if self.request.user.is_authenticated:
            if not self.request.user.has_perm('quizzes.view_sittings'):
                raise PermissionDenied("You don't have any access to this page.")
        return super().dispatch(*args, **kwargs)

我的团结,它工作正常。

class TestQuestionMarking(TestCase):

    def setUp(self):
        self.c1 = Category.objects.create_category(name='elderberries')
        self.student = User.objects.create_user(username='student',
                                                email='student@rebels.com',
                                                password='top_secret')
        self.teacher = User.objects.create_user(username='teacher',
                                                email='teacher@jedis.com',
                                                password='use_d@_force')
        self.teacher.user_permissions.add(Permission.objects.get(codename='view_sittings'))

        ....


    def test_paper_marking_list_view(self):
        # should be 302 (redirection)
        marking_url = reverse('quizzes:quiz_marking')
        response = self.client.get(marking_url)
        print('response1', response)

        # should be 403 (permission denied)
        self.client.login(username='student', password='top_secret')
        response = self.client.get(marking_url)
        print('response2', response)

        # should be 200 (allowed)
        self.client.login(username='teacher', password='use_d@_force')
        response = self.client.get(marking_url)
        print('response3', response)

但不知何故,PermissionDenied 的错误显示如下错误:

docker-compose -f local.yml run --rm django python manage.py test labs.tests.quizzes.test_views.TestQuestionMarking.test_paper_marking_list_view


response1 <HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/accounts/login/?next=/quizzes/marking/">


WARNING 2021-02-04 00:28:44,270 log 1 139681767683904 [log.py:222] Forbidden (Permission denied): /quizzes/marking/
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/app/my_app/quizzes/views.py", line 37, in dispatch
    raise PermissionDenied("You don't have any access to this page.")
django.core.exceptions.PermissionDenied: You don't have any access to this page.
response2 <HttpResponseForbidden status_code=403, "text/html; charset=utf-8">


response3 <TemplateResponse status_code=200, "text/html; charset=utf-8">

我所期望的仍然被捕获为错误,没有像上面那样显示。应该是这样的:

response1 <HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/accounts/login/?next=/quizzes/marking/">

response2 <HttpResponseForbidden status_code=403, "text/html; charset=utf-8">

response3 <TemplateResponse status_code=200, "text/html; charset=utf-8">

但是,在另一个测试中,当我尝试使用 raise Http404 更改它时,它运行良好:

class LoginQuizMarkerMixin(LoginRequiredMixin):

    def dispatch(self, *args, **kwargs):
        if self.request.user.is_authenticated:
            if not self.request.user.has_perm('quizzes.view_sittings'):
                # raise PermissionDenied("You don't have any access to this page.")
                raise Http404
        return super().dispatch(*args, **kwargs)

控制台输出:

response1 <HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/accounts/login/?next=/quizzes/marking/">
WARNING 2021-02-04 13:39:05,368 log 1 139683990402880 [log.py:222] Not Found: /quizzes/marking/
response2 <HttpResponseNotFound status_code=404, "text/html; charset=utf-8">
response3 <TemplateResponse status_code=200, "text/html; charset=utf-8">

【问题讨论】:

    标签: python-3.x django unit-testing django-2.2


    【解决方案1】:

    我通过在测试模式的设置中禁用我的日志记录来解决此问题。

    docker-compose -f local.yml run --rm django python manage.py test --settings=config.settings.test
    

    在我的config/settings/test.py里面;

    LOGGING = {}
    

    相关问题:How can I disable logging while running unit tests in Python Django?

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2017-10-19
      • 2016-08-16
      • 1970-01-01
      • 2021-04-08
      • 2017-12-19
      相关资源
      最近更新 更多