Esempio n. 1
0
 def setUp(self):
     self.user1 = create_user('john')
     self.user2 = create_user('tony')
     self.anonymous = create_anonymous()
     self.perm1 = 'auth.add_user'
     self.perm2 = 'auth.change_user'
     self.perm3 = 'auth.delete_user'
Esempio n. 2
0
 def setUp(self):
     self.user1 = create_user('john')
     self.user2 = create_user('tony')
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test', user=self.user2)
Esempio n. 3
0
 def setUp(self):
     self.user1 = create_user('john', is_staff=True)
     self.user2 = create_user('tony')
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
 def setUp(self):
     self.user1 = create_user('john')
     self.user2 = create_user('tony')
     self.anonymous = create_anonymous()
     self.perm1 = 'auth.add_user'
     self.perm2 = 'auth.change_user'
     self.perm3 = 'auth.delete_user'
 def setUp(self):
     self.user1 = create_user('john')
     self.user2 = create_user('tony')
     self.anonymous = create_anonymous()
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test', user=self.user2)
 def setUp(self):
     self.user1 = create_user('john')
     self.user2 = create_user('tony')
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
     self.article.authors.add(self.user2)
Esempio n. 7
0
 def setUp(self):
     self.user1 = create_user('john', is_staff=True)
     self.user2 = create_user('tony')
     self.anonymous = create_anonymous()
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
Esempio n. 8
0
 def setUp(self):
     self.user1 = create_user("john")
     self.user2 = create_user("tony")
     self.user3 = create_user("peter")
     self.anonymous = create_anonymous()
     self.group1 = create_group("admin", self.user1)
     self.group2 = create_group("staff", self.user2)
     self.perm1 = "permission.add_article"
     self.perm2 = "permission.change_article"
     self.perm3 = "permission.delete_article"
     self.article = create_article("test")
 def setUp(self):
     self.user1 = create_user('john')
     self.user2 = create_user('tony')
     self.user3 = create_user('peter')
     self.anonymous = create_anonymous()
     self.group1 = create_group('admin', self.user1)
     self.group2 = create_group('staff', self.user2)
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
Esempio n. 10
0
 def setUp(self):
     self.user1 = create_user('john')
     self.user2 = create_user('tony')
     self.user3 = create_user('peter')
     self.anonymous = create_anonymous()
     self.group1 = create_group('admin', self.user1)
     self.group2 = create_group('staff', self.user2)
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
Esempio n. 11
0
 def setUp(self):
     self.author = create_user('permission_test_articles_author')
     self.editor1 = create_user('permission_test_articles_editor1')
     self.editor2 = create_user('permission_test_articles_editor2')
     self.bridge1 = create_bridge()
     self.bridge2 = create_bridge()
     self.bridge3 = create_bridge()
     self.model = create_article('permission_test_article', self.author,
                                 self.bridge1)
     self.model.editors.add(self.editor1)
     self.model.editors.add(self.editor2)
     self.model.multiple_bridge.add(self.bridge2)
     self.model.multiple_bridge.add(self.bridge3)
Esempio n. 12
0
    def test_filter_by_user(self):
        # role1           -- user1, user2
        #   +- role2      -- user3
        #   +- role3      -- user4, user5
        #   |    +- role4 -- user6
        #   |    +- role5 -- user7
        #   +- role6      -- user8
        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')
        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)

        self.assertItemsEqual(Role.objects.filter_by_user(user1), [
            role1,
        ])
        self.assertItemsEqual(Role.objects.filter_by_user(user2), [
            role1,
        ])
        self.assertItemsEqual(Role.objects.filter_by_user(user3), [
            role1,
            role2,
        ])
        self.assertItemsEqual(Role.objects.filter_by_user(user4), [
            role1,
            role3,
        ])
        self.assertItemsEqual(Role.objects.filter_by_user(user5), [
            role1,
            role3,
        ])
        self.assertItemsEqual(Role.objects.filter_by_user(user6), [
            role1,
            role3,
            role4,
        ])
        self.assertItemsEqual(Role.objects.filter_by_user(user7), [
            role1,
            role3,
            role5,
        ])
        self.assertItemsEqual(Role.objects.filter_by_user(user8), [
            role1,
            role6,
        ])
 def setUp(self):
     self.author = create_user('permission_test_articles_author')
     self.editor1 = create_user(
         'permission_test_articles_editor1')
     self.editor2 = create_user(
         'permission_test_articles_editor2')
     self.bridge1 = create_bridge()
     self.bridge2 = create_bridge()
     self.bridge3 = create_bridge()
     self.model = create_article('permission_test_article',
                                 self.author, self.bridge1)
     self.model.editors.add(self.editor1)
     self.model.editors.add(self.editor2)
     self.model.multiple_bridge.add(self.bridge2)
     self.model.multiple_bridge.add(self.bridge3)
    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 setUp(self):
     self.user = create_user('john')
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
     self.original_get_handlers = registry.get_handlers
    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_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_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_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")
