From d665c2e772ee2ea2c5f7531adee0fb6ff3c5557f Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 20 Jul 2016 22:09:07 +0200 Subject: [PATCH 1/4] Add resource event textual representation --- kinto/core/events.py | 5 +++++ kinto/tests/core/resource/test_events.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/kinto/core/events.py b/kinto/core/events.py index 2e95f3e85..429748a42 100644 --- a/kinto/core/events.py +++ b/kinto/core/events.py @@ -24,6 +24,11 @@ def __init__(self, payload, request): self.payload = payload self.request = request + def __repr__(self): + return "<{klass} action={action} uri={uri}>".format( + klass=self.__class__.__name__, + **self.payload) + class ResourceRead(_ResourceEvent): """Triggered when a resource is being read. diff --git a/kinto/tests/core/resource/test_events.py b/kinto/tests/core/resource/test_events.py index 84259b40d..bd30ffc7a 100644 --- a/kinto/tests/core/resource/test_events.py +++ b/kinto/tests/core/resource/test_events.py @@ -93,6 +93,13 @@ class ResourceChangedTest(BaseEventTest, unittest.TestCase): subscribed = (ResourceChanged,) + def test_events_have_custom_representation(self): + self.app.post_json(self.collection_url, self.body, + headers=self.headers, status=201) + self.assertEqual(repr(self.events[0]), + "" % self.collection_url) + def test_post_sends_create_action(self): self.app.post_json(self.collection_url, self.body, headers=self.headers, status=201) From 7f5c2f199eb68f34b806b57849247f9ea69f3f2c Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 20 Jul 2016 22:12:28 +0200 Subject: [PATCH 2/4] Group events by parent id --- kinto/core/events.py | 6 ++++-- kinto/core/resource/__init__.py | 4 +++- kinto/tests/core/resource/test_events.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/kinto/core/events.py b/kinto/core/events.py index 429748a42..047248629 100644 --- a/kinto/core/events.py +++ b/kinto/core/events.py @@ -123,7 +123,8 @@ def get_resource_events(request, after_commit=False): return events -def notify_resource_event(request, timestamp, data, action, old=None): +def notify_resource_event(request, parent_id, timestamp, data, action, + old=None): """ Request helper to stack a resource event. @@ -146,10 +147,11 @@ def notify_resource_event(request, timestamp, data, action, old=None): # Get previously triggered events. events = request.bound_data.setdefault("resource_events", OrderedDict()) + resource_name = request.current_resource_name # Group events by resource and action. - group_by = resource_name + action.value + group_by = resource_name + parent_id + action.value if group_by in events: # Add to impacted records of existing event. diff --git a/kinto/core/resource/__init__.py b/kinto/core/resource/__init__.py index d9e269397..751e8be0c 100644 --- a/kinto/core/resource/__init__.py +++ b/kinto/core/resource/__init__.py @@ -653,7 +653,9 @@ def postprocess(self, result, action=ACTIONS.READ, old=None): 'data': result } - self.request.notify_resource_event(timestamp=self.timestamp, + parent_id = self.get_parent_id(self.request) + self.request.notify_resource_event(parent_id=parent_id, + timestamp=self.timestamp, data=result, action=action, old=old) diff --git a/kinto/tests/core/resource/test_events.py b/kinto/tests/core/resource/test_events.py index bd30ffc7a..7695b294a 100644 --- a/kinto/tests/core/resource/test_events.py +++ b/kinto/tests/core/resource/test_events.py @@ -359,6 +359,22 @@ def test_one_event_is_sent_per_resource(self): self.app.post_json("/batch", body, headers=self.headers) self.assertEqual(len(self.events), 2) + def test_one_event_is_sent_per_parent_id(self): + body = { + "defaults": { + "path": '/mushrooms', + "method": "POST", + "body": self.body + }, + "requests": [ + {"headers": {"Authorization": "Basic bWF0OjE="}}, + {"headers": {"Authorization": "Basic dG90bzp0dXR1"}}, + {"headers": {"Authorization": "Basic bWF0OjE="}}, + ] + } + self.app.post_json("/batch", body, headers=self.headers) + self.assertEqual(len(self.events), 2) + def test_one_event_is_sent_per_action(self): body = { "defaults": { From 18ace475b7d49f572c73567bec4fbe903fb7b365 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 20 Jul 2016 22:14:08 +0200 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f5f68a3bc..1083739c1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,8 @@ This document describes changes between each past release. **Bug fixes** - Fix Redis get_accessible_object implementation (#725) +- Fix bug where the resource events of a request targetting two groups/collection + from different buckets would be grouped together. 3.3.1 (2016-07-19) From 5d591a229fd2074873a813d134c4f200d2830a3d Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Thu, 21 Jul 2016 10:44:19 +0200 Subject: [PATCH 4/4] @Natim review --- kinto/tests/core/resource/test_events.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kinto/tests/core/resource/test_events.py b/kinto/tests/core/resource/test_events.py index 7695b294a..4c50b73a0 100644 --- a/kinto/tests/core/resource/test_events.py +++ b/kinto/tests/core/resource/test_events.py @@ -360,6 +360,8 @@ def test_one_event_is_sent_per_resource(self): self.assertEqual(len(self.events), 2) def test_one_event_is_sent_per_parent_id(self): + # /mushrooms is a UserResource (see testapp.views), which means + # that parent_id depends on the authenticated user. body = { "defaults": { "path": '/mushrooms', @@ -373,6 +375,7 @@ def test_one_event_is_sent_per_parent_id(self): ] } self.app.post_json("/batch", body, headers=self.headers) + # Two different auth headers, thus two different parent_id: self.assertEqual(len(self.events), 2) def test_one_event_is_sent_per_action(self):