mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Fix up federation tests
This commit is contained in:
parent
a5a4ef3fd7
commit
24305ba5bf
@ -24,7 +24,6 @@ from ..utils import MockHttpResource, MockClock, MockKey
|
|||||||
from synapse.server import HomeServer
|
from synapse.server import HomeServer
|
||||||
from synapse.federation import initialize_http_replication
|
from synapse.federation import initialize_http_replication
|
||||||
from synapse.federation.units import Pdu
|
from synapse.federation.units import Pdu
|
||||||
from synapse.storage.pdu import PduTuple, PduEntry
|
|
||||||
|
|
||||||
|
|
||||||
def make_pdu(prev_pdus=[], **kwargs):
|
def make_pdu(prev_pdus=[], **kwargs):
|
||||||
@ -41,7 +40,7 @@ def make_pdu(prev_pdus=[], **kwargs):
|
|||||||
}
|
}
|
||||||
pdu_fields.update(kwargs)
|
pdu_fields.update(kwargs)
|
||||||
|
|
||||||
return PduTuple(PduEntry(**pdu_fields), prev_pdus, {}, {}, {})
|
return Pdu(prev_pdus=prev_pdus, **pdu_fields)
|
||||||
|
|
||||||
|
|
||||||
class FederationTestCase(unittest.TestCase):
|
class FederationTestCase(unittest.TestCase):
|
||||||
@ -52,10 +51,6 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
"put_json",
|
"put_json",
|
||||||
])
|
])
|
||||||
self.mock_persistence = Mock(spec=[
|
self.mock_persistence = Mock(spec=[
|
||||||
"get_current_state_for_context",
|
|
||||||
"get_pdu",
|
|
||||||
"persist_event",
|
|
||||||
"update_min_depth_for_context",
|
|
||||||
"prep_send_transaction",
|
"prep_send_transaction",
|
||||||
"delivered_txn",
|
"delivered_txn",
|
||||||
"get_received_txn_response",
|
"get_received_txn_response",
|
||||||
@ -67,7 +62,8 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
self.mock_config = Mock()
|
self.mock_config = Mock()
|
||||||
self.mock_config.signing_key = [MockKey()]
|
self.mock_config.signing_key = [MockKey()]
|
||||||
self.clock = MockClock()
|
self.clock = MockClock()
|
||||||
hs = HomeServer("test",
|
hs = HomeServer(
|
||||||
|
"test",
|
||||||
resource_for_federation=self.mock_resource,
|
resource_for_federation=self.mock_resource,
|
||||||
http_client=self.mock_http_client,
|
http_client=self.mock_http_client,
|
||||||
db_pool=None,
|
db_pool=None,
|
||||||
@ -81,71 +77,91 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_get_state(self):
|
def test_get_state(self):
|
||||||
self.mock_persistence.get_current_state_for_context.return_value = (
|
mock_handler = Mock(spec=[
|
||||||
defer.succeed([])
|
"get_state_for_pdu",
|
||||||
)
|
])
|
||||||
|
|
||||||
|
self.federation.set_handler(mock_handler)
|
||||||
|
|
||||||
|
mock_handler.get_state_for_pdu.return_value = defer.succeed([])
|
||||||
|
|
||||||
# Empty context initially
|
# Empty context initially
|
||||||
(code, response) = yield self.mock_resource.trigger("GET",
|
(code, response) = yield self.mock_resource.trigger(
|
||||||
"/_matrix/federation/v1/state/my-context/", None)
|
"GET",
|
||||||
|
"/_matrix/federation/v1/state/my-context/",
|
||||||
|
None
|
||||||
|
)
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
self.assertFalse(response["pdus"])
|
self.assertFalse(response["pdus"])
|
||||||
|
|
||||||
# Now lets give the context some state
|
# Now lets give the context some state
|
||||||
self.mock_persistence.get_current_state_for_context.return_value = (
|
mock_handler.get_state_for_pdu.return_value = (
|
||||||
defer.succeed([
|
defer.succeed([
|
||||||
make_pdu(
|
make_pdu(
|
||||||
pdu_id="the-pdu-id",
|
event_id="the-pdu-id",
|
||||||
origin="red",
|
origin="red",
|
||||||
context="my-context",
|
room_id="my-context",
|
||||||
pdu_type="m.topic",
|
type="m.topic",
|
||||||
ts=123456789000,
|
origin_server_ts=123456789000,
|
||||||
depth=1,
|
depth=1,
|
||||||
is_state=True,
|
content={"topic": "The topic"},
|
||||||
content_json='{"topic":"The topic"}',
|
|
||||||
state_key="",
|
state_key="",
|
||||||
power_level=1000,
|
power_level=1000,
|
||||||
prev_state_id="last-pdu-id",
|
prev_state="last-pdu-id",
|
||||||
prev_state_origin="blue",
|
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("GET",
|
(code, response) = yield self.mock_resource.trigger(
|
||||||
"/_matrix/federation/v1/state/my-context/", None)
|
"GET",
|
||||||
|
"/_matrix/federation/v1/state/my-context/",
|
||||||
|
None
|
||||||
|
)
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
self.assertEquals(1, len(response["pdus"]))
|
self.assertEquals(1, len(response["pdus"]))
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_get_pdu(self):
|
def test_get_pdu(self):
|
||||||
self.mock_persistence.get_pdu.return_value = (
|
mock_handler = Mock(spec=[
|
||||||
|
"get_persisted_pdu",
|
||||||
|
])
|
||||||
|
|
||||||
|
self.federation.set_handler(mock_handler)
|
||||||
|
|
||||||
|
mock_handler.get_persisted_pdu.return_value = (
|
||||||
defer.succeed(None)
|
defer.succeed(None)
|
||||||
)
|
)
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("GET",
|
(code, response) = yield self.mock_resource.trigger(
|
||||||
"/_matrix/federation/v1/pdu/red/abc123def456/", None)
|
"GET",
|
||||||
|
"/_matrix/federation/v1/event/abc123def456/",
|
||||||
|
None
|
||||||
|
)
|
||||||
self.assertEquals(404, code)
|
self.assertEquals(404, code)
|
||||||
|
|
||||||
# Now insert such a PDU
|
# Now insert such a PDU
|
||||||
self.mock_persistence.get_pdu.return_value = (
|
mock_handler.get_persisted_pdu.return_value = (
|
||||||
defer.succeed(
|
defer.succeed(
|
||||||
make_pdu(
|
make_pdu(
|
||||||
pdu_id="abc123def456",
|
event_id="abc123def456",
|
||||||
origin="red",
|
origin="red",
|
||||||
context="my-context",
|
room_id="my-context",
|
||||||
pdu_type="m.text",
|
type="m.text",
|
||||||
ts=123456789001,
|
origin_server_ts=123456789001,
|
||||||
depth=1,
|
depth=1,
|
||||||
content_json='{"text":"Here is the message"}',
|
content={"text": "Here is the message"},
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("GET",
|
(code, response) = yield self.mock_resource.trigger(
|
||||||
"/_matrix/federation/v1/pdu/red/abc123def456/", None)
|
"GET",
|
||||||
|
"/_matrix/federation/v1/event/abc123def456/",
|
||||||
|
None
|
||||||
|
)
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
self.assertEquals(1, len(response["pdus"]))
|
self.assertEquals(1, len(response["pdus"]))
|
||||||
self.assertEquals("m.text", response["pdus"][0]["pdu_type"])
|
self.assertEquals("m.text", response["pdus"][0]["type"])
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_send_pdu(self):
|
def test_send_pdu(self):
|
||||||
@ -154,14 +170,14 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
pdu = Pdu(
|
pdu = Pdu(
|
||||||
pdu_id="abc123def456",
|
event_id="abc123def456",
|
||||||
origin="red",
|
origin="red",
|
||||||
destinations=["remote"],
|
room_id="my-context",
|
||||||
context="my-context",
|
type="m.text",
|
||||||
origin_server_ts=123456789002,
|
origin_server_ts=123456789001,
|
||||||
pdu_type="m.test",
|
|
||||||
content={"testing": "content here"},
|
|
||||||
depth=1,
|
depth=1,
|
||||||
|
content={"text": "Here is the message"},
|
||||||
|
destinations=["remote"],
|
||||||
)
|
)
|
||||||
|
|
||||||
yield self.federation.send_pdu(pdu)
|
yield self.federation.send_pdu(pdu)
|
||||||
@ -173,20 +189,9 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
"origin_server_ts": 1000000,
|
"origin_server_ts": 1000000,
|
||||||
"origin": "test",
|
"origin": "test",
|
||||||
"pdus": [
|
"pdus": [
|
||||||
{
|
pdu.get_dict(),
|
||||||
"origin": "red",
|
],
|
||||||
"pdu_id": "abc123def456",
|
'pdu_failures': [],
|
||||||
"prev_pdus": [],
|
|
||||||
"origin_server_ts": 123456789002,
|
|
||||||
"context": "my-context",
|
|
||||||
"pdu_type": "m.test",
|
|
||||||
"is_state": False,
|
|
||||||
"content": {"testing": "content here"},
|
|
||||||
"depth": 1,
|
|
||||||
"hashes": {},
|
|
||||||
"signatures": {},
|
|
||||||
},
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
json_data_callback=ANY,
|
json_data_callback=ANY,
|
||||||
)
|
)
|
||||||
@ -220,11 +225,11 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
"content": {"testing": "content here"},
|
"content": {"testing": "content here"},
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
'pdu_failures': [],
|
||||||
},
|
},
|
||||||
json_data_callback=ANY,
|
json_data_callback=ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_recv_edu(self):
|
def test_recv_edu(self):
|
||||||
recv_observer = Mock()
|
recv_observer = Mock()
|
||||||
@ -232,7 +237,8 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
self.federation.register_edu_handler("m.test", recv_observer)
|
self.federation.register_edu_handler("m.test", recv_observer)
|
||||||
|
|
||||||
yield self.mock_resource.trigger("PUT",
|
yield self.mock_resource.trigger(
|
||||||
|
"PUT",
|
||||||
"/_matrix/federation/v1/send/1001000/",
|
"/_matrix/federation/v1/send/1001000/",
|
||||||
"""{
|
"""{
|
||||||
"origin": "remote",
|
"origin": "remote",
|
||||||
@ -246,7 +252,8 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
"content": {"testing": "reply here"}
|
"content": {"testing": "reply here"}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}""")
|
}"""
|
||||||
|
)
|
||||||
|
|
||||||
recv_observer.assert_called_with(
|
recv_observer.assert_called_with(
|
||||||
"remote", {"testing": "reply here"}
|
"remote", {"testing": "reply here"}
|
||||||
@ -280,8 +287,11 @@ class FederationTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
self.federation.register_query_handler("a-question", recv_handler)
|
self.federation.register_query_handler("a-question", recv_handler)
|
||||||
|
|
||||||
code, response = yield self.mock_resource.trigger("GET",
|
code, response = yield self.mock_resource.trigger(
|
||||||
"/_matrix/federation/v1/query/a-question?three=3&four=4", None)
|
"GET",
|
||||||
|
"/_matrix/federation/v1/query/a-question?three=3&four=4",
|
||||||
|
None
|
||||||
|
)
|
||||||
|
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
self.assertEquals({"another": "response"}, response)
|
self.assertEquals({"another": "response"}, response)
|
||||||
|
Loading…
Reference in New Issue
Block a user