def test_invalid_display_name(self): with self.assertRaisesMessage(AnymailInvalidAddress, "Invalid email address 'webmaster'"): parse_address_list(['webmaster']) with self.assertRaisesMessage(AnymailInvalidAddress, "Maybe missing quotes around a display-name?"): # this parses as multiple email addresses, because of the comma: parse_address_list(['Display Name, Inc. <*****@*****.**>'])
def test_lazy_strings(self): parsed_list = parse_address_list([ugettext_lazy('"Example, Inc." <*****@*****.**>')]) self.assertEqual(len(parsed_list), 1) self.assertEqual(parsed_list[0].name, "Example, Inc.") self.assertEqual(parsed_list[0].email, "*****@*****.**") parsed_list = parse_address_list(ugettext_lazy("*****@*****.**")) self.assertEqual(len(parsed_list), 1) self.assertEqual(parsed_list[0].name, "") self.assertEqual(parsed_list[0].email, "*****@*****.**")
def test_lazy_strings(self): parsed_list = parse_address_list([ugettext_lazy('"Example, Inc." <*****@*****.**>')]) self.assertEqual(len(parsed_list), 1) self.assertEqual(parsed_list[0].display_name, "Example, Inc.") self.assertEqual(parsed_list[0].addr_spec, "*****@*****.**") parsed_list = parse_address_list(ugettext_lazy("*****@*****.**")) self.assertEqual(len(parsed_list), 1) self.assertEqual(parsed_list[0].display_name, "") self.assertEqual(parsed_list[0].addr_spec, "*****@*****.**")
def test_invalid_address(self): with self.assertRaises(AnymailInvalidAddress): parse_address_list(['localonly']) with self.assertRaises(AnymailInvalidAddress): parse_address_list(['localonly@']) with self.assertRaises(AnymailInvalidAddress): parse_address_list(['@domainonly']) with self.assertRaises(AnymailInvalidAddress): parse_address_list(['<localonly@>']) with self.assertRaises(AnymailInvalidAddress): parse_address_list(['<@domainonly>'])
def test_idn(self): parsed_list = parse_address_list([u"idn@\N{ENVELOPE}.example.com"]) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertEqual(parsed.addr_spec, u"idn@\N{ENVELOPE}.example.com") self.assertEqual(parsed.address, "*****@*****.**") # punycode-encoded domain self.assertEqual(parsed.username, "idn") self.assertEqual(parsed.domain, u"\N{ENVELOPE}.example.com")
def test_unicode_display_name(self): parsed_list = parse_address_list([u'"Unicode \N{HEAVY BLACK HEART}" <*****@*****.**>']) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertEqual(parsed.addr_spec, "*****@*****.**") self.assertEqual(parsed.display_name, u"Unicode \N{HEAVY BLACK HEART}") # formatted display-name automatically shifts to quoted-printable/base64 for non-ascii chars: self.assertEqual(parsed.address, '=?utf-8?b?VW5pY29kZSDinaQ=?= <*****@*****.**>')
def test_multiple_emails(self): # Django's EmailMessage allows multiple, comma-separated emails # in a single recipient string. (It passes them along to the backend intact.) # (Depending on this behavior is not recommended.) parsed_list = parse_address_list(["[email protected], [email protected]"]) self.assertEqual(len(parsed_list), 2) self.assertEqual(parsed_list[0].email, "*****@*****.**") self.assertEqual(parsed_list[1].email, "*****@*****.**")
def test_unicode_display_name(self): parsed_list = parse_address_list([u'"Unicode \N{HEAVY BLACK HEART}" <*****@*****.**>']) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertEqual(parsed.email, "*****@*****.**") self.assertEqual(parsed.name, u"Unicode \N{HEAVY BLACK HEART}") # formatted display-name automatically shifts to quoted-printable/base64 for non-ascii chars: self.assertEqual(parsed.address, '=?utf-8?b?VW5pY29kZSDinaQ=?= <*****@*****.**>')
def test_multiple_emails(self): # Django's EmailMessage allows multiple, comma-separated emails # in a single recipient string. (It passes them along to the backend intact.) # (Depending on this behavior is not recommended.) parsed_list = parse_address_list(["[email protected], [email protected]"]) self.assertEqual(len(parsed_list), 2) self.assertEqual(parsed_list[0].addr_spec, "*****@*****.**") self.assertEqual(parsed_list[1].addr_spec, "*****@*****.**")
def test_idn(self): parsed_list = parse_address_list([u"idn@\N{ENVELOPE}.example.com"]) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertEqual(parsed.email, u"idn@\N{ENVELOPE}.example.com") self.assertEqual(parsed.address, "*****@*****.**") # punycode-encoded domain self.assertEqual(parsed.localpart, "idn") self.assertEqual(parsed.domain, u"\N{ENVELOPE}.example.com")
def test_obsolete_display_name(self): # you can get away without the quotes if there are no commas or parens # (but it's not recommended) parsed_list = parse_address_list(['Display Name <*****@*****.**>']) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertEqual(parsed.email, "*****@*****.**") self.assertEqual(parsed.name, "Display Name") self.assertEqual(parsed.address, 'Display Name <*****@*****.**>')
def test_display_name(self): parsed_list = parse_address_list(['"Display Name, Inc." <*****@*****.**>']) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertEqual(parsed.addr_spec, "*****@*****.**") self.assertEqual(parsed.display_name, "Display Name, Inc.") self.assertEqual(parsed.address, '"Display Name, Inc." <*****@*****.**>') self.assertEqual(parsed.username, "test") self.assertEqual(parsed.domain, "example.com")
def test_display_name(self): parsed_list = parse_address_list(['"Display Name, Inc." <*****@*****.**>']) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertEqual(parsed.email, "*****@*****.**") self.assertEqual(parsed.name, "Display Name, Inc.") self.assertEqual(parsed.address, '"Display Name, Inc." <*****@*****.**>') self.assertEqual(parsed.localpart, "test") self.assertEqual(parsed.domain, "example.com")
def test_simple_email(self): parsed_list = parse_address_list(["*****@*****.**"]) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertIsInstance(parsed, EmailAddress) self.assertEqual(parsed.addr_spec, "*****@*****.**") self.assertEqual(parsed.display_name, "") self.assertEqual(parsed.address, "*****@*****.**") self.assertEqual(parsed.username, "test") self.assertEqual(parsed.domain, "example.com")
def test_simple_email(self): parsed_list = parse_address_list(["*****@*****.**"]) self.assertEqual(len(parsed_list), 1) parsed = parsed_list[0] self.assertIsInstance(parsed, ParsedEmail) self.assertEqual(parsed.email, "*****@*****.**") self.assertEqual(parsed.name, "") self.assertEqual(parsed.address, "*****@*****.**") self.assertEqual(parsed.localpart, "test") self.assertEqual(parsed.domain, "example.com")
def test_single_string(self): # bare strings are used by the from_email parsing in BasePayload parsed_list = parse_address_list("*****@*****.**") self.assertEqual(len(parsed_list), 1) self.assertEqual(parsed_list[0].email, "*****@*****.**")
def test_invalid_in_list(self): # Make sure it's not just concatenating list items... # the bare "Display Name" below should *not* get merged with # the email in the second item with self.assertRaisesMessage(AnymailInvalidAddress, "Display Name"): parse_address_list(['"Display Name"', '<*****@*****.**>'])
def test_email_list(self): parsed_list = parse_address_list( ["*****@*****.**", "*****@*****.**"]) self.assertEqual(len(parsed_list), 2) self.assertEqual(parsed_list[0].email, "*****@*****.**") self.assertEqual(parsed_list[1].email, "*****@*****.**")
def test_whitespace_only_address(self): with self.assertRaises(AnymailInvalidAddress): parse_address_list([' '])
def test_none_address(self): # used for, e.g., telling Mandrill to use template default from_email self.assertEqual(parse_address_list([None]), []) self.assertEqual(parse_address_list(None), [])
def test_invalid_with_unicode(self): with self.assertRaisesMessage(AnymailInvalidAddress, "Invalid email address '\N{ENVELOPE}'"): parse_address_list(["\N{ENVELOPE}"])
def test_email_list(self): parsed_list = parse_address_list(["*****@*****.**", "*****@*****.**"]) self.assertEqual(len(parsed_list), 2) self.assertEqual(parsed_list[0].email, "*****@*****.**") self.assertEqual(parsed_list[1].email, "*****@*****.**")
def test_empty_address(self): with self.assertRaises(AnymailInvalidAddress): parse_address_list([''])
def test_invalid_with_unicode(self): # (assertRaisesMessage can't handle unicode in Python 2) with self.assertRaises(AnymailInvalidAddress) as cm: parse_address_list([u"\N{ENVELOPE}"]) self.assertIn(u"Invalid email address '\N{ENVELOPE}'", six.text_type(cm.exception))