test_MailTemplate.py 31.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
# -*- coding: latin-1 -*-
# Copyright (c) 2005-2006 Simplistix Ltd
#
# This Software is released under the MIT License:
# http://www.opensource.org/licenses/mit-license.html
# See license.txt for more details.
import os

try:
    import Zope2 as Zope
except ImportError:
    import Zope

from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
from AccessControl.User import system as SystemUser, SimpleUser
from cStringIO import StringIO
from difflib import unified_diff
from OFS.Folder import Folder
from Products.MailHost.MailHost import MailHost
from Testing.makerequest import makerequest
from unittest import TestCase,TestSuite,makeSuite,main

try:
    # Zope 2.8 only
    from transaction import get as get_transaction
except ImportError:
    # Zope 2.7 only, allows get_transaction
    # to be imported from test_FSMailTemplate.
    get_transaction = get_transaction

test_folder = os.path.dirname(__file__)

class DummyFieldStorage:
    def __init__(self,filename,value):
        self.filename = filename
        self.value = value
        self.file = StringIO(value)
        self.content_type = None
        self.headers = {}

class DummyMailHost(MailHost):

    sent = False
    
    def setExpected(self,mfrom,mto,filename):
        self.mfrom = mfrom
        self.mto = mto
        self.messageText = open(
            os.path.join(test_folder,filename)
            ).read().replace('\r','')
        self.filename = filename

    def getId(self):
        return 'MailHost'

    def title_and_id(self):
        return 'MHTID'
    
    def assertEqual(self,x,y,message=None,field=None):
        if x!=y:
            if message:
                raise AssertionError(message)
            error = '%r!=%r' % (x,y)
            if field:
                error = field+':'+error
            raise AssertionError(error)
        
        
    def _send(self,mfrom,mto,messageText):
        self.assertEqual(self.mfrom,mfrom,field='mfrom')
        self.assertEqual(self.mto,mto,field='mto')
        expected_data = self.messageText.strip().split('\n')
        actual_data = messageText.strip().split('\n')
        # ignore dates
        for i in range(len(actual_data)):
            if actual_data[i].startswith('Date:'):
                actual_data[i]='Date:'
        diff = tuple(unified_diff(
            expected_data,
            actual_data,
            self.filename,
            'Test results',
            ))
        if diff:
            raise AssertionError(
                'Mail sent was not as expected:\n\n'+'\n'.join(diff)
                )

        self.sent = True

    def checkSent(self,value=True):
        if value:
            error = "Mail not sent"
        else:
            error = "Mail sent when it shouldn't have been"
        self.assertEqual(self.sent,value,error)
        
class DummyMailDropHost(DummyMailHost):

    meta_type = 'Maildrop Host'
    
class TestMailTemplate(TestCase):

    def setUp(self):
        self.app = makerequest(Zope.app())
        self.r = self.app.REQUEST
        self.r.other['URL1'] = 'http://foo/test_mt'
        self._add= self.app.manage_addProduct['MailTemplates'].addMailTemplate
        if getattr(self.app,'test_mt',None):
            self.app.manage_delObjects(ids=['test_mt'])
        if getattr(self.app,'MailHost',None):
            self.app.manage_delObjects(ids=['MailHost'])
        self.MailHost = self.app.MailHost = DummyMailHost()
        o = list(self.app._objects)
        o.append({'meta_type': 'Mail Host', 'id': 'MailHost'})
        self.app._objects = tuple(o)
        newSecurityManager( None, SystemUser )
        
    def tearDown(self):
        noSecurityManager()
        get_transaction().abort()
        self.app._p_jar.close()

    def makeFileUpload(self,filename='test.txt',value='test text',
                       diskname=''):
        if diskname:
            filename = diskname
            value = open(
                os.path.join(test_folder,diskname)
                ).read().replace('\r','').strip()
        from ZPublisher.HTTPRequest import FileUpload
        return FileUpload(DummyFieldStorage(
            filename,
            value
            ))
    
    def checkContent(self,text='test text'):
        if text is None:
            text = open(os.path.join(test_folder,'..','www','default.txt')).read()            
        self.assertEqual(
            self.app.test_mt.document_src({'raw':1}),
            text
            )
        
    # Test Adding
    
    def test_addAddForm(self):
        self.app.manage_addProduct['MailTemplates'].addMailTemplateForm()

    def test_addAddFormNoMailHosts(self):
        self.app.manage_delObjects(ids=['MailHost'])
        res = self.app.manage_addProduct['MailTemplates'].addMailTemplateForm()
