1 from __future__
import absolute_import
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
10 from auf
.django
.permissions
import Rules
, Predicate
, AuthenticationBackend
12 from tests
.food
.models
import Food
15 def is_allergic(user
, obj
, cls
):
16 return Q(allergic_users
=user
)
19 class FoodTestCase(unittest
.TestCase
):
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
)
28 AuthenticationBackend
.rules
= self
.rules
37 class RulesTestCase(FoodTestCase
):
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'))
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'))
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
))
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
))
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
))
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())),
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
))
77 class TemplateTagsTestCase(FoodTestCase
):
80 FoodTestCase
.setUp(self
)
81 self
.rules
.allow('eat', Food
, ~is_allergic
)
82 self
.rules
.allow('throw', Food
, Predicate(True))
84 def test_ifhasperm(self
):
85 template
= Template("""{% load permissions %}
86 {% for fruit in food %}
87 {% ifhasperm "eat" fruit %}
88 Eat the {{ fruit.name }}
90 Don't eat the {{ fruit.name }}
93 request
= HttpRequest()
94 request
.user
= self
.alice
95 context
= RequestContext(request
, {'food': Food
.objects
.all()})
96 self
.assertRegexpMatches(template
.render(context
).strip(),
99 "Don't eat the banana"
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 }}
112 request
= HttpRequest()
113 request
.user
= self
.alice
114 context
= RequestContext(request
, {'food': Food
.objects
.all()})
115 self
.assertRegexpMatches(template
.render(context
).strip(),