test_openid_listener.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 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 mock import Mock, patch
  16. from parameterized import parameterized
  17. from synapse.app.federation_reader import FederationReaderServer
  18. from synapse.app.homeserver import SynapseHomeServer
  19. from tests.unittest import HomeserverTestCase
  20. class FederationReaderOpenIDListenerTests(HomeserverTestCase):
  21. def make_homeserver(self, reactor, clock):
  22. hs = self.setup_test_homeserver(
  23. http_client=None, homeserverToUse=FederationReaderServer,
  24. )
  25. return hs
  26. @parameterized.expand([
  27. (["federation"], "auth_fail"),
  28. ([], "no_resource"),
  29. (["openid", "federation"], "auth_fail"),
  30. (["openid"], "auth_fail"),
  31. ])
  32. def test_openid_listener(self, names, expectation):
  33. """
  34. Test different openid listener configurations.
  35. 401 is success here since it means we hit the handler and auth failed.
  36. """
  37. config = {
  38. "port": 8080,
  39. "bind_addresses": ["0.0.0.0"],
  40. "resources": [{"names": names}],
  41. }
  42. # Listen with the config
  43. self.hs._listen_http(config)
  44. # Grab the resource from the site that was told to listen
  45. site = self.reactor.tcpServers[0][1]
  46. try:
  47. self.resource = (
  48. site.resource.children[b"_matrix"].children[b"federation"]
  49. )
  50. except KeyError:
  51. if expectation == "no_resource":
  52. return
  53. raise
  54. request, channel = self.make_request("GET", "/_matrix/federation/v1/openid/userinfo")
  55. self.render(request)
  56. self.assertEqual(channel.code, 401)
  57. @patch("synapse.app.homeserver.KeyApiV2Resource", new=Mock())
  58. class SynapseHomeserverOpenIDListenerTests(HomeserverTestCase):
  59. def make_homeserver(self, reactor, clock):
  60. hs = self.setup_test_homeserver(
  61. http_client=None, homeserverToUse=SynapseHomeServer,
  62. )
  63. return hs
  64. @parameterized.expand([
  65. (["federation"], "auth_fail"),
  66. ([], "no_resource"),
  67. (["openid", "federation"], "auth_fail"),
  68. (["openid"], "auth_fail"),
  69. ])
  70. def test_openid_listener(self, names, expectation):
  71. """
  72. Test different openid listener configurations.
  73. 401 is success here since it means we hit the handler and auth failed.
  74. """
  75. config = {
  76. "port": 8080,
  77. "bind_addresses": ["0.0.0.0"],
  78. "resources": [{"names": names}],
  79. }
  80. # Listen with the config
  81. self.hs._listener_http(config, config)
  82. # Grab the resource from the site that was told to listen
  83. site = self.reactor.tcpServers[0][1]
  84. try:
  85. self.resource = (
  86. site.resource.children[b"_matrix"].children[b"federation"]
  87. )
  88. except KeyError:
  89. if expectation == "no_resource":
  90. return
  91. raise
  92. request, channel = self.make_request("GET", "/_matrix/federation/v1/openid/userinfo")
  93. self.render(request)
  94. self.assertEqual(channel.code, 401)