comparison test/test_mailgw.py @ 1873:f63aa57386b0

Backend improvements. - using Zope3's test runner now, allowing GC checks, nicer controls and coverage analysis - all RDMBS backends now have indexes on several columns - added testing of schema mutation, fixed rdbms backends handling of a couple of cases - !BETA! added postgresql backend, needs work !BETA!
author Richard Jones <richard@users.sourceforge.net>
date Sat, 25 Oct 2003 22:53:26 +0000
parents 3260268e45d2
children 9d8d5fa0d9ad
comparison
equal deleted inserted replaced
1872:c085b4f4f0c0 1873:f63aa57386b0
6 # 6 #
7 # This module is distributed in the hope that it will be useful, 7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # 10 #
11 # $Id: test_mailgw.py,v 1.56 2003-10-25 12:02:36 jlgijsbers Exp $ 11 # $Id: test_mailgw.py,v 1.57 2003-10-25 22:53:26 richard Exp $
12 12
13 import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822 13 import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822
14 14
15 from cStringIO import StringIO 15 from cStringIO import StringIO
16
17 if not os.environ.has_key('SENDMAILDEBUG'):
18 os.environ['SENDMAILDEBUG'] = 'mail-test.log'
19 SENDMAILDEBUG = os.environ['SENDMAILDEBUG']
16 20
17 from roundup.mailgw import MailGW, Unauthorized, uidFromAddress, parseContent 21 from roundup.mailgw import MailGW, Unauthorized, uidFromAddress, parseContent
18 from roundup import init, instance, rfc2822 22 from roundup import init, instance, rfc2822
19 23
20 NEEDS_INSTANCE = 1
21 24
22 class Message(rfc822.Message): 25 class Message(rfc822.Message):
23 """String-based Message class with equivalence test.""" 26 """String-based Message class with equivalence test."""
24 def __init__(self, s): 27 def __init__(self, s):
25 rfc822.Message.__init__(self, StringIO(s.strip())) 28 rfc822.Message.__init__(self, StringIO(s.strip()))
77 try: 80 try:
78 shutil.rmtree(self.dirname) 81 shutil.rmtree(self.dirname)
79 except OSError, error: 82 except OSError, error:
80 if error.errno not in (errno.ENOENT, errno.ESRCH): raise 83 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
81 # create the instance 84 # create the instance
82 shutil.copytree('_empty_instance', self.dirname) 85 init.install(self.dirname, 'templates/classic')
86 init.write_select_db(self.dirname, 'anydbm')
87 init.initialise(self.dirname, 'sekrit')
83 88
84 # check we can load the package 89 # check we can load the package
85 self.instance = instance.open(self.dirname) 90 self.instance = instance.open(self.dirname)
91
86 # and open the database 92 # and open the database
87 self.db = self.instance.open('admin') 93 self.db = self.instance.open('admin')
88 self.db.user.create(username='Chef', address='chef@bork.bork.bork', 94 self.db.user.create(username='Chef', address='chef@bork.bork.bork',
89 realname='Bork, Chef', roles='User') 95 realname='Bork, Chef', roles='User')
90 self.db.user.create(username='richard', address='richard@test', 96 self.db.user.create(username='richard', address='richard@test',
94 self.db.user.create(username='john', address='john@test', 100 self.db.user.create(username='john', address='john@test',
95 alternate_addresses='jondoe@test\njohn.doe@test', roles='User', 101 alternate_addresses='jondoe@test\njohn.doe@test', roles='User',
96 realname='John Doe') 102 realname='John Doe')
97 103
98 def tearDown(self): 104 def tearDown(self):
99 if os.path.exists(os.environ['SENDMAILDEBUG']): 105 if os.path.exists(SENDMAILDEBUG):
100 os.remove(os.environ['SENDMAILDEBUG']) 106 os.remove(SENDMAILDEBUG)
101 self.db.close() 107 self.db.close()
102 try: 108 try:
103 shutil.rmtree(self.dirname) 109 shutil.rmtree(self.dirname)
104 except OSError, error: 110 except OSError, error:
105 if error.errno not in (errno.ENOENT, errno.ESRCH): raise 111 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
106 112
113 def _get_mail(self):
114 f = open(SENDMAILDEBUG)
115 try:
116 return f.read()
117 finally:
118 f.close()
119
107 def testEmptyMessage(self): 120 def testEmptyMessage(self):
108 message = StringIO('''Content-Type: text/plain; 121 message = StringIO('''Content-Type: text/plain;
109 charset="iso-8859-1" 122 charset="iso-8859-1"
110 From: Chef <chef@bork.bork.bork> 123 From: Chef <chef@bork.bork.bork>
111 To: issue_tracker@your.tracker.email.domain.example 124 To: issue_tracker@your.tracker.email.domain.example
115 128
116 ''') 129 ''')
117 handler = self.instance.MailGW(self.instance, self.db) 130 handler = self.instance.MailGW(self.instance, self.db)
118 handler.trapExceptions = 0 131 handler.trapExceptions = 0
119 nodeid = handler.main(message) 132 nodeid = handler.main(message)
120 if os.path.exists(os.environ['SENDMAILDEBUG']): 133 assert not os.path.exists(SENDMAILDEBUG)
121 error = open(os.environ['SENDMAILDEBUG']).read()
122 self.assertEqual('no error', error)
123 self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...') 134 self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...')
124 135
125 def doNewIssue(self): 136 def doNewIssue(self):
126 message = StringIO('''Content-Type: text/plain; 137 message = StringIO('''Content-Type: text/plain;
127 charset="iso-8859-1" 138 charset="iso-8859-1"
134 This is a test submission of a new issue. 145 This is a test submission of a new issue.
135 ''') 146 ''')
136 handler = self.instance.MailGW(self.instance, self.db) 147 handler = self.instance.MailGW(self.instance, self.db)
137 handler.trapExceptions = 0 148 handler.trapExceptions = 0
138 nodeid = handler.main(message) 149 nodeid = handler.main(message)
139 if os.path.exists(os.environ['SENDMAILDEBUG']): 150 assert not os.path.exists(SENDMAILDEBUG)
140 error = open(os.environ['SENDMAILDEBUG']).read()
141 self.assertEqual('no error', error)
142 l = self.db.issue.get(nodeid, 'nosy') 151 l = self.db.issue.get(nodeid, 'nosy')
143 l.sort() 152 l.sort()
144 self.assertEqual(l, ['3', '4']) 153 self.assertEqual(l, ['3', '4'])
145 return nodeid 154 return nodeid
146 155
160 This is a test submission of a new issue. 169 This is a test submission of a new issue.
161 ''') 170 ''')
162 handler = self.instance.MailGW(self.instance, self.db) 171 handler = self.instance.MailGW(self.instance, self.db)
163 handler.trapExceptions = 0 172 handler.trapExceptions = 0
164 nodeid = handler.main(message) 173 nodeid = handler.main(message)
165 if os.path.exists(os.environ['SENDMAILDEBUG']): 174 assert not os.path.exists(SENDMAILDEBUG)
166 error = open(os.environ['SENDMAILDEBUG']).read()
167 self.assertEqual('no error', error)
168 l = self.db.issue.get(nodeid, 'nosy') 175 l = self.db.issue.get(nodeid, 'nosy')
169 l.sort() 176 l.sort()
170 self.assertEqual(l, ['3', '4']) 177 self.assertEqual(l, ['3', '4'])
171 178
172 def testAlternateAddress(self): 179 def testAlternateAddress(self):
181 ''') 188 ''')
182 userlist = self.db.user.list() 189 userlist = self.db.user.list()
183 handler = self.instance.MailGW(self.instance, self.db) 190 handler = self.instance.MailGW(self.instance, self.db)
184 handler.trapExceptions = 0 191 handler.trapExceptions = 0
185 handler.main(message) 192 handler.main(message)
186 if os.path.exists(os.environ['SENDMAILDEBUG']): 193 assert not os.path.exists(SENDMAILDEBUG)
187 error = open(os.environ['SENDMAILDEBUG']).read()
188 self.assertEqual('no error', error)
189 self.assertEqual(userlist, self.db.user.list(), 194 self.assertEqual(userlist, self.db.user.list(),
190 "user created when it shouldn't have been") 195 "user created when it shouldn't have been")
191 196
192 def testNewIssueNoClass(self): 197 def testNewIssueNoClass(self):
193 message = StringIO('''Content-Type: text/plain; 198 message = StringIO('''Content-Type: text/plain;
201 This is a test submission of a new issue. 206 This is a test submission of a new issue.
202 ''') 207 ''')
203 handler = self.instance.MailGW(self.instance, self.db) 208 handler = self.instance.MailGW(self.instance, self.db)
204 handler.trapExceptions = 0 209 handler.trapExceptions = 0
205 handler.main(message) 210 handler.main(message)
206 if os.path.exists(os.environ['SENDMAILDEBUG']): 211 assert not os.path.exists(SENDMAILDEBUG)
207 error = open(os.environ['SENDMAILDEBUG']).read()
208 self.assertEqual('no error', error)
209 212
210 def testNewIssueAuthMsg(self): 213 def testNewIssueAuthMsg(self):
211 message = StringIO('''Content-Type: text/plain; 214 message = StringIO('''Content-Type: text/plain;
212 charset="iso-8859-1" 215 charset="iso-8859-1"
213 From: Chef <chef@bork.bork.bork> 216 From: Chef <chef@bork.bork.bork>
221 handler.trapExceptions = 0 224 handler.trapExceptions = 0
222 # TODO: fix the damn config - this is apalling 225 # TODO: fix the damn config - this is apalling
223 self.db.config.MESSAGES_TO_AUTHOR = 'yes' 226 self.db.config.MESSAGES_TO_AUTHOR = 'yes'
224 handler.main(message) 227 handler.main(message)
225 228
226 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 229 self.compareMessages(self._get_mail(),
227 '''FROM: roundup-admin@your.tracker.email.domain.example 230 '''FROM: roundup-admin@your.tracker.email.domain.example
228 TO: chef@bork.bork.bork, mary@test, richard@test 231 TO: chef@bork.bork.bork, mary@test, richard@test
229 Content-Type: text/plain; charset=utf-8 232 Content-Type: text/plain; charset=utf-8
230 Subject: [issue1] Testing... 233 Subject: [issue1] Testing...
231 To: chef@bork.bork.bork, mary@test, richard@test 234 To: chef@bork.bork.bork, mary@test, richard@test
276 This is a second followup 279 This is a second followup
277 ''') 280 ''')
278 handler = self.instance.MailGW(self.instance, self.db) 281 handler = self.instance.MailGW(self.instance, self.db)
279 handler.trapExceptions = 0 282 handler.trapExceptions = 0
280 handler.main(message) 283 handler.main(message)
281 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 284 self.compareMessages(self._get_mail(),
282 '''FROM: roundup-admin@your.tracker.email.domain.example 285 '''FROM: roundup-admin@your.tracker.email.domain.example
283 TO: chef@bork.bork.bork, richard@test 286 TO: chef@bork.bork.bork, richard@test
284 Content-Type: text/plain; charset=utf-8 287 Content-Type: text/plain; charset=utf-8
285 Subject: [issue1] Testing... 288 Subject: [issue1] Testing...
286 To: chef@bork.bork.bork, richard@test 289 To: chef@bork.bork.bork, richard@test
324 handler.main(message) 327 handler.main(message)
325 l = self.db.issue.get('1', 'nosy') 328 l = self.db.issue.get('1', 'nosy')
326 l.sort() 329 l.sort()
327 self.assertEqual(l, ['3', '4', '5', '6']) 330 self.assertEqual(l, ['3', '4', '5', '6'])
328 331
329 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 332 self.compareMessages(self._get_mail(),
330 '''FROM: roundup-admin@your.tracker.email.domain.example 333 '''FROM: roundup-admin@your.tracker.email.domain.example
331 TO: chef@bork.bork.bork, john@test, mary@test 334 TO: chef@bork.bork.bork, john@test, mary@test
332 Content-Type: text/plain; charset=utf-8 335 Content-Type: text/plain; charset=utf-8
333 Subject: [issue1] Testing... 336 Subject: [issue1] Testing...
334 To: chef@bork.bork.bork, john@test, mary@test 337 To: chef@bork.bork.bork, john@test, mary@test
370 ''') 373 ''')
371 handler = self.instance.MailGW(self.instance, self.db) 374 handler = self.instance.MailGW(self.instance, self.db)
372 handler.trapExceptions = 0 375 handler.trapExceptions = 0
373 handler.main(message) 376 handler.main(message)
374 377
375 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 378 self.compareMessages(self._get_mail(),
376 '''FROM: roundup-admin@your.tracker.email.domain.example 379 '''FROM: roundup-admin@your.tracker.email.domain.example
377 TO: chef@bork.bork.bork, john@test, mary@test 380 TO: chef@bork.bork.bork, john@test, mary@test
378 Content-Type: text/plain; charset=utf-8 381 Content-Type: text/plain; charset=utf-8
379 Subject: [issue1] Testing... 382 Subject: [issue1] Testing...
380 To: chef@bork.bork.bork, john@test, mary@test 383 To: chef@bork.bork.bork, john@test, mary@test
417 ''') 420 ''')
418 handler = self.instance.MailGW(self.instance, self.db) 421 handler = self.instance.MailGW(self.instance, self.db)
419 handler.trapExceptions = 0 422 handler.trapExceptions = 0
420 handler.main(message) 423 handler.main(message)
421 424
422 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 425 self.compareMessages(self._get_mail(),
423 '''FROM: roundup-admin@your.tracker.email.domain.example 426 '''FROM: roundup-admin@your.tracker.email.domain.example
424 TO: chef@bork.bork.bork, richard@test 427 TO: chef@bork.bork.bork, richard@test
425 Content-Type: text/plain; charset=utf-8 428 Content-Type: text/plain; charset=utf-8
426 Subject: [issue1] Testing... 429 Subject: [issue1] Testing...
427 To: chef@bork.bork.bork, richard@test 430 To: chef@bork.bork.bork, richard@test
465 ''') 468 ''')
466 handler = self.instance.MailGW(self.instance, self.db) 469 handler = self.instance.MailGW(self.instance, self.db)
467 handler.trapExceptions = 0 470 handler.trapExceptions = 0
468 handler.main(message) 471 handler.main(message)
469 472
470 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 473 self.compareMessages(self._get_mail(),
471 '''FROM: roundup-admin@your.tracker.email.domain.example 474 '''FROM: roundup-admin@your.tracker.email.domain.example
472 TO: chef@bork.bork.bork 475 TO: chef@bork.bork.bork
473 Content-Type: text/plain; charset=utf-8 476 Content-Type: text/plain; charset=utf-8
474 Subject: [issue1] Testing... 477 Subject: [issue1] Testing...
475 To: chef@bork.bork.bork 478 To: chef@bork.bork.bork
513 ''') 516 ''')
514 handler = self.instance.MailGW(self.instance, self.db) 517 handler = self.instance.MailGW(self.instance, self.db)
515 handler.trapExceptions = 0 518 handler.trapExceptions = 0
516 handler.main(message) 519 handler.main(message)
517 520
518 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 521 self.compareMessages(self._get_mail(),
519 '''FROM: roundup-admin@your.tracker.email.domain.example 522 '''FROM: roundup-admin@your.tracker.email.domain.example
520 TO: chef@bork.bork.bork, john@test, richard@test 523 TO: chef@bork.bork.bork, john@test, richard@test
521 Content-Type: text/plain; charset=utf-8 524 Content-Type: text/plain; charset=utf-8
522 Subject: [issue1] Testing... 525 Subject: [issue1] Testing...
523 To: chef@bork.bork.bork, john@test, richard@test 526 To: chef@bork.bork.bork, john@test, richard@test
560 ''') 563 ''')
561 handler = self.instance.MailGW(self.instance, self.db) 564 handler = self.instance.MailGW(self.instance, self.db)
562 handler.trapExceptions = 0 565 handler.trapExceptions = 0
563 handler.main(message) 566 handler.main(message)
564 567
565 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 568 self.compareMessages(self._get_mail(),
566 '''FROM: roundup-admin@your.tracker.email.domain.example 569 '''FROM: roundup-admin@your.tracker.email.domain.example
567 TO: chef@bork.bork.bork, richard@test 570 TO: chef@bork.bork.bork, richard@test
568 Content-Type: text/plain; charset=utf-8 571 Content-Type: text/plain; charset=utf-8
569 Subject: [issue1] Testing... 572 Subject: [issue1] Testing...
570 To: chef@bork.bork.bork, richard@test 573 To: chef@bork.bork.bork, richard@test
607 ''') 610 ''')
608 handler = self.instance.MailGW(self.instance, self.db) 611 handler = self.instance.MailGW(self.instance, self.db)
609 handler.trapExceptions = 0 612 handler.trapExceptions = 0
610 handler.main(message) 613 handler.main(message)
611 614
612 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 615 self.compareMessages(self._get_mail(),
613 '''FROM: roundup-admin@your.tracker.email.domain.example 616 '''FROM: roundup-admin@your.tracker.email.domain.example
614 TO: chef@bork.bork.bork 617 TO: chef@bork.bork.bork
615 Content-Type: text/plain; charset=utf-8 618 Content-Type: text/plain; charset=utf-8
616 Subject: [issue1] Testing... 619 Subject: [issue1] Testing...
617 To: chef@bork.bork.bork 620 To: chef@bork.bork.bork
656 l = self.db.issue.get('1', 'nosy') 659 l = self.db.issue.get('1', 'nosy')
657 l.sort() 660 l.sort()
658 self.assertEqual(l, ['3', '4', '5', '6']) 661 self.assertEqual(l, ['3', '4', '5', '6'])
659 662
660 # should be no file created (ie. no message) 663 # should be no file created (ie. no message)
661 assert not os.path.exists(os.environ['SENDMAILDEBUG']) 664 assert not os.path.exists(SENDMAILDEBUG)
662 665
663 def testNosyRemove(self): 666 def testNosyRemove(self):
664 self.doNewIssue() 667 self.doNewIssue()
665 668
666 message = StringIO('''Content-Type: text/plain; 669 message = StringIO('''Content-Type: text/plain;
678 l = self.db.issue.get('1', 'nosy') 681 l = self.db.issue.get('1', 'nosy')
679 l.sort() 682 l.sort()
680 self.assertEqual(l, ['3']) 683 self.assertEqual(l, ['3'])
681 684
682 # NO NOSY MESSAGE SHOULD BE SENT! 685 # NO NOSY MESSAGE SHOULD BE SENT!
683 self.assert_(not os.path.exists(os.environ['SENDMAILDEBUG'])) 686 assert not os.path.exists(SENDMAILDEBUG)
684 687
685 def testNewUserAuthor(self): 688 def testNewUserAuthor(self):
686 # first without the permission 689 # first without the permission
687 # heh... just ignore the API for a second ;) 690 # heh... just ignore the API for a second ;)
688 self.db.security.role['anonymous'].permissions=[] 691 self.db.security.role['anonymous'].permissions=[]
737 740
738 ''') 741 ''')
739 handler = self.instance.MailGW(self.instance, self.db) 742 handler = self.instance.MailGW(self.instance, self.db)
740 handler.trapExceptions = 0 743 handler.trapExceptions = 0
741 handler.main(message) 744 handler.main(message)
742 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 745 self.compareMessages(self._get_mail(),
743 '''FROM: roundup-admin@your.tracker.email.domain.example 746 '''FROM: roundup-admin@your.tracker.email.domain.example
744 TO: chef@bork.bork.bork, richard@test 747 TO: chef@bork.bork.bork, richard@test
745 Content-Type: text/plain; charset=utf-8 748 Content-Type: text/plain; charset=utf-8
746 Subject: [issue1] Testing... 749 Subject: [issue1] Testing...
747 To: chef@bork.bork.bork, richard@test 750 To: chef@bork.bork.bork, richard@test
792 795
793 ''') 796 ''')
794 handler = self.instance.MailGW(self.instance, self.db) 797 handler = self.instance.MailGW(self.instance, self.db)
795 handler.trapExceptions = 0 798 handler.trapExceptions = 0
796 handler.main(message) 799 handler.main(message)
797 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 800 self.compareMessages(self._get_mail(),
798 '''FROM: roundup-admin@your.tracker.email.domain.example 801 '''FROM: roundup-admin@your.tracker.email.domain.example
799 TO: chef@bork.bork.bork, richard@test 802 TO: chef@bork.bork.bork, richard@test
800 Content-Type: text/plain; charset=utf-8 803 Content-Type: text/plain; charset=utf-8
801 Subject: [issue1] Testing... 804 Subject: [issue1] Testing...
802 To: chef@bork.bork.bork, richard@test 805 To: chef@bork.bork.bork, richard@test
872 ''') 875 ''')
873 handler = self.instance.MailGW(self.instance, self.db) 876 handler = self.instance.MailGW(self.instance, self.db)
874 handler.trapExceptions = 0 877 handler.trapExceptions = 0
875 handler.main(message) 878 handler.main(message)
876 879
877 self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(), 880 self.compareMessages(self._get_mail(),
878 '''FROM: roundup-admin@your.tracker.email.domain.example 881 '''FROM: roundup-admin@your.tracker.email.domain.example
879 TO: chef@bork.bork.bork 882 TO: chef@bork.bork.bork
880 Content-Type: text/plain; charset=utf-8 883 Content-Type: text/plain; charset=utf-8
881 Subject: [issue1] Testing... 884 Subject: [issue1] Testing...
882 To: chef@bork.bork.bork 885 To: chef@bork.bork.bork
992 handler.trapExceptions = 0 995 handler.trapExceptions = 0
993 handler.main(message) 996 handler.main(message)
994 997
995 self.db.user.lookup('johannes') 998 self.db.user.lookup('johannes')
996 999
997 def suite(): 1000 def test_suite():
998 l = [unittest.makeSuite(MailgwTestCase)] 1001 suite = unittest.TestSuite()
999 return unittest.TestSuite(l) 1002 suite.addTest(unittest.makeSuite(MailgwTestCase))
1000 1003 return suite
1004
1005 if __name__ == '__main__':
1006 runner = unittest.TextTestRunner()
1007 unittest.main(testRunner=runner)
1001 1008
1002 # vim: set filetype=python ts=4 sw=4 et si 1009 # vim: set filetype=python ts=4 sw=4 et si

Roundup Issue Tracker: http://roundup-tracker.org/