test_terms_auth.py 4.6 KB

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