test_terms_auth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. channel = self.make_request(b"POST", self.url, request_data)
  47. self.assertEquals(channel.result["code"], b"401", channel.result)
  48. self.assertTrue(channel.json_body is not None)
  49. self.assertIsInstance(channel.json_body["session"], str)
  50. self.assertIsInstance(channel.json_body["flows"], list)
  51. for flow in channel.json_body["flows"]:
  52. self.assertIsInstance(flow["stages"], list)
  53. self.assertTrue(len(flow["stages"]) > 0)
  54. self.assertTrue("m.login.terms" in flow["stages"])
  55. expected_params = {
  56. "m.login.terms": {
  57. "policies": {
  58. "privacy_policy": {
  59. "en": {
  60. "name": "My Cool Privacy Policy",
  61. "url": "https://example.org/_matrix/consent?v=1.0",
  62. },
  63. "version": "1.0",
  64. }
  65. }
  66. }
  67. }
  68. self.assertIsInstance(channel.json_body["params"], dict)
  69. self.assertDictContainsSubset(channel.json_body["params"], expected_params)
  70. # We have to complete the dummy auth stage before completing the terms stage
  71. request_data = json.dumps(
  72. {
  73. "username": "kermit",
  74. "password": "monkey",
  75. "auth": {
  76. "session": channel.json_body["session"],
  77. "type": "m.login.dummy",
  78. },
  79. }
  80. )
  81. self.registration_handler.check_username = Mock(return_value=True)
  82. channel = self.make_request(b"POST", self.url, request_data)
  83. # We don't bother checking that the response is correct - we'll leave that to
  84. # other tests. We just want to make sure we're on the right path.
  85. self.assertEquals(channel.result["code"], b"401", channel.result)
  86. # Finish the UI auth for terms
  87. request_data = json.dumps(
  88. {
  89. "username": "kermit",
  90. "password": "monkey",
  91. "auth": {
  92. "session": channel.json_body["session"],
  93. "type": "m.login.terms",
  94. },
  95. }
  96. )
  97. channel = self.make_request(b"POST", self.url, request_data)
  98. # We're interested in getting a response that looks like a successful
  99. # registration, not so much that the details are exactly what we want.
  100. self.assertEquals(channel.result["code"], b"200", channel.result)
  101. self.assertTrue(channel.json_body is not None)
  102. self.assertIsInstance(channel.json_body["user_id"], str)
  103. self.assertIsInstance(channel.json_body["access_token"], str)
  104. self.assertIsInstance(channel.json_body["device_id"], str)