Esempio n. 20
0
 def setUp(self):
     self.user = create_user('john')
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
     self.original_get_handlers = registry.get_handlers
    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")
Esempio n. 22
0
 def setUp(self):
     self.handler = PermissionHandler
     self.user = create_user('john')
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
 def setUp(self):
     self.handler = PermissionHandler
     self.user = create_user('john')
     self.perm1 = 'permission.add_article'
     self.perm2 = 'permission.change_article'
     self.perm3 = 'permission.delete_article'
     self.article = create_article('test')
Esempio n. 24
0
    def test__get_all_users(self):
        # role1           -- user1, user2
        #   +- role2      -- user3
        #   +- role3      -- user4, user5
        #   |    +- role4 -- user6
        #   |    +- role5 -- user7
        #   +- role6      -- user8
        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')
        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)

        self.assertItemsEqual(role1.users, [
            user1,
            user2,
            user3,
            user4,
            user5,
            user6,
            user7,
            user8,
        ])
        self.assertItemsEqual(role2.users, [
            user3,
        ])
        self.assertItemsEqual(role3.users, [
            user4,
            user5,
            user6,
            user7,
        ])
        self.assertItemsEqual(role4.users, [
            user6,
        ])
        self.assertItemsEqual(role5.users, [
            user7,
        ])
        self.assertItemsEqual(role6.users, [
            user8,
        ])
Esempio n. 25
0
    def test_is_belong(self):
        # role1           -- user1, user2
        #   +- role2      -- user3
        #   +- role3      -- user4, user5
        #   |    +- role4 -- user6
        #   |    +- role5 -- user7
        #   +- role6      -- user8
        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')
        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)

        self.assertTrue(role1.is_belong(user1))
        self.assertTrue(role1.is_belong(user2))
        self.assertTrue(role1.is_belong(user3))
        self.assertTrue(role1.is_belong(user4))
        self.assertTrue(role1.is_belong(user5))
        self.assertTrue(role1.is_belong(user6))
        self.assertTrue(role1.is_belong(user7))
        self.assertTrue(role1.is_belong(user8))

        self.assertFalse(role2.is_belong(user1))
        self.assertFalse(role2.is_belong(user2))
        self.assertTrue(role2.is_belong(user3))
        self.assertFalse(role2.is_belong(user4))
        self.assertFalse(role2.is_belong(user5))
        self.assertFalse(role2.is_belong(user6))
        self.assertFalse(role2.is_belong(user7))
        self.assertFalse(role2.is_belong(user8))

        self.assertFalse(role3.is_belong(user1))
        self.assertFalse(role3.is_belong(user2))
        self.assertFalse(role3.is_belong(user3))
        self.assertTrue(role3.is_belong(user4))
        self.assertTrue(role3.is_belong(user5))
        self.assertTrue(role3.is_belong(user6))
        self.assertTrue(role3.is_belong(user7))
        self.assertFalse(role3.is_belong(user8))
Esempio n. 26
0
    def test_is_belong(self):
        # role1           -- user1, user2
        #   +- role2      -- user3
        #   +- role3      -- user4, user5
        #   |    +- role4 -- user6
        #   |    +- role5 -- user7
        #   +- role6      -- user8
        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')
        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)

        self.assertTrue(role1.is_belong(user1))
        self.assertTrue(role1.is_belong(user2))
        self.assertTrue(role1.is_belong(user3))
        self.assertTrue(role1.is_belong(user4))
        self.assertTrue(role1.is_belong(user5))
        self.assertTrue(role1.is_belong(user6))
        self.assertTrue(role1.is_belong(user7))
        self.assertTrue(role1.is_belong(user8))

        self.assertFalse(role2.is_belong(user1))
        self.assertFalse(role2.is_belong(user2))
        self.assertTrue(role2.is_belong(user3))
        self.assertFalse(role2.is_belong(user4))
        self.assertFalse(role2.is_belong(user5))
        self.assertFalse(role2.is_belong(user6))
        self.assertFalse(role2.is_belong(user7))
        self.assertFalse(role2.is_belong(user8))

        self.assertFalse(role3.is_belong(user1))
        self.assertFalse(role3.is_belong(user2))
        self.assertFalse(role3.is_belong(user3))
        self.assertTrue(role3.is_belong(user4))
        self.assertTrue(role3.is_belong(user5))
        self.assertTrue(role3.is_belong(user6))
        self.assertTrue(role3.is_belong(user7))
        self.assertFalse(role3.is_belong(user8))
