test_frontend_proxy.py 2.8 KB

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