Back to index

plone3  3.1.7
test_validateEmail.py
Go to the documentation of this file.
00001 #
00002 # Tests the email validation
00003 #
00004 
00005 import os, sys
00006 if __name__ == '__main__':
00007     execfile(os.path.join(sys.path[0], 'framework.py'))
00008 
00009 from common import *
00010 
00011 class TestValidateEmail(ZopeTestCase.ZopeTestCase):
00012 
00013     def afterSetUp(self):
00014         self.mailhost = SecureMailBase('securemailhost', '')
00015 
00016     def testvalidateSingleEmailAddress(self):
00017         # Any RFC 822 email address allowed, but address list must fail
00018         val = self.mailhost.validateSingleEmailAddress
00019         validInputs = (
00020             'user@example.org',
00021             'user@host.example.org',
00022             'm@t.nu',
00023             'm+m@example.biz',
00024             'm.+m@example.info',
00025             'foo&.%#$&\'*+-/=?^_`{}|~bar@baz.org'
00026 
00027             ## Some trickier ones, from RFC 822
00028             #'"A Name" user @ example',
00029             #'"A Name" user\n @ example',
00030             #'nn@[1.2.3.4]'
00031         )
00032         invalidInputs = (
00033             'user@foo', # rfc 822 requires the domain part
00034             'user@example.org, user2@example.org',   # only single address allowed
00035             'user@example.org,user2@example.org',
00036             #'user@example.org;user2@example.org',
00037             'user@example.org\n\nfoo', # double new lines
00038             'user@example.org\n\rfoo',
00039             'user@example.org\r\nfoo',
00040             'user@example.org\r\rfoo',
00041             'user@example.org\nfoo@example.org', # only single address allowed, even if given one per line
00042             'user@example.org\nBcc: user@example.org',
00043             'user@example.org\nCc: user@example.org',
00044             'user@example.org\nSubject: Spam',
00045             # and a full email (note the missing ,!)
00046             'From: foo@example.org\n'
00047             'To: bar@example.org, spam@example.org\n'
00048             'Cc: egg@example.org\n'
00049             'Subject: Spam me plenty\n'
00050             'Spam Spam Spam\n'
00051             'I hate spam',
00052             
00053         )
00054         for address in validInputs:
00055             self.failUnless(val(address), '%s should validate' % address)
00056         for address in invalidInputs:
00057             self.failIf(val(address), '%s should fail' % address)
00058     
00059     def testvalidateEmailAddresses(self):
00060         # Any RFC 822 email address allowed and address list allowed
00061         val = self.mailhost.validateEmailAddresses
00062 
00063         validInputs = (
00064             'user@example.org',
00065             'foo&.%#$&\'*+-/=?^_`{}|~bar@baz.org,\n foo&.%#$&\'*+-/=?^_`{}|~bar@baz.org',
00066             'user@example.org,\n user2@example.org',
00067             'user@example.org\n user2@example.org' # omitting comma is ok
00068         )
00069         invalidInputs = (
00070             'user@example.org\n\nfoo', # double new lines
00071             'user@example.org\n\rfoo',
00072             'user@example.org\r\nfoo',
00073             'user@example.org\r\rfoo',
00074             #py stdlib bug? 'user@example.org\nuser2@example.org', # continuation line doesn't begin with white space
00075         )
00076         for address in validInputs:
00077             self.failUnless(val(address), '%s should validate' % address)
00078         for address in invalidInputs:
00079             self.failIf(val(address), '%s should fail' % address)
00080 
00081 def test_suite():
00082     from unittest import TestSuite, makeSuite
00083     suite = TestSuite()
00084     suite.addTest(makeSuite(TestValidateEmail))
00085     return suite
00086 
00087 if __name__ == '__main__':
00088     framework()