test_well_known.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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({"serve_server_wellknown": True})
  53. def test_server_well_known(self) -> None:
  54. channel = self.make_request(
  55. "GET", "/.well-known/matrix/server", shorthand=False
  56. )
  57. self.assertEqual(channel.code, HTTPStatus.OK)
  58. self.assertEqual(
  59. channel.json_body,
  60. {"m.server": "test:443"},
  61. )
  62. def test_server_well_known_disabled(self) -> None:
  63. channel = self.make_request(
  64. "GET", "/.well-known/matrix/server", shorthand=False
  65. )
  66. self.assertEqual(channel.code, HTTPStatus.NOT_FOUND)