test_openid_listener.py 4.4 KB

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