1
0

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. 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, name="test"):
  24. config = super().default_config(name)
  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, channel = self.make_request(b"POST", self.url, b"{}")
  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"], six.text_type)
  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"], six.text_type)
  106. self.assertIsInstance(channel.json_body["access_token"], six.text_type)
  107. self.assertIsInstance(channel.json_body["device_id"], six.text_type)