test_capabilities.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import synapse.rest.admin
  16. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  17. from synapse.rest.client.v1 import login
  18. from synapse.rest.client.v2_alpha import capabilities
  19. from tests import unittest
  20. class CapabilitiesTestCase(unittest.HomeserverTestCase):
  21. servlets = [
  22. synapse.rest.admin.register_servlets_for_client_rest_resource,
  23. capabilities.register_servlets,
  24. login.register_servlets,
  25. ]
  26. def make_homeserver(self, reactor, clock):
  27. self.url = b"/_matrix/client/r0/capabilities"
  28. hs = self.setup_test_homeserver()
  29. self.store = hs.get_datastore()
  30. self.config = hs.config
  31. return hs
  32. def test_check_auth_required(self):
  33. request, channel = self.make_request("GET", self.url)
  34. self.render(request)
  35. self.assertEqual(channel.code, 401)
  36. def test_get_room_version_capabilities(self):
  37. self.register_user("user", "pass")
  38. access_token = self.login("user", "pass")
  39. request, channel = self.make_request("GET", self.url, access_token=access_token)
  40. self.render(request)
  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.default_room_version.identifier,
  47. capabilities["m.room_versions"]["default"],
  48. )
  49. def test_get_change_password_capabilities(self):
  50. localpart = "user"
  51. password = "pass"
  52. user = self.register_user(localpart, password)
  53. access_token = self.login(user, password)
  54. request, channel = self.make_request("GET", self.url, access_token=access_token)
  55. self.render(request)
  56. capabilities = channel.json_body["capabilities"]
  57. self.assertEqual(channel.code, 200)
  58. # Test case where password is handled outside of Synapse
  59. self.assertTrue(capabilities["m.change_password"]["enabled"])
  60. self.get_success(self.store.user_set_password_hash(user, None))
  61. request, channel = self.make_request("GET", self.url, access_token=access_token)
  62. self.render(request)
  63. capabilities = channel.json_body["capabilities"]
  64. self.assertEqual(channel.code, 200)
  65. self.assertFalse(capabilities["m.change_password"]["enabled"])