1
0

test_redaction.py 6.3 KB

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