test_keys.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  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. from http import HTTPStatus
  15. from synapse.api.errors import Codes
  16. from synapse.rest import admin
  17. from synapse.rest.client import keys, login
  18. from tests import unittest
  19. from tests.http.server._base import make_request_with_cancellation_test
  20. class KeyQueryTestCase(unittest.HomeserverTestCase):
  21. servlets = [
  22. keys.register_servlets,
  23. admin.register_servlets_for_client_rest_resource,
  24. login.register_servlets,
  25. ]
  26. def test_rejects_device_id_ice_key_outside_of_list(self) -> None:
  27. self.register_user("alice", "wonderland")
  28. alice_token = self.login("alice", "wonderland")
  29. bob = self.register_user("bob", "uncle")
  30. channel = self.make_request(
  31. "POST",
  32. "/_matrix/client/r0/keys/query",
  33. {
  34. "device_keys": {
  35. bob: "device_id1",
  36. },
  37. },
  38. alice_token,
  39. )
  40. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  41. self.assertEqual(
  42. channel.json_body["errcode"],
  43. Codes.BAD_JSON,
  44. channel.result,
  45. )
  46. def test_rejects_device_key_given_as_map_to_bool(self) -> None:
  47. self.register_user("alice", "wonderland")
  48. alice_token = self.login("alice", "wonderland")
  49. bob = self.register_user("bob", "uncle")
  50. channel = self.make_request(
  51. "POST",
  52. "/_matrix/client/r0/keys/query",
  53. {
  54. "device_keys": {
  55. bob: {
  56. "device_id1": True,
  57. },
  58. },
  59. },
  60. alice_token,
  61. )
  62. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  63. self.assertEqual(
  64. channel.json_body["errcode"],
  65. Codes.BAD_JSON,
  66. channel.result,
  67. )
  68. def test_requires_device_key(self) -> None:
  69. """`device_keys` is required. We should complain if it's missing."""
  70. self.register_user("alice", "wonderland")
  71. alice_token = self.login("alice", "wonderland")
  72. channel = self.make_request(
  73. "POST",
  74. "/_matrix/client/r0/keys/query",
  75. {},
  76. alice_token,
  77. )
  78. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  79. self.assertEqual(
  80. channel.json_body["errcode"],
  81. Codes.BAD_JSON,
  82. channel.result,
  83. )
  84. def test_key_query_cancellation(self) -> None:
  85. """
  86. Tests that /keys/query is cancellable and does not swallow the
  87. CancelledError.
  88. """
  89. self.register_user("alice", "wonderland")
  90. alice_token = self.login("alice", "wonderland")
  91. bob = self.register_user("bob", "uncle")
  92. channel = make_request_with_cancellation_test(
  93. "test_key_query_cancellation",
  94. self.reactor,
  95. self.site,
  96. "POST",
  97. "/_matrix/client/r0/keys/query",
  98. {
  99. "device_keys": {
  100. # Empty list means we request keys for all bob's devices
  101. bob: [],
  102. },
  103. },
  104. token=alice_token,
  105. )
  106. self.assertEqual(200, channel.code, msg=channel.result["body"])
  107. self.assertIn(bob, channel.json_body["device_keys"])