test_federation.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright 2014 OpenMarket Ltd
  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. from twisted.internet import defer
  15. from tests import unittest
  16. from synapse.api.constants import EventTypes
  17. from synapse.events import FrozenEvent
  18. from synapse.handlers.federation import FederationHandler
  19. from mock import NonCallableMock, ANY, Mock
  20. from ..utils import setup_test_homeserver
  21. class FederationTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def setUp(self):
  24. self.state_handler = NonCallableMock(spec_set=[
  25. "compute_event_context",
  26. ])
  27. self.auth = NonCallableMock(spec_set=[
  28. "check",
  29. "check_host_in_room",
  30. ])
  31. self.hostname = "test"
  32. hs = yield setup_test_homeserver(
  33. self.hostname,
  34. datastore=NonCallableMock(spec_set=[
  35. "persist_event",
  36. "store_room",
  37. "get_room",
  38. "get_destination_retry_timings",
  39. "set_destination_retry_timings",
  40. "have_events",
  41. ]),
  42. resource_for_federation=NonCallableMock(),
  43. http_client=NonCallableMock(spec_set=[]),
  44. notifier=NonCallableMock(spec_set=["on_new_room_event"]),
  45. handlers=NonCallableMock(spec_set=[
  46. "room_member_handler",
  47. "federation_handler",
  48. ]),
  49. auth=self.auth,
  50. state_handler=self.state_handler,
  51. keyring=Mock(),
  52. )
  53. self.datastore = hs.get_datastore()
  54. self.handlers = hs.get_handlers()
  55. self.notifier = hs.get_notifier()
  56. self.hs = hs
  57. self.handlers.federation_handler = FederationHandler(self.hs)
  58. @defer.inlineCallbacks
  59. def test_msg(self):
  60. pdu = FrozenEvent({
  61. "type": EventTypes.Message,
  62. "room_id": "foo",
  63. "content": {"msgtype": u"fooo"},
  64. "origin_server_ts": 0,
  65. "event_id": "$a:b",
  66. "user_id":"@a:b",
  67. "origin": "b",
  68. "auth_events": [],
  69. "hashes": {"sha256":"AcLrgtUIqqwaGoHhrEvYG1YLDIsVPYJdSRGhkp3jJp8"},
  70. })
  71. self.datastore.persist_event.return_value = defer.succeed(None)
  72. self.datastore.get_room.return_value = defer.succeed(True)
  73. self.auth.check_host_in_room.return_value = defer.succeed(True)
  74. def have_events(event_ids):
  75. return defer.succeed({})
  76. self.datastore.have_events.side_effect = have_events
  77. def annotate(ev, old_state=None):
  78. context = Mock()
  79. context.current_state = {}
  80. context.auth_events = {}
  81. return defer.succeed(context)
  82. self.state_handler.compute_event_context.side_effect = annotate
  83. yield self.handlers.federation_handler.on_receive_pdu(
  84. "fo", pdu, False
  85. )
  86. self.datastore.persist_event.assert_called_once_with(
  87. ANY,
  88. is_new_state=True,
  89. backfilled=False,
  90. current_state=None,
  91. context=ANY,
  92. )
  93. self.state_handler.compute_event_context.assert_called_once_with(
  94. ANY, old_state=None,
  95. )
  96. self.auth.check.assert_called_once_with(ANY, auth_events={})
  97. self.notifier.on_new_room_event.assert_called_once_with(
  98. ANY, extra_users=[]
  99. )