Code

extended date syntax to make range searches even more useful
[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.44 2003-04-17 06:51:44 richard Exp $
13 import unittest, cStringIO, tempfile, os, shutil, errno, imp, sys, difflib
14 import rfc822
16 # Note: Should parse emails according to RFC2822 instead of performing a
17 # literal string comparision.  Parsing the messages allows the tests to work for
18 # any legal serialization of an email.
19 #try :
20 #    import email
21 #except ImportError :
22 #    import rfc822 as email
24 from roundup.mailgw import MailGW, Unauthorized, uidFromAddress
25 from roundup import init, instance
27 # TODO: make this output only enough equal lines for context, not all of
28 # them
29 class DiffHelper:
30     def compareStrings(self, s2, s1):
31         '''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
32            the first to be the "original" but in the calls in this file,
33            the second arg is the original. Ho hum.
34         '''
35         # we have to special-case the Date: header here 'cos we can't test
36         # for it properly
37         l1=s1.strip().split('\n')
38         l2=[x for x in s2.strip().split('\n') if not x.startswith('Date: ')]
39         if l1 == l2:
40             return
42         s = difflib.SequenceMatcher(None, l1, l2)
43         res = ['Generated message not correct (diff follows):']
44         for value, s1s, s1e, s2s, s2e in s.get_opcodes():
45             if value == 'equal':
46                 for i in range(s1s, s1e):
47                     res.append('  %s'%l1[i])
48             elif value == 'delete':
49                 for i in range(s1s, s1e):
50                     res.append('- %s'%l1[i])
51             elif value == 'insert':
52                 for i in range(s2s, s2e):
53                     res.append('+ %s'%l2[i])
54             elif value == 'replace':
55                 for i, j in zip(range(s1s, s1e), range(s2s, s2e)):
56                     res.append('- %s'%l1[i])
57                     res.append('+ %s'%l2[j])
59         raise AssertionError, '\n'.join(res)
61 class MailgwTestCase(unittest.TestCase, DiffHelper):
62     count = 0
63     schema = 'classic'
64     def setUp(self):
65         MailgwTestCase.count = MailgwTestCase.count + 1
66         self.dirname = '_test_mailgw_%s'%self.count
67         try:
68             shutil.rmtree(self.dirname)
69         except OSError, error:
70             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
71         # create the instance
72         init.install(self.dirname, 'templates/classic')
73         init.write_select_db(self.dirname, 'anydbm')
74         init.initialise(self.dirname, 'sekrit')
75         # check we can load the package
76         self.instance = instance.open(self.dirname)
77         # and open the database
78         self.db = self.instance.open('admin')
79         self.db.user.create(username='Chef', address='chef@bork.bork.bork',
80             realname='Bork, Chef', roles='User')
81         self.db.user.create(username='richard', address='richard@test',
82             roles='User')
83         self.db.user.create(username='mary', address='mary@test',
84             roles='User', realname='Contrary, Mary')
85         self.db.user.create(username='john', address='john@test',
86             alternate_addresses='jondoe@test\njohn.doe@test', roles='User',
87             realname='John Doe')
89     def tearDown(self):
90         if os.path.exists(os.environ['SENDMAILDEBUG']):
91             os.remove(os.environ['SENDMAILDEBUG'])
92         self.db.close()
93         try:
94             shutil.rmtree(self.dirname)
95         except OSError, error:
96             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
98     def doNewIssue(self):
99         message = cStringIO.StringIO('''Content-Type: text/plain;
100   charset="iso-8859-1"
101 From: Chef <chef@bork.bork.bork>
102 To: issue_tracker@your.tracker.email.domain.example
103 Cc: richard@test
104 Message-Id: <dummy_test_message_id>
105 Subject: [issue] Testing...
107 This is a test submission of a new issue.
108 ''')
109         handler = self.instance.MailGW(self.instance, self.db)
110         handler.trapExceptions = 0
111         nodeid = handler.main(message)
112         if os.path.exists(os.environ['SENDMAILDEBUG']):
113             error = open(os.environ['SENDMAILDEBUG']).read()
114             self.assertEqual('no error', error)
115         l = self.db.issue.get(nodeid, 'nosy')
116         l.sort()
117         self.assertEqual(l, ['3', '4'])
118         return nodeid
120     def testNewIssue(self):
121         self.doNewIssue()
123     def testNewIssueNosy(self):
124         self.instance.config.ADD_AUTHOR_TO_NOSY = 'yes'
125         message = cStringIO.StringIO('''Content-Type: text/plain;
126   charset="iso-8859-1"
127 From: Chef <chef@bork.bork.bork>
128 To: issue_tracker@your.tracker.email.domain.example
129 Cc: richard@test
130 Message-Id: <dummy_test_message_id>
131 Subject: [issue] Testing...
133 This is a test submission of a new issue.
134 ''')
135         handler = self.instance.MailGW(self.instance, self.db)
136         handler.trapExceptions = 0
137         nodeid = handler.main(message)
138         if os.path.exists(os.environ['SENDMAILDEBUG']):
139             error = open(os.environ['SENDMAILDEBUG']).read()
140             self.assertEqual('no error', error)
141         l = self.db.issue.get(nodeid, 'nosy')
142         l.sort()
143         self.assertEqual(l, ['3', '4'])
145     def testAlternateAddress(self):
146         message = cStringIO.StringIO('''Content-Type: text/plain;
147   charset="iso-8859-1"
148 From: John Doe <john.doe@test>
149 To: issue_tracker@your.tracker.email.domain.example
150 Message-Id: <dummy_test_message_id>
151 Subject: [issue] Testing...
153 This is a test submission of a new issue.
154 ''')
155         userlist = self.db.user.list()
156         handler = self.instance.MailGW(self.instance, self.db)
157         handler.trapExceptions = 0
158         handler.main(message)
159         if os.path.exists(os.environ['SENDMAILDEBUG']):
160             error = open(os.environ['SENDMAILDEBUG']).read()
161             self.assertEqual('no error', error)
162         self.assertEqual(userlist, self.db.user.list(),
163             "user created when it shouldn't have been")
165     def testNewIssueNoClass(self):
166         message = cStringIO.StringIO('''Content-Type: text/plain;
167   charset="iso-8859-1"
168 From: Chef <chef@bork.bork.bork>
169 To: issue_tracker@your.tracker.email.domain.example
170 Cc: richard@test
171 Message-Id: <dummy_test_message_id>
172 Subject: Testing...
174 This is a test submission of a new issue.
175 ''')
176         handler = self.instance.MailGW(self.instance, self.db)
177         handler.trapExceptions = 0
178         handler.main(message)
179         if os.path.exists(os.environ['SENDMAILDEBUG']):
180             error = open(os.environ['SENDMAILDEBUG']).read()
181             self.assertEqual('no error', error)
183     def testNewIssueAuthMsg(self):
184         message = cStringIO.StringIO('''Content-Type: text/plain;
185   charset="iso-8859-1"
186 From: Chef <chef@bork.bork.bork>
187 To: issue_tracker@your.tracker.email.domain.example
188 Message-Id: <dummy_test_message_id>
189 Subject: [issue] Testing... [nosy=mary; assignedto=richard]
191 This is a test submission of a new issue.
192 ''')
193         handler = self.instance.MailGW(self.instance, self.db)
194         handler.trapExceptions = 0
195         # TODO: fix the damn config - this is apalling
196         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
197         handler.main(message)
199         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
200 '''FROM: roundup-admin@your.tracker.email.domain.example
201 TO: chef@bork.bork.bork, mary@test, richard@test
202 Content-Type: text/plain; charset=utf-8
203 Subject: [issue1] Testing...
204 To: chef@bork.bork.bork, mary@test, richard@test
205 From: "Bork, Chef" <issue_tracker@your.tracker.email.domain.example>
206 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
207 MIME-Version: 1.0
208 Message-Id: <dummy_test_message_id>
209 X-Roundup-Name: Roundup issue tracker
210 X-Roundup-Loop: hello
211 Content-Transfer-Encoding: quoted-printable
214 New submission from Bork, Chef <chef@bork.bork.bork>:
216 This is a test submission of a new issue.
218 ----------
219 assignedto: richard
220 messages: 1
221 nosy: Chef, mary, richard
222 status: unread
223 title: Testing...
224 _______________________________________________________________________
225 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
226 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
227 _______________________________________________________________________
228 ''')
230     # BUG
231     # def testMultipart(self):
232     #         '''With more than one part'''
233     #        see MultipartEnc tests: but if there is more than one part
234     #        we return a multipart/mixed and the boundary contains
235     #        the ip address of the test machine. 
237     # BUG should test some binary attamchent too.
239     def testSimpleFollowup(self):
240         self.doNewIssue()
241         message = cStringIO.StringIO('''Content-Type: text/plain;
242   charset="iso-8859-1"
243 From: mary <mary@test>
244 To: issue_tracker@your.tracker.email.domain.example
245 Message-Id: <followup_dummy_id>
246 In-Reply-To: <dummy_test_message_id>
247 Subject: [issue1] Testing...
249 This is a second followup
250 ''')
251         handler = self.instance.MailGW(self.instance, self.db)
252         handler.trapExceptions = 0
253         handler.main(message)
254         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
255 '''FROM: roundup-admin@your.tracker.email.domain.example
256 TO: chef@bork.bork.bork, richard@test
257 Content-Type: text/plain; charset=utf-8
258 Subject: [issue1] Testing...
259 To: chef@bork.bork.bork, richard@test
260 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
261 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
262 MIME-Version: 1.0
263 Message-Id: <followup_dummy_id>
264 In-Reply-To: <dummy_test_message_id>
265 X-Roundup-Name: Roundup issue tracker
266 X-Roundup-Loop: hello
267 Content-Transfer-Encoding: quoted-printable
270 Contrary, Mary <mary@test> added the comment:
272 This is a second followup
274 ----------
275 status: unread -> chatting
276 _______________________________________________________________________
277 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
278 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
279 _______________________________________________________________________
280 ''')
282     def testFollowup(self):
283         self.doNewIssue()
285         message = cStringIO.StringIO('''Content-Type: text/plain;
286   charset="iso-8859-1"
287 From: richard <richard@test>
288 To: issue_tracker@your.tracker.email.domain.example
289 Message-Id: <followup_dummy_id>
290 In-Reply-To: <dummy_test_message_id>
291 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
293 This is a followup
294 ''')
295         handler = self.instance.MailGW(self.instance, self.db)
296         handler.trapExceptions = 0
297         handler.main(message)
298         l = self.db.issue.get('1', 'nosy')
299         l.sort()
300         self.assertEqual(l, ['3', '4', '5', '6'])
302         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
303 '''FROM: roundup-admin@your.tracker.email.domain.example
304 TO: chef@bork.bork.bork, john@test, mary@test
305 Content-Type: text/plain; charset=utf-8
306 Subject: [issue1] Testing...
307 To: chef@bork.bork.bork, john@test, mary@test
308 From: richard <issue_tracker@your.tracker.email.domain.example>
309 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
310 MIME-Version: 1.0
311 Message-Id: <followup_dummy_id>
312 In-Reply-To: <dummy_test_message_id>
313 X-Roundup-Name: Roundup issue tracker
314 X-Roundup-Loop: hello
315 Content-Transfer-Encoding: quoted-printable
318 richard <richard@test> added the comment:
320 This is a followup
322 ----------
323 assignedto:  -> mary
324 nosy: +john, mary
325 status: unread -> chatting
326 _______________________________________________________________________
327 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
328 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
329 _______________________________________________________________________
330 ''')
332     def testFollowupTitleMatch(self):
333         self.doNewIssue()
334         message = cStringIO.StringIO('''Content-Type: text/plain;
335   charset="iso-8859-1"
336 From: richard <richard@test>
337 To: issue_tracker@your.tracker.email.domain.example
338 Message-Id: <followup_dummy_id>
339 In-Reply-To: <dummy_test_message_id>
340 Subject: Re: Testing... [assignedto=mary; nosy=+john]
342 This is a followup
343 ''')
344         handler = self.instance.MailGW(self.instance, self.db)
345         handler.trapExceptions = 0
346         handler.main(message)
348         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
349 '''FROM: roundup-admin@your.tracker.email.domain.example
350 TO: chef@bork.bork.bork, john@test, mary@test
351 Content-Type: text/plain; charset=utf-8
352 Subject: [issue1] Testing...
353 To: chef@bork.bork.bork, john@test, mary@test
354 From: richard <issue_tracker@your.tracker.email.domain.example>
355 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
356 MIME-Version: 1.0
357 Message-Id: <followup_dummy_id>
358 In-Reply-To: <dummy_test_message_id>
359 X-Roundup-Name: Roundup issue tracker
360 X-Roundup-Loop: hello
361 Content-Transfer-Encoding: quoted-printable
364 richard <richard@test> added the comment:
366 This is a followup
368 ----------
369 assignedto:  -> mary
370 nosy: +john, mary
371 status: unread -> chatting
372 _______________________________________________________________________
373 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
374 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
375 _______________________________________________________________________
376 ''')
378     def testFollowupNosyAuthor(self):
379         self.doNewIssue()
380         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
381         message = cStringIO.StringIO('''Content-Type: text/plain;
382   charset="iso-8859-1"
383 From: john@test
384 To: issue_tracker@your.tracker.email.domain.example
385 Message-Id: <followup_dummy_id>
386 In-Reply-To: <dummy_test_message_id>
387 Subject: [issue1] Testing...
389 This is a followup
390 ''')
391         handler = self.instance.MailGW(self.instance, self.db)
392         handler.trapExceptions = 0
393         handler.main(message)
395         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
396 '''FROM: roundup-admin@your.tracker.email.domain.example
397 TO: chef@bork.bork.bork, richard@test
398 Content-Type: text/plain; charset=utf-8
399 Subject: [issue1] Testing...
400 To: chef@bork.bork.bork, richard@test
401 From: John Doe <issue_tracker@your.tracker.email.domain.example>
402 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
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 X-Roundup-Loop: hello
408 Content-Transfer-Encoding: quoted-printable
411 John Doe <john@test> added the comment:
413 This is a followup
415 ----------
416 nosy: +john
417 status: unread -> chatting
418 _______________________________________________________________________
419 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
420 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
421 _______________________________________________________________________
423 ''')
425     def testFollowupNosyRecipients(self):
426         self.doNewIssue()
427         self.db.config.ADD_RECIPIENTS_TO_NOSY = 'yes'
428         message = cStringIO.StringIO('''Content-Type: text/plain;
429   charset="iso-8859-1"
430 From: richard@test
431 To: issue_tracker@your.tracker.email.domain.example
432 Cc: john@test
433 Message-Id: <followup_dummy_id>
434 In-Reply-To: <dummy_test_message_id>
435 Subject: [issue1] Testing...
437 This is a followup
438 ''')
439         handler = self.instance.MailGW(self.instance, self.db)
440         handler.trapExceptions = 0
441         handler.main(message)
443         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
444 '''FROM: roundup-admin@your.tracker.email.domain.example
445 TO: chef@bork.bork.bork
446 Content-Type: text/plain; charset=utf-8
447 Subject: [issue1] Testing...
448 To: chef@bork.bork.bork
449 From: richard <issue_tracker@your.tracker.email.domain.example>
450 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
451 MIME-Version: 1.0
452 Message-Id: <followup_dummy_id>
453 In-Reply-To: <dummy_test_message_id>
454 X-Roundup-Name: Roundup issue tracker
455 X-Roundup-Loop: hello
456 Content-Transfer-Encoding: quoted-printable
459 richard <richard@test> added the comment:
461 This is a followup
463 ----------
464 nosy: +john
465 status: unread -> chatting
466 _______________________________________________________________________
467 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
468 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
469 _______________________________________________________________________
471 ''')
473     def testFollowupNosyAuthorAndCopy(self):
474         self.doNewIssue()
475         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
476         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
477         message = cStringIO.StringIO('''Content-Type: text/plain;
478   charset="iso-8859-1"
479 From: john@test
480 To: issue_tracker@your.tracker.email.domain.example
481 Message-Id: <followup_dummy_id>
482 In-Reply-To: <dummy_test_message_id>
483 Subject: [issue1] Testing...
485 This is a followup
486 ''')
487         handler = self.instance.MailGW(self.instance, self.db)
488         handler.trapExceptions = 0
489         handler.main(message)
491         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
492 '''FROM: roundup-admin@your.tracker.email.domain.example
493 TO: chef@bork.bork.bork, john@test, richard@test
494 Content-Type: text/plain; charset=utf-8
495 Subject: [issue1] Testing...
496 To: chef@bork.bork.bork, john@test, richard@test
497 From: John Doe <issue_tracker@your.tracker.email.domain.example>
498 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
499 MIME-Version: 1.0
500 Message-Id: <followup_dummy_id>
501 In-Reply-To: <dummy_test_message_id>
502 X-Roundup-Name: Roundup issue tracker
503 X-Roundup-Loop: hello
504 Content-Transfer-Encoding: quoted-printable
507 John Doe <john@test> added the comment:
509 This is a followup
511 ----------
512 nosy: +john
513 status: unread -> chatting
514 _______________________________________________________________________
515 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
516 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
517 _______________________________________________________________________
519 ''')
521     def testFollowupNoNosyAuthor(self):
522         self.doNewIssue()
523         self.instance.config.ADD_AUTHOR_TO_NOSY = 'no'
524         message = cStringIO.StringIO('''Content-Type: text/plain;
525   charset="iso-8859-1"
526 From: john@test
527 To: issue_tracker@your.tracker.email.domain.example
528 Message-Id: <followup_dummy_id>
529 In-Reply-To: <dummy_test_message_id>
530 Subject: [issue1] Testing...
532 This is a followup
533 ''')
534         handler = self.instance.MailGW(self.instance, self.db)
535         handler.trapExceptions = 0
536         handler.main(message)
538         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
539 '''FROM: roundup-admin@your.tracker.email.domain.example
540 TO: chef@bork.bork.bork, richard@test
541 Content-Type: text/plain; charset=utf-8
542 Subject: [issue1] Testing...
543 To: chef@bork.bork.bork, richard@test
544 From: John Doe <issue_tracker@your.tracker.email.domain.example>
545 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
546 MIME-Version: 1.0
547 Message-Id: <followup_dummy_id>
548 In-Reply-To: <dummy_test_message_id>
549 X-Roundup-Name: Roundup issue tracker
550 X-Roundup-Loop: hello
551 Content-Transfer-Encoding: quoted-printable
554 John Doe <john@test> added the comment:
556 This is a followup
558 ----------
559 status: unread -> chatting
560 _______________________________________________________________________
561 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
562 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
563 _______________________________________________________________________
565 ''')
567     def testFollowupNoNosyRecipients(self):
568         self.doNewIssue()
569         self.instance.config.ADD_RECIPIENTS_TO_NOSY = 'no'
570         message = cStringIO.StringIO('''Content-Type: text/plain;
571   charset="iso-8859-1"
572 From: richard@test
573 To: issue_tracker@your.tracker.email.domain.example
574 Cc: john@test
575 Message-Id: <followup_dummy_id>
576 In-Reply-To: <dummy_test_message_id>
577 Subject: [issue1] Testing...
579 This is a followup
580 ''')
581         handler = self.instance.MailGW(self.instance, self.db)
582         handler.trapExceptions = 0
583         handler.main(message)
585         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
586 '''FROM: roundup-admin@your.tracker.email.domain.example
587 TO: chef@bork.bork.bork
588 Content-Type: text/plain; charset=utf-8
589 Subject: [issue1] Testing...
590 To: chef@bork.bork.bork
591 From: richard <issue_tracker@your.tracker.email.domain.example>
592 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
593 MIME-Version: 1.0
594 Message-Id: <followup_dummy_id>
595 In-Reply-To: <dummy_test_message_id>
596 X-Roundup-Name: Roundup issue tracker
597 X-Roundup-Loop: hello
598 Content-Transfer-Encoding: quoted-printable
601 richard <richard@test> added the comment:
603 This is a followup
605 ----------
606 status: unread -> chatting
607 _______________________________________________________________________
608 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
609 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
610 _______________________________________________________________________
612 ''')
614     def testFollowupEmptyMessage(self):
615         self.doNewIssue()
617         message = cStringIO.StringIO('''Content-Type: text/plain;
618   charset="iso-8859-1"
619 From: richard <richard@test>
620 To: issue_tracker@your.tracker.email.domain.example
621 Message-Id: <followup_dummy_id>
622 In-Reply-To: <dummy_test_message_id>
623 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
625 ''')
626         handler = self.instance.MailGW(self.instance, self.db)
627         handler.trapExceptions = 0
628         handler.main(message)
629         l = self.db.issue.get('1', 'nosy')
630         l.sort()
631         self.assertEqual(l, ['3', '4', '5', '6'])
633         # should be no file created (ie. no message)
634         assert not os.path.exists(os.environ['SENDMAILDEBUG'])
636     def testNosyRemove(self):
637         self.doNewIssue()
639         message = cStringIO.StringIO('''Content-Type: text/plain;
640   charset="iso-8859-1"
641 From: richard <richard@test>
642 To: issue_tracker@your.tracker.email.domain.example
643 Message-Id: <followup_dummy_id>
644 In-Reply-To: <dummy_test_message_id>
645 Subject: [issue1] Testing... [nosy=-richard]
647 ''')
648         handler = self.instance.MailGW(self.instance, self.db)
649         handler.trapExceptions = 0
650         handler.main(message)
651         l = self.db.issue.get('1', 'nosy')
652         l.sort()
653         self.assertEqual(l, ['3'])
655         # NO NOSY MESSAGE SHOULD BE SENT!
656         self.assert_(not os.path.exists(os.environ['SENDMAILDEBUG']))
658     def testNewUserAuthor(self):
659         # first without the permission
660         # heh... just ignore the API for a second ;)
661         self.db.security.role['anonymous'].permissions=[]
662         anonid = self.db.user.lookup('anonymous')
663         self.db.user.set(anonid, roles='Anonymous')
665         self.db.security.hasPermission('Email Registration', anonid)
666         l = self.db.user.list()
667         l.sort()
668         s = '''Content-Type: text/plain;
669   charset="iso-8859-1"
670 From: fubar <fubar@bork.bork.bork>
671 To: issue_tracker@your.tracker.email.domain.example
672 Message-Id: <dummy_test_message_id>
673 Subject: [issue] Testing...
675 This is a test submission of a new issue.
676 '''
677         message = cStringIO.StringIO(s)
678         handler = self.instance.MailGW(self.instance, self.db)
679         handler.trapExceptions = 0
680         self.assertRaises(Unauthorized, handler.main, message)
681         m = self.db.user.list()
682         m.sort()
683         self.assertEqual(l, m)
685         # now with the permission
686         p = self.db.security.getPermission('Email Registration')
687         self.db.security.role['anonymous'].permissions=[p]
688         handler = self.instance.MailGW(self.instance, self.db)
689         handler.trapExceptions = 0
690         message = cStringIO.StringIO(s)
691         handler.main(message)
692         m = self.db.user.list()
693         m.sort()
694         self.assertNotEqual(l, m)
696     def testEnc01(self):
697         self.doNewIssue()
698         message = cStringIO.StringIO('''Content-Type: text/plain;
699   charset="iso-8859-1"
700 From: mary <mary@test>
701 To: issue_tracker@your.tracker.email.domain.example
702 Message-Id: <followup_dummy_id>
703 In-Reply-To: <dummy_test_message_id>
704 Subject: [issue1] Testing...
705 Content-Type: text/plain;
706         charset="iso-8859-1"
707 Content-Transfer-Encoding: quoted-printable
709 A message with encoding (encoded oe =F6)
711 ''')
712         handler = self.instance.MailGW(self.instance, self.db)
713         handler.trapExceptions = 0
714         handler.main(message)
715         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
716 '''FROM: roundup-admin@your.tracker.email.domain.example
717 TO: chef@bork.bork.bork, richard@test
718 Content-Type: text/plain; charset=utf-8
719 Subject: [issue1] Testing...
720 To: chef@bork.bork.bork, richard@test
721 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
722 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
723 MIME-Version: 1.0
724 Message-Id: <followup_dummy_id>
725 In-Reply-To: <dummy_test_message_id>
726 X-Roundup-Name: Roundup issue tracker
727 X-Roundup-Loop: hello
728 Content-Transfer-Encoding: quoted-printable
731 Contrary, Mary <mary@test> added the comment:
733 A message with encoding (encoded oe =C3=B6)
735 ----------
736 status: unread -> chatting
737 _______________________________________________________________________
738 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
739 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
740 _______________________________________________________________________
741 ''')
744     def testMultipartEnc01(self):
745         self.doNewIssue()
746         message = cStringIO.StringIO('''Content-Type: text/plain;
747   charset="iso-8859-1"
748 From: mary <mary@test>
749 To: issue_tracker@your.tracker.email.domain.example
750 Message-Id: <followup_dummy_id>
751 In-Reply-To: <dummy_test_message_id>
752 Subject: [issue1] Testing...
753 Content-Type: multipart/mixed;
754         boundary="----_=_NextPart_000_01"
756 This message is in MIME format. Since your mail reader does not understand
757 this format, some or all of this message may not be legible.
759 ------_=_NextPart_000_01
760 Content-Type: text/plain;
761         charset="iso-8859-1"
762 Content-Transfer-Encoding: quoted-printable
764 A message with first part encoded (encoded oe =F6)
766 ''')
767         handler = self.instance.MailGW(self.instance, self.db)
768         handler.trapExceptions = 0
769         handler.main(message)
770         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
771 '''FROM: roundup-admin@your.tracker.email.domain.example
772 TO: chef@bork.bork.bork, richard@test
773 Content-Type: text/plain; charset=utf-8
774 Subject: [issue1] Testing...
775 To: chef@bork.bork.bork, richard@test
776 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
777 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
778 MIME-Version: 1.0
779 Message-Id: <followup_dummy_id>
780 In-Reply-To: <dummy_test_message_id>
781 X-Roundup-Name: Roundup issue tracker
782 X-Roundup-Loop: hello
783 Content-Transfer-Encoding: quoted-printable
786 Contrary, Mary <mary@test> added the comment:
788 A message with first part encoded (encoded oe =C3=B6)
790 ----------
791 status: unread -> chatting
792 _______________________________________________________________________
793 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
794 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
795 _______________________________________________________________________
796 ''')
798     def testContentDisposition(self):
799         self.doNewIssue()
800         message = cStringIO.StringIO('''Content-Type: text/plain;
801   charset="iso-8859-1"
802 From: mary <mary@test>
803 To: issue_tracker@your.tracker.email.domain.example
804 Message-Id: <followup_dummy_id>
805 In-Reply-To: <dummy_test_message_id>
806 Subject: [issue1] Testing...
807 Content-Type: multipart/mixed; boundary="bCsyhTFzCvuiizWE" 
808 Content-Disposition: inline 
809  
810  
811 --bCsyhTFzCvuiizWE 
812 Content-Type: text/plain; charset=us-ascii 
813 Content-Disposition: inline 
815 test attachment binary 
817 --bCsyhTFzCvuiizWE 
818 Content-Type: application/octet-stream 
819 Content-Disposition: attachment; filename="main.dvi" 
821 xxxxxx 
823 --bCsyhTFzCvuiizWE--
824 ''')
825         handler = self.instance.MailGW(self.instance, self.db)
826         handler.trapExceptions = 0
827         handler.main(message)
828         messages = self.db.issue.get('1', 'messages')
829         messages.sort()
830         file = self.db.msg.get(messages[-1], 'files')[0]
831         self.assertEqual(self.db.file.get(file, 'name'), 'main.dvi')
833     def testFollowupStupidQuoting(self):
834         self.doNewIssue()
836         message = cStringIO.StringIO('''Content-Type: text/plain;
837   charset="iso-8859-1"
838 From: richard <richard@test>
839 To: issue_tracker@your.tracker.email.domain.example
840 Message-Id: <followup_dummy_id>
841 In-Reply-To: <dummy_test_message_id>
842 Subject: Re: "[issue1] Testing... "
844 This is a followup
845 ''')
846         handler = self.instance.MailGW(self.instance, self.db)
847         handler.trapExceptions = 0
848         handler.main(message)
850         self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
851 '''FROM: roundup-admin@your.tracker.email.domain.example
852 TO: chef@bork.bork.bork
853 Content-Type: text/plain; charset=utf-8
854 Subject: [issue1] Testing...
855 To: chef@bork.bork.bork
856 From: richard <issue_tracker@your.tracker.email.domain.example>
857 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
858 MIME-Version: 1.0
859 Message-Id: <followup_dummy_id>
860 In-Reply-To: <dummy_test_message_id>
861 X-Roundup-Name: Roundup issue tracker
862 X-Roundup-Loop: hello
863 Content-Transfer-Encoding: quoted-printable
866 richard <richard@test> added the comment:
868 This is a followup
870 ----------
871 status: unread -> chatting
872 _______________________________________________________________________
873 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
874 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
875 _______________________________________________________________________
876 ''')
878     def testEmailQuoting(self):
879         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'no'
880         self.innerTestQuoting('''This is a followup
881 ''')
883     def testEmailQuotingRemove(self):
884         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'yes'
885         self.innerTestQuoting('''Blah blah wrote:
886 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
887 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
890 This is a followup
891 ''')
893     def innerTestQuoting(self, expect):
894         nodeid = self.doNewIssue()
896         messages = self.db.issue.get(nodeid, 'messages')
898         message = cStringIO.StringIO('''Content-Type: text/plain;
899   charset="iso-8859-1"
900 From: richard <richard@test>
901 To: issue_tracker@your.tracker.email.domain.example
902 Message-Id: <followup_dummy_id>
903 In-Reply-To: <dummy_test_message_id>
904 Subject: Re: [issue1] Testing...
906 Blah blah wrote:
907 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
908 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
911 This is a followup
912 ''')
913         handler = self.instance.MailGW(self.instance, self.db)
914         handler.trapExceptions = 0
915         handler.main(message)
917         # figure the new message id
918         newmessages = self.db.issue.get(nodeid, 'messages')
919         for msg in messages:
920             newmessages.remove(msg)
921         messageid = newmessages[0]
923         self.compareStrings(self.db.msg.get(messageid, 'content'), expect)
925     def testUserLookup(self):
926         i = self.db.user.create(username='user1', address='user1@foo.com')
927         self.assertEqual(uidFromAddress(self.db, ('', 'user1@foo.com'), 0), i)
928         self.assertEqual(uidFromAddress(self.db, ('', 'USER1@foo.com'), 0), i)
929         i = self.db.user.create(username='user2', address='USER2@foo.com')
930         self.assertEqual(uidFromAddress(self.db, ('', 'USER2@foo.com'), 0), i)
931         self.assertEqual(uidFromAddress(self.db, ('', 'user2@foo.com'), 0), i)
933     def testUserCreate(self):
934         i = uidFromAddress(self.db, ('', 'user@foo.com'), 1)
935         self.assertNotEqual(uidFromAddress(self.db, ('', 'user@bar.com'), 1), i)
937 def suite():
938     l = [unittest.makeSuite(MailgwTestCase),
939     ]
940     return unittest.TestSuite(l)
943 # vim: set filetype=python ts=4 sw=4 et si