Detecting permission errors in django tests

I write detailed functional tests for my views to complement unit tests on my models. This is a Django project, and I am using the Django embedded test environment with unittest. Obviously, one of the important things to check for these functional tests is that the permissions are configured correctly. With that in mind, I'm trying something like this:

anonclient = Client()
userclient = Client()
userclient.login(username='test_user', password='test')
adminclient = Client()
adminclient.login(username='test_admin', password='test')

path = "/path/to/my/page"
anonresponse = anonclient.get(path)
userresponse = userclient.get(path)
adminresponse = adminclient.get(path)

I need to be able to confirm that anonclient and userclient were denied access while the admin client was working correctly. Howerver, I can’t find a way to verify that this has happened reliably!

, 302 ( ), , , . ( self.assertRedirects(, url), URL- login_url !)

, user_passes_test, , ? . , , , . , , .

, , ?

!

+5
2

django , , , .

[17] >>> from django.test import Client
[18] >>> c = Client()
[19] >>> c.login(username='superuser', password='bar')
[19] : False
[20] >>> c.login(username='superuser', password='correct_password')
[20] : True
+1

permission_required decorator PermissionRequiredMixin raise_exception.

, - PERMISSIONS_RAISE_EXCEPTION=True settings_test.py (

from django.conf import settings

@permission_required('<perm>',
                     raise_exception=settings.PERMISSIONS_RAISE_EXCEPTION)
def your_view(...)
    pass

class YourView(PermissionRequired):
    raise_exception = settings.PERMISSIONS_RAISE_EXCEPTION

, / 403 ​​ .

client.login('user', 'blah')
response = client.get('/yourview')
assertNot(403, response.status_code)

permission_required , , , .

+1

All Articles