diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fdd9b33977..09baf2f88c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,7 +6,10 @@ This document describes changes between each past release. 3.2.4 (unreleased) ================== -- Nothing changed yet. +**Bug fixes** + +- Fix bug where the resource events of a request targetting two groups/collection + from different buckets would be grouped together. 3.2.3 (2016-07-18) diff --git a/kinto/core/events.py b/kinto/core/events.py index 3a2de3afdd..1a962cd56f 100644 --- a/kinto/core/events.py +++ b/kinto/core/events.py @@ -131,7 +131,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. @@ -156,8 +157,8 @@ def notify_resource_event(request, timestamp, data, action, old=None): events = request.bound_data.setdefault("resource_events", OrderedDict()) resource_name = request.current_resource_name - # Add to impacted records or create new event. - group_by = resource_name + action.value + # Group events by resource and action. + group_by = resource_name + parent_id + action.value if group_by in events: already_impacted = events[group_by][2] diff --git a/kinto/core/resource/__init__.py b/kinto/core/resource/__init__.py index 40f600f94f..45b048327b 100644 --- a/kinto/core/resource/__init__.py +++ b/kinto/core/resource/__init__.py @@ -645,7 +645,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 84259b40d0..4c50b73a0f 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) @@ -352,6 +359,25 @@ 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): + # /mushrooms is a UserResource (see testapp.views), which means + # that parent_id depends on the authenticated user. + 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) + # Two different auth headers, thus two different parent_id: + self.assertEqual(len(self.events), 2) + def test_one_event_is_sent_per_action(self): body = { "defaults": {