Exemplo n.º 1
0
def email_validator(email):
    try:
        e = Email()
        e.to_python(email)
        return True
    except Invalid:
        return False
Exemplo n.º 2
0
class TestEmail(unittest.TestCase):

    def setUp(self):
        self.validator = Email()

    def validate(self, *args):
        try:
            return self.validator.to_python(*args)
        except Invalid as e:
            return unicode(e)

    def message(self, message_name, username, domain):
        email = '@'.join((username, domain))
        return self.validator.message(
            message_name, email, username=username, domain=domain)

    def test_invalid_email_addresses(self):
        invalid_usernames = [
            # (username, domain, message_name),
            ('foo\tbar', 'formencode.org', 'badUsername'),
            ('foo\nbar', 'formencode.org', 'badUsername'),
            ('test', '', 'noAt'),
            ('test', 'foobar', 'badDomain'),
            ('test', 'foobar.5', 'badDomain'),
            ('test', 'foo..bar.com', 'badDomain'),
            ('test', '.foo.bar.com', 'badDomain'),
            ('foo,bar', 'formencode.org', 'badUsername')]

        for username, domain, message_name in invalid_usernames:
            email = '@'.join(el for el in (username, domain) if el)
            error = self.validate(email)
            expected = self.message(message_name, username, domain)
            self.assertEqual(error, expected)

    def test_valid_email_addresses(self):
        valid_email_addresses = [
            # (email address, expected email address),
            (' [email protected] ', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('o*[email protected]', 'o*[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('[email protected]', '[email protected]'),
            ('foo{bar}@example.com', 'foo{bar}@example.com'),
            # examples from RFC 3696
            #   punting on the difficult and extremely uncommon ones
            #('"Abc\@def"@example.com', '"Abc\@def"@example.com'),
            #('"Fred Bloggs"@example.com', '"Fred Bloggs"@example.com'),
            #('"Joe\\Blow"@example.com', '"Joe\\Blow"@example.com'),
            #('"Abc@def"@example.com', '"Abc@def"@example.com'),
            ('customer/[email protected]',
                'customer/[email protected]'),
            ('[email protected]', '[email protected]'),
            ('!def!xyz%[email protected]', '!def!xyz%[email protected]'),
            ('*****@*****.**', '*****@*****.**')]

        for email, expected in valid_email_addresses:
            self.assertEqual(self.validate(email), expected)
Exemplo n.º 3
0
class TestUnicodeEmailWithResolveDomain(TestCase):
    
    def setUp(self):
        self.validator = Email(resolve_domain=True)

    def test_unicode_ascii_subgroup(self):
        self.assertEqual(self.validator.to_python(u'*****@*****.**'), '*****@*****.**')
    
    def test_cyrillic_email(self):
        self.assertEqual(self.validator.to_python(u'me@письмо.рф'), u'me@письмо.рф')
Exemplo n.º 4
0
 def split_email_to(self, email_to):
     lines = email_to.splitlines()
     errors = []
     validator = Email()
     emails = []
     for line in lines:
         line = line.strip()
         if not line:
             continue
         try:
             emails.append(validator.to_python(line))
         except Invalid, e:
             errors.append(str(e))
Exemplo n.º 5
0
class TestEmailDomainBlacklist(unittest.TestCase):

    def setUp(self):
        self.validator = Email(domain_blacklist=[
            'example.com',
            '123.net'
        ])

    def validate(self, *args):
        return self.validator.to_python(*args)

    def test_blacklisted_domains(self):
        email_addresses = [
            # (email address, expected email address),
            (' [email protected] ', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('o*[email protected]', 'o*[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('[email protected]', '[email protected]'),
            ('foo{bar}@example.com', 'foo{bar}@example.com'),
            ('customer/[email protected]',
                'customer/[email protected]'),
            ('[email protected]', '[email protected]'),
            ('!def!xyz%[email protected]', '!def!xyz%[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**')]

        for email, expected in email_addresses:
            if 'example.com' in email or '123.net' in email:
                self.assertRaises(Invalid, self.validate, email)
            else:
                self.assertEqual(self.validate(email), expected)
Exemplo n.º 6
0
 def send(self, recipient):
     if recipient in self.ignored_recipients:
         return
     if hasattr(self, "subject") and hasattr(self, "text"):
         if isinstance(recipient, basestring):
             try:
                 EmailValidator.to_python(recipient)
                 recipient.encode('ascii')
                 send_email(self.sender, recipient, self.subject, self.text,
                            html_body=self.html,
                            attachments=self.attachments)
             except (Invalid, UnicodeEncodeError):
                 log.debug("Invalid email %(email)s" % dict(email=recipient))
         else:
             Message.send(self, recipient=recipient)
     else:
         raise RuntimeError("The message must have a subject and a text to be sent.")
Exemplo n.º 7
0
class TestEmail(unittest.TestCase):

    def setUp(self):
        self.validator = Email()

    def validate(self, *args):
        try:
            return self.validator.to_python(*args)
        except Invalid, e:
            return unicode(e)
Exemplo n.º 8
0
 def setUp(self):
     self.validator = Email(resolve_domain=True)
Exemplo n.º 9
0
 def setUp(self):
     self.validator = Email()
Exemplo n.º 10
0
 def setUp(self):
     self.validator = Email(domain_blacklist=[
         'example.com',
         '123.net'
     ])
Exemplo n.º 11
0
 def setUp(self):
     self.validator = Email(resolve_domain=True)
Exemplo n.º 12
0
 def setUp(self):
     self.validator = Email()
Exemplo n.º 13
0
class TestEmail(unittest.TestCase):
    def setUp(self):
        self.validator = Email()

    def validate(self, *args):
        try:
            return self.validator.to_python(*args)
        except Invalid as e:
            return six.text_type(e)

    def message(self, message_name, username, domain):
        email = '@'.join((username, domain))
        return self.validator.message(message_name,
                                      email,
                                      username=username,
                                      domain=domain)

    def test_invalid_email_addresses(self):
        invalid_usernames = [
            # (username, domain, message_name),
            ('foo\tbar', 'formencode.org', 'badUsername'),
            ('foo\nbar', 'formencode.org', 'badUsername'),
            ('test', '', 'noAt'),
            ('test', 'foobar', 'badDomain'),
            ('test', 'foobar.5', 'badDomain'),
            ('test', 'foo..bar.com', 'badDomain'),
            ('test', '.foo.bar.com', 'badDomain'),
            ('foo,bar', 'formencode.org', 'badUsername')
        ]

        for username, domain, message_name in invalid_usernames:
            email = '@'.join(el for el in (username, domain) if el)
            error = self.validate(email)
            expected = self.message(message_name, username, domain)
            self.assertEqual(error, expected)

    def test_valid_email_addresses(self):
        valid_email_addresses = [
            # (email address, expected email address),
            (' [email protected] ', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('o*[email protected]', 'o*[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('[email protected]', '[email protected]'),
            ('foo{bar}@example.com', 'foo{bar}@example.com'),
            # examples from RFC 3696
            #   punting on the difficult and extremely uncommon ones
            #('"Abc\@def"@example.com', '"Abc\@def"@example.com'),
            #('"Fred Bloggs"@example.com', '"Fred Bloggs"@example.com'),
            #('"Joe\\Blow"@example.com', '"Joe\\Blow"@example.com'),
            #('"Abc@def"@example.com', '"Abc@def"@example.com'),
            ('customer/[email protected]',
             'customer/[email protected]'),
            ('[email protected]', '[email protected]'),
            ('!def!xyz%[email protected]', '!def!xyz%[email protected]'),
            ('*****@*****.**', '*****@*****.**')
        ]

        for email, expected in valid_email_addresses:
            self.assertEqual(self.validate(email), expected)