1
0

test_well_known.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 http import HTTPStatus
  15. from twisted.web.resource import Resource
  16. from synapse.rest.well_known import well_known_resource
  17. from tests import unittest
  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, HTTPStatus.OK)
  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, HTTPStatus.NOT_FOUND)
  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, HTTPStatus.OK)
  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, HTTPStatus.OK)
  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, HTTPStatus.NOT_FOUND)