2019-05-08 18:01:30 +02:00
|
|
|
#
|
2023-11-21 21:29:58 +01: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]
|
2019-05-08 18:01:30 +02:00
|
|
|
#
|
|
|
|
#
|
2021-11-12 20:56:00 +01:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2022-06-17 13:19:22 +02:00
|
|
|
from synapse.appservice import ApplicationService
|
2019-05-08 22:57:03 +02:00
|
|
|
from synapse.rest import admin
|
2021-08-17 13:57:58 +02:00
|
|
|
from synapse.rest.client import directory, login, room
|
2021-11-12 20:56:00 +01:00
|
|
|
from synapse.server import HomeServer
|
2019-05-08 18:01:30 +02:00
|
|
|
from synapse.types import RoomAlias
|
2021-11-12 20:56:00 +01:00
|
|
|
from synapse.util import Clock
|
2019-05-08 18:01:30 +02:00
|
|
|
from synapse.util.stringutils import random_string
|
|
|
|
|
|
|
|
from tests import unittest
|
2020-10-05 22:40:51 +02:00
|
|
|
from tests.unittest import override_config
|
2019-05-08 18:01:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DirectoryTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
2019-05-08 22:57:03 +02:00
|
|
|
admin.register_servlets_for_client_rest_resource,
|
2019-05-08 18:01:30 +02:00
|
|
|
directory.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2019-05-08 18:01:30 +02:00
|
|
|
config = self.default_config()
|
2019-05-13 22:01:14 +02:00
|
|
|
config["require_membership_for_aliases"] = True
|
2019-05-08 18:01:30 +02:00
|
|
|
|
|
|
|
self.hs = self.setup_test_homeserver(config=config)
|
|
|
|
|
|
|
|
return self.hs
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def prepare(
|
|
|
|
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
|
|
|
|
) -> None:
|
|
|
|
"""Create two local users and access tokens for them.
|
|
|
|
One of them creates a room."""
|
2019-05-08 18:01:30 +02:00
|
|
|
self.room_owner = self.register_user("room_owner", "test")
|
|
|
|
self.room_owner_tok = self.login("room_owner", "test")
|
|
|
|
|
|
|
|
self.room_id = self.helper.create_room_as(
|
2019-05-10 07:12:11 +02:00
|
|
|
self.room_owner, tok=self.room_owner_tok
|
2019-05-08 18:01:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.user = self.register_user("user", "test")
|
|
|
|
self.user_tok = self.login("user", "test")
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_state_event_not_in_room(self) -> None:
|
2020-03-17 12:37:04 +01:00
|
|
|
self.ensure_user_left_room()
|
2021-11-12 20:56:00 +01:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.FORBIDDEN)
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_directory_endpoint_not_in_room(self) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
self.ensure_user_left_room()
|
2021-11-12 20:56:00 +01:00
|
|
|
self.set_alias_via_directory(HTTPStatus.FORBIDDEN)
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_state_event_in_room_too_long(self) -> None:
|
2020-03-17 12:37:04 +01:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 20:56:00 +01:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.BAD_REQUEST, alias_length=256)
|
2020-03-17 12:37:04 +01:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_directory_in_room_too_long(self) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 20:56:00 +01:00
|
|
|
self.set_alias_via_directory(HTTPStatus.BAD_REQUEST, alias_length=256)
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2020-10-05 22:40:51 +02:00
|
|
|
@override_config({"default_room_version": 5})
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_state_event_user_in_v5_room(self) -> None:
|
2020-10-05 22:40:51 +02:00
|
|
|
"""Test that a regular user can add alias events before room v6"""
|
2020-03-17 12:37:04 +01:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 20:56:00 +01:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.OK)
|
2020-03-17 12:37:04 +01:00
|
|
|
|
2020-10-05 22:40:51 +02:00
|
|
|
@override_config({"default_room_version": 6})
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_state_event_v6_room(self) -> None:
|
2020-10-05 22:40:51 +02:00
|
|
|
"""Test that a regular user can *not* add alias events from room v6"""
|
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 20:56:00 +01:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.FORBIDDEN)
|
2020-10-05 22:40:51 +02:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_directory_in_room(self) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 20:56:00 +01:00
|
|
|
self.set_alias_via_directory(HTTPStatus.OK)
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_room_creation_too_long(self) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
url = "/_matrix/client/r0/createRoom"
|
|
|
|
|
|
|
|
# We use deliberately a localpart under the length threshold so
|
|
|
|
# that we can make sure that the check is done on the whole alias.
|
2022-07-17 23:28:45 +02:00
|
|
|
request_data = {"room_alias_name": random_string(256 - len(self.hs.hostname))}
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2019-05-10 07:12:11 +02:00
|
|
|
"POST", url, request_data, access_token=self.user_tok
|
2019-05-08 18:01:30 +02:00
|
|
|
)
|
2021-11-12 20:56:00 +01:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_room_creation(self) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
url = "/_matrix/client/r0/createRoom"
|
|
|
|
|
|
|
|
# Check with an alias of allowed length. There should already be
|
|
|
|
# a test that ensures it works in test_register.py, but let's be
|
|
|
|
# as cautious as possible here.
|
2022-07-17 23:28:45 +02:00
|
|
|
request_data = {"room_alias_name": random_string(5)}
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2019-05-10 07:12:11 +02:00
|
|
|
"POST", url, request_data, access_token=self.user_tok
|
2019-05-08 18:01:30 +02:00
|
|
|
)
|
2021-11-12 20:56:00 +01:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
|
|
|
|
|
|
|
|
def test_deleting_alias_via_directory(self) -> None:
|
|
|
|
# Add an alias for the room. We must be joined to do so.
|
|
|
|
self.ensure_user_joined_room()
|
|
|
|
alias = self.set_alias_via_directory(HTTPStatus.OK)
|
|
|
|
|
|
|
|
# Then try to remove the alias
|
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
f"/_matrix/client/r0/directory/room/{alias}",
|
|
|
|
access_token=self.user_tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
|
|
|
|
|
2022-06-17 13:19:22 +02:00
|
|
|
def test_deleting_alias_via_directory_appservice(self) -> None:
|
|
|
|
user_id = "@as:test"
|
|
|
|
as_token = "i_am_an_app_service"
|
|
|
|
|
|
|
|
appservice = ApplicationService(
|
|
|
|
as_token,
|
|
|
|
id="1234",
|
|
|
|
namespaces={"aliases": [{"regex": "#asns-*", "exclusive": True}]},
|
|
|
|
sender=user_id,
|
|
|
|
)
|
|
|
|
self.hs.get_datastores().main.services_cache.append(appservice)
|
|
|
|
|
|
|
|
# Add an alias for the room, as the appservice
|
|
|
|
alias = RoomAlias(f"asns-{random_string(5)}", self.hs.hostname).to_string()
|
2022-07-17 23:28:45 +02:00
|
|
|
request_data = {"room_id": self.room_id}
|
2022-06-17 13:19:22 +02:00
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
f"/_matrix/client/r0/directory/room/{alias}",
|
|
|
|
request_data,
|
|
|
|
access_token=as_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
|
|
|
|
|
|
|
|
# Then try to remove the alias, as the appservice
|
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
f"/_matrix/client/r0/directory/room/{alias}",
|
|
|
|
access_token=as_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def test_deleting_nonexistant_alias(self) -> None:
|
|
|
|
# Check that no alias exists
|
|
|
|
alias = "#potato:test"
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/r0/directory/room/{alias}",
|
|
|
|
access_token=self.user_tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.NOT_FOUND, channel.result)
|
|
|
|
self.assertIn("error", channel.json_body, channel.json_body)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_NOT_FOUND", channel.json_body)
|
|
|
|
|
|
|
|
# Then try to remove the alias
|
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
f"/_matrix/client/r0/directory/room/{alias}",
|
|
|
|
access_token=self.user_tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.NOT_FOUND, channel.result)
|
|
|
|
self.assertIn("error", channel.json_body, channel.json_body)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_NOT_FOUND", channel.json_body)
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def set_alias_via_state_event(
|
|
|
|
self, expected_code: HTTPStatus, alias_length: int = 5
|
|
|
|
) -> None:
|
2020-03-17 12:37:04 +01:00
|
|
|
url = "/_matrix/client/r0/rooms/%s/state/m.room.aliases/%s" % (
|
|
|
|
self.room_id,
|
|
|
|
self.hs.hostname,
|
|
|
|
)
|
|
|
|
|
2022-07-17 23:28:45 +02:00
|
|
|
request_data = {"aliases": [self.random_alias(alias_length)]}
|
2020-03-17 12:37:04 +01:00
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-03-17 12:37:04 +01:00
|
|
|
"PUT", url, request_data, access_token=self.user_tok
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, expected_code, channel.result)
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def set_alias_via_directory(
|
|
|
|
self, expected_code: HTTPStatus, alias_length: int = 5
|
|
|
|
) -> str:
|
|
|
|
alias = self.random_alias(alias_length)
|
|
|
|
url = "/_matrix/client/r0/directory/room/%s" % alias
|
2022-07-17 23:28:45 +02:00
|
|
|
request_data = {"room_id": self.room_id}
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2019-05-10 07:12:11 +02:00
|
|
|
"PUT", url, request_data, access_token=self.user_tok
|
2019-05-08 18:01:30 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, expected_code, channel.result)
|
2021-11-12 20:56:00 +01:00
|
|
|
return alias
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2022-06-22 16:32:18 +02:00
|
|
|
def test_invalid_alias(self) -> None:
|
|
|
|
alias = "#potato"
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/r0/directory/room/{alias}",
|
|
|
|
access_token=self.user_tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
|
|
|
|
self.assertIn("error", channel.json_body, channel.json_body)
|
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["errcode"], "M_INVALID_PARAM", channel.json_body
|
|
|
|
)
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def random_alias(self, length: int) -> str:
|
2019-05-10 07:12:11 +02:00
|
|
|
return RoomAlias(random_string(length), self.hs.hostname).to_string()
|
2019-05-08 18:01:30 +02:00
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def ensure_user_left_room(self) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
self.ensure_membership("leave")
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def ensure_user_joined_room(self) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
self.ensure_membership("join")
|
|
|
|
|
2021-11-12 20:56:00 +01:00
|
|
|
def ensure_membership(self, membership: str) -> None:
|
2019-05-08 18:01:30 +02:00
|
|
|
try:
|
|
|
|
if membership == "leave":
|
2019-05-10 07:12:11 +02:00
|
|
|
self.helper.leave(room=self.room_id, user=self.user, tok=self.user_tok)
|
2019-05-08 18:01:30 +02:00
|
|
|
if membership == "join":
|
2019-05-10 07:12:11 +02:00
|
|
|
self.helper.join(room=self.room_id, user=self.user, tok=self.user_tok)
|
2019-05-08 18:01:30 +02:00
|
|
|
except AssertionError:
|
|
|
|
# We don't care whether the leave request didn't return a 200 (e.g.
|
|
|
|
# if the user isn't already in the room), because we only want to
|
|
|
|
# make sure the user isn't in the room.
|
|
|
|
pass
|