|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright The IETF Trust 2010, All Rights Reserved |
| 3 | + |
| 4 | +import unittest |
| 5 | +import mail |
| 6 | +from django.conf import settings |
| 7 | + |
| 8 | +mail.test_mode = True |
| 9 | + |
| 10 | +class MailDestinationTest(unittest.TestCase): |
| 11 | + def testSendMailText(self): |
| 12 | + mail.empty_outbox() |
| 13 | + mail.send_mail_text(None, [ 'a', 'b' ], None, 'Subject', 'body', cc=[ 'c', 'd' ], |
| 14 | + extra={ 'Extra1': 'Value1', 'Extra2': 'Value2' }, toUser=False, |
| 15 | + bcc='e') |
| 16 | + (result, to, txt) = mail.outbox[ 0 ] |
| 17 | + # add_headers must have added what we needed: |
| 18 | + self.assertNotEquals( result[ 'Message-ID' ], None ) |
| 19 | + self.assertNotEquals( result[ 'Date' ], None ) |
| 20 | + self.assertEquals( result[ 'From' ], settings.DEFAULT_FROM_EMAIL ) |
| 21 | + # The arguments should have come through |
| 22 | + self.assertEquals( result[ 'To' ], 'a, b' ) |
| 23 | + self.assertEquals( result[ 'Subject' ], 'Subject' ) |
| 24 | + self.assertEquals( result[ 'Cc' ], 'c, d' ) |
| 25 | + self.assertEquals( result[ 'Extra1' ], 'Value1' ) |
| 26 | + self.assertEquals( result[ 'Extra2' ], 'Value2' ) |
| 27 | + self.assertTrue( txt.endswith( 'body' ) ) |
| 28 | + # make sure that Bcc worked |
| 29 | + self.assertTrue( 'e' in to ) |
| 30 | + |
| 31 | + # Also test that archiving is done. |
| 32 | + self.assertTrue( len( mail.outbox ) == 2 ) |
| 33 | + (archive, to, txt) = mail.outbox[ 1 ] |
| 34 | + self.assertEquals( archive[ 'Subject' ], '[Django development] Subject' ) |
| 35 | + |
| 36 | + mail.empty_outbox() |
| 37 | + # Another test, just testing that a well-formed "From:" makes it through. |
| 38 | + test_from = "The person the email is from <person@email.exmaple.com>" |
| 39 | + mail.send_mail_text(None, [ 'a' ], test_from, 'Subject', 'body') |
| 40 | + (result, to, txt) = mail.outbox[ 0 ] |
| 41 | + self.assertEqual( result[ 'From' ], test_from ) |
| 42 | + |
| 43 | + mail.empty_outbox() |
| 44 | + # This should be the same result due to formataddr |
| 45 | + # Also check the use of formataddr in the To header. |
| 46 | + mail.send_mail_text(None, [ ('a', 'a@a.example.com'), 'b <b@b.example.com>' ], ( 'The person the email is from', 'person@email.exmaple.com' ), 'Subject', 'body', cc=[ 'd@d.example.com', ('e', 'e@e.example.com' ) ]) |
| 47 | + (result, to, txt) = mail.outbox[ 0 ] |
| 48 | + self.assertEqual( result[ 'From' ], test_from ) |
| 49 | + self.assertEqual( result[ 'To' ], 'a <a@a.example.com>, b <b@b.example.com>' ) |
| 50 | + self.assertEqual( result[ 'Cc' ], 'd@d.example.com, e <e@e.example.com>' ) |
| 51 | + self.assertEqual( to, [ 'a@a.example.com', 'b@b.example.com', 'd@d.example.com', 'e@e.example.com' ] ) |
| 52 | + |
| 53 | + def testSendMailSubj( self ): |
| 54 | + mail.empty_outbox() |
| 55 | + mail.send_mail_subj( None, 'a@a.example.com', None, |
| 56 | + 'test/mail_subject.txt', 'test/mail_body.txt', |
| 57 | + { 'value': 'one', 'thing': 'two', |
| 58 | + 'unsafe': '<>&'} ) |
| 59 | + (result, to, txt) = mail.outbox[ 0 ] |
| 60 | + self.assertEqual( result[ 'Subject' ], 'you can have <>& in your context' ) |
| 61 | + # In the test environment, we get base64-encoded ASCII. In the real environment, |
| 62 | + # we don't seem to. We'd like to test this. |
| 63 | + #self.assertTrue( 'template one rendered with a good two' in txt ) |
| 64 | + |
| 65 | + def testSendMail( self ): |
| 66 | + mail.empty_outbox() |
| 67 | + mail.send_mail( None, 'a@a.example.com', None, 'Subject', 'test/mail_body.txt', |
| 68 | + { 'value': 'one', 'thing': 'two' } ) |
| 69 | + (result, to, txt) = mail.outbox[ 0 ] |
| 70 | + self.assertEqual( result[ 'Subject' ], 'Subject' ) |
| 71 | + # In the test environment, we get base64-encoded ASCII. In the real environment, |
| 72 | + # we don't seem to. We'd like to test this. |
| 73 | + #self.assertTrue( 'template one rendered with a good two' in txt ) |
| 74 | + |
| 75 | + def testSendMailPreformatted( self ): |
| 76 | + mail.empty_outbox() |
| 77 | + mail.send_mail_preformatted( None, '''From: me@here.example.com |
| 78 | +To: Joe Blow <joe@a.example.com>, fred@b.example.com |
| 79 | +Subject: Hello, what do you know? |
| 80 | +Cc: d@d.example.com |
| 81 | +Bcc: x@x.example.com |
| 82 | +
|
| 83 | +body body body''' ) |
| 84 | + (result, to, txt) = mail.outbox[ 0 ] |
| 85 | + self.assertEqual( result[ 'From' ], 'me@here.example.com' ) |
| 86 | + self.assertEqual( result[ 'To' ], 'Joe Blow <joe@a.example.com>, fred@b.example.com' ) |
| 87 | + self.assertEqual( result[ 'Subject' ], 'Hello, what do you know?' ) |
| 88 | + self.assertEqual( result[ 'Cc' ], 'd@d.example.com' ) |
| 89 | + self.assertTrue( txt.endswith( 'body body body' ) ) |
| 90 | + self.assertEqual( set( to ), set( [ 'joe@a.example.com', 'fred@b.example.com', 'd@d.example.com', 'x@x.example.com' ] ) ) |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + unittest.main() |
0 commit comments