1
0

test_devices.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  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.errors import NotFoundError
  17. from synapse.rest import admin, devices, room, sync
  18. from synapse.rest.client import account, login, register
  19. from synapse.server import HomeServer
  20. from synapse.util import Clock
  21. from tests import unittest
  22. class DeviceListsTestCase(unittest.HomeserverTestCase):
  23. """Tests regarding device list changes."""
  24. servlets = [
  25. admin.register_servlets_for_client_rest_resource,
  26. login.register_servlets,
  27. register.register_servlets,
  28. account.register_servlets,
  29. room.register_servlets,
  30. sync.register_servlets,
  31. devices.register_servlets,
  32. ]
  33. def test_receiving_local_device_list_changes(self) -> None:
  34. """Tests that a local users that share a room receive each other's device list
  35. changes.
  36. """
  37. # Register two users
  38. test_device_id = "TESTDEVICE"
  39. alice_user_id = self.register_user("alice", "correcthorse")
  40. alice_access_token = self.login(
  41. alice_user_id, "correcthorse", device_id=test_device_id
  42. )
  43. bob_user_id = self.register_user("bob", "ponyponypony")
  44. bob_access_token = self.login(bob_user_id, "ponyponypony")
  45. # Create a room for them to coexist peacefully in
  46. new_room_id = self.helper.create_room_as(
  47. alice_user_id, is_public=True, tok=alice_access_token
  48. )
  49. self.assertIsNotNone(new_room_id)
  50. # Have Bob join the room
  51. self.helper.invite(
  52. new_room_id, alice_user_id, bob_user_id, tok=alice_access_token
  53. )
  54. self.helper.join(new_room_id, bob_user_id, tok=bob_access_token)
  55. # Now have Bob initiate an initial sync (in order to get a since token)
  56. channel = self.make_request(
  57. "GET",
  58. "/sync",
  59. access_token=bob_access_token,
  60. )
  61. self.assertEqual(channel.code, 200, channel.json_body)
  62. next_batch_token = channel.json_body["next_batch"]
  63. # ...and then an incremental sync. This should block until the sync stream is woken up,
  64. # which we hope will happen as a result of Alice updating their device list.
  65. bob_sync_channel = self.make_request(
  66. "GET",
  67. f"/sync?since={next_batch_token}&timeout=30000",
  68. access_token=bob_access_token,
  69. # Start the request, then continue on.
  70. await_result=False,
  71. )
  72. # Have alice update their device list
  73. channel = self.make_request(
  74. "PUT",
  75. f"/devices/{test_device_id}",
  76. {
  77. "display_name": "New Device Name",
  78. },
  79. access_token=alice_access_token,
  80. )
  81. self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
  82. # Check that bob's incremental sync contains the updated device list.
  83. # If not, the client would only receive the device list update on the
  84. # *next* sync.
  85. bob_sync_channel.await_result()
  86. self.assertEqual(bob_sync_channel.code, 200, bob_sync_channel.json_body)
  87. changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get(
  88. "changed", []
  89. )
  90. self.assertIn(alice_user_id, changed_device_lists, bob_sync_channel.json_body)
  91. def test_not_receiving_local_device_list_changes(self) -> None:
  92. """Tests a local users DO NOT receive device updates from each other if they do not
  93. share a room.
  94. """
  95. # Register two users
  96. test_device_id = "TESTDEVICE"
  97. alice_user_id = self.register_user("alice", "correcthorse")
  98. alice_access_token = self.login(
  99. alice_user_id, "correcthorse", device_id=test_device_id
  100. )
  101. bob_user_id = self.register_user("bob", "ponyponypony")
  102. bob_access_token = self.login(bob_user_id, "ponyponypony")
  103. # These users do not share a room. They are lonely.
  104. # Have Bob initiate an initial sync (in order to get a since token)
  105. channel = self.make_request(
  106. "GET",
  107. "/sync",
  108. access_token=bob_access_token,
  109. )
  110. self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
  111. next_batch_token = channel.json_body["next_batch"]
  112. # ...and then an incremental sync. This should block until the sync stream is woken up,
  113. # which we hope will happen as a result of Alice updating their device list.
  114. bob_sync_channel = self.make_request(
  115. "GET",
  116. f"/sync?since={next_batch_token}&timeout=1000",
  117. access_token=bob_access_token,
  118. # Start the request, then continue on.
  119. await_result=False,
  120. )
  121. # Have alice update their device list
  122. channel = self.make_request(
  123. "PUT",
  124. f"/devices/{test_device_id}",
  125. {
  126. "display_name": "New Device Name",
  127. },
  128. access_token=alice_access_token,
  129. )
  130. self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
  131. # Check that bob's incremental sync does not contain the updated device list.
  132. bob_sync_channel.await_result()
  133. self.assertEqual(
  134. bob_sync_channel.code, HTTPStatus.OK, bob_sync_channel.json_body
  135. )
  136. changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get(
  137. "changed", []
  138. )
  139. self.assertNotIn(
  140. alice_user_id, changed_device_lists, bob_sync_channel.json_body
  141. )
  142. class DevicesTestCase(unittest.HomeserverTestCase):
  143. servlets = [
  144. admin.register_servlets,
  145. login.register_servlets,
  146. sync.register_servlets,
  147. ]
  148. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  149. self.handler = hs.get_device_handler()
  150. @unittest.override_config({"delete_stale_devices_after": 72000000})
  151. def test_delete_stale_devices(self) -> None:
  152. """Tests that stale devices are automatically removed after a set time of
  153. inactivity.
  154. The configuration is set to delete devices that haven't been used in the past 20h.
  155. """
  156. # Register a user and creates 2 devices for them.
  157. user_id = self.register_user("user", "password")
  158. tok1 = self.login("user", "password", device_id="abc")
  159. tok2 = self.login("user", "password", device_id="def")
  160. # Sync them so they have a last_seen value.
  161. self.make_request("GET", "/sync", access_token=tok1)
  162. self.make_request("GET", "/sync", access_token=tok2)
  163. # Advance half a day and sync again with one of the devices, so that the next
  164. # time the background job runs we don't delete this device (since it will look
  165. # for devices that haven't been used for over an hour).
  166. self.reactor.advance(43200)
  167. self.make_request("GET", "/sync", access_token=tok1)
  168. # Advance another half a day, and check that the device that has synced still
  169. # exists but the one that hasn't has been removed.
  170. self.reactor.advance(43200)
  171. self.get_success(self.handler.get_device(user_id, "abc"))
  172. self.get_failure(self.handler.get_device(user_id, "def"), NotFoundError)