File size: 2,070 Bytes
de745bf | 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 | """Internationalization primitives (spec §7.1 Punycode / §7.2 SMTPUTF8)."""
import unittest
from app.validator.i18n import (
EAI_REASON,
domain_is_unicode,
eai_gate,
local_part_is_ascii,
to_punycode,
)
class PunycodeTests(unittest.TestCase):
def test_ascii_domain_untouched(self):
self.assertEqual(to_punycode("example.com"), "example.com")
def test_unicode_domain_encoded(self):
out = to_punycode("bücher.example")
self.assertTrue(out.startswith("xn--"))
self.assertEqual(out.encode("ascii").decode(), out)
def test_already_punycode_roundtrip(self):
self.assertEqual(to_punycode("xn--bcher-kva.example"), "xn--bcher-kva.example")
def test_unicode_tld(self):
out = to_punycode("пример.рф")
self.assertTrue(all(ord(c) < 128 for c in out))
self.assertIn("xn--", out)
def test_garbage_domain_returns_as_is(self):
# Must never raise — DNS will fail naturally and the verdict degrades.
self.assertEqual(to_punycode("\x00bad"), "\x00bad")
def test_empty_and_dot_stripping_safe(self):
self.assertEqual(to_punycode(""), "")
self.assertEqual(to_punycode("example.com"), "example.com")
class UnicodeDetectionTests(unittest.TestCase):
def test_domain_is_unicode(self):
self.assertTrue(domain_is_unicode("münchen.de"))
self.assertFalse(domain_is_unicode("xn--mnchen-3ya.de"))
self.assertFalse(domain_is_unicode("example.com"))
class EAITests(unittest.TestCase):
def test_ascii_local_passes_gate(self):
self.assertIsNone(eai_gate("john.smith"))
self.assertIsNone(eai_gate("o+brien+tag"))
def test_unicode_local_blocked_deterministically(self):
self.assertEqual(eai_gate("用户"), EAI_REASON)
self.assertEqual(eai_gate("café"), EAI_REASON)
def test_ascii_flag_consistency(self):
self.assertTrue(local_part_is_ascii("simple"))
self.assertFalse(local_part_is_ascii("日本語"))
if __name__ == "__main__":
unittest.main()
|