Пример #1
0
    def test_has_perm_with_obj(self):
        perms = [
            'permission.add_article',
            'permission.change_article',
            'permission.delete_article',
        ]
        registry.get_handlers = MagicMock(return_value=[
            MagicMock(get_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=False)),
            MagicMock(get_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=True)),
        ])

        backend = PermissionBackend()
        self.assertFalse(registry.get_handlers.called)
        self.assertFalse(registry.get_handlers()[0].get_permissions.called)
        self.assertFalse(registry.get_handlers()[1].get_permissions.called)
        self.assertFalse(registry.get_handlers()[0].has_perm.called)
        self.assertFalse(registry.get_handlers()[1].has_perm.called)

        self.assertTrue(backend.has_perm(self.user, self.perm1, self.article))

        self.assertTrue(registry.get_handlers.called)
        self.assertTrue(registry.get_handlers()[0].get_permissions.called)
        self.assertTrue(registry.get_handlers()[1].get_permissions.called)
        self.assertTrue(registry.get_handlers()[0].has_perm.called)
        self.assertTrue(registry.get_handlers()[1].has_perm.called)
    def test_has_perm_with_obj(self):
        perms = [
            'permission.add_article',
            'permission.change_article',
            'permission.delete_article',
        ]
        registry.get_handlers = MagicMock(return_value=[
            MagicMock(get_supported_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=False)),
            MagicMock(get_supported_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=True)),
        ])

        backend = PermissionBackend()
        self.assertFalse(registry.get_handlers.called)
        self.assertFalse(registry.get_handlers()[0].get_supported_permissions.called)
        self.assertFalse(registry.get_handlers()[1].get_supported_permissions.called)
        self.assertFalse(registry.get_handlers()[0].has_perm.called)
        self.assertFalse(registry.get_handlers()[1].has_perm.called)

        self.assertTrue(backend.has_perm(self.user, self.perm1, self.article))

        self.assertTrue(registry.get_handlers.called)
        self.assertTrue(registry.get_handlers()[0].get_supported_permissions.called)
        self.assertTrue(registry.get_handlers()[1].get_supported_permissions.called)
        self.assertTrue(registry.get_handlers()[0].has_perm.called)
        self.assertTrue(registry.get_handlers()[1].has_perm.called)
