test_deviceinbox.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright 2021 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 twisted.test.proto_helpers import MemoryReactor
  15. from synapse.rest import admin
  16. from synapse.rest.client import devices
  17. from synapse.server import HomeServer
  18. from synapse.util import Clock
  19. from tests.unittest import HomeserverTestCase
  20. class DeviceInboxBackgroundUpdateStoreTestCase(HomeserverTestCase):
  21. servlets = [
  22. admin.register_servlets,
  23. devices.register_servlets,
  24. ]
  25. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  26. self.store = hs.get_datastores().main
  27. self.user_id = self.register_user("foo", "pass")
  28. def test_background_remove_deleted_devices_from_device_inbox(self) -> None:
  29. """Test that the background task to delete old device_inboxes works properly."""
  30. # create a valid device
  31. self.get_success(
  32. self.store.store_device(self.user_id, "cur_device", "display_name")
  33. )
  34. # Add device_inbox to devices
  35. self.get_success(
  36. self.store.db_pool.simple_insert(
  37. "device_inbox",
  38. {
  39. "user_id": self.user_id,
  40. "device_id": "cur_device",
  41. "stream_id": 1,
  42. "message_json": "{}",
  43. },
  44. )
  45. )
  46. self.get_success(
  47. self.store.db_pool.simple_insert(
  48. "device_inbox",
  49. {
  50. "user_id": self.user_id,
  51. "device_id": "old_device",
  52. "stream_id": 2,
  53. "message_json": "{}",
  54. },
  55. )
  56. )
  57. # Insert and run the background update.
  58. self.get_success(
  59. self.store.db_pool.simple_insert(
  60. "background_updates",
  61. {
  62. "update_name": "remove_dead_devices_from_device_inbox",
  63. "progress_json": "{}",
  64. },
  65. )
  66. )
  67. # ... and tell the DataStore that it hasn't finished all updates yet
  68. self.store.db_pool.updates._all_done = False
  69. self.wait_for_background_updates()
  70. # Make sure the background task deleted old device_inbox
  71. res = self.get_success(
  72. self.store.db_pool.simple_select_onecol(
  73. table="device_inbox",
  74. keyvalues={},
  75. retcol="device_id",
  76. desc="get_device_id_from_device_inbox",
  77. )
  78. )
  79. self.assertEqual(1, len(res))
  80. self.assertEqual(res[0], "cur_device")
  81. def test_background_remove_hidden_devices_from_device_inbox(self) -> None:
  82. """Test that the background task to delete hidden devices
  83. from device_inboxes works properly."""
  84. # create a valid device
  85. self.get_success(
  86. self.store.store_device(self.user_id, "cur_device", "display_name")
  87. )
  88. # create a hidden device
  89. self.get_success(
  90. self.store.db_pool.simple_insert(
  91. "devices",
  92. values={
  93. "user_id": self.user_id,
  94. "device_id": "hidden_device",
  95. "display_name": "hidden_display_name",
  96. "hidden": True,
  97. },
  98. )
  99. )
  100. # Add device_inbox to devices
  101. self.get_success(
  102. self.store.db_pool.simple_insert(
  103. "device_inbox",
  104. {
  105. "user_id": self.user_id,
  106. "device_id": "cur_device",
  107. "stream_id": 1,
  108. "message_json": "{}",
  109. },
  110. )
  111. )
  112. self.get_success(
  113. self.store.db_pool.simple_insert(
  114. "device_inbox",
  115. {
  116. "user_id": self.user_id,
  117. "device_id": "hidden_device",
  118. "stream_id": 2,
  119. "message_json": "{}",
  120. },
  121. )
  122. )
  123. # Insert and run the background update.
  124. self.get_success(
  125. self.store.db_pool.simple_insert(
  126. "background_updates",
  127. {
  128. "update_name": "remove_dead_devices_from_device_inbox",
  129. "progress_json": "{}",
  130. },
  131. )
  132. )
  133. # ... and tell the DataStore that it hasn't finished all updates yet
  134. self.store.db_pool.updates._all_done = False
  135. self.wait_for_background_updates()
  136. # Make sure the background task deleted hidden devices from device_inbox
  137. res = self.get_success(
  138. self.store.db_pool.simple_select_onecol(
  139. table="device_inbox",
  140. keyvalues={},
  141. retcol="device_id",
  142. desc="get_device_id_from_device_inbox",
  143. )
  144. )
  145. self.assertEqual(1, len(res))
  146. self.assertEqual(res[0], "cur_device")