Esempio n. 27
0
    def test_filter_by_user(self):
        # role1           -- user1, user2
        #   +- role2      -- user3
        #   +- role3      -- user4, user5
        #   |    +- role4 -- user6
        #   |    +- role5 -- user7
        #   +- role6      -- user8
        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')
        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)

        self.assertItemsEqual(Role.objects.filter_by_user(user1), [
                role1,
            ])
        self.assertItemsEqual(Role.objects.filter_by_user(user2), [
                role1,
            ])
        self.assertItemsEqual(Role.objects.filter_by_user(user3), [
                role1, role2,
            ])
        self.assertItemsEqual(Role.objects.filter_by_user(user4), [
                role1, role3,
            ])
        self.assertItemsEqual(Role.objects.filter_by_user(user5), [
                role1, role3,
            ])
        self.assertItemsEqual(Role.objects.filter_by_user(user6), [
                role1, role3, role4,
            ])
        self.assertItemsEqual(Role.objects.filter_by_user(user7), [
                role1, role3, role5,
            ])
        self.assertItemsEqual(Role.objects.filter_by_user(user8), [
                role1, role6,
            ])
    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")
Esempio n. 30
0
    def test__get_all_users(self):
        # role1           -- user1, user2
        #   +- role2      -- user3
        #   +- role3      -- user4, user5
        #   |    +- role4 -- user6
        #   |    +- role5 -- user7
        #   +- role6      -- user8
        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')
        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)

        self.assertItemsEqual(role1.users, [
                user1, user2, user3, user4,
                user5, user6, user7, user8,
            ])
        self.assertItemsEqual(role2.users, [
                user3,
            ])
        self.assertItemsEqual(role3.users, [
                user4, user5, user6, user7,
            ])
        self.assertItemsEqual(role4.users, [
                user6,
            ])
        self.assertItemsEqual(role5.users, [
                user7,
            ])
        self.assertItemsEqual(role6.users, [
                user8,
            ])
Esempio n. 31
0
    def setUp(self):
        self.handler = LogicalPermissionHandler
        self.user = create_user('john')
        self.perm1 = 'permission.add_article'
        self.perm2 = 'permission.change_article'
        self.perm3 = 'permission.delete_article'
        self.article = create_article('test')

        from permission.logics import PermissionLogic
        from permission import add_permission_logic
        self.mock_logic1 = MagicMock(spec=PermissionLogic)
        self.mock_logic1.has_perm = MagicMock(return_value=False)
        self.mock_logic2 = MagicMock(spec=PermissionLogic)
        self.mock_logic2.has_perm = MagicMock(return_value=False)
        add_permission_logic(Article, self.mock_logic1)
        add_permission_logic(Article, self.mock_logic2)
Esempio n. 32
0
    def setUp(self):
        self.handler = LogicalPermissionHandler
        self.user = create_user('john')
        self.perm1 = 'permission.add_article'
        self.perm2 = 'permission.change_article'
        self.perm3 = 'permission.delete_article'
        self.article = create_article('test')

        from permission.logics import PermissionLogic
        from permission import add_permission_logic
        self.mock_logic1 = MagicMock(spec=PermissionLogic)
        self.mock_logic1.has_perm = MagicMock(return_value=False)
        self.mock_logic2 = MagicMock(spec=PermissionLogic)
        self.mock_logic2.has_perm = MagicMock(return_value=False)
        add_permission_logic(Article, self.mock_logic1)
        add_permission_logic(Article, self.mock_logic2)
    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")
Esempio n. 35
0
    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,
                              ])
Esempio n. 36
0
    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,
            ])