您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

100 行
3.7 KiB

  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.api.constants import EventContentFields, EventTypes
  15. from synapse.rest import admin
  16. from synapse.rest.client import room
  17. from tests import unittest
  18. class EphemeralMessageTestCase(unittest.HomeserverTestCase):
  19. user_id = "@user:test"
  20. servlets = [
  21. admin.register_servlets,
  22. room.register_servlets,
  23. ]
  24. def make_homeserver(self, reactor, clock):
  25. config = self.default_config()
  26. config["enable_ephemeral_messages"] = True
  27. self.hs = self.setup_test_homeserver(config=config)
  28. return self.hs
  29. def prepare(self, reactor, clock, homeserver):
  30. self.room_id = self.helper.create_room_as(self.user_id)
  31. def test_message_expiry_no_delay(self):
  32. """Tests that sending a message sent with a m.self_destruct_after field set to the
  33. past results in that event being deleted right away.
  34. """
  35. # Send a message in the room that has expired. From here, the reactor clock is
  36. # at 200ms, so 0 is in the past, and even if that wasn't the case and the clock
  37. # is at 0ms the code path is the same if the event's expiry timestamp is the
  38. # current timestamp.
  39. res = self.helper.send_event(
  40. room_id=self.room_id,
  41. type=EventTypes.Message,
  42. content={
  43. "msgtype": "m.text",
  44. "body": "hello",
  45. EventContentFields.SELF_DESTRUCT_AFTER: 0,
  46. },
  47. )
  48. event_id = res["event_id"]
  49. # Check that we can't retrieve the content of the event.
  50. event_content = self.get_event(self.room_id, event_id)["content"]
  51. self.assertFalse(bool(event_content), event_content)
  52. def test_message_expiry_delay(self):
  53. """Tests that sending a message with a m.self_destruct_after field set to the
  54. future results in that event not being deleted right away, but advancing the
  55. clock to after that expiry timestamp causes the event to be deleted.
  56. """
  57. # Send a message in the room that'll expire in 1s.
  58. res = self.helper.send_event(
  59. room_id=self.room_id,
  60. type=EventTypes.Message,
  61. content={
  62. "msgtype": "m.text",
  63. "body": "hello",
  64. EventContentFields.SELF_DESTRUCT_AFTER: self.clock.time_msec() + 1000,
  65. },
  66. )
  67. event_id = res["event_id"]
  68. # Check that we can retrieve the content of the event before it has expired.
  69. event_content = self.get_event(self.room_id, event_id)["content"]
  70. self.assertTrue(bool(event_content), event_content)
  71. # Advance the clock to after the deletion.
  72. self.reactor.advance(1)
  73. # Check that we can't retrieve the content of the event anymore.
  74. event_content = self.get_event(self.room_id, event_id)["content"]
  75. self.assertFalse(bool(event_content), event_content)
  76. def get_event(self, room_id, event_id, expected_code=200):
  77. url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)
  78. channel = self.make_request("GET", url)
  79. self.assertEqual(channel.code, expected_code, channel.result)
  80. return channel.json_body