def test_user_does_not_have_global_permissions(self): registry.register("negative_permission", negative_permission) user = User() user.save() context = Context({"user": user}) template = Template( """ {% load rottweiler_tags %} {% rottweiler_perms negative_permission as can_access %} """ ) template.render(context) self.assertFalse(context["can_access"])
def test_user_has_global_permissions(self): registry.register("positive_permission", positive_permission) user = User() user.save() context = Context({"user": user}) template = Template( """ {% load rottweiler_tags %} {% rottweiler_perms positive_permission as can_access %} """ ) template.render(context) self.assertTrue(context["can_access"])
def test_user_does_not_have_permissions_against_model_instance(self): registry.register("negative_permission", negative_permission, ModelStub) user = User() user.save() context = Context({"user": user, "object": ModelStub()}) template = Template( """ {% load rottweiler_tags %} {% rottweiler_perms negative_permission object as can_access %} """ ) template.render(context) self.assertFalse(context["can_access"])
def test_assigns_list_of_all_rules_to_object(self): registry.register('first_permission', first_permission, ModelStub) registry.register('second_permission', second_permission, ModelStub) self.request.user = User(is_superuser=True) view = ShowAllRules.as_view() response = view(self.request) model_stub_rule = None for rule in response.context_data['object']: if rule['model_name'] == 'ModelStub': model_stub_rule = rule break self.assertTrue(model_stub_rule) self.assertTrue({'name': 'first_permission', 'definition': ' return user.is_superuser or user.is_staff\n'} in model_stub_rule['permissions']) self.assertTrue({'name': 'second_permission', 'definition': ' return user.is_active\n'} in model_stub_rule['permissions'])
from rottweiler import registry from django.contrib.auth import get_user_model def change_user(self, user): return True registry.register('project.change_user', change_user) registry.register('project.change_user', change_user, get_user_model())
from django.test import TestCase from rottweiler import registry from rottweiler.backends import PermissionBackend from .stubs import ModelStub def test_global_permission(self, user): return user.is_staff def test_object_permission(self, user): return self.return_value registry.register('test_app.test_permission', test_global_permission) registry.register('test_app.test_permission', test_object_permission, ModelStub) class TestBackends(TestCase): def test_user_is_checked_for_global_rule_permission(self): user = User(is_active=True, is_staff=True) user.save() backend = PermissionBackend() self.assertTrue(backend.has_perm(user, 'test_app.test_permission')) def test_user_is_checked_for_global_db_permission(self): user = User(is_active=True) user.save() content_type = ContentType(app_label='test_app') content_type.save()
def test_add_global_permission(self): registry.register('test_permission', test_permission) user = User() user.save() self.assertTrue(user.has_perm('test_permission'))
def test_add_object_permission(self): registry.register('test_permission', test_permission, ModelStub) user = User() user.save() self.assertTrue(user.has_perm('test_permission', ModelStub()))