Support pour les permissions globales
[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
03fab9eb 10from auf.django.permissions import Rules, Predicate, AuthenticationBackend
f8b57b7e 11
03fab9eb 12from tests.food.models import Food
f8b57b7e 13
03fab9eb
EMS
14@Predicate
15def is_allergic(user, obj, cls):
16 return Q(allergic_users=user)
f8b57b7e 17
f8b57b7e
EMS
18
19class FoodTestCase(unittest.TestCase):
20
21 def setUp(self):
22 self.alice = User.objects.create_user('alice', 'alice@example.com')
23 self.bob = User.objects.create_user('bob', 'bob@example.com')
24 self.apple = Food.objects.create(name='apple')
25 self.banana = Food.objects.create(name='banana')
26 self.banana.allergic_users.add(self.alice)
03fab9eb
EMS
27 self.rules = Rules()
28 AuthenticationBackend.rules = self.rules
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
EMS
39 def test_global_perms(self):
40 self.rules.allow_global('sing', Predicate(lambda user: user is self.alice))
41 self.assertTrue(self.alice.has_perm('sing'))
42 self.assertFalse(self.alice.has_perm('dance'))
43
44 def test_global_deny(self):
45 self.rules.allow_global('eat', Predicate(True))
46 self.rules.deny_global('eat', Predicate(lambda user: user is self.bob))
47 self.assertTrue(self.alice.has_perm('eat'))
48 self.assertFalse(self.bob.has_perm('eat'))
49
f8b57b7e 50 def test_object_perms(self):
03fab9eb 51 self.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):
03fab9eb
EMS
56 self.rules.allow('eat', Food, Predicate(True))
57 self.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'))
03fab9eb 63 self.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):
03fab9eb
EMS
68 self.rules.allow('eat', Food, ~is_allergic)
69 self.assertListEqual(list(self.rules.filter_queryset(self.alice, 'eat', Food.objects.all())),
f8b57b7e 70 [self.apple])
03fab9eb 71 self.assertListEqual(list(self.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)
03fab9eb
EMS
81 self.rules.allow('eat', Food, ~is_allergic)
82 self.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