test_device.py 17 KB

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