From 776ad3e5e9cb082a3c7d205da29b64e946a8176c Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 29 Nov 2021 11:11:46 +0000 Subject: [PATCH] Add a test case for the SendJoinParser (#11441) This would have caught the bug #11438 introduced in #11217 and fixed in #11439. --- changelog.d/11441.misc | 1 + mypy.ini | 4 ++ tests/federation/transport/test_client.py | 50 +++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 changelog.d/11441.misc create mode 100644 tests/federation/transport/test_client.py diff --git a/changelog.d/11441.misc b/changelog.d/11441.misc new file mode 100644 index 000000000..1baef41d7 --- /dev/null +++ b/changelog.d/11441.misc @@ -0,0 +1 @@ +Fix a bug introduced in 1.47.0 where `send_join` could fail due to an outdated `ijson` version. \ No newline at end of file diff --git a/mypy.ini b/mypy.ini index eb3976e74..f3361c00b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -222,6 +222,10 @@ disallow_untyped_defs = True [mypy-tests.rest.client.test_directory] disallow_untyped_defs = True +[mypy-tests.federation.transport.test_client] +disallow_untyped_defs = True + + ;; Dependencies without annotations ;; Before ignoring a module, check to see if type stubs are available. ;; The `typeshed` project maintains stubs here: diff --git a/tests/federation/transport/test_client.py b/tests/federation/transport/test_client.py new file mode 100644 index 000000000..0b1915996 --- /dev/null +++ b/tests/federation/transport/test_client.py @@ -0,0 +1,50 @@ +import json + +from synapse.api.room_versions import RoomVersions +from synapse.federation.transport.client import SendJoinParser + +from tests.unittest import TestCase + + +class SendJoinParserTestCase(TestCase): + def test_two_writes(self) -> None: + """Test that the parser can sensibly deserialise an input given in two slices.""" + parser = SendJoinParser(RoomVersions.V1, True) + parent_event = { + "content": { + "see_room_version_spec": "The event format changes depending on the room version." + }, + "event_id": "$authparent", + "room_id": "!somewhere:example.org", + "type": "m.room.minimal_pdu", + } + state = { + "content": { + "see_room_version_spec": "The event format changes depending on the room version." + }, + "event_id": "$DoNotThinkAboutTheEvent", + "room_id": "!somewhere:example.org", + "type": "m.room.minimal_pdu", + } + response = [ + 200, + { + "auth_chain": [parent_event], + "origin": "matrix.org", + "state": [state], + }, + ] + serialised_response = json.dumps(response).encode() + + # Send data to the parser + parser.write(serialised_response[:100]) + parser.write(serialised_response[100:]) + + # Retrieve the parsed SendJoinResponse + parsed_response = parser.finish() + + # Sanity check the parsing gave us sensible data. + self.assertEqual(len(parsed_response.auth_events), 1, parsed_response) + self.assertEqual(len(parsed_response.state), 1, parsed_response) + self.assertEqual(parsed_response.event_dict, {}, parsed_response) + self.assertIsNone(parsed_response.event, parsed_response)