Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Change strategy, use notify_resource_event
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Jul 21, 2016
1 parent 7302c37 commit bfe239b
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 67 deletions.
31 changes: 20 additions & 11 deletions kinto_signer/tests/test_updater.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import mock
import pytest

from kinto.core.storage import Filter
from kinto.core.storage.exceptions import UnicityError, RecordNotFoundError
from kinto.core.utils import COMPARISON
from kinto.tests.core.support import DummyRequest

from kinto_signer.updater import LocalUpdater
from .support import unittest
Expand All @@ -25,6 +27,11 @@ def setUp(self):
storage=self.storage,
permission=self.permission)

# Resource events are bypassed completely in this test suite.
patcher = mock.patch('kinto_signer.updater.build_request')
self.addCleanup(patcher.stop)
patcher.start()

def patch(self, obj, *args, **kwargs):
patcher = mock.patch.object(obj, *args, **kwargs)
self.addCleanup(patcher.stop)
Expand Down Expand Up @@ -82,7 +89,7 @@ def test_push_records_to_destination(self):
records = [{'id': idx, 'foo': 'bar %s' % idx} for idx in range(1, 4)]
self.patch(self.updater, 'get_source_records',
return_value=(records, '42'))
self.updater.push_records_to_destination()
self.updater.push_records_to_destination(DummyRequest())
assert self.storage.update.call_count == 3

def test_push_records_removes_deleted_records(self):
Expand All @@ -93,7 +100,7 @@ def test_push_records_removes_deleted_records(self):
for idx in range(3, 5)])
self.patch(self.updater, 'get_source_records',
return_value=(records, '42'))
self.updater.push_records_to_destination()
self.updater.push_records_to_destination(DummyRequest())
self.updater.get_source_records.assert_called_with(
1324, include_deleted=True)
assert self.storage.update.call_count == 2
Expand All @@ -111,22 +118,23 @@ def test_push_records_skip_already_deleted_records(self):
self.patch(self.updater, 'get_source_records',
return_value=(records, '42'))
# Calling the updater should not raise the RecordNotFoundError.
self.updater.push_records_to_destination()
self.updater.push_records_to_destination(DummyRequest())

def test_push_records_to_destination_with_no_destination_changes(self):
self.patch(self.updater, 'get_destination_last_modified',
return_value=(1324, 0))
records = [{'id': idx, 'foo': 'bar %s' % idx} for idx in range(1, 4)]
self.patch(self.updater, 'get_source_records',
return_value=(records, '42'))
self.updater.push_records_to_destination()
self.updater.push_records_to_destination(DummyRequest())
self.updater.get_source_records.assert_called_with(
None, include_deleted=True)
assert self.storage.update.call_count == 3

def test_set_destination_signature_modifies_the_source_collection(self):
self.storage.get.return_value = {'id': 1234, 'last_modified': 1234}
self.updater.set_destination_signature(mock.sentinel.signature)
self.updater.set_destination_signature(mock.sentinel.signature,
DummyRequest())

self.storage.update.assert_called_with(
collection_id='collection',
Expand All @@ -140,7 +148,7 @@ def test_set_destination_signature_modifies_the_source_collection(self):
def test_update_source_status_modifies_the_source_collection(self):
self.storage.get.return_value = {'id': 1234, 'last_modified': 1234,
'status': 'to-sign'}
self.updater.update_source_status("signed")
self.updater.update_source_status("signed", DummyRequest())

self.storage.update.assert_called_with(
collection_id='collection',
Expand All @@ -153,29 +161,30 @@ def test_update_source_status_modifies_the_source_collection(self):

def test_create_destination_updates_collection_permissions(self):
collection_id = '/buckets/destbucket/collections/destcollection'
self.updater.create_destination()
self.updater.create_destination(DummyRequest())
self.permission.replace_object_permissions.assert_called_with(
collection_id,
{"read": ("system.Everyone",)})

def test_create_destination_creates_bucket(self):
self.updater.create_destination()
self.updater.create_destination(DummyRequest())
self.storage.create.assert_any_call(
collection_id='bucket',
parent_id='',
record={"id": 'destbucket'})

def test_create_destination_creates_collection(self):
bucket_id = '/buckets/destbucket'
self.updater.create_destination()
self.updater.create_destination(DummyRequest())
self.storage.create.assert_any_call(
collection_id='collection',
parent_id=bucket_id,
record={"id": 'destcollection'})

def test_ensure_resource_exists_handles_uniticy_errors(self):
self.storage.create.side_effect = UnicityError('id', 'record')
self.updater._ensure_resource_exists('bucket', '', 'abcd')
self.updater._ensure_resource_exists('bucket', '', 'abcd',
DummyRequest())

def test_sign_and_update_destination(self):
records = [{'id': idx, 'foo': 'bar %s' % idx, 'last_modified': idx}
Expand All @@ -186,7 +195,7 @@ def test_sign_and_update_destination(self):
self.patch(self.updater, 'get_source_records', return_value=([], '0'))
self.patch(self.updater, 'push_records_to_destination')
self.patch(self.updater, 'set_destination_signature')
self.updater.sign_and_update_destination()
self.updater.sign_and_update_destination(DummyRequest())

assert self.updater.get_source_records.call_count == 1
assert self.updater.push_records_to_destination.call_count == 1
Expand Down
Loading

0 comments on commit bfe239b

Please sign in to comment.