Skip to content

Commit 8fdbaba

Browse files
committed
Add some tests for invalid addresses with quotes in preparation for parsing them
1 parent ca7640b commit 8fdbaba

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

email_validator/syntax.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def get_length_reason(addr, utf8=False, limit=EMAIL_MAX_LENGTH):
1717

1818
def safe_character_display(c):
1919
# Return safely displayable characters in quotes.
20+
if c == '\\':
21+
return f"\"{c}\"" # can't use repr because it escapes it
2022
if unicodedata.category(c)[0] in ("L", "N", "P", "S"):
2123
return repr(c)
2224

tests/test_syntax.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ def test_email_valid_intl_local_part(email_input, output):
223223
assert "Internationalized characters before the @-sign are not supported: " in str(exc_info.value)
224224

225225

226+
@pytest.mark.parametrize(
227+
'email_input,error_msg',
228+
[
229+
('"unnecessarily.quoted.local.part"@example.com', 'The email address contains invalid characters before the @-sign: \'"\'.'),
230+
('"quoted..local.part"@example.com', 'The email address contains invalid characters before the @-sign: \'"\'.'),
231+
('"quoted.with.at@"@example.com', 'The email address is not valid. It must have exactly one @-sign.'),
232+
('"quoted with space"@example.com', 'The email address contains invalid characters before the @-sign: \'\"\', SPACE.'),
233+
('"quoted.with.dquote\\""@example.com', 'The email address contains invalid characters before the @-sign: "\\", \'"\'.'),
234+
('"unnecessarily.quoted.with.unicode.λ"@example.com', 'The email address contains invalid characters before the @-sign: \'"\'.'),
235+
('"quoted.with..unicode.λ"@example.com', 'The email address contains invalid characters before the @-sign: \'"\'.'),
236+
('"quoted.with.extraneous.\\escape"@example.com', 'The email address contains invalid characters before the @-sign: "\\", \'"\'.'),
237+
])
238+
def test_email_valid_only_if_quoted_local_part(email_input, error_msg):
239+
with pytest.raises(EmailSyntaxError) as exc_info:
240+
validate_email(email_input)
241+
assert str(exc_info.value) == error_msg
242+
243+
226244
@pytest.mark.parametrize(
227245
'email_input,error_msg',
228246
[
@@ -261,11 +279,8 @@ def test_email_valid_intl_local_part(email_input, output):
261279
('\nmy@example.com', 'The email address contains invalid characters before the @-sign: U+000A.'),
262280
('m\ny@example.com', 'The email address contains invalid characters before the @-sign: U+000A.'),
263281
('my\n@example.com', 'The email address contains invalid characters before the @-sign: U+000A.'),
264-
<<<<<<< HEAD
265-
=======
266282
('test@\n', 'The part after the @-sign contains invalid characters: U+000A.'),
267283
('bad"quotes"@example.com', 'The email address contains invalid characters before the @-sign: \'"\'.'),
268-
>>>>>>> 6146614 (fixup)
269284
('11111111112222222222333333333344444444445555555555666666666677777@example.com', 'The email address is too long before the @-sign (1 character too many).'),
270285
('111111111122222222223333333333444444444455555555556666666666777777@example.com', 'The email address is too long before the @-sign (2 characters too many).'),
271286
('me@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444444444455555555556.com', 'The email address is too long (4 characters too many).'),
@@ -343,7 +358,7 @@ def test_email_unsafe_character(s, expected_error):
343358
('λambdaツ@test', 'Internationalized characters before the @-sign are not supported: \'λ\', \'\'.'),
344359
],
345360
)
346-
def test_email_invalid_character_smtputf8(email_input, expected_error):
361+
def test_email_invalid_character_smtputf8_off(email_input, expected_error):
347362
# Check that internationalized characters are rejected if allow_smtputf8=False.
348363
with pytest.raises(EmailSyntaxError) as exc_info:
349364
validate_email(email_input, allow_smtputf8=False, test_environment=True)

0 commit comments

Comments
 (0)