test_consent.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import synapse.rest.admin
  17. from synapse.api.urls import ConsentURIBuilder
  18. from synapse.rest.client.v1 import login, room
  19. from synapse.rest.consent import consent_resource
  20. from tests import unittest
  21. from tests.server import FakeSite, make_request
  22. class ConsentResourceTestCase(unittest.HomeserverTestCase):
  23. servlets = [
  24. synapse.rest.admin.register_servlets_for_client_rest_resource,
  25. room.register_servlets,
  26. login.register_servlets,
  27. ]
  28. user_id = True
  29. hijack_auth = False
  30. def make_homeserver(self, reactor, clock):
  31. config = self.default_config()
  32. config["public_baseurl"] = "aaaa"
  33. config["form_secret"] = "123abc"
  34. # Make some temporary templates...
  35. temp_consent_path = self.mktemp()
  36. os.mkdir(temp_consent_path)
  37. os.mkdir(os.path.join(temp_consent_path, "en"))
  38. config["user_consent"] = {
  39. "version": "1",
  40. "template_dir": os.path.abspath(temp_consent_path),
  41. }
  42. with open(os.path.join(temp_consent_path, "en/1.html"), "w") as f:
  43. f.write("{{version}},{{has_consented}}")
  44. with open(os.path.join(temp_consent_path, "en/success.html"), "w") as f:
  45. f.write("yay!")
  46. hs = self.setup_test_homeserver(config=config)
  47. return hs
  48. def test_render_public_consent(self):
  49. """You can observe the terms form without specifying a user"""
  50. resource = consent_resource.ConsentResource(self.hs)
  51. channel = make_request(
  52. self.reactor, FakeSite(resource), "GET", "/consent?v=1", shorthand=False
  53. )
  54. self.assertEqual(channel.code, 200)
  55. def test_accept_consent(self):
  56. """
  57. A user can use the consent form to accept the terms.
  58. """
  59. uri_builder = ConsentURIBuilder(self.hs.config)
  60. resource = consent_resource.ConsentResource(self.hs)
  61. # Register a user
  62. user_id = self.register_user("user", "pass")
  63. access_token = self.login("user", "pass")
  64. # Fetch the consent page, to get the consent version
  65. consent_uri = (
  66. uri_builder.build_user_consent_uri(user_id).replace("_matrix/", "")
  67. + "&u=user"
  68. )
  69. channel = make_request(
  70. self.reactor,
  71. FakeSite(resource),
  72. "GET",
  73. consent_uri,
  74. access_token=access_token,
  75. shorthand=False,
  76. )
  77. self.assertEqual(channel.code, 200)
  78. # Get the version from the body, and whether we've consented
  79. version, consented = channel.result["body"].decode("ascii").split(",")
  80. self.assertEqual(consented, "False")
  81. # POST to the consent page, saying we've agreed
  82. channel = make_request(
  83. self.reactor,
  84. FakeSite(resource),
  85. "POST",
  86. consent_uri + "&v=" + version,
  87. access_token=access_token,
  88. shorthand=False,
  89. )
  90. self.assertEqual(channel.code, 200)
  91. # Fetch the consent page, to get the consent version -- it should have
  92. # changed
  93. channel = make_request(
  94. self.reactor,
  95. FakeSite(resource),
  96. "GET",
  97. consent_uri,
  98. access_token=access_token,
  99. shorthand=False,
  100. )
  101. self.assertEqual(channel.code, 200)
  102. # Get the version from the body, and check that it's the version we
  103. # agreed to, and that we've consented to it.
  104. version, consented = channel.result["body"].decode("ascii").split(",")
  105. self.assertEqual(consented, "True")
  106. self.assertEqual(version, "1")