test_federation_client.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright 2022 Matrix.org Federation 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 unittest import mock
  16. import twisted.web.client
  17. from twisted.internet import defer
  18. from twisted.internet.protocol import Protocol
  19. from twisted.python.failure import Failure
  20. from twisted.test.proto_helpers import MemoryReactor
  21. from synapse.api.room_versions import RoomVersions
  22. from synapse.server import HomeServer
  23. from synapse.types import JsonDict
  24. from synapse.util import Clock
  25. from tests.unittest import FederatingHomeserverTestCase
  26. class FederationClientTest(FederatingHomeserverTestCase):
  27. def prepare(self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer):
  28. super().prepare(reactor, clock, homeserver)
  29. # mock out the Agent used by the federation client, which is easier than
  30. # catching the HTTPS connection and do the TLS stuff.
  31. self._mock_agent = mock.create_autospec(twisted.web.client.Agent, spec_set=True)
  32. homeserver.get_federation_http_client().agent = self._mock_agent
  33. def test_get_room_state(self):
  34. creator = f"@creator:{self.OTHER_SERVER_NAME}"
  35. test_room_id = "!room_id"
  36. # mock up some events to use in the response.
  37. # In real life, these would have things in `prev_events` and `auth_events`, but that's
  38. # a bit annoying to mock up, and the code under test doesn't care, so we don't bother.
  39. create_event_dict = self.add_hashes_and_signatures(
  40. {
  41. "room_id": test_room_id,
  42. "type": "m.room.create",
  43. "state_key": "",
  44. "sender": creator,
  45. "content": {"creator": creator},
  46. "prev_events": [],
  47. "auth_events": [],
  48. "origin_server_ts": 500,
  49. }
  50. )
  51. member_event_dict = self.add_hashes_and_signatures(
  52. {
  53. "room_id": test_room_id,
  54. "type": "m.room.member",
  55. "sender": creator,
  56. "state_key": creator,
  57. "content": {"membership": "join"},
  58. "prev_events": [],
  59. "auth_events": [],
  60. "origin_server_ts": 600,
  61. }
  62. )
  63. pl_event_dict = self.add_hashes_and_signatures(
  64. {
  65. "room_id": test_room_id,
  66. "type": "m.room.power_levels",
  67. "sender": creator,
  68. "state_key": "",
  69. "content": {},
  70. "prev_events": [],
  71. "auth_events": [],
  72. "origin_server_ts": 700,
  73. }
  74. )
  75. # mock up the response, and have the agent return it
  76. self._mock_agent.request.side_effect = lambda *args, **kwargs: defer.succeed(
  77. _mock_response(
  78. {
  79. "pdus": [
  80. create_event_dict,
  81. member_event_dict,
  82. pl_event_dict,
  83. ],
  84. "auth_chain": [
  85. create_event_dict,
  86. member_event_dict,
  87. ],
  88. }
  89. )
  90. )
  91. # now fire off the request
  92. state_resp, auth_resp = self.get_success(
  93. self.hs.get_federation_client().get_room_state(
  94. "yet_another_server",
  95. test_room_id,
  96. "event_id",
  97. RoomVersions.V9,
  98. )
  99. )
  100. # check the right call got made to the agent
  101. self._mock_agent.request.assert_called_once_with(
  102. b"GET",
  103. b"matrix://yet_another_server/_matrix/federation/v1/state/%21room_id?event_id=event_id",
  104. headers=mock.ANY,
  105. bodyProducer=None,
  106. )
  107. # ... and that the response is correct.
  108. # the auth_resp should be empty because all the events are also in state
  109. self.assertEqual(auth_resp, [])
  110. # all of the events should be returned in state_resp, though not necessarily
  111. # in the same order. We just check the type on the assumption that if the type
  112. # is right, so is the rest of the event.
  113. self.assertCountEqual(
  114. [e.type for e in state_resp],
  115. ["m.room.create", "m.room.member", "m.room.power_levels"],
  116. )
  117. def _mock_response(resp: JsonDict):
  118. body = json.dumps(resp).encode("utf-8")
  119. def deliver_body(p: Protocol):
  120. p.dataReceived(body)
  121. p.connectionLost(Failure(twisted.web.client.ResponseDone()))
  122. response = mock.Mock(
  123. code=200,
  124. phrase=b"OK",
  125. headers=twisted.web.client.Headers({"content-Type": ["application/json"]}),
  126. length=len(body),
  127. deliverBody=deliver_body,
  128. )
  129. mock.seal(response)
  130. return response