test_device.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 Dirk Klimpel
  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. import json
  16. import urllib.parse
  17. import synapse.rest.admin
  18. from synapse.api.errors import Codes
  19. from synapse.rest.client.v1 import login
  20. from tests import unittest
  21. class DeviceRestTestCase(unittest.HomeserverTestCase):
  22. servlets = [
  23. synapse.rest.admin.register_servlets,
  24. login.register_servlets,
  25. ]
  26. def prepare(self, reactor, clock, hs):
  27. self.handler = hs.get_device_handler()
  28. self.admin_user = self.register_user("admin", "pass", admin=True)
  29. self.admin_user_tok = self.login("admin", "pass")
  30. self.other_user = self.register_user("user", "pass")
  31. self.other_user_token = self.login("user", "pass")
  32. res = self.get_success(self.handler.get_devices_by_user(self.other_user))
  33. self.other_user_device_id = res[0]["device_id"]
  34. self.url = "/_synapse/admin/v2/users/%s/devices/%s" % (
  35. urllib.parse.quote(self.other_user),
  36. self.other_user_device_id,
  37. )
  38. def test_no_auth(self):
  39. """
  40. Try to get a device of an user without authentication.
  41. """
  42. channel = self.make_request("GET", self.url, b"{}")
  43. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  44. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  45. channel = self.make_request("PUT", self.url, b"{}")
  46. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  47. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  48. channel = self.make_request("DELETE", self.url, b"{}")
  49. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  50. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  51. def test_requester_is_no_admin(self):
  52. """
  53. If the user is not a server admin, an error is returned.
  54. """
  55. channel = self.make_request(
  56. "GET", self.url, access_token=self.other_user_token,
  57. )
  58. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  59. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  60. channel = self.make_request(
  61. "PUT", self.url, access_token=self.other_user_token,
  62. )
  63. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  64. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  65. channel = self.make_request(
  66. "DELETE", self.url, access_token=self.other_user_token,
  67. )
  68. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  69. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  70. def test_user_does_not_exist(self):
  71. """
  72. Tests that a lookup for a user that does not exist returns a 404
  73. """
  74. url = (
  75. "/_synapse/admin/v2/users/@unknown_person:test/devices/%s"
  76. % self.other_user_device_id
  77. )
  78. channel = self.make_request("GET", url, access_token=self.admin_user_tok,)
  79. self.assertEqual(404, channel.code, msg=channel.json_body)
  80. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  81. channel = self.make_request("PUT", url, access_token=self.admin_user_tok,)
  82. self.assertEqual(404, channel.code, msg=channel.json_body)
  83. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  84. channel = self.make_request("DELETE", url, access_token=self.admin_user_tok,)
  85. self.assertEqual(404, channel.code, msg=channel.json_body)
  86. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  87. def test_user_is_not_local(self):
  88. """
  89. Tests that a lookup for a user that is not a local returns a 400
  90. """
  91. url = (
  92. "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices/%s"
  93. % self.other_user_device_id
  94. )
  95. channel = self.make_request("GET", url, access_token=self.admin_user_tok,)
  96. self.assertEqual(400, channel.code, msg=channel.json_body)
  97. self.assertEqual("Can only lookup local users", channel.json_body["error"])
  98. channel = self.make_request("PUT", url, access_token=self.admin_user_tok,)
  99. self.assertEqual(400, channel.code, msg=channel.json_body)
  100. self.assertEqual("Can only lookup local users", channel.json_body["error"])
  101. channel = self.make_request("DELETE", url, access_token=self.admin_user_tok,)
  102. self.assertEqual(400, channel.code, msg=channel.json_body)
  103. self.assertEqual("Can only lookup local users", channel.json_body["error"])
  104. def test_unknown_device(self):
  105. """
  106. Tests that a lookup for a device that does not exist returns either 404 or 200.
  107. """
  108. url = "/_synapse/admin/v2/users/%s/devices/unknown_device" % urllib.parse.quote(
  109. self.other_user
  110. )
  111. channel = self.make_request("GET", url, access_token=self.admin_user_tok,)
  112. self.assertEqual(404, channel.code, msg=channel.json_body)
  113. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  114. channel = self.make_request("PUT", url, access_token=self.admin_user_tok,)
  115. self.assertEqual(200, channel.code, msg=channel.json_body)
  116. channel = self.make_request("DELETE", url, access_token=self.admin_user_tok,)
  117. # Delete unknown device returns status 200
  118. self.assertEqual(200, channel.code, msg=channel.json_body)
  119. def test_update_device_too_long_display_name(self):
  120. """
  121. Update a device with a display name that is invalid (too long).
  122. """
  123. # Set iniital display name.
  124. update = {"display_name": "new display"}
  125. self.get_success(
  126. self.handler.update_device(
  127. self.other_user, self.other_user_device_id, update
  128. )
  129. )
  130. # Request to update a device display name with a new value that is longer than allowed.
  131. update = {
  132. "display_name": "a"
  133. * (synapse.handlers.device.MAX_DEVICE_DISPLAY_NAME_LEN + 1)
  134. }
  135. body = json.dumps(update)
  136. channel = self.make_request(
  137. "PUT",
  138. self.url,
  139. access_token=self.admin_user_tok,
  140. content=body.encode(encoding="utf_8"),
  141. )
  142. self.assertEqual(400, channel.code, msg=channel.json_body)
  143. self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"])
  144. # Ensure the display name was not updated.
  145. channel = self.make_request("GET", self.url, access_token=self.admin_user_tok,)
  146. self.assertEqual(200, channel.code, msg=channel.json_body)
  147. self.assertEqual("new display", channel.json_body["display_name"])
  148. def test_update_no_display_name(self):
  149. """
  150. Tests that a update for a device without JSON returns a 200
  151. """
  152. # Set iniital display name.
  153. update = {"display_name": "new display"}
  154. self.get_success(
  155. self.handler.update_device(
  156. self.other_user, self.other_user_device_id, update
  157. )
  158. )
  159. channel = self.make_request("PUT", self.url, access_token=self.admin_user_tok,)
  160. self.assertEqual(200, channel.code, msg=channel.json_body)
  161. # Ensure the display name was not updated.
  162. channel = self.make_request("GET", self.url, access_token=self.admin_user_tok,)
  163. self.assertEqual(200, channel.code, msg=channel.json_body)
  164. self.assertEqual("new display", channel.json_body["display_name"])
  165. def test_update_display_name(self):
  166. """
  167. Tests a normal successful update of display name
  168. """
  169. # Set new display_name
  170. body = json.dumps({"display_name": "new displayname"})
  171. channel = self.make_request(
  172. "PUT",
  173. self.url,
  174. access_token=self.admin_user_tok,
  175. content=body.encode(encoding="utf_8"),
  176. )
  177. self.assertEqual(200, channel.code, msg=channel.json_body)
  178. # Check new display_name
  179. channel = self.make_request("GET", self.url, access_token=self.admin_user_tok,)
  180. self.assertEqual(200, channel.code, msg=channel.json_body)
  181. self.assertEqual("new displayname", channel.json_body["display_name"])
  182. def test_get_device(self):
  183. """
  184. Tests that a normal lookup for a device is successfully
  185. """
  186. channel = self.make_request("GET", self.url, access_token=self.admin_user_tok,)
  187. self.assertEqual(200, channel.code, msg=channel.json_body)
  188. self.assertEqual(self.other_user, channel.json_body["user_id"])
  189. # Check that all fields are available
  190. self.assertIn("user_id", channel.json_body)
  191. self.assertIn("device_id", channel.json_body)
  192. self.assertIn("display_name", channel.json_body)
  193. self.assertIn("last_seen_ip", channel.json_body)
  194. self.assertIn("last_seen_ts", channel.json_body)
  195. def test_delete_device(self):
  196. """
  197. Tests that a remove of a device is successfully
  198. """
  199. # Count number of devies of an user.
  200. res = self.get_success(self.handler.get_devices_by_user(self.other_user))
  201. number_devices = len(res)
  202. self.assertEqual(1, number_devices)
  203. # Delete device
  204. channel = self.make_request(
  205. "DELETE", self.url, access_token=self.admin_user_tok,
  206. )
  207. self.assertEqual(200, channel.code, msg=channel.json_body)
  208. # Ensure that the number of devices is decreased
  209. res = self.get_success(self.handler.get_devices_by_user(self.other_user))
  210. self.assertEqual(number_devices - 1, len(res))
  211. class DevicesRestTestCase(unittest.HomeserverTestCase):
  212. servlets = [
  213. synapse.rest.admin.register_servlets,
  214. login.register_servlets,
  215. ]
  216. def prepare(self, reactor, clock, hs):
  217. self.admin_user = self.register_user("admin", "pass", admin=True)
  218. self.admin_user_tok = self.login("admin", "pass")
  219. self.other_user = self.register_user("user", "pass")
  220. self.url = "/_synapse/admin/v2/users/%s/devices" % urllib.parse.quote(
  221. self.other_user
  222. )
  223. def test_no_auth(self):
  224. """
  225. Try to list devices of an user without authentication.
  226. """
  227. channel = self.make_request("GET", self.url, b"{}")
  228. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  229. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  230. def test_requester_is_no_admin(self):
  231. """
  232. If the user is not a server admin, an error is returned.
  233. """
  234. other_user_token = self.login("user", "pass")
  235. channel = self.make_request("GET", self.url, access_token=other_user_token,)
  236. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  237. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  238. def test_user_does_not_exist(self):
  239. """
  240. Tests that a lookup for a user that does not exist returns a 404
  241. """
  242. url = "/_synapse/admin/v2/users/@unknown_person:test/devices"
  243. channel = self.make_request("GET", url, access_token=self.admin_user_tok,)
  244. self.assertEqual(404, channel.code, msg=channel.json_body)
  245. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  246. def test_user_is_not_local(self):
  247. """
  248. Tests that a lookup for a user that is not a local returns a 400
  249. """
  250. url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices"
  251. channel = self.make_request("GET", url, access_token=self.admin_user_tok,)
  252. self.assertEqual(400, channel.code, msg=channel.json_body)
  253. self.assertEqual("Can only lookup local users", channel.json_body["error"])
  254. def test_user_has_no_devices(self):
  255. """
  256. Tests that a normal lookup for devices is successfully
  257. if user has no devices
  258. """
  259. # Get devices
  260. channel = self.make_request("GET", self.url, access_token=self.admin_user_tok,)
  261. self.assertEqual(200, channel.code, msg=channel.json_body)
  262. self.assertEqual(0, channel.json_body["total"])
  263. self.assertEqual(0, len(channel.json_body["devices"]))
  264. def test_get_devices(self):
  265. """
  266. Tests that a normal lookup for devices is successfully
  267. """
  268. # Create devices
  269. number_devices = 5
  270. for n in range(number_devices):
  271. self.login("user", "pass")
  272. # Get devices
  273. channel = self.make_request("GET", self.url, access_token=self.admin_user_tok,)
  274. self.assertEqual(200, channel.code, msg=channel.json_body)
  275. self.assertEqual(number_devices, channel.json_body["total"])
  276. self.assertEqual(number_devices, len(channel.json_body["devices"]))
  277. self.assertEqual(self.other_user, channel.json_body["devices"][0]["user_id"])
  278. # Check that all fields are available
  279. for d in channel.json_body["devices"]:
  280. self.assertIn("user_id", d)
  281. self.assertIn("device_id", d)
  282. self.assertIn("display_name", d)
  283. self.assertIn("last_seen_ip", d)
  284. self.assertIn("last_seen_ts", d)
  285. class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
  286. servlets = [
  287. synapse.rest.admin.register_servlets,
  288. login.register_servlets,
  289. ]
  290. def prepare(self, reactor, clock, hs):
  291. self.handler = hs.get_device_handler()
  292. self.admin_user = self.register_user("admin", "pass", admin=True)
  293. self.admin_user_tok = self.login("admin", "pass")
  294. self.other_user = self.register_user("user", "pass")
  295. self.url = "/_synapse/admin/v2/users/%s/delete_devices" % urllib.parse.quote(
  296. self.other_user
  297. )
  298. def test_no_auth(self):
  299. """
  300. Try to delete devices of an user without authentication.
  301. """
  302. channel = self.make_request("POST", self.url, b"{}")
  303. self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
  304. self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
  305. def test_requester_is_no_admin(self):
  306. """
  307. If the user is not a server admin, an error is returned.
  308. """
  309. other_user_token = self.login("user", "pass")
  310. channel = self.make_request("POST", self.url, access_token=other_user_token,)
  311. self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
  312. self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
  313. def test_user_does_not_exist(self):
  314. """
  315. Tests that a lookup for a user that does not exist returns a 404
  316. """
  317. url = "/_synapse/admin/v2/users/@unknown_person:test/delete_devices"
  318. channel = self.make_request("POST", url, access_token=self.admin_user_tok,)
  319. self.assertEqual(404, channel.code, msg=channel.json_body)
  320. self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
  321. def test_user_is_not_local(self):
  322. """
  323. Tests that a lookup for a user that is not a local returns a 400
  324. """
  325. url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/delete_devices"
  326. channel = self.make_request("POST", url, access_token=self.admin_user_tok,)
  327. self.assertEqual(400, channel.code, msg=channel.json_body)
  328. self.assertEqual("Can only lookup local users", channel.json_body["error"])
  329. def test_unknown_devices(self):
  330. """
  331. Tests that a remove of a device that does not exist returns 200.
  332. """
  333. body = json.dumps({"devices": ["unknown_device1", "unknown_device2"]})
  334. channel = self.make_request(
  335. "POST",
  336. self.url,
  337. access_token=self.admin_user_tok,
  338. content=body.encode(encoding="utf_8"),
  339. )
  340. # Delete unknown devices returns status 200
  341. self.assertEqual(200, channel.code, msg=channel.json_body)
  342. def test_delete_devices(self):
  343. """
  344. Tests that a remove of devices is successfully
  345. """
  346. # Create devices
  347. number_devices = 5
  348. for n in range(number_devices):
  349. self.login("user", "pass")
  350. # Get devices
  351. res = self.get_success(self.handler.get_devices_by_user(self.other_user))
  352. self.assertEqual(number_devices, len(res))
  353. # Create list of device IDs
  354. device_ids = []
  355. for d in res:
  356. device_ids.append(str(d["device_id"]))
  357. # Delete devices
  358. body = json.dumps({"devices": device_ids})
  359. channel = self.make_request(
  360. "POST",
  361. self.url,
  362. access_token=self.admin_user_tok,
  363. content=body.encode(encoding="utf_8"),
  364. )
  365. self.assertEqual(200, channel.code, msg=channel.json_body)
  366. res = self.get_success(self.handler.get_devices_by_user(self.other_user))
  367. self.assertEqual(0, len(res))