Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

beaf up the test cases and add logging to test.py

  • Loading branch information...
commit 27a380cd114798be86b49a6d0f853e57d4dc215f 1 parent 11b57f5
Val Neekman authored
Showing with 41 additions and 10 deletions.
  1. +5 −1 emailahoy/__init__.py
  2. +36 −9 test.py
View
6 emailahoy/__init__.py
@@ -37,7 +37,7 @@ class VerifyEmail(object):
""" Verify if email exists """
EMAIL_RE = re.compile('([\w\-\.+]+@\w[\w\-]+\.+[\w\-]+)')
- default_response = (550, 'Unknown')
+ default_response = (550, 'Reason not known')
# given a hostname, all mx records will be returned
def get_mx_for_hostname(self, hostname):
@@ -128,6 +128,8 @@ def verify_email_smtp(
if self.is_email_valid(email):
hostname = self.get_hostname_from_email(email)
mx = self.get_mx_for_hostname(hostname)
+ if not mx:
+ return (550, 'No-Mx-Found')
for m in mx:
server = self.get_smtp_connection(m[1])
if server:
@@ -146,6 +148,8 @@ def verify_email_smtp(
except:
continue
break
+ if not server:
+ return (550, 'No-Server-Connection')
return resp
# given an email it returns True if it can tell it exist or False
View
45 test.py
@@ -5,50 +5,69 @@
from emailahoy import query_mx
from emailahoy import verify_email_address
import sys
+import logging
+
class TestEmailVerificationFunctions(unittest.TestCase):
def setUp(self):
""" Instantiate the class """
self.e = VerifyEmail()
+ self.log= logging.getLogger( "TestEmailVerificationFunctions" )
+
def test_class_based_invalid_email(self):
""" Test the existance of an invalid email address (class based)"""
+
+ email = 'non-existing-email@gmail.com'
+ self.log.debug("Testing classy invalid email address (%s)" % email)
+
status = self.e.verify_email_smtp(
- email='non-existing-email@gmail.com',
+ email=email,
from_host='neekware.com',
from_email='info@neekware.com'
)
- # email doesn't exist
+
+ self.log.debug(status)
self.assertEquals(self.e.not_found(status), True)
def test_class_based_valid_email(self):
""" Test the existance of a valid email address (class based)"""
+
+ email = 'info@neekware.com'
+ self.log.debug("Testing classy valid email address (%s)" % email)
status = self.e.verify_email_smtp(
email='info@neekware.com',
from_host='neekware.com',
from_email='info@neekware.com'
)
- print >> sys.stderr, status
- # email exists
+
+ self.log.debug(status)
self.assertEquals(self.e.was_found(status), True)
def test_function_based_invalid_email(self):
""" Test the existance of an invalid email address (function based)"""
+ email = 'non-existing-email@gmail.com'
+ self.log.debug("Testing invalid email address (%s)" % email)
+
found = verify_email_address(
- email='non-existing-email@gmail.com',
+ email=email,
from_host='neekware.com',
from_email='info@neekware.com'
)
+
# email doesn't exists
self.assertEquals(found, False)
def test_function_based_valid_email(self):
""" Test the existance of a valid email address (function based)"""
-
+
+ email = 'info@neekware.com'
+ self.log.debug("Testing valid email address (%s)" % email)
+
found = verify_email_address(
- email='info@neekware.com',
+ email=email,
from_host='neekware.com',
from_email='info@neekware.com'
)
@@ -58,18 +77,26 @@ def test_function_based_valid_email(self):
def test_mx_query_invalid_domain(self):
""" Query mx of an invalid domain name """
- mx = query_mx('invalid_domain_address.com')
+
+ domain = 'invalid_domain_address.com'
+ self.log.debug("Testing MX Query for invalid domain (%s)" % domain)
+ mx = query_mx(domain)
self.assertEquals(len(mx), 0)
def test_mx_query_valid_domain(self):
""" Query mx of a valid domain name """
-
+
+ domain = 'gmail.com'
+ self.log.debug("Testing MX Query for valid domain (%s)" % domain)
+
mx = query_mx('gmail.com')
self.assertGreater(len(mx), 0)
if __name__ == '__main__':
+ logging.basicConfig( stream=sys.stderr )
+ logging.getLogger( "TestEmailVerificationFunctions" ).setLevel( logging.DEBUG )
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.