test_devices.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2016-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. import synapse.api.errors
  15. from tests.unittest import HomeserverTestCase
  16. class DeviceStoreTestCase(HomeserverTestCase):
  17. def prepare(self, reactor, clock, hs):
  18. self.store = hs.get_datastore()
  19. def test_store_new_device(self):
  20. self.get_success(
  21. self.store.store_device("user_id", "device_id", "display_name")
  22. )
  23. res = self.get_success(self.store.get_device("user_id", "device_id"))
  24. self.assertDictContainsSubset(
  25. {
  26. "user_id": "user_id",
  27. "device_id": "device_id",
  28. "display_name": "display_name",
  29. },
  30. res,
  31. )
  32. def test_get_devices_by_user(self):
  33. self.get_success(
  34. self.store.store_device("user_id", "device1", "display_name 1")
  35. )
  36. self.get_success(
  37. self.store.store_device("user_id", "device2", "display_name 2")
  38. )
  39. self.get_success(
  40. self.store.store_device("user_id2", "device3", "display_name 3")
  41. )
  42. res = self.get_success(self.store.get_devices_by_user("user_id"))
  43. self.assertEqual(2, len(res.keys()))
  44. self.assertDictContainsSubset(
  45. {
  46. "user_id": "user_id",
  47. "device_id": "device1",
  48. "display_name": "display_name 1",
  49. },
  50. res["device1"],
  51. )
  52. self.assertDictContainsSubset(
  53. {
  54. "user_id": "user_id",
  55. "device_id": "device2",
  56. "display_name": "display_name 2",
  57. },
  58. res["device2"],
  59. )
  60. def test_count_devices_by_users(self):
  61. self.get_success(
  62. self.store.store_device("user_id", "device1", "display_name 1")
  63. )
  64. self.get_success(
  65. self.store.store_device("user_id", "device2", "display_name 2")
  66. )
  67. self.get_success(
  68. self.store.store_device("user_id2", "device3", "display_name 3")
  69. )
  70. res = self.get_success(self.store.count_devices_by_users())
  71. self.assertEqual(0, res)
  72. res = self.get_success(self.store.count_devices_by_users(["unknown"]))
  73. self.assertEqual(0, res)
  74. res = self.get_success(self.store.count_devices_by_users(["user_id"]))
  75. self.assertEqual(2, res)
  76. res = self.get_success(
  77. self.store.count_devices_by_users(["user_id", "user_id2"])
  78. )
  79. self.assertEqual(3, res)
  80. def test_get_device_updates_by_remote(self):
  81. device_ids = ["device_id1", "device_id2"]
  82. # Add two device updates with a single stream_id
  83. self.get_success(
  84. self.store.add_device_change_to_streams("user_id", device_ids, ["somehost"])
  85. )
  86. # Get all device updates ever meant for this remote
  87. now_stream_id, device_updates = self.get_success(
  88. self.store.get_device_updates_by_remote("somehost", -1, limit=100)
  89. )
  90. # Check original device_ids are contained within these updates
  91. self._check_devices_in_updates(device_ids, device_updates)
  92. def _check_devices_in_updates(self, expected_device_ids, device_updates):
  93. """Check that an specific device ids exist in a list of device update EDUs"""
  94. self.assertEqual(len(device_updates), len(expected_device_ids))
  95. received_device_ids = {
  96. update["device_id"] for edu_type, update in device_updates
  97. }
  98. self.assertEqual(received_device_ids, set(expected_device_ids))
  99. def test_update_device(self):
  100. self.get_success(
  101. self.store.store_device("user_id", "device_id", "display_name 1")
  102. )
  103. res = self.get_success(self.store.get_device("user_id", "device_id"))
  104. self.assertEqual("display_name 1", res["display_name"])
  105. # do a no-op first
  106. self.get_success(self.store.update_device("user_id", "device_id"))
  107. res = self.get_success(self.store.get_device("user_id", "device_id"))
  108. self.assertEqual("display_name 1", res["display_name"])
  109. # do the update
  110. self.get_success(
  111. self.store.update_device(
  112. "user_id", "device_id", new_display_name="display_name 2"
  113. )
  114. )
  115. # check it worked
  116. res = self.get_success(self.store.get_device("user_id", "device_id"))
  117. self.assertEqual("display_name 2", res["display_name"])
  118. def test_update_unknown_device(self):
  119. exc = self.get_failure(
  120. self.store.update_device(
  121. "user_id", "unknown_device_id", new_display_name="display_name 2"
  122. ),
  123. synapse.api.errors.StoreError,
  124. )
  125. self.assertEqual(404, exc.value.code)