1
0

test_message.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from typing import Tuple
  17. from synapse.api.constants import EventTypes
  18. from synapse.events import EventBase
  19. from synapse.events.snapshot import EventContext
  20. from synapse.rest import admin
  21. from synapse.rest.client.v1 import login, room
  22. from synapse.types import create_requester
  23. from synapse.util.stringutils import random_string
  24. from tests import unittest
  25. logger = logging.getLogger(__name__)
  26. class EventCreationTestCase(unittest.HomeserverTestCase):
  27. servlets = [
  28. admin.register_servlets,
  29. login.register_servlets,
  30. room.register_servlets,
  31. ]
  32. def prepare(self, reactor, clock, hs):
  33. self.handler = self.hs.get_event_creation_handler()
  34. self.persist_event_storage = self.hs.get_storage().persistence
  35. self.user_id = self.register_user("tester", "foobar")
  36. self.access_token = self.login("tester", "foobar")
  37. self.room_id = self.helper.create_room_as(self.user_id, tok=self.access_token)
  38. self.info = self.get_success(
  39. self.hs.get_datastore().get_user_by_access_token(self.access_token,)
  40. )
  41. self.token_id = self.info.token_id
  42. self.requester = create_requester(self.user_id, access_token_id=self.token_id)
  43. def _create_duplicate_event(self, txn_id: str) -> Tuple[EventBase, EventContext]:
  44. """Create a new event with the given transaction ID. All events produced
  45. by this method will be considered duplicates.
  46. """
  47. # We create a new event with a random body, as otherwise we'll produce
  48. # *exactly* the same event with the same hash, and so same event ID.
  49. return self.get_success(
  50. self.handler.create_event(
  51. self.requester,
  52. {
  53. "type": EventTypes.Message,
  54. "room_id": self.room_id,
  55. "sender": self.requester.user.to_string(),
  56. "content": {"msgtype": "m.text", "body": random_string(5)},
  57. },
  58. txn_id=txn_id,
  59. )
  60. )
  61. def test_duplicated_txn_id(self):
  62. """Test that attempting to handle/persist an event with a transaction ID
  63. that has already been persisted correctly returns the old event and does
  64. *not* produce duplicate messages.
  65. """
  66. txn_id = "something_suitably_random"
  67. event1, context = self._create_duplicate_event(txn_id)
  68. ret_event1 = self.get_success(
  69. self.handler.handle_new_client_event(self.requester, event1, context)
  70. )
  71. stream_id1 = ret_event1.internal_metadata.stream_ordering
  72. self.assertEqual(event1.event_id, ret_event1.event_id)
  73. event2, context = self._create_duplicate_event(txn_id)
  74. # We want to test that the deduplication at the persit event end works,
  75. # so we want to make sure we test with different events.
  76. self.assertNotEqual(event1.event_id, event2.event_id)
  77. ret_event2 = self.get_success(
  78. self.handler.handle_new_client_event(self.requester, event2, context)
  79. )
  80. stream_id2 = ret_event2.internal_metadata.stream_ordering
  81. # Assert that the returned values match those from the initial event
  82. # rather than the new one.
  83. self.assertEqual(ret_event1.event_id, ret_event2.event_id)
  84. self.assertEqual(stream_id1, stream_id2)
  85. # Let's test that calling `persist_event` directly also does the right
  86. # thing.
  87. event3, context = self._create_duplicate_event(txn_id)
  88. self.assertNotEqual(event1.event_id, event3.event_id)
  89. ret_event3, event_pos3, _ = self.get_success(
  90. self.persist_event_storage.persist_event(event3, context)
  91. )
  92. # Assert that the returned values match those from the initial event
  93. # rather than the new one.
  94. self.assertEqual(ret_event1.event_id, ret_event3.event_id)
  95. self.assertEqual(stream_id1, event_pos3.stream)
  96. # Let's test that calling `persist_events` directly also does the right
  97. # thing.
  98. event4, context = self._create_duplicate_event(txn_id)
  99. self.assertNotEqual(event1.event_id, event3.event_id)
  100. events, _ = self.get_success(
  101. self.persist_event_storage.persist_events([(event3, context)])
  102. )
  103. ret_event4 = events[0]
  104. # Assert that the returned values match those from the initial event
  105. # rather than the new one.
  106. self.assertEqual(ret_event1.event_id, ret_event4.event_id)
  107. def test_duplicated_txn_id_one_call(self):
  108. """Test that we correctly handle duplicates that we try and persist at
  109. the same time.
  110. """
  111. txn_id = "something_else_suitably_random"
  112. # Create two duplicate events to persist at the same time
  113. event1, context1 = self._create_duplicate_event(txn_id)
  114. event2, context2 = self._create_duplicate_event(txn_id)
  115. # Ensure their event IDs are different to start with
  116. self.assertNotEqual(event1.event_id, event2.event_id)
  117. events, _ = self.get_success(
  118. self.persist_event_storage.persist_events(
  119. [(event1, context1), (event2, context2)]
  120. )
  121. )
  122. # Check that we've deduplicated the events.
  123. self.assertEqual(len(events), 2)
  124. self.assertEqual(events[0].event_id, events[1].event_id)
  125. class ServerAclValidationTestCase(unittest.HomeserverTestCase):
  126. servlets = [
  127. admin.register_servlets,
  128. login.register_servlets,
  129. room.register_servlets,
  130. ]
  131. def prepare(self, reactor, clock, hs):
  132. self.user_id = self.register_user("tester", "foobar")
  133. self.access_token = self.login("tester", "foobar")
  134. self.room_id = self.helper.create_room_as(self.user_id, tok=self.access_token)
  135. def test_allow_server_acl(self):
  136. """Test that sending an ACL that blocks everyone but ourselves works.
  137. """
  138. self.helper.send_state(
  139. self.room_id,
  140. EventTypes.ServerACL,
  141. body={"allow": [self.hs.hostname]},
  142. tok=self.access_token,
  143. expect_code=200,
  144. )
  145. def test_deny_server_acl_block_outselves(self):
  146. """Test that sending an ACL that blocks ourselves does not work.
  147. """
  148. self.helper.send_state(
  149. self.room_id,
  150. EventTypes.ServerACL,
  151. body={},
  152. tok=self.access_token,
  153. expect_code=400,
  154. )
  155. def test_deny_redact_server_acl(self):
  156. """Test that attempting to redact an ACL is blocked.
  157. """
  158. body = self.helper.send_state(
  159. self.room_id,
  160. EventTypes.ServerACL,
  161. body={"allow": [self.hs.hostname]},
  162. tok=self.access_token,
  163. expect_code=200,
  164. )
  165. event_id = body["event_id"]
  166. # Redaction of event should fail.
  167. path = "/_matrix/client/r0/rooms/%s/redact/%s" % (self.room_id, event_id)
  168. request, channel = self.make_request(
  169. "POST", path, content={}, access_token=self.access_token
  170. )
  171. self.render(request)
  172. self.assertEqual(int(channel.result["code"]), 403)