[#2711] setting AUF_PERMISSIONS_RULES
[auf_django_permissions.git] / tests / food / tests.py
CommitLineData
03fab9eb 1from __future__ import absolute_import
f8b57b7e
EMS
2
3from django.contrib.auth.models import User
f8b57b7e
EMS
4from django.db.models import Q
5from django.http import HttpRequest
6from django.template import Template, RequestContext
7from django.utils import unittest
8from django.utils.text import normalize_newlines
9
3e56a4a6 10from auf.django.permissions import Predicate, AuthenticationBackend
f8b57b7e 11
03fab9eb 12from tests.food.models import Food
3e56a4a6 13from tests.permissions import rules
f8b57b7e 14
03fab9eb
EMS
15@Predicate
16def is_allergic(user, obj, cls):
17 return Q(allergic_users=user)
f8b57b7e 18
f8b57b7e
EMS
19
20class FoodTestCase(unittest.TestCase):
21
22 def setUp(self):
23 self.alice = User.objects.create_user('alice', 'alice@example.com')
24 self.bob = User.objects.create_user('bob', 'bob@example.com')
25 self.apple = Food.objects.create(name='apple')
26 self.banana = Food.objects.create(name='banana')
27 self.banana.allergic_users.add(self.alice)
3e56a4a6 28 rules.clear()
f8b57b7e
EMS
29
30 def tearDown(self):
31 self.alice.delete()
32 self.bob.delete()
33 self.apple.delete()
34 self.banana.delete()
f8b57b7e
EMS
35
36
37class RulesTestCase(FoodTestCase):
38
8f8c8a27 39 def test_global_perms(self):
3e56a4a6 40 rules.allow_global('sing', Predicate(lambda user: user is self.alice))
8f8c8a27
EMS
41 self.assertTrue(self.alice.has_perm('sing'))
42 self.assertFalse(self.alice.has_perm('dance'))
43
44 def test_global_deny(self):
3e56a4a6
EMS
45 rules.allow_global('eat', Predicate(True))
46 rules.deny_global('eat', Predicate(lambda user: user is self.bob))
8f8c8a27
EMS
47 self.assertTrue(self.alice.has_perm('eat'))
48 self.assertFalse(self.bob.has_perm('eat'))
49
f8b57b7e 50 def test_object_perms(self):
3e56a4a6 51 rules.allow('eat', Food, ~is_allergic)
f8b57b7e
EMS
52 self.assertTrue(self.alice.has_perm('eat', self.apple))
53 self.assertFalse(self.alice.has_perm('eat', self.banana))
54
55 def test_object_deny(self):
3e56a4a6
EMS
56 rules.allow('eat', Food, Predicate(True))
57 rules.deny('eat', Food, is_allergic)
f8b57b7e
EMS
58 self.assertTrue(self.alice.has_perm('eat', self.apple))
59 self.assertFalse(self.alice.has_perm('eat', self.banana))
60
61 def test_no_rules(self):
62 self.assertFalse(self.alice.has_perm('climb'))
3e56a4a6 63 rules.allow('eat', Food, Predicate(True))
f8b57b7e
EMS
64 self.assertTrue(self.alice.has_perm('eat', self.apple))
65 self.assertFalse(self.alice.has_perm('eat', self.bob))
66
67 def test_q_rules(self):
3e56a4a6
EMS
68 rules.allow('eat', Food, ~is_allergic)
69 self.assertListEqual(list(rules.filter_queryset(self.alice, 'eat', Food.objects.all())),
f8b57b7e 70 [self.apple])
3e56a4a6 71 self.assertListEqual(list(rules.filter_queryset(self.bob, 'eat', Food.objects.all())),
f8b57b7e
EMS
72 [self.apple, self.banana])
73 self.assertTrue(self.alice.has_perm('eat', self.apple))
74 self.assertFalse(self.alice.has_perm('eat', self.banana))
75
03fab9eb 76
f8b57b7e
EMS
77class TemplateTagsTestCase(FoodTestCase):
78
79 def setUp(self):
80 FoodTestCase.setUp(self)
3e56a4a6
EMS
81 rules.allow('eat', Food, ~is_allergic)
82 rules.allow('throw', Food, Predicate(True))
f8b57b7e
EMS
83
84 def test_ifhasperm(self):
85 template = Template("""{% load permissions %}
86{% for fruit in food %}
87{% ifhasperm "eat" fruit %}
88Eat the {{ fruit.name }}
89{% else %}
90Don't eat the {{ fruit.name }}
91{% endifhasperm %}
92{% endfor %}""")
93 request = HttpRequest()
94 request.user = self.alice
95 context = RequestContext(request, {'food': Food.objects.all()})
96 self.assertRegexpMatches(template.render(context).strip(),
97 r'\s+'.join([
98 "Eat the apple",
99 "Don't eat the banana"
100 ])
101 )
102
103 def test_withperms(self):
104 template = Template("""{% load permissions %}
105{% for fruit in food %}
106{% withperms fruit as fruit_perms %}
107Eat {{ fruit.name }}: {{ fruit_perms.eat|yesno }}
108Throw {{ fruit.name }}: {{ fruit_perms.throw|yesno }}
109Smoke {{ fruit.name }}: {{ fruit_perms.smoke|yesno }}
110{% endwithperms %}
111{% endfor %}""")
112 request = HttpRequest()
113 request.user = self.alice
114 context = RequestContext(request, {'food': Food.objects.all()})
115 self.assertRegexpMatches(template.render(context).strip(),
116 r'\s+'.join([
117 "Eat apple: yes",
118 "Throw apple: yes",
119 "Smoke apple: no",
120 "Eat banana: no",
121 "Throw banana: yes",
122 "Smoke banana: no"
123 ])
124 )
03fab9eb
EMS
125
126