154
        self.assertTrue(
155 156 157 158 159 160 161 162
            res.find(
            '<option value="MailHost">MHTID</option>'
            )==-1
            )

    def test_addAddFormMailHost(self):
        self.app._objects = ({'meta_type': 'Mail Host', 'id': 'MailHost'},)
        res = self.app.manage_addProduct['MailTemplates'].addMailTemplateForm()
163
        self.assertFalse(
164 165 166 167 168 169 170 171 172 173 174
            res.find(
            '<option value="MailHost">MHTID</option>'
            )==-1
            )

    def test_addAddFormMailDropHost(self):
        if getattr(self.app,'MailHost',None):
            self.app.manage_delObjects(ids=['MailHost'])
        self.MailHost = self.app.MailHost = DummyMailDropHost()
        self.app._objects = ({'meta_type': 'Maildrop Host', 'id': 'MailHost'},)
        res = self.app.manage_addProduct['MailTemplates'].addMailTemplateForm()
175
        self.assertFalse(
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
            res.find(
            '<option value="MailHost">MHTID</option>'
            )==-1
            )

    def test_addNoREQUEST(self):
        self._add('test_mt','MailHost')        
        # check settings
        self.assertEqual(self.app.test_mt.expand,0)
        self.assertEqual(self.app.test_mt.mailhost,'MailHost')
        self.assertEqual(self.app.test_mt.content_type,'text/plain')
        # check default content
        self.checkContent(None)

    def test_addNoMailHostSelected(self):
        self._add('test_mt',REQUEST=self.r)        
        # check settings
        self.assertEqual(self.app.test_mt.expand,0)
        self.assertEqual(self.app.test_mt.mailhost,None)
        self.assertEqual(self.app.test_mt.content_type,'text/plain')
        # check default content
        self.checkContent(None)
        # check the error we get when we try to send
        self.assertRaises(
            RuntimeError,self.app.test_mt,
            mfrom='from@example.com',
            mto='to@example.com',
            subject='Test Subject',
            )
        # no put a mogus mailhost in and check we get the same error
        self.app.test_mt.mailhost='bogus'
        self.assertRaises(
            RuntimeError,self.app.test_mt,
            mfrom='from@example.com',
            mto='to@example.com',
            subject='Test Subject',
            )

    def test_add(self,body = None):
        text = open(os.path.join(test_folder,'..','www','default.txt')).read()
        if body is not None:
            text = text[:-12] + body + text[-11:]
            self._add('test_mt','MailHost',text=text,REQUEST=self.r)
        else:
            self._add('test_mt','MailHost',REQUEST=self.r)
            
        self.assertEqual(
            self.r.RESPONSE.headers,
            {'status': '302 Moved Temporarily', 'location': 'http://foo/manage_main'}
            )
        self.mt = self.app.test_mt
        # check settings
        self.assertEqual(self.mt.expand,0)
        self.assertEqual(self.mt.mailhost,'MailHost')
        self.assertEqual(self.mt.content_type,'text/plain')
        # check default content
        self.assertEqual(
            self.app.test_mt.read(),
            text
            )
        # check default content type is text/plain
        self.assertEqual(self.app.test_mt.content_type,'text/plain')

    def test_addFile(self):
        self.r.form['file'] = self.makeFileUpload()
        self._add('test_mt','MailHost',REQUEST=self.r)
        # check settings
        self.assertEqual(self.app.test_mt.expand,0)
        self.assertEqual(self.app.test_mt.mailhost,'MailHost')
        self.assertEqual(self.app.test_mt.content_type,'text/plain')
        # check default content
        self.checkContent()

    def test_addEdit(self):
        self._add('test_mt','MailHost',REQUEST=self.r,submit=' Add and Edit ')
        self.assertEqual(
            self.r.RESPONSE.headers,
            {'status': '302 Moved Temporarily', 'location': 'http://foo/test_mt/manage_main'}
            )
        # check settings
        self.assertEqual(self.app.test_mt.expand,0)
        self.assertEqual(self.app.test_mt.mailhost,'MailHost')
        self.assertEqual(self.app.test_mt.content_type,'text/plain')
        # check default content
        self.checkContent(None)
        
    def test_addEditFile(self):
        self.r.form['file'] = self.makeFileUpload()
        self._add('test_mt','MailHost',REQUEST=self.r,submit=' Add and Edit ')
        self.assertEqual(
            self.r.RESPONSE.headers,
            {'status': '302 Moved Temporarily', 'location': 'http://foo/test_mt/manage_main'}
            )
        # check settings
        self.assertEqual(self.app.test_mt.expand,0)
        self.assertEqual(self.app.test_mt.mailhost,'MailHost')
        self.assertEqual(self.app.test_mt.content_type,'text/plain')
        # check default content
        self.checkContent()

    # Test Properties Tab
    # Not much here, as we assume PropertyManager does its job ;-)
    
    def test_PropertiesForm(self):
        self.test_add()
        self.mt.manage_propertiesForm()

    def test_PropertiesStartsEmpty(self):
        self.test_add()
