test_consent.py 4.1 KB

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