2021-08-20 17:31:02 +02:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2021-08-20 16:47:03 +02:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
from synapse.api.errors import Codes
|
|
|
|
from synapse.rest import admin
|
|
|
|
from synapse.rest.client import keys, login
|
|
|
|
|
|
|
|
from tests import unittest
|
2022-09-07 13:03:32 +02:00
|
|
|
from tests.http.server._base import make_request_with_cancellation_test
|
2021-08-20 16:47:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KeyQueryTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
keys.register_servlets,
|
|
|
|
admin.register_servlets_for_client_rest_resource,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-02-24 19:56:38 +01:00
|
|
|
def test_rejects_device_id_ice_key_outside_of_list(self) -> None:
|
2021-08-20 16:47:03 +02:00
|
|
|
self.register_user("alice", "wonderland")
|
|
|
|
alice_token = self.login("alice", "wonderland")
|
|
|
|
bob = self.register_user("bob", "uncle")
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/_matrix/client/r0/keys/query",
|
|
|
|
{
|
|
|
|
"device_keys": {
|
|
|
|
bob: "device_id1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
alice_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
|
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["errcode"],
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
channel.result,
|
|
|
|
)
|
|
|
|
|
2022-02-24 19:56:38 +01:00
|
|
|
def test_rejects_device_key_given_as_map_to_bool(self) -> None:
|
2021-08-20 16:47:03 +02:00
|
|
|
self.register_user("alice", "wonderland")
|
|
|
|
alice_token = self.login("alice", "wonderland")
|
|
|
|
bob = self.register_user("bob", "uncle")
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/_matrix/client/r0/keys/query",
|
|
|
|
{
|
|
|
|
"device_keys": {
|
|
|
|
bob: {
|
|
|
|
"device_id1": True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
alice_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
|
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["errcode"],
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
channel.result,
|
|
|
|
)
|
|
|
|
|
2022-02-24 19:56:38 +01:00
|
|
|
def test_requires_device_key(self) -> None:
|
2021-08-20 16:47:03 +02:00
|
|
|
"""`device_keys` is required. We should complain if it's missing."""
|
|
|
|
self.register_user("alice", "wonderland")
|
|
|
|
alice_token = self.login("alice", "wonderland")
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/_matrix/client/r0/keys/query",
|
|
|
|
{},
|
|
|
|
alice_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
|
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["errcode"],
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
channel.result,
|
|
|
|
)
|
2022-09-07 13:03:32 +02:00
|
|
|
|
|
|
|
def test_key_query_cancellation(self) -> None:
|
|
|
|
"""
|
|
|
|
Tests that /keys/query is cancellable and does not swallow the
|
|
|
|
CancelledError.
|
|
|
|
"""
|
|
|
|
self.register_user("alice", "wonderland")
|
|
|
|
alice_token = self.login("alice", "wonderland")
|
|
|
|
|
|
|
|
bob = self.register_user("bob", "uncle")
|
|
|
|
|
|
|
|
channel = make_request_with_cancellation_test(
|
|
|
|
"test_key_query_cancellation",
|
|
|
|
self.reactor,
|
|
|
|
self.site,
|
|
|
|
"POST",
|
|
|
|
"/_matrix/client/r0/keys/query",
|
|
|
|
{
|
|
|
|
"device_keys": {
|
|
|
|
# Empty list means we request keys for all bob's devices
|
|
|
|
bob: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
token=alice_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
|
|
|
self.assertIn(bob, channel.json_body["device_keys"])
|