2ece9a601420ea0b79d61621b51a801f1d2f8131
[auf_django_permissions.git] / tests / food / tests.py
1 from __future__ import absolute_import
2
3 from django.contrib.auth.models import User
4 from django.db.models import Q
5 from django.http import HttpRequest
6 from django.template import Template, RequestContext
7 from django.utils import unittest
8 from django.utils.text import normalize_newlines
9
10 from auf.django.permissions import Rules, Predicate, AuthenticationBackend
11
12 from tests.food.models import Food
13
14 @Predicate
15 def is_allergic(user, obj, cls):
16 return Q(allergic_users=user)
17
18
19 class 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)
27 self.rules = Rules()
28 AuthenticationBackend.rules = self.rules
29
30 def tearDown(self):
31 self.alice.delete()
32 self.bob.delete()
33 self.apple.delete()
34 self.banana.delete()
35
36
37 class RulesTestCase(FoodTestCase):
38
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
50 def test_object_perms(self):
51 self.rules.allow('eat', Food, ~is_allergic)
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):
56 self.rules.allow('eat', Food, Predicate(True))
57 self.rules.deny('eat', Food, is_allergic)
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'))
63 self.rules.allow('eat', Food, Predicate(True))
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):
68 self.rules.allow('eat', Food, ~is_allergic)
69 self.assertListEqual(list(self.rules.filter_queryset(self.alice, 'eat', Food.objects.all())),
70 [self.apple])
71 self.assertListEqual(list(self.rules.filter_queryset(self.bob, 'eat', Food.objects.all())),
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
76
77 class TemplateTagsTestCase(FoodTestCase):
78
79 def setUp(self):
80 FoodTestCase.setUp(self)
81 self.rules.allow('eat', Food, ~is_allergic)
82 self.rules.allow('throw', Food, Predicate(True))
83
84 def test_ifhasperm(self):
85 template = Template("""{% load permissions %}
86 {% for fruit in food %}
87 {% ifhasperm "eat" fruit %}
88 Eat the {{ fruit.name }}
89 {% else %}
90 Don'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 %}
107 Eat {{ fruit.name }}: {{ fruit_perms.eat|yesno }}
108 Throw {{ fruit.name }}: {{ fruit_perms.throw|yesno }}
109 Smoke {{ 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 )
125
126