beffe93d8dfb4866f96a707888e395e89b4b9710
[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_object_perms(self):
40 self.rules.allow('eat', Food, ~is_allergic)
41 self.assertTrue(self.alice.has_perm('eat', self.apple))
42 self.assertFalse(self.alice.has_perm('eat', self.banana))
43
44 def test_object_deny(self):
45 self.rules.allow('eat', Food, Predicate(True))
46 self.rules.deny('eat', Food, is_allergic)
47 self.assertTrue(self.alice.has_perm('eat', self.apple))
48 self.assertFalse(self.alice.has_perm('eat', self.banana))
49
50 def test_no_rules(self):
51 self.assertFalse(self.alice.has_perm('climb'))
52 self.rules.allow('eat', Food, Predicate(True))
53 self.assertTrue(self.alice.has_perm('eat', self.apple))
54 self.assertFalse(self.alice.has_perm('eat', self.bob))
55
56 def test_q_rules(self):
57 self.rules.allow('eat', Food, ~is_allergic)
58 self.assertListEqual(list(self.rules.filter_queryset(self.alice, 'eat', Food.objects.all())),
59 [self.apple])
60 self.assertListEqual(list(self.rules.filter_queryset(self.bob, 'eat', Food.objects.all())),
61 [self.apple, self.banana])
62 self.assertTrue(self.alice.has_perm('eat', self.apple))
63 self.assertFalse(self.alice.has_perm('eat', self.banana))
64
65
66 class TemplateTagsTestCase(FoodTestCase):
67
68 def setUp(self):
69 FoodTestCase.setUp(self)
70 self.rules.allow('eat', Food, ~is_allergic)
71 self.rules.allow('throw', Food, Predicate(True))
72
73 def test_ifhasperm(self):
74 template = Template("""{% load permissions %}
75 {% for fruit in food %}
76 {% ifhasperm "eat" fruit %}
77 Eat the {{ fruit.name }}
78 {% else %}
79 Don't eat the {{ fruit.name }}
80 {% endifhasperm %}
81 {% endfor %}""")
82 request = HttpRequest()
83 request.user = self.alice
84 context = RequestContext(request, {'food': Food.objects.all()})
85 self.assertRegexpMatches(template.render(context).strip(),
86 r'\s+'.join([
87 "Eat the apple",
88 "Don't eat the banana"
89 ])
90 )
91
92 def test_withperms(self):
93 template = Template("""{% load permissions %}
94 {% for fruit in food %}
95 {% withperms fruit as fruit_perms %}
96 Eat {{ fruit.name }}: {{ fruit_perms.eat|yesno }}
97 Throw {{ fruit.name }}: {{ fruit_perms.throw|yesno }}
98 Smoke {{ fruit.name }}: {{ fruit_perms.smoke|yesno }}
99 {% endwithperms %}
100 {% endfor %}""")
101 request = HttpRequest()
102 request.user = self.alice
103 context = RequestContext(request, {'food': Food.objects.all()})
104 self.assertRegexpMatches(template.render(context).strip(),
105 r'\s+'.join([
106 "Eat apple: yes",
107 "Throw apple: yes",
108 "Smoke apple: no",
109 "Eat banana: no",
110 "Throw banana: yes",
111 "Smoke banana: no"
112 ])
113 )
114
115