iro/tests/email_validate.py
branchdevel
changeset 231 3929338fd17f
parent 193 e5ec4bfa4929
child 282 50cc13814bfb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/tests/email_validate.py	Sun Mar 18 14:06:27 2012 +0100
@@ -0,0 +1,65 @@
+import unittest
+
+from iro.validate import vEmail
+from iro.error import InvalidMail, ValidateException
+
+
+class testEmail(unittest.TestCase):
+    """tests for email adresses"""
+
+    def testVaildEmail(self):
+        '''test vaild email adresses (got from wikipedia)'''
+        validmails=["niceandsimple@example.com",
+                "simplewith+symbol@example.com",
+                'a.little.unusual@example.com',
+                'a.little.more.unusual@dept.example.com',
+                '@[10.10.10.10]',
+                '@[1.1.1.1]',
+                '@[200.100.100.100]',
+                'user@[2001:db8:1ff::a0b:dbd0]',
+                '"much.more\ unusual"@example.com',
+                't."test".t@example.com',
+                '"very.unusual.@.unusual.com"@example.com',
+                '"very.(),:;<>[]\\".VERY.\\"very@\\\ \\"very\\".unusual"@strange.example.com',
+                "!#$%&'*+-/=?^_`{}|~@example.org",
+                '"()<>[]:;@,\\\"!#$%&\'*+-/=?^_`{}| ~  ? ^_`{}|~."@example.org',
+                '""@example.org']
+        
+        for num in validmails:
+            self.failUnlessEqual(vEmail([num],None),[num])
+    
+    def testInvaildEmail(self):
+        '''test invaild email adresses (got from wikipedia)'''
+        invalid=["Abc.example.com",  # (an @ character must separate the local and domain parts)
+                "Abc.@example.com",          # (character dot(.) is last in local part)
+                "Abc..123@example.com",      # (character dot(.) is double)
+                "A@b@c@example.com",         # (only one @ is allowed outside quotation marks)
+                'a"b(c)d,e:f;g<h>i[j\k]l@example.com',  # (none of the special characters in this local part is allowed outside quotation marks)
+                'just"not"right@example.com',           # (quoted strings must be dot separated, or the only element making up the local-part)
+                'thisis."notallowed@example.com',      # (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a slash)
+                'this\\ still\\"not\\allowed@example.com',   # (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes)
+                't."test".t"test".t@example.com',
+                't."test"t."test".t@example.com',
+                ]
+
+        for number in invalid:
+            with self.assertRaises(InvalidMail) as e:
+                vEmail([number],None)
+            self.failUnlessEqual(e.exception.number, number)
+
+    def testInvalidDomain(self):
+        '''invalid Domainname'''
+        with self.assertRaises(InvalidMail):
+            vEmail(['x@&.de'],None)
+
+    def testDoubles(self):
+        self.assertEqual(vEmail(['x@test.de','x@test.de'],None),["x@test.de"])
+
+    def testString(self):
+        self.assertEqual(vEmail('x@test.de', None),"x@test.de")
+
+    def testAllowString(self):
+        self.assertRaises(ValidateException,vEmail, 'x@test.de', None, allowString=False)
+
+    def testAllowList(self):
+        self.assertRaises(ValidateException,vEmail, ['x@test.de'], None, allowList=False)