diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/federation/test_federation.py | 2 | ||||
-rw-r--r-- | tests/handlers/test_federation.py | 43 | ||||
-rw-r--r-- | tests/handlers/test_room.py | 50 | ||||
-rw-r--r-- | tests/rest/test_events.py | 5 | ||||
-rw-r--r-- | tests/rest/test_rooms.py | 12 | ||||
-rw-r--r-- | tests/test_state.py | 17 | ||||
-rw-r--r-- | tests/utils.py | 9 |
7 files changed, 91 insertions, 47 deletions
diff --git a/tests/federation/test_federation.py b/tests/federation/test_federation.py index 58590e4fcd..938b57bec9 100644 --- a/tests/federation/test_federation.py +++ b/tests/federation/test_federation.py @@ -58,7 +58,7 @@ class FederationTestCase(unittest.TestCase): self.mock_persistence = Mock(spec=[ "get_current_state_for_context", "get_pdu", - "persist_pdu", + "persist_event", "update_min_depth_for_context", "prep_send_transaction", "delivered_txn", diff --git a/tests/handlers/test_federation.py b/tests/handlers/test_federation.py index a92d825f49..5ad40e484c 100644 --- a/tests/handlers/test_federation.py +++ b/tests/handlers/test_federation.py @@ -22,8 +22,9 @@ from synapse.api.events.room import ( from synapse.api.constants import Membership from synapse.handlers.federation import FederationHandler from synapse.server import HomeServer +from synapse.federation.units import Pdu -from mock import NonCallableMock +from mock import NonCallableMock, ANY import logging @@ -60,37 +61,42 @@ class FederationTestCase(unittest.TestCase): @defer.inlineCallbacks def test_msg(self): - event = self.hs.get_event_factory().create_event( - etype=MessageEvent.TYPE, - msg_id="bob", - room_id="foo", + pdu = Pdu( + pdu_type=MessageEvent.TYPE, + context="foo", content={"msgtype": u"fooo"}, + ts=0, + pdu_id="a", + origin="b", ) store_id = "ASD" self.datastore.persist_event.return_value = defer.succeed(store_id) self.datastore.get_room.return_value = defer.succeed(True) - yield self.handlers.federation_handler.on_receive(event, False, False) + yield self.handlers.federation_handler.on_receive_pdu(pdu, False) - self.datastore.persist_event.assert_called_once_with(event, False) + self.datastore.persist_event.assert_called_once_with(ANY, False) self.notifier.on_new_room_event.assert_called_once_with( - event, store_id) + ANY, store_id) @defer.inlineCallbacks def test_invite_join_target_this(self): room_id = "foo" user_id = "@bob:red" - event = self.hs.get_event_factory().create_event( - etype=InviteJoinEvent.TYPE, + pdu = Pdu( + pdu_type=InviteJoinEvent.TYPE, user_id=user_id, target_host=self.hostname, - room_id=room_id, + context=room_id, content={}, + ts=0, + pdu_id="a", + origin="b", ) - yield self.handlers.federation_handler.on_receive(event, False, False) + yield self.handlers.federation_handler.on_receive_pdu(pdu, False) mem_handler = self.handlers.room_member_handler self.assertEquals(1, mem_handler.change_membership.call_count) @@ -107,15 +113,18 @@ class FederationTestCase(unittest.TestCase): room_id = "foo" user_id = "@bob:red" - event = self.hs.get_event_factory().create_event( - etype=InviteJoinEvent.TYPE, + pdu = Pdu( + pdu_type=InviteJoinEvent.TYPE, user_id=user_id, - target_user_id="@red:not%s" % self.hostname, - room_id=room_id, + state_key="@red:not%s" % self.hostname, + context=room_id, content={}, + ts=0, + pdu_id="a", + origin="b", ) - yield self.handlers.federation_handler.on_receive(event, False, False) + yield self.handlers.federation_handler.on_receive_pdu(pdu, False) mem_handler = self.handlers.room_member_handler self.assertEquals(0, mem_handler.change_membership.call_count) diff --git a/tests/handlers/test_room.py b/tests/handlers/test_room.py index fddab8f74f..a84dbcc471 100644 --- a/tests/handlers/test_room.py +++ b/tests/handlers/test_room.py @@ -45,6 +45,7 @@ class RoomMemberHandlerTestCase(unittest.TestCase): "get_room_member", "get_room", "store_room", + "snapshot_room", ]), resource_for_federation=NonCallableMock(), http_client=NonCallableMock(spec_set=[]), @@ -52,29 +53,36 @@ class RoomMemberHandlerTestCase(unittest.TestCase): handlers=NonCallableMock(spec_set=[ "room_member_handler", "profile_handler", + "federation_handler", ]), auth=NonCallableMock(spec_set=["check"]), - federation=NonCallableMock(spec_set=[ - "handle_new_event", - "get_state_for_room", - ]), state_handler=NonCallableMock(spec_set=["handle_new_event"]), ) + self.federation = NonCallableMock(spec_set=[ + "handle_new_event", + "get_state_for_room", + ]) + self.datastore = hs.get_datastore() self.handlers = hs.get_handlers() self.notifier = hs.get_notifier() - self.federation = hs.get_federation() self.state_handler = hs.get_state_handler() self.distributor = hs.get_distributor() self.hs = hs + self.handlers.federation_handler = self.federation + self.distributor.declare("collect_presencelike_data") self.handlers.room_member_handler = RoomMemberHandler(self.hs) self.handlers.profile_handler = ProfileHandler(self.hs) self.room_member_handler = self.handlers.room_member_handler + self.snapshot = Mock() + self.datastore.snapshot_room.return_value = self.snapshot + + @defer.inlineCallbacks def test_invite(self): room_id = "!foo:red" @@ -104,8 +112,12 @@ class RoomMemberHandlerTestCase(unittest.TestCase): # Actual invocation yield self.room_member_handler.change_membership(event) - self.state_handler.handle_new_event.assert_called_once_with(event) - self.federation.handle_new_event.assert_called_once_with(event) + self.state_handler.handle_new_event.assert_called_once_with( + event, self.snapshot, + ) + self.federation.handle_new_event.assert_called_once_with( + event, self.snapshot, + ) self.assertEquals( set(["blue", "red", "green"]), @@ -116,7 +128,8 @@ class RoomMemberHandlerTestCase(unittest.TestCase): event ) self.notifier.on_new_room_event.assert_called_once_with( - event, store_id) + event, store_id + ) self.assertFalse(self.datastore.get_room.called) self.assertFalse(self.datastore.store_room.called) @@ -148,6 +161,7 @@ class RoomMemberHandlerTestCase(unittest.TestCase): self.datastore.get_joined_hosts_for_room.side_effect = get_joined + store_id = "store_id_fooo" self.datastore.persist_event.return_value = defer.succeed(store_id) self.datastore.get_room.return_value = defer.succeed(1) # Not None. @@ -163,8 +177,12 @@ class RoomMemberHandlerTestCase(unittest.TestCase): # Actual invocation yield self.room_member_handler.change_membership(event) - self.state_handler.handle_new_event.assert_called_once_with(event) - self.federation.handle_new_event.assert_called_once_with(event) + self.state_handler.handle_new_event.assert_called_once_with( + event, self.snapshot + ) + self.federation.handle_new_event.assert_called_once_with( + event, self.snapshot + ) self.assertEquals( set(["red", "green"]), @@ -312,27 +330,31 @@ class RoomCreationTest(unittest.TestCase): db_pool=None, datastore=NonCallableMock(spec_set=[ "store_room", + "snapshot_room", ]), http_client=NonCallableMock(spec_set=[]), notifier=NonCallableMock(spec_set=["on_new_room_event"]), handlers=NonCallableMock(spec_set=[ "room_creation_handler", "room_member_handler", + "federation_handler", ]), auth=NonCallableMock(spec_set=["check"]), - federation=NonCallableMock(spec_set=[ - "handle_new_event", - ]), state_handler=NonCallableMock(spec_set=["handle_new_event"]), ) + self.federation = NonCallableMock(spec_set=[ + "handle_new_event", + ]) + self.datastore = hs.get_datastore() self.handlers = hs.get_handlers() self.notifier = hs.get_notifier() - self.federation = hs.get_federation() self.state_handler = hs.get_state_handler() self.hs = hs + self.handlers.federation_handler = self.federation + self.handlers.room_creation_handler = RoomCreationHandler(self.hs) self.room_creation_handler = self.handlers.room_creation_handler diff --git a/tests/rest/test_events.py b/tests/rest/test_events.py index 94ad8910e3..3099a24e8c 100644 --- a/tests/rest/test_events.py +++ b/tests/rest/test_events.py @@ -128,9 +128,9 @@ class EventStreamPermissionsTestCase(RestTestCase): "test", db_pool=None, http_client=None, - federation=Mock(), replication_layer=Mock(), state_handler=state_handler, + datastore=MemoryDataStore(), persistence_service=persistence_service, clock=Mock(spec=[ "call_later", @@ -139,9 +139,10 @@ class EventStreamPermissionsTestCase(RestTestCase): ]), ) + hs.get_handlers().federation_handler = Mock() + hs.get_clock().time_msec.return_value = 1000000 - hs.datastore = MemoryDataStore() synapse.rest.register.register_servlets(hs, self.mock_resource) synapse.rest.events.register_servlets(hs, self.mock_resource) synapse.rest.room.register_servlets(hs, self.mock_resource) diff --git a/tests/rest/test_rooms.py b/tests/rest/test_rooms.py index 589b434446..914dc28f53 100644 --- a/tests/rest/test_rooms.py +++ b/tests/rest/test_rooms.py @@ -54,12 +54,12 @@ class RoomPermissionsTestCase(RestTestCase): "test", db_pool=None, http_client=None, - federation=Mock(), datastore=MemoryDataStore(), replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, ) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): return hs.parse_userid(self.auth_user_id) @@ -401,12 +401,12 @@ class RoomsMemberListTestCase(RestTestCase): "test", db_pool=None, http_client=None, - federation=Mock(), datastore=MemoryDataStore(), replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, ) + hs.get_handlers().federation_handler = Mock() self.auth_user_id = self.user_id @@ -479,12 +479,12 @@ class RoomsCreateTestCase(RestTestCase): "test", db_pool=None, http_client=None, - federation=Mock(), datastore=MemoryDataStore(), replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, ) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): return hs.parse_userid(self.auth_user_id) @@ -569,12 +569,12 @@ class RoomTopicTestCase(RestTestCase): "test", db_pool=None, http_client=None, - federation=Mock(), datastore=MemoryDataStore(), replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, ) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): return hs.parse_userid(self.auth_user_id) @@ -672,12 +672,12 @@ class RoomMemberStateTestCase(RestTestCase): "test", db_pool=None, http_client=None, - federation=Mock(), datastore=MemoryDataStore(), replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, ) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): return hs.parse_userid(self.auth_user_id) @@ -797,12 +797,12 @@ class RoomMessagesTestCase(RestTestCase): "test", db_pool=None, http_client=None, - federation=Mock(), datastore=MemoryDataStore(), replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, ) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): return hs.parse_userid(self.auth_user_id) diff --git a/tests/test_state.py b/tests/test_state.py index e64d15a3a2..58fd0bf3be 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -243,21 +243,24 @@ class StateTestCase(unittest.TestCase): state_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 20) - tup = ("pdu_id", "origin.com", 5) - pdus = [tup] + snapshot = Mock() + snapshot.prev_state_pdu = state_pdu + event_id = "pdu_id@origin.com" - self.persistence.get_latest_pdus_in_context.return_value = pdus - self.persistence.get_current_state_pdu.return_value = state_pdu + def fill_out_prev_events(event): + event.prev_events = [event_id] + event.depth = 6 + snapshot.fill_out_prev_events = fill_out_prev_events - yield self.state.handle_new_event(event) + yield self.state.handle_new_event(event, snapshot) - self.assertLess(tup[2], event.depth) + self.assertLess(5, event.depth) self.assertEquals(1, len(event.prev_events)) prev_id = event.prev_events[0] - self.assertEqual(encode_event_id(tup[0], tup[1]), prev_id) + self.assertEqual(event_id, prev_id) self.assertEqual( encode_event_id(state_pdu.pdu_id, state_pdu.origin), diff --git a/tests/utils.py b/tests/utils.py index f40cbce51d..6666b06931 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -127,6 +127,15 @@ class MemoryDataStore(object): self.current_state = {} self.events = [] + class Snapshot(namedtuple("Snapshot", "room_id user_id membership_state")): + def fill_out_prev_events(self, event): + pass + + def snapshot_room(self, room_id, user_id, state_type=None, state_key=None): + return self.Snapshot( + room_id, user_id, self.get_room_member(user_id, room_id) + ) + def register(self, user_id, token, password_hash): if user_id in self.tokens_to_users.values(): raise StoreError(400, "User in use.") |