test_client.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. from synapse.api.room_versions import RoomVersions
  16. from synapse.federation.transport.client import SendJoinParser
  17. from tests.unittest import TestCase
  18. class SendJoinParserTestCase(TestCase):
  19. def test_two_writes(self) -> None:
  20. """Test that the parser can sensibly deserialise an input given in two slices."""
  21. parser = SendJoinParser(RoomVersions.V1, True)
  22. parent_event = {
  23. "content": {
  24. "see_room_version_spec": "The event format changes depending on the room version."
  25. },
  26. "event_id": "$authparent",
  27. "room_id": "!somewhere:example.org",
  28. "type": "m.room.minimal_pdu",
  29. }
  30. state = {
  31. "content": {
  32. "see_room_version_spec": "The event format changes depending on the room version."
  33. },
  34. "event_id": "$DoNotThinkAboutTheEvent",
  35. "room_id": "!somewhere:example.org",
  36. "type": "m.room.minimal_pdu",
  37. }
  38. response = [
  39. 200,
  40. {
  41. "auth_chain": [parent_event],
  42. "origin": "matrix.org",
  43. "state": [state],
  44. },
  45. ]
  46. serialised_response = json.dumps(response).encode()
  47. # Send data to the parser
  48. parser.write(serialised_response[:100])
  49. parser.write(serialised_response[100:])
  50. # Retrieve the parsed SendJoinResponse
  51. parsed_response = parser.finish()
  52. # Sanity check the parsing gave us sensible data.
  53. self.assertEqual(len(parsed_response.auth_events), 1, parsed_response)
  54. self.assertEqual(len(parsed_response.state), 1, parsed_response)
  55. self.assertEqual(parsed_response.event_dict, {}, parsed_response)
  56. self.assertIsNone(parsed_response.event, parsed_response)
  57. self.assertFalse(parsed_response.partial_state, parsed_response)
  58. self.assertEqual(parsed_response.servers_in_room, None, parsed_response)
  59. def test_partial_state(self) -> None:
  60. """Check that the partial_state flag is correctly parsed"""
  61. parser = SendJoinParser(RoomVersions.V1, False)
  62. response = {
  63. "org.matrix.msc3706.partial_state": True,
  64. }
  65. serialised_response = json.dumps(response).encode()
  66. # Send data to the parser
  67. parser.write(serialised_response)
  68. # Retrieve and check the parsed SendJoinResponse
  69. parsed_response = parser.finish()
  70. self.assertTrue(parsed_response.partial_state)
  71. def test_servers_in_room(self) -> None:
  72. """Check that the servers_in_room field is correctly parsed"""
  73. parser = SendJoinParser(RoomVersions.V1, False)
  74. response = {"org.matrix.msc3706.servers_in_room": ["hs1", "hs2"]}
  75. serialised_response = json.dumps(response).encode()
  76. # Send data to the parser
  77. parser.write(serialised_response)
  78. # Retrieve and check the parsed SendJoinResponse
  79. parsed_response = parser.finish()
  80. self.assertEqual(parsed_response.servers_in_room, ["hs1", "hs2"])