Example #1
0
    def test_ProtectedProperties(self):
        # Protected properties checks against permission for properties
        context = BaseNode()

        # 'viewprotected' property gets protected by 'view' permission
        permissions = {
            'viewprotected': ['view'],
        }
        props = ProtectedProperties(context, permissions)

        # Setting properties works always
        props.viewprotected = True
        props.unprotected = True

        # Unauthorized just permits access to unprotected property
        self.layer.new_request()

        self.assertTrue(props.viewprotected is None)
        self.assertTrue(props.unprotected)

        self.assertFalse('viewprotected' in props)
        self.assertTrue('unprotected' in props)

        self.assertEqual(props.keys(), ['unprotected'])
        self.assertTrue(props.get('viewprotected') is None)
        self.assertTrue(props.get('unprotected'))

        err = self.expect_error(KeyError, lambda: props['viewprotected'])
        expected = '"No permission to access \'viewprotected\'"'
        self.assertEqual(str(err).strip('u'), expected)

        self.assertTrue(props['unprotected'])

        # Authenticate, both properties are now available
        with self.layer.authenticated('viewer'):
            self.layer.new_request()

            self.assertTrue(props['viewprotected'])
            self.assertTrue(props.viewprotected)
            self.assertTrue(props.unprotected)

            self.assertEqual(
                sorted(props.keys()),
                ['unprotected', 'viewprotected']
            )
            self.assertTrue(props.get('viewprotected'))
            self.assertTrue(props.get('unprotected'))

            props.viewprotected = False
            self.assertFalse(props.viewprotected)
Example #2
0
    def test_ProtectedProperties(self):
        # Protected properties checks against permission for properties
        context = BaseNode()

        # 'viewprotected' property gets protected by 'view' permission
        permissions = {
            'viewprotected': ['view'],
        }
        props = ProtectedProperties(context, permissions)

        # Setting properties works always
        props.viewprotected = True
        props.unprotected = True

        # Unauthorized just permits access to unprotected property
        self.layer.new_request()

        self.assertTrue(props.viewprotected is None)
        self.assertTrue(props.unprotected)

        self.assertFalse('viewprotected' in props)
        self.assertTrue('unprotected' in props)

        self.assertEqual(props.keys(), ['unprotected'])
        self.assertTrue(props.get('viewprotected') is None)
        self.assertTrue(props.get('unprotected'))

        err = self.expect_error(KeyError, lambda: props['viewprotected'])
        expected = '"No permission to access \'viewprotected\'"'
        self.assertEqual(str(err).strip('u'), expected)

        self.assertTrue(props['unprotected'])

        # Authenticate, both properties are now available
        with self.layer.authenticated('viewer'):
            self.layer.new_request()

            self.assertTrue(props['viewprotected'])
            self.assertTrue(props.viewprotected)
            self.assertTrue(props.unprotected)

            self.assertEqual(sorted(props.keys()),
                             ['unprotected', 'viewprotected'])
            self.assertTrue(props.get('viewprotected'))
            self.assertTrue(props.get('unprotected'))

            props.viewprotected = False
            self.assertFalse(props.viewprotected)
Example #3
0
    def test_ProtectedProperties(self):
        # Protected properties checks against permission for properties
        context = BaseNode()

        # 'viewprotected' property gets protected by 'view' permission
        permissions = {
            'viewprotected': ['view'],
        }
        props = ProtectedProperties(context, permissions)
        self.assertTrue(IProperties.providedBy(props))

        # Setting properties works always
        props.viewprotected = True
        props.unprotected = True

        # Unauthorized just permits access to unprotected property
        self.layer.new_request()

        self.assertTrue(props.viewprotected is None)
        self.assertTrue(props.unprotected)

        self.assertFalse('viewprotected' in props)
        self.assertTrue('unprotected' in props)

        self.assertEqual(props.keys(), ['unprotected'])
        self.assertTrue(props.get('viewprotected') is None)
        self.assertTrue(props.get('unprotected'))

        err = self.expect_error(KeyError, lambda: props['viewprotected'])
        expected = '"No permission to access \'viewprotected\'"'
        self.assertEqual(str(err).strip('u'), expected)

        self.assertTrue(props['unprotected'])

        # Authenticate, both properties are now available
        with self.layer.authenticated('viewer'):
            self.layer.new_request()

            self.assertTrue(props['viewprotected'])
            self.assertTrue(props.viewprotected)
            self.assertTrue(props.unprotected)

            self.assertEqual(
                sorted(props.keys()),
                ['unprotected', 'viewprotected']
            )
            self.assertTrue(props.get('viewprotected'))
            self.assertTrue(props.get('unprotected'))

            props.viewprotected = False
            self.assertFalse(props.viewprotected)

        # internal data
        self.assertEqual(
            o_getattr(props, '_data'),
            {'unprotected': True, 'viewprotected': False}
        )
        self.assertTrue(o_getattr(props, '_context') is context)
        self.assertEqual(o_getattr(props, '_permissions'), permissions)

        # ``__copy__``
        props_copy = copy.copy(props)
        self.assertFalse(props_copy is props)
        self.assertEqual(props_copy.__class__, ProtectedProperties)
        self.assertEqual(
            o_getattr(props_copy, '_data'),
            o_getattr(props, '_data')
        )
        self.assertTrue(o_getattr(props, '_context') is context)
        self.assertEqual(o_getattr(props, '_permissions'), permissions)

        # ``__deepcopy__``
        props_deepcopy = copy.deepcopy(props)
        self.assertFalse(props_deepcopy is props)
        self.assertEqual(props_deepcopy.__class__, ProtectedProperties)
        self.assertEqual(
            o_getattr(props_deepcopy, '_data'),
            o_getattr(props, '_data')
        )
        self.assertTrue(o_getattr(props, '_context') is context)
        self.assertEqual(o_getattr(props, '_permissions'), permissions)