test_federation.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 twisted.trial import unittest
  16. from synapse.api.events.room import (
  17. InviteJoinEvent, MessageEvent, RoomMemberEvent
  18. )
  19. from synapse.api.constants import Membership
  20. from synapse.handlers.federation import FederationHandler
  21. from synapse.server import HomeServer
  22. from synapse.federation.units import Pdu
  23. from mock import NonCallableMock, ANY
  24. import logging
  25. from ..utils import get_mock_call_args
  26. logging.getLogger().addHandler(logging.NullHandler())
  27. class FederationTestCase(unittest.TestCase):
  28. def setUp(self):
  29. self.hostname = "test"
  30. hs = HomeServer(
  31. self.hostname,
  32. db_pool=None,
  33. datastore=NonCallableMock(spec_set=[
  34. "persist_event",
  35. "store_room",
  36. "get_room",
  37. ]),
  38. resource_for_federation=NonCallableMock(),
  39. http_client=NonCallableMock(spec_set=[]),
  40. notifier=NonCallableMock(spec_set=["on_new_room_event"]),
  41. handlers=NonCallableMock(spec_set=[
  42. "room_member_handler",
  43. "federation_handler",
  44. ]),
  45. )
  46. self.datastore = hs.get_datastore()
  47. self.handlers = hs.get_handlers()
  48. self.notifier = hs.get_notifier()
  49. self.hs = hs
  50. self.handlers.federation_handler = FederationHandler(self.hs)
  51. @defer.inlineCallbacks
  52. def test_msg(self):
  53. pdu = Pdu(
  54. pdu_type=MessageEvent.TYPE,
  55. context="foo",
  56. content={"msgtype": u"fooo"},
  57. ts=0,
  58. pdu_id="a",
  59. origin="b",
  60. )
  61. store_id = "ASD"
  62. self.datastore.persist_event.return_value = defer.succeed(store_id)
  63. self.datastore.get_room.return_value = defer.succeed(True)
  64. yield self.handlers.federation_handler.on_receive_pdu(pdu, False)
  65. self.datastore.persist_event.assert_called_once_with(ANY, False)
  66. self.notifier.on_new_room_event.assert_called_once_with(ANY)
  67. @defer.inlineCallbacks
  68. def test_invite_join_target_this(self):
  69. room_id = "foo"
  70. user_id = "@bob:red"
  71. pdu = Pdu(
  72. pdu_type=InviteJoinEvent.TYPE,
  73. user_id=user_id,
  74. target_host=self.hostname,
  75. context=room_id,
  76. content={},
  77. ts=0,
  78. pdu_id="a",
  79. origin="b",
  80. )
  81. yield self.handlers.federation_handler.on_receive_pdu(pdu, False)
  82. mem_handler = self.handlers.room_member_handler
  83. self.assertEquals(1, mem_handler.change_membership.call_count)
  84. call_args = get_mock_call_args(
  85. lambda event, do_auth: None,
  86. mem_handler.change_membership
  87. )
  88. self.assertEquals(True, call_args["do_auth"])
  89. new_event = call_args["event"]
  90. self.assertEquals(RoomMemberEvent.TYPE, new_event.type)
  91. self.assertEquals(room_id, new_event.room_id)
  92. self.assertEquals(user_id, new_event.state_key)
  93. self.assertEquals(Membership.JOIN, new_event.membership)
  94. @defer.inlineCallbacks
  95. def test_invite_join_target_other(self):
  96. room_id = "foo"
  97. user_id = "@bob:red"
  98. pdu = Pdu(
  99. pdu_type=InviteJoinEvent.TYPE,
  100. user_id=user_id,
  101. state_key="@red:not%s" % self.hostname,
  102. context=room_id,
  103. content={},
  104. ts=0,
  105. pdu_id="a",
  106. origin="b",
  107. )
  108. yield self.handlers.federation_handler.on_receive_pdu(pdu, False)
  109. mem_handler = self.handlers.room_member_handler
  110. self.assertEquals(0, mem_handler.change_membership.call_count)