test_openid_listener.py 3.6 KB

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