test_frontend_proxy.py 3.0 KB

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