1
0

test_consent.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2018 New Vector
  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 os
  15. import synapse.rest.admin
  16. from synapse.api.urls import ConsentURIBuilder
  17. from synapse.rest.client import login, room
  18. from synapse.rest.consent import consent_resource
  19. from tests import unittest
  20. from tests.server import FakeSite, make_request
  21. class ConsentResourceTestCase(unittest.HomeserverTestCase):
  22. servlets = [
  23. synapse.rest.admin.register_servlets_for_client_rest_resource,
  24. room.register_servlets,
  25. login.register_servlets,
  26. ]
  27. user_id = True
  28. hijack_auth = False
  29. def make_homeserver(self, reactor, clock):
  30. config = self.default_config()
  31. config["form_secret"] = "123abc"
  32. # Make some temporary templates...
  33. temp_consent_path = self.mktemp()
  34. os.mkdir(temp_consent_path)
  35. os.mkdir(os.path.join(temp_consent_path, "en"))
  36. config["user_consent"] = {
  37. "version": "1",
  38. "template_dir": os.path.abspath(temp_consent_path),
  39. }
  40. with open(os.path.join(temp_consent_path, "en/1.html"), "w") as f:
  41. f.write("{{version}},{{has_consented}}")
  42. with open(os.path.join(temp_consent_path, "en/success.html"), "w") as f:
  43. f.write("yay!")
  44. hs = self.setup_test_homeserver(config=config)
  45. return hs
  46. def test_render_public_consent(self):
  47. """You can observe the terms form without specifying a user"""
  48. resource = consent_resource.ConsentResource(self.hs)
  49. channel = make_request(
  50. self.reactor,
  51. FakeSite(resource, self.reactor),
  52. "GET",
  53. "/consent?v=1",
  54. shorthand=False,
  55. )
  56. self.assertEqual(channel.code, 200)
  57. def test_accept_consent(self):
  58. """
  59. A user can use the consent form to accept the terms.
  60. """
  61. uri_builder = ConsentURIBuilder(self.hs.config)
  62. resource = consent_resource.ConsentResource(self.hs)
  63. # Register a user
  64. user_id = self.register_user("user", "pass")
  65. access_token = self.login("user", "pass")
  66. # Fetch the consent page, to get the consent version
  67. consent_uri = (
  68. uri_builder.build_user_consent_uri(user_id).replace("_matrix/", "")
  69. + "&u=user"
  70. )
  71. channel = make_request(
  72. self.reactor,
  73. FakeSite(resource, self.reactor),
  74. "GET",
  75. consent_uri,
  76. access_token=access_token,
  77. shorthand=False,
  78. )
  79. self.assertEqual(channel.code, 200)
  80. # Get the version from the body, and whether we've consented
  81. version, consented = channel.result["body"].decode("ascii").split(",")
  82. self.assertEqual(consented, "False")
  83. # POST to the consent page, saying we've agreed
  84. channel = make_request(
  85. self.reactor,
  86. FakeSite(resource, self.reactor),
  87. "POST",
  88. consent_uri + "&v=" + version,
  89. access_token=access_token,
  90. shorthand=False,
  91. )
  92. self.assertEqual(channel.code, 200)
  93. # Fetch the consent page, to get the consent version -- it should have
  94. # changed
  95. channel = make_request(
  96. self.reactor,
  97. FakeSite(resource, self.reactor),
  98. "GET",
  99. consent_uri,
  100. access_token=access_token,
  101. shorthand=False,
  102. )
  103. self.assertEqual(channel.code, 200)
  104. # Get the version from the body, and check that it's the version we
  105. # agreed to, and that we've consented to it.
  106. version, consented = channel.result["body"].decode("ascii").split(",")
  107. self.assertEqual(consented, "True")
  108. self.assertEqual(version, "1")