test_terms_auth.py 4.3 KB

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