test_terms_auth.py 4.6 KB

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