MatrixSynapse/tests/federation/test_federation.py

299 lines
8.9 KiB
Python
Raw Normal View History

# Copyright 2014 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2014-08-12 16:10:52 +02:00
# trial imports
from twisted.internet import defer
from tests import unittest
2014-08-12 16:10:52 +02:00
# python imports
from mock import Mock, ANY
2014-08-12 16:10:52 +02:00
2014-09-24 18:25:41 +02:00
from ..utils import MockHttpResource, MockClock, MockKey
2014-08-12 16:10:52 +02:00
from synapse.server import HomeServer
from synapse.federation import initialize_http_replication
2014-11-14 22:25:02 +01:00
from synapse.api.events import SynapseEvent
2014-08-12 16:10:52 +02:00
def make_pdu(prev_pdus=[], **kwargs):
"""Provide some default fields for making a PduTuple."""
pdu_fields = {
"is_state": False,
"unrecognized_keys": [],
"outlier": False,
"have_processed": True,
"state_key": None,
"power_level": None,
"prev_state_id": None,
"prev_state_origin": None,
}
pdu_fields.update(kwargs)
2014-11-14 22:25:02 +01:00
return SynapseEvent(prev_pdus=prev_pdus, **pdu_fields)
2014-08-12 16:10:52 +02:00
class FederationTestCase(unittest.TestCase):
def setUp(self):
self.mock_resource = MockHttpResource()
2014-08-12 16:10:52 +02:00
self.mock_http_client = Mock(spec=[
"get_json",
2014-08-12 16:10:52 +02:00
"put_json",
])
self.mock_persistence = Mock(spec=[
"prep_send_transaction",
"delivered_txn",
"get_received_txn_response",
"set_received_txn_response",
])
self.mock_persistence.get_received_txn_response.return_value = (
2014-11-04 17:15:30 +01:00
defer.succeed(None)
2014-08-12 16:10:52 +02:00
)
2014-09-24 18:25:41 +02:00
self.mock_config = Mock()
self.mock_config.signing_key = [MockKey()]
2014-08-12 16:10:52 +02:00
self.clock = MockClock()
2014-11-04 17:15:30 +01:00
hs = HomeServer(
"test",
resource_for_federation=self.mock_resource,
http_client=self.mock_http_client,
db_pool=None,
datastore=self.mock_persistence,
clock=self.clock,
config=self.mock_config,
keyring=Mock(),
2014-08-12 16:10:52 +02:00
)
self.federation = initialize_http_replication(hs)
self.distributor = hs.get_distributor()
@defer.inlineCallbacks
def test_get_state(self):
2014-11-04 17:15:30 +01:00
mock_handler = Mock(spec=[
"get_state_for_pdu",
])
self.federation.set_handler(mock_handler)
mock_handler.get_state_for_pdu.return_value = defer.succeed([])
2014-08-12 16:10:52 +02:00
# Empty context initially
2014-11-04 17:15:30 +01:00
(code, response) = yield self.mock_resource.trigger(
"GET",
"/_matrix/federation/v1/state/my-context/",
None
)
2014-08-12 16:10:52 +02:00
self.assertEquals(200, code)
self.assertFalse(response["pdus"])
# Now lets give the context some state
2014-11-04 17:15:30 +01:00
mock_handler.get_state_for_pdu.return_value = (
2014-08-12 16:10:52 +02:00
defer.succeed([
make_pdu(
2014-11-04 17:15:30 +01:00
event_id="the-pdu-id",
2014-08-12 16:10:52 +02:00
origin="red",
2014-11-04 17:15:30 +01:00
room_id="my-context",
type="m.topic",
origin_server_ts=123456789000,
2014-08-12 16:10:52 +02:00
depth=1,
2014-11-04 17:15:30 +01:00
content={"topic": "The topic"},
2014-08-12 16:10:52 +02:00
state_key="",
power_level=1000,
2014-11-04 17:15:30 +01:00
prev_state="last-pdu-id",
2014-08-12 16:10:52 +02:00
),
])
)
2014-11-04 17:15:30 +01:00
(code, response) = yield self.mock_resource.trigger(
"GET",
"/_matrix/federation/v1/state/my-context/",
None
)
2014-08-12 16:10:52 +02:00
self.assertEquals(200, code)
self.assertEquals(1, len(response["pdus"]))
@defer.inlineCallbacks
def test_get_pdu(self):
2014-11-04 17:15:30 +01:00
mock_handler = Mock(spec=[
"get_persisted_pdu",
])
self.federation.set_handler(mock_handler)
mock_handler.get_persisted_pdu.return_value = (
2014-08-12 16:10:52 +02:00
defer.succeed(None)
)
2014-11-04 17:15:30 +01:00
(code, response) = yield self.mock_resource.trigger(
"GET",
"/_matrix/federation/v1/event/abc123def456/",
None
)
2014-08-12 16:10:52 +02:00
self.assertEquals(404, code)
# Now insert such a PDU
2014-11-04 17:15:30 +01:00
mock_handler.get_persisted_pdu.return_value = (
2014-08-12 16:10:52 +02:00
defer.succeed(
make_pdu(
2014-11-04 17:15:30 +01:00
event_id="abc123def456",
2014-08-12 16:10:52 +02:00
origin="red",
2014-11-04 17:15:30 +01:00
room_id="my-context",
type="m.text",
origin_server_ts=123456789001,
2014-08-12 16:10:52 +02:00
depth=1,
2014-11-04 17:15:30 +01:00
content={"text": "Here is the message"},
2014-08-12 16:10:52 +02:00
)
)
)
2014-11-04 17:15:30 +01:00
(code, response) = yield self.mock_resource.trigger(
"GET",
"/_matrix/federation/v1/event/abc123def456/",
None
)
2014-08-12 16:10:52 +02:00
self.assertEquals(200, code)
self.assertEquals(1, len(response["pdus"]))
2014-11-04 17:15:30 +01:00
self.assertEquals("m.text", response["pdus"][0]["type"])
2014-08-12 16:10:52 +02:00
@defer.inlineCallbacks
def test_send_pdu(self):
self.mock_http_client.put_json.return_value = defer.succeed(
2014-11-04 17:15:30 +01:00
(200, "OK")
2014-08-12 16:10:52 +02:00
)
2014-11-14 22:25:02 +01:00
pdu = SynapseEvent(
2014-11-04 17:15:30 +01:00
event_id="abc123def456",
origin="red",
room_id="my-context",
type="m.text",
origin_server_ts=123456789001,
depth=1,
content={"text": "Here is the message"},
destinations=["remote"],
2014-08-12 16:10:52 +02:00
)
yield self.federation.send_pdu(pdu)
self.mock_http_client.put_json.assert_called_with(
2014-11-04 17:15:30 +01:00
"remote",
path="/_matrix/federation/v1/send/1000000/",
data={
"origin_server_ts": 1000000,
"origin": "test",
"pdus": [
2014-11-14 22:25:02 +01:00
pdu.get_pdu_json(),
2014-11-04 17:15:30 +01:00
],
'pdu_failures': [],
},
json_data_callback=ANY,
2014-08-12 16:10:52 +02:00
)
@defer.inlineCallbacks
def test_send_edu(self):
self.mock_http_client.put_json.return_value = defer.succeed(
2014-11-04 17:15:30 +01:00
(200, "OK")
2014-08-12 16:10:52 +02:00
)
yield self.federation.send_edu(
2014-11-04 17:15:30 +01:00
destination="remote",
edu_type="m.test",
content={"testing": "content here"},
2014-08-12 16:10:52 +02:00
)
# MockClock ensures we can guess these timestamps
self.mock_http_client.put_json.assert_called_with(
2014-11-04 17:15:30 +01:00
"remote",
path="/_matrix/federation/v1/send/1000000/",
data={
"origin": "test",
"origin_server_ts": 1000000,
"pdus": [],
"edus": [
{
"edu_type": "m.test",
"content": {"testing": "content here"},
}
],
'pdu_failures': [],
},
json_data_callback=ANY,
)
2014-08-12 16:10:52 +02:00
@defer.inlineCallbacks
def test_recv_edu(self):
recv_observer = Mock()
recv_observer.return_value = defer.succeed(())
self.federation.register_edu_handler("m.test", recv_observer)
2014-11-04 17:15:30 +01:00
yield self.mock_resource.trigger(
"PUT",
"/_matrix/federation/v1/send/1001000/",
"""{
"origin": "remote",
"origin_server_ts": 1001000,
"pdus": [],
"edus": [
{
"origin": "remote",
"destination": "test",
"edu_type": "m.test",
"content": {"testing": "reply here"}
}
]
}"""
)
2014-08-12 16:10:52 +02:00
recv_observer.assert_called_with(
2014-11-04 17:15:30 +01:00
"remote", {"testing": "reply here"}
2014-08-12 16:10:52 +02:00
)
@defer.inlineCallbacks
def test_send_query(self):
self.mock_http_client.get_json.return_value = defer.succeed(
{"your": "response"}
)
response = yield self.federation.make_query(
destination="remote",
query_type="a-question",
args={"one": "1", "two": "2"},
)
self.assertEquals({"your": "response"}, response)
self.mock_http_client.get_json.assert_called_with(
destination="remote",
path="/_matrix/federation/v1/query/a-question",
args={"one": "1", "two": "2"},
retry_on_dns_fail=True,
)
@defer.inlineCallbacks
def test_recv_query(self):
recv_handler = Mock()
recv_handler.return_value = defer.succeed({"another": "response"})
self.federation.register_query_handler("a-question", recv_handler)
2014-11-04 17:15:30 +01:00
code, response = yield self.mock_resource.trigger(
"GET",
"/_matrix/federation/v1/query/a-question?three=3&four=4",
None
)
self.assertEquals(200, code)
self.assertEquals({"another": "response"}, response)
recv_handler.assert_called_with(
{"three": "3", "four": "4"}
)