2018-11-05 19:53:44 +01:00
|
|
|
# Copyright 2018 New Vector
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2019-05-01 16:32:38 +02:00
|
|
|
import synapse.rest.admin
|
2018-11-05 19:53:44 +01:00
|
|
|
from synapse.api.urls import ConsentURIBuilder
|
2019-05-01 16:32:38 +02:00
|
|
|
from synapse.rest.client.v1 import login, room
|
2018-11-05 19:53:44 +01:00
|
|
|
from synapse.rest.consent import consent_resource
|
|
|
|
|
|
|
|
from tests import unittest
|
2020-11-15 23:49:21 +01:00
|
|
|
from tests.server import FakeSite, make_request
|
2018-11-05 19:53:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ConsentResourceTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
2019-05-02 12:59:16 +02:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2018-11-05 19:53:44 +01:00
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
user_id = True
|
|
|
|
hijack_auth = False
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
|
|
|
|
config = self.default_config()
|
2019-05-13 22:01:14 +02:00
|
|
|
config["public_baseurl"] = "aaaa"
|
|
|
|
config["form_secret"] = "123abc"
|
2018-11-05 19:53:44 +01:00
|
|
|
|
|
|
|
# Make some temporary templates...
|
|
|
|
temp_consent_path = self.mktemp()
|
|
|
|
os.mkdir(temp_consent_path)
|
2019-06-20 11:32:02 +02:00
|
|
|
os.mkdir(os.path.join(temp_consent_path, "en"))
|
2019-05-13 22:01:14 +02:00
|
|
|
|
|
|
|
config["user_consent"] = {
|
|
|
|
"version": "1",
|
|
|
|
"template_dir": os.path.abspath(temp_consent_path),
|
|
|
|
}
|
2018-11-05 19:53:44 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
with open(os.path.join(temp_consent_path, "en/1.html"), "w") as f:
|
2018-11-05 19:53:44 +01:00
|
|
|
f.write("{{version}},{{has_consented}}")
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
with open(os.path.join(temp_consent_path, "en/success.html"), "w") as f:
|
2018-11-05 19:53:44 +01:00
|
|
|
f.write("yay!")
|
|
|
|
|
|
|
|
hs = self.setup_test_homeserver(config=config)
|
|
|
|
return hs
|
|
|
|
|
2018-11-14 11:44:46 +01:00
|
|
|
def test_render_public_consent(self):
|
|
|
|
"""You can observe the terms form without specifying a user"""
|
|
|
|
resource = consent_resource.ConsentResource(self.hs)
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = make_request(
|
2020-11-14 00:11:43 +01:00
|
|
|
self.reactor, FakeSite(resource), "GET", "/consent?v=1", shorthand=False
|
|
|
|
)
|
2018-11-14 11:44:46 +01:00
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
2018-11-05 19:53:44 +01:00
|
|
|
def test_accept_consent(self):
|
|
|
|
"""
|
|
|
|
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 15:44:04 +01:00
|
|
|
channel = make_request(
|
2020-11-14 00:11:43 +01:00
|
|
|
self.reactor,
|
|
|
|
FakeSite(resource),
|
|
|
|
"GET",
|
|
|
|
consent_uri,
|
|
|
|
access_token=access_token,
|
|
|
|
shorthand=False,
|
2018-11-05 19:53:44 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
|
|
|
# Get the version from the body, and whether we've consented
|
2019-06-20 11:32:02 +02:00
|
|
|
version, consented = channel.result["body"].decode("ascii").split(",")
|
2018-11-05 19:53:44 +01:00
|
|
|
self.assertEqual(consented, "False")
|
|
|
|
|
|
|
|
# POST to the consent page, saying we've agreed
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = make_request(
|
2020-11-14 00:11:43 +01:00
|
|
|
self.reactor,
|
|
|
|
FakeSite(resource),
|
2018-11-05 19:53:44 +01:00
|
|
|
"POST",
|
|
|
|
consent_uri + "&v=" + version,
|
|
|
|
access_token=access_token,
|
|
|
|
shorthand=False,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
|
|
|
# Fetch the consent page, to get the consent version -- it should have
|
|
|
|
# changed
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = make_request(
|
2020-11-14 00:11:43 +01:00
|
|
|
self.reactor,
|
|
|
|
FakeSite(resource),
|
|
|
|
"GET",
|
|
|
|
consent_uri,
|
|
|
|
access_token=access_token,
|
|
|
|
shorthand=False,
|
2018-11-05 19:53:44 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
|
|
|
# Get the version from the body, and check that it's the version we
|
|
|
|
# agreed to, and that we've consented to it.
|
2019-06-20 11:32:02 +02:00
|
|
|
version, consented = channel.result["body"].decode("ascii").split(",")
|
2018-11-05 19:53:44 +01:00
|
|
|
self.assertEqual(consented, "True")
|
|
|
|
self.assertEqual(version, "1")
|