285
        self.assertFalse(self.mt.propertyMap())
286 287 288 289 290 291 292 293

    # Test Test tab, well, actually, make sure it's not there ;-)
    
    def test_NoTestTab(self):
        from Products.MailTemplates.MailTemplate import MailTemplate
        for option in MailTemplate.manage_options:
            if option['label']=='Test':
                self.fail('Test label found')
294
        self.assertFalse(MailTemplate.ZScriptHTML_tryForm, 'try form not None')
295 296 297 298 299 300 301 302 303 304 305

    # Test Editing

    def test_editForm(self):
        self.test_add()
        self.mt.pt_editForm()

    def test_editFormMailHostGone(self):
        self.test_add()
        self.app.manage_delObjects('MailHost')
        r = self.mt.pt_editForm()
306
        self.assertFalse(
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
            r.find(
            """<option selected="selected" value="MailHost">'MailHost' is no longer valid!</option>"""
            )==-1,'No warning for MailHost being invalid found in:\n'+r
            )

    def test_editAction(self):
        self.test_add()
        self.mt.pt_editAction(REQUEST=self.r,
                              mailhost='MH2',
                              text='new text',
                              content_type='text/fish',
                              expand=1)
        self.assertEqual(self.mt.expand,1)
        self.assertEqual(self.mt.mailhost,'MH2')
        self.assertEqual(self.mt.content_type,'text/fish')
        self.checkContent('new text')

    def test_view_manage_workspace(self):
        self.test_add()
        from zExceptions import Redirect
        try:
            self.assertRaises(self.mt.manage_workspace(self.r))
        except Redirect,r:
            # this may appear to be incorrect, but http://foo/test_mt
            # is what we set as REQUEST['URL1']
            self.assertEqual(r.args,('http://foo/test_mt/pt_editForm',))
        # ugh, okay, so we can't really test for security, but lets
        # test for the missing docstring that was causing problems!
