test_redaction.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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
  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. {
  43. "type": EventTypes.Member,
  44. "sender": user.to_string(),
  45. "state_key": user.to_string(),
  46. "room_id": room.to_string(),
  47. "content": content,
  48. }
  49. )
  50. event, context = yield self.event_creation_handler.create_new_client_event(
  51. builder
  52. )
  53. yield self.store.persist_event(event, context)
  54. defer.returnValue(event)
  55. @defer.inlineCallbacks
  56. def inject_message(self, room, user, body):
  57. self.depth += 1
  58. builder = self.event_builder_factory.new(
  59. {
  60. "type": EventTypes.Message,
  61. "sender": user.to_string(),
  62. "state_key": user.to_string(),
  63. "room_id": room.to_string(),
  64. "content": {"body": body, "msgtype": u"message"},
  65. }
  66. )
  67. event, context = yield self.event_creation_handler.create_new_client_event(
  68. builder
  69. )
  70. yield self.store.persist_event(event, context)
  71. defer.returnValue(event)
  72. @defer.inlineCallbacks
  73. def inject_redaction(self, room, event_id, user, reason):
  74. builder = self.event_builder_factory.new(
  75. {
  76. "type": EventTypes.Redaction,
  77. "sender": user.to_string(),
  78. "state_key": user.to_string(),
  79. "room_id": room.to_string(),
  80. "content": {"reason": reason},
  81. "redacts": event_id,
  82. }
  83. )
  84. event, context = yield self.event_creation_handler.create_new_client_event(
  85. builder
  86. )
  87. yield self.store.persist_event(event, context)
  88. @defer.inlineCallbacks
  89. def test_redact(self):
  90. yield self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  91. msg_event = yield self.inject_message(self.room1, self.u_alice, u"t")
  92. # Check event has not been redacted:
  93. event = yield self.store.get_event(msg_event.event_id)
  94. self.assertObjectHasAttributes(
  95. {
  96. "type": EventTypes.Message,
  97. "user_id": self.u_alice.to_string(),
  98. "content": {"body": "t", "msgtype": "message"},
  99. },
  100. event,
  101. )
  102. self.assertFalse("redacted_because" in event.unsigned)
  103. # Redact event
  104. reason = "Because I said so"
  105. yield self.inject_redaction(
  106. self.room1, msg_event.event_id, self.u_alice, reason
  107. )
  108. event = yield self.store.get_event(msg_event.event_id)
  109. self.assertEqual(msg_event.event_id, event.event_id)
  110. self.assertTrue("redacted_because" in event.unsigned)
  111. self.assertObjectHasAttributes(
  112. {
  113. "type": EventTypes.Message,
  114. "user_id": self.u_alice.to_string(),
  115. "content": {},
  116. },
  117. event,
  118. )
  119. self.assertObjectHasAttributes(
  120. {
  121. "type": EventTypes.Redaction,
  122. "user_id": self.u_alice.to_string(),
  123. "content": {"reason": reason},
  124. },
  125. event.unsigned["redacted_because"],
  126. )
  127. @defer.inlineCallbacks
  128. def test_redact_join(self):
  129. yield self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  130. msg_event = yield self.inject_room_member(
  131. self.room1, self.u_bob, Membership.JOIN, extra_content={"blue": "red"}
  132. )
  133. event = yield self.store.get_event(msg_event.event_id)
  134. self.assertObjectHasAttributes(
  135. {
  136. "type": EventTypes.Member,
  137. "user_id": self.u_bob.to_string(),
  138. "content": {"membership": Membership.JOIN, "blue": "red"},
  139. },
  140. event,
  141. )
  142. self.assertFalse(hasattr(event, "redacted_because"))
  143. # Redact event
  144. reason = "Because I said so"
  145. yield self.inject_redaction(
  146. self.room1, msg_event.event_id, self.u_alice, reason
  147. )
  148. # Check redaction
  149. event = yield self.store.get_event(msg_event.event_id)
  150. self.assertTrue("redacted_because" in event.unsigned)
  151. self.assertObjectHasAttributes(
  152. {
  153. "type": EventTypes.Member,
  154. "user_id": self.u_bob.to_string(),
  155. "content": {"membership": Membership.JOIN},
  156. },
  157. event,
  158. )
  159. self.assertObjectHasAttributes(
  160. {
  161. "type": EventTypes.Redaction,
  162. "user_id": self.u_alice.to_string(),
  163. "content": {"reason": reason},
  164. },
  165. event.unsigned["redacted_because"],
  166. )