test_frontend_proxy.py 2.9 KB

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