test_terms_auth.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright 2018 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 unittest.mock import Mock
  15. from twisted.internet.interfaces import IReactorTime
  16. from twisted.test.proto_helpers import MemoryReactor, MemoryReactorClock
  17. from synapse.rest.client.register import register_servlets
  18. from synapse.server import HomeServer
  19. from synapse.types import JsonDict
  20. from synapse.util import Clock
  21. from tests import unittest
  22. class TermsTestCase(unittest.HomeserverTestCase):
  23. servlets = [register_servlets]
  24. def default_config(self) -> JsonDict:
  25. config = super().default_config()
  26. config.update(
  27. {
  28. "public_baseurl": "https://example.org/",
  29. "user_consent": {
  30. "version": "1.0",
  31. "policy_name": "My Cool Privacy Policy",
  32. "template_dir": "/",
  33. "require_at_registration": True,
  34. },
  35. }
  36. )
  37. return config
  38. def prepare(
  39. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  40. ) -> None:
  41. # type-ignore: mypy-zope doesn't seem to recognise that MemoryReactorClock
  42. # implements IReactorTime, via inheritance from twisted.internet.testing.Clock
  43. self.clock: IReactorTime = MemoryReactorClock() # type: ignore[assignment]
  44. self.hs_clock = Clock(self.clock)
  45. self.url = "/_matrix/client/r0/register"
  46. self.registration_handler = Mock()
  47. self.auth_handler = Mock()
  48. self.device_handler = Mock()
  49. def test_ui_auth(self) -> None:
  50. # Do a UI auth request
  51. request_data: JsonDict = {"username": "kermit", "password": "monkey"}
  52. channel = self.make_request(b"POST", self.url, request_data)
  53. self.assertEqual(channel.code, 401, channel.result)
  54. self.assertTrue(channel.json_body is not None)
  55. self.assertIsInstance(channel.json_body["session"], str)
  56. self.assertIsInstance(channel.json_body["flows"], list)
  57. for flow in channel.json_body["flows"]:
  58. self.assertIsInstance(flow["stages"], list)
  59. self.assertTrue(len(flow["stages"]) > 0)
  60. self.assertTrue("m.login.terms" in flow["stages"])
  61. expected_params = {
  62. "m.login.terms": {
  63. "policies": {
  64. "privacy_policy": {
  65. "en": {
  66. "name": "My Cool Privacy Policy",
  67. "url": "https://example.org/_matrix/consent?v=1.0",
  68. },
  69. "version": "1.0",
  70. }
  71. }
  72. }
  73. }
  74. self.assertIsInstance(channel.json_body["params"], dict)
  75. self.assertDictContainsSubset(channel.json_body["params"], expected_params)
  76. # We have to complete the dummy auth stage before completing the terms stage
  77. request_data = {
  78. "username": "kermit",
  79. "password": "monkey",
  80. "auth": {
  81. "session": channel.json_body["session"],
  82. "type": "m.login.dummy",
  83. },
  84. }
  85. self.registration_handler.check_username = Mock(return_value=True)
  86. channel = self.make_request(b"POST", self.url, request_data)
  87. # We don't bother checking that the response is correct - we'll leave that to
  88. # other tests. We just want to make sure we're on the right path.
  89. self.assertEqual(channel.code, 401, channel.result)
  90. # Finish the UI auth for terms
  91. request_data = {
  92. "username": "kermit",
  93. "password": "monkey",
  94. "auth": {
  95. "session": channel.json_body["session"],
  96. "type": "m.login.terms",
  97. },
  98. }
  99. channel = self.make_request(b"POST", self.url, request_data)
  100. # We're interested in getting a response that looks like a successful
  101. # registration, not so much that the details are exactly what we want.
  102. self.assertEqual(channel.code, 200, channel.result)
  103. self.assertTrue(channel.json_body is not None)
  104. self.assertIsInstance(channel.json_body["user_id"], str)
  105. self.assertIsInstance(channel.json_body["access_token"], str)
  106. self.assertIsInstance(channel.json_body["device_id"], str)