test_redaction.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from mock import Mock
  17. from synapse.api.constants import EventTypes, Membership
  18. from synapse.api.room_versions import RoomVersions
  19. from synapse.types import RoomID, UserID
  20. from tests import unittest
  21. from tests.utils import create_room
  22. class RedactionTestCase(unittest.HomeserverTestCase):
  23. def make_homeserver(self, reactor, clock):
  24. return self.setup_test_homeserver(
  25. resource_for_federation=Mock(), http_client=None
  26. )
  27. def prepare(self, reactor, clock, hs):
  28. self.store = hs.get_datastore()
  29. self.event_builder_factory = hs.get_event_builder_factory()
  30. self.event_creation_handler = hs.get_event_creation_handler()
  31. self.u_alice = UserID.from_string("@alice:test")
  32. self.u_bob = UserID.from_string("@bob:test")
  33. self.room1 = RoomID.from_string("!abc123:test")
  34. self.get_success(
  35. create_room(hs, self.room1.to_string(), self.u_alice.to_string())
  36. )
  37. self.depth = 1
  38. def inject_room_member(
  39. self, room, user, membership, replaces_state=None, extra_content={}
  40. ):
  41. content = {"membership": membership}
  42. content.update(extra_content)
  43. builder = self.event_builder_factory.for_room_version(
  44. RoomVersions.V1,
  45. {
  46. "type": EventTypes.Member,
  47. "sender": user.to_string(),
  48. "state_key": user.to_string(),
  49. "room_id": room.to_string(),
  50. "content": content,
  51. },
  52. )
  53. event, context = self.get_success(
  54. self.event_creation_handler.create_new_client_event(builder)
  55. )
  56. self.get_success(self.store.persist_event(event, context))
  57. return event
  58. def inject_message(self, room, user, body):
  59. self.depth += 1
  60. builder = self.event_builder_factory.for_room_version(
  61. RoomVersions.V1,
  62. {
  63. "type": EventTypes.Message,
  64. "sender": user.to_string(),
  65. "state_key": user.to_string(),
  66. "room_id": room.to_string(),
  67. "content": {"body": body, "msgtype": "message"},
  68. },
  69. )
  70. event, context = self.get_success(
  71. self.event_creation_handler.create_new_client_event(builder)
  72. )
  73. self.get_success(self.store.persist_event(event, context))
  74. return event
  75. def inject_redaction(self, room, event_id, user, reason):
  76. builder = self.event_builder_factory.for_room_version(
  77. RoomVersions.V1,
  78. {
  79. "type": EventTypes.Redaction,
  80. "sender": user.to_string(),
  81. "state_key": user.to_string(),
  82. "room_id": room.to_string(),
  83. "content": {"reason": reason},
  84. "redacts": event_id,
  85. },
  86. )
  87. event, context = self.get_success(
  88. self.event_creation_handler.create_new_client_event(builder)
  89. )
  90. self.get_success(self.store.persist_event(event, context))
  91. def test_redact(self):
  92. self.get_success(
  93. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  94. )
  95. msg_event = self.get_success(self.inject_message(self.room1, self.u_alice, "t"))
  96. # Check event has not been redacted:
  97. event = self.get_success(self.store.get_event(msg_event.event_id))
  98. self.assertObjectHasAttributes(
  99. {
  100. "type": EventTypes.Message,
  101. "user_id": self.u_alice.to_string(),
  102. "content": {"body": "t", "msgtype": "message"},
  103. },
  104. event,
  105. )
  106. self.assertFalse("redacted_because" in event.unsigned)
  107. # Redact event
  108. reason = "Because I said so"
  109. self.get_success(
  110. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  111. )
  112. event = self.get_success(self.store.get_event(msg_event.event_id))
  113. self.assertEqual(msg_event.event_id, event.event_id)
  114. self.assertTrue("redacted_because" in event.unsigned)
  115. self.assertObjectHasAttributes(
  116. {
  117. "type": EventTypes.Message,
  118. "user_id": self.u_alice.to_string(),
  119. "content": {},
  120. },
  121. event,
  122. )
  123. self.assertObjectHasAttributes(
  124. {
  125. "type": EventTypes.Redaction,
  126. "user_id": self.u_alice.to_string(),
  127. "content": {"reason": reason},
  128. },
  129. event.unsigned["redacted_because"],
  130. )
  131. def test_redact_join(self):
  132. self.get_success(
  133. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  134. )
  135. msg_event = self.get_success(
  136. self.inject_room_member(
  137. self.room1, self.u_bob, Membership.JOIN, extra_content={"blue": "red"}
  138. )
  139. )
  140. event = self.get_success(self.store.get_event(msg_event.event_id))
  141. self.assertObjectHasAttributes(
  142. {
  143. "type": EventTypes.Member,
  144. "user_id": self.u_bob.to_string(),
  145. "content": {"membership": Membership.JOIN, "blue": "red"},
  146. },
  147. event,
  148. )
  149. self.assertFalse(hasattr(event, "redacted_because"))
  150. # Redact event
  151. reason = "Because I said so"
  152. self.get_success(
  153. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  154. )
  155. # Check redaction
  156. event = self.get_success(self.store.get_event(msg_event.event_id))
  157. self.assertTrue("redacted_because" in event.unsigned)
  158. self.assertObjectHasAttributes(
  159. {
  160. "type": EventTypes.Member,
  161. "user_id": self.u_bob.to_string(),
  162. "content": {"membership": Membership.JOIN},
  163. },
  164. event,
  165. )
  166. self.assertObjectHasAttributes(
  167. {
  168. "type": EventTypes.Redaction,
  169. "user_id": self.u_alice.to_string(),
  170. "content": {"reason": reason},
  171. },
  172. event.unsigned["redacted_because"],
  173. )