summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrendan Abolivier <babolivier@matrix.org>2019-09-20 10:57:07 +0100
committerGitHub <noreply@github.com>2019-09-20 10:57:07 +0100
commit07eb311b11680dfcec8bd5ef26396dd7f051a5b8 (patch)
tree5c6325638b515bdbe4de6ebb1100efac9a335c11
parentMerge pull request #2 from matrix-org/babolivier/dinsic-3pid-invite (diff)
parentRemove unnecessary cast to list (diff)
downloadsynapse-07eb311b11680dfcec8bd5ef26396dd7f051a5b8.tar.xz
Merge pull request #4 from matrix-org/babolivier/strip_invalid_mxid_characters
Fix handling of filtered strings in Python 3 when processing MXIDs
-rw-r--r--changelog.d/4.bugfix1
-rw-r--r--synapse/types.py5
-rw-r--r--tests/test_types.py22
3 files changed, 26 insertions, 2 deletions
diff --git a/changelog.d/4.bugfix b/changelog.d/4.bugfix
new file mode 100644

index 0000000000..fe717920a6 --- /dev/null +++ b/changelog.d/4.bugfix
@@ -0,0 +1 @@ +Fix handling of filtered strings in Python 3. diff --git a/synapse/types.py b/synapse/types.py
index eebe29d1f0..e6afc05cee 100644 --- a/synapse/types.py +++ b/synapse/types.py
@@ -16,6 +16,8 @@ import re import string from collections import namedtuple +from six.moves import filter + import attr from synapse.api.errors import SynapseError @@ -240,7 +242,8 @@ def strip_invalid_mxid_characters(localpart): Returns: localpart (basestring): the localpart having been stripped """ - return filter(lambda c: c in mxid_localpart_allowed_characters, localpart) + filtered = filter(lambda c: c in mxid_localpart_allowed_characters, localpart) + return "".join(filtered) UPPER_CASE_PATTERN = re.compile(b"[A-Z_]") diff --git a/tests/test_types.py b/tests/test_types.py
index d83c36559f..73d3b2cda2 100644 --- a/tests/test_types.py +++ b/tests/test_types.py
@@ -12,9 +12,16 @@ # 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. +from six import string_types from synapse.api.errors import SynapseError -from synapse.types import GroupID, RoomAlias, UserID, map_username_to_mxid_localpart +from synapse.types import ( + GroupID, + RoomAlias, + UserID, + map_username_to_mxid_localpart, + strip_invalid_mxid_characters, +) from tests import unittest from tests.utils import TestHomeServer @@ -106,3 +113,16 @@ class MapUsernameTestCase(unittest.TestCase): self.assertEqual( map_username_to_mxid_localpart(u'têst'.encode('utf-8')), "t=c3=aast" ) + + +class StripInvalidMxidCharactersTestCase(unittest.TestCase): + def test_return_type(self): + unstripped = strip_invalid_mxid_characters("test") + stripped = strip_invalid_mxid_characters("test@") + + self.assertTrue(isinstance(unstripped, string_types), type(unstripped)) + self.assertTrue(isinstance(stripped, string_types), type(stripped)) + + def test_strip(self): + stripped = strip_invalid_mxid_characters("test@") + self.assertEqual(stripped, "test", stripped)