1
0

test_consent.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 render
  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. request, channel = self.make_request("GET", "/consent?v=1", shorthand=False)
  52. render(request, resource, self.reactor)
  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. request, channel = self.make_request(
  69. "GET", consent_uri, access_token=access_token, shorthand=False
  70. )
  71. render(request, resource, self.reactor)
  72. self.assertEqual(channel.code, 200)
  73. # Get the version from the body, and whether we've consented
  74. version, consented = channel.result["body"].decode("ascii").split(",")
  75. self.assertEqual(consented, "False")
  76. # POST to the consent page, saying we've agreed
  77. request, channel = self.make_request(
  78. "POST",
  79. consent_uri + "&v=" + version,
  80. access_token=access_token,
  81. shorthand=False,
  82. )
  83. render(request, resource, self.reactor)
  84. self.assertEqual(channel.code, 200)
  85. # Fetch the consent page, to get the consent version -- it should have
  86. # changed
  87. request, channel = self.make_request(
  88. "GET", consent_uri, access_token=access_token, shorthand=False
  89. )
  90. render(request, resource, self.reactor)
  91. self.assertEqual(channel.code, 200)
  92. # Get the version from the body, and check that it's the version we
  93. # agreed to, and that we've consented to it.
  94. version, consented = channel.result["body"].decode("ascii").split(",")
  95. self.assertEqual(consented, "True")
  96. self.assertEqual(version, "1")