Back to index

plone3  3.1.7
test_messages.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 #
00003 # Tests the email validation
00004 #
00005 
00006 import os, sys
00007 if __name__ == '__main__':
00008     execfile(os.path.join(sys.path[0], 'framework.py'))
00009 
00010 from common import *
00011 from DateTime import DateTime
00012 from email.MIMEText import MIMEText
00013 import email.Message
00014 from Products.SecureMailHost import mail
00015 
00016 HERE = os.path.dirname(__file__)
00017 
00018 buergschaft_latin1_in = open(os.path.join(HERE, 'in', 'buergschaft.txt'), 'r').read()
00019 buergschaft_utf8_in = unicode(buergschaft_latin1_in, 'latin-1').encode('utf-8')
00020 loremipsum_in = open(os.path.join(HERE, 'in', 'loremipsum.txt'), 'r').read()
00021 buergschaft_latin1_msg = MIMEText(buergschaft_latin1_in, 'plain', 'latin-1')
00022 
00023 buergschaft_latin1_out = open(os.path.join(HERE, 'out', 'buergschaft_latin1.txt'), 'r').read()
00024 buergschaft_out = open(os.path.join(HERE, 'out', 'buergschaft.txt'), 'r').read()
00025 buergschaft_utf8_out = open(os.path.join(HERE, 'out', 'buergschaft_utf8.txt'), 'r').read()
00026 buergschaft_utf8_to_out = open(os.path.join(HERE, 'out', 'buergschaft_utf8_to.txt'), 'r').read()
00027 loremipsum_out = open(os.path.join(HERE, 'out', 'loremipsum.txt'), 'r').read()
00028 
00029 
00030 class TestMessage(ZopeTestCase.ZopeTestCase):
00031     """base message test
00032     """
00033     name    = 'no test'
00034     message = ''
00035     out     = ''
00036     subject = 'empty'
00037     charset = 'us-ascii'
00038     subtype = 'plain'
00039     out     = ''
00040     mfrom   = 'from@example.org'
00041     mto     = 'to@example.org'
00042     mto_out = 'to@example.org'
00043     mcc     = None
00044     mbcc    = None
00045     addHeaders = {'Message-Id' : '<1>' }
00046 
00047     def afterSetUp(self):
00048         self.mailhost = SecureMailBase('securemailhost', '')
00049 
00050     def testMessage(self):
00051         """
00052         """
00053         send = self.mailhost.secureSend
00054         kwargs = self.addHeaders
00055         kwargs['Date'] = DateTime(0).rfc822()
00056         result = send(self.message, self.mto, self.mfrom, subject=self.subject,
00057                       mcc=self.mcc, mbcc = self.mbcc,
00058                       subtype=self.subtype, charset=self.charset,
00059                       debug=True,
00060                       **self.addHeaders)
00061 
00062         self.failUnless(isinstance(result, mail.Mail), 'Result is not a mail.Mail instance')
00063 
00064         mfrom, mto, msg = result.mfrom, result.mto, result.message
00065         self.failUnlessEqual([self.mto_out], mto)
00066         self.failUnlessEqual(self.mfrom, mfrom)
00067         self.failUnless(isinstance(msg, email.Message.Message), 'message is not a email.Message.Message instance')
00068 
00069         msgstr = msg.as_string()
00070 
00071         # compare line by line
00072         outlines = self.out.split('\n')
00073         for i, m in enumerate(msgstr.split('\n')):
00074             if len(outlines) < i:
00075                 self.fail('output has less lines than msg')
00076             o = outlines[i]
00077             self.failUnlessEqual(m, o)
00078 
00079         # compare the complete string 
00080         self.failUnlessEqual(msgstr, self.out)
00081 
00082 tests = []
00083 
00084 class TestBuergschaftLatin1(TestMessage):
00085     name    = 'buergschaft_latin'
00086     message = buergschaft_latin1_in
00087     out     = buergschaft_latin1_out
00088 
00089     subject = 'Die Buergschaft'
00090     charset = 'latin-1'
00091     subtype = 'plain'
00092 
00093 tests.append(TestBuergschaftLatin1)
00094 
00095 class TestBuergschaftASCII(TestMessage):
00096     name    = 'buergschaft_latin_msg'
00097     message = buergschaft_latin1_msg
00098     out     = buergschaft_out
00099 
00100     subject = 'Die Buergschaft'
00101     #charset = 'us-ascii'
00102     #subtype = 'plain'
00103 
00104 tests.append(TestBuergschaftASCII)
00105 
00106 class TestBuergschaftUTF8(TestMessage):
00107     name    = 'buergschaft_uft8'
00108     message = buergschaft_utf8_in
00109     out     = buergschaft_utf8_out
00110 
00111     subject = 'Die Buergschaft'
00112     charset = 'utf8'
00113     subtype = 'plain'
00114 
00115 tests.append(TestBuergschaftUTF8)
00116 
00117 class TestLoremIpsum(TestMessage):
00118     name    = 'loremipsum'
00119     message = loremipsum_in
00120     out     = loremipsum_out
00121 
00122     subject = 'Lorem Ipsum'
00123     charset = 'ascii'
00124     subtype = 'plain'
00125 
00126 tests.append(TestLoremIpsum)
00127 
00128 class TestMimeTextAndNonAsciiHeaders(TestMessage):
00129     name    = 'mimetext_and_nonascii_headers'
00130     message = buergschaft_latin1_msg
00131     out     = buergschaft_utf8_to_out
00132 
00133     subject = u'Die B\u00fcrgschaft' 
00134     mto     = u'G\u00fcnter Schiller <gunter@example.org>'.encode('utf-8')
00135     mto_out = '=?utf-8?q?G=C3=BCnter_Schiller_?= <gunter@example.org>'
00136     charset = 'utf-8'
00137 
00138 tests.append(TestMimeTextAndNonAsciiHeaders)
00139 
00140 def test_suite():
00141     from unittest import TestSuite, makeSuite
00142     suite = TestSuite()
00143     for test in tests:
00144         suite.addTest(makeSuite(test))
00145     return suite
00146 
00147 if __name__ == '__main__':
00148     framework()