335
        self.assertTrue(self.mt.__doc__)
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    def test_view_manage_main(self):
        self.test_add()
        # for some bizare reason the output differs by a newline the first time these are called :-(
        self.mt.manage_main()
        self.mt.pt_editForm()
        self.assertEqual(self.mt.manage_main(),self.mt.pt_editForm())

    # Test Sending
    def testSendSimple(self):
        self.test_add('Test Body')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com', 'to2@example.com',
                                       'cc@example.com', 'bcc@example.com'),
                                  filename='mail_SendSimple.txt')
        self.mt.send(
            mfrom='from@example.com',
            mto=('to@example.com','to2@example.com'),
            mcc=('cc@example.com',),
            mbcc=('bcc@example.com',),
            subject='Hello out there',
            )

        self.MailHost.checkSent()

        # check we're not setting a content type
362
        self.assertFalse(self.r.RESPONSE.headers.get('content-type'),
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
                    self.r.RESPONSE.headers)

    def testMailHostNotAMailHost(self):
        self.test_add('Test Body')
        self.app.MailHost='Hahaha, not a MailHost'
        self.assertRaises(
            RuntimeError,
            self.mt.send,
            mfrom='from@example.com',
            mto=('to@example.com','to2@example.com'),
            mcc=('cc@example.com',),
            mbcc=('bcc@example.com',),
            subject='Hello out there',
            )
        
    def _shouldFail(self,error,**params):        
        self.test_add('Test Body')
        try:
            self.mt.send(**params)
        except TypeError,e:
            self.assertEqual(e.args[0],error)
        else:
            self.fail('Mail sent even though params missing')
        self.MailHost.checkSent(False)

    def testSendMissingParams1(self):
        self._shouldFail(
            'The following parameters were required by not specified: subject',
            mto='to@example.com',
            mfrom='from@example.com'
            )

    def testSendMissingParams2(self):
        self._shouldFail(
            'The following parameters were required by not specified: mfrom',
            mto='to@example.com',
            subject='Test Subject'
            )

    def testSendMissingParams3(self):
        self._shouldFail(
            'The following parameters were required by not specified: mto',
            mfrom='from@example.com',
            subject='Test Subject'
            )

    def testSendMissingParamsAll(self):
        self._shouldFail(
            'The following parameters were required by not specified: mfrom, mto, subject',
            )

    def testSendProperties(self):
        self.test_add('Test Body')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com', 'to2@example.com',
                                       'cc@example.com', 'bcc@example.com'),
                                  filename='mail_SendSimple.txt')
        for name,type,value in (
            ('mfrom','string','from@example.com'),
            ('mto','string','to@example.com, to2@example.com'),
            ('mbcc','lines',('bcc@example.com',)),
            ('subject','string','Hello out there'),
            ('headers','lines',
             ('Cc:cc@example.com',)),
            ):
            self.mt.manage_addProperty(name,value,type)
            
        self.mt.send()

        self.MailHost.checkSent()
        
    def testSendHeadersDict(self):
        self.test_add('Test Body')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com','to2@example.com',
                                       'cc@example.com', 'bcc@example.com'),
                                  filename='mail_SendHeaders.txt')
        self.mt.send(
            headers = {
            'From':'from@example.com',
            'To':('to@example.com','to2@example.com'),
            'Cc':('cc@example.com',),
            'Bcc':('bcc@example.com',),
            'Subject':'Hello out there',
            'X-Mailer':'MailTemplates',
            }
            )

        self.MailHost.checkSent()
        
    def testSendParametersOverrideHeadersDictOverridesProperties(self):
        self.test_add('Test Body')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com','to2@example.com',
                                       'cc@example.com', 'bcc@example.com'),
                                  filename='mail_SendHeaders2.txt')
        for name,type,value in (
            ('mfrom','string','from@example.com'),
            ('mto','string','frog@example.com'),
            ('mcc','lines',('cc@example.com',)),
            ('mbcc','lines',('bcc@example.com',)),
            ('subject','string','Hello %s there'),
            ('headers','lines',(
                'X-Mailer: MailTemplates',
                'X-Mailer2: MailTemplatesBad',
                ))
            ):
            self.mt.manage_addProperty(name,value,type)
            
        self.mt.send(subject=self.mt.subject % 'out',
                     headers={
            'To':('to@example.com','to2@example.com'),
            'Subject':'cheese',
            'X-Mailer2':'MailTemplates',
            })

        self.MailHost.checkSent()

    def testSendParametersGoToOptions(self):
        self.test_add('Test <tal:x replace="options/body"/>')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com','to2@example.com',
                                       'cc@example.com', 'bcc@example.com'),
                                  filename='mail_SendSimple.txt')
        for name,type,value in (
            ('mfrom','string','from@example.com'),
            ('mto','string','frog@example.com'),
            ('mcc','lines',('cc@example.com',)),
            ('mbcc','lines',('bcc@example.com',)),
            ('subject','string','Hello %s there'),
            ):
            self.mt.manage_addProperty(name,value,type)
            
        self.mt.send(subject=self.mt.subject % 'out',
                     headers={
            'To':('to@example.com','to2@example.com'),
            'Subject':'cheese',
            },
                     body='Body')

        self.MailHost.checkSent()

    def testPropertiesParametersAndSubstitution(self):
        self.test_add('Test Body')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com', 'to2@example.com',
                                       'cc@example.com', 'bcc@example.com'),
                                  filename='mail_SendSimple.txt')
        for name,type,value in (
            ('mfrom','string','from@example.com'),
            ('mto','string','to@example.com, to2@example.com'),
            ('mcc','lines',('cc@example.com',)),
            ('mbcc','lines',('bcc@example.com',)),
            ('subject','string','Hello %s there'),
            ):
            self.mt.manage_addProperty(name,value,type)
            
        self.mt.send(subject=self.mt.subject % 'out')

        self.MailHost.checkSent()
        
    def testGetMessage(self):
        from email.MIMEMultipart import MIMEMultipart
        from email.MIMEText import MIMEText
        
        self.test_add('Test <tal:x replace="options/body"/>')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com','to2@example.com'),
                                  filename='mail_SendAttachment.txt')
        for name,type,value in (
            ('mfrom','string','from@example.com'),
            ('mto','string','frog@example.com'),
            ('mcc','lines',('cc@example.com',)),
            ('mbcc','lines',('bcc@example.com',)),
            ('subject','string','Hello %s there'),
            ):
            self.mt.manage_addProperty(name,value,type)
            
        msg = self.mt.as_message(subject=self.mt.subject % 'out',
                                 headers={
            'To':('to@example.com','to2@example.com'),
            'Subject':'cheese',
            },
                                 body='Body',
                                 boundary='111',
                                 subtype='alternative')