Пример #3
0
    def test_has_perm_with_nil_permission(self):
        perms = [
            'permission.add_article',
            'permission.change_article',
            'permission.delete_article',
        ]
        registry.get_handlers = MagicMock(return_value=[
            MagicMock(get_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=False)),
            MagicMock(get_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=True)),
        ])

        backend = PermissionBackend()
        self.assertFalse(backend.has_perm(None, 'permissions.nil_permission'))
    def test_has_perm_with_nil_permission(self):
        perms = [
            'permission.add_article',
            'permission.change_article',
            'permission.delete_article',
        ]
        registry.get_handlers = MagicMock(return_value=[
            MagicMock(get_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=False)),
            MagicMock(get_permissions=MagicMock(return_value=perms),
                      has_perm=MagicMock(return_value=True)),
        ])

        backend = PermissionBackend()
        self.assertFalse(backend.has_perm(None, 'permissions.nil_permission'))
    def test_has_perm_get_permissions(self):
        class TestPermissionHandler2(TestPermissionHandler):
            # 'permission.add_article' is removed so the handler is not used
            # for treating that permission.
            def get_permissions(self):
                return set(['permission.change_article', 'permission.delete_article',])
        registry.register(Article, TestPermissionHandler2)

        backend = PermissionBackend()

        # the handler treat 'chage', 'delete' permissions
        permissions = registry._registry[Article].get_permissions()
        self.assertFalse('permission.add_article' in permissions)
        self.assertTrue('permission.change_article' in permissions)
        self.assertTrue('permission.delete_article' in permissions)

        # anonymous user have no permission
        self.assertFalse(backend.has_perm(self.anonymous, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.anonymous, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.anonymous, 'permission.delete_article', self.article1
            ))
        # superuser have all permission generally but in this backend, they 
        # don't. the permissions for superuser will handled by downstream.
        self.assertFalse(backend.has_perm(self.user1, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.user1, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user1, 'permission.delete_article', self.article1
            ))
        # staff user
        self.assertFalse(backend.has_perm(self.user2, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.user2, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user2, 'permission.delete_article', self.article1
            ))
        # user3 is an author of the article1 so have all permissions
        # but for article2
        self.assertFalse(backend.has_perm(self.user3, 'permission.add_article'))
        self.assertTrue(backend.has_perm(
                self.user3, 'permission.change_article', self.article1
            ))
        self.assertTrue(backend.has_perm(
                self.user3, 'permission.delete_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user3, 'permission.change_article', self.article2
            ))
        self.assertFalse(backend.has_perm(
                self.user3, 'permission.delete_article', self.article2
            ))
        # user4 is an author of the article2 so have all permissions
        # but for article1
        self.assertFalse(backend.has_perm(self.user4, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.user4, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user4, 'permission.delete_article', self.article1
            ))
        self.assertTrue(backend.has_perm(
                self.user4, 'permission.change_article', self.article2
            ))
        self.assertTrue(backend.has_perm(
                self.user4, 'permission.delete_article', self.article2
            ))
    def test_has_perm(self):
        registry.register(Article, TestPermissionHandler)

        backend = PermissionBackend()

        # the registered handler is used
        backend.has_perm(self.anonymous, 'permission.add_article')
        self.assertTrue(registry._registry[Article].called)

        # the handler treat 'add', 'chage', 'delete' permissions
        permissions = registry._registry[Article].get_permissions()
        self.assertTrue('permission.add_article' in permissions)
        self.assertTrue('permission.change_article' in permissions)
        self.assertTrue('permission.delete_article' in permissions)

        # anonymous user have no permission
        self.assertFalse(backend.has_perm(self.anonymous, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.anonymous, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.anonymous, 'permission.delete_article', self.article1
            ))
        # superuser have all permission generally but in this backend, they 
        # don't. the permissions for superuser will handled by downstream.
        self.assertTrue(backend.has_perm(self.user1, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.user1, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user1, 'permission.delete_article', self.article1
            ))
        # staff user
        self.assertTrue(backend.has_perm(self.user2, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.user2, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user2, 'permission.delete_article', self.article1
            ))
        # user3 is an author of the article1 so have all permissions
        # but for article2
        self.assertTrue(backend.has_perm(self.user3, 'permission.add_article'))
        self.assertTrue(backend.has_perm(
                self.user3, 'permission.change_article', self.article1
            ))
        self.assertTrue(backend.has_perm(
                self.user3, 'permission.delete_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user3, 'permission.change_article', self.article2
            ))
        self.assertFalse(backend.has_perm(
                self.user3, 'permission.delete_article', self.article2
            ))
        # user4 is an author of the article2 so have all permissions
        # but for article1
        self.assertTrue(backend.has_perm(self.user4, 'permission.add_article'))
        self.assertFalse(backend.has_perm(
                self.user4, 'permission.change_article', self.article1
            ))
        self.assertFalse(backend.has_perm(
                self.user4, 'permission.delete_article', self.article1
            ))
        self.assertTrue(backend.has_perm(
                self.user4, 'permission.change_article', self.article2
            ))
        self.assertTrue(backend.has_perm(
                self.user4, 'permission.delete_article', self.article2
            ))
