Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow Synapse Admin API's Room Search to accept non-ASCII characters #10859

Merged
merged 13 commits into from
Sep 21, 2021
2 changes: 2 additions & 0 deletions changelog.d/10859.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Made a change that Synapse Admin API's Rooms Search so that it can now accept non-ASCII characters, allowing admins
to find rooms with non-ASCII characters.
clokep marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion synapse/http/servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def parse_string(
default: Optional[str] = None,
required: bool = False,
allowed_values: Optional[Iterable[str]] = None,
encoding: str = "ascii",
encoding: str = "UTF-8",
clokep marked this conversation as resolved.
Show resolved Hide resolved
) -> Optional[str]:
"""
Parse a string parameter from the request query string.
Expand Down
46 changes: 46 additions & 0 deletions tests/rest/admin/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,52 @@ def _search_test(
_search_test(None, "bar")
_search_test(None, "", expected_http_code=400)

def test_search_term_non_ASCII(self):
clokep marked this conversation as resolved.
Show resolved Hide resolved
"""Test that searching for a room with non-ASCII characters works correctly"""

# Create test room
room_id = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)

room_name = "ж"

# Set the name for the room
self.helper.send_state(
room_id,
"m.room.name",
{"name": room_name},
tok=self.admin_user_tok,
)

def _search_test_utf8(
clokep marked this conversation as resolved.
Show resolved Hide resolved
expected_room_id,
search_term: str,
expected_http_code: int = 200,
):
"""Search for a room with a non-ascii character in name
and check that the returned room's id is a match

Args:
expected_room_id: The room_id expected to be returned by the API. Set
to None to expect zero results for the search
search_term: The term to search for room names with
expected_http_code: The expected http code for the request
"""
encoded_search_term = urllib.parse.quote(search_term, "utf-8")
url = "/_synapse/admin/v1/rooms?search_term=%s" % (encoded_search_term,)
channel = self.make_request(
"GET",
url.encode("ascii"),
access_token=self.admin_user_tok,
)
self.assertEqual(expected_http_code, channel.code, msg=channel.json_body)
self.assertIn(
expected_room_id, channel.json_body.get("rooms")[0].get("room_id")
)
self.assertIn("ж", channel.json_body.get("rooms")[0].get("name"))

search_term = "ж"
_search_test_utf8(room_id, search_term)

def test_single_room(self):
"""Test that a single room can be requested correctly"""
# Create two test rooms
Expand Down