test_capabilities.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Copyright 2019 New Vector Ltd
  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 twisted.test.proto_helpers import MemoryReactor
  16. import synapse.rest.admin
  17. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  18. from synapse.rest.client import capabilities, login
  19. from synapse.server import HomeServer
  20. from synapse.util import Clock
  21. from tests import unittest
  22. from tests.unittest import override_config
  23. class CapabilitiesTestCase(unittest.HomeserverTestCase):
  24. servlets = [
  25. synapse.rest.admin.register_servlets_for_client_rest_resource,
  26. capabilities.register_servlets,
  27. login.register_servlets,
  28. ]
  29. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  30. self.url = b"/capabilities"
  31. hs = self.setup_test_homeserver()
  32. self.config = hs.config
  33. self.auth_handler = hs.get_auth_handler()
  34. return hs
  35. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  36. self.localpart = "user"
  37. self.password = "pass"
  38. self.user = self.register_user(self.localpart, self.password)
  39. def test_check_auth_required(self) -> None:
  40. channel = self.make_request("GET", self.url)
  41. self.assertEqual(channel.code, 401)
  42. def test_get_room_version_capabilities(self) -> None:
  43. access_token = self.login(self.localpart, self.password)
  44. channel = self.make_request("GET", self.url, access_token=access_token)
  45. capabilities = channel.json_body["capabilities"]
  46. self.assertEqual(channel.code, 200)
  47. for room_version in capabilities["m.room_versions"]["available"].keys():
  48. self.assertTrue(room_version in KNOWN_ROOM_VERSIONS, "" + room_version)
  49. self.assertEqual(
  50. self.config.server.default_room_version.identifier,
  51. capabilities["m.room_versions"]["default"],
  52. )
  53. def test_get_change_password_capabilities_password_login(self) -> None:
  54. access_token = self.login(self.localpart, self.password)
  55. channel = self.make_request("GET", self.url, access_token=access_token)
  56. capabilities = channel.json_body["capabilities"]
  57. self.assertEqual(channel.code, 200)
  58. self.assertTrue(capabilities["m.change_password"]["enabled"])
  59. @override_config({"password_config": {"localdb_enabled": False}})
  60. def test_get_change_password_capabilities_localdb_disabled(self) -> None:
  61. access_token = self.get_success(
  62. self.auth_handler.create_access_token_for_user_id(
  63. self.user, device_id=None, valid_until_ms=None
  64. )
  65. )
  66. channel = self.make_request("GET", self.url, access_token=access_token)
  67. capabilities = channel.json_body["capabilities"]
  68. self.assertEqual(channel.code, 200)
  69. self.assertFalse(capabilities["m.change_password"]["enabled"])
  70. @override_config({"password_config": {"enabled": False}})
  71. def test_get_change_password_capabilities_password_disabled(self) -> None:
  72. access_token = self.get_success(
  73. self.auth_handler.create_access_token_for_user_id(
  74. self.user, device_id=None, valid_until_ms=None
  75. )
  76. )
  77. channel = self.make_request("GET", self.url, access_token=access_token)
  78. capabilities = channel.json_body["capabilities"]
  79. self.assertEqual(channel.code, 200)
  80. self.assertFalse(capabilities["m.change_password"]["enabled"])
  81. def test_get_change_users_attributes_capabilities(self) -> None:
  82. """Test that server returns capabilities by default."""
  83. access_token = self.login(self.localpart, self.password)
  84. channel = self.make_request("GET", self.url, access_token=access_token)
  85. capabilities = channel.json_body["capabilities"]
  86. self.assertEqual(channel.code, HTTPStatus.OK)
  87. self.assertTrue(capabilities["m.change_password"]["enabled"])
  88. self.assertTrue(capabilities["m.set_displayname"]["enabled"])
  89. self.assertTrue(capabilities["m.set_avatar_url"]["enabled"])
  90. self.assertTrue(capabilities["m.3pid_changes"]["enabled"])
  91. @override_config({"enable_set_displayname": False})
  92. def test_get_set_displayname_capabilities_displayname_disabled(self) -> None:
  93. """Test if set displayname is disabled that the server responds it."""
  94. access_token = self.login(self.localpart, self.password)
  95. channel = self.make_request("GET", self.url, access_token=access_token)
  96. capabilities = channel.json_body["capabilities"]
  97. self.assertEqual(channel.code, HTTPStatus.OK)
  98. self.assertFalse(capabilities["m.set_displayname"]["enabled"])
  99. @override_config({"enable_set_avatar_url": False})
  100. def test_get_set_avatar_url_capabilities_avatar_url_disabled(self) -> None:
  101. """Test if set avatar_url is disabled that the server responds it."""
  102. access_token = self.login(self.localpart, self.password)
  103. channel = self.make_request("GET", self.url, access_token=access_token)
  104. capabilities = channel.json_body["capabilities"]
  105. self.assertEqual(channel.code, HTTPStatus.OK)
  106. self.assertFalse(capabilities["m.set_avatar_url"]["enabled"])
  107. @override_config({"enable_3pid_changes": False})
  108. def test_get_change_3pid_capabilities_3pid_disabled(self) -> None:
  109. """Test if change 3pid is disabled that the server responds it."""
  110. access_token = self.login(self.localpart, self.password)
  111. channel = self.make_request("GET", self.url, access_token=access_token)
  112. capabilities = channel.json_body["capabilities"]
  113. self.assertEqual(channel.code, HTTPStatus.OK)
  114. self.assertFalse(capabilities["m.3pid_changes"]["enabled"])
  115. @override_config({"experimental_features": {"msc3244_enabled": False}})
  116. def test_get_does_not_include_msc3244_fields_when_disabled(self) -> None:
  117. access_token = self.get_success(
  118. self.auth_handler.create_access_token_for_user_id(
  119. self.user, device_id=None, valid_until_ms=None
  120. )
  121. )
  122. channel = self.make_request("GET", self.url, access_token=access_token)
  123. capabilities = channel.json_body["capabilities"]
  124. self.assertEqual(channel.code, 200)
  125. self.assertNotIn(
  126. "org.matrix.msc3244.room_capabilities", capabilities["m.room_versions"]
  127. )
  128. def test_get_does_include_msc3244_fields_when_enabled(self) -> None:
  129. access_token = self.get_success(
  130. self.auth_handler.create_access_token_for_user_id(
  131. self.user, device_id=None, valid_until_ms=None
  132. )
  133. )
  134. channel = self.make_request("GET", self.url, access_token=access_token)
  135. capabilities = channel.json_body["capabilities"]
  136. self.assertEqual(channel.code, 200)
  137. for details in capabilities["m.room_versions"][
  138. "org.matrix.msc3244.room_capabilities"
  139. ].values():
  140. if details["preferred"] is not None:
  141. self.assertTrue(
  142. details["preferred"] in KNOWN_ROOM_VERSIONS,
  143. str(details["preferred"]),
  144. )
  145. self.assertGreater(len(details["support"]), 0)
  146. for room_version in details["support"]:
  147. self.assertTrue(room_version in KNOWN_ROOM_VERSIONS, str(room_version))