2019-05-08 12:01:30 -04:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
2021-11-12 14:56:00 -05:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2022-06-17 07:19:22 -04:00
|
|
|
from synapse.appservice import ApplicationService
|
2019-05-08 16:57:03 -04:00
|
|
|
from synapse.rest import admin
|
2021-08-17 07:57:58 -04:00
|
|
|
from synapse.rest.client import directory, login, room
|
2021-11-12 14:56:00 -05:00
|
|
|
from synapse.server import HomeServer
|
2019-05-08 12:01:30 -04:00
|
|
|
from synapse.types import RoomAlias
|
2021-11-12 14:56:00 -05:00
|
|
|
from synapse.util import Clock
|
2019-05-08 12:01:30 -04:00
|
|
|
from synapse.util.stringutils import random_string
|
|
|
|
|
|
|
|
from tests import unittest
|
2020-10-05 16:40:51 -04:00
|
|
|
from tests.unittest import override_config
|
2019-05-08 12:01:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
class DirectoryTestCase(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
2019-05-08 16:57:03 -04:00
|
|
|
admin.register_servlets_for_client_rest_resource,
|
2019-05-08 12:01:30 -04:00
|
|
|
directory.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2019-05-08 12:01:30 -04:00
|
|
|
config = self.default_config()
|
2019-05-13 16:01:14 -04:00
|
|
|
config["require_membership_for_aliases"] = True
|
2019-05-08 12:01:30 -04:00
|
|
|
|
|
|
|
self.hs = self.setup_test_homeserver(config=config)
|
|
|
|
|
|
|
|
return self.hs
|
|
|
|
|
2021-11-12 14:56:00 -05: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 12:01:30 -04: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 01:12:11 -04:00
|
|
|
self.room_owner, tok=self.room_owner_tok
|
2019-05-08 12:01:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
self.user = self.register_user("user", "test")
|
|
|
|
self.user_tok = self.login("user", "test")
|
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_state_event_not_in_room(self) -> None:
|
2020-03-17 07:37:04 -04:00
|
|
|
self.ensure_user_left_room()
|
2021-11-12 14:56:00 -05:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.FORBIDDEN)
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_directory_endpoint_not_in_room(self) -> None:
|
2019-05-08 12:01:30 -04:00
|
|
|
self.ensure_user_left_room()
|
2021-11-12 14:56:00 -05:00
|
|
|
self.set_alias_via_directory(HTTPStatus.FORBIDDEN)
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_state_event_in_room_too_long(self) -> None:
|
2020-03-17 07:37:04 -04:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 14:56:00 -05:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.BAD_REQUEST, alias_length=256)
|
2020-03-17 07:37:04 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_directory_in_room_too_long(self) -> None:
|
2019-05-08 12:01:30 -04:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 14:56:00 -05:00
|
|
|
self.set_alias_via_directory(HTTPStatus.BAD_REQUEST, alias_length=256)
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2020-10-05 16:40:51 -04:00
|
|
|
@override_config({"default_room_version": 5})
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_state_event_user_in_v5_room(self) -> None:
|
2020-10-05 16:40:51 -04:00
|
|
|
"""Test that a regular user can add alias events before room v6"""
|
2020-03-17 07:37:04 -04:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 14:56:00 -05:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.OK)
|
2020-03-17 07:37:04 -04:00
|
|
|
|
2020-10-05 16:40:51 -04:00
|
|
|
@override_config({"default_room_version": 6})
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_state_event_v6_room(self) -> None:
|
2020-10-05 16:40:51 -04:00
|
|
|
"""Test that a regular user can *not* add alias events from room v6"""
|
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 14:56:00 -05:00
|
|
|
self.set_alias_via_state_event(HTTPStatus.FORBIDDEN)
|
2020-10-05 16:40:51 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_directory_in_room(self) -> None:
|
2019-05-08 12:01:30 -04:00
|
|
|
self.ensure_user_joined_room()
|
2021-11-12 14:56:00 -05:00
|
|
|
self.set_alias_via_directory(HTTPStatus.OK)
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_room_creation_too_long(self) -> None:
|
2019-05-08 12:01:30 -04: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 17:28:45 -04:00
|
|
|
request_data = {"room_alias_name": random_string(256 - len(self.hs.hostname))}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-05-10 01:12:11 -04:00
|
|
|
"POST", url, request_data, access_token=self.user_tok
|
2019-05-08 12:01:30 -04:00
|
|
|
)
|
2021-11-12 14:56:00 -05:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def test_room_creation(self) -> None:
|
2019-05-08 12:01:30 -04: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 17:28:45 -04:00
|
|
|
request_data = {"room_alias_name": random_string(5)}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-05-10 01:12:11 -04:00
|
|
|
"POST", url, request_data, access_token=self.user_tok
|
2019-05-08 12:01:30 -04:00
|
|
|
)
|
2021-11-12 14:56:00 -05: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 07:19:22 -04: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 17:28:45 -04:00
|
|
|
request_data = {"room_id": self.room_id}
|
2022-06-17 07:19:22 -04: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 14:56:00 -05: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 12:01:30 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def set_alias_via_state_event(
|
|
|
|
self, expected_code: HTTPStatus, alias_length: int = 5
|
|
|
|
) -> None:
|
2020-03-17 07:37:04 -04:00
|
|
|
url = "/_matrix/client/r0/rooms/%s/state/m.room.aliases/%s" % (
|
|
|
|
self.room_id,
|
|
|
|
self.hs.hostname,
|
|
|
|
)
|
|
|
|
|
2022-07-17 17:28:45 -04:00
|
|
|
request_data = {"aliases": [self.random_alias(alias_length)]}
|
2020-03-17 07:37:04 -04:00
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-17 07:37:04 -04:00
|
|
|
"PUT", url, request_data, access_token=self.user_tok
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, expected_code, channel.result)
|
|
|
|
|
2021-11-12 14:56:00 -05: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 17:28:45 -04:00
|
|
|
request_data = {"room_id": self.room_id}
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-05-10 01:12:11 -04:00
|
|
|
"PUT", url, request_data, access_token=self.user_tok
|
2019-05-08 12:01:30 -04:00
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, expected_code, channel.result)
|
2021-11-12 14:56:00 -05:00
|
|
|
return alias
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2022-06-22 10:32:18 -04: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 14:56:00 -05:00
|
|
|
def random_alias(self, length: int) -> str:
|
2019-05-10 01:12:11 -04:00
|
|
|
return RoomAlias(random_string(length), self.hs.hostname).to_string()
|
2019-05-08 12:01:30 -04:00
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def ensure_user_left_room(self) -> None:
|
2019-05-08 12:01:30 -04:00
|
|
|
self.ensure_membership("leave")
|
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def ensure_user_joined_room(self) -> None:
|
2019-05-08 12:01:30 -04:00
|
|
|
self.ensure_membership("join")
|
|
|
|
|
2021-11-12 14:56:00 -05:00
|
|
|
def ensure_membership(self, membership: str) -> None:
|
2019-05-08 12:01:30 -04:00
|
|
|
try:
|
|
|
|
if membership == "leave":
|
2019-05-10 01:12:11 -04:00
|
|
|
self.helper.leave(room=self.room_id, user=self.user, tok=self.user_tok)
|
2019-05-08 12:01:30 -04:00
|
|
|
if membership == "join":
|
2019-05-10 01:12:11 -04:00
|
|
|
self.helper.join(room=self.room_id, user=self.user, tok=self.user_tok)
|
2019-05-08 12:01:30 -04: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
|