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")
Beispiel #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.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.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
Beispiel #6
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)
Beispiel #7
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
Beispiel #8
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.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)
Beispiel #10
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')
 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.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')
 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')
Beispiel #15
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)
 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)
Beispiel #17
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 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)