test_well_known.py 4.4 KB

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