test_well_known.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2018 New Vector
  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 twisted.web.resource import Resource
  15. from synapse.rest.well_known import well_known_resource
  16. from tests import unittest
  17. try:
  18. import authlib # noqa: F401
  19. HAS_AUTHLIB = True
  20. except ImportError:
  21. HAS_AUTHLIB = False
  22. class WellKnownTests(unittest.HomeserverTestCase):
  23. def create_test_resource(self) -> Resource:
  24. # replace the JsonResource with a Resource wrapping the WellKnownResource
  25. res = Resource()
  26. res.putChild(b".well-known", well_known_resource(self.hs))
  27. return res
  28. @unittest.override_config(
  29. {
  30. "public_baseurl": "https://tesths",
  31. "default_identity_server": "https://testis",
  32. }
  33. )
  34. def test_client_well_known(self) -> None:
  35. channel = self.make_request(
  36. "GET", "/.well-known/matrix/client", shorthand=False
  37. )
  38. self.assertEqual(channel.code, 200)
  39. self.assertEqual(
  40. channel.json_body,
  41. {
  42. "m.homeserver": {"base_url": "https://tesths/"},
  43. "m.identity_server": {"base_url": "https://testis"},
  44. },
  45. )
  46. @unittest.override_config(
  47. {
  48. "public_baseurl": None,
  49. }
  50. )
  51. def test_client_well_known_no_public_baseurl(self) -> None:
  52. channel = self.make_request(
  53. "GET", "/.well-known/matrix/client", shorthand=False
  54. )
  55. self.assertEqual(channel.code, 404)
  56. @unittest.override_config(
  57. {
  58. "public_baseurl": "https://tesths",
  59. "default_identity_server": "https://testis",
  60. "extra_well_known_client_content": {"custom": False},
  61. }
  62. )
  63. def test_client_well_known_custom(self) -> None:
  64. channel = self.make_request(
  65. "GET", "/.well-known/matrix/client", shorthand=False
  66. )
  67. self.assertEqual(channel.code, 200)
  68. self.assertEqual(
  69. channel.json_body,
  70. {
  71. "m.homeserver": {"base_url": "https://tesths/"},
  72. "m.identity_server": {"base_url": "https://testis"},
  73. "custom": False,
  74. },
  75. )
  76. @unittest.override_config({"serve_server_wellknown": True})
  77. def test_server_well_known(self) -> None:
  78. channel = self.make_request(
  79. "GET", "/.well-known/matrix/server", shorthand=False
  80. )
  81. self.assertEqual(channel.code, 200)
  82. self.assertEqual(
  83. channel.json_body,
  84. {"m.server": "test:443"},
  85. )
  86. def test_server_well_known_disabled(self) -> None:
  87. channel = self.make_request(
  88. "GET", "/.well-known/matrix/server", shorthand=False
  89. )
  90. self.assertEqual(channel.code, 404)
  91. @unittest.skip_unless(HAS_AUTHLIB, "requires authlib")
  92. @unittest.override_config(
  93. {
  94. "public_baseurl": "https://homeserver", # this is only required so that client well known is served
  95. "experimental_features": {
  96. "msc3861": {
  97. "enabled": True,
  98. "issuer": "https://issuer",
  99. "account_management_url": "https://my-account.issuer",
  100. "client_id": "id",
  101. "client_auth_method": "client_secret_post",
  102. "client_secret": "secret",
  103. },
  104. },
  105. "disable_registration": True,
  106. }
  107. )
  108. def test_client_well_known_msc3861_oauth_delegation(self) -> None:
  109. channel = self.make_request(
  110. "GET", "/.well-known/matrix/client", shorthand=False
  111. )
  112. self.assertEqual(channel.code, 200)
  113. self.assertEqual(
  114. channel.json_body,
  115. {
  116. "m.homeserver": {"base_url": "https://homeserver/"},
  117. "org.matrix.msc2965.authentication": {
  118. "issuer": "https://issuer",
  119. "account": "https://my-account.issuer",
  120. },
  121. },
  122. )