def test_unregister(self):
        """Testing APIExtraDataAccessHook unregistration"""
        hook = APIExtraDataAccessHook(
            self.extension, self.resource,
            [(('public', ), ExtraDataAccessLevel.ACCESS_STATE_PUBLIC)])

        hook.shutdown()

        self.assertSetEqual(set(),
                            set(self.resource.extra_data_access_callbacks))
    def test_register(self):
        """Testing APIExtraDataAccessHook registration"""
        APIExtraDataAccessHook(
            self.extension, self.resource,
            [(('public', ), ExtraDataAccessLevel.ACCESS_STATE_PUBLIC)])

        self.assertNotEqual(list(self.resource.extra_data_access_callbacks),
                            [])
    def test_private_state_get(self):
        """Testing APIExtraDataAccessHook private state get"""
        APIExtraDataAccessHook(
            self.extension, self.resource,
            [(('private', ), ExtraDataAccessLevel.ACCESS_STATE_PRIVATE)])

        rsp = self.api_get(self.url,
                           expected_mimetype=self.resource.item_mimetype)

        self.assertNotIn('private', rsp['test']['extra_data'])
    def test_readonly_state_get(self):
        """Testing APIExtraDataAccessHook readonly state get"""
        APIExtraDataAccessHook(self.extension, self.resource, [
            (('readonly', ), ExtraDataAccessLevel.ACCESS_STATE_PUBLIC_READONLY)
        ])

        rsp = self.api_get(self.url,
                           expected_mimetype=self.resource.item_mimetype)

        self.assertIn('readonly', rsp['test']['extra_data'])
    def test_public_state_get(self):
        """Testing APIExtraDataAccessHook public state GET"""
        APIExtraDataAccessHook(
            self.extension, self.resource,
            [(('public', ), ExtraDataAccessLevel.ACCESS_STATE_PUBLIC)])

        rsp = self.api_get(self.url,
                           expected_mimetype=self.resource.item_mimetype)

        self.assertIn('public', rsp['test']['extra_data'])
    def test_register_hook_already_registered(self):
        """Testing APIExtraDataAccessHook registration with already
        registered callback
        """
        hook = APIExtraDataAccessHook(
            self.extension, self.resource,
            [(('public', ), ExtraDataAccessLevel.ACCESS_STATE_PUBLIC)])

        with self.assertRaises(AlreadyRegisteredError):
            hook.resource.extra_data_access_callbacks.register(
                hook.get_extra_data_state)

        self.assertNotEqual(set(),
                            set(self.resource.extra_data_access_callbacks))
    def test_private_state_put(self):
        """Testing APIExtraDataAccessHook private state put"""
        APIExtraDataAccessHook(
            self.extension, self.resource,
            [(('private', ), ExtraDataAccessLevel.ACCESS_STATE_PRIVATE)])

        original_value = self.resource.extra_data['private']
        modified_extra_fields = {
            'extra_data.private': 'modified',
        }

        rsp = self.api_put(self.url,
                           modified_extra_fields,
                           expected_mimetype=self.resource.item_mimetype)

        self.assertEqual(original_value, rsp['test']['private'])
    def test_readonly_state_put(self):
        """Testing APIExtraDataAccessHook readonly state put"""
        APIExtraDataAccessHook(self.extension, self.resource, [
            (('readonly', ), ExtraDataAccessLevel.ACCESS_STATE_PUBLIC_READONLY)
        ])

        original_value = self.resource.extra_data['readonly']
        modified_extra_fields = {
            'extra_data.readonly': 'modified',
        }

        rsp = self.api_put(self.url,
                           modified_extra_fields,
                           expected_mimetype=self.resource.item_mimetype)

        self.assertEqual(original_value, rsp['test']['readonly'])
    def test_public_state_put(self):
        """Testing APIExtraDataAccessHook public state PUT"""
        APIExtraDataAccessHook(
            self.extension, self.resource,
            [(('public', ), ExtraDataAccessLevel.ACCESS_STATE_PUBLIC)])

        modified_extra_fields = {
            'extra_data.public': 'modified',
        }

        rsp = self.api_put(self.url,
                           modified_extra_fields,
                           expected_mimetype=self.resource.item_mimetype)

        self.assertEqual(modified_extra_fields['extra_data.public'],
                         rsp['test']['public'])