Rename superuser_or_403 to superuser_required
1 parent 14da5de commit 30fde3e357e106394a33c85b387cee3fb49b4c0c
@Simon Hanna Simon Hanna authored on 1 Aug 2016
Showing 2 changed files
View
2
■■■
src/postorius/auth/decorators.py
raise PermissionDenied
return wrapper
 
 
def superuser_or_403(fn):
def superuser_required(fn):
"""Make sure that the logged in user is a superuser or otherwise raise
PermissionDenied.
Assumes the request object to be the first arg."""
def wrapper(*args, **kwargs):
View
16
src/postorius/tests/test_auth_decorators.py
from mock import patch
 
from postorius.auth.decorators import (list_owner_required,
list_moderator_required,
superuser_or_403)
superuser_required)
from postorius.tests.utils import create_mock_list
from mailmanclient import Client
 
 
def dummy_function_mod_req(request, list_id):
return True
 
 
@superuser_or_403
def dummy_superuser_or_403(request):
@superuser_required
def dummy_superuser_required(request):
return True
 
 
class ListOwnerRequiredTest(TestCase):
self.assertEqual(return_value, True)
 
 
class TestSuperUserOr403(TestCase):
"""Tests superuser_or_403 auth decorator"""
"""Tests superuser_required auth decorator"""
 
def setUp(self):
self.request_factory = RequestFactory()
# create a mock list with members
 
request = self.request_factory.get(
'/lists/foolist.example.org/settings/')
request.user = AnonymousUser()
self.assertRaises(PermissionDenied, dummy_superuser_or_403, request)
self.assertRaises(PermissionDenied, dummy_superuser_required, request)
 
@patch.object(Client, 'get_list')
def test_normal_user(self, mock_get_list):
"""Should raise PermissionDenied if user is a normal user."""
request = self.request_factory.get(
'/lists/foolist.example.org/settings/')
request.user = User.objects.create_user(
'new user', 'new@usersfactory.net', 'testing')
self.assertRaises(PermissionDenied, dummy_superuser_or_403, request)
self.assertRaises(PermissionDenied, dummy_superuser_required, request)
 
@patch.object(Client, 'get_list')
def test_super_user(self, mock_get_list):
"""Should not raise PermissionDenied if user is superuser."""
request = self.request_factory.get(
'/lists/foolist.example.org/settings/')
request.user = User.objects.create_superuser(
'new su', 'new@su.net', 'testing')
self.assertTrue(dummy_superuser_or_403(request))
self.assertTrue(dummy_superuser_required(request))