1
0

test_ephemeral_message.py 4.0 KB

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