diff --git a/CHANGES.rst b/CHANGES.rst index 05238c72..10897177 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,10 @@ +0.5.2 (??-10-2016) +------------------ + +This minor release fixes a bug with the protected resource event. The event should +give the uri of a concept instead of the url path. In addition to the uri the request +is added to the event. + 0.5.1 (04-10-2016) ------------------ diff --git a/atramhasis/protected_resources.py b/atramhasis/protected_resources.py index f6321ef6..12b459d3 100644 --- a/atramhasis/protected_resources.py +++ b/atramhasis/protected_resources.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- +import re +from atramhasis.errors import ConceptSchemeNotFoundException + """ Thid module is used when blocking operations on a certain Concept or Collection that might be used in external applications. @@ -12,8 +15,9 @@ class ProtectedResourceEvent(object): Event triggered when calling a protected operation on a resource """ - def __init__(self, uri): + def __init__(self, uri, request): self.uri = uri + self.request = request def protected_operation(fn): @@ -23,8 +27,16 @@ def protected_operation(fn): """ def advice(parent_object, *args, **kw): - uri = parent_object.request.path_url - event = ProtectedResourceEvent(uri) + request = parent_object.request + url = request.path + match = re.compile('/conceptschemes/(\w+)/c/(\w+)').match(url) + scheme_id = match.group(1) + c_id = match.group(2) + provider = request.skos_registry.get_provider(scheme_id) + if not provider: + raise ConceptSchemeNotFoundException(scheme_id) + uri = provider.uri_generator.generate(id=c_id) + event = ProtectedResourceEvent(uri, request) parent_object.request.registry.notify(event) return fn(parent_object, *args, **kw) diff --git a/setup.py b/setup.py index 24e35a45..135c8634 100644 --- a/setup.py +++ b/setup.py @@ -115,7 +115,7 @@ def run(self): ] setup(name='atramhasis', - version='0.5.1', + version='0.5.2', description='A web based editor for thesauri adhering to the SKOS specification.', long_description=README + '\n\n' + CHANGES, classifiers=[ diff --git a/tests/test_functional.py b/tests/test_functional.py index 73c5bc8b..44c8a316 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -196,14 +196,14 @@ def tearDown(self): @staticmethod def mock_event_handler(event): - if event.uri == 'http://localhost/conceptschemes/GEOGRAPHY/c/9': + if event.uri == 'urn:x-vioe:geography:9': referenced_in = ['urn:someobject', 'http://test.test.org/object/2'] raise ProtectedResourceException('resource {0} is still in use, preventing operation'.format(event.uri), referenced_in) @staticmethod def mock_event_handler_provider_unavailable(event): - if event.uri == 'http://localhost/conceptschemes/GEOGRAPHY/c/55': + if event.uri == 'urn:x-vioe:geography:55': raise ProviderUnavailableException('test msg') @@ -503,7 +503,7 @@ def test_delete_protected_resource(self): self.assertIn('application/json', res.headers['Content-Type']) self.assertIsNotNone(res.json) self.assertEqual(res.json, { - "message": "resource http://localhost/conceptschemes/GEOGRAPHY/c/9 is still in use, preventing operation", + "message": "resource urn:x-vioe:geography:9 is still in use, preventing operation", "referenced_in": ["urn:someobject", "http://test.test.org/object/2"] }) diff --git a/tests/test_protected_resources.py b/tests/test_protected_resources.py index ff0ca731..bfacb5cc 100644 --- a/tests/test_protected_resources.py +++ b/tests/test_protected_resources.py @@ -5,13 +5,14 @@ from unittest.mock import Mock, MagicMock except ImportError: from mock import Mock, MagicMock, call # pragma: no cover +from atramhasis.errors import ConceptSchemeNotFoundException class DummyParent(object): def __init__(self): self.request = MagicMock() - self.request.path_url = 'http://localhost/conceptschemes/GEOGRAPHY/c/9' + self.request.path = '/conceptschemes/GEOGRAPHY/c/9' @protected_operation def protected_dummy(self): @@ -24,8 +25,9 @@ def setUp(self): pass def test_protected_resource_event(self): - event = ProtectedResourceEvent('urn:test') + event = ProtectedResourceEvent('urn:test', 'request') self.assertEqual('urn:test', event.uri) + self.assertEqual('request', event.request) def test_protected_resource_exception(self): referenced_in = ['urn:someobject', 'http://test.test.org/object/2'] @@ -35,8 +37,18 @@ def test_protected_resource_exception(self): def test_protected_event(self): dummy = DummyParent() + provider = MagicMock() + provider.uri_generator.generate = lambda id: 'urn:x-vioe:geography:9' + dummy.request.skos_registry.get_provider = lambda scheme_id: provider notify_mock = Mock() dummy.request.registry.notify = notify_mock dummy.protected_dummy() notify_call = notify_mock.mock_calls[0] - self.assertEqual('http://localhost/conceptschemes/GEOGRAPHY/c/9', notify_call[1][0].uri) \ No newline at end of file + self.assertEqual('urn:x-vioe:geography:9', notify_call[1][0].uri) + + def test_protected_event_error(self): + dummy = DummyParent() + dummy.request.skos_registry.get_provider = lambda scheme_id: None + notify_mock = Mock() + dummy.request.registry.notify = notify_mock + self.assertRaises(ConceptSchemeNotFoundException, dummy.protected_dummy)