1
0

test_cas.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 unittest.mock import Mock
  15. from synapse.handlers.cas import CasResponse
  16. from tests.test_utils import simple_async_mock
  17. from tests.unittest import HomeserverTestCase, override_config
  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. # Update this config with what's in the default config so that
  31. # override_config works as expected.
  32. cas_config.update(config.get("cas_config", {}))
  33. config["cas_config"] = cas_config
  34. return config
  35. def make_homeserver(self, reactor, clock):
  36. hs = self.setup_test_homeserver()
  37. self.handler = hs.get_cas_handler()
  38. # Reduce the number of attempts when generating MXIDs.
  39. sso_handler = hs.get_sso_handler()
  40. sso_handler._MAP_USERNAME_RETRIES = 3
  41. return hs
  42. def test_map_cas_user_to_user(self):
  43. """Ensure that mapping the CAS user returned from a provider to an MXID works properly."""
  44. # stub out the auth handler
  45. auth_handler = self.hs.get_auth_handler()
  46. auth_handler.complete_sso_login = simple_async_mock()
  47. cas_response = CasResponse("test_user", {})
  48. request = _mock_request()
  49. self.get_success(
  50. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  51. )
  52. # check that the auth handler got called as expected
  53. auth_handler.complete_sso_login.assert_called_once_with(
  54. "@test_user:test", "cas", request, "redirect_uri", None, new_user=True
  55. )
  56. def test_map_cas_user_to_existing_user(self):
  57. """Existing users can log in with CAS account."""
  58. store = self.hs.get_datastore()
  59. self.get_success(
  60. store.register_user(user_id="@test_user:test", password_hash=None)
  61. )
  62. # stub out the auth handler
  63. auth_handler = self.hs.get_auth_handler()
  64. auth_handler.complete_sso_login = simple_async_mock()
  65. # Map a user via SSO.
  66. cas_response = CasResponse("test_user", {})
  67. request = _mock_request()
  68. self.get_success(
  69. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  70. )
  71. # check that the auth handler got called as expected
  72. auth_handler.complete_sso_login.assert_called_once_with(
  73. "@test_user:test", "cas", request, "redirect_uri", None, new_user=False
  74. )
  75. # Subsequent calls should map to the same mxid.
  76. auth_handler.complete_sso_login.reset_mock()
  77. self.get_success(
  78. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  79. )
  80. auth_handler.complete_sso_login.assert_called_once_with(
  81. "@test_user:test", "cas", request, "redirect_uri", None, new_user=False
  82. )
  83. def test_map_cas_user_to_invalid_localpart(self):
  84. """CAS automaps invalid characters to base-64 encoding."""
  85. # stub out the auth handler
  86. auth_handler = self.hs.get_auth_handler()
  87. auth_handler.complete_sso_login = simple_async_mock()
  88. cas_response = CasResponse("föö", {})
  89. request = _mock_request()
  90. self.get_success(
  91. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  92. )
  93. # check that the auth handler got called as expected
  94. auth_handler.complete_sso_login.assert_called_once_with(
  95. "@f=c3=b6=c3=b6:test", "cas", request, "redirect_uri", None, new_user=True
  96. )
  97. @override_config(
  98. {
  99. "cas_config": {
  100. "required_attributes": {"userGroup": "staff", "department": None}
  101. }
  102. }
  103. )
  104. def test_required_attributes(self):
  105. """The required attributes must be met from the CAS response."""
  106. # stub out the auth handler
  107. auth_handler = self.hs.get_auth_handler()
  108. auth_handler.complete_sso_login = simple_async_mock()
  109. # The response doesn't have the proper userGroup or department.
  110. cas_response = CasResponse("test_user", {})
  111. request = _mock_request()
  112. self.get_success(
  113. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  114. )
  115. auth_handler.complete_sso_login.assert_not_called()
  116. # The response doesn't have any department.
  117. cas_response = CasResponse("test_user", {"userGroup": "staff"})
  118. request.reset_mock()
  119. self.get_success(
  120. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  121. )
  122. auth_handler.complete_sso_login.assert_not_called()
  123. # Add the proper attributes and it should succeed.
  124. cas_response = CasResponse(
  125. "test_user", {"userGroup": ["staff", "admin"], "department": ["sales"]}
  126. )
  127. request.reset_mock()
  128. self.get_success(
  129. self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
  130. )
  131. # check that the auth handler got called as expected
  132. auth_handler.complete_sso_login.assert_called_once_with(
  133. "@test_user:test", "cas", request, "redirect_uri", None, new_user=True
  134. )
  135. def _mock_request():
  136. """Returns a mock which will stand in as a SynapseRequest"""
  137. return Mock(spec=["getClientIP", "getHeader", "_disconnected"])