Code

. for python2.1 test_mailgw compareString allows an extra trailing empty line (for...
[roundup.git] / test / test_mailgw.py
1 #
2 # Copyright (c) 2001 Richard Jones, richard@bofh.asn.au.
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 #
11 # $Id: test_mailgw.py,v 1.16 2002-03-19 21:58:11 grubert Exp $
13 import unittest, cStringIO, tempfile, os, shutil, errno, imp, sys, difflib
15 from roundup.mailgw import MailGW
16 from roundup import init, instance
18 # TODO: make this output only enough equal lines for context, not all of
19 # them
20 class DiffHelper:
21     def compareStrings(self, s2, s1):
22         '''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
23            the first to be the "original" but in the calls in this file,
24            the second arg is the original. Ho hum.
25         '''
26         if s1 == s2:
27             return
28         # under python2.1 we allow a difference of one trailing empty line.
29         if sys.version_info[0:2] == (2,1):
30             if s1+'\n' == s2:
31                 return
32         
33         l1=s1.split('\n')
34         l2=s2.split('\n')
35         s = difflib.SequenceMatcher(None, l1, l2)
36         res = ['Generated message not correct (diff follows):']
37         for value, s1s, s1e, s2s, s2e in s.get_opcodes():
38             if value == 'equal':
39                 for i in range(s1s, s1e):
40                     res.append('  %s'%l1[i])
41             elif value == 'delete':
42                 for i in range(s1s, s1e):
43                     res.append('- %s'%l1[i])
44             elif value == 'insert':
45                 for i in range(s2s, s2e):
46                     res.append('+ %s'%l2[i])
47             elif value == 'replace':
48                 for i, j in zip(range(s1s, s1e), range(s2s, s2e)):
49                     res.append('- %s'%l1[i])
50                     res.append('+ %s'%l2[j])
52         raise AssertionError, '\n'.join(res)
54 class MailgwTestCase(unittest.TestCase, DiffHelper):
55     count = 0
56     schema = 'classic'
57     def setUp(self):
58         MailgwTestCase.count = MailgwTestCase.count + 1
59         self.dirname = '_test_%s'%self.count
60         try:
61             shutil.rmtree(self.dirname)
62         except OSError, error:
63             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
64         # create the instance
65         init.init(self.dirname, self.schema, 'anydbm', 'sekrit')
66         # check we can load the package
67         self.instance = instance.open(self.dirname)
68         # and open the database
69         self.db = self.instance.open('sekrit')
70         self.db.user.create(username='Chef', address='chef@bork.bork.bork')
71         self.db.user.create(username='richard', address='richard@test')
72         self.db.user.create(username='mary', address='mary@test')
73         self.db.user.create(username='john', address='john@test',
74             alternate_addresses='jondoe@test\njohn.doe@test')
76     def tearDown(self):
77         if os.path.exists(os.environ['SENDMAILDEBUG']):
78             os.remove(os.environ['SENDMAILDEBUG'])
79         try:
80             shutil.rmtree(self.dirname)
81         except OSError, error:
82             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
84     def testNewIssue(self):
85         message = cStringIO.StringIO('''Content-Type: text/plain;
86   charset="iso-8859-1"
87 From: Chef <chef@bork.bork.bork
88 To: issue_tracker@fill.me.in.
89 Cc: richard@test
90 Message-Id: <dummy_test_message_id>
91 Subject: [issue] Testing...
93 This is a test submission of a new issue.
94 ''')
95         handler = self.instance.MailGW(self.instance, self.db)
96         handler.main(message)
97         if os.path.exists(os.environ['SENDMAILDEBUG']):
98             error = open(os.environ['SENDMAILDEBUG']).read()
99             self.assertEqual('no error', error)
101     def testAlternateAddress(self):
102         message = cStringIO.StringIO('''Content-Type: text/plain;
103   charset="iso-8859-1"
104 From: John Doe <john.doe@test>
105 To: issue_tracker@fill.me.in.
106 Message-Id: <dummy_test_message_id>
107 Subject: [issue] Testing...
109 This is a test submission of a new issue.
110 ''')
111         userlist = self.db.user.list()
112         handler = self.instance.MailGW(self.instance, self.db)
113         handler.main(message)
114         if os.path.exists(os.environ['SENDMAILDEBUG']):
115             error = open(os.environ['SENDMAILDEBUG']).read()
116             self.assertEqual('no error', error)
117         self.assertEqual(userlist, self.db.user.list(),
118             "user created when it shouldn't have been")
120     def testNewIssueNoClass(self):
121         message = cStringIO.StringIO('''Content-Type: text/plain;
122   charset="iso-8859-1"
123 From: Chef <chef@bork.bork.bork
124 To: issue_tracker@fill.me.in.
125 Cc: richard@test
126 Message-Id: <dummy_test_message_id>
127 Subject: Testing...
129 This is a test submission of a new issue.
130 ''')
131         handler = self.instance.MailGW(self.instance, self.db)
132         handler.main(message)
133         if os.path.exists(os.environ['SENDMAILDEBUG']):
134             error = open(os.environ['SENDMAILDEBUG']).read()
135             self.assertEqual('no error', error)
137     def testNewIssueAuthMsg(self):
138         message = cStringIO.StringIO('''Content-Type: text/plain;
139   charset="iso-8859-1"
140 From: Chef <chef@bork.bork.bork
141 To: issue_tracker@fill.me.in.
142 Message-Id: <dummy_test_message_id>
143 Subject: [issue] Testing... [nosy=mary; assignedto=richard]
145 This is a test submission of a new issue.
146 ''')
147         handler = self.instance.MailGW(self.instance, self.db)
148         # TODO: fix the damn config - this is apalling
149         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
150         handler.main(message)
152         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
153 '''FROM: roundup-admin@fill.me.in.
154 TO: chef@bork.bork.bork, mary@test, richard@test
155 Content-Type: text/plain
156 Subject: [issue1] Testing...
157 To: chef@bork.bork.bork, mary@test, richard@test
158 From: Chef <issue_tracker@fill.me.in.>
159 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
160 MIME-Version: 1.0
161 Message-Id: <dummy_test_message_id>
162 X-Roundup-Name: Roundup issue tracker
163 Content-Transfer-Encoding: quoted-printable
166 New submission from Chef <chef@bork.bork.bork>:
168 This is a test submission of a new issue.
171 ----------
172 assignedto: richard
173 messages: 1
174 nosy: mary, Chef, richard
175 status: unread
176 title: Testing...
177 ___________________________________________________
178 "Roundup issue tracker" <issue_tracker@fill.me.in.>
179 http://some.useful.url/issue1
180 ___________________________________________________
181 ''')
183     # BUG
184     # def testMultipart(self):
185     #         '''With more than one part'''
186     #        see MultipartEnc tests: but if there is more than one part
187     #        we return a multipart/mixed and the boundary contains
188     #        the ip address of the test machine. 
190     # BUG should test some binary attamchent too.
192     def testFollowup(self):
193         self.testNewIssue()
194         message = cStringIO.StringIO('''Content-Type: text/plain;
195   charset="iso-8859-1"
196 From: richard <richard@test>
197 To: issue_tracker@fill.me.in.
198 Message-Id: <followup_dummy_id>
199 In-Reply-To: <dummy_test_message_id>
200 Subject: [issue1] Testing... [assignedto=mary; nosy=john]
202 This is a followup
203 ''')
204         handler = self.instance.MailGW(self.instance, self.db)
205         handler.main(message)
207         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
208 '''FROM: roundup-admin@fill.me.in.
209 TO: chef@bork.bork.bork, mary@test, john@test
210 Content-Type: text/plain
211 Subject: [issue1] Testing...
212 To: chef@bork.bork.bork, mary@test, john@test
213 From: richard <issue_tracker@fill.me.in.>
214 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
215 MIME-Version: 1.0
216 Message-Id: <followup_dummy_id>
217 In-Reply-To: <dummy_test_message_id>
218 X-Roundup-Name: Roundup issue tracker
219 Content-Transfer-Encoding: quoted-printable
222 richard <richard@test> added the comment:
224 This is a followup
227 ----------
228 assignedto:  -> mary
229 nosy: +mary, john
230 status: unread -> chatting
231 ___________________________________________________
232 "Roundup issue tracker" <issue_tracker@fill.me.in.>
233 http://some.useful.url/issue1
234 ___________________________________________________
235 ''')
237     def testFollowup2(self):
238         self.testNewIssue()
239         message = cStringIO.StringIO('''Content-Type: text/plain;
240   charset="iso-8859-1"
241 From: mary <mary@test>
242 To: issue_tracker@fill.me.in.
243 Message-Id: <followup_dummy_id>
244 In-Reply-To: <dummy_test_message_id>
245 Subject: [issue1] Testing...
247 This is a second followup
248 ''')
249         handler = self.instance.MailGW(self.instance, self.db)
250         handler.main(message)
251         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
252 '''FROM: roundup-admin@fill.me.in.
253 TO: chef@bork.bork.bork, richard@test
254 Content-Type: text/plain
255 Subject: [issue1] Testing...
256 To: chef@bork.bork.bork, richard@test
257 From: mary <issue_tracker@fill.me.in.>
258 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
259 MIME-Version: 1.0
260 Message-Id: <followup_dummy_id>
261 In-Reply-To: <dummy_test_message_id>
262 X-Roundup-Name: Roundup issue tracker
263 Content-Transfer-Encoding: quoted-printable
266 mary <mary@test> added the comment:
268 This is a second followup
271 ----------
272 status: unread -> chatting
273 ___________________________________________________
274 "Roundup issue tracker" <issue_tracker@fill.me.in.>
275 http://some.useful.url/issue1
276 ___________________________________________________
277 ''')
279     def testFollowupTitleMatch(self):
280         self.testNewIssue()
281         message = cStringIO.StringIO('''Content-Type: text/plain;
282   charset="iso-8859-1"
283 From: richard <richard@test>
284 To: issue_tracker@fill.me.in.
285 Message-Id: <followup_dummy_id>
286 In-Reply-To: <dummy_test_message_id>
287 Subject: Re: Testing... [assignedto=mary; nosy=john]
289 This is a followup
290 ''')
291         handler = self.instance.MailGW(self.instance, self.db)
292         handler.main(message)
294         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
295 '''FROM: roundup-admin@fill.me.in.
296 TO: chef@bork.bork.bork, mary@test, john@test
297 Content-Type: text/plain
298 Subject: [issue1] Testing...
299 To: chef@bork.bork.bork, mary@test, john@test
300 From: richard <issue_tracker@fill.me.in.>
301 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
302 MIME-Version: 1.0
303 Message-Id: <followup_dummy_id>
304 In-Reply-To: <dummy_test_message_id>
305 X-Roundup-Name: Roundup issue tracker
306 Content-Transfer-Encoding: quoted-printable
309 richard <richard@test> added the comment:
311 This is a followup
314 ----------
315 assignedto:  -> mary
316 nosy: +mary, john
317 status: unread -> chatting
318 ___________________________________________________
319 "Roundup issue tracker" <issue_tracker@fill.me.in.>
320 http://some.useful.url/issue1
321 ___________________________________________________
322 ''')
324     def testEnc01(self):
325         self.testNewIssue()
326         message = cStringIO.StringIO('''Content-Type: text/plain;
327   charset="iso-8859-1"
328 From: mary <mary@test>
329 To: issue_tracker@fill.me.in.
330 Message-Id: <followup_dummy_id>
331 In-Reply-To: <dummy_test_message_id>
332 Subject: [issue1] Testing...
333 Content-Type: text/plain;
334         charset="iso-8859-1"
335 Content-Transfer-Encoding: quoted-printable
337 A message with encoding (encoded oe =F6)
339 ''')
340         handler = self.instance.MailGW(self.instance, self.db)
341         handler.main(message)
342         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
343 '''FROM: roundup-admin@fill.me.in.
344 TO: chef@bork.bork.bork, richard@test
345 Content-Type: text/plain
346 Subject: [issue1] Testing...
347 To: chef@bork.bork.bork, richard@test
348 From: mary <issue_tracker@fill.me.in.>
349 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
350 MIME-Version: 1.0
351 Message-Id: <followup_dummy_id>
352 In-Reply-To: <dummy_test_message_id>
353 X-Roundup-Name: Roundup issue tracker
354 Content-Transfer-Encoding: quoted-printable
357 mary <mary@test> added the comment:
359 A message with encoding (encoded oe =F6)
361 ----------
362 status: unread -> chatting
363 ___________________________________________________
364 "Roundup issue tracker" <issue_tracker@fill.me.in.>
365 http://some.useful.url/issue1
366 ___________________________________________________
367 ''')
370     def testMultipartEnc01(self):
371         self.testNewIssue()
372         message = cStringIO.StringIO('''Content-Type: text/plain;
373   charset="iso-8859-1"
374 From: mary <mary@test>
375 To: issue_tracker@fill.me.in.
376 Message-Id: <followup_dummy_id>
377 In-Reply-To: <dummy_test_message_id>
378 Subject: [issue1] Testing...
379 Content-Type: multipart/mixed;
380         boundary="----_=_NextPart_000_01"
382 This message is in MIME format. Since your mail reader does not understand
383 this format, some or all of this message may not be legible.
385 ------_=_NextPart_000_01
386 Content-Type: text/plain;
387         charset="iso-8859-1"
388 Content-Transfer-Encoding: quoted-printable
390 A message with first part encoded (encoded oe =F6)
392 ''')
393         handler = self.instance.MailGW(self.instance, self.db)
394         handler.main(message)
395         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
396 '''FROM: roundup-admin@fill.me.in.
397 TO: chef@bork.bork.bork, richard@test
398 Content-Type: text/plain
399 Subject: [issue1] Testing...
400 To: chef@bork.bork.bork, richard@test
401 From: mary <issue_tracker@fill.me.in.>
402 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
403 MIME-Version: 1.0
404 Message-Id: <followup_dummy_id>
405 In-Reply-To: <dummy_test_message_id>
406 X-Roundup-Name: Roundup issue tracker
407 Content-Transfer-Encoding: quoted-printable
410 mary <mary@test> added the comment:
412 A message with first part encoded (encoded oe =F6)
414 ----------
415 status: unread -> chatting
416 ___________________________________________________
417 "Roundup issue tracker" <issue_tracker@fill.me.in.>
418 http://some.useful.url/issue1
419 ___________________________________________________
420 ''')
422 class ExtMailgwTestCase(MailgwTestCase):
423     schema = 'extended'
425 def suite():
426     l = [unittest.makeSuite(MailgwTestCase, 'test'),
427 #         unittest.makeSuite(ExtMailgwTestCase, 'test')
428     ]
429     return unittest.TestSuite(l)
433 # $Log: not supported by cvs2svn $
434 # Revision 1.15  2002/03/19 06:37:00  richard
435 # Made the email checking spit out a diff - much easier to spot the problem!
437 # Revision 1.14  2002/03/18 18:32:00  rochecompaan
438 # All messages sent to the nosy list are now encoded as quoted-printable.
440 # Revision 1.13  2002/02/15 07:08:45  richard
441 #  . Alternate email addresses are now available for users. See the MIGRATION
442 #    file for info on how to activate the feature.
444 # Revision 1.12  2002/02/15 00:13:38  richard
445 #  . #503204 ] mailgw needs a default class
446 #     - partially done - the setting of additional properties can wait for a
447 #       better configuration system.
449 # Revision 1.11  2002/02/14 23:38:12  richard
450 # Fixed the unit tests for the mailgw re: the x-roundup-name header.
451 # Also made the test runner more user-friendly:
452 #   ./run_tests            - detect all tests in test/test_<name>.py and run them
453 #   ./run_tests <name>     - run only test/test_<name>.py
454 # eg ./run_tests mailgw    - run the mailgw test from test/test_mailgw.py
456 # Revision 1.10  2002/02/12 08:08:55  grubert
457 #  . Clean up mail handling, multipart handling.
459 # Revision 1.9  2002/02/05 14:15:29  grubert
460 #  . respect encodings in non multipart messages.
462 # Revision 1.8  2002/02/04 09:40:21  grubert
463 #  . add test for multipart messages with first part being encoded.
465 # Revision 1.7  2002/01/22 11:54:45  rochecompaan
466 # Fixed status change in mail gateway.
468 # Revision 1.6  2002/01/21 10:05:48  rochecompaan
469 # Feature:
470 #  . the mail gateway now responds with an error message when invalid
471 #    values for arguments are specified for link or multilink properties
472 #  . modified unit test to check nosy and assignedto when specified as
473 #    arguments
475 # Fixed:
476 #  . fixed setting nosy as argument in subject line
478 # Revision 1.5  2002/01/15 00:12:40  richard
479 # #503340 ] creating issue with [asignedto=p.ohly]
481 # Revision 1.4  2002/01/14 07:12:15  richard
482 # removed file writing from tests...
484 # Revision 1.3  2002/01/14 02:20:15  richard
485 #  . changed all config accesses so they access either the instance or the
486 #    config attriubute on the db. This means that all config is obtained from
487 #    instance_config instead of the mish-mash of classes. This will make
488 #    switching to a ConfigParser setup easier too, I hope.
490 # At a minimum, this makes migration a _little_ easier (a lot easier in the
491 # 0.5.0 switch, I hope!)
493 # Revision 1.2  2002/01/11 23:22:29  richard
494 #  . #502437 ] rogue reactor and unittest
495 #    in short, the nosy reactor was modifying the nosy list. That code had
496 #    been there for a long time, and I suspsect it was there because we
497 #    weren't generating the nosy list correctly in other places of the code.
498 #    We're now doing that, so the nosy-modifying code can go away from the
499 #    nosy reactor.
501 # Revision 1.1  2002/01/02 02:31:38  richard
502 # Sorry for the huge checkin message - I was only intending to implement #496356
503 # but I found a number of places where things had been broken by transactions:
504 #  . modified ROUNDUPDBSENDMAILDEBUG to be SENDMAILDEBUG and hold a filename
505 #    for _all_ roundup-generated smtp messages to be sent to.
506 #  . the transaction cache had broken the roundupdb.Class set() reactors
507 #  . newly-created author users in the mailgw weren't being committed to the db
509 # Stuff that made it into CHANGES.txt (ie. the stuff I was actually working
510 # on when I found that stuff :):
511 #  . #496356 ] Use threading in messages
512 #  . detectors were being registered multiple times
513 #  . added tests for mailgw
514 #  . much better attaching of erroneous messages in the mail gateway
519 # vim: set filetype=python ts=4 sw=4 et si