test_device.py 17 KB

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