test_cas.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 mock import Mock
  15. from synapse.handlers.cas_handler import CasResponse
  16. from tests.test_utils import simple_async_mock
  17. from tests.unittest import HomeserverTestCase
  18. # These are a few constants that are used as config parameters in the tests.
  19. BASE_URL = "https://synapse/"
  20. SERVER_URL = "https://issuer/"
  21. class CasHandlerTestCase(HomeserverTestCase):
  22. def default_config(self):
  23. config = super().default_config()
  24. config["public_baseurl"] = BASE_URL
  25. cas_config = {
  26. "enabled": True,
  27. "server_url": SERVER_URL,
  28. "service_url": BASE_URL,
  29. }
  30. config["cas_config"] = cas_config
  31. return config
  32. def make_homeserver(self, reactor, clock):
  33. hs = self.setup_test_homeserver()
  34. self.handler = hs.get_cas_handler()
  35. # Reduce the number of attempts when generating MXIDs.
  36. sso_handler = hs.get_sso_handler()
  37. sso_handler._MAP_USERNAME_RETRIES = 3
  38. return hs
  39. def test_map_cas_user_to_user(self):
  40. """Ensure that mapping the CAS user returned from a provider to an MXID works properly."""
  41. # stub out the auth handler
  42. auth_handler = self.hs.get_auth_handler()
  43. auth_handler.complete_sso_login = simple_async_mock()
  44. cas_response = CasResponse("test_user", {})
  45. request = _mock_request()
  46. self.get_success(
  47. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  48. )
  49. # check that the auth handler got called as expected
  50. auth_handler.complete_sso_login.assert_called_once_with(
  51. "@test_user:test", request, "redirect_uri", None, new_user=True
  52. )
  53. def test_map_cas_user_to_existing_user(self):
  54. """Existing users can log in with CAS account."""
  55. store = self.hs.get_datastore()
  56. self.get_success(
  57. store.register_user(user_id="@test_user:test", password_hash=None)
  58. )
  59. # stub out the auth handler
  60. auth_handler = self.hs.get_auth_handler()
  61. auth_handler.complete_sso_login = simple_async_mock()
  62. # Map a user via SSO.
  63. cas_response = CasResponse("test_user", {})
  64. request = _mock_request()
  65. self.get_success(
  66. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  67. )
  68. # check that the auth handler got called as expected
  69. auth_handler.complete_sso_login.assert_called_once_with(
  70. "@test_user:test", request, "redirect_uri", None, new_user=False
  71. )
  72. # Subsequent calls should map to the same mxid.
  73. auth_handler.complete_sso_login.reset_mock()
  74. self.get_success(
  75. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  76. )
  77. auth_handler.complete_sso_login.assert_called_once_with(
  78. "@test_user:test", request, "redirect_uri", None, new_user=False
  79. )
  80. def test_map_cas_user_to_invalid_localpart(self):
  81. """CAS automaps invalid characters to base-64 encoding."""
  82. # stub out the auth handler
  83. auth_handler = self.hs.get_auth_handler()
  84. auth_handler.complete_sso_login = simple_async_mock()
  85. cas_response = CasResponse("föö", {})
  86. request = _mock_request()
  87. self.get_success(
  88. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  89. )
  90. # check that the auth handler got called as expected
  91. auth_handler.complete_sso_login.assert_called_once_with(
  92. "@f=c3=b6=c3=b6:test", request, "redirect_uri", None, new_user=True
  93. )
  94. def _mock_request():
  95. """Returns a mock which will stand in as a SynapseRequest"""
  96. return Mock(spec=["getClientIP", "getHeader"])