Skip to content

Commit

Permalink
Merge pull request #355 from OnroerendErfgoed/FIX_0_5_2_protected_res…
Browse files Browse the repository at this point in the history
…ource_event

update protected resource event
  • Loading branch information
BartSaelen authored Oct 7, 2016
2 parents 819ed03 + b6d0680 commit 1d914a2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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)
------------------

Expand Down
18 changes: 15 additions & 3 deletions atramhasis/protected_resources.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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):
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
6 changes: 3 additions & 3 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down Expand Up @@ -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"]
})

Expand Down
18 changes: 15 additions & 3 deletions tests/test_protected_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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']
Expand All @@ -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)
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)

0 comments on commit 1d914a2

Please sign in to comment.