1
0

test_capabilities.py 8.6 KB

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