test_devices.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. import synapse.api.errors
  17. import tests.unittest
  18. import tests.utils
  19. class DeviceStoreTestCase(tests.unittest.TestCase):
  20. def __init__(self, *args, **kwargs):
  21. super(DeviceStoreTestCase, self).__init__(*args, **kwargs)
  22. self.store = None # type: synapse.storage.DataStore
  23. @defer.inlineCallbacks
  24. def setUp(self):
  25. hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  26. self.store = hs.get_datastore()
  27. @defer.inlineCallbacks
  28. def test_store_new_device(self):
  29. yield self.store.store_device("user_id", "device_id", "display_name")
  30. res = yield self.store.get_device("user_id", "device_id")
  31. self.assertDictContainsSubset(
  32. {
  33. "user_id": "user_id",
  34. "device_id": "device_id",
  35. "display_name": "display_name",
  36. },
  37. res,
  38. )
  39. @defer.inlineCallbacks
  40. def test_get_devices_by_user(self):
  41. yield self.store.store_device("user_id", "device1", "display_name 1")
  42. yield self.store.store_device("user_id", "device2", "display_name 2")
  43. yield self.store.store_device("user_id2", "device3", "display_name 3")
  44. res = yield self.store.get_devices_by_user("user_id")
  45. self.assertEqual(2, len(res.keys()))
  46. self.assertDictContainsSubset(
  47. {
  48. "user_id": "user_id",
  49. "device_id": "device1",
  50. "display_name": "display_name 1",
  51. },
  52. res["device1"],
  53. )
  54. self.assertDictContainsSubset(
  55. {
  56. "user_id": "user_id",
  57. "device_id": "device2",
  58. "display_name": "display_name 2",
  59. },
  60. res["device2"],
  61. )
  62. @defer.inlineCallbacks
  63. def test_get_devices_by_remote(self):
  64. device_ids = ["device_id1", "device_id2"]
  65. # Add two device updates with a single stream_id
  66. yield self.store.add_device_change_to_streams(
  67. "user_id", device_ids, ["somehost"]
  68. )
  69. # Get all device updates ever meant for this remote
  70. now_stream_id, device_updates = yield self.store.get_devices_by_remote(
  71. "somehost", -1, limit=100
  72. )
  73. # Check original device_ids are contained within these updates
  74. self._check_devices_in_updates(device_ids, device_updates)
  75. @defer.inlineCallbacks
  76. def test_get_devices_by_remote_limited(self):
  77. # Test breaking the update limit in 1, 101, and 1 device_id segments
  78. # first add one device
  79. device_ids1 = ["device_id0"]
  80. yield self.store.add_device_change_to_streams(
  81. "user_id", device_ids1, ["someotherhost"]
  82. )
  83. # then add 101
  84. device_ids2 = ["device_id" + str(i + 1) for i in range(101)]
  85. yield self.store.add_device_change_to_streams(
  86. "user_id", device_ids2, ["someotherhost"]
  87. )
  88. # then one more
  89. device_ids3 = ["newdevice"]
  90. yield self.store.add_device_change_to_streams(
  91. "user_id", device_ids3, ["someotherhost"]
  92. )
  93. #
  94. # now read them back.
  95. #
  96. # first we should get a single update
  97. now_stream_id, device_updates = yield self.store.get_devices_by_remote(
  98. "someotherhost", -1, limit=100
  99. )
  100. self._check_devices_in_updates(device_ids1, device_updates)
  101. # Then we should get an empty list back as the 101 devices broke the limit
  102. now_stream_id, device_updates = yield self.store.get_devices_by_remote(
  103. "someotherhost", now_stream_id, limit=100
  104. )
  105. self.assertEqual(len(device_updates), 0)
  106. # The 101 devices should've been cleared, so we should now just get one device
  107. # update
  108. now_stream_id, device_updates = yield self.store.get_devices_by_remote(
  109. "someotherhost", now_stream_id, limit=100
  110. )
  111. self._check_devices_in_updates(device_ids3, device_updates)
  112. def _check_devices_in_updates(self, expected_device_ids, device_updates):
  113. """Check that an specific device ids exist in a list of device update EDUs"""
  114. self.assertEqual(len(device_updates), len(expected_device_ids))
  115. received_device_ids = {update["device_id"] for update in device_updates}
  116. self.assertEqual(received_device_ids, set(expected_device_ids))
  117. @defer.inlineCallbacks
  118. def test_update_device(self):
  119. yield self.store.store_device("user_id", "device_id", "display_name 1")
  120. res = yield self.store.get_device("user_id", "device_id")
  121. self.assertEqual("display_name 1", res["display_name"])
  122. # do a no-op first
  123. yield self.store.update_device("user_id", "device_id")
  124. res = yield self.store.get_device("user_id", "device_id")
  125. self.assertEqual("display_name 1", res["display_name"])
  126. # do the update
  127. yield self.store.update_device(
  128. "user_id", "device_id", new_display_name="display_name 2"
  129. )
  130. # check it worked
  131. res = yield self.store.get_device("user_id", "device_id")
  132. self.assertEqual("display_name 2", res["display_name"])
  133. @defer.inlineCallbacks
  134. def test_update_unknown_device(self):
  135. with self.assertRaises(synapse.api.errors.StoreError) as cm:
  136. yield self.store.update_device(
  137. "user_id", "unknown_device_id", new_display_name="display_name 2"
  138. )
  139. self.assertEqual(404, cm.exception.code)