test_redaction.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  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. from mock import Mock
  16. from twisted.internet import defer
  17. from synapse.api.constants import EventTypes, Membership, RoomVersions
  18. from synapse.types import RoomID, UserID
  19. from tests import unittest
  20. from tests.utils import create_room, setup_test_homeserver
  21. class RedactionTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def setUp(self):
  24. hs = yield setup_test_homeserver(
  25. self.addCleanup, resource_for_federation=Mock(), http_client=None
  26. )
  27. self.store = hs.get_datastore()
  28. self.event_builder_factory = hs.get_event_builder_factory()
  29. self.event_creation_handler = hs.get_event_creation_handler()
  30. self.u_alice = UserID.from_string("@alice:test")
  31. self.u_bob = UserID.from_string("@bob:test")
  32. self.room1 = RoomID.from_string("!abc123:test")
  33. yield create_room(hs, self.room1.to_string(), self.u_alice.to_string())
  34. self.depth = 1
  35. @defer.inlineCallbacks
  36. def inject_room_member(
  37. self, room, user, membership, replaces_state=None, extra_content={}
  38. ):
  39. content = {"membership": membership}
  40. content.update(extra_content)
  41. builder = self.event_builder_factory.new(
  42. RoomVersions.V1,
  43. {
  44. "type": EventTypes.Member,
  45. "sender": user.to_string(),
  46. "state_key": user.to_string(),
  47. "room_id": room.to_string(),
  48. "content": content,
  49. }
  50. )
  51. event, context = yield self.event_creation_handler.create_new_client_event(
  52. builder
  53. )
  54. yield self.store.persist_event(event, context)
  55. defer.returnValue(event)
  56. @defer.inlineCallbacks
  57. def inject_message(self, room, user, body):
  58. self.depth += 1
  59. builder = self.event_builder_factory.new(
  60. RoomVersions.V1,
  61. {
  62. "type": EventTypes.Message,
  63. "sender": user.to_string(),
  64. "state_key": user.to_string(),
  65. "room_id": room.to_string(),
  66. "content": {"body": body, "msgtype": u"message"},
  67. }
  68. )
  69. event, context = yield self.event_creation_handler.create_new_client_event(
  70. builder
  71. )
  72. yield self.store.persist_event(event, context)
  73. defer.returnValue(event)
  74. @defer.inlineCallbacks
  75. def inject_redaction(self, room, event_id, user, reason):
  76. builder = self.event_builder_factory.new(
  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 = yield self.event_creation_handler.create_new_client_event(
  88. builder
  89. )
  90. yield self.store.persist_event(event, context)
  91. @defer.inlineCallbacks
  92. def test_redact(self):
  93. yield self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  94. msg_event = yield self.inject_message(self.room1, self.u_alice, u"t")
  95. # Check event has not been redacted:
  96. event = yield self.store.get_event(msg_event.event_id)
  97. self.assertObjectHasAttributes(
  98. {
  99. "type": EventTypes.Message,
  100. "user_id": self.u_alice.to_string(),
  101. "content": {"body": "t", "msgtype": "message"},
  102. },
  103. event,
  104. )
  105. self.assertFalse("redacted_because" in event.unsigned)
  106. # Redact event
  107. reason = "Because I said so"
  108. yield self.inject_redaction(
  109. self.room1, msg_event.event_id, self.u_alice, reason
  110. )
  111. event = yield self.store.get_event(msg_event.event_id)
  112. self.assertEqual(msg_event.event_id, event.event_id)
  113. self.assertTrue("redacted_because" in event.unsigned)
  114. self.assertObjectHasAttributes(
  115. {
  116. "type": EventTypes.Message,
  117. "user_id": self.u_alice.to_string(),
  118. "content": {},
  119. },
  120. event,
  121. )
  122. self.assertObjectHasAttributes(
  123. {
  124. "type": EventTypes.Redaction,
  125. "user_id": self.u_alice.to_string(),
  126. "content": {"reason": reason},
  127. },
  128. event.unsigned["redacted_because"],
  129. )
  130. @defer.inlineCallbacks
  131. def test_redact_join(self):
  132. yield self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  133. msg_event = yield self.inject_room_member(
  134. self.room1, self.u_bob, Membership.JOIN, extra_content={"blue": "red"}
  135. )
  136. event = yield self.store.get_event(msg_event.event_id)
  137. self.assertObjectHasAttributes(
  138. {
  139. "type": EventTypes.Member,
  140. "user_id": self.u_bob.to_string(),
  141. "content": {"membership": Membership.JOIN, "blue": "red"},
  142. },
  143. event,
  144. )
  145. self.assertFalse(hasattr(event, "redacted_because"))
  146. # Redact event
  147. reason = "Because I said so"
  148. yield self.inject_redaction(
  149. self.room1, msg_event.event_id, self.u_alice, reason
  150. )
  151. # Check redaction
  152. event = yield self.store.get_event(msg_event.event_id)
  153. self.assertTrue("redacted_because" in event.unsigned)
  154. self.assertObjectHasAttributes(
  155. {
  156. "type": EventTypes.Member,
  157. "user_id": self.u_bob.to_string(),
  158. "content": {"membership": Membership.JOIN},
  159. },
  160. event,
  161. )
  162. self.assertObjectHasAttributes(
  163. {
  164. "type": EventTypes.Redaction,
  165. "user_id": self.u_alice.to_string(),
  166. "content": {"reason": reason},
  167. },
  168. event.unsigned["redacted_because"],
  169. )