def test_if_tag_and(self): user = create_user('permission_templatetag_test_user1') perm1 = create_permission('permission_templatetag_test_perm1') perm2 = create_permission('permission_templatetag_test_perm2') user.user_permissions.add(perm1, perm2) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm1')) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm2')) context = Context({ 'user': user, }) out = Template( "{% if user has 'permission.unknown_perm' " "and user has 'permission.permission_templatetag_test_perm2' %}" "Fail" "{% elif user has 'permission.permission_templatetag_test_perm1' " "and user has 'permission.unknown_perm' %}" "Fail" "{% elif user has 'permission.permission_templatetag_test_perm1' " "and user has 'permission.permission_templatetag_test_perm2' %}" "Success" "{% endif %}" ).render(context) self.assertEqual(out, "Success")
def test_permissionif_tag_or(self): user = create_user('permission_templatetag_test_user1') perm1 = create_permission('permission_templatetag_test_perm1') perm2 = create_permission('permission_templatetag_test_perm2') user.user_permissions.add(perm1) self.assertTrue(user.has_perm('permission.permission_templatetag_test_perm1')) self.assertFalse(user.has_perm('permission.permission_templatetag_test_perm2')) context = Context({ 'user': user, }) out = Template( "{% load permissionif %}" "{% permission user has 'permission.permission_templatetag_test_perm1' " "and user has 'permission.permission_templatetag_test_perm2' %}" "Fail" "{% elpermission user has 'permission.permission_templatetag_test_perm1' " "or user has 'permission.permission_templatetag_test_perm2' %}" "Success" "{% endpermission %}" ).render(context) self.assertEqual(out, "Success")
def test_if_tag_or(self): user = create_user('permission_templatetag_test_user1') perm1 = create_permission('permission_templatetag_test_perm1') create_permission('permission_templatetag_test_perm2') user.user_permissions.add(perm1) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm1')) self.assertFalse( user.has_perm('permission.permission_templatetag_test_perm2')) context = Context({ 'user': user, }) out = Template( "{% if user has 'permission.permission_templatetag_test_perm1' " "and user has 'permission.permission_templatetag_test_perm2' %}" "Fail" "{% elif user has 'permission.permission_templatetag_test_perm1' " "or user has 'permission.permission_templatetag_test_perm2' %}" "Success" "{% endif %}").render(context) self.assertEqual(out, "Success")
def test_permissionif_tag_and(self): user = create_user('permission_templatetag_test_user1') perm1 = create_permission('permission_templatetag_test_perm1') perm2 = create_permission('permission_templatetag_test_perm2') user.user_permissions.add(perm1, perm2) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm1')) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm2')) context = Context({ 'user': user, }) out = Template( "{% permission user has 'permission.unknown_perm' " "and user has 'permission.permission_templatetag_test_perm2' %}" "Fail" "{% elpermission user has " "'permission.permission_templatetag_test_perm1' " "and user has 'permission.unknown_perm' %}" "Fail" "{% elpermission user has " "'permission.permission_templatetag_test_perm1' " "and user has 'permission.permission_templatetag_test_perm2' %}" "Success" "{% endpermission %}").render(context) self.assertEqual(out, "Success")
def test_permissionif_tag_with_obj(self): from permission.tests.models import Article from permission.handlers import PermissionHandler user = create_user('permission_templatetag_test_user1') art1 = create_article('permission_templatetag_test_article1') art2 = create_article('permission_templatetag_test_article2') create_permission('permission_templatetag_test_perm1') class ArticlePermissionHandler(PermissionHandler): def has_perm(self, user_obj, perm, obj=None): if perm == 'permission.permission_templatetag_test_perm1': if (obj and obj.title == 'permission_templatetag_test_article2'): return True return False registry.register(Article, ArticlePermissionHandler) self.assertFalse( user.has_perm('permission.permission_templatetag_test_perm1')) self.assertFalse( user.has_perm('permission.permission_templatetag_test_perm1', art1)) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm1', art2)) context = Context({ 'user': user, 'art1': art1, 'art2': art2, }) out = Template( "{% load permissionif %}" "{% permission user has " "'permission.permission_templatetag_test_perm1' %}" "Fail" "{% elpermission user has " "'permission.permission_templatetag_test_perm1' of art1 %}" "Fail" "{% elpermission user has " "'permission.permission_templatetag_test_perm1' of art2 %}" "Success" "{% else %}" "Fail" "{% endpermission %}" ).render(context) self.assertEqual(out, "Success")
def test_permissionif_tag_with_obj(self): from permission.tests.models import Article from permission.handlers import PermissionHandler user = create_user('permission_templatetag_test_user1') art1 = create_article('permission_templatetag_test_article1') art2 = create_article('permission_templatetag_test_article2') create_permission('permission_templatetag_test_perm1') class ArticlePermissionHandler(PermissionHandler): def has_perm(self, user_obj, perm, obj=None): if perm == 'permission.permission_templatetag_test_perm1': if (obj and obj.title == 'permission_templatetag_test_article2'): return True return False registry.register(Article, ArticlePermissionHandler) self.assertFalse( user.has_perm('permission.permission_templatetag_test_perm1')) self.assertFalse( user.has_perm('permission.permission_templatetag_test_perm1', art1)) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm1', art2)) context = Context({ 'user': user, 'art1': art1, 'art2': art2, }) out = Template( "{% load permissionif %}" "{% permission user has " "'permission.permission_templatetag_test_perm1' %}" "Fail" "{% elpermission user has " "'permission.permission_templatetag_test_perm1' of art1 %}" "Fail" "{% elpermission user has " "'permission.permission_templatetag_test_perm1' of art2 %}" "Success" "{% else %}" "Fail" "{% endpermission %}").render(context) self.assertEqual(out, "Success")
def test__get_all_permissions(self): # role1 -- perm1, perm2 # +- role2 -- perm3 # +- role3 -- perm4, perm5 # | +- role4 -- perm6 # | +- role5 -- perm7 # +- role6 -- perm8 perm1 = create_permission('permission_test_perm1') perm2 = create_permission('permission_test_perm2') perm3 = create_permission('permission_test_perm3') perm4 = create_permission('permission_test_perm4') perm5 = create_permission('permission_test_perm5') perm6 = create_permission('permission_test_perm6') perm7 = create_permission('permission_test_perm7') perm8 = create_permission('permission_test_perm8') role1 = create_role('permission_test_role1') role2 = create_role('permission_test_role2', role1) role3 = create_role('permission_test_role3', role1) role4 = create_role('permission_test_role4', role3) role5 = create_role('permission_test_role5', role3) role6 = create_role('permission_test_role6', role1) role1._permissions.add(perm1, perm2) role2._permissions.add(perm3) role3._permissions.add(perm4, perm5) role4._permissions.add(perm6) role5._permissions.add(perm7) role6._permissions.add(perm8) self.assertItemsEqual(role1.permissions, [perm1, perm2]) self.assertItemsEqual(role2.permissions, [ perm1, perm2, perm3, ]) self.assertItemsEqual(role3.permissions, [ perm1, perm2, perm4, perm5, ]) self.assertItemsEqual(role4.permissions, [ perm1, perm2, perm4, perm5, perm6, ]) self.assertItemsEqual(role5.permissions, [ perm1, perm2, perm4, perm5, perm7, ]) self.assertItemsEqual(role6.permissions, [ perm1, perm2, perm8, ])
def test_permissionif_tag_elif(self): user = create_user('permission_templatetag_test_user1') perm = create_permission('permission_templatetag_test_perm1') user.user_permissions.add(perm) self.assertTrue(user.has_perm('permission.permission_templatetag_test_perm1')) context = Context({ 'user': user, }) out = Template( "{% load permissionif %}" "{% permission user has 'permission.unknown_permission' %}" "Fail" "{% elpermission user has 'permission.unknown_permisson2' %}" "Fail" "{% elpermission user has 'permission.permission_templatetag_test_perm1' %}" "Success" "{% else %}" "Fail" "{% endpermission %}" ).render(context) self.assertEqual(out, "Success")
def test_permissionif_tag_elif(self): user = create_user('permission_templatetag_test_user1') perm = create_permission('permission_templatetag_test_perm1') user.user_permissions.add(perm) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm1')) context = Context({ 'user': user, }) out = Template( "{% load permissionif %}" "{% permission user has 'permission.unknown_permission' %}" "Fail" "{% elpermission user has 'permission.unknown_permisson2' %}" "Fail" "{% elpermission user has " "'permission.permission_templatetag_test_perm1' %}" "Success" "{% else %}" "Fail" "{% endpermission %}").render(context) self.assertEqual(out, "Success")
def test_add_permissions(self): # role1 -- perm1, perm2 # +- role2 -- perm3 # +- role3 -- perm4, perm5 # | +- role4 -- perm6 # | +- role5 -- perm7 # +- role6 -- perm8 perm1 = create_permission('permission_test_perm1') perm2 = create_permission('permission_test_perm2') perm3 = create_permission('permission_test_perm3') perm4 = create_permission('permission_test_perm4') perm5 = create_permission('permission_test_perm5') perm6 = create_permission('permission_test_perm6') perm7 = create_permission('permission_test_perm7') perm8 = create_permission('permission_test_perm8') role1 = create_role('permission_test_role1') role2 = create_role('permission_test_role2', role1) role3 = create_role('permission_test_role3', role1) role4 = create_role('permission_test_role4', role3) role5 = create_role('permission_test_role5', role3) role6 = create_role('permission_test_role6', role1) role1.permissions.add(perm1, perm2) role2.permissions.add(perm3) role3.permissions.add(perm4, perm5) role4.permissions.add(perm6) role5.permissions.add(perm7) role6.permissions.add(perm8) self.assertItemsEqual(role1._permissions.all(), [ perm1, perm2, ]) self.assertItemsEqual(role2._permissions.all(), [ perm3, ]) self.assertItemsEqual(role3._permissions.all(), [ perm4, perm5, ]) self.assertItemsEqual(role4._permissions.all(), [ perm6, ]) self.assertItemsEqual(role5._permissions.all(), [ perm7, ]) self.assertItemsEqual(role6._permissions.all(), [ perm8, ])
def test_permissionif_tag_with_obj(self): from permission import registry from permission import PermissionHandler from permission.models import Role user = create_user('permission_templatetag_test_user1') role1 = create_role('permission_templatetag_test_role1') role2 = create_role('permission_templatetag_test_role2') perm = create_permission('permission_templatetag_test_perm1') class RolePermissionHandler(PermissionHandler): def has_perm(self, user_obj, perm, obj=None): if perm == 'permission.permission_templatetag_test_perm1': if obj and obj.codename == 'permission_templatetag_test_role2': return True return False registry.register(Role, RolePermissionHandler) self.assertFalse( user.has_perm('permission.permission_templatetag_test_perm1')) self.assertFalse( user.has_perm('permission.permission_templatetag_test_perm1', role1)) self.assertTrue( user.has_perm('permission.permission_templatetag_test_perm1', role2)) context = Context({ 'user': user, 'role1': role1, 'role2': role2, }) out = Template( "{% load permission_tags %}" "{% permission user has 'permission.permission_templatetag_test_perm1' %}" "Fail" "{% elpermission user has 'permission.permission_templatetag_test_perm1' of role1 %}" "Fail" "{% elpermission user has 'permission.permission_templatetag_test_perm1' of role2 %}" "Success" "{% else %}" "Fail" "{% endpermission %}").render(context) self.assertEqual(out, "Success")
def test_permissionif_tag_with_obj(self): from permission import registry from permission import PermissionHandler from permission.models import Role user = create_user('permission_templatetag_test_user1') role1 = create_role('permission_templatetag_test_role1') role2 = create_role('permission_templatetag_test_role2') perm = create_permission('permission_templatetag_test_perm1') class RolePermissionHandler(PermissionHandler): def has_perm(self, user_obj, perm, obj=None): if perm == 'permission.permission_templatetag_test_perm1': if obj and obj.codename == 'permission_templatetag_test_role2': return True return False registry.register(Role, RolePermissionHandler) self.assertFalse(user.has_perm('permission.permission_templatetag_test_perm1')) self.assertFalse(user.has_perm('permission.permission_templatetag_test_perm1', role1)) self.assertTrue(user.has_perm('permission.permission_templatetag_test_perm1', role2)) context = Context({ 'user': user, 'role1': role1, 'role2': role2, }) out = Template( "{% load permission_tags %}" "{% permission user has 'permission.permission_templatetag_test_perm1' %}" "Fail" "{% elpermission user has 'permission.permission_templatetag_test_perm1' of role1 %}" "Fail" "{% elpermission user has 'permission.permission_templatetag_test_perm1' of role2 %}" "Success" "{% else %}" "Fail" "{% endpermission %}" ).render(context) self.assertEqual(out, "Success")
def test_get_all_permissions_of_user(self): # role1 -- user1, user2 -- perm1, perm2 # +- role2 -- user3 -- perm3 # +- role3 -- user4, user5 -- perm4, perm5 # | +- role4 -- user6 -- perm6 # | +- role5 -- user7 -- perm7 # +- role6 -- user8 -- perm8 user1 = create_user('permission_test_user1') user2 = create_user('permission_test_user2') user3 = create_user('permission_test_user3') user4 = create_user('permission_test_user4') user5 = create_user('permission_test_user5') user6 = create_user('permission_test_user6') user7 = create_user('permission_test_user7') user8 = create_user('permission_test_user8') perm1 = create_permission('permission_test_perm1') perm2 = create_permission('permission_test_perm2') perm3 = create_permission('permission_test_perm3') perm4 = create_permission('permission_test_perm4') perm5 = create_permission('permission_test_perm5') perm6 = create_permission('permission_test_perm6') perm7 = create_permission('permission_test_perm7') perm8 = create_permission('permission_test_perm8') role1 = create_role('permission_test_role1') role2 = create_role('permission_test_role2', role1) role3 = create_role('permission_test_role3', role1) role4 = create_role('permission_test_role4', role3) role5 = create_role('permission_test_role5', role3) role6 = create_role('permission_test_role6', role1) role1._users.add(user1, user2) role2._users.add(user3) role3._users.add(user4, user5) role4._users.add(user6) role5._users.add(user7) role6._users.add(user8) role1._permissions.add(perm1, perm2) role2._permissions.add(perm3) role3._permissions.add(perm4, perm5) role4._permissions.add(perm6) role5._permissions.add(perm7) role6._permissions.add(perm8) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user1), [ perm1, perm2, ]) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user2), [ perm1, perm2, ]) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user3), [ perm1, perm2, perm3, ]) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user4), [ perm1, perm2, perm4, perm5, ]) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user5), [ perm1, perm2, perm4, perm5, ]) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user6), [ perm1, perm2, perm4, perm5, perm6, ]) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user7), [ perm1, perm2, perm4, perm5, perm7, ]) self.assertItemsEqual(Role.objects.get_all_permissions_of_user(user8), [ perm1, perm2, perm8, ])