/usr/lib/python2.7/dist-packages/guardian/testapp/tests/tags_test.py is in python-django-guardian 1.2.4+git20141127-0.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | from __future__ import unicode_literals
from django.conf import settings
from django.contrib.auth.models import Group, AnonymousUser
from django.contrib.contenttypes.models import ContentType
from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase
from guardian.compat import get_user_model
from guardian.exceptions import NotUserNorGroup
from guardian.models import UserObjectPermission, GroupObjectPermission
User = get_user_model()
def render(template, context):
"""
Returns rendered ``template`` with ``context``, which are given as string
and dict respectively.
"""
t = Template(template)
return t.render(Context(context))
class GetObjPermsTagTest(TestCase):
def setUp(self):
self.ctype = ContentType.objects.create(name='foo', model='bar',
app_label='fake-for-guardian-tests')
self.group = Group.objects.create(name='jackGroup')
self.user = User.objects.create(username='jack')
self.user.groups.add(self.group)
def test_wrong_formats(self):
wrong_formats = (
'{% get_obj_perms user for contenttype as obj_perms %}', # no quotes
'{% get_obj_perms user for contenttype as \'obj_perms" %}', # wrong quotes
'{% get_obj_perms user for contenttype as \'obj_perms" %}', # wrong quotes
'{% get_obj_perms user for contenttype as obj_perms" %}', # wrong quotes
'{% get_obj_perms user for contenttype as obj_perms\' %}', # wrong quotes
'{% get_obj_perms user for contenttype as %}', # no context_var
'{% get_obj_perms for contenttype as "obj_perms" %}', # no user/group
'{% get_obj_perms user contenttype as "obj_perms" %}', # no "for" bit
'{% get_obj_perms user for contenttype "obj_perms" %}', # no "as" bit
'{% get_obj_perms user for as "obj_perms" %}', # no object
)
context = {'user': User.get_anonymous(), 'contenttype': self.ctype}
for wrong in wrong_formats:
fullwrong = '{% load guardian_tags %}' + wrong
try:
render(fullwrong, context)
self.fail("Used wrong get_obj_perms tag format: \n\n\t%s\n\n "
"but TemplateSyntaxError have not been raised" % wrong)
except TemplateSyntaxError:
pass
def test_obj_none(self):
template = ''.join((
'{% load guardian_tags %}',
'{% get_obj_perms user for object as "obj_perms" %}{{ perms }}',
))
context = {'user': User.get_anonymous(), 'object': None}
output = render(template, context)
self.assertEqual(output, '')
def test_anonymous_user(self):
template = ''.join((
'{% load guardian_tags %}',
'{% get_obj_perms user for contenttype as "obj_perms" %}{{ perms }}',
))
context = {'user': AnonymousUser(), 'contenttype': self.ctype}
anon_output = render(template, context)
context = {'user': User.get_anonymous(), 'contenttype': self.ctype}
real_anon_user_output = render(template, context)
self.assertEqual(anon_output, real_anon_user_output)
def test_wrong_user_or_group(self):
template = ''.join((
'{% load guardian_tags %}',
'{% get_obj_perms some_obj for contenttype as "obj_perms" %}',
))
context = {'some_obj': ContentType(), 'contenttype': self.ctype}
# This test would raise TemplateSyntaxError instead of NotUserNorGroup
# if TEMPLATE_DEBUG is set to True during tests
tmp = settings.TEMPLATE_DEBUG
settings.TEMPLATE_DEBUG = False
self.assertRaises(NotUserNorGroup, render, template, context)
settings.TEMPLATE_DEBUG = tmp
def test_superuser(self):
user = User.objects.create(username='superuser', is_superuser=True)
template = ''.join((
'{% load guardian_tags %}',
'{% get_obj_perms user for contenttype as "obj_perms" %}',
'{{ obj_perms|join:" " }}',
))
context = {'user': user, 'contenttype': self.ctype}
output = render(template, context)
for perm in ('add_contenttype', 'change_contenttype', 'delete_contenttype'):
self.assertTrue(perm in output)
def test_user(self):
UserObjectPermission.objects.assign_perm("change_contenttype", self.user,
self.ctype)
GroupObjectPermission.objects.assign_perm("delete_contenttype", self.group,
self.ctype)
template = ''.join((
'{% load guardian_tags %}',
'{% get_obj_perms user for contenttype as "obj_perms" %}',
'{{ obj_perms|join:" " }}',
))
context = {'user': self.user, 'contenttype': self.ctype}
output = render(template, context)
self.assertEqual(
set(output.split(' ')),
set('change_contenttype delete_contenttype'.split(' ')))
def test_group(self):
GroupObjectPermission.objects.assign_perm("delete_contenttype", self.group,
self.ctype)
template = ''.join((
'{% load guardian_tags %}',
'{% get_obj_perms group for contenttype as "obj_perms" %}',
'{{ obj_perms|join:" " }}',
))
context = {'group': self.group, 'contenttype': self.ctype}
output = render(template, context)
self.assertEqual(output, 'delete_contenttype')
|