test_deviceinbox.py 5.2 KB

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