2018-11-05 13:53:44 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-11-05 13:53:44 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
2022-02-24 13:56:38 -05:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2018-11-05 13:53:44 -05:00
|
|
|
|
2019-05-01 10:32:38 -04:00
|
|
|
import synapse.rest.admin
|
2018-11-05 13:53:44 -05:00
|
|
|
from synapse.api.urls import ConsentURIBuilder
|
2021-08-17 07:57:58 -04:00
|
|
|
from synapse.rest.client import login, room
|
2018-11-05 13:53:44 -05:00
|
|
|
from synapse.rest.consent import consent_resource
|
2022-02-24 13:56:38 -05:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
from tests import unittest
|
2020-11-15 17:49:21 -05:00
|
|
|
from tests.server import FakeSite, make_request
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
class ConsentResourceTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
2019-05-02 06:59:16 -04:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2018-11-05 13:53:44 -05:00
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
user_id = True
|
|
|
|
hijack_auth = False
|
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2018-11-05 13:53:44 -05:00
|
|
|
config = self.default_config()
|
2019-05-13 16:01:14 -04:00
|
|
|
config["form_secret"] = "123abc"
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
# Make some temporary templates...
|
|
|
|
temp_consent_path = self.mktemp()
|
|
|
|
os.mkdir(temp_consent_path)
|
|
|
|
os.mkdir(os.path.join(temp_consent_path, "en"))
|
2019-05-13 16:01:14 -04:00
|
|
|
|
|
|
|
config["user_consent"] = {
|
|
|
|
"version": "1",
|
|
|
|
"template_dir": os.path.abspath(temp_consent_path),
|
|
|
|
}
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
with open(os.path.join(temp_consent_path, "en/1.html"), "w") as f:
|
|
|
|
f.write("{{version}},{{has_consented}}")
|
|
|
|
|
|
|
|
with open(os.path.join(temp_consent_path, "en/success.html"), "w") as f:
|
|
|
|
f.write("yay!")
|
|
|
|
|
|
|
|
hs = self.setup_test_homeserver(config=config)
|
|
|
|
return hs
|
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
def test_render_public_consent(self) -> None:
|
2018-11-14 05:44:46 -05:00
|
|
|
"""You can observe the terms form without specifying a user"""
|
|
|
|
resource = consent_resource.ConsentResource(self.hs)
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = make_request(
|
2021-09-24 06:01:25 -04:00
|
|
|
self.reactor,
|
|
|
|
FakeSite(resource, self.reactor),
|
|
|
|
"GET",
|
|
|
|
"/consent?v=1",
|
|
|
|
shorthand=False,
|
2020-11-13 18:11:43 -05:00
|
|
|
)
|
2022-02-24 13:56:38 -05:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2018-11-14 05:44:46 -05:00
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
def test_accept_consent(self) -> None:
|
2018-11-05 13:53:44 -05:00
|
|
|
"""
|
|
|
|
A user can use the consent form to accept the terms.
|
|
|
|
"""
|
|
|
|
uri_builder = ConsentURIBuilder(self.hs.config)
|
|
|
|
resource = consent_resource.ConsentResource(self.hs)
|
|
|
|
|
|
|
|
# Register a user
|
|
|
|
user_id = self.register_user("user", "pass")
|
|
|
|
access_token = self.login("user", "pass")
|
|
|
|
|
|
|
|
# Fetch the consent page, to get the consent version
|
|
|
|
consent_uri = (
|
|
|
|
uri_builder.build_user_consent_uri(user_id).replace("_matrix/", "")
|
|
|
|
+ "&u=user"
|
|
|
|
)
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = make_request(
|
2020-11-13 18:11:43 -05:00
|
|
|
self.reactor,
|
2021-09-24 06:01:25 -04:00
|
|
|
FakeSite(resource, self.reactor),
|
2020-11-13 18:11:43 -05:00
|
|
|
"GET",
|
|
|
|
consent_uri,
|
|
|
|
access_token=access_token,
|
|
|
|
shorthand=False,
|
2018-11-05 13:53:44 -05:00
|
|
|
)
|
2022-02-24 13:56:38 -05:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
# Get the version from the body, and whether we've consented
|
|
|
|
version, consented = channel.result["body"].decode("ascii").split(",")
|
|
|
|
self.assertEqual(consented, "False")
|
|
|
|
|
|
|
|
# POST to the consent page, saying we've agreed
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = make_request(
|
2020-11-13 18:11:43 -05:00
|
|
|
self.reactor,
|
2021-09-24 06:01:25 -04:00
|
|
|
FakeSite(resource, self.reactor),
|
2018-11-05 13:53:44 -05:00
|
|
|
"POST",
|
|
|
|
consent_uri + "&v=" + version,
|
|
|
|
access_token=access_token,
|
|
|
|
shorthand=False,
|
|
|
|
)
|
2022-02-24 13:56:38 -05:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
# Fetch the consent page, to get the consent version -- it should have
|
|
|
|
# changed
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = make_request(
|
2020-11-13 18:11:43 -05:00
|
|
|
self.reactor,
|
2021-09-24 06:01:25 -04:00
|
|
|
FakeSite(resource, self.reactor),
|
2020-11-13 18:11:43 -05:00
|
|
|
"GET",
|
|
|
|
consent_uri,
|
|
|
|
access_token=access_token,
|
|
|
|
shorthand=False,
|
2018-11-05 13:53:44 -05:00
|
|
|
)
|
2022-02-24 13:56:38 -05:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
# Get the version from the body, and check that it's the version we
|
|
|
|
# agreed to, and that we've consented to it.
|
|
|
|
version, consented = channel.result["body"].decode("ascii").split(",")
|
|
|
|
self.assertEqual(consented, "True")
|
|
|
|
self.assertEqual(version, "1")
|