Пример #7
0
    def test_has_perm_get_permissions(self):
        class TestPermissionHandler2(TestPermissionHandler):
            # 'permission.add_article' is removed so the handler is not used
            # for treating that permission.
            def get_permissions(self):
                return set([
                    'permission.change_article',
                    'permission.delete_article',
                ])

        registry.register(Article, TestPermissionHandler2)

        backend = PermissionBackend()

        # the handler treat 'chage', 'delete' permissions
        permissions = registry._registry[Article].get_permissions()
        self.assertFalse('permission.add_article' in permissions)
        self.assertTrue('permission.change_article' in permissions)
        self.assertTrue('permission.delete_article' in permissions)

        # anonymous user have no permission
        self.assertFalse(
            backend.has_perm(self.anonymous, 'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.anonymous, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.anonymous, 'permission.delete_article',
                             self.article1))
        # superuser have all permission generally but in this backend, they
        # don't. the permissions for superuser will handled by downstream.
        self.assertFalse(backend.has_perm(self.user1,
                                          'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.user1, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user1, 'permission.delete_article',
                             self.article1))
        # staff user
        self.assertFalse(backend.has_perm(self.user2,
                                          'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.user2, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user2, 'permission.delete_article',
                             self.article1))
        # user3 is an author of the article1 so have all permissions
        # but for article2
        self.assertFalse(backend.has_perm(self.user3,
                                          'permission.add_article'))
        self.assertTrue(
            backend.has_perm(self.user3, 'permission.change_article',
                             self.article1))
        self.assertTrue(
            backend.has_perm(self.user3, 'permission.delete_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user3, 'permission.change_article',
                             self.article2))
        self.assertFalse(
            backend.has_perm(self.user3, 'permission.delete_article',
                             self.article2))
        # user4 is an author of the article2 so have all permissions
        # but for article1
        self.assertFalse(backend.has_perm(self.user4,
                                          'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.user4, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user4, 'permission.delete_article',
                             self.article1))
        self.assertTrue(
            backend.has_perm(self.user4, 'permission.change_article',
                             self.article2))
        self.assertTrue(
            backend.has_perm(self.user4, 'permission.delete_article',
                             self.article2))
Пример #8
0
    def test_has_perm(self):
        registry.register(Article, TestPermissionHandler)

        backend = PermissionBackend()

        # the registered handler is used
        backend.has_perm(self.anonymous, 'permission.add_article')
        self.assertTrue(registry._registry[Article].called)

        # the handler treat 'add', 'chage', 'delete' permissions
        permissions = registry._registry[Article].get_permissions()
        self.assertTrue('permission.add_article' in permissions)
        self.assertTrue('permission.change_article' in permissions)
        self.assertTrue('permission.delete_article' in permissions)

        # anonymous user have no permission
        self.assertFalse(
            backend.has_perm(self.anonymous, 'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.anonymous, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.anonymous, 'permission.delete_article',
                             self.article1))
        # superuser have all permission generally but in this backend, they
        # don't. the permissions for superuser will handled by downstream.
        self.assertTrue(backend.has_perm(self.user1, 'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.user1, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user1, 'permission.delete_article',
                             self.article1))
        # staff user
        self.assertTrue(backend.has_perm(self.user2, 'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.user2, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user2, 'permission.delete_article',
                             self.article1))
        # user3 is an author of the article1 so have all permissions
        # but for article2
        self.assertTrue(backend.has_perm(self.user3, 'permission.add_article'))
        self.assertTrue(
            backend.has_perm(self.user3, 'permission.change_article',
                             self.article1))
        self.assertTrue(
            backend.has_perm(self.user3, 'permission.delete_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user3, 'permission.change_article',
                             self.article2))
        self.assertFalse(
            backend.has_perm(self.user3, 'permission.delete_article',
                             self.article2))
        # user4 is an author of the article2 so have all permissions
        # but for article1
        self.assertTrue(backend.has_perm(self.user4, 'permission.add_article'))
        self.assertFalse(
            backend.has_perm(self.user4, 'permission.change_article',
                             self.article1))
        self.assertFalse(
            backend.has_perm(self.user4, 'permission.delete_article',
                             self.article1))
        self.assertTrue(
            backend.has_perm(self.user4, 'permission.change_article',
                             self.article2))
        self.assertTrue(
            backend.has_perm(self.user4, 'permission.delete_article',
                             self.article2))