Trim whitespace when setting display names (#16031)

This commit is contained in:
Mohit Rathee 2023-08-01 18:44:02 +05:30 committed by GitHub
parent 7cbb2a00d1
commit 5eb3fd785b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

1
changelog.d/16031.bugfix Normal file
View File

@ -0,0 +1 @@
Remove leading and trailing spaces when setting a display name.

View File

@ -163,7 +163,7 @@ class ProfileHandler:
400, "Displayname is too long (max %i)" % (MAX_DISPLAYNAME_LEN,)
)
displayname_to_set: Optional[str] = new_displayname
displayname_to_set: Optional[str] = new_displayname.strip()
if new_displayname == "":
displayname_to_set = None

View File

@ -68,6 +68,18 @@ class ProfileTestCase(unittest.HomeserverTestCase):
res = self._get_displayname()
self.assertEqual(res, "test")
def test_set_displayname_with_extra_spaces(self) -> None:
channel = self.make_request(
"PUT",
"/profile/%s/displayname" % (self.owner,),
content={"displayname": " test "},
access_token=self.owner_tok,
)
self.assertEqual(channel.code, 200, channel.result)
res = self._get_displayname()
self.assertEqual(res, "test")
def test_set_displayname_noauth(self) -> None:
channel = self.make_request(
"PUT",