Back to index

moin  1.9.0~rc2
test_sendmail.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     MoinMoin - MoinMoin.mail.sendmail Tests
00004 
00005     @copyright: 2003-2004 by Juergen Hermann <jh@web.de>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 from email.Charset import Charset, QP
00010 from email.Header import Header
00011 from MoinMoin.mail import sendmail
00012 from MoinMoin import config
00013 
00014 
00015 class TestdecodeSpamSafeEmail:
00016     """mail.sendmail: testing mail"""
00017 
00018     _tests = (
00019         ('', ''),
00020         ('AT', '@'),
00021         ('DOT', '.'),
00022         ('DASH', '-'),
00023         ('CAPS', ''),
00024         ('Mixed', 'Mixed'),
00025         ('lower', 'lower'),
00026         ('Firstname DOT Lastname AT example DOT net',
00027          'Firstname.Lastname@example.net'),
00028         ('Firstname . Lastname AT exa mp le DOT n e t',
00029          'Firstname.Lastname@example.net'),
00030         ('Firstname I DONT WANT SPAM . Lastname@example DOT net',
00031          'Firstname.Lastname@example.net'),
00032         ('First name I Lastname DONT AT WANT SPAM example DOT n e t',
00033          'FirstnameLastname@example.net'),
00034         ('first.last@example.com', 'first.last@example.com'),
00035         ('first . last @ example . com', 'first.last@example.com'),
00036         )
00037 
00038     def testDecodeSpamSafeMail(self):
00039         """mail.sendmail: decoding spam safe mail"""
00040         for coded, expected in self._tests:
00041             assert sendmail.decodeSpamSafeEmail(coded) == expected
00042 
00043 class TestencodeSpamSafeEmail:
00044     """mail.sendmail: testing spam safe mail"""
00045 
00046     _tests = (
00047         ('', ''),
00048         ('@', ' AT '),
00049         ('.', ' DOT '),
00050         ('-', ' DASH '),
00051         ('lower', 'lower'),
00052         ('Firstname.Lastname@example.net',
00053          'firstname DOT lastname AT example DOT net'),
00054         ('F.Lastname@example.net',
00055          'f DOT lastname AT example DOT net'),
00056         )
00057 
00058     def testEncodeSpamSafeMail(self):
00059         """mail.sendmail: encoding mail address to spam safe mail"""
00060         for coded, expected in self._tests:
00061             assert sendmail.encodeSpamSafeEmail(coded) == expected
00062 
00063     def testEncodeSpamSafeMailAndObfuscate(self):
00064         """mail.sendmail: encoding mail address by an obfuscate string to spam safe mail """
00065         for coded, expected in self._tests:
00066             expected = expected.replace(' AT ', ' AT SYCTE ')
00067             assert sendmail.encodeSpamSafeEmail(coded, 'SYCTE') == expected
00068 
00069 class TestEncodeAddress:
00070     """ Address encoding tests
00071 
00072     See http://www.faqs.org/rfcs/rfc2822.html section 3.4.
00073     Address Specification.
00074 
00075     mailbox     =   name-addr / addr-spec
00076     name-addr   =   [display-name] angle-addr
00077     angle-addr  =   [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr
00078     """
00079     charset = Charset(config.charset)
00080     charset.header_encoding = QP
00081     charset.body_encoding = QP
00082 
00083     def testSimpleAddress(self):
00084         """ mail.sendmail: encode simple address: local@domain """
00085         address = u'local@domain'
00086         expected = address.encode(config.charset)
00087         assert sendmail.encodeAddress(address, self.charset) == expected
00088 
00089     def testComposite(self):
00090         """ mail.sendmail: encode address: 'Phrase <local@domain>' """
00091         address = u'Phrase <local@domain>'
00092         expected = str(address)
00093         assert sendmail.encodeAddress(address, self.charset) == expected
00094 
00095     def testCompositeUnicode(self):
00096         """ mail.sendmail: encode Uncode address: 'ויקי <local@domain>' """
00097         address = u'ויקי <local@domain>'
00098         phrase = str(Header(u'ויקי'.encode('utf-8'), self.charset))
00099         expected = phrase + ' ' + '<local@domain>'
00100         assert sendmail.encodeAddress(address, self.charset) == expected
00101 
00102     def testEmptyPhrase(self):
00103         """ mail.sendmail: encode address with empty phrase: '<local@domain>' """
00104         address = u'<local@domain>'
00105         expected = 'local@domain'
00106         assert sendmail.encodeAddress(address, self.charset) == expected
00107 
00108     def testEmptyAddress(self):
00109         """ mail.sendmail: encode address with empty address: 'Phrase <>'
00110 
00111         Let the smtp server handle this. We may raise error in such
00112         case, but we don't do error checking for mail addresses.
00113         """
00114         address = u'Phrase <>'
00115         expected = str(address)
00116         assert sendmail.encodeAddress(address, self.charset) == expected
00117 
00118     def testInvalidAddress(self):
00119         """ mail.sendmail: encode invalid address 'Phrase <blah'
00120 
00121         Assume that this is a simple address. This address will
00122         probably cause an error when trying to send mail. Junk in, junk
00123         out.
00124         """
00125         address = u'Phrase <blah'
00126         expected = str(address)
00127         assert sendmail.encodeAddress(address, self.charset) == expected
00128 
00129 coverage_modules = ['MoinMoin.mail.sendmail']
00130