mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-01-25 00:36:39 -05:00
158d73ebdd
Revert "Sort internal changes in changelog" Revert "Update CHANGES.md" Revert "1.49.0rc1" Revert "Revert "Move `glob_to_regex` and `re_word_boundary` to `matrix-python-common` (#11505) (#11527)" Revert "Refactors in `_generate_sync_entry_for_rooms` (#11515)" Revert "Correctly register shutdown handler for presence workers (#11518)" Revert "Fix `ModuleApi.looping_background_call` for non-async functions (#11524)" Revert "Fix 'delete room' admin api to work on incomplete rooms (#11523)" Revert "Correctly ignore invites from ignored users (#11511)" Revert "Fix the test breakage introduced by #11435 as a result of concurrent PRs (#11522)" Revert "Stabilise support for MSC2918 refresh tokens as they have now been merged into the Matrix specification. (#11435)" Revert "Save the OIDC session ID (sid) with the device on login (#11482)" Revert "Add admin API to get some information about federation status (#11407)" Revert "Include bundled aggregations in /sync and related fixes (#11478)" Revert "Move `glob_to_regex` and `re_word_boundary` to `matrix-python-common` (#11505)" Revert "Update backward extremity docs to make it clear that it does not indicate whether we have fetched an events' `prev_events` (#11469)" Revert "Support configuring the lifetime of non-refreshable access tokens separately to refreshable access tokens. (#11445)" Revert "Add type hints to `synapse/tests/rest/admin` (#11501)" Revert "Revert accidental commits to develop." Revert "Newsfile" Revert "Give `tests.server.setup_test_homeserver` (nominally!) the same behaviour" Revert "Move `tests.utils.setup_test_homeserver` to `tests.server`" Revert "Convert one of the `setup_test_homeserver`s to `make_test_homeserver_synchronous`" Revert "Disambiguate queries on `state_key` (#11497)" Revert "Comments on the /sync tentacles (#11494)" Revert "Clean up tests.storage.test_appservice (#11492)" Revert "Clean up `tests.storage.test_main` to remove use of legacy code. (#11493)" Revert "Clean up `tests.test_visibility` to remove legacy code. (#11495)" Revert "Minor cleanup on recently ported doc pages (#11466)" Revert "Add most of the missing type hints to `synapse.federation`. (#11483)" Revert "Avoid waiting for zombie processes in `synctl stop` (#11490)" Revert "Fix media repository failing when media store path contains symlinks (#11446)" Revert "Add type annotations to `tests.storage.test_appservice`. (#11488)" Revert "`scripts-dev/sign_json`: support for signing events (#11486)" Revert "Add MSC3030 experimental client and federation API endpoints to get the closest event to a given timestamp (#9445)" Revert "Port wiki pages to documentation website (#11402)" Revert "Add a license header and comment. (#11479)" Revert "Clean-up get_version_string (#11468)" Revert "Link background update controller docs to summary (#11475)" Revert "Additional type hints for config module. (#11465)" Revert "Register the login redirect endpoint for v3. (#11451)" Revert "Update openid.md" Revert "Remove mention of OIDC certification from Dex (#11470)" Revert "Add a note about huge pages to our Postgres doc (#11467)" Revert "Don't start Synapse master process if `worker_app` is set (#11416)" Revert "Expose worker & homeserver as entrypoints in `setup.py` (#11449)" Revert "Bundle relations of relations into the `/relations` result. (#11284)" Revert "Fix `LruCache` corruption bug with a `size_callback` that can return 0 (#11454)" Revert "Eliminate a few `Any`s in `LruCache` type hints (#11453)" Revert "Remove unnecessary `json.dumps` from `tests.rest.admin` (#11461)" Revert "Merge branch 'master' into develop" This reverts commit26b5d2320f
. This reverts commitbce4220f38
. This reverts commit966b5d0fa0
. This reverts commit088d748f2c
. This reverts commit14d593f72d
. This reverts commit2a3ec6facf
. This reverts commiteccc49d755
. This reverts commitb1ecd19c5d
. This reverts commit9c55dedc8c
. This reverts commit2d42e586a8
. This reverts commit2f053f3f82
. This reverts commita15a893df8
. This reverts commit8b4b153c9e
. This reverts commit494ebd7347
. This reverts commita77c369897
. This reverts commit4eb77965cd
. This reverts commit637df95de6
. This reverts commite5f426cd54
. This reverts commit8cd68b8102
. This reverts commit6cae125e20
. This reverts commit7be88fbf48
. This reverts commitb3fd99b74a
. This reverts commitf7ec6e7d9e
. This reverts commit5640992d17
. This reverts commitd26808dd85
. This reverts commitf91624a595
. This reverts commit16d39a5490
. This reverts commit8a4c296987
. This reverts commit49e1356ee3
. This reverts commitd2279f471b
. This reverts commitb50e39df57
. This reverts commit858d80bf0f
. This reverts commit435f044807
. This reverts commitf61462e1be
. This reverts commita6f1a3abec
. This reverts commit84dc50e160
. This reverts commited635d3285
. This reverts commit7b62791e00
. This reverts commit153194c771
. This reverts commitf44d729d4c
. This reverts commita265fbd397
. This reverts commitb9fef1a7cd
. This reverts commitb0eb64ff7b
. This reverts commitf1795463bf
. This reverts commit70cbb1a5e3
. This reverts commit42bf020463
. This reverts commit379f2650cf
. This reverts commit7ff22d6da4
. This reverts commit5a0b652d36
. This reverts commit432a174bc1
. This reverts commitb14f8a1baf
, reversing changes made toe713855dca
.
512 lines
16 KiB
Python
512 lines
16 KiB
Python
# Copyright 2020 Dirk Klimpel
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import urllib.parse
|
|
|
|
from parameterized import parameterized
|
|
|
|
import synapse.rest.admin
|
|
from synapse.api.errors import Codes
|
|
from synapse.rest.client import login
|
|
|
|
from tests import unittest
|
|
|
|
|
|
class DeviceRestTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
synapse.rest.admin.register_servlets,
|
|
login.register_servlets,
|
|
]
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
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.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,
|
|
)
|
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
|
def test_no_auth(self, method: str):
|
|
"""
|
|
Try to get a device of an user without authentication.
|
|
"""
|
|
channel = self.make_request(method, self.url, b"{}")
|
|
|
|
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
|
def test_requester_is_no_admin(self, method: str):
|
|
"""
|
|
If the user is not a server admin, an error is returned.
|
|
"""
|
|
channel = self.make_request(
|
|
method,
|
|
self.url,
|
|
access_token=self.other_user_token,
|
|
)
|
|
|
|
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
|
def test_user_does_not_exist(self, method: str):
|
|
"""
|
|
Tests that a lookup for a user that does not exist returns a 404
|
|
"""
|
|
url = (
|
|
"/_synapse/admin/v2/users/@unknown_person:test/devices/%s"
|
|
% self.other_user_device_id
|
|
)
|
|
|
|
channel = self.make_request(
|
|
method,
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
@parameterized.expand(["GET", "PUT", "DELETE"])
|
|
def test_user_is_not_local(self, method: str):
|
|
"""
|
|
Tests that a lookup for a user that is not a local returns a 400
|
|
"""
|
|
url = (
|
|
"/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices/%s"
|
|
% self.other_user_device_id
|
|
)
|
|
|
|
channel = self.make_request(
|
|
method,
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
|
self.assertEqual("Can only lookup local users", channel.json_body["error"])
|
|
|
|
def test_unknown_device(self):
|
|
"""
|
|
Tests that a lookup for a device that does not exist returns either 404 or 200.
|
|
"""
|
|
url = "/_synapse/admin/v2/users/%s/devices/unknown_device" % urllib.parse.quote(
|
|
self.other_user
|
|
)
|
|
|
|
channel = self.make_request(
|
|
"GET",
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
channel = self.make_request(
|
|
"PUT",
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
|
|
channel = self.make_request(
|
|
"DELETE",
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
# Delete unknown device returns status 200
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
|
|
def test_update_device_too_long_display_name(self):
|
|
"""
|
|
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)
|
|
}
|
|
|
|
channel = self.make_request(
|
|
"PUT",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
content=update,
|
|
)
|
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
|
self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"])
|
|
|
|
# Ensure the display name was not updated.
|
|
channel = self.make_request(
|
|
"GET",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
self.assertEqual("new display", channel.json_body["display_name"])
|
|
|
|
def test_update_no_display_name(self):
|
|
"""
|
|
Tests that a update for a device without JSON returns a 200
|
|
"""
|
|
# 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
|
|
)
|
|
)
|
|
|
|
channel = self.make_request(
|
|
"PUT",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
|
|
# Ensure the display name was not updated.
|
|
channel = self.make_request(
|
|
"GET",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
self.assertEqual("new display", channel.json_body["display_name"])
|
|
|
|
def test_update_display_name(self):
|
|
"""
|
|
Tests a normal successful update of display name
|
|
"""
|
|
# Set new display_name
|
|
channel = self.make_request(
|
|
"PUT",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
content={"display_name": "new displayname"},
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
|
|
# Check new display_name
|
|
channel = self.make_request(
|
|
"GET",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
self.assertEqual("new displayname", channel.json_body["display_name"])
|
|
|
|
def test_get_device(self):
|
|
"""
|
|
Tests that a normal lookup for a device is successfully
|
|
"""
|
|
channel = self.make_request(
|
|
"GET",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
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)
|
|
|
|
def test_delete_device(self):
|
|
"""
|
|
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
|
|
channel = self.make_request(
|
|
"DELETE",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
|
|
# 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,
|
|
]
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
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
|
|
)
|
|
|
|
def test_no_auth(self):
|
|
"""
|
|
Try to list devices of an user without authentication.
|
|
"""
|
|
channel = self.make_request("GET", self.url, b"{}")
|
|
|
|
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
def test_requester_is_no_admin(self):
|
|
"""
|
|
If the user is not a server admin, an error is returned.
|
|
"""
|
|
other_user_token = self.login("user", "pass")
|
|
|
|
channel = self.make_request(
|
|
"GET",
|
|
self.url,
|
|
access_token=other_user_token,
|
|
)
|
|
|
|
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
def test_user_does_not_exist(self):
|
|
"""
|
|
Tests that a lookup for a user that does not exist returns a 404
|
|
"""
|
|
url = "/_synapse/admin/v2/users/@unknown_person:test/devices"
|
|
channel = self.make_request(
|
|
"GET",
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
def test_user_is_not_local(self):
|
|
"""
|
|
Tests that a lookup for a user that is not a local returns a 400
|
|
"""
|
|
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices"
|
|
|
|
channel = self.make_request(
|
|
"GET",
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
|
self.assertEqual("Can only lookup local users", channel.json_body["error"])
|
|
|
|
def test_user_has_no_devices(self):
|
|
"""
|
|
Tests that a normal lookup for devices is successfully
|
|
if user has no devices
|
|
"""
|
|
|
|
# Get devices
|
|
channel = self.make_request(
|
|
"GET",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
self.assertEqual(0, channel.json_body["total"])
|
|
self.assertEqual(0, len(channel.json_body["devices"]))
|
|
|
|
def test_get_devices(self):
|
|
"""
|
|
Tests that a normal lookup for devices is successfully
|
|
"""
|
|
# Create devices
|
|
number_devices = 5
|
|
for _ in range(number_devices):
|
|
self.login("user", "pass")
|
|
|
|
# Get devices
|
|
channel = self.make_request(
|
|
"GET",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
self.assertEqual(number_devices, channel.json_body["total"])
|
|
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,
|
|
]
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
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
|
|
)
|
|
|
|
def test_no_auth(self):
|
|
"""
|
|
Try to delete devices of an user without authentication.
|
|
"""
|
|
channel = self.make_request("POST", self.url, b"{}")
|
|
|
|
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
def test_requester_is_no_admin(self):
|
|
"""
|
|
If the user is not a server admin, an error is returned.
|
|
"""
|
|
other_user_token = self.login("user", "pass")
|
|
|
|
channel = self.make_request(
|
|
"POST",
|
|
self.url,
|
|
access_token=other_user_token,
|
|
)
|
|
|
|
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
def test_user_does_not_exist(self):
|
|
"""
|
|
Tests that a lookup for a user that does not exist returns a 404
|
|
"""
|
|
url = "/_synapse/admin/v2/users/@unknown_person:test/delete_devices"
|
|
channel = self.make_request(
|
|
"POST",
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
def test_user_is_not_local(self):
|
|
"""
|
|
Tests that a lookup for a user that is not a local returns a 400
|
|
"""
|
|
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/delete_devices"
|
|
|
|
channel = self.make_request(
|
|
"POST",
|
|
url,
|
|
access_token=self.admin_user_tok,
|
|
)
|
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
|
self.assertEqual("Can only lookup local users", channel.json_body["error"])
|
|
|
|
def test_unknown_devices(self):
|
|
"""
|
|
Tests that a remove of a device that does not exist returns 200.
|
|
"""
|
|
channel = self.make_request(
|
|
"POST",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
content={"devices": ["unknown_device1", "unknown_device2"]},
|
|
)
|
|
|
|
# Delete unknown devices returns status 200
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
|
|
def test_delete_devices(self):
|
|
"""
|
|
Tests that a remove of devices is successfully
|
|
"""
|
|
|
|
# Create devices
|
|
number_devices = 5
|
|
for _ in range(number_devices):
|
|
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
|
|
channel = self.make_request(
|
|
"POST",
|
|
self.url,
|
|
access_token=self.admin_user_tok,
|
|
content={"devices": device_ids},
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
|
|
|
res = self.get_success(self.handler.get_devices_by_user(self.other_user))
|
|
self.assertEqual(0, len(res))
|