Code

All messages sent to the nosy list are now encoded as quoted-printable.
[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.14 2002-03-18 18:32:00 rochecompaan Exp $
13 import unittest, cStringIO, tempfile, os, shutil, errno, imp, sys
15 from roundup.mailgw import MailGW
16 from roundup import init, instance
18 class MailgwTestCase(unittest.TestCase):
19     count = 0
20     schema = 'classic'
21     def setUp(self):
22         MailgwTestCase.count = MailgwTestCase.count + 1
23         self.dirname = '_test_%s'%self.count
24         try:
25             shutil.rmtree(self.dirname)
26         except OSError, error:
27             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
28         # create the instance
29         init.init(self.dirname, self.schema, 'anydbm', 'sekrit')
30         # check we can load the package
31         self.instance = instance.open(self.dirname)
32         # and open the database
33         self.db = self.instance.open('sekrit')
34         self.db.user.create(username='Chef', address='chef@bork.bork.bork')
35         self.db.user.create(username='richard', address='richard@test')
36         self.db.user.create(username='mary', address='mary@test')
37         self.db.user.create(username='john', address='john@test',
38             alternate_addresses='jondoe@test\njohn.doe@test')
40     def tearDown(self):
41         if os.path.exists(os.environ['SENDMAILDEBUG']):
42             os.remove(os.environ['SENDMAILDEBUG'])
43         try:
44             shutil.rmtree(self.dirname)
45         except OSError, error:
46             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
48     def testNewIssue(self):
49         message = cStringIO.StringIO('''Content-Type: text/plain;
50   charset="iso-8859-1"
51 From: Chef <chef@bork.bork.bork
52 To: issue_tracker@fill.me.in.
53 Cc: richard@test
54 Message-Id: <dummy_test_message_id>
55 Subject: [issue] Testing...
57 This is a test submission of a new issue.
58 ''')
59         handler = self.instance.MailGW(self.instance, self.db)
60         handler.main(message)
61         if os.path.exists(os.environ['SENDMAILDEBUG']):
62             error = open(os.environ['SENDMAILDEBUG']).read()
63             self.assertEqual('no error', error)
65     def testAlternateAddress(self):
66         message = cStringIO.StringIO('''Content-Type: text/plain;
67   charset="iso-8859-1"
68 From: John Doe <john.doe@test>
69 To: issue_tracker@fill.me.in.
70 Message-Id: <dummy_test_message_id>
71 Subject: [issue] Testing...
73 This is a test submission of a new issue.
74 ''')
75         userlist = self.db.user.list()
76         handler = self.instance.MailGW(self.instance, self.db)
77         handler.main(message)
78         if os.path.exists(os.environ['SENDMAILDEBUG']):
79             error = open(os.environ['SENDMAILDEBUG']).read()
80             self.assertEqual('no error', error)
81         self.assertEqual(userlist, self.db.user.list(),
82             "user created when it shouldn't have been")
84     def testNewIssueNoClass(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: 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 testNewIssueAuthMsg(self):
102         message = cStringIO.StringIO('''Content-Type: text/plain;
103   charset="iso-8859-1"
104 From: Chef <chef@bork.bork.bork
105 To: issue_tracker@fill.me.in.
106 Message-Id: <dummy_test_message_id>
107 Subject: [issue] Testing... [nosy=mary; assignedto=richard]
109 This is a test submission of a new issue.
110 ''')
111         handler = self.instance.MailGW(self.instance, self.db)
112         # TODO: fix the damn config - this is apalling
113         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
114         handler.main(message)
116         self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
117 '''FROM: roundup-admin@fill.me.in.
118 TO: chef@bork.bork.bork, mary@test, richard@test
119 Content-Type: text/plain
120 Subject: [issue1] Testing...
121 To: chef@bork.bork.bork, mary@test, richard@test
122 From: Chef <issue_tracker@fill.me.in.>
123 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
124 MIME-Version: 1.0
125 Message-Id: <dummy_test_message_id>
126 X-Roundup-Name: Roundup issue tracker
127 Content-Transfer-Encoding: quoted-printable
130 New submission from Chef <chef@bork.bork.bork>:
132 This is a test submission of a new issue.
135 ----------
136 assignedto: richard
137 messages: 1
138 nosy: mary, Chef, richard
139 status: unread
140 title: Testing...
141 ___________________________________________________
142 "Roundup issue tracker" <issue_tracker@fill.me.in.>
143 http://some.useful.url/issue1
144 ___________________________________________________
145 ''', 'Generated message not correct')
147     # BUG
148     # def testMultipart(self):
149     #         '''With more than one part'''
150     #        see MultipartEnc tests: but if there is more than one part
151     #        we return a multipart/mixed and the boundary contains
152     #        the ip address of the test machine. 
154     # BUG should test some binary attamchent too.
156     def testFollowup(self):
157         self.testNewIssue()
158         message = cStringIO.StringIO('''Content-Type: text/plain;
159   charset="iso-8859-1"
160 From: richard <richard@test>
161 To: issue_tracker@fill.me.in.
162 Message-Id: <followup_dummy_id>
163 In-Reply-To: <dummy_test_message_id>
164 Subject: [issue1] Testing... [assignedto=mary; nosy=john]
166 This is a followup
167 ''')
168         handler = self.instance.MailGW(self.instance, self.db)
169         handler.main(message)
171         self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
172 '''FROM: roundup-admin@fill.me.in.
173 TO: chef@bork.bork.bork, mary@test, john@test
174 Content-Type: text/plain
175 Subject: [issue1] Testing...
176 To: chef@bork.bork.bork, mary@test, john@test
177 From: richard <issue_tracker@fill.me.in.>
178 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
179 MIME-Version: 1.0
180 Message-Id: <followup_dummy_id>
181 In-Reply-To: <dummy_test_message_id>
182 X-Roundup-Name: Roundup issue tracker
183 Content-Transfer-Encoding: quoted-printable
186 richard <richard@test> added the comment:
188 This is a followup
191 ----------
192 assignedto:  -> mary
193 nosy: +mary, john
194 status: unread -> chatting
195 ___________________________________________________
196 "Roundup issue tracker" <issue_tracker@fill.me.in.>
197 http://some.useful.url/issue1
198 ___________________________________________________
199 ''', 'Generated message not correct')
201     def testFollowup2(self):
202         self.testNewIssue()
203         message = cStringIO.StringIO('''Content-Type: text/plain;
204   charset="iso-8859-1"
205 From: mary <mary@test>
206 To: issue_tracker@fill.me.in.
207 Message-Id: <followup_dummy_id>
208 In-Reply-To: <dummy_test_message_id>
209 Subject: [issue1] Testing...
211 This is a second followup
212 ''')
213         handler = self.instance.MailGW(self.instance, self.db)
214         handler.main(message)
215         self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
216 '''FROM: roundup-admin@fill.me.in.
217 TO: chef@bork.bork.bork, richard@test
218 Content-Type: text/plain
219 Subject: [issue1] Testing...
220 To: chef@bork.bork.bork, richard@test
221 From: mary <issue_tracker@fill.me.in.>
222 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
223 MIME-Version: 1.0
224 Message-Id: <followup_dummy_id>
225 In-Reply-To: <dummy_test_message_id>
226 X-Roundup-Name: Roundup issue tracker
227 Content-Transfer-Encoding: quoted-printable
230 mary <mary@test> added the comment:
232 This is a second followup
235 ----------
236 status: unread -> chatting
237 ___________________________________________________
238 "Roundup issue tracker" <issue_tracker@fill.me.in.>
239 http://some.useful.url/issue1
240 ___________________________________________________
241 ''', 'Generated message not correct')
243     def testFollowupTitleMatch(self):
244         self.testNewIssue()
245         message = cStringIO.StringIO('''Content-Type: text/plain;
246   charset="iso-8859-1"
247 From: richard <richard@test>
248 To: issue_tracker@fill.me.in.
249 Message-Id: <followup_dummy_id>
250 In-Reply-To: <dummy_test_message_id>
251 Subject: Re: Testing... [assignedto=mary; nosy=john]
253 This is a followup
254 ''')
255         handler = self.instance.MailGW(self.instance, self.db)
256         handler.main(message)
258         self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
259 '''FROM: roundup-admin@fill.me.in.
260 TO: chef@bork.bork.bork, mary@test, john@test
261 Content-Type: text/plain
262 Subject: [issue1] Testing...
263 To: chef@bork.bork.bork, mary@test, john@test
264 From: richard <issue_tracker@fill.me.in.>
265 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
266 MIME-Version: 1.0
267 Message-Id: <followup_dummy_id>
268 In-Reply-To: <dummy_test_message_id>
269 X-Roundup-Name: Roundup issue tracker
270 Content-Transfer-Encoding: quoted-printable
273 richard <richard@test> added the comment:
275 This is a followup
278 ----------
279 assignedto:  -> mary
280 nosy: +mary, john
281 status: unread -> chatting
282 ___________________________________________________
283 "Roundup issue tracker" <issue_tracker@fill.me.in.>
284 http://some.useful.url/issue1
285 ___________________________________________________
286 ''') #, 'Generated message not correct')
288     def testEnc01(self):
289         self.testNewIssue()
290         message = cStringIO.StringIO('''Content-Type: text/plain;
291   charset="iso-8859-1"
292 From: mary <mary@test>
293 To: issue_tracker@fill.me.in.
294 Message-Id: <followup_dummy_id>
295 In-Reply-To: <dummy_test_message_id>
296 Subject: [issue1] Testing...
297 Content-Type: text/plain;
298         charset="iso-8859-1"
299 Content-Transfer-Encoding: quoted-printable
301 A message with encoding (encoded oe =F6)
303 ''')
304         handler = self.instance.MailGW(self.instance, self.db)
305         handler.main(message)
306         message_data = open(os.environ['SENDMAILDEBUG']).read()
307         self.assertEqual(message_data,
308 '''FROM: roundup-admin@fill.me.in.
309 TO: chef@bork.bork.bork, richard@test
310 Content-Type: text/plain
311 Subject: [issue1] Testing...
312 To: chef@bork.bork.bork, richard@test
313 From: mary <issue_tracker@fill.me.in.>
314 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
315 MIME-Version: 1.0
316 Message-Id: <followup_dummy_id>
317 In-Reply-To: <dummy_test_message_id>
318 X-Roundup-Name: Roundup issue tracker
319 Content-Transfer-Encoding: quoted-printable
322 mary <mary@test> added the comment:
324 A message with encoding (encoded oe =F6)
326 ----------
327 status: unread -> chatting
328 ___________________________________________________
329 "Roundup issue tracker" <issue_tracker@fill.me.in.>
330 http://some.useful.url/issue1
331 ___________________________________________________
332 ''', 'Generated message not correct')
335     def testMultipartEnc01(self):
336         self.testNewIssue()
337         message = cStringIO.StringIO('''Content-Type: text/plain;
338   charset="iso-8859-1"
339 From: mary <mary@test>
340 To: issue_tracker@fill.me.in.
341 Message-Id: <followup_dummy_id>
342 In-Reply-To: <dummy_test_message_id>
343 Subject: [issue1] Testing...
344 Content-Type: multipart/mixed;
345         boundary="----_=_NextPart_000_01"
347 This message is in MIME format. Since your mail reader does not understand
348 this format, some or all of this message may not be legible.
350 ------_=_NextPart_000_01
351 Content-Type: text/plain;
352         charset="iso-8859-1"
353 Content-Transfer-Encoding: quoted-printable
355 A message with first part encoded (encoded oe =F6)
357 ''')
358         handler = self.instance.MailGW(self.instance, self.db)
359         handler.main(message)
360         message_data = open(os.environ['SENDMAILDEBUG']).read()
361         self.assertEqual(message_data,
362 '''FROM: roundup-admin@fill.me.in.
363 TO: chef@bork.bork.bork, richard@test
364 Content-Type: text/plain
365 Subject: [issue1] Testing...
366 To: chef@bork.bork.bork, richard@test
367 From: mary <issue_tracker@fill.me.in.>
368 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
369 MIME-Version: 1.0
370 Message-Id: <followup_dummy_id>
371 In-Reply-To: <dummy_test_message_id>
372 X-Roundup-Name: Roundup issue tracker
373 Content-Transfer-Encoding: quoted-printable
376 mary <mary@test> added the comment:
378 A message with first part encoded (encoded oe =F6)
380 ----------
381 status: unread -> chatting
382 ___________________________________________________
383 "Roundup issue tracker" <issue_tracker@fill.me.in.>
384 http://some.useful.url/issue1
385 ___________________________________________________
386 ''', 'Generated message not correct')
388 class ExtMailgwTestCase(MailgwTestCase):
389     schema = 'extended'
391 def suite():
392     l = [unittest.makeSuite(MailgwTestCase, 'test'),
393          unittest.makeSuite(ExtMailgwTestCase, 'test')
394     ]
395     return unittest.TestSuite(l)
399 # $Log: not supported by cvs2svn $
400 # Revision 1.13  2002/02/15 07:08:45  richard
401 #  . Alternate email addresses are now available for users. See the MIGRATION
402 #    file for info on how to activate the feature.
404 # Revision 1.12  2002/02/15 00:13:38  richard
405 #  . #503204 ] mailgw needs a default class
406 #     - partially done - the setting of additional properties can wait for a
407 #       better configuration system.
409 # Revision 1.11  2002/02/14 23:38:12  richard
410 # Fixed the unit tests for the mailgw re: the x-roundup-name header.
411 # Also made the test runner more user-friendly:
412 #   ./run_tests            - detect all tests in test/test_<name>.py and run them
413 #   ./run_tests <name>     - run only test/test_<name>.py
414 # eg ./run_tests mailgw    - run the mailgw test from test/test_mailgw.py
416 # Revision 1.10  2002/02/12 08:08:55  grubert
417 #  . Clean up mail handling, multipart handling.
419 # Revision 1.9  2002/02/05 14:15:29  grubert
420 #  . respect encodings in non multipart messages.
422 # Revision 1.8  2002/02/04 09:40:21  grubert
423 #  . add test for multipart messages with first part being encoded.
425 # Revision 1.7  2002/01/22 11:54:45  rochecompaan
426 # Fixed status change in mail gateway.
428 # Revision 1.6  2002/01/21 10:05:48  rochecompaan
429 # Feature:
430 #  . the mail gateway now responds with an error message when invalid
431 #    values for arguments are specified for link or multilink properties
432 #  . modified unit test to check nosy and assignedto when specified as
433 #    arguments
435 # Fixed:
436 #  . fixed setting nosy as argument in subject line
438 # Revision 1.5  2002/01/15 00:12:40  richard
439 # #503340 ] creating issue with [asignedto=p.ohly]
441 # Revision 1.4  2002/01/14 07:12:15  richard
442 # removed file writing from tests...
444 # Revision 1.3  2002/01/14 02:20:15  richard
445 #  . changed all config accesses so they access either the instance or the
446 #    config attriubute on the db. This means that all config is obtained from
447 #    instance_config instead of the mish-mash of classes. This will make
448 #    switching to a ConfigParser setup easier too, I hope.
450 # At a minimum, this makes migration a _little_ easier (a lot easier in the
451 # 0.5.0 switch, I hope!)
453 # Revision 1.2  2002/01/11 23:22:29  richard
454 #  . #502437 ] rogue reactor and unittest
455 #    in short, the nosy reactor was modifying the nosy list. That code had
456 #    been there for a long time, and I suspsect it was there because we
457 #    weren't generating the nosy list correctly in other places of the code.
458 #    We're now doing that, so the nosy-modifying code can go away from the
459 #    nosy reactor.
461 # Revision 1.1  2002/01/02 02:31:38  richard
462 # Sorry for the huge checkin message - I was only intending to implement #496356
463 # but I found a number of places where things had been broken by transactions:
464 #  . modified ROUNDUPDBSENDMAILDEBUG to be SENDMAILDEBUG and hold a filename
465 #    for _all_ roundup-generated smtp messages to be sent to.
466 #  . the transaction cache had broken the roundupdb.Class set() reactors
467 #  . newly-created author users in the mailgw weren't being committed to the db
469 # Stuff that made it into CHANGES.txt (ie. the stuff I was actually working
470 # on when I found that stuff :):
471 #  . #496356 ] Use threading in messages
472 #  . detectors were being registered multiple times
473 #  . added tests for mailgw
474 #  . much better attaching of erroneous messages in the mail gateway
479 # vim: set filetype=python ts=4 sw=4 et si