test_device.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 synapse.handlers.device
  18. import synapse.storage
  19. from tests import unittest, utils
  20. user1 = "@boris:aaa"
  21. user2 = "@theresa:bbb"
  22. class DeviceTestCase(unittest.TestCase):
  23. def __init__(self, *args, **kwargs):
  24. super(DeviceTestCase, self).__init__(*args, **kwargs)
  25. self.store = None # type: synapse.storage.DataStore
  26. self.handler = None # type: synapse.handlers.device.DeviceHandler
  27. self.clock = None # type: utils.MockClock
  28. @defer.inlineCallbacks
  29. def setUp(self):
  30. hs = yield utils.setup_test_homeserver()
  31. self.handler = hs.get_device_handler()
  32. self.store = hs.get_datastore()
  33. self.clock = hs.get_clock()
  34. @defer.inlineCallbacks
  35. def test_device_is_created_if_doesnt_exist(self):
  36. res = yield self.handler.check_device_registered(
  37. user_id="@boris:foo",
  38. device_id="fco",
  39. initial_device_display_name="display name"
  40. )
  41. self.assertEqual(res, "fco")
  42. dev = yield self.handler.store.get_device("@boris:foo", "fco")
  43. self.assertEqual(dev["display_name"], "display name")
  44. @defer.inlineCallbacks
  45. def test_device_is_preserved_if_exists(self):
  46. res1 = yield self.handler.check_device_registered(
  47. user_id="@boris:foo",
  48. device_id="fco",
  49. initial_device_display_name="display name"
  50. )
  51. self.assertEqual(res1, "fco")
  52. res2 = yield self.handler.check_device_registered(
  53. user_id="@boris:foo",
  54. device_id="fco",
  55. initial_device_display_name="new display name"
  56. )
  57. self.assertEqual(res2, "fco")
  58. dev = yield self.handler.store.get_device("@boris:foo", "fco")
  59. self.assertEqual(dev["display_name"], "display name")
  60. @defer.inlineCallbacks
  61. def test_device_id_is_made_up_if_unspecified(self):
  62. device_id = yield self.handler.check_device_registered(
  63. user_id="@theresa:foo",
  64. device_id=None,
  65. initial_device_display_name="display"
  66. )
  67. dev = yield self.handler.store.get_device("@theresa:foo", device_id)
  68. self.assertEqual(dev["display_name"], "display")
  69. @defer.inlineCallbacks
  70. def test_get_devices_by_user(self):
  71. yield self._record_users()
  72. res = yield self.handler.get_devices_by_user(user1)
  73. self.assertEqual(3, len(res))
  74. device_map = {
  75. d["device_id"]: d for d in res
  76. }
  77. self.assertDictContainsSubset({
  78. "user_id": user1,
  79. "device_id": "xyz",
  80. "display_name": "display 0",
  81. "last_seen_ip": None,
  82. "last_seen_ts": None,
  83. }, device_map["xyz"])
  84. self.assertDictContainsSubset({
  85. "user_id": user1,
  86. "device_id": "fco",
  87. "display_name": "display 1",
  88. "last_seen_ip": "ip1",
  89. "last_seen_ts": 1000000,
  90. }, device_map["fco"])
  91. self.assertDictContainsSubset({
  92. "user_id": user1,
  93. "device_id": "abc",
  94. "display_name": "display 2",
  95. "last_seen_ip": "ip3",
  96. "last_seen_ts": 3000000,
  97. }, device_map["abc"])
  98. @defer.inlineCallbacks
  99. def test_get_device(self):
  100. yield self._record_users()
  101. res = yield self.handler.get_device(user1, "abc")
  102. self.assertDictContainsSubset({
  103. "user_id": user1,
  104. "device_id": "abc",
  105. "display_name": "display 2",
  106. "last_seen_ip": "ip3",
  107. "last_seen_ts": 3000000,
  108. }, res)
  109. @defer.inlineCallbacks
  110. def test_delete_device(self):
  111. yield self._record_users()
  112. # delete the device
  113. yield self.handler.delete_device(user1, "abc")
  114. # check the device was deleted
  115. with self.assertRaises(synapse.api.errors.NotFoundError):
  116. yield self.handler.get_device(user1, "abc")
  117. # we'd like to check the access token was invalidated, but that's a
  118. # bit of a PITA.
  119. @defer.inlineCallbacks
  120. def test_update_device(self):
  121. yield self._record_users()
  122. update = {"display_name": "new display"}
  123. yield self.handler.update_device(user1, "abc", update)
  124. res = yield self.handler.get_device(user1, "abc")
  125. self.assertEqual(res["display_name"], "new display")
  126. @defer.inlineCallbacks
  127. def test_update_unknown_device(self):
  128. update = {"display_name": "new_display"}
  129. with self.assertRaises(synapse.api.errors.NotFoundError):
  130. yield self.handler.update_device("user_id", "unknown_device_id",
  131. update)
  132. @defer.inlineCallbacks
  133. def _record_users(self):
  134. # check this works for both devices which have a recorded client_ip,
  135. # and those which don't.
  136. yield self._record_user(user1, "xyz", "display 0")
  137. yield self._record_user(user1, "fco", "display 1", "token1", "ip1")
  138. yield self._record_user(user1, "abc", "display 2", "token2", "ip2")
  139. yield self._record_user(user1, "abc", "display 2", "token3", "ip3")
  140. yield self._record_user(user2, "def", "dispkay", "token4", "ip4")
  141. @defer.inlineCallbacks
  142. def _record_user(self, user_id, device_id, display_name,
  143. access_token=None, ip=None):
  144. device_id = yield self.handler.check_device_registered(
  145. user_id=user_id,
  146. device_id=device_id,
  147. initial_device_display_name=display_name
  148. )
  149. if ip is not None:
  150. yield self.store.insert_client_ip(
  151. user_id,
  152. access_token, ip, "user_agent", device_id)
  153. self.clock.advance_time(1000)