test_openid_listener.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright 2019 New Vector Ltd
  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 unittest.mock import Mock, patch
  15. from parameterized import parameterized
  16. from synapse.app.generic_worker import GenericWorkerServer
  17. from synapse.app.homeserver import SynapseHomeServer
  18. from synapse.config.server import parse_listener_def
  19. from tests.server import make_request
  20. from tests.unittest import HomeserverTestCase
  21. class FederationReaderOpenIDListenerTests(HomeserverTestCase):
  22. def make_homeserver(self, reactor, clock):
  23. hs = self.setup_test_homeserver(
  24. federation_http_client=None, homeserver_to_use=GenericWorkerServer
  25. )
  26. return hs
  27. def default_config(self):
  28. conf = super().default_config()
  29. # we're using FederationReaderServer, which uses a SlavedStore, so we
  30. # have to tell the FederationHandler not to try to access stuff that is only
  31. # in the primary store.
  32. conf["worker_app"] = "yes"
  33. return conf
  34. @parameterized.expand(
  35. [
  36. (["federation"], "auth_fail"),
  37. ([], "no_resource"),
  38. (["openid", "federation"], "auth_fail"),
  39. (["openid"], "auth_fail"),
  40. ]
  41. )
  42. def test_openid_listener(self, names, expectation):
  43. """
  44. Test different openid listener configurations.
  45. 401 is success here since it means we hit the handler and auth failed.
  46. """
  47. config = {
  48. "port": 8080,
  49. "type": "http",
  50. "bind_addresses": ["0.0.0.0"],
  51. "resources": [{"names": names}],
  52. }
  53. # Listen with the config
  54. self.hs._listen_http(parse_listener_def(config))
  55. # Grab the resource from the site that was told to listen
  56. site = self.reactor.tcpServers[0][1]
  57. try:
  58. site.resource.children[b"_matrix"].children[b"federation"]
  59. except KeyError:
  60. if expectation == "no_resource":
  61. return
  62. raise
  63. channel = make_request(
  64. self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
  65. )
  66. self.assertEqual(channel.code, 401)
  67. @patch("synapse.app.homeserver.KeyApiV2Resource", new=Mock())
  68. class SynapseHomeserverOpenIDListenerTests(HomeserverTestCase):
  69. def make_homeserver(self, reactor, clock):
  70. hs = self.setup_test_homeserver(
  71. federation_http_client=None, homeserver_to_use=SynapseHomeServer
  72. )
  73. return hs
  74. @parameterized.expand(
  75. [
  76. (["federation"], "auth_fail"),
  77. ([], "no_resource"),
  78. (["openid", "federation"], "auth_fail"),
  79. (["openid"], "auth_fail"),
  80. ]
  81. )
  82. def test_openid_listener(self, names, expectation):
  83. """
  84. Test different openid listener configurations.
  85. 401 is success here since it means we hit the handler and auth failed.
  86. """
  87. config = {
  88. "port": 8080,
  89. "type": "http",
  90. "bind_addresses": ["0.0.0.0"],
  91. "resources": [{"names": names}],
  92. }
  93. # Listen with the config
  94. self.hs._listener_http(self.hs.config, parse_listener_def(config))
  95. # Grab the resource from the site that was told to listen
  96. site = self.reactor.tcpServers[0][1]
  97. try:
  98. site.resource.children[b"_matrix"].children[b"federation"]
  99. except KeyError:
  100. if expectation == "no_resource":
  101. return
  102. raise
  103. channel = make_request(
  104. self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
  105. )
  106. self.assertEqual(channel.code, 401)