test_keys.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. class KeyQueryTestCase(unittest.HomeserverTestCase):
  20. servlets = [
  21. keys.register_servlets,
  22. admin.register_servlets_for_client_rest_resource,
  23. login.register_servlets,
  24. ]
  25. def test_rejects_device_id_ice_key_outside_of_list(self) -> None:
  26. self.register_user("alice", "wonderland")
  27. alice_token = self.login("alice", "wonderland")
  28. bob = self.register_user("bob", "uncle")
  29. channel = self.make_request(
  30. "POST",
  31. "/_matrix/client/r0/keys/query",
  32. {
  33. "device_keys": {
  34. bob: "device_id1",
  35. },
  36. },
  37. alice_token,
  38. )
  39. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  40. self.assertEqual(
  41. channel.json_body["errcode"],
  42. Codes.BAD_JSON,
  43. channel.result,
  44. )
  45. def test_rejects_device_key_given_as_map_to_bool(self) -> None:
  46. self.register_user("alice", "wonderland")
  47. alice_token = self.login("alice", "wonderland")
  48. bob = self.register_user("bob", "uncle")
  49. channel = self.make_request(
  50. "POST",
  51. "/_matrix/client/r0/keys/query",
  52. {
  53. "device_keys": {
  54. bob: {
  55. "device_id1": True,
  56. },
  57. },
  58. },
  59. alice_token,
  60. )
  61. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  62. self.assertEqual(
  63. channel.json_body["errcode"],
  64. Codes.BAD_JSON,
  65. channel.result,
  66. )
  67. def test_requires_device_key(self) -> None:
  68. """`device_keys` is required. We should complain if it's missing."""
  69. self.register_user("alice", "wonderland")
  70. alice_token = self.login("alice", "wonderland")
  71. channel = self.make_request(
  72. "POST",
  73. "/_matrix/client/r0/keys/query",
  74. {},
  75. alice_token,
  76. )
  77. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  78. self.assertEqual(
  79. channel.json_body["errcode"],
  80. Codes.BAD_JSON,
  81. channel.result,
  82. )