1
0

test_ephemeral_message.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector 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 synapse.api.constants import EventContentFields, EventTypes
  16. from synapse.rest import admin
  17. from synapse.rest.client.v1 import room
  18. from tests import unittest
  19. class EphemeralMessageTestCase(unittest.HomeserverTestCase):
  20. user_id = "@user:test"
  21. servlets = [
  22. admin.register_servlets,
  23. room.register_servlets,
  24. ]
  25. def make_homeserver(self, reactor, clock):
  26. config = self.default_config()
  27. config["enable_ephemeral_messages"] = True
  28. self.hs = self.setup_test_homeserver(config=config)
  29. return self.hs
  30. def prepare(self, reactor, clock, homeserver):
  31. self.room_id = self.helper.create_room_as(self.user_id)
  32. def test_message_expiry_no_delay(self):
  33. """Tests that sending a message sent with a m.self_destruct_after field set to the
  34. past results in that event being deleted right away.
  35. """
  36. # Send a message in the room that has expired. From here, the reactor clock is
  37. # at 200ms, so 0 is in the past, and even if that wasn't the case and the clock
  38. # is at 0ms the code path is the same if the event's expiry timestamp is the
  39. # current timestamp.
  40. res = self.helper.send_event(
  41. room_id=self.room_id,
  42. type=EventTypes.Message,
  43. content={
  44. "msgtype": "m.text",
  45. "body": "hello",
  46. EventContentFields.SELF_DESTRUCT_AFTER: 0,
  47. },
  48. )
  49. event_id = res["event_id"]
  50. # Check that we can't retrieve the content of the event.
  51. event_content = self.get_event(self.room_id, event_id)["content"]
  52. self.assertFalse(bool(event_content), event_content)
  53. def test_message_expiry_delay(self):
  54. """Tests that sending a message with a m.self_destruct_after field set to the
  55. future results in that event not being deleted right away, but advancing the
  56. clock to after that expiry timestamp causes the event to be deleted.
  57. """
  58. # Send a message in the room that'll expire in 1s.
  59. res = self.helper.send_event(
  60. room_id=self.room_id,
  61. type=EventTypes.Message,
  62. content={
  63. "msgtype": "m.text",
  64. "body": "hello",
  65. EventContentFields.SELF_DESTRUCT_AFTER: self.clock.time_msec() + 1000,
  66. },
  67. )
  68. event_id = res["event_id"]
  69. # Check that we can retrieve the content of the event before it has expired.
  70. event_content = self.get_event(self.room_id, event_id)["content"]
  71. self.assertTrue(bool(event_content), event_content)
  72. # Advance the clock to after the deletion.
  73. self.reactor.advance(1)
  74. # Check that we can't retrieve the content of the event anymore.
  75. event_content = self.get_event(self.room_id, event_id)["content"]
  76. self.assertFalse(bool(event_content), event_content)
  77. def get_event(self, room_id, event_id, expected_code=200):
  78. url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)
  79. request, channel = self.make_request("GET", url)
  80. self.render(request)
  81. self.assertEqual(channel.code, expected_code, channel.result)
  82. return channel.json_body