550
        self.assertTrue(isinstance(msg,MIMEMultipart))
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
        attachment = MIMEText('A Test Attachment',_subtype='plain')
        attachment.add_header('Content-Disposition', 'attachment', filename='test.txt')
        msg.attach(attachment)
        msg.send()

        self.MailHost.checkSent()

    def _addFileSetup(self):
        from email.MIMEMultipart import MIMEMultipart
        
        self.test_add('Test <tal:x replace="options/body"/>')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com','to2@example.com'),
                                  filename='mail_SendFile.txt')
        for name,type,value in (
            ('mfrom','string','from@example.com'),
            ('mto','string','frog@example.com'),
            ('mcc','lines',('cc@example.com',)),
            ('mbcc','lines',('bcc@example.com',)),
            ('subject','string','Hello %s there'),
            ):
            self.mt.manage_addProperty(name,value,type)
            
        msg = self.mt.as_message(subject=self.mt.subject % 'out',
                                 headers={
            'To':('to@example.com','to2@example.com'),
            'Subject':'cheese',
            },
                                 body='Body',
                                 boundary='111',
                                 subtype='alternative')

583
        self.assertTrue(isinstance(msg,MIMEMultipart))
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
        return msg
        
    def testZopeFileObject(self):
        self.app.manage_addFile('test.txt',
                                'A Test Attachment')        
        msg = self._addFileSetup()
        msg.add_file(self.app['test.txt'])
        msg.send()
        
        self.MailHost.checkSent()

    def testPythonFileObject(self):
        msg = self._addFileSetup()
        msg.add_file(open(
            os.path.join(test_folder,'test.txt')
            ))
        msg.send()
        
        self.MailHost.checkSent()

    def testFileUploadObject(self):
        msg = self._addFileSetup()
        msg.add_file(self.makeFileUpload(
            value='A Test Attachment'
            ))
        msg.send()
        
        self.MailHost.checkSent()

    def testStringWithContentType(self):
        msg = self._addFileSetup()
        msg.add_file(
            data=open(
            os.path.join(test_folder,'test.txt')
            ).read(),
            filename='test.txt',
            content_type='text/plain'
            )
        msg.send()
        
        self.MailHost.checkSent()

    def testStringWithoutContentType(self):
        msg = self._addFileSetup()
        msg.add_file(
            data=open(
            os.path.join(test_folder,'test.txt')
            ).read(),
            filename='test.txt'
            )
        msg.send()
        
        self.MailHost.checkSent()

    def testTooManyParameters(self):
        msg = self._addFileSetup()
        self.assertRaises(
            TypeError,
            msg.add_file,
            self.makeFileUpload(
            value='A Test Attachment'
            ),
            data=open(
            os.path.join(test_folder,'test.txt')
            ).read(),
            filename='test.txt',
            content_type='text/plain'
            )

    def testTooFewParameters(self):
        msg = self._addFileSetup()
        self.assertRaises(
            TypeError,
            msg.add_file
            )

    def testDataWithoutFilename(self):
        msg = self._addFileSetup()
        self.assertRaises(
            TypeError,
            msg.add_file,
            data=open(
            os.path.join(test_folder,'test.txt')
            ).read(),
            content_type='text/plain'
            )

    def testFilenameWithoutData(self):
        msg = self._addFileSetup()
        self.assertRaises(
            TypeError,
            msg.add_file,
            filename='test.txt',
            content_type='text/plain'
            )

    def testCallAliasesSend(self):
        self.test_add('Test Body')
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com',),
                                  filename='mail_SendSimpleSomeHeaders.txt')
        self.mt(
            mfrom='from@example.com',
            mto=('to@example.com',),
            subject='Test Subject'
            )

        self.MailHost.checkSent()

    def test_encoded_not_html_mode(self):
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com',),
                                  filename='mail_unicode.txt')
        self.test_add('Test <tal:x replace="options/unicode"/>')
        # we get a unicode error here because we're trying to
        # use an encoded string in a non-html-mode page template.
        # It should have been decoded first.
        self.assertRaises(
            UnicodeDecodeError,
            self.mt,
            mfrom='from@example.com',
            mto=('to@example.com',),
            subject='Test Subject',
            unicode=u''.encode('utf-8'),
            encoding='utf-8'
            )

    def test_encoded_html_mode(self):
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com',),
                                  filename='mail_unicode2.txt')
        self.test_add('')
        self.mt.pt_edit('Test <tal:x replace="options/unicode"/>',
                        'text/html')
        
        self.mt(
            mfrom='from@example.com',
            mto=('to@example.com',),
            subject='Test Subject',
            unicode=u''.encode('utf-8'),
            encoding='utf-8'
            )

    def test_unicode_not_html_mode(self):
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com',),
                                  filename='mail_unicode.txt')
        self.test_add('Test <tal:x replace="options/unicode"/>')
        self.mt(
            mfrom='from@example.com',
            mto=('to@example.com',),
            subject='Test Subject',
            unicode=u'',
            encoding='utf-8'
            )

    def test_unicode_html_mode(self):
        self.MailHost.setExpected(mfrom='from@example.com',
                                  mto=('to@example.com',),
                                  filename='mail_unicode2.txt')
        self.test_add('')
        self.mt.pt_edit('Test <tal:x replace="options/unicode"/>',
                        'text/html')

        # We get a unicode error here because we're trying to
        # insert a unicode into an html-mode template.
        # It should have been encoded first.
        self.assertRaises(
            UnicodeEncodeError,
            self.mt,
            mfrom='from@example.com',
            mto=('to@example.com',),
            subject='Test Subject',
            unicode=u'',
            encoding='utf-8'
            )

    def test_example1(self):
        # login
        noSecurityManager()
        self.app.aq_chain[-1].id = 'testing'
        newSecurityManager(
            None,
            SimpleUser('Test User','',('Manager',),[]).__of__(self.app)
            )
        try:
            # setup 
            self.r.form['file']=self.makeFileUpload(diskname='example1.mt')
            self.app.manage_addProduct['MailTemplates'].addMailTemplate(
                id='my_mt',
                mailhost='MailHost',
                REQUEST=self.r
                )        
            self.r.form['file']=self.makeFileUpload(diskname='example1.py')
            self.app.manage_addProduct['PythonScripts'].manage_addPythonScript(
                id='test_mt',
                REQUEST=self.r
                )
            # set expected
            self.MailHost.setExpected(mfrom='webmaster@example.com',
                                      mto=('user@example.com',),
                                      filename='example1.txt')
            # test
            self.assertEqual(self.app.test_mt(),'Mail Sent!')
            self.MailHost.checkSent()
        finally:
            # logout
            noSecurityManager()
            newSecurityManager( None, SystemUser )

    def test_example3(self):
        # login
        noSecurityManager()
        self.app.aq_chain[-1].id = 'testing'
        newSecurityManager(
            None,
            SimpleUser('Test User','',('Manager',),[]).__of__(self.app)
            )
        try:
            # setup 
            self.r.form['file']=self.makeFileUpload(diskname='example3.mt')
            self.app.manage_addProduct['MailTemplates'].addMailTemplate(
                id='my_mt',
                mailhost='MailHost',
                REQUEST=self.r
                )        
            self.app.manage_addFile(
                id='myfile.bin',
                file=self.makeFileUpload(diskname='example3.bin')
                )        
            self.r.form['file']=self.makeFileUpload(diskname='example3.py')
            self.app.manage_addProduct['PythonScripts'].manage_addPythonScript(
                id='send_mail',
                REQUEST=self.r
                )
            # set expected
            self.MailHost.setExpected(mfrom='from@example.com',
                                      mto='to1@example.com',
                                      filename='example3.txt')
            # test
            self.assertEqual(self.app.send_mail(),'Mail Sent!')
            self.MailHost.checkSent()
        finally:
            # logout
            noSecurityManager()
            newSecurityManager( None, SystemUser )

    def test_example4(self):
        # login
        noSecurityManager()
        self.app.aq_chain[-1].id = 'testing'
        newSecurityManager(
            None,
            SimpleUser('Test User','',('Manager',),[]).__of__(self.app)
            )
        try:
            # setup 
            self.r.form['file']=self.makeFileUpload(diskname='example4.mt')
            self.app.manage_addProduct['MailTemplates'].addMailTemplate(
                id='my_mt',
                mailhost='MailHost',
                REQUEST=self.r
                )
            self.app.my_mt.manage_addProperty(
                'subject','Welcome to %s','string'
                )
            self.app.my_mt.manage_addProperty(
                'mfrom','webmaster@example.com','string'
                )
            self.r.form['file']=self.makeFileUpload(diskname='example4.py')
            self.app.manage_addProduct['PythonScripts'].manage_addPythonScript(
                id='send_mail',
                REQUEST=self.r
                )
            # set expected
            self.MailHost.setExpected(mfrom='webmaster@example.com',
                                      mto=('user@example.com',),
                                      filename='example4.txt')
            # test
            self.assertEqual(self.app.send_mail(),'Mail Sent!')
            self.MailHost.checkSent()
        finally:
            # logout
            noSecurityManager()
            newSecurityManager( None, SystemUser )

def test_suite():
    return TestSuite((
        makeSuite(TestMailTemplate),
        ))

if __name__ == '__main__':
    main(defaultTest='test_suite')