1
0

test_redaction.py 6.2 KB

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