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.12 2002-02-15 00:13:38 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')
39 def tearDown(self):
40 if os.path.exists(os.environ['SENDMAILDEBUG']):
41 os.remove(os.environ['SENDMAILDEBUG'])
42 try:
43 shutil.rmtree(self.dirname)
44 except OSError, error:
45 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
47 def testNewIssue(self):
48 message = cStringIO.StringIO('''Content-Type: text/plain;
49 charset="iso-8859-1"
50 From: Chef <chef@bork.bork.bork
51 To: issue_tracker@fill.me.in.
52 Cc: richard@test
53 Message-Id: <dummy_test_message_id>
54 Subject: [issue] Testing...
56 This is a test submission of a new issue.
57 ''')
58 handler = self.instance.MailGW(self.instance, self.db)
59 handler.main(message)
60 if os.path.exists(os.environ['SENDMAILDEBUG']):
61 error = open(os.environ['SENDMAILDEBUG']).read()
62 self.assertEqual('no error', error)
64 def testNewIssueNoClass(self):
65 message = cStringIO.StringIO('''Content-Type: text/plain;
66 charset="iso-8859-1"
67 From: Chef <chef@bork.bork.bork
68 To: issue_tracker@fill.me.in.
69 Cc: richard@test
70 Message-Id: <dummy_test_message_id>
71 Subject: Testing...
73 This is a test submission of a new issue.
74 ''')
75 handler = self.instance.MailGW(self.instance, self.db)
76 handler.main(message)
77 if os.path.exists(os.environ['SENDMAILDEBUG']):
78 error = open(os.environ['SENDMAILDEBUG']).read()
79 self.assertEqual('no error', error)
81 def testNewIssueAuthMsg(self):
82 message = cStringIO.StringIO('''Content-Type: text/plain;
83 charset="iso-8859-1"
84 From: Chef <chef@bork.bork.bork
85 To: issue_tracker@fill.me.in.
86 Message-Id: <dummy_test_message_id>
87 Subject: [issue] Testing... [nosy=mary; assignedto=richard]
89 This is a test submission of a new issue.
90 ''')
91 handler = self.instance.MailGW(self.instance, self.db)
92 # TODO: fix the damn config - this is apalling
93 self.db.config.MESSAGES_TO_AUTHOR = 'yes'
94 handler.main(message)
96 self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
97 '''FROM: roundup-admin@fill.me.in.
98 TO: chef@bork.bork.bork, mary@test, richard@test
99 Content-Type: text/plain
100 Subject: [issue1] Testing...
101 To: chef@bork.bork.bork, mary@test, richard@test
102 From: Chef <issue_tracker@fill.me.in.>
103 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
104 MIME-Version: 1.0
105 Message-Id: <dummy_test_message_id>
106 X-Roundup-Name: Roundup issue tracker
109 New submission from Chef <chef@bork.bork.bork>:
111 This is a test submission of a new issue.
114 ----------
115 assignedto: richard
116 messages: 1
117 nosy: mary, Chef, richard
118 status: unread
119 title: Testing...
120 ___________________________________________________
121 "Roundup issue tracker" <issue_tracker@fill.me.in.>
122 http://some.useful.url/issue1
123 ___________________________________________________
124 ''', 'Generated message not correct')
126 # BUG
127 # def testMultipart(self):
128 # '''With more than one part'''
129 # see MultipartEnc tests: but if there is more than one part
130 # we return a multipart/mixed and the boundary contains
131 # the ip address of the test machine.
133 # BUG should test some binary attamchent too.
135 def testFollowup(self):
136 self.testNewIssue()
137 message = cStringIO.StringIO('''Content-Type: text/plain;
138 charset="iso-8859-1"
139 From: richard <richard@test>
140 To: issue_tracker@fill.me.in.
141 Message-Id: <followup_dummy_id>
142 In-Reply-To: <dummy_test_message_id>
143 Subject: [issue1] Testing... [assignedto=mary; nosy=john]
145 This is a followup
146 ''')
147 handler = self.instance.MailGW(self.instance, self.db)
148 handler.main(message)
150 self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
151 '''FROM: roundup-admin@fill.me.in.
152 TO: chef@bork.bork.bork, mary@test, john@test
153 Content-Type: text/plain
154 Subject: [issue1] Testing...
155 To: chef@bork.bork.bork, mary@test, john@test
156 From: richard <issue_tracker@fill.me.in.>
157 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
158 MIME-Version: 1.0
159 Message-Id: <followup_dummy_id>
160 In-Reply-To: <dummy_test_message_id>
161 X-Roundup-Name: Roundup issue tracker
164 richard <richard@test> added the comment:
166 This is a followup
169 ----------
170 assignedto: -> mary
171 nosy: +mary, john
172 status: unread -> chatting
173 ___________________________________________________
174 "Roundup issue tracker" <issue_tracker@fill.me.in.>
175 http://some.useful.url/issue1
176 ___________________________________________________
177 ''', 'Generated message not correct')
179 def testFollowup2(self):
180 self.testNewIssue()
181 message = cStringIO.StringIO('''Content-Type: text/plain;
182 charset="iso-8859-1"
183 From: mary <mary@test>
184 To: issue_tracker@fill.me.in.
185 Message-Id: <followup_dummy_id>
186 In-Reply-To: <dummy_test_message_id>
187 Subject: [issue1] Testing...
189 This is a second followup
190 ''')
191 handler = self.instance.MailGW(self.instance, self.db)
192 handler.main(message)
193 self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
194 '''FROM: roundup-admin@fill.me.in.
195 TO: chef@bork.bork.bork, richard@test
196 Content-Type: text/plain
197 Subject: [issue1] Testing...
198 To: chef@bork.bork.bork, richard@test
199 From: mary <issue_tracker@fill.me.in.>
200 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
201 MIME-Version: 1.0
202 Message-Id: <followup_dummy_id>
203 In-Reply-To: <dummy_test_message_id>
204 X-Roundup-Name: Roundup issue tracker
207 mary <mary@test> added the comment:
209 This is a second followup
212 ----------
213 status: unread -> chatting
214 ___________________________________________________
215 "Roundup issue tracker" <issue_tracker@fill.me.in.>
216 http://some.useful.url/issue1
217 ___________________________________________________
218 ''', 'Generated message not correct')
220 def testFollowupTitleMatch(self):
221 self.testNewIssue()
222 message = cStringIO.StringIO('''Content-Type: text/plain;
223 charset="iso-8859-1"
224 From: richard <richard@test>
225 To: issue_tracker@fill.me.in.
226 Message-Id: <followup_dummy_id>
227 In-Reply-To: <dummy_test_message_id>
228 Subject: Re: Testing... [assignedto=mary; nosy=john]
230 This is a followup
231 ''')
232 handler = self.instance.MailGW(self.instance, self.db)
233 handler.main(message)
235 self.assertEqual(open(os.environ['SENDMAILDEBUG']).read(),
236 '''FROM: roundup-admin@fill.me.in.
237 TO: chef@bork.bork.bork, mary@test, john@test
238 Content-Type: text/plain
239 Subject: [issue1] Testing...
240 To: chef@bork.bork.bork, mary@test, john@test
241 From: richard <issue_tracker@fill.me.in.>
242 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
243 MIME-Version: 1.0
244 Message-Id: <followup_dummy_id>
245 In-Reply-To: <dummy_test_message_id>
246 X-Roundup-Name: Roundup issue tracker
249 richard <richard@test> added the comment:
251 This is a followup
254 ----------
255 assignedto: -> mary
256 nosy: +mary, john
257 status: unread -> chatting
258 ___________________________________________________
259 "Roundup issue tracker" <issue_tracker@fill.me.in.>
260 http://some.useful.url/issue1
261 ___________________________________________________
262 ''') #, 'Generated message not correct')
264 def testEnc01(self):
265 self.testNewIssue()
266 message = cStringIO.StringIO('''Content-Type: text/plain;
267 charset="iso-8859-1"
268 From: mary <mary@test>
269 To: issue_tracker@fill.me.in.
270 Message-Id: <followup_dummy_id>
271 In-Reply-To: <dummy_test_message_id>
272 Subject: [issue1] Testing...
273 Content-Type: text/plain;
274 charset="iso-8859-1"
275 Content-Transfer-Encoding: quoted-printable
277 A message with encoding (encoded oe =F6)
279 ''')
280 handler = self.instance.MailGW(self.instance, self.db)
281 handler.main(message)
282 message_data = open(os.environ['SENDMAILDEBUG']).read()
283 self.assertEqual(message_data,
284 '''FROM: roundup-admin@fill.me.in.
285 TO: chef@bork.bork.bork, richard@test
286 Content-Type: text/plain
287 Subject: [issue1] Testing...
288 To: chef@bork.bork.bork, richard@test
289 From: mary <issue_tracker@fill.me.in.>
290 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
291 MIME-Version: 1.0
292 Message-Id: <followup_dummy_id>
293 In-Reply-To: <dummy_test_message_id>
294 X-Roundup-Name: Roundup issue tracker
297 mary <mary@test> added the comment:
299 A message with encoding (encoded oe ö)
301 ----------
302 status: unread -> chatting
303 ___________________________________________________
304 "Roundup issue tracker" <issue_tracker@fill.me.in.>
305 http://some.useful.url/issue1
306 ___________________________________________________
307 ''', 'Generated message not correct')
310 def testMultipartEnc01(self):
311 self.testNewIssue()
312 message = cStringIO.StringIO('''Content-Type: text/plain;
313 charset="iso-8859-1"
314 From: mary <mary@test>
315 To: issue_tracker@fill.me.in.
316 Message-Id: <followup_dummy_id>
317 In-Reply-To: <dummy_test_message_id>
318 Subject: [issue1] Testing...
319 Content-Type: multipart/mixed;
320 boundary="----_=_NextPart_000_01"
322 This message is in MIME format. Since your mail reader does not understand
323 this format, some or all of this message may not be legible.
325 ------_=_NextPart_000_01
326 Content-Type: text/plain;
327 charset="iso-8859-1"
328 Content-Transfer-Encoding: quoted-printable
330 A message with first part encoded (encoded oe =F6)
332 ''')
333 handler = self.instance.MailGW(self.instance, self.db)
334 handler.main(message)
335 message_data = open(os.environ['SENDMAILDEBUG']).read()
336 self.assertEqual(message_data,
337 '''FROM: roundup-admin@fill.me.in.
338 TO: chef@bork.bork.bork, richard@test
339 Content-Type: text/plain
340 Subject: [issue1] Testing...
341 To: chef@bork.bork.bork, richard@test
342 From: mary <issue_tracker@fill.me.in.>
343 Reply-To: Roundup issue tracker <issue_tracker@fill.me.in.>
344 MIME-Version: 1.0
345 Message-Id: <followup_dummy_id>
346 In-Reply-To: <dummy_test_message_id>
347 X-Roundup-Name: Roundup issue tracker
350 mary <mary@test> added the comment:
352 A message with first part encoded (encoded oe ö)
354 ----------
355 status: unread -> chatting
356 ___________________________________________________
357 "Roundup issue tracker" <issue_tracker@fill.me.in.>
358 http://some.useful.url/issue1
359 ___________________________________________________
360 ''', 'Generated message not correct')
362 class ExtMailgwTestCase(MailgwTestCase):
363 schema = 'extended'
365 def suite():
366 l = [unittest.makeSuite(MailgwTestCase, 'test'),
367 unittest.makeSuite(ExtMailgwTestCase, 'test')
368 ]
369 return unittest.TestSuite(l)
372 #
373 # $Log: not supported by cvs2svn $
374 # Revision 1.11 2002/02/14 23:38:12 richard
375 # Fixed the unit tests for the mailgw re: the x-roundup-name header.
376 # Also made the test runner more user-friendly:
377 # ./run_tests - detect all tests in test/test_<name>.py and run them
378 # ./run_tests <name> - run only test/test_<name>.py
379 # eg ./run_tests mailgw - run the mailgw test from test/test_mailgw.py
380 #
381 # Revision 1.10 2002/02/12 08:08:55 grubert
382 # . Clean up mail handling, multipart handling.
383 #
384 # Revision 1.9 2002/02/05 14:15:29 grubert
385 # . respect encodings in non multipart messages.
386 #
387 # Revision 1.8 2002/02/04 09:40:21 grubert
388 # . add test for multipart messages with first part being encoded.
389 #
390 # Revision 1.7 2002/01/22 11:54:45 rochecompaan
391 # Fixed status change in mail gateway.
392 #
393 # Revision 1.6 2002/01/21 10:05:48 rochecompaan
394 # Feature:
395 # . the mail gateway now responds with an error message when invalid
396 # values for arguments are specified for link or multilink properties
397 # . modified unit test to check nosy and assignedto when specified as
398 # arguments
399 #
400 # Fixed:
401 # . fixed setting nosy as argument in subject line
402 #
403 # Revision 1.5 2002/01/15 00:12:40 richard
404 # #503340 ] creating issue with [asignedto=p.ohly]
405 #
406 # Revision 1.4 2002/01/14 07:12:15 richard
407 # removed file writing from tests...
408 #
409 # Revision 1.3 2002/01/14 02:20:15 richard
410 # . changed all config accesses so they access either the instance or the
411 # config attriubute on the db. This means that all config is obtained from
412 # instance_config instead of the mish-mash of classes. This will make
413 # switching to a ConfigParser setup easier too, I hope.
414 #
415 # At a minimum, this makes migration a _little_ easier (a lot easier in the
416 # 0.5.0 switch, I hope!)
417 #
418 # Revision 1.2 2002/01/11 23:22:29 richard
419 # . #502437 ] rogue reactor and unittest
420 # in short, the nosy reactor was modifying the nosy list. That code had
421 # been there for a long time, and I suspsect it was there because we
422 # weren't generating the nosy list correctly in other places of the code.
423 # We're now doing that, so the nosy-modifying code can go away from the
424 # nosy reactor.
425 #
426 # Revision 1.1 2002/01/02 02:31:38 richard
427 # Sorry for the huge checkin message - I was only intending to implement #496356
428 # but I found a number of places where things had been broken by transactions:
429 # . modified ROUNDUPDBSENDMAILDEBUG to be SENDMAILDEBUG and hold a filename
430 # for _all_ roundup-generated smtp messages to be sent to.
431 # . the transaction cache had broken the roundupdb.Class set() reactors
432 # . newly-created author users in the mailgw weren't being committed to the db
433 #
434 # Stuff that made it into CHANGES.txt (ie. the stuff I was actually working
435 # on when I found that stuff :):
436 # . #496356 ] Use threading in messages
437 # . detectors were being registered multiple times
438 # . added tests for mailgw
439 # . much better attaching of erroneous messages in the mail gateway
440 #
441 #
442 #
443 #
444 # vim: set filetype=python ts=4 sw=4 et si