test_frontend_proxy.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 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. from synapse.app.generic_worker import GenericWorkerServer
  16. from tests.unittest import HomeserverTestCase
  17. class FrontendProxyTests(HomeserverTestCase):
  18. def make_homeserver(self, reactor, clock):
  19. hs = self.setup_test_homeserver(
  20. http_client=None, homeserver_to_use=GenericWorkerServer
  21. )
  22. return hs
  23. def default_config(self):
  24. c = super().default_config()
  25. c["worker_app"] = "synapse.app.frontend_proxy"
  26. c["worker_listeners"] = [
  27. {
  28. "type": "http",
  29. "port": 8080,
  30. "bind_addresses": ["0.0.0.0"],
  31. "resources": [{"names": ["client"]}],
  32. }
  33. ]
  34. return c
  35. def test_listen_http_with_presence_enabled(self):
  36. """
  37. When presence is on, the stub servlet will not register.
  38. """
  39. # Presence is on
  40. self.hs.config.use_presence = True
  41. # Listen with the config
  42. self.hs._listen_http(self.hs.config.worker.worker_listeners[0])
  43. # Grab the resource from the site that was told to listen
  44. self.assertEqual(len(self.reactor.tcpServers), 1)
  45. site = self.reactor.tcpServers[0][1]
  46. self.resource = site.resource.children[b"_matrix"].children[b"client"]
  47. request, channel = self.make_request("PUT", "presence/a/status")
  48. self.render(request)
  49. # 400 + unrecognised, because nothing is registered
  50. self.assertEqual(channel.code, 400)
  51. self.assertEqual(channel.json_body["errcode"], "M_UNRECOGNIZED")
  52. def test_listen_http_with_presence_disabled(self):
  53. """
  54. When presence is off, the stub servlet will register.
  55. """
  56. # Presence is off
  57. self.hs.config.use_presence = False
  58. # Listen with the config
  59. self.hs._listen_http(self.hs.config.worker.worker_listeners[0])
  60. # Grab the resource from the site that was told to listen
  61. self.assertEqual(len(self.reactor.tcpServers), 1)
  62. site = self.reactor.tcpServers[0][1]
  63. self.resource = site.resource.children[b"_matrix"].children[b"client"]
  64. request, channel = self.make_request("PUT", "presence/a/status")
  65. self.render(request)
  66. # 401, because the stub servlet still checks authentication
  67. self.assertEqual(channel.code, 401)
  68. self.assertEqual(channel.json_body["errcode"], "M_MISSING_TOKEN")