2020-06-05 15:07:22 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
# Copyright 2020 Dirk Klimpel
|
2023-11-21 23:29:58 +03:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-06-05 15:07:22 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import urllib.parse
|
|
|
|
|
2021-08-20 18:43:26 +03:00
|
|
|
from parameterized import parameterized
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2020-06-05 15:07:22 +03:00
|
|
|
import synapse.rest.admin
|
|
|
|
from synapse.api.errors import Codes
|
2022-11-22 22:08:04 +03:00
|
|
|
from synapse.handlers.device import DeviceHandler
|
2021-08-17 14:57:58 +03:00
|
|
|
from synapse.rest.client import login
|
2021-12-03 16:57:13 +03:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2020-06-05 15:07:22 +03:00
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceRestTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-11-22 22:08:04 +03:00
|
|
|
handler = hs.get_device_handler()
|
|
|
|
assert isinstance(handler, DeviceHandler)
|
|
|
|
self.handler = handler
|
2020-06-05 15:07:22 +03:00
|
|
|
|
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
self.other_user_token = self.login("user", "pass")
|
|
|
|
res = self.get_success(self.handler.get_devices_by_user(self.other_user))
|
|
|
|
self.other_user_device_id = res[0]["device_id"]
|
|
|
|
|
|
|
|
self.url = "/_synapse/admin/v2/users/%s/devices/%s" % (
|
|
|
|
urllib.parse.quote(self.other_user),
|
|
|
|
self.other_user_device_id,
|
|
|
|
)
|
|
|
|
|
2021-08-20 18:43:26 +03:00
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_no_auth(self, method: str) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Try to get a device of an user without authentication.
|
|
|
|
"""
|
2021-08-20 18:43:26 +03:00
|
|
|
channel = self.make_request(method, self.url, b"{}")
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-11-30 12:53:54 +03:00
|
|
|
self.assertEqual(
|
2022-08-09 16:56:43 +03:00
|
|
|
401,
|
2021-11-30 12:53:54 +03:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
2021-08-20 18:43:26 +03:00
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_requester_is_no_admin(self, method: str) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2021-08-20 18:43:26 +03:00
|
|
|
method,
|
2021-02-17 01:32:34 +03:00
|
|
|
self.url,
|
|
|
|
access_token=self.other_user_token,
|
2020-06-05 15:07:22 +03:00
|
|
|
)
|
|
|
|
|
2021-11-30 12:53:54 +03:00
|
|
|
self.assertEqual(
|
2022-08-09 16:56:43 +03:00
|
|
|
403,
|
2021-11-30 12:53:54 +03:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-08-20 18:43:26 +03:00
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_user_does_not_exist(self, method: str) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-09 16:56:43 +03:00
|
|
|
Tests that a lookup for a user that does not exist returns a 404
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
url = (
|
|
|
|
"/_synapse/admin/v2/users/@unknown_person:test/devices/%s"
|
|
|
|
% self.other_user_device_id
|
|
|
|
)
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
2021-08-20 18:43:26 +03:00
|
|
|
method,
|
2021-02-17 01:32:34 +03:00
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-09 16:56:43 +03:00
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
|
2021-08-20 18:43:26 +03:00
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_user_is_not_local(self, method: str) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-10 21:01:12 +03:00
|
|
|
Tests that a lookup for a user that is not a local returns a 400
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
url = (
|
|
|
|
"/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices/%s"
|
|
|
|
% self.other_user_device_id
|
|
|
|
)
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
2021-08-20 18:43:26 +03:00
|
|
|
method,
|
2021-02-17 01:32:34 +03:00
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-10 21:01:12 +03:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual("Can only lookup local users", channel.json_body["error"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_unknown_device(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-09 16:56:43 +03:00
|
|
|
Tests that a lookup for a device that does not exist returns either 404 or 200.
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v2/users/%s/devices/unknown_device" % urllib.parse.quote(
|
|
|
|
self.other_user
|
|
|
|
)
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-09 16:56:43 +03:00
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
# Delete unknown device returns status 200
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_update_device_too_long_display_name(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Update a device with a display name that is invalid (too long).
|
|
|
|
"""
|
|
|
|
# Set iniital display name.
|
|
|
|
update = {"display_name": "new display"}
|
|
|
|
self.get_success(
|
|
|
|
self.handler.update_device(
|
|
|
|
self.other_user, self.other_user_device_id, update
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Request to update a device display name with a new value that is longer than allowed.
|
|
|
|
update = {
|
|
|
|
"display_name": "a"
|
|
|
|
* (synapse.handlers.device.MAX_DEVICE_DISPLAY_NAME_LEN + 1)
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-06-05 15:07:22 +03:00
|
|
|
"PUT",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
2021-08-20 18:43:26 +03:00
|
|
|
content=update,
|
2020-06-05 15:07:22 +03:00
|
|
|
)
|
|
|
|
|
2022-08-10 21:01:12 +03:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-09-22 13:42:55 +03:00
|
|
|
self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"])
|
2020-06-05 15:07:22 +03:00
|
|
|
|
|
|
|
# Ensure the display name was not updated.
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual("new display", channel.json_body["display_name"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_update_no_display_name(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-08 23:21:27 +03:00
|
|
|
Tests that a update for a device without JSON returns a 200
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
# Set iniital display name.
|
|
|
|
update = {"display_name": "new display"}
|
|
|
|
self.get_success(
|
|
|
|
self.handler.update_device(
|
|
|
|
self.other_user, self.other_user_device_id, update
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
|
|
|
# Ensure the display name was not updated.
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual("new display", channel.json_body["display_name"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_update_display_name(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Tests a normal successful update of display name
|
|
|
|
"""
|
|
|
|
# Set new display_name
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-06-05 15:07:22 +03:00
|
|
|
"PUT",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
2021-08-20 18:43:26 +03:00
|
|
|
content={"display_name": "new displayname"},
|
2020-06-05 15:07:22 +03:00
|
|
|
)
|
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
|
|
|
# Check new display_name
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual("new displayname", channel.json_body["display_name"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_get_device(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Tests that a normal lookup for a device is successfully
|
|
|
|
"""
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(self.other_user, channel.json_body["user_id"])
|
|
|
|
# Check that all fields are available
|
|
|
|
self.assertIn("user_id", channel.json_body)
|
|
|
|
self.assertIn("device_id", channel.json_body)
|
|
|
|
self.assertIn("display_name", channel.json_body)
|
|
|
|
self.assertIn("last_seen_ip", channel.json_body)
|
|
|
|
self.assertIn("last_seen_ts", channel.json_body)
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_delete_device(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Tests that a remove of a device is successfully
|
|
|
|
"""
|
|
|
|
# Count number of devies of an user.
|
|
|
|
res = self.get_success(self.handler.get_devices_by_user(self.other_user))
|
|
|
|
number_devices = len(res)
|
|
|
|
self.assertEqual(1, number_devices)
|
|
|
|
|
|
|
|
# Delete device
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2021-02-17 01:32:34 +03:00
|
|
|
"DELETE",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
2020-06-05 15:07:22 +03:00
|
|
|
)
|
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
|
|
|
# Ensure that the number of devices is decreased
|
|
|
|
res = self.get_success(self.handler.get_devices_by_user(self.other_user))
|
|
|
|
self.assertEqual(number_devices - 1, len(res))
|
|
|
|
|
|
|
|
|
|
|
|
class DevicesRestTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
|
|
|
|
self.url = "/_synapse/admin/v2/users/%s/devices" % urllib.parse.quote(
|
|
|
|
self.other_user
|
|
|
|
)
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_no_auth(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Try to list devices of an user without authentication.
|
|
|
|
"""
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request("GET", self.url, b"{}")
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-11-30 12:53:54 +03:00
|
|
|
self.assertEqual(
|
2022-08-09 16:56:43 +03:00
|
|
|
401,
|
2021-11-30 12:53:54 +03:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_requester_is_no_admin(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
|
|
|
other_user_token = self.login("user", "pass")
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url,
|
|
|
|
access_token=other_user_token,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-11-30 12:53:54 +03:00
|
|
|
self.assertEqual(
|
2022-08-09 16:56:43 +03:00
|
|
|
403,
|
2021-11-30 12:53:54 +03:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_user_does_not_exist(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-09 16:56:43 +03:00
|
|
|
Tests that a lookup for a user that does not exist returns a 404
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v2/users/@unknown_person:test/devices"
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-09 16:56:43 +03:00
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_user_is_not_local(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-10 21:01:12 +03:00
|
|
|
Tests that a lookup for a user that is not a local returns a 400
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices"
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-10 21:01:12 +03:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual("Can only lookup local users", channel.json_body["error"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_user_has_no_devices(self) -> None:
|
2020-10-26 17:07:51 +03:00
|
|
|
"""
|
|
|
|
Tests that a normal lookup for devices is successfully
|
|
|
|
if user has no devices
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Get devices
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-10-26 17:07:51 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 17:07:51 +03:00
|
|
|
self.assertEqual(0, channel.json_body["total"])
|
|
|
|
self.assertEqual(0, len(channel.json_body["devices"]))
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_get_devices(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Tests that a normal lookup for devices is successfully
|
|
|
|
"""
|
|
|
|
# Create devices
|
|
|
|
number_devices = 5
|
2021-04-20 13:50:49 +03:00
|
|
|
for _ in range(number_devices):
|
2020-06-05 15:07:22 +03:00
|
|
|
self.login("user", "pass")
|
|
|
|
|
|
|
|
# Get devices
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 17:07:51 +03:00
|
|
|
self.assertEqual(number_devices, channel.json_body["total"])
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(number_devices, len(channel.json_body["devices"]))
|
|
|
|
self.assertEqual(self.other_user, channel.json_body["devices"][0]["user_id"])
|
|
|
|
# Check that all fields are available
|
|
|
|
for d in channel.json_body["devices"]:
|
|
|
|
self.assertIn("user_id", d)
|
|
|
|
self.assertIn("device_id", d)
|
|
|
|
self.assertIn("display_name", d)
|
|
|
|
self.assertIn("last_seen_ip", d)
|
|
|
|
self.assertIn("last_seen_ts", d)
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
self.handler = hs.get_device_handler()
|
|
|
|
|
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
|
|
|
|
self.url = "/_synapse/admin/v2/users/%s/delete_devices" % urllib.parse.quote(
|
|
|
|
self.other_user
|
|
|
|
)
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_no_auth(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Try to delete devices of an user without authentication.
|
|
|
|
"""
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request("POST", self.url, b"{}")
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-11-30 12:53:54 +03:00
|
|
|
self.assertEqual(
|
2022-08-09 16:56:43 +03:00
|
|
|
401,
|
2021-11-30 12:53:54 +03:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_requester_is_no_admin(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
|
|
|
other_user_token = self.login("user", "pass")
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url,
|
|
|
|
access_token=other_user_token,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-11-30 12:53:54 +03:00
|
|
|
self.assertEqual(
|
2022-08-09 16:56:43 +03:00
|
|
|
403,
|
2021-11-30 12:53:54 +03:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_user_does_not_exist(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-09 16:56:43 +03:00
|
|
|
Tests that a lookup for a user that does not exist returns a 404
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v2/users/@unknown_person:test/delete_devices"
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-09 16:56:43 +03:00
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_user_is_not_local(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-10 21:01:12 +03:00
|
|
|
Tests that a lookup for a user that is not a local returns a 400
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/delete_devices"
|
|
|
|
|
2021-02-17 01:32:34 +03:00
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2022-08-10 21:01:12 +03:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
self.assertEqual("Can only lookup local users", channel.json_body["error"])
|
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_unknown_devices(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2022-08-08 23:21:27 +03:00
|
|
|
Tests that a remove of a device that does not exist returns 200.
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-06-05 15:07:22 +03:00
|
|
|
"POST",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
2021-08-20 18:43:26 +03:00
|
|
|
content={"devices": ["unknown_device1", "unknown_device2"]},
|
2020-06-05 15:07:22 +03:00
|
|
|
)
|
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
# Delete unknown devices returns status 200
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
2021-12-03 16:57:13 +03:00
|
|
|
def test_delete_devices(self) -> None:
|
2020-06-05 15:07:22 +03:00
|
|
|
"""
|
|
|
|
Tests that a remove of devices is successfully
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create devices
|
|
|
|
number_devices = 5
|
2021-04-20 13:50:49 +03:00
|
|
|
for _ in range(number_devices):
|
2020-06-05 15:07:22 +03:00
|
|
|
self.login("user", "pass")
|
|
|
|
|
|
|
|
# Get devices
|
|
|
|
res = self.get_success(self.handler.get_devices_by_user(self.other_user))
|
|
|
|
self.assertEqual(number_devices, len(res))
|
|
|
|
|
|
|
|
# Create list of device IDs
|
|
|
|
device_ids = []
|
|
|
|
for d in res:
|
|
|
|
device_ids.append(str(d["device_id"]))
|
|
|
|
|
|
|
|
# Delete devices
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-06-05 15:07:22 +03:00
|
|
|
"POST",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
2021-08-20 18:43:26 +03:00
|
|
|
content={"devices": device_ids},
|
2020-06-05 15:07:22 +03:00
|
|
|
)
|
|
|
|
|
2022-08-08 23:21:27 +03:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-06-05 15:07:22 +03:00
|
|
|
|
|
|
|
res = self.get_success(self.handler.get_devices_by_user(self.other_user))
|
|
|
|
self.assertEqual(0, len(res))
|