test_read_marker.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright 2023 Beeper
  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 twisted.test.proto_helpers import MemoryReactor
  15. import synapse.rest.admin
  16. from synapse.api.constants import EventTypes
  17. from synapse.rest import admin
  18. from synapse.rest.client import login, read_marker, register, room
  19. from synapse.server import HomeServer
  20. from synapse.util import Clock
  21. from tests import unittest
  22. ONE_HOUR_MS = 3600000
  23. ONE_DAY_MS = ONE_HOUR_MS * 24
  24. class ReadMarkerTestCase(unittest.HomeserverTestCase):
  25. servlets = [
  26. login.register_servlets,
  27. register.register_servlets,
  28. read_marker.register_servlets,
  29. room.register_servlets,
  30. synapse.rest.admin.register_servlets,
  31. admin.register_servlets,
  32. ]
  33. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  34. config = self.default_config()
  35. # merge this default retention config with anything that was specified in
  36. # @override_config
  37. retention_config = {
  38. "enabled": True,
  39. "allowed_lifetime_min": ONE_DAY_MS,
  40. "allowed_lifetime_max": ONE_DAY_MS * 3,
  41. }
  42. retention_config.update(config.get("retention", {}))
  43. config["retention"] = retention_config
  44. self.hs = self.setup_test_homeserver(config=config)
  45. return self.hs
  46. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  47. self.owner = self.register_user("owner", "pass")
  48. self.owner_tok = self.login("owner", "pass")
  49. self.store = self.hs.get_datastores().main
  50. self.clock = self.hs.get_clock()
  51. def test_send_read_marker(self) -> None:
  52. room_id = self.helper.create_room_as(self.owner, tok=self.owner_tok)
  53. def send_message() -> str:
  54. res = self.helper.send(room_id=room_id, body="1", tok=self.owner_tok)
  55. return res["event_id"]
  56. # Test setting the read marker on the room
  57. event_id_1 = send_message()
  58. channel = self.make_request(
  59. "POST",
  60. "/rooms/!abc:beep/read_markers",
  61. content={
  62. "m.fully_read": event_id_1,
  63. },
  64. access_token=self.owner_tok,
  65. )
  66. self.assertEqual(channel.code, 200, channel.result)
  67. # Test moving the read marker to a newer event
  68. event_id_2 = send_message()
  69. channel = self.make_request(
  70. "POST",
  71. "/rooms/!abc:beep/read_markers",
  72. content={
  73. "m.fully_read": event_id_2,
  74. },
  75. access_token=self.owner_tok,
  76. )
  77. self.assertEqual(channel.code, 200, channel.result)
  78. def test_send_read_marker_missing_previous_event(self) -> None:
  79. """
  80. Test moving a read marker from an event that previously existed but was
  81. later removed due to retention rules.
  82. """
  83. room_id = self.helper.create_room_as(self.owner, tok=self.owner_tok)
  84. # Set retention rule on the room so we remove old events to test this case
  85. self.helper.send_state(
  86. room_id=room_id,
  87. event_type=EventTypes.Retention,
  88. body={"max_lifetime": ONE_DAY_MS},
  89. tok=self.owner_tok,
  90. )
  91. def send_message() -> str:
  92. res = self.helper.send(room_id=room_id, body="1", tok=self.owner_tok)
  93. return res["event_id"]
  94. # Test setting the read marker on the room
  95. event_id_1 = send_message()
  96. channel = self.make_request(
  97. "POST",
  98. "/rooms/!abc:beep/read_markers",
  99. content={
  100. "m.fully_read": event_id_1,
  101. },
  102. access_token=self.owner_tok,
  103. )
  104. # Send a second message (retention will not remove the latest event ever)
  105. send_message()
  106. # And then advance so retention rules remove the first event (where the marker is)
  107. self.reactor.advance(ONE_DAY_MS * 2 / 1000)
  108. event = self.get_success(self.store.get_event(event_id_1, allow_none=True))
  109. assert event is None
  110. # Test moving the read marker to a newer event
  111. event_id_2 = send_message()
  112. channel = self.make_request(
  113. "POST",
  114. "/rooms/!abc:beep/read_markers",
  115. content={
  116. "m.fully_read": event_id_2,
  117. },
  118. access_token=self.owner_tok,
  119. )
  120. self.assertEqual(channel.code, 200, channel.result)