Code

ignore incoming email with "Precedence: bulk" (sf patch 843489)
[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.63 2003-12-04 23:34:25 richard 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, \
22     parseContent, IgnoreLoop, IgnoreBulk
23 from roundup import init, instance, rfc2822
26 class Message(rfc822.Message):
27     """String-based Message class with equivalence test."""
28     def __init__(self, s):
29         rfc822.Message.__init__(self, StringIO(s.strip()))
30         
31     def __eq__(self, other):
32         return (self.dict == other.dict and
33                 self.fp.read() == other.fp.read()) 
35 class DiffHelper:
36     def compareMessages(self, new, old):
37         """Compare messages for semantic equivalence."""
38         new, old = Message(new), Message(old)
39         del new['date'], old['date']
41         if not new == old:
42             res = ['Generated message not correct (diff follows):']
44             for key in new.keys():
45                 if new[key] != old[key]:
46                     res.append('  %s: %s != %s' % (key, old[key], new[key]))
47             
48             body_diff = self.compareStrings(new.fp.read(), old.fp.read())
49             if body_diff:
50                 res.append('')
51                 res.extend(body_diff)
53             raise AssertionError, '\n'.join(res)
54     
55     def compareStrings(self, s2, s1):
56         '''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
57            the first to be the "original" but in the calls in this file,
58            the second arg is the original. Ho hum.
59         '''
60         l1 = s1.strip().split('\n')
61         l2 = s2.strip().split('\n')
62         if l1 == l2:
63             return
64         s = difflib.SequenceMatcher(None, l1, l2)
65         res = []
66         for value, s1s, s1e, s2s, s2e in s.get_opcodes():
67             if value == 'equal':
68                 for i in range(s1s, s1e):
69                     res.append('  %s'%l1[i])
70             elif value == 'delete':
71                 for i in range(s1s, s1e):
72                     res.append('- %s'%l1[i])
73             elif value == 'insert':
74                 for i in range(s2s, s2e):
75                     res.append('+ %s'%l2[i])
76             elif value == 'replace':
77                 for i, j in zip(range(s1s, s1e), range(s2s, s2e)):
78                     res.append('- %s'%l1[i])
79                     res.append('+ %s'%l2[j])
81         return res
83 class MailgwTestCase(unittest.TestCase, DiffHelper):
84     count = 0
85     schema = 'classic'
86     def setUp(self):
87         MailgwTestCase.count = MailgwTestCase.count + 1
88         self.dirname = '_test_mailgw_%s'%self.count
89         try:
90             shutil.rmtree(self.dirname)
91         except OSError, error:
92             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
93         # create the instance
94         init.install(self.dirname, 'templates/classic')
95         init.write_select_db(self.dirname, 'anydbm')
96         init.initialise(self.dirname, 'sekrit')
98         # check we can load the package
99         self.instance = instance.open(self.dirname)
101         # and open the database
102         self.db = self.instance.open('admin')
103         self.chef_id = self.db.user.create(username='Chef',
104             address='chef@bork.bork.bork', realname='Bork, Chef', roles='User')
105         self.richard_id = self.db.user.create(username='richard',
106             address='richard@test', roles='User')
107         self.mary_id = self.db.user.create(username='mary', address='mary@test',
108             roles='User', realname='Contrary, Mary')
109         self.john_id = self.db.user.create(username='john', address='john@test',
110             alternate_addresses='jondoe@test\njohn.doe@test', roles='User',
111             realname='John Doe')
113     def tearDown(self):
114         if os.path.exists(SENDMAILDEBUG):
115             os.remove(SENDMAILDEBUG)
116         self.db.close()
117         try:
118             shutil.rmtree(self.dirname)
119         except OSError, error:
120             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
122     def _send_mail(self, message):
123         handler = self.instance.MailGW(self.instance, self.db)
124         handler.trapExceptions = 0
125         return handler.main(StringIO(message))
126         
127     def _get_mail(self):
128         f = open(SENDMAILDEBUG)
129         try:
130             return f.read()
131         finally:
132             f.close()
134     def testEmptyMessage(self):
135         nodeid = self._send_mail('''Content-Type: text/plain;
136   charset="iso-8859-1"
137 From: Chef <chef@bork.bork.bork>
138 To: issue_tracker@your.tracker.email.domain.example
139 Cc: richard@test
140 Message-Id: <dummy_test_message_id>
141 Subject: [issue] Testing...
143 ''')
144         assert not os.path.exists(SENDMAILDEBUG)
145         self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...')
147     def doNewIssue(self):
148         nodeid = self._send_mail('''Content-Type: text/plain;
149   charset="iso-8859-1"
150 From: Chef <chef@bork.bork.bork>
151 To: issue_tracker@your.tracker.email.domain.example
152 Cc: richard@test
153 Message-Id: <dummy_test_message_id>
154 Subject: [issue] Testing...
156 This is a test submission of a new issue.
157 ''')
158         assert not os.path.exists(SENDMAILDEBUG)
159         l = self.db.issue.get(nodeid, 'nosy')
160         l.sort()
161         self.assertEqual(l, [self.chef_id, self.richard_id])
162         return nodeid
164     def testNewIssue(self):
165         self.doNewIssue()
167     def testNewIssueNosy(self):
168         self.instance.config.ADD_AUTHOR_TO_NOSY = 'yes'
169         nodeid = self._send_mail('''Content-Type: text/plain;
170   charset="iso-8859-1"
171 From: Chef <chef@bork.bork.bork>
172 To: issue_tracker@your.tracker.email.domain.example
173 Cc: richard@test
174 Message-Id: <dummy_test_message_id>
175 Subject: [issue] Testing...
177 This is a test submission of a new issue.
178 ''')
179         assert not os.path.exists(SENDMAILDEBUG)
180         l = self.db.issue.get(nodeid, 'nosy')
181         l.sort()
182         self.assertEqual(l, [self.chef_id, self.richard_id])
184     def testAlternateAddress(self):
185         self._send_mail('''Content-Type: text/plain;
186   charset="iso-8859-1"
187 From: John Doe <john.doe@test>
188 To: issue_tracker@your.tracker.email.domain.example
189 Message-Id: <dummy_test_message_id>
190 Subject: [issue] Testing...
192 This is a test submission of a new issue.
193 ''')
194         userlist = self.db.user.list()        
195         assert not os.path.exists(SENDMAILDEBUG)
196         self.assertEqual(userlist, self.db.user.list(),
197             "user created when it shouldn't have been")
199     def testNewIssueNoClass(self):
200         self._send_mail('''Content-Type: text/plain;
201   charset="iso-8859-1"
202 From: Chef <chef@bork.bork.bork>
203 To: issue_tracker@your.tracker.email.domain.example
204 Cc: richard@test
205 Message-Id: <dummy_test_message_id>
206 Subject: Testing...
208 This is a test submission of a new issue.
209 ''')
210         assert not os.path.exists(SENDMAILDEBUG)
212     def testNewIssueAuthMsg(self):
213         # TODO: fix the damn config - this is apalling
214         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
215         self._send_mail('''Content-Type: text/plain;
216   charset="iso-8859-1"
217 From: Chef <chef@bork.bork.bork>
218 To: issue_tracker@your.tracker.email.domain.example
219 Message-Id: <dummy_test_message_id>
220 Subject: [issue] Testing... [nosy=mary; assignedto=richard]
222 This is a test submission of a new issue.
223 ''')
224         self.compareMessages(self._get_mail(),
225 '''FROM: roundup-admin@your.tracker.email.domain.example
226 TO: chef@bork.bork.bork, mary@test, richard@test
227 Content-Type: text/plain; charset=utf-8
228 Subject: [issue1] Testing...
229 To: chef@bork.bork.bork, mary@test, richard@test
230 From: "Bork, Chef" <issue_tracker@your.tracker.email.domain.example>
231 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
232 MIME-Version: 1.0
233 Message-Id: <dummy_test_message_id>
234 X-Roundup-Name: Roundup issue tracker
235 X-Roundup-Loop: hello
236 Content-Transfer-Encoding: quoted-printable
239 New submission from Bork, Chef <chef@bork.bork.bork>:
241 This is a test submission of a new issue.
243 ----------
244 assignedto: richard
245 messages: 1
246 nosy: Chef, mary, richard
247 status: unread
248 title: Testing...
249 _______________________________________________________________________
250 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
251 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
252 _______________________________________________________________________
253 ''')
255     # BUG
256     # def testMultipart(self):
257     #         '''With more than one part'''
258     #        see MultipartEnc tests: but if there is more than one part
259     #        we return a multipart/mixed and the boundary contains
260     #        the ip address of the test machine. 
262     # BUG should test some binary attamchent too.
264     def testSimpleFollowup(self):
265         self.doNewIssue()
266         self._send_mail('''Content-Type: text/plain;
267   charset="iso-8859-1"
268 From: mary <mary@test>
269 To: issue_tracker@your.tracker.email.domain.example
270 Message-Id: <followup_dummy_id>
271 In-Reply-To: <dummy_test_message_id>
272 Subject: [issue1] Testing...
274 This is a second followup
275 ''')
276         self.compareMessages(self._get_mail(),
277 '''FROM: roundup-admin@your.tracker.email.domain.example
278 TO: chef@bork.bork.bork, richard@test
279 Content-Type: text/plain; charset=utf-8
280 Subject: [issue1] Testing...
281 To: chef@bork.bork.bork, richard@test
282 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
283 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
284 MIME-Version: 1.0
285 Message-Id: <followup_dummy_id>
286 In-Reply-To: <dummy_test_message_id>
287 X-Roundup-Name: Roundup issue tracker
288 X-Roundup-Loop: hello
289 Content-Transfer-Encoding: quoted-printable
292 Contrary, Mary <mary@test> added the comment:
294 This is a second followup
296 ----------
297 status: unread -> chatting
298 _______________________________________________________________________
299 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
300 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
301 _______________________________________________________________________
302 ''')
304     def testFollowup(self):
305         self.doNewIssue()
307         self._send_mail('''Content-Type: text/plain;
308   charset="iso-8859-1"
309 From: richard <richard@test>
310 To: issue_tracker@your.tracker.email.domain.example
311 Message-Id: <followup_dummy_id>
312 In-Reply-To: <dummy_test_message_id>
313 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
315 This is a followup
316 ''')
317         l = self.db.issue.get('1', 'nosy')
318         l.sort()
319         self.assertEqual(l, [self.chef_id, self.richard_id, self.mary_id,
320             self.john_id])
322         self.compareMessages(self._get_mail(),
323 '''FROM: roundup-admin@your.tracker.email.domain.example
324 TO: chef@bork.bork.bork, john@test, mary@test
325 Content-Type: text/plain; charset=utf-8
326 Subject: [issue1] Testing...
327 To: chef@bork.bork.bork, john@test, mary@test
328 From: richard <issue_tracker@your.tracker.email.domain.example>
329 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
330 MIME-Version: 1.0
331 Message-Id: <followup_dummy_id>
332 In-Reply-To: <dummy_test_message_id>
333 X-Roundup-Name: Roundup issue tracker
334 X-Roundup-Loop: hello
335 Content-Transfer-Encoding: quoted-printable
338 richard <richard@test> added the comment:
340 This is a followup
342 ----------
343 assignedto:  -> mary
344 nosy: +john, mary
345 status: unread -> chatting
346 _______________________________________________________________________
347 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
348 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
349 _______________________________________________________________________
350 ''')
352     def testFollowupTitleMatch(self):
353         self.doNewIssue()
354         self._send_mail('''Content-Type: text/plain;
355   charset="iso-8859-1"
356 From: richard <richard@test>
357 To: issue_tracker@your.tracker.email.domain.example
358 Message-Id: <followup_dummy_id>
359 In-Reply-To: <dummy_test_message_id>
360 Subject: Re: Testing... [assignedto=mary; nosy=+john]
362 This is a followup
363 ''')
364         self.compareMessages(self._get_mail(),
365 '''FROM: roundup-admin@your.tracker.email.domain.example
366 TO: chef@bork.bork.bork, john@test, mary@test
367 Content-Type: text/plain; charset=utf-8
368 Subject: [issue1] Testing...
369 To: chef@bork.bork.bork, john@test, mary@test
370 From: richard <issue_tracker@your.tracker.email.domain.example>
371 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
372 MIME-Version: 1.0
373 Message-Id: <followup_dummy_id>
374 In-Reply-To: <dummy_test_message_id>
375 X-Roundup-Name: Roundup issue tracker
376 X-Roundup-Loop: hello
377 Content-Transfer-Encoding: quoted-printable
380 richard <richard@test> added the comment:
382 This is a followup
384 ----------
385 assignedto:  -> mary
386 nosy: +john, mary
387 status: unread -> chatting
388 _______________________________________________________________________
389 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
390 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
391 _______________________________________________________________________
392 ''')
394     def testFollowupNosyAuthor(self):
395         self.doNewIssue()
396         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
397         self._send_mail('''Content-Type: text/plain;
398   charset="iso-8859-1"
399 From: john@test
400 To: issue_tracker@your.tracker.email.domain.example
401 Message-Id: <followup_dummy_id>
402 In-Reply-To: <dummy_test_message_id>
403 Subject: [issue1] Testing...
405 This is a followup
406 ''')
408         self.compareMessages(self._get_mail(),
409 '''FROM: roundup-admin@your.tracker.email.domain.example
410 TO: chef@bork.bork.bork, richard@test
411 Content-Type: text/plain; charset=utf-8
412 Subject: [issue1] Testing...
413 To: chef@bork.bork.bork, richard@test
414 From: John Doe <issue_tracker@your.tracker.email.domain.example>
415 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
416 MIME-Version: 1.0
417 Message-Id: <followup_dummy_id>
418 In-Reply-To: <dummy_test_message_id>
419 X-Roundup-Name: Roundup issue tracker
420 X-Roundup-Loop: hello
421 Content-Transfer-Encoding: quoted-printable
424 John Doe <john@test> added the comment:
426 This is a followup
428 ----------
429 nosy: +john
430 status: unread -> chatting
431 _______________________________________________________________________
432 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
433 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
434 _______________________________________________________________________
436 ''')
438     def testFollowupNosyRecipients(self):
439         self.doNewIssue()
440         self.db.config.ADD_RECIPIENTS_TO_NOSY = 'yes'
441         self._send_mail('''Content-Type: text/plain;
442   charset="iso-8859-1"
443 From: richard@test
444 To: issue_tracker@your.tracker.email.domain.example
445 Cc: john@test
446 Message-Id: <followup_dummy_id>
447 In-Reply-To: <dummy_test_message_id>
448 Subject: [issue1] Testing...
450 This is a followup
451 ''')
452         self.compareMessages(self._get_mail(),
453 '''FROM: roundup-admin@your.tracker.email.domain.example
454 TO: chef@bork.bork.bork
455 Content-Type: text/plain; charset=utf-8
456 Subject: [issue1] Testing...
457 To: chef@bork.bork.bork
458 From: richard <issue_tracker@your.tracker.email.domain.example>
459 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
460 MIME-Version: 1.0
461 Message-Id: <followup_dummy_id>
462 In-Reply-To: <dummy_test_message_id>
463 X-Roundup-Name: Roundup issue tracker
464 X-Roundup-Loop: hello
465 Content-Transfer-Encoding: quoted-printable
468 richard <richard@test> added the comment:
470 This is a followup
472 ----------
473 nosy: +john
474 status: unread -> chatting
475 _______________________________________________________________________
476 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
477 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
478 _______________________________________________________________________
480 ''')
482     def testFollowupNosyAuthorAndCopy(self):
483         self.doNewIssue()
484         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
485         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
486         self._send_mail('''Content-Type: text/plain;
487   charset="iso-8859-1"
488 From: john@test
489 To: issue_tracker@your.tracker.email.domain.example
490 Message-Id: <followup_dummy_id>
491 In-Reply-To: <dummy_test_message_id>
492 Subject: [issue1] Testing...
494 This is a followup
495 ''')
496         self.compareMessages(self._get_mail(),
497 '''FROM: roundup-admin@your.tracker.email.domain.example
498 TO: chef@bork.bork.bork, john@test, richard@test
499 Content-Type: text/plain; charset=utf-8
500 Subject: [issue1] Testing...
501 To: chef@bork.bork.bork, john@test, richard@test
502 From: John Doe <issue_tracker@your.tracker.email.domain.example>
503 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
504 MIME-Version: 1.0
505 Message-Id: <followup_dummy_id>
506 In-Reply-To: <dummy_test_message_id>
507 X-Roundup-Name: Roundup issue tracker
508 X-Roundup-Loop: hello
509 Content-Transfer-Encoding: quoted-printable
512 John Doe <john@test> added the comment:
514 This is a followup
516 ----------
517 nosy: +john
518 status: unread -> chatting
519 _______________________________________________________________________
520 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
521 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
522 _______________________________________________________________________
524 ''')
526     def testFollowupNoNosyAuthor(self):
527         self.doNewIssue()
528         self.instance.config.ADD_AUTHOR_TO_NOSY = 'no'
529         self._send_mail('''Content-Type: text/plain;
530   charset="iso-8859-1"
531 From: john@test
532 To: issue_tracker@your.tracker.email.domain.example
533 Message-Id: <followup_dummy_id>
534 In-Reply-To: <dummy_test_message_id>
535 Subject: [issue1] Testing...
537 This is a followup
538 ''')
539         self.compareMessages(self._get_mail(),
540 '''FROM: roundup-admin@your.tracker.email.domain.example
541 TO: chef@bork.bork.bork, richard@test
542 Content-Type: text/plain; charset=utf-8
543 Subject: [issue1] Testing...
544 To: chef@bork.bork.bork, richard@test
545 From: John Doe <issue_tracker@your.tracker.email.domain.example>
546 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
547 MIME-Version: 1.0
548 Message-Id: <followup_dummy_id>
549 In-Reply-To: <dummy_test_message_id>
550 X-Roundup-Name: Roundup issue tracker
551 X-Roundup-Loop: hello
552 Content-Transfer-Encoding: quoted-printable
555 John Doe <john@test> added the comment:
557 This is a followup
559 ----------
560 status: unread -> chatting
561 _______________________________________________________________________
562 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
563 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
564 _______________________________________________________________________
566 ''')
568     def testFollowupNoNosyRecipients(self):
569         self.doNewIssue()
570         self.instance.config.ADD_RECIPIENTS_TO_NOSY = 'no'
571         self._send_mail('''Content-Type: text/plain;
572   charset="iso-8859-1"
573 From: richard@test
574 To: issue_tracker@your.tracker.email.domain.example
575 Cc: john@test
576 Message-Id: <followup_dummy_id>
577 In-Reply-To: <dummy_test_message_id>
578 Subject: [issue1] Testing...
580 This is a followup
581 ''')
582         self.compareMessages(self._get_mail(),
583 '''FROM: roundup-admin@your.tracker.email.domain.example
584 TO: chef@bork.bork.bork
585 Content-Type: text/plain; charset=utf-8
586 Subject: [issue1] Testing...
587 To: chef@bork.bork.bork
588 From: richard <issue_tracker@your.tracker.email.domain.example>
589 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
590 MIME-Version: 1.0
591 Message-Id: <followup_dummy_id>
592 In-Reply-To: <dummy_test_message_id>
593 X-Roundup-Name: Roundup issue tracker
594 X-Roundup-Loop: hello
595 Content-Transfer-Encoding: quoted-printable
598 richard <richard@test> added the comment:
600 This is a followup
602 ----------
603 status: unread -> chatting
604 _______________________________________________________________________
605 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
606 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
607 _______________________________________________________________________
609 ''')
611     def testFollowupEmptyMessage(self):
612         self.doNewIssue()
614         self._send_mail('''Content-Type: text/plain;
615   charset="iso-8859-1"
616 From: richard <richard@test>
617 To: issue_tracker@your.tracker.email.domain.example
618 Message-Id: <followup_dummy_id>
619 In-Reply-To: <dummy_test_message_id>
620 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
622 ''')
623         l = self.db.issue.get('1', 'nosy')
624         l.sort()
625         self.assertEqual(l, [self.chef_id, self.richard_id, self.mary_id,
626             self.john_id])
628         # should be no file created (ie. no message)
629         assert not os.path.exists(SENDMAILDEBUG)
631     def testNosyRemove(self):
632         self.doNewIssue()
634         self._send_mail('''Content-Type: text/plain;
635   charset="iso-8859-1"
636 From: richard <richard@test>
637 To: issue_tracker@your.tracker.email.domain.example
638 Message-Id: <followup_dummy_id>
639 In-Reply-To: <dummy_test_message_id>
640 Subject: [issue1] Testing... [nosy=-richard]
642 ''')
643         l = self.db.issue.get('1', 'nosy')
644         l.sort()
645         self.assertEqual(l, [self.chef_id])
647         # NO NOSY MESSAGE SHOULD BE SENT!
648         assert not os.path.exists(SENDMAILDEBUG)
650     def testNewUserAuthor(self):
651         # first without the permission
652         # heh... just ignore the API for a second ;)
653         self.db.security.role['anonymous'].permissions=[]
654         anonid = self.db.user.lookup('anonymous')
655         self.db.user.set(anonid, roles='Anonymous')
657         self.db.security.hasPermission('Email Registration', anonid)
658         l = self.db.user.list()
659         l.sort()
660         message = '''Content-Type: text/plain;
661   charset="iso-8859-1"
662 From: fubar <fubar@bork.bork.bork>
663 To: issue_tracker@your.tracker.email.domain.example
664 Message-Id: <dummy_test_message_id>
665 Subject: [issue] Testing...
667 This is a test submission of a new issue.
668 '''
669         self.assertRaises(Unauthorized, self._send_mail, message)
670         m = self.db.user.list()
671         m.sort()
672         self.assertEqual(l, m)
674         # now with the permission
675         p = self.db.security.getPermission('Email Registration')
676         self.db.security.role['anonymous'].permissions=[p]
677         self._send_mail(message)
678         m = self.db.user.list()
679         m.sort()
680         self.assertNotEqual(l, m)
682     def testEnc01(self):
683         self.doNewIssue()
684         self._send_mail('''Content-Type: text/plain;
685   charset="iso-8859-1"
686 From: mary <mary@test>
687 To: issue_tracker@your.tracker.email.domain.example
688 Message-Id: <followup_dummy_id>
689 In-Reply-To: <dummy_test_message_id>
690 Subject: [issue1] Testing...
691 Content-Type: text/plain;
692         charset="iso-8859-1"
693 Content-Transfer-Encoding: quoted-printable
695 A message with encoding (encoded oe =F6)
697 ''')
698         self.compareMessages(self._get_mail(),
699 '''FROM: roundup-admin@your.tracker.email.domain.example
700 TO: chef@bork.bork.bork, richard@test
701 Content-Type: text/plain; charset=utf-8
702 Subject: [issue1] Testing...
703 To: chef@bork.bork.bork, richard@test
704 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
705 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
706 MIME-Version: 1.0
707 Message-Id: <followup_dummy_id>
708 In-Reply-To: <dummy_test_message_id>
709 X-Roundup-Name: Roundup issue tracker
710 X-Roundup-Loop: hello
711 Content-Transfer-Encoding: quoted-printable
714 Contrary, Mary <mary@test> added the comment:
716 A message with encoding (encoded oe =C3=B6)
718 ----------
719 status: unread -> chatting
720 _______________________________________________________________________
721 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
722 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
723 _______________________________________________________________________
724 ''')
727     def testMultipartEnc01(self):
728         self.doNewIssue()
729         self._send_mail('''Content-Type: text/plain;
730   charset="iso-8859-1"
731 From: mary <mary@test>
732 To: issue_tracker@your.tracker.email.domain.example
733 Message-Id: <followup_dummy_id>
734 In-Reply-To: <dummy_test_message_id>
735 Subject: [issue1] Testing...
736 Content-Type: multipart/mixed;
737         boundary="----_=_NextPart_000_01"
739 This message is in MIME format. Since your mail reader does not understand
740 this format, some or all of this message may not be legible.
742 ------_=_NextPart_000_01
743 Content-Type: text/plain;
744         charset="iso-8859-1"
745 Content-Transfer-Encoding: quoted-printable
747 A message with first part encoded (encoded oe =F6)
749 ''')
750         self.compareMessages(self._get_mail(),
751 '''FROM: roundup-admin@your.tracker.email.domain.example
752 TO: chef@bork.bork.bork, richard@test
753 Content-Type: text/plain; charset=utf-8
754 Subject: [issue1] Testing...
755 To: chef@bork.bork.bork, richard@test
756 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
757 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
758 MIME-Version: 1.0
759 Message-Id: <followup_dummy_id>
760 In-Reply-To: <dummy_test_message_id>
761 X-Roundup-Name: Roundup issue tracker
762 X-Roundup-Loop: hello
763 Content-Transfer-Encoding: quoted-printable
766 Contrary, Mary <mary@test> added the comment:
768 A message with first part encoded (encoded oe =C3=B6)
770 ----------
771 status: unread -> chatting
772 _______________________________________________________________________
773 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
774 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
775 _______________________________________________________________________
776 ''')
778     def testContentDisposition(self):
779         self.doNewIssue()
780         self._send_mail('''Content-Type: text/plain;
781   charset="iso-8859-1"
782 From: mary <mary@test>
783 To: issue_tracker@your.tracker.email.domain.example
784 Message-Id: <followup_dummy_id>
785 In-Reply-To: <dummy_test_message_id>
786 Subject: [issue1] Testing...
787 Content-Type: multipart/mixed; boundary="bCsyhTFzCvuiizWE" 
788 Content-Disposition: inline 
789  
790  
791 --bCsyhTFzCvuiizWE 
792 Content-Type: text/plain; charset=us-ascii 
793 Content-Disposition: inline 
795 test attachment binary 
797 --bCsyhTFzCvuiizWE 
798 Content-Type: application/octet-stream 
799 Content-Disposition: attachment; filename="main.dvi" 
801 xxxxxx 
803 --bCsyhTFzCvuiizWE--
804 ''')
805         messages = self.db.issue.get('1', 'messages')
806         messages.sort()
807         file = self.db.msg.get(messages[-1], 'files')[0]
808         self.assertEqual(self.db.file.get(file, 'name'), 'main.dvi')
810     def testFollowupStupidQuoting(self):
811         self.doNewIssue()
813         self._send_mail('''Content-Type: text/plain;
814   charset="iso-8859-1"
815 From: richard <richard@test>
816 To: issue_tracker@your.tracker.email.domain.example
817 Message-Id: <followup_dummy_id>
818 In-Reply-To: <dummy_test_message_id>
819 Subject: Re: "[issue1] Testing... "
821 This is a followup
822 ''')
823         self.compareMessages(self._get_mail(),
824 '''FROM: roundup-admin@your.tracker.email.domain.example
825 TO: chef@bork.bork.bork
826 Content-Type: text/plain; charset=utf-8
827 Subject: [issue1] Testing...
828 To: chef@bork.bork.bork
829 From: richard <issue_tracker@your.tracker.email.domain.example>
830 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
831 MIME-Version: 1.0
832 Message-Id: <followup_dummy_id>
833 In-Reply-To: <dummy_test_message_id>
834 X-Roundup-Name: Roundup issue tracker
835 X-Roundup-Loop: hello
836 Content-Transfer-Encoding: quoted-printable
839 richard <richard@test> added the comment:
841 This is a followup
843 ----------
844 status: unread -> chatting
845 _______________________________________________________________________
846 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
847 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
848 _______________________________________________________________________
849 ''')
851     def testEmailQuoting(self):
852         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'no'
853         self.innerTestQuoting('''This is a followup
854 ''')
856     def testEmailQuotingRemove(self):
857         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'yes'
858         self.innerTestQuoting('''Blah blah wrote:
859 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
860 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
863 This is a followup
864 ''')
866     def innerTestQuoting(self, expect):
867         nodeid = self.doNewIssue()
869         messages = self.db.issue.get(nodeid, 'messages')
871         self._send_mail('''Content-Type: text/plain;
872   charset="iso-8859-1"
873 From: richard <richard@test>
874 To: issue_tracker@your.tracker.email.domain.example
875 Message-Id: <followup_dummy_id>
876 In-Reply-To: <dummy_test_message_id>
877 Subject: Re: [issue1] Testing...
879 Blah blah wrote:
880 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
881 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
884 This is a followup
885 ''')
886         # figure the new message id
887         newmessages = self.db.issue.get(nodeid, 'messages')
888         for msg in messages:
889             newmessages.remove(msg)
890         messageid = newmessages[0]
892         self.compareMessages(self.db.msg.get(messageid, 'content'), expect)
894     def testUserLookup(self):
895         i = self.db.user.create(username='user1', address='user1@foo.com')
896         self.assertEqual(uidFromAddress(self.db, ('', 'user1@foo.com'), 0), i)
897         self.assertEqual(uidFromAddress(self.db, ('', 'USER1@foo.com'), 0), i)
898         i = self.db.user.create(username='user2', address='USER2@foo.com')
899         self.assertEqual(uidFromAddress(self.db, ('', 'USER2@foo.com'), 0), i)
900         self.assertEqual(uidFromAddress(self.db, ('', 'user2@foo.com'), 0), i)
902     def testUserAlternateLookup(self):
903         i = self.db.user.create(username='user1', address='user1@foo.com',
904                                 alternate_addresses='user1@bar.com')
905         self.assertEqual(uidFromAddress(self.db, ('', 'user1@bar.com'), 0), i)
906         self.assertEqual(uidFromAddress(self.db, ('', 'USER1@bar.com'), 0), i)
908     def testUserCreate(self):
909         i = uidFromAddress(self.db, ('', 'user@foo.com'), 1)
910         self.assertNotEqual(uidFromAddress(self.db, ('', 'user@bar.com'), 1), i)
912     def testRFC2822(self):
913         ascii_header = "[issue243] This is a \"test\" - with 'quotation' marks"
914         unicode_header = '[issue244] \xd0\xb0\xd0\xbd\xd0\xb4\xd1\x80\xd0\xb5\xd0\xb9'
915         unicode_encoded = '=?utf-8?q?[issue244]_=D0=B0=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?='
916         self.assertEqual(rfc2822.encode_header(ascii_header), ascii_header)
917         self.assertEqual(rfc2822.encode_header(unicode_header), unicode_encoded)
919     def testRegistrationConfirmation(self):
920         otk = "Aj4euk4LZSAdwePohj90SME5SpopLETL"
921         self.db.otks.set(otk, username='johannes', __time='')
922         self._send_mail('''Content-Type: text/plain;
923   charset="iso-8859-1"
924 From: Chef <chef@bork.bork.bork>
925 To: issue_tracker@your.tracker.email.domain.example
926 Cc: richard@test
927 Message-Id: <dummy_test_message_id>
928 Subject: Re: Complete your registration to Roundup issue tracker\r
929  -- key %s
931 This is a test confirmation of registration.
932 ''' % otk)
933         self.db.user.lookup('johannes')
935     def testFollowupOnNonIssue(self):
936         self.db.keyword.create(name='Foo')
937         self._send_mail('''Content-Type: text/plain;
938   charset="iso-8859-1"
939 From: richard <richard@test>
940 To: issue_tracker@your.tracker.email.domain.example
941 Message-Id: <followup_dummy_id>
942 In-Reply-To: <dummy_test_message_id>
943 Subject: [keyword1] Testing... [name=Bar]
945 ''')        
946         self.assertEqual(self.db.keyword.get('1', 'name'), 'Bar')
948     def testResentFrom(self):
949         nodeid = self._send_mail('''Content-Type: text/plain;
950   charset="iso-8859-1"
951 From: Chef <chef@bork.bork.bork>
952 Resent-From: mary <mary@test>
953 To: issue_tracker@your.tracker.email.domain.example
954 Cc: richard@test
955 Message-Id: <dummy_test_message_id>
956 Subject: [issue] Testing...
958 This is a test submission of a new issue.
959 ''')
960         assert not os.path.exists(SENDMAILDEBUG)
961         l = self.db.issue.get(nodeid, 'nosy')
962         l.sort()
963         self.assertEqual(l, [self.richard_id, self.mary_id])
964         return nodeid
967     def testDejaVu(self):
968         self.assertRaises(IgnoreLoop, self._send_mail,
969             '''Content-Type: text/plain;
970   charset="iso-8859-1"
971 From: Chef <chef@bork.bork.bork>
972 X-Roundup-Loop: hello
973 To: issue_tracker@your.tracker.email.domain.example
974 Cc: richard@test
975 Message-Id: <dummy_test_message_id>
976 Subject: Re: [issue] Testing...
978 Hi, I've been mis-configured to loop messages back to myself.
979 ''')
981     def testItsBulkStupid(self):
982         self.assertRaises(IgnoreBulk, self._send_mail,
983             '''Content-Type: text/plain;
984   charset="iso-8859-1"
985 From: Chef <chef@bork.bork.bork>
986 Precedence: bulk
987 To: issue_tracker@your.tracker.email.domain.example
988 Cc: richard@test
989 Message-Id: <dummy_test_message_id>
990 Subject: Re: [issue] Testing...
992 Hi, I'm on holidays, and this is a dumb auto-responder.
993 ''')
995 def test_suite():
996     suite = unittest.TestSuite()
997     suite.addTest(unittest.makeSuite(MailgwTestCase))
998     return suite
1000 if __name__ == '__main__':
1001     runner = unittest.TextTestRunner()
1002     unittest.main(testRunner=runner)
1004 # vim: set filetype=python ts=4 sw=4 et si