123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- # -*- coding: utf-8 -*-
- # 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 json
- import urllib.parse
- import synapse.rest.admin
- from synapse.api.errors import Codes
- from synapse.rest.client.v1 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,
- )
- def test_no_auth(self):
- """
- Try to get a device of an user without authentication.
- """
- request, channel = self.make_request("GET", self.url, b"{}")
- self.render(request)
- self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
- self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
- request, channel = self.make_request("PUT", self.url, b"{}")
- self.render(request)
- self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
- self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
- request, channel = self.make_request("DELETE", self.url, b"{}")
- self.render(request)
- 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.
- """
- request, channel = self.make_request(
- "GET", self.url, access_token=self.other_user_token,
- )
- self.render(request)
- self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
- self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
- request, channel = self.make_request(
- "PUT", self.url, access_token=self.other_user_token,
- )
- self.render(request)
- self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
- self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
- request, channel = self.make_request(
- "DELETE", self.url, access_token=self.other_user_token,
- )
- self.render(request)
- 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/%s"
- % self.other_user_device_id
- )
- request, channel = self.make_request(
- "GET", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(404, channel.code, msg=channel.json_body)
- self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
- request, channel = self.make_request(
- "PUT", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(404, channel.code, msg=channel.json_body)
- self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
- request, channel = self.make_request(
- "DELETE", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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/%s"
- % self.other_user_device_id
- )
- request, channel = self.make_request(
- "GET", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(400, channel.code, msg=channel.json_body)
- self.assertEqual("Can only lookup local users", channel.json_body["error"])
- request, channel = self.make_request(
- "PUT", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(400, channel.code, msg=channel.json_body)
- self.assertEqual("Can only lookup local users", channel.json_body["error"])
- request, channel = self.make_request(
- "DELETE", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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
- )
- request, channel = self.make_request(
- "GET", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(404, channel.code, msg=channel.json_body)
- self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
- request, channel = self.make_request(
- "PUT", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(200, channel.code, msg=channel.json_body)
- request, channel = self.make_request(
- "DELETE", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- # 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)
- }
- body = json.dumps(update)
- request, channel = self.make_request(
- "PUT",
- self.url,
- access_token=self.admin_user_tok,
- content=body.encode(encoding="utf_8"),
- )
- self.render(request)
- self.assertEqual(400, channel.code, msg=channel.json_body)
- self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
- # Ensure the display name was not updated.
- request, channel = self.make_request(
- "GET", self.url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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
- )
- )
- request, channel = self.make_request(
- "PUT", self.url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(200, channel.code, msg=channel.json_body)
- # Ensure the display name was not updated.
- request, channel = self.make_request(
- "GET", self.url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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
- body = json.dumps({"display_name": "new displayname"})
- request, channel = self.make_request(
- "PUT",
- self.url,
- access_token=self.admin_user_tok,
- content=body.encode(encoding="utf_8"),
- )
- self.render(request)
- self.assertEqual(200, channel.code, msg=channel.json_body)
- # Check new display_name
- request, channel = self.make_request(
- "GET", self.url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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
- """
- request, channel = self.make_request(
- "GET", self.url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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
- request, channel = self.make_request(
- "DELETE", self.url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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.
- """
- request, channel = self.make_request("GET", self.url, b"{}")
- self.render(request)
- 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")
- request, channel = self.make_request(
- "GET", self.url, access_token=other_user_token,
- )
- self.render(request)
- 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"
- request, channel = self.make_request(
- "GET", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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"
- request, channel = self.make_request(
- "GET", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(400, channel.code, msg=channel.json_body)
- self.assertEqual("Can only lookup local users", channel.json_body["error"])
- def test_get_devices(self):
- """
- Tests that a normal lookup for devices is successfully
- """
- # Create devices
- number_devices = 5
- for n in range(number_devices):
- self.login("user", "pass")
- # Get devices
- request, channel = self.make_request(
- "GET", self.url, access_token=self.admin_user_tok,
- )
- self.render(request)
- self.assertEqual(200, channel.code, msg=channel.json_body)
- 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.
- """
- request, channel = self.make_request("POST", self.url, b"{}")
- self.render(request)
- 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")
- request, channel = self.make_request(
- "POST", self.url, access_token=other_user_token,
- )
- self.render(request)
- 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"
- request, channel = self.make_request(
- "POST", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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"
- request, channel = self.make_request(
- "POST", url, access_token=self.admin_user_tok,
- )
- self.render(request)
- 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.
- """
- body = json.dumps({"devices": ["unknown_device1", "unknown_device2"]})
- request, channel = self.make_request(
- "POST",
- self.url,
- access_token=self.admin_user_tok,
- content=body.encode(encoding="utf_8"),
- )
- self.render(request)
- # 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 n 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
- body = json.dumps({"devices": device_ids})
- request, channel = self.make_request(
- "POST",
- self.url,
- access_token=self.admin_user_tok,
- content=body.encode(encoding="utf_8"),
- )
- self.render(request)
- 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))
|