Code

Change nosymessage and send_message to accept msgid=None (RFE #707235).
[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.60 2003-11-03 22:23:02 jlgijsbers Exp $
13 import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822
15 from cStringIO import StringIO
17 if not os.environ.has_key('SENDMAILDEBUG'):
18     os.environ['SENDMAILDEBUG'] = 'mail-test.log'
19 SENDMAILDEBUG = os.environ['SENDMAILDEBUG']
21 from roundup.mailgw import MailGW, Unauthorized, uidFromAddress, parseContent
22 from roundup import init, instance, rfc2822
25 class Message(rfc822.Message):
26     """String-based Message class with equivalence test."""
27     def __init__(self, s):
28         rfc822.Message.__init__(self, StringIO(s.strip()))
29         
30     def __eq__(self, other):
31         return (self.dict == other.dict and
32                 self.fp.read() == other.fp.read()) 
34 class DiffHelper:
35     def compareMessages(self, new, old):
36         """Compare messages for semantic equivalence."""
37         new, old = Message(new), Message(old)
38         del new['date'], old['date']
40         if not new == old:
41             res = ['Generated message not correct (diff follows):']
43             for key in new.keys():
44                 if new[key] != old[key]:
45                     res.append('  %s: %s != %s' % (key, old[key], new[key]))
46             
47             body_diff = self.compareStrings(new.fp.read(), old.fp.read())
48             if body_diff:
49                 res.append('')
50                 res.extend(body_diff)
52             raise AssertionError, '\n'.join(res)
53     
54     def compareStrings(self, s2, s1):
55         '''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
56            the first to be the "original" but in the calls in this file,
57            the second arg is the original. Ho hum.
58         '''
59         l1 = s1.strip().split('\n')
60         l2 = s2.strip().split('\n')
61         if l1 == l2:
62             return
63         s = difflib.SequenceMatcher(None, l1, l2)
64         res = []
65         for value, s1s, s1e, s2s, s2e in s.get_opcodes():
66             if value == 'equal':
67                 for i in range(s1s, s1e):
68                     res.append('  %s'%l1[i])
69             elif value == 'delete':
70                 for i in range(s1s, s1e):
71                     res.append('- %s'%l1[i])
72             elif value == 'insert':
73                 for i in range(s2s, s2e):
74                     res.append('+ %s'%l2[i])
75             elif value == 'replace':
76                 for i, j in zip(range(s1s, s1e), range(s2s, s2e)):
77                     res.append('- %s'%l1[i])
78                     res.append('+ %s'%l2[j])
80         return res
82 class MailgwTestCase(unittest.TestCase, DiffHelper):
83     count = 0
84     schema = 'classic'
85     def setUp(self):
86         MailgwTestCase.count = MailgwTestCase.count + 1
87         self.dirname = '_test_mailgw_%s'%self.count
88         try:
89             shutil.rmtree(self.dirname)
90         except OSError, error:
91             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
92         # create the instance
93         init.install(self.dirname, 'templates/classic')
94         init.write_select_db(self.dirname, 'anydbm')
95         init.initialise(self.dirname, 'sekrit')
96         
97         # check we can load the package
98         self.instance = instance.open(self.dirname)
100         # and open the database
101         self.db = self.instance.open('admin')
102         self.db.user.create(username='Chef', address='chef@bork.bork.bork',
103             realname='Bork, Chef', roles='User')
104         self.db.user.create(username='richard', address='richard@test',
105             roles='User')
106         self.db.user.create(username='mary', address='mary@test',
107             roles='User', realname='Contrary, Mary')
108         self.db.user.create(username='john', address='john@test',
109             alternate_addresses='jondoe@test\njohn.doe@test', roles='User',
110             realname='John Doe')
112     def tearDown(self):
113         if os.path.exists(SENDMAILDEBUG):
114             os.remove(SENDMAILDEBUG)
115         self.db.close()
116         try:
117             shutil.rmtree(self.dirname)
118         except OSError, error:
119             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
121     def _send_mail(self, message):
122         handler = self.instance.MailGW(self.instance, self.db)
123         handler.trapExceptions = 0
124         return handler.main(StringIO(message))
125         
126     def _get_mail(self):
127         f = open(SENDMAILDEBUG)
128         try:
129             return f.read()
130         finally:
131             f.close()
133     def testEmptyMessage(self):
134         nodeid = self._send_mail('''Content-Type: text/plain;
135   charset="iso-8859-1"
136 From: Chef <chef@bork.bork.bork>
137 To: issue_tracker@your.tracker.email.domain.example
138 Cc: richard@test
139 Message-Id: <dummy_test_message_id>
140 Subject: [issue] Testing...
142 ''')
143         assert not os.path.exists(SENDMAILDEBUG)
144         self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...')
146     def doNewIssue(self):
147         nodeid = self._send_mail('''Content-Type: text/plain;
148   charset="iso-8859-1"
149 From: Chef <chef@bork.bork.bork>
150 To: issue_tracker@your.tracker.email.domain.example
151 Cc: richard@test
152 Message-Id: <dummy_test_message_id>
153 Subject: [issue] Testing...
155 This is a test submission of a new issue.
156 ''')
157         assert not os.path.exists(SENDMAILDEBUG)
158         l = self.db.issue.get(nodeid, 'nosy')
159         l.sort()
160         self.assertEqual(l, ['3', '4'])
161         return nodeid
163     def testNewIssue(self):
164         self.doNewIssue()
166     def testNewIssueNosy(self):
167         self.instance.config.ADD_AUTHOR_TO_NOSY = 'yes'
168         nodeid = self._send_mail('''Content-Type: text/plain;
169   charset="iso-8859-1"
170 From: Chef <chef@bork.bork.bork>
171 To: issue_tracker@your.tracker.email.domain.example
172 Cc: richard@test
173 Message-Id: <dummy_test_message_id>
174 Subject: [issue] Testing...
176 This is a test submission of a new issue.
177 ''')
178         assert not os.path.exists(SENDMAILDEBUG)
179         l = self.db.issue.get(nodeid, 'nosy')
180         l.sort()
181         self.assertEqual(l, ['3', '4'])
183     def testAlternateAddress(self):
184         self._send_mail('''Content-Type: text/plain;
185   charset="iso-8859-1"
186 From: John Doe <john.doe@test>
187 To: issue_tracker@your.tracker.email.domain.example
188 Message-Id: <dummy_test_message_id>
189 Subject: [issue] Testing...
191 This is a test submission of a new issue.
192 ''')
193         userlist = self.db.user.list()        
194         assert not os.path.exists(SENDMAILDEBUG)
195         self.assertEqual(userlist, self.db.user.list(),
196             "user created when it shouldn't have been")
198     def testNewIssueNoClass(self):
199         self._send_mail('''Content-Type: text/plain;
200   charset="iso-8859-1"
201 From: Chef <chef@bork.bork.bork>
202 To: issue_tracker@your.tracker.email.domain.example
203 Cc: richard@test
204 Message-Id: <dummy_test_message_id>
205 Subject: Testing...
207 This is a test submission of a new issue.
208 ''')
209         assert not os.path.exists(SENDMAILDEBUG)
211     def testNewIssueAuthMsg(self):
212         # TODO: fix the damn config - this is apalling
213         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
214         self._send_mail('''Content-Type: text/plain;
215   charset="iso-8859-1"
216 From: Chef <chef@bork.bork.bork>
217 To: issue_tracker@your.tracker.email.domain.example
218 Message-Id: <dummy_test_message_id>
219 Subject: [issue] Testing... [nosy=mary; assignedto=richard]
221 This is a test submission of a new issue.
222 ''')
223         self.compareMessages(self._get_mail(),
224 '''FROM: roundup-admin@your.tracker.email.domain.example
225 TO: chef@bork.bork.bork, mary@test, richard@test
226 Content-Type: text/plain; charset=utf-8
227 Subject: [issue1] Testing...
228 To: chef@bork.bork.bork, mary@test, richard@test
229 From: "Bork, Chef" <issue_tracker@your.tracker.email.domain.example>
230 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
231 MIME-Version: 1.0
232 Message-Id: <dummy_test_message_id>
233 X-Roundup-Name: Roundup issue tracker
234 X-Roundup-Loop: hello
235 Content-Transfer-Encoding: quoted-printable
238 New submission from Bork, Chef <chef@bork.bork.bork>:
240 This is a test submission of a new issue.
242 ----------
243 assignedto: richard
244 messages: 1
245 nosy: Chef, mary, richard
246 status: unread
247 title: Testing...
248 _______________________________________________________________________
249 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
250 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
251 _______________________________________________________________________
252 ''')
254     # BUG
255     # def testMultipart(self):
256     #         '''With more than one part'''
257     #        see MultipartEnc tests: but if there is more than one part
258     #        we return a multipart/mixed and the boundary contains
259     #        the ip address of the test machine. 
261     # BUG should test some binary attamchent too.
263     def testSimpleFollowup(self):
264         self.doNewIssue()
265         self._send_mail('''Content-Type: text/plain;
266   charset="iso-8859-1"
267 From: mary <mary@test>
268 To: issue_tracker@your.tracker.email.domain.example
269 Message-Id: <followup_dummy_id>
270 In-Reply-To: <dummy_test_message_id>
271 Subject: [issue1] Testing...
273 This is a second followup
274 ''')
275         self.compareMessages(self._get_mail(),
276 '''FROM: roundup-admin@your.tracker.email.domain.example
277 TO: chef@bork.bork.bork, richard@test
278 Content-Type: text/plain; charset=utf-8
279 Subject: [issue1] Testing...
280 To: chef@bork.bork.bork, richard@test
281 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
282 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
283 MIME-Version: 1.0
284 Message-Id: <followup_dummy_id>
285 In-Reply-To: <dummy_test_message_id>
286 X-Roundup-Name: Roundup issue tracker
287 X-Roundup-Loop: hello
288 Content-Transfer-Encoding: quoted-printable
291 Contrary, Mary <mary@test> added the comment:
293 This is a second followup
295 ----------
296 status: unread -> chatting
297 _______________________________________________________________________
298 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
299 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
300 _______________________________________________________________________
301 ''')
303     def testFollowup(self):
304         self.doNewIssue()
306         self._send_mail('''Content-Type: text/plain;
307   charset="iso-8859-1"
308 From: richard <richard@test>
309 To: issue_tracker@your.tracker.email.domain.example
310 Message-Id: <followup_dummy_id>
311 In-Reply-To: <dummy_test_message_id>
312 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
314 This is a followup
315 ''')
316         l = self.db.issue.get('1', 'nosy')
317         l.sort()
318         self.assertEqual(l, ['3', '4', '5', '6'])
320         self.compareMessages(self._get_mail(),
321 '''FROM: roundup-admin@your.tracker.email.domain.example
322 TO: chef@bork.bork.bork, john@test, mary@test
323 Content-Type: text/plain; charset=utf-8
324 Subject: [issue1] Testing...
325 To: chef@bork.bork.bork, john@test, mary@test
326 From: richard <issue_tracker@your.tracker.email.domain.example>
327 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
328 MIME-Version: 1.0
329 Message-Id: <followup_dummy_id>
330 In-Reply-To: <dummy_test_message_id>
331 X-Roundup-Name: Roundup issue tracker
332 X-Roundup-Loop: hello
333 Content-Transfer-Encoding: quoted-printable
336 richard <richard@test> added the comment:
338 This is a followup
340 ----------
341 assignedto:  -> mary
342 nosy: +john, mary
343 status: unread -> chatting
344 _______________________________________________________________________
345 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
346 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
347 _______________________________________________________________________
348 ''')
350     def testFollowupTitleMatch(self):
351         self.doNewIssue()
352         self._send_mail('''Content-Type: text/plain;
353   charset="iso-8859-1"
354 From: richard <richard@test>
355 To: issue_tracker@your.tracker.email.domain.example
356 Message-Id: <followup_dummy_id>
357 In-Reply-To: <dummy_test_message_id>
358 Subject: Re: Testing... [assignedto=mary; nosy=+john]
360 This is a followup
361 ''')
362         self.compareMessages(self._get_mail(),
363 '''FROM: roundup-admin@your.tracker.email.domain.example
364 TO: chef@bork.bork.bork, john@test, mary@test
365 Content-Type: text/plain; charset=utf-8
366 Subject: [issue1] Testing...
367 To: chef@bork.bork.bork, john@test, mary@test
368 From: richard <issue_tracker@your.tracker.email.domain.example>
369 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
370 MIME-Version: 1.0
371 Message-Id: <followup_dummy_id>
372 In-Reply-To: <dummy_test_message_id>
373 X-Roundup-Name: Roundup issue tracker
374 X-Roundup-Loop: hello
375 Content-Transfer-Encoding: quoted-printable
378 richard <richard@test> added the comment:
380 This is a followup
382 ----------
383 assignedto:  -> mary
384 nosy: +john, mary
385 status: unread -> chatting
386 _______________________________________________________________________
387 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
388 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
389 _______________________________________________________________________
390 ''')
392     def testFollowupNosyAuthor(self):
393         self.doNewIssue()
394         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
395         self._send_mail('''Content-Type: text/plain;
396   charset="iso-8859-1"
397 From: john@test
398 To: issue_tracker@your.tracker.email.domain.example
399 Message-Id: <followup_dummy_id>
400 In-Reply-To: <dummy_test_message_id>
401 Subject: [issue1] Testing...
403 This is a followup
404 ''')
406         self.compareMessages(self._get_mail(),
407 '''FROM: roundup-admin@your.tracker.email.domain.example
408 TO: chef@bork.bork.bork, richard@test
409 Content-Type: text/plain; charset=utf-8
410 Subject: [issue1] Testing...
411 To: chef@bork.bork.bork, richard@test
412 From: John Doe <issue_tracker@your.tracker.email.domain.example>
413 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
414 MIME-Version: 1.0
415 Message-Id: <followup_dummy_id>
416 In-Reply-To: <dummy_test_message_id>
417 X-Roundup-Name: Roundup issue tracker
418 X-Roundup-Loop: hello
419 Content-Transfer-Encoding: quoted-printable
422 John Doe <john@test> added the comment:
424 This is a followup
426 ----------
427 nosy: +john
428 status: unread -> chatting
429 _______________________________________________________________________
430 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
431 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
432 _______________________________________________________________________
434 ''')
436     def testFollowupNosyRecipients(self):
437         self.doNewIssue()
438         self.db.config.ADD_RECIPIENTS_TO_NOSY = 'yes'
439         self._send_mail('''Content-Type: text/plain;
440   charset="iso-8859-1"
441 From: richard@test
442 To: issue_tracker@your.tracker.email.domain.example
443 Cc: john@test
444 Message-Id: <followup_dummy_id>
445 In-Reply-To: <dummy_test_message_id>
446 Subject: [issue1] Testing...
448 This is a followup
449 ''')
450         self.compareMessages(self._get_mail(),
451 '''FROM: roundup-admin@your.tracker.email.domain.example
452 TO: chef@bork.bork.bork
453 Content-Type: text/plain; charset=utf-8
454 Subject: [issue1] Testing...
455 To: chef@bork.bork.bork
456 From: richard <issue_tracker@your.tracker.email.domain.example>
457 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
458 MIME-Version: 1.0
459 Message-Id: <followup_dummy_id>
460 In-Reply-To: <dummy_test_message_id>
461 X-Roundup-Name: Roundup issue tracker
462 X-Roundup-Loop: hello
463 Content-Transfer-Encoding: quoted-printable
466 richard <richard@test> added the comment:
468 This is a followup
470 ----------
471 nosy: +john
472 status: unread -> chatting
473 _______________________________________________________________________
474 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
475 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
476 _______________________________________________________________________
478 ''')
480     def testFollowupNosyAuthorAndCopy(self):
481         self.doNewIssue()
482         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
483         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
484         self._send_mail('''Content-Type: text/plain;
485   charset="iso-8859-1"
486 From: john@test
487 To: issue_tracker@your.tracker.email.domain.example
488 Message-Id: <followup_dummy_id>
489 In-Reply-To: <dummy_test_message_id>
490 Subject: [issue1] Testing...
492 This is a followup
493 ''')
494         self.compareMessages(self._get_mail(),
495 '''FROM: roundup-admin@your.tracker.email.domain.example
496 TO: chef@bork.bork.bork, john@test, richard@test
497 Content-Type: text/plain; charset=utf-8
498 Subject: [issue1] Testing...
499 To: chef@bork.bork.bork, john@test, richard@test
500 From: John Doe <issue_tracker@your.tracker.email.domain.example>
501 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
502 MIME-Version: 1.0
503 Message-Id: <followup_dummy_id>
504 In-Reply-To: <dummy_test_message_id>
505 X-Roundup-Name: Roundup issue tracker
506 X-Roundup-Loop: hello
507 Content-Transfer-Encoding: quoted-printable
510 John Doe <john@test> added the comment:
512 This is a followup
514 ----------
515 nosy: +john
516 status: unread -> chatting
517 _______________________________________________________________________
518 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
519 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
520 _______________________________________________________________________
522 ''')
524     def testFollowupNoNosyAuthor(self):
525         self.doNewIssue()
526         self.instance.config.ADD_AUTHOR_TO_NOSY = 'no'
527         self._send_mail('''Content-Type: text/plain;
528   charset="iso-8859-1"
529 From: john@test
530 To: issue_tracker@your.tracker.email.domain.example
531 Message-Id: <followup_dummy_id>
532 In-Reply-To: <dummy_test_message_id>
533 Subject: [issue1] Testing...
535 This is a followup
536 ''')
537         self.compareMessages(self._get_mail(),
538 '''FROM: roundup-admin@your.tracker.email.domain.example
539 TO: chef@bork.bork.bork, richard@test
540 Content-Type: text/plain; charset=utf-8
541 Subject: [issue1] Testing...
542 To: chef@bork.bork.bork, richard@test
543 From: John Doe <issue_tracker@your.tracker.email.domain.example>
544 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
545 MIME-Version: 1.0
546 Message-Id: <followup_dummy_id>
547 In-Reply-To: <dummy_test_message_id>
548 X-Roundup-Name: Roundup issue tracker
549 X-Roundup-Loop: hello
550 Content-Transfer-Encoding: quoted-printable
553 John Doe <john@test> added the comment:
555 This is a followup
557 ----------
558 status: unread -> chatting
559 _______________________________________________________________________
560 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
561 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
562 _______________________________________________________________________
564 ''')
566     def testFollowupNoNosyRecipients(self):
567         self.doNewIssue()
568         self.instance.config.ADD_RECIPIENTS_TO_NOSY = 'no'
569         self._send_mail('''Content-Type: text/plain;
570   charset="iso-8859-1"
571 From: richard@test
572 To: issue_tracker@your.tracker.email.domain.example
573 Cc: john@test
574 Message-Id: <followup_dummy_id>
575 In-Reply-To: <dummy_test_message_id>
576 Subject: [issue1] Testing...
578 This is a followup
579 ''')
580         self.compareMessages(self._get_mail(),
581 '''FROM: roundup-admin@your.tracker.email.domain.example
582 TO: chef@bork.bork.bork
583 Content-Type: text/plain; charset=utf-8
584 Subject: [issue1] Testing...
585 To: chef@bork.bork.bork
586 From: richard <issue_tracker@your.tracker.email.domain.example>
587 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
588 MIME-Version: 1.0
589 Message-Id: <followup_dummy_id>
590 In-Reply-To: <dummy_test_message_id>
591 X-Roundup-Name: Roundup issue tracker
592 X-Roundup-Loop: hello
593 Content-Transfer-Encoding: quoted-printable
596 richard <richard@test> added the comment:
598 This is a followup
600 ----------
601 status: unread -> chatting
602 _______________________________________________________________________
603 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
604 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
605 _______________________________________________________________________
607 ''')
609     def testFollowupEmptyMessage(self):
610         self.doNewIssue()
612         self._send_mail('''Content-Type: text/plain;
613   charset="iso-8859-1"
614 From: richard <richard@test>
615 To: issue_tracker@your.tracker.email.domain.example
616 Message-Id: <followup_dummy_id>
617 In-Reply-To: <dummy_test_message_id>
618 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
620 ''')
621         l = self.db.issue.get('1', 'nosy')
622         l.sort()
623         self.assertEqual(l, ['3', '4', '5', '6'])
625         # should be no file created (ie. no message)
626         assert not os.path.exists(SENDMAILDEBUG)
628     def testNosyRemove(self):
629         self.doNewIssue()
631         self._send_mail('''Content-Type: text/plain;
632   charset="iso-8859-1"
633 From: richard <richard@test>
634 To: issue_tracker@your.tracker.email.domain.example
635 Message-Id: <followup_dummy_id>
636 In-Reply-To: <dummy_test_message_id>
637 Subject: [issue1] Testing... [nosy=-richard]
639 ''')
640         l = self.db.issue.get('1', 'nosy')
641         l.sort()
642         self.assertEqual(l, ['3'])
644         # NO NOSY MESSAGE SHOULD BE SENT!
645         assert not os.path.exists(SENDMAILDEBUG)
647     def testNewUserAuthor(self):
648         # first without the permission
649         # heh... just ignore the API for a second ;)
650         self.db.security.role['anonymous'].permissions=[]
651         anonid = self.db.user.lookup('anonymous')
652         self.db.user.set(anonid, roles='Anonymous')
654         self.db.security.hasPermission('Email Registration', anonid)
655         l = self.db.user.list()
656         l.sort()
657         message = '''Content-Type: text/plain;
658   charset="iso-8859-1"
659 From: fubar <fubar@bork.bork.bork>
660 To: issue_tracker@your.tracker.email.domain.example
661 Message-Id: <dummy_test_message_id>
662 Subject: [issue] Testing...
664 This is a test submission of a new issue.
665 '''
666         self.assertRaises(Unauthorized, self._send_mail, message)
667         m = self.db.user.list()
668         m.sort()
669         self.assertEqual(l, m)
671         # now with the permission
672         p = self.db.security.getPermission('Email Registration')
673         self.db.security.role['anonymous'].permissions=[p]
674         self._send_mail(message)
675         m = self.db.user.list()
676         m.sort()
677         self.assertNotEqual(l, m)
679     def testEnc01(self):
680         self.doNewIssue()
681         self._send_mail('''Content-Type: text/plain;
682   charset="iso-8859-1"
683 From: mary <mary@test>
684 To: issue_tracker@your.tracker.email.domain.example
685 Message-Id: <followup_dummy_id>
686 In-Reply-To: <dummy_test_message_id>
687 Subject: [issue1] Testing...
688 Content-Type: text/plain;
689         charset="iso-8859-1"
690 Content-Transfer-Encoding: quoted-printable
692 A message with encoding (encoded oe =F6)
694 ''')
695         self.compareMessages(self._get_mail(),
696 '''FROM: roundup-admin@your.tracker.email.domain.example
697 TO: chef@bork.bork.bork, richard@test
698 Content-Type: text/plain; charset=utf-8
699 Subject: [issue1] Testing...
700 To: chef@bork.bork.bork, richard@test
701 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
702 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
703 MIME-Version: 1.0
704 Message-Id: <followup_dummy_id>
705 In-Reply-To: <dummy_test_message_id>
706 X-Roundup-Name: Roundup issue tracker
707 X-Roundup-Loop: hello
708 Content-Transfer-Encoding: quoted-printable
711 Contrary, Mary <mary@test> added the comment:
713 A message with encoding (encoded oe =C3=B6)
715 ----------
716 status: unread -> chatting
717 _______________________________________________________________________
718 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
719 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
720 _______________________________________________________________________
721 ''')
724     def testMultipartEnc01(self):
725         self.doNewIssue()
726         self._send_mail('''Content-Type: text/plain;
727   charset="iso-8859-1"
728 From: mary <mary@test>
729 To: issue_tracker@your.tracker.email.domain.example
730 Message-Id: <followup_dummy_id>
731 In-Reply-To: <dummy_test_message_id>
732 Subject: [issue1] Testing...
733 Content-Type: multipart/mixed;
734         boundary="----_=_NextPart_000_01"
736 This message is in MIME format. Since your mail reader does not understand
737 this format, some or all of this message may not be legible.
739 ------_=_NextPart_000_01
740 Content-Type: text/plain;
741         charset="iso-8859-1"
742 Content-Transfer-Encoding: quoted-printable
744 A message with first part encoded (encoded oe =F6)
746 ''')
747         self.compareMessages(self._get_mail(),
748 '''FROM: roundup-admin@your.tracker.email.domain.example
749 TO: chef@bork.bork.bork, richard@test
750 Content-Type: text/plain; charset=utf-8
751 Subject: [issue1] Testing...
752 To: chef@bork.bork.bork, richard@test
753 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
754 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
755 MIME-Version: 1.0
756 Message-Id: <followup_dummy_id>
757 In-Reply-To: <dummy_test_message_id>
758 X-Roundup-Name: Roundup issue tracker
759 X-Roundup-Loop: hello
760 Content-Transfer-Encoding: quoted-printable
763 Contrary, Mary <mary@test> added the comment:
765 A message with first part encoded (encoded oe =C3=B6)
767 ----------
768 status: unread -> chatting
769 _______________________________________________________________________
770 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
771 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
772 _______________________________________________________________________
773 ''')
775     def testContentDisposition(self):
776         self.doNewIssue()
777         self._send_mail('''Content-Type: text/plain;
778   charset="iso-8859-1"
779 From: mary <mary@test>
780 To: issue_tracker@your.tracker.email.domain.example
781 Message-Id: <followup_dummy_id>
782 In-Reply-To: <dummy_test_message_id>
783 Subject: [issue1] Testing...
784 Content-Type: multipart/mixed; boundary="bCsyhTFzCvuiizWE" 
785 Content-Disposition: inline 
786  
787  
788 --bCsyhTFzCvuiizWE 
789 Content-Type: text/plain; charset=us-ascii 
790 Content-Disposition: inline 
792 test attachment binary 
794 --bCsyhTFzCvuiizWE 
795 Content-Type: application/octet-stream 
796 Content-Disposition: attachment; filename="main.dvi" 
798 xxxxxx 
800 --bCsyhTFzCvuiizWE--
801 ''')
802         messages = self.db.issue.get('1', 'messages')
803         messages.sort()
804         file = self.db.msg.get(messages[-1], 'files')[0]
805         self.assertEqual(self.db.file.get(file, 'name'), 'main.dvi')
807     def testFollowupStupidQuoting(self):
808         self.doNewIssue()
810         self._send_mail('''Content-Type: text/plain;
811   charset="iso-8859-1"
812 From: richard <richard@test>
813 To: issue_tracker@your.tracker.email.domain.example
814 Message-Id: <followup_dummy_id>
815 In-Reply-To: <dummy_test_message_id>
816 Subject: Re: "[issue1] Testing... "
818 This is a followup
819 ''')
820         self.compareMessages(self._get_mail(),
821 '''FROM: roundup-admin@your.tracker.email.domain.example
822 TO: chef@bork.bork.bork
823 Content-Type: text/plain; charset=utf-8
824 Subject: [issue1] Testing...
825 To: chef@bork.bork.bork
826 From: richard <issue_tracker@your.tracker.email.domain.example>
827 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
828 MIME-Version: 1.0
829 Message-Id: <followup_dummy_id>
830 In-Reply-To: <dummy_test_message_id>
831 X-Roundup-Name: Roundup issue tracker
832 X-Roundup-Loop: hello
833 Content-Transfer-Encoding: quoted-printable
836 richard <richard@test> added the comment:
838 This is a followup
840 ----------
841 status: unread -> chatting
842 _______________________________________________________________________
843 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
844 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
845 _______________________________________________________________________
846 ''')
848     def testEmailQuoting(self):
849         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'no'
850         self.innerTestQuoting('''This is a followup
851 ''')
853     def testEmailQuotingRemove(self):
854         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'yes'
855         self.innerTestQuoting('''Blah blah wrote:
856 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
857 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
860 This is a followup
861 ''')
863     def innerTestQuoting(self, expect):
864         nodeid = self.doNewIssue()
866         messages = self.db.issue.get(nodeid, 'messages')
868         self._send_mail('''Content-Type: text/plain;
869   charset="iso-8859-1"
870 From: richard <richard@test>
871 To: issue_tracker@your.tracker.email.domain.example
872 Message-Id: <followup_dummy_id>
873 In-Reply-To: <dummy_test_message_id>
874 Subject: Re: [issue1] Testing...
876 Blah blah wrote:
877 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
878 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
881 This is a followup
882 ''')
883         # figure the new message id
884         newmessages = self.db.issue.get(nodeid, 'messages')
885         for msg in messages:
886             newmessages.remove(msg)
887         messageid = newmessages[0]
889         self.compareMessages(self.db.msg.get(messageid, 'content'), expect)
891     def testUserLookup(self):
892         i = self.db.user.create(username='user1', address='user1@foo.com')
893         self.assertEqual(uidFromAddress(self.db, ('', 'user1@foo.com'), 0), i)
894         self.assertEqual(uidFromAddress(self.db, ('', 'USER1@foo.com'), 0), i)
895         i = self.db.user.create(username='user2', address='USER2@foo.com')
896         self.assertEqual(uidFromAddress(self.db, ('', 'USER2@foo.com'), 0), i)
897         self.assertEqual(uidFromAddress(self.db, ('', 'user2@foo.com'), 0), i)
899     def testUserAlternateLookup(self):
900         i = self.db.user.create(username='user1', address='user1@foo.com',
901                                 alternate_addresses='user1@bar.com')
902         self.assertEqual(uidFromAddress(self.db, ('', 'user1@bar.com'), 0), i)
903         self.assertEqual(uidFromAddress(self.db, ('', 'USER1@bar.com'), 0), i)
905     def testUserCreate(self):
906         i = uidFromAddress(self.db, ('', 'user@foo.com'), 1)
907         self.assertNotEqual(uidFromAddress(self.db, ('', 'user@bar.com'), 1), i)
909     def testRFC2822(self):
910         ascii_header = "[issue243] This is a \"test\" - with 'quotation' marks"
911         unicode_header = '[issue244] \xd0\xb0\xd0\xbd\xd0\xb4\xd1\x80\xd0\xb5\xd0\xb9'
912         unicode_encoded = '=?utf-8?q?[issue244]_=D0=B0=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?='
913         self.assertEqual(rfc2822.encode_header(ascii_header), ascii_header)
914         self.assertEqual(rfc2822.encode_header(unicode_header), unicode_encoded)
916     def testRegistrationConfirmation(self):
917         otk = "Aj4euk4LZSAdwePohj90SME5SpopLETL"
918         self.db.otks.set(otk, username='johannes', __time='')
919         self._send_mail('''Content-Type: text/plain;
920   charset="iso-8859-1"
921 From: Chef <chef@bork.bork.bork>
922 To: issue_tracker@your.tracker.email.domain.example
923 Cc: richard@test
924 Message-Id: <dummy_test_message_id>
925 Subject: Re: Complete your registration to Roundup issue tracker\r
926  -- key %s
928 This is a test confirmation of registration.
929 ''' % otk)
930         self.db.user.lookup('johannes')
932     def testFollowupOnNonIssue(self):
933         self.db.keyword.create(name='Foo')
934         self._send_mail('''Content-Type: text/plain;
935   charset="iso-8859-1"
936 From: richard <richard@test>
937 To: issue_tracker@your.tracker.email.domain.example
938 Message-Id: <followup_dummy_id>
939 In-Reply-To: <dummy_test_message_id>
940 Subject: [keyword1] Testing... [name=Bar]
942 ''')        
943         self.assertEqual(self.db.keyword.get('1', 'name'), 'Bar')
944     
945 def test_suite():
946     suite = unittest.TestSuite()
947     suite.addTest(unittest.makeSuite(MailgwTestCase))
948     return suite
950 if __name__ == '__main__':
951     runner = unittest.TextTestRunner()
952     unittest.main(testRunner=runner)
954 # vim: set filetype=python ts=4 sw=4 et si