1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
# stdlib
import json
import sys
# 3rd party
import pytest
from coincidence import AdvancedFileRegressionFixture
from coincidence.params import param
from domdf_python_tools.compat import PYPY36
# this package
from apeye_core.email_validator import EmailSyntaxError, ValidatedEmail
from apeye_core.email_validator import main as validator_main
from apeye_core.email_validator import validate_email
@pytest.mark.parametrize(
"email_input,output",
[
(
"Abc@example.com",
ValidatedEmail(
local_part="Abc",
ascii_local_part="Abc",
smtputf8=False,
ascii_domain="example.com",
domain="example.com",
email="Abc@example.com",
original_email="Abc@example.com",
ascii_email="Abc@example.com",
),
),
(
b"Abc@example.com",
ValidatedEmail(
local_part="Abc",
ascii_local_part="Abc",
smtputf8=False,
ascii_domain="example.com",
domain="example.com",
email="Abc@example.com",
original_email="Abc@example.com",
ascii_email="Abc@example.com",
),
),
(
"Abc.123@example.com",
ValidatedEmail(
local_part="Abc.123",
ascii_local_part="Abc.123",
smtputf8=False,
ascii_domain="example.com",
domain="example.com",
email="Abc.123@example.com",
original_email="Abc.123@example.com",
ascii_email="Abc.123@example.com",
),
),
(
b"Abc.123@example.com",
ValidatedEmail(
local_part="Abc.123",
ascii_local_part="Abc.123",
smtputf8=False,
ascii_domain="example.com",
domain="example.com",
email="Abc.123@example.com",
original_email="Abc.123@example.com",
ascii_email="Abc.123@example.com",
),
),
(
"user+mailbox/department=shipping@example.com",
ValidatedEmail(
local_part="user+mailbox/department=shipping",
ascii_local_part="user+mailbox/department=shipping",
smtputf8=False,
ascii_domain="example.com",
domain="example.com",
email="user+mailbox/department=shipping@example.com",
original_email="user+mailbox/department=shipping@example.com",
ascii_email="user+mailbox/department=shipping@example.com",
),
),
(
b"user+mailbox/department=shipping@example.com",
ValidatedEmail(
local_part="user+mailbox/department=shipping",
ascii_local_part="user+mailbox/department=shipping",
smtputf8=False,
ascii_domain="example.com",
domain="example.com",
email="user+mailbox/department=shipping@example.com",
original_email="user+mailbox/department=shipping@example.com",
ascii_email="user+mailbox/department=shipping@example.com",
),
),
(
"!#$%&'*+-/=?^_`.{|}~@example.com",
ValidatedEmail(
local_part="!#$%&'*+-/=?^_`.{|}~",
ascii_local_part="!#$%&'*+-/=?^_`.{|}~",
smtputf8=False,
ascii_domain="example.com",
domain="example.com",
email="!#$%&'*+-/=?^_`.{|}~@example.com",
original_email="!#$%&'*+-/=?^_`.{|}~@example.com",
ascii_email="!#$%&'*+-/=?^_`.{|}~@example.com",
),
),
(
"伊昭傑@郵件.商務",
ValidatedEmail(
local_part="伊昭傑",
smtputf8=True,
ascii_domain="xn--5nqv22n.xn--lhr59c",
domain="郵件.商務",
email="伊昭傑@郵件.商務",
original_email="伊昭傑@郵件.商務",
),
),
(
"राम@मोहन.ईन्फो",
ValidatedEmail(
local_part="राम",
smtputf8=True,
ascii_domain="xn--l2bl7a9d.xn--o1b8dj2ki",
domain="मोहन.ईन्फो",
email="राम@मोहन.ईन्फो",
original_email="राम@मोहन.ईन्फो",
),
),
(
"юзер@екзампл.ком",
ValidatedEmail(
local_part="юзер",
smtputf8=True,
ascii_domain="xn--80ajglhfv.xn--j1aef",
domain="екзампл.ком",
email="юзер@екзампл.ком",
original_email="юзер@екзампл.ком",
),
),
(
"θσερ@εχαμπλε.ψομ",
ValidatedEmail(
local_part="θσερ",
smtputf8=True,
ascii_domain="xn--mxahbxey0c.xn--xxaf0a",
domain="εχαμπλε.ψομ",
email="θσερ@εχαμπλε.ψομ",
original_email="θσερ@εχαμπλε.ψομ",
),
),
(
"葉士豪@臺網中心.tw",
ValidatedEmail(
local_part="葉士豪",
smtputf8=True,
ascii_domain="xn--fiqq24b10vi0d.tw",
domain="臺網中心.tw",
email="葉士豪@臺網中心.tw",
original_email="葉士豪@臺網中心.tw",
),
),
(
"jeff@臺網中心.tw",
ValidatedEmail(
local_part="jeff",
ascii_local_part="jeff",
smtputf8=False,
ascii_domain="xn--fiqq24b10vi0d.tw",
domain="臺網中心.tw",
email="jeff@臺網中心.tw",
original_email="jeff@臺網中心.tw",
ascii_email="jeff@xn--fiqq24b10vi0d.tw",
),
),
(
"葉士豪@臺網中心.台灣",
ValidatedEmail(
local_part="葉士豪",
smtputf8=True,
ascii_domain="xn--fiqq24b10vi0d.xn--kpry57d",
domain="臺網中心.台灣",
email="葉士豪@臺網中心.台灣",
original_email="葉士豪@臺網中心.台灣",
),
),
(
"jeff葉@臺網中心.tw",
ValidatedEmail(
local_part="jeff葉",
smtputf8=True,
ascii_domain="xn--fiqq24b10vi0d.tw",
domain="臺網中心.tw",
email="jeff葉@臺網中心.tw",
original_email="jeff葉@臺網中心.tw",
),
),
(
"ñoñó@example.com",
ValidatedEmail(
local_part="ñoñó",
smtputf8=True,
ascii_domain="example.com",
domain="example.com",
email="ñoñó@example.com",
original_email="ñoñó@example.com",
),
),
(
"我買@example.com",
ValidatedEmail(
local_part="我買",
smtputf8=True,
ascii_domain="example.com",
domain="example.com",
email="我買@example.com",
original_email="我買@example.com",
),
),
(
"甲斐黒川日本@example.com",
ValidatedEmail(
local_part="甲斐黒川日本",
smtputf8=True,
ascii_domain="example.com",
domain="example.com",
email="甲斐黒川日本@example.com",
original_email="甲斐黒川日本@example.com",
),
),
param(
"чебурашкаящик-с-апельсинами.рф@example.com",
ValidatedEmail(
local_part="чебурашкаящик-с-апельсинами.рф",
smtputf8=True,
ascii_domain="example.com",
domain="example.com",
email="чебурашкаящик-с-апельсинами.рф@example.com",
original_email="чебурашкаящик-с-апельсинами.рф@example.com",
),
marks=pytest.mark.skipif(
sys.platform == "win32" and PYPY36,
reason="Fails due to unicode issue with filename",
),
id="unicode_1",
),
(
"उदाहरण.परीक्ष@domain.with.idn.tld",
ValidatedEmail(
local_part="उदाहरण.परीक्ष",
smtputf8=True,
ascii_domain="domain.with.idn.tld",
domain="domain.with.idn.tld",
email="उदाहरण.परीक्ष@domain.with.idn.tld",
original_email="उदाहरण.परीक्ष@domain.with.idn.tld",
),
),
(
"ιωάννης@εεττ.gr",
ValidatedEmail(
local_part="ιωάννης",
smtputf8=True,
ascii_domain="xn--qxaa9ba.gr",
domain="εεττ.gr",
email="ιωάννης@εεττ.gr",
original_email="ιωάννης@εεττ.gr",
),
),
],
)
def test_email_valid(
email_input: str,
output: ValidatedEmail,
advanced_file_regression: AdvancedFileRegressionFixture,
):
# print(f'({email_input!r}, {validate_email(email_input)!r}),')
assert validate_email(email_input) == output
data = output.as_dict()
data["__repr__"] = repr(output)
data["__str__"] = str(output)
advanced_file_regression.check(json.dumps(data), extension=".json")
@pytest.mark.parametrize(
"email_input,error_msg",
[
("my@.leadingdot.com", "An email address cannot have a period immediately after the @-sign."),
("my@..leadingfwdot.com", "An email address cannot have a period immediately after the @-sign."),
("my@..twodots.com", "An email address cannot have a period immediately after the @-sign."),
("my@twodots..com", "An email address cannot have two periods in a row."),
(
"my@baddash.-.com",
{
"The domain name baddash.-.com contains invalid characters (Label must not start or end with a hyphen).",
"The domain name baddash.-.com contains invalid characters (The label b'-' is not a valid A-label).",
}
),
(
"my@baddash.-a.com",
{
"The domain name baddash.-a.com contains invalid characters (Label must not start or end with a hyphen).",
"The domain name baddash.-a.com contains invalid characters (The label b'-a' is not a valid A-label).",
}
),
(
"my@baddash.b-.com",
{
"The domain name baddash.b-.com contains invalid characters (Label must not start or end with a hyphen).",
"The domain name baddash.b-.com contains invalid characters (The label b'b-' is not a valid A-label)."
}
),
(
'my@example.com\n',
{
'The domain name example.com\n contains invalid characters (Codepoint U+000A at position 4 of \'com\\n\' not allowed).',
"The domain name example.com\n contains invalid characters (Codepoint U+000A not allowed at position 12 in 'example.com\\n').",
}
),
(
'my@example\n.com',
{
"The domain name example\n.com contains invalid characters (Codepoint U+000A at position 8 of 'example\\n' not allowed).",
"The domain name example\n.com contains invalid characters (Codepoint U+000A not allowed at position 8 in 'example\\n.com').",
}
),
(".leadingdot@domain.com", "The email address contains invalid characters before the @-sign: .."),
("..twodots@domain.com", "The email address contains invalid characters before the @-sign: .."),
(
"twodots..here@domain.com",
"The email address contains invalid characters before the @-sign: .."
),
(
"me@⒈wouldbeinvalid.com",
"The domain name ⒈wouldbeinvalid.com contains invalid characters (Codepoint U+2488 not allowed "
"at position 1 in '⒈wouldbeinvalid.com')."
),
("@example.com", "There must be something before the @-sign."),
('\nmy@example.com', 'The email address contains invalid characters before the @-sign: \n.'),
('m\ny@example.com', 'The email address contains invalid characters before the @-sign: \n.'),
('my\n@example.com', 'The email address contains invalid characters before the @-sign: \n.'),
(
"11111111112222222222333333333344444444445555555555666666666677777@example.com",
"The email address is too long before the @-sign (1 character too many)."
),
(
"111111111122222222223333333333444444444455555555556666666666777777@example.com",
"The email address is too long before the @-sign (2 characters too many)."
),
(
"me@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444444444455555555556.com",
"The email address is too long after the @-sign."
),
(
"my.long.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.11111111112222222222333333333344444.info",
"The email address is too long (2 characters too many)."
),
(
"my.long.address@λ111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.11111111112222222222333333.info",
"The email address is too long (when converted to IDNA ASCII)."
),
(
"my.long.address@λ111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444.info",
"The email address is too long (at least 1 character too many)."
),
(
"my.λong.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444.info",
"The email address is too long (when encoded in bytes)."
),
(
"my.λong.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444.info",
"The email address is too long (at least 1 character too many)."
),
("local_part_only@", "There must be something after the @-sign."),
("dom@example.com.", "An email address cannot end with a period."),
("☃@example.com.".encode(), "The email address is not valid ASCII."),
("dom@ex@mple.com", "The email address is not valid. It must have exactly one @-sign."),
("example.com", "The email address is not valid. It must have exactly one @-sign."),
],
)
def test_email_invalid(email_input: str, error_msg: str):
with pytest.raises(EmailSyntaxError) as exc_info:
validate_email(email_input)
# print(f'({email_input!r}, {str(exc_info.value)!r}),')
if isinstance(error_msg, set):
assert str(exc_info.value) in error_msg
else:
assert str(exc_info.value) == error_msg
def test_dict_accessor():
input_email = "testaddr@example.com"
valid_email = validate_email(input_email)
assert isinstance(valid_email.as_dict(), dict)
assert valid_email.as_dict()["original_email"] == input_email
def test_main_single_good_input(monkeypatch, capsys):
# stdlib
import json
test_email = "test@example.com"
monkeypatch.setattr("sys.argv", ["email_validator", test_email])
validator_main()
stdout, _ = capsys.readouterr()
output = json.loads(str(stdout))
assert isinstance(output, dict)
assert validate_email(test_email).original_email == output["original_email"]
def test_main_single_bad_input(monkeypatch, capsys):
bad_email = "test@..com"
monkeypatch.setattr("sys.argv", ["email_validator", bad_email])
validator_main()
stdout, _ = capsys.readouterr()
assert stdout == 'An email address cannot have a period immediately after the @-sign.\n'
def test_main_multi_input(monkeypatch, capsys):
# stdlib
import io
test_cases = ["test@example.com", "test2@example.com", "test@.com", "test3@.com"]
test_input = io.StringIO('\n'.join(test_cases))
monkeypatch.setattr("sys.stdin", test_input)
monkeypatch.setattr("sys.argv", ["email_validator"])
validator_main()
stdout, _ = capsys.readouterr()
assert test_cases[0] not in stdout
assert test_cases[1] not in stdout
assert test_cases[2] in stdout
assert test_cases[3] in stdout
|