From ae036ed63605cbe0bb62010565eece6c7b1a9249 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 19 Sep 2019 11:58:06 +0100 Subject: Add unit tests for strip_invalid_mxid_characters --- tests/test_types.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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) -- cgit 1.5.1 From 30c085fbc33e7ad47c31f28debd23bacaf41b599 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 19 Sep 2019 12:03:10 +0100 Subject: Use six.moves.filter when filtering out from MXID Python 2's filter() function and Python 3's don't return the same type when processing a string (respectively str and filter), therefore use six's compatibility mapping (which resolves to itertools.ifilter() if using Python2), then generate a string from the filtered list, in order to ensure consistent behaviour between Python 2 and Python 3. --- synapse/types.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/synapse/types.py b/synapse/types.py index eebe29d1f0..842275e0fa 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -17,6 +17,7 @@ import string from collections import namedtuple import attr +from six.moves import filter from synapse.api.errors import SynapseError @@ -240,7 +241,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(list(filtered)) UPPER_CASE_PATTERN = re.compile(b"[A-Z_]") -- cgit 1.5.1 From 8bc39401fec09c4338c9cc2f1dfe139cf5461945 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 19 Sep 2019 13:01:05 +0100 Subject: Lint --- synapse/types.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/synapse/types.py b/synapse/types.py index 842275e0fa..6d20ef3641 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -16,9 +16,10 @@ import re import string from collections import namedtuple -import attr from six.moves import filter +import attr + from synapse.api.errors import SynapseError -- cgit 1.5.1 From 6f364634eed95c99cbfa9fed610701bf0c9ebd62 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 19 Sep 2019 13:02:23 +0100 Subject: Changelog --- changelog.d/4.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/4.bugfix 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. -- cgit 1.5.1 From 736394d46b0b7c35ba3f394fc1e74448a07c22f5 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Fri, 20 Sep 2019 10:07:55 +0100 Subject: Remove unnecessary cast to list --- synapse/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/types.py b/synapse/types.py index 6d20ef3641..e6afc05cee 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -243,7 +243,7 @@ def strip_invalid_mxid_characters(localpart): localpart (basestring): the localpart having been stripped """ filtered = filter(lambda c: c in mxid_localpart_allowed_characters, localpart) - return "".join(list(filtered)) + return "".join(filtered) UPPER_CASE_PATTERN = re.compile(b"[A-Z_]") -- cgit 1.5.1