Code

. Alternate email addresses are now available for users. See the MIGRATION
[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.13 2002-02-15 07:08:45 richard 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 xtestNewIssue(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 xtestNewIssueNoClass(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 xtestNewIssueAuthMsg(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
129 New submission from Chef <chef@bork.bork.bork>:
131 This is a test submission of a new issue.
134 ----------
135 assignedto: richard
136 messages: 1
137 nosy: mary, Chef, richard
138 status: unread
139 title: Testing...
140 ___________________________________________________
141 "Roundup issue tracker" <issue_tracker@fill.me.in.>
142 http://some.useful.url/issue1
143 ___________________________________________________
144 ''', 'Generated message not correct')
146     # BUG
147     # def testMultipart(self):
148     #         '''With more than one part'''
149     #        see MultipartEnc tests: but if there is more than one part
150     #        we return a multipart/mixed and the boundary contains
151     #        the ip address of the test machine. 
153     # BUG should test some binary attamchent too.
155     def xtestFollowup(self):
156         self.testNewIssue()
157         message = cStringIO.StringIO('''Content-Type: text/plain;
158   charset="iso-8859-1"
159 From: richard <richard@test>
160 To: issue_tracker@fill.me.in.
161 Message-Id: <followup_dummy_id>
162 In-Reply-To: <dummy_test_message_id>
163 Subject: [issue1] Testing... [assignedto=mary; nosy=john]
165 This is a followup
166 ''')
167         handler = self.instance.MailGW(self.instance, self.db)
168         handler.main(message)
170         self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
171 '''FROM: roundup-admin@fill.me.in.
172 TO: chef@bork.bork.bork, mary@test, john@test
173 Content-Type: text/plain
174 Subject: [issue1] Testing...
175 To: chef@bork.bork.bork, mary@test, john@test
176 From: richard <issue_tracker@fill.me.in.>
177 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
178 MIME-Version: 1.0
179 Message-Id: <followup_dummy_id>
180 In-Reply-To: <dummy_test_message_id>
181 X-Roundup-Name: Roundup issue tracker
184 richard <richard@test> added the comment:
186 This is a followup
189 ----------
190 assignedto:  -> mary
191 nosy: +mary, john
192 status: unread -> chatting
193 ___________________________________________________
194 "Roundup issue tracker" <issue_tracker@fill.me.in.>
195 http://some.useful.url/issue1
196 ___________________________________________________
197 ''', 'Generated message not correct')
199     def xtestFollowup2(self):
200         self.testNewIssue()
201         message = cStringIO.StringIO('''Content-Type: text/plain;
202   charset="iso-8859-1"
203 From: mary <mary@test>
204 To: issue_tracker@fill.me.in.
205 Message-Id: <followup_dummy_id>
206 In-Reply-To: <dummy_test_message_id>
207 Subject: [issue1] Testing...
209 This is a second followup
210 ''')
211         handler = self.instance.MailGW(self.instance, self.db)
212         handler.main(message)
213         self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
214 '''FROM: roundup-admin@fill.me.in.
215 TO: chef@bork.bork.bork, richard@test
216 Content-Type: text/plain
217 Subject: [issue1] Testing...
218 To: chef@bork.bork.bork, richard@test
219 From: mary <issue_tracker@fill.me.in.>
220 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
221 MIME-Version: 1.0
222 Message-Id: <followup_dummy_id>
223 In-Reply-To: <dummy_test_message_id>
224 X-Roundup-Name: Roundup issue tracker
227 mary <mary@test> added the comment:
229 This is a second followup
232 ----------
233 status: unread -> chatting
234 ___________________________________________________
235 "Roundup issue tracker" <issue_tracker@fill.me.in.>
236 http://some.useful.url/issue1
237 ___________________________________________________
238 ''', 'Generated message not correct')
240     def xtestFollowupTitleMatch(self):
241         self.testNewIssue()
242         message = cStringIO.StringIO('''Content-Type: text/plain;
243   charset="iso-8859-1"
244 From: richard <richard@test>
245 To: issue_tracker@fill.me.in.
246 Message-Id: <followup_dummy_id>
247 In-Reply-To: <dummy_test_message_id>
248 Subject: Re: Testing... [assignedto=mary; nosy=john]
250 This is a followup
251 ''')
252         handler = self.instance.MailGW(self.instance, self.db)
253         handler.main(message)
255         self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
256 '''FROM: roundup-admin@fill.me.in.
257 TO: chef@bork.bork.bork, mary@test, john@test
258 Content-Type: text/plain
259 Subject: [issue1] Testing...
260 To: chef@bork.bork.bork, mary@test, john@test
261 From: richard <issue_tracker@fill.me.in.>
262 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
263 MIME-Version: 1.0
264 Message-Id: <followup_dummy_id>
265 In-Reply-To: <dummy_test_message_id>
266 X-Roundup-Name: Roundup issue tracker
269 richard <richard@test> added the comment:
271 This is a followup
274 ----------
275 assignedto:  -> mary
276 nosy: +mary, john
277 status: unread -> chatting
278 ___________________________________________________
279 "Roundup issue tracker" <issue_tracker@fill.me.in.>
280 http://some.useful.url/issue1
281 ___________________________________________________
282 ''') #, 'Generated message not correct')
284     def xtestEnc01(self):
285         self.testNewIssue()
286         message = cStringIO.StringIO('''Content-Type: text/plain;
287   charset="iso-8859-1"
288 From: mary <mary@test>
289 To: issue_tracker@fill.me.in.
290 Message-Id: <followup_dummy_id>
291 In-Reply-To: <dummy_test_message_id>
292 Subject: [issue1] Testing...
293 Content-Type: text/plain;
294         charset="iso-8859-1"
295 Content-Transfer-Encoding: quoted-printable
297 A message with encoding (encoded oe =F6)
299 ''')
300         handler = self.instance.MailGW(self.instance, self.db)
301         handler.main(message)
302         message_data = open(os.environ['SENDMAILDEBUG']).read()
303         self.assertEqual(message_data,
304 '''FROM: roundup-admin@fill.me.in.
305 TO: chef@bork.bork.bork, richard@test
306 Content-Type: text/plain
307 Subject: [issue1] Testing...
308 To: chef@bork.bork.bork, richard@test
309 From: mary <issue_tracker@fill.me.in.>
310 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
311 MIME-Version: 1.0
312 Message-Id: <followup_dummy_id>
313 In-Reply-To: <dummy_test_message_id>
314 X-Roundup-Name: Roundup issue tracker
317 mary <mary@test> added the comment:
319 A message with encoding (encoded oe ö)
321 ----------
322 status: unread -> chatting
323 ___________________________________________________
324 "Roundup issue tracker" <issue_tracker@fill.me.in.>
325 http://some.useful.url/issue1
326 ___________________________________________________
327 ''', 'Generated message not correct')
330     def xtestMultipartEnc01(self):
331         self.testNewIssue()
332         message = cStringIO.StringIO('''Content-Type: text/plain;
333   charset="iso-8859-1"
334 From: mary <mary@test>
335 To: issue_tracker@fill.me.in.
336 Message-Id: <followup_dummy_id>
337 In-Reply-To: <dummy_test_message_id>
338 Subject: [issue1] Testing...
339 Content-Type: multipart/mixed;
340         boundary="----_=_NextPart_000_01"
342 This message is in MIME format. Since your mail reader does not understand
343 this format, some or all of this message may not be legible.
345 ------_=_NextPart_000_01
346 Content-Type: text/plain;
347         charset="iso-8859-1"
348 Content-Transfer-Encoding: quoted-printable
350 A message with first part encoded (encoded oe =F6)
352 ''')
353         handler = self.instance.MailGW(self.instance, self.db)
354         handler.main(message)
355         message_data = open(os.environ['SENDMAILDEBUG']).read()
356         self.assertEqual(message_data,
357 '''FROM: roundup-admin@fill.me.in.
358 TO: chef@bork.bork.bork, richard@test
359 Content-Type: text/plain
360 Subject: [issue1] Testing...
361 To: chef@bork.bork.bork, richard@test
362 From: mary <issue_tracker@fill.me.in.>
363 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
364 MIME-Version: 1.0
365 Message-Id: <followup_dummy_id>
366 In-Reply-To: <dummy_test_message_id>
367 X-Roundup-Name: Roundup issue tracker
370 mary <mary@test> added the comment:
372 A message with first part encoded (encoded oe ö)
374 ----------
375 status: unread -> chatting
376 ___________________________________________________
377 "Roundup issue tracker" <issue_tracker@fill.me.in.>
378 http://some.useful.url/issue1
379 ___________________________________________________
380 ''', 'Generated message not correct')
382 class ExtMailgwTestCase(MailgwTestCase):
383     schema = 'extended'
385 def suite():
386     l = [unittest.makeSuite(MailgwTestCase, 'test'),
387          unittest.makeSuite(ExtMailgwTestCase, 'test')
388     ]
389     return unittest.TestSuite(l)
393 # $Log: not supported by cvs2svn $
394 # Revision 1.12  2002/02/15 00:13:38  richard
395 #  . #503204 ] mailgw needs a default class
396 #     - partially done - the setting of additional properties can wait for a
397 #       better configuration system.
399 # Revision 1.11  2002/02/14 23:38:12  richard
400 # Fixed the unit tests for the mailgw re: the x-roundup-name header.
401 # Also made the test runner more user-friendly:
402 #   ./run_tests            - detect all tests in test/test_<name>.py and run them
403 #   ./run_tests <name>     - run only test/test_<name>.py
404 # eg ./run_tests mailgw    - run the mailgw test from test/test_mailgw.py
406 # Revision 1.10  2002/02/12 08:08:55  grubert
407 #  . Clean up mail handling, multipart handling.
409 # Revision 1.9  2002/02/05 14:15:29  grubert
410 #  . respect encodings in non multipart messages.
412 # Revision 1.8  2002/02/04 09:40:21  grubert
413 #  . add test for multipart messages with first part being encoded.
415 # Revision 1.7  2002/01/22 11:54:45  rochecompaan
416 # Fixed status change in mail gateway.
418 # Revision 1.6  2002/01/21 10:05:48  rochecompaan
419 # Feature:
420 #  . the mail gateway now responds with an error message when invalid
421 #    values for arguments are specified for link or multilink properties
422 #  . modified unit test to check nosy and assignedto when specified as
423 #    arguments
425 # Fixed:
426 #  . fixed setting nosy as argument in subject line
428 # Revision 1.5  2002/01/15 00:12:40  richard
429 # #503340 ] creating issue with [asignedto=p.ohly]
431 # Revision 1.4  2002/01/14 07:12:15  richard
432 # removed file writing from tests...
434 # Revision 1.3  2002/01/14 02:20:15  richard
435 #  . changed all config accesses so they access either the instance or the
436 #    config attriubute on the db. This means that all config is obtained from
437 #    instance_config instead of the mish-mash of classes. This will make
438 #    switching to a ConfigParser setup easier too, I hope.
440 # At a minimum, this makes migration a _little_ easier (a lot easier in the
441 # 0.5.0 switch, I hope!)
443 # Revision 1.2  2002/01/11 23:22:29  richard
444 #  . #502437 ] rogue reactor and unittest
445 #    in short, the nosy reactor was modifying the nosy list. That code had
446 #    been there for a long time, and I suspsect it was there because we
447 #    weren't generating the nosy list correctly in other places of the code.
448 #    We're now doing that, so the nosy-modifying code can go away from the
449 #    nosy reactor.
451 # Revision 1.1  2002/01/02 02:31:38  richard
452 # Sorry for the huge checkin message - I was only intending to implement #496356
453 # but I found a number of places where things had been broken by transactions:
454 #  . modified ROUNDUPDBSENDMAILDEBUG to be SENDMAILDEBUG and hold a filename
455 #    for _all_ roundup-generated smtp messages to be sent to.
456 #  . the transaction cache had broken the roundupdb.Class set() reactors
457 #  . newly-created author users in the mailgw weren't being committed to the db
459 # Stuff that made it into CHANGES.txt (ie. the stuff I was actually working
460 # on when I found that stuff :):
461 #  . #496356 ] Use threading in messages
462 #  . detectors were being registered multiple times
463 #  . added tests for mailgw
464 #  . much better attaching of erroneous messages in the mail gateway
469 # vim: set filetype=python ts=4 sw=4 et si