title: test PermissionRequiredMixin raises PermissionDenied instead of 403
body: <p>I am using django 1.10.5 and the PermissionRequiredMixin.<br>
  Instead of redirecting users to the login, I want to return a 403-error if they don't have the right permission.<br>
  In theory, if PermissionDenied is raised anywhere, the response-object should have a status_code of 403:</p>
  
  <blockquote>
    <p>The only exceptions that are not visible to the test client are Http404, PermissionDenied, SystemExit, and SuspiciousOperation. Django catches these exceptions internally and converts them into the appropriate HTTP response codes. In these cases, you can check response.status_code in your test.</p>
  </blockquote>
  
  <p>From the documentation: <a href="https://docs.djangoproject.com/en/1.10/topics/testing/tools/#exceptions" rel="noreferrer">https://docs.djangoproject.com/en/1.10/topics/testing/tools/#exceptions</a></p>
  
  <p>However, my tests stop on an exception django.core.exceptions.PermissionDenied
  This is the simplest test.py:</p>
  
  <pre><code>from django.test import TestCase, RequestFactory
  from doingTests.views import ViewWithPermission
  from django.contrib.auth.models import User
  from django.core.exceptions import PermissionDenied
  
  class TestPermission(TestCase):
      def test_permission_view(self):
          user = User.objects.create(username="john", password="doh")
          permView = ViewWithPermission.as_view()
          rf = RequestFactory()
          request = rf.get('/test/')
          request.user = user
          self.assertEqual(permView(request).status_code, 403)
  </code></pre>
  
  <p>With this view.py:</p>
  
  <pre><code>from django.contrib.auth.mixins import PermissionRequiredMixin
  from django.views.generic.base import TemplateView
  
  class ViewWithPermission(PermissionRequiredMixin, TemplateView):
      permission_required="doingTests.add_mock"
      raise_exception=True
  </code></pre>
  
  <p>What permission is required is actually not important, random strings, custom permissions and builtin permissions all raise PermissionDenied.</p>
  
  <p>Since there are so many variables here, I might just have missed something, e.g. permission_required needs a permission-object instead of a string (tried that).</p>
  
  <hr>
  
  <p>I missunderstood the following, correction below:</p>
  
  <p>But I'm starting to think this might be a bug, because testing for PermissionDenied Exception still raises an exception instead of passing the test!<br>
  Chaning the last line in test.py to:</p>
  
  <pre><code>self.assertRaises(PermissionDenied, permView(request))
  </code></pre>
  
  <hr>
  
  <p>It all works fine if handled correctly, which requires the with-statement:</p>
  
  <pre><code>with self.assertRaises(PermissionDenied):
      permView(request)
  </code></pre>
  
  <p>Doesn't raise exception, test passes. I would still like to test the error-code though.</p>