From 2a3d3550aa0e9da15d9c05b355be03bb858eecac Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Sat, 23 Nov 2019 22:04:14 +0100 Subject: [PATCH] CharField: Cover handling of surrogate characters --- tests/test_fields.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_fields.py b/tests/test_fields.py index 1d302b730..30ae0b719 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -759,6 +759,21 @@ class TestCharField(FieldValues): 'Null characters are not allowed.' ] + def test_surrogate_characters(self): + field = serializers.CharField() + + for code_point, expected_message in ( + (0xD800, 'Surrogate characters are not allowed: U+D800.'), + (0xDFFF, 'Surrogate characters are not allowed: U+DFFF.'), + ): + with pytest.raises(serializers.ValidationError) as exc_info: + field.run_validation(chr(code_point)) + assert exc_info.value.detail[0].code == 'surrogate_characters_not_allowed' + assert str(exc_info.value.detail[0]) == expected_message + + for code_point in (0xD800 - 1, 0xDFFF + 1): + field.run_validation(chr(code_point)) + def test_iterable_validators(self): """ Ensure `validators` parameter is compatible with reasonable iterables.