Code

Resent-From: header
[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.62 2003-11-13 03:41:38 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, 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')
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.chef_id = self.db.user.create(username='Chef',
103             address='chef@bork.bork.bork', realname='Bork, Chef', roles='User')
104         self.richard_id = self.db.user.create(username='richard',
105             address='richard@test', roles='User')
106         self.mary_id = self.db.user.create(username='mary', address='mary@test',
107             roles='User', realname='Contrary, Mary')
108         self.john_id = 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, [self.chef_id, self.richard_id])
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, [self.chef_id, self.richard_id])
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, [self.chef_id, self.richard_id, self.mary_id,
319             self.john_id])
321         self.compareMessages(self._get_mail(),
322 '''FROM: roundup-admin@your.tracker.email.domain.example
323 TO: chef@bork.bork.bork, john@test, mary@test
324 Content-Type: text/plain; charset=utf-8
325 Subject: [issue1] Testing...
326 To: chef@bork.bork.bork, john@test, mary@test
327 From: richard <issue_tracker@your.tracker.email.domain.example>
328 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
329 MIME-Version: 1.0
330 Message-Id: <followup_dummy_id>
331 In-Reply-To: <dummy_test_message_id>
332 X-Roundup-Name: Roundup issue tracker
333 X-Roundup-Loop: hello
334 Content-Transfer-Encoding: quoted-printable
337 richard <richard@test> added the comment:
339 This is a followup
341 ----------
342 assignedto:  -> mary
343 nosy: +john, mary
344 status: unread -> chatting
345 _______________________________________________________________________
346 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
347 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
348 _______________________________________________________________________
349 ''')
351     def testFollowupTitleMatch(self):
352         self.doNewIssue()
353         self._send_mail('''Content-Type: text/plain;
354   charset="iso-8859-1"
355 From: richard <richard@test>
356 To: issue_tracker@your.tracker.email.domain.example
357 Message-Id: <followup_dummy_id>
358 In-Reply-To: <dummy_test_message_id>
359 Subject: Re: Testing... [assignedto=mary; nosy=+john]
361 This is a followup
362 ''')
363         self.compareMessages(self._get_mail(),
364 '''FROM: roundup-admin@your.tracker.email.domain.example
365 TO: chef@bork.bork.bork, john@test, mary@test
366 Content-Type: text/plain; charset=utf-8
367 Subject: [issue1] Testing...
368 To: chef@bork.bork.bork, john@test, mary@test
369 From: richard <issue_tracker@your.tracker.email.domain.example>
370 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
371 MIME-Version: 1.0
372 Message-Id: <followup_dummy_id>
373 In-Reply-To: <dummy_test_message_id>
374 X-Roundup-Name: Roundup issue tracker
375 X-Roundup-Loop: hello
376 Content-Transfer-Encoding: quoted-printable
379 richard <richard@test> added the comment:
381 This is a followup
383 ----------
384 assignedto:  -> mary
385 nosy: +john, mary
386 status: unread -> chatting
387 _______________________________________________________________________
388 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
389 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
390 _______________________________________________________________________
391 ''')
393     def testFollowupNosyAuthor(self):
394         self.doNewIssue()
395         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
396         self._send_mail('''Content-Type: text/plain;
397   charset="iso-8859-1"
398 From: john@test
399 To: issue_tracker@your.tracker.email.domain.example
400 Message-Id: <followup_dummy_id>
401 In-Reply-To: <dummy_test_message_id>
402 Subject: [issue1] Testing...
404 This is a followup
405 ''')
407         self.compareMessages(self._get_mail(),
408 '''FROM: roundup-admin@your.tracker.email.domain.example
409 TO: chef@bork.bork.bork, richard@test
410 Content-Type: text/plain; charset=utf-8
411 Subject: [issue1] Testing...
412 To: chef@bork.bork.bork, richard@test
413 From: John Doe <issue_tracker@your.tracker.email.domain.example>
414 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
415 MIME-Version: 1.0
416 Message-Id: <followup_dummy_id>
417 In-Reply-To: <dummy_test_message_id>
418 X-Roundup-Name: Roundup issue tracker
419 X-Roundup-Loop: hello
420 Content-Transfer-Encoding: quoted-printable
423 John Doe <john@test> added the comment:
425 This is a followup
427 ----------
428 nosy: +john
429 status: unread -> chatting
430 _______________________________________________________________________
431 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
432 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
433 _______________________________________________________________________
435 ''')
437     def testFollowupNosyRecipients(self):
438         self.doNewIssue()
439         self.db.config.ADD_RECIPIENTS_TO_NOSY = 'yes'
440         self._send_mail('''Content-Type: text/plain;
441   charset="iso-8859-1"
442 From: richard@test
443 To: issue_tracker@your.tracker.email.domain.example
444 Cc: john@test
445 Message-Id: <followup_dummy_id>
446 In-Reply-To: <dummy_test_message_id>
447 Subject: [issue1] Testing...
449 This is a followup
450 ''')
451         self.compareMessages(self._get_mail(),
452 '''FROM: roundup-admin@your.tracker.email.domain.example
453 TO: chef@bork.bork.bork
454 Content-Type: text/plain; charset=utf-8
455 Subject: [issue1] Testing...
456 To: chef@bork.bork.bork
457 From: richard <issue_tracker@your.tracker.email.domain.example>
458 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
459 MIME-Version: 1.0
460 Message-Id: <followup_dummy_id>
461 In-Reply-To: <dummy_test_message_id>
462 X-Roundup-Name: Roundup issue tracker
463 X-Roundup-Loop: hello
464 Content-Transfer-Encoding: quoted-printable
467 richard <richard@test> added the comment:
469 This is a followup
471 ----------
472 nosy: +john
473 status: unread -> chatting
474 _______________________________________________________________________
475 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
476 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
477 _______________________________________________________________________
479 ''')
481     def testFollowupNosyAuthorAndCopy(self):
482         self.doNewIssue()
483         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
484         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
485         self._send_mail('''Content-Type: text/plain;
486   charset="iso-8859-1"
487 From: john@test
488 To: issue_tracker@your.tracker.email.domain.example
489 Message-Id: <followup_dummy_id>
490 In-Reply-To: <dummy_test_message_id>
491 Subject: [issue1] Testing...
493 This is a followup
494 ''')
495         self.compareMessages(self._get_mail(),
496 '''FROM: roundup-admin@your.tracker.email.domain.example
497 TO: chef@bork.bork.bork, john@test, richard@test
498 Content-Type: text/plain; charset=utf-8
499 Subject: [issue1] Testing...
500 To: chef@bork.bork.bork, john@test, richard@test
501 From: John Doe <issue_tracker@your.tracker.email.domain.example>
502 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
503 MIME-Version: 1.0
504 Message-Id: <followup_dummy_id>
505 In-Reply-To: <dummy_test_message_id>
506 X-Roundup-Name: Roundup issue tracker
507 X-Roundup-Loop: hello
508 Content-Transfer-Encoding: quoted-printable
511 John Doe <john@test> added the comment:
513 This is a followup
515 ----------
516 nosy: +john
517 status: unread -> chatting
518 _______________________________________________________________________
519 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
520 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
521 _______________________________________________________________________
523 ''')
525     def testFollowupNoNosyAuthor(self):
526         self.doNewIssue()
527         self.instance.config.ADD_AUTHOR_TO_NOSY = 'no'
528         self._send_mail('''Content-Type: text/plain;
529   charset="iso-8859-1"
530 From: john@test
531 To: issue_tracker@your.tracker.email.domain.example
532 Message-Id: <followup_dummy_id>
533 In-Reply-To: <dummy_test_message_id>
534 Subject: [issue1] Testing...
536 This is a followup
537 ''')
538         self.compareMessages(self._get_mail(),
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         self._send_mail('''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         self.compareMessages(self._get_mail(),
582 '''FROM: roundup-admin@your.tracker.email.domain.example
583 TO: chef@bork.bork.bork
584 Content-Type: text/plain; charset=utf-8
585 Subject: [issue1] Testing...
586 To: chef@bork.bork.bork
587 From: richard <issue_tracker@your.tracker.email.domain.example>
588 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
589 MIME-Version: 1.0
590 Message-Id: <followup_dummy_id>
591 In-Reply-To: <dummy_test_message_id>
592 X-Roundup-Name: Roundup issue tracker
593 X-Roundup-Loop: hello
594 Content-Transfer-Encoding: quoted-printable
597 richard <richard@test> added the comment:
599 This is a followup
601 ----------
602 status: unread -> chatting
603 _______________________________________________________________________
604 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
605 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
606 _______________________________________________________________________
608 ''')
610     def testFollowupEmptyMessage(self):
611         self.doNewIssue()
613         self._send_mail('''Content-Type: text/plain;
614   charset="iso-8859-1"
615 From: richard <richard@test>
616 To: issue_tracker@your.tracker.email.domain.example
617 Message-Id: <followup_dummy_id>
618 In-Reply-To: <dummy_test_message_id>
619 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
621 ''')
622         l = self.db.issue.get('1', 'nosy')
623         l.sort()
624         self.assertEqual(l, [self.chef_id, self.richard_id, self.mary_id,
625             self.john_id])
627         # should be no file created (ie. no message)
628         assert not os.path.exists(SENDMAILDEBUG)
630     def testNosyRemove(self):
631         self.doNewIssue()
633         self._send_mail('''Content-Type: text/plain;
634   charset="iso-8859-1"
635 From: richard <richard@test>
636 To: issue_tracker@your.tracker.email.domain.example
637 Message-Id: <followup_dummy_id>
638 In-Reply-To: <dummy_test_message_id>
639 Subject: [issue1] Testing... [nosy=-richard]
641 ''')
642         l = self.db.issue.get('1', 'nosy')
643         l.sort()
644         self.assertEqual(l, [self.chef_id])
646         # NO NOSY MESSAGE SHOULD BE SENT!
647         assert not os.path.exists(SENDMAILDEBUG)
649     def testNewUserAuthor(self):
650         # first without the permission
651         # heh... just ignore the API for a second ;)
652         self.db.security.role['anonymous'].permissions=[]
653         anonid = self.db.user.lookup('anonymous')
654         self.db.user.set(anonid, roles='Anonymous')
656         self.db.security.hasPermission('Email Registration', anonid)
657         l = self.db.user.list()
658         l.sort()
659         message = '''Content-Type: text/plain;
660   charset="iso-8859-1"
661 From: fubar <fubar@bork.bork.bork>
662 To: issue_tracker@your.tracker.email.domain.example
663 Message-Id: <dummy_test_message_id>
664 Subject: [issue] Testing...
666 This is a test submission of a new issue.
667 '''
668         self.assertRaises(Unauthorized, self._send_mail, message)
669         m = self.db.user.list()
670         m.sort()
671         self.assertEqual(l, m)
673         # now with the permission
674         p = self.db.security.getPermission('Email Registration')
675         self.db.security.role['anonymous'].permissions=[p]
676         self._send_mail(message)
677         m = self.db.user.list()
678         m.sort()
679         self.assertNotEqual(l, m)
681     def testEnc01(self):
682         self.doNewIssue()
683         self._send_mail('''Content-Type: text/plain;
684   charset="iso-8859-1"
685 From: mary <mary@test>
686 To: issue_tracker@your.tracker.email.domain.example
687 Message-Id: <followup_dummy_id>
688 In-Reply-To: <dummy_test_message_id>
689 Subject: [issue1] Testing...
690 Content-Type: text/plain;
691         charset="iso-8859-1"
692 Content-Transfer-Encoding: quoted-printable
694 A message with encoding (encoded oe =F6)
696 ''')
697         self.compareMessages(self._get_mail(),
698 '''FROM: roundup-admin@your.tracker.email.domain.example
699 TO: chef@bork.bork.bork, richard@test
700 Content-Type: text/plain; charset=utf-8
701 Subject: [issue1] Testing...
702 To: chef@bork.bork.bork, richard@test
703 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
704 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
705 MIME-Version: 1.0
706 Message-Id: <followup_dummy_id>
707 In-Reply-To: <dummy_test_message_id>
708 X-Roundup-Name: Roundup issue tracker
709 X-Roundup-Loop: hello
710 Content-Transfer-Encoding: quoted-printable
713 Contrary, Mary <mary@test> added the comment:
715 A message with encoding (encoded oe =C3=B6)
717 ----------
718 status: unread -> chatting
719 _______________________________________________________________________
720 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
721 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
722 _______________________________________________________________________
723 ''')
726     def testMultipartEnc01(self):
727         self.doNewIssue()
728         self._send_mail('''Content-Type: text/plain;
729   charset="iso-8859-1"
730 From: mary <mary@test>
731 To: issue_tracker@your.tracker.email.domain.example
732 Message-Id: <followup_dummy_id>
733 In-Reply-To: <dummy_test_message_id>
734 Subject: [issue1] Testing...
735 Content-Type: multipart/mixed;
736         boundary="----_=_NextPart_000_01"
738 This message is in MIME format. Since your mail reader does not understand
739 this format, some or all of this message may not be legible.
741 ------_=_NextPart_000_01
742 Content-Type: text/plain;
743         charset="iso-8859-1"
744 Content-Transfer-Encoding: quoted-printable
746 A message with first part encoded (encoded oe =F6)
748 ''')
749         self.compareMessages(self._get_mail(),
750 '''FROM: roundup-admin@your.tracker.email.domain.example
751 TO: chef@bork.bork.bork, richard@test
752 Content-Type: text/plain; charset=utf-8
753 Subject: [issue1] Testing...
754 To: chef@bork.bork.bork, richard@test
755 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
756 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
757 MIME-Version: 1.0
758 Message-Id: <followup_dummy_id>
759 In-Reply-To: <dummy_test_message_id>
760 X-Roundup-Name: Roundup issue tracker
761 X-Roundup-Loop: hello
762 Content-Transfer-Encoding: quoted-printable
765 Contrary, Mary <mary@test> added the comment:
767 A message with first part encoded (encoded oe =C3=B6)
769 ----------
770 status: unread -> chatting
771 _______________________________________________________________________
772 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
773 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
774 _______________________________________________________________________
775 ''')
777     def testContentDisposition(self):
778         self.doNewIssue()
779         self._send_mail('''Content-Type: text/plain;
780   charset="iso-8859-1"
781 From: mary <mary@test>
782 To: issue_tracker@your.tracker.email.domain.example
783 Message-Id: <followup_dummy_id>
784 In-Reply-To: <dummy_test_message_id>
785 Subject: [issue1] Testing...
786 Content-Type: multipart/mixed; boundary="bCsyhTFzCvuiizWE" 
787 Content-Disposition: inline 
788  
789  
790 --bCsyhTFzCvuiizWE 
791 Content-Type: text/plain; charset=us-ascii 
792 Content-Disposition: inline 
794 test attachment binary 
796 --bCsyhTFzCvuiizWE 
797 Content-Type: application/octet-stream 
798 Content-Disposition: attachment; filename="main.dvi" 
800 xxxxxx 
802 --bCsyhTFzCvuiizWE--
803 ''')
804         messages = self.db.issue.get('1', 'messages')
805         messages.sort()
806         file = self.db.msg.get(messages[-1], 'files')[0]
807         self.assertEqual(self.db.file.get(file, 'name'), 'main.dvi')
809     def testFollowupStupidQuoting(self):
810         self.doNewIssue()
812         self._send_mail('''Content-Type: text/plain;
813   charset="iso-8859-1"
814 From: richard <richard@test>
815 To: issue_tracker@your.tracker.email.domain.example
816 Message-Id: <followup_dummy_id>
817 In-Reply-To: <dummy_test_message_id>
818 Subject: Re: "[issue1] Testing... "
820 This is a followup
821 ''')
822         self.compareMessages(self._get_mail(),
823 '''FROM: roundup-admin@your.tracker.email.domain.example
824 TO: chef@bork.bork.bork
825 Content-Type: text/plain; charset=utf-8
826 Subject: [issue1] Testing...
827 To: chef@bork.bork.bork
828 From: richard <issue_tracker@your.tracker.email.domain.example>
829 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
830 MIME-Version: 1.0
831 Message-Id: <followup_dummy_id>
832 In-Reply-To: <dummy_test_message_id>
833 X-Roundup-Name: Roundup issue tracker
834 X-Roundup-Loop: hello
835 Content-Transfer-Encoding: quoted-printable
838 richard <richard@test> added the comment:
840 This is a followup
842 ----------
843 status: unread -> chatting
844 _______________________________________________________________________
845 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
846 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
847 _______________________________________________________________________
848 ''')
850     def testEmailQuoting(self):
851         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'no'
852         self.innerTestQuoting('''This is a followup
853 ''')
855     def testEmailQuotingRemove(self):
856         self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'yes'
857         self.innerTestQuoting('''Blah blah wrote:
858 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
859 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
862 This is a followup
863 ''')
865     def innerTestQuoting(self, expect):
866         nodeid = self.doNewIssue()
868         messages = self.db.issue.get(nodeid, 'messages')
870         self._send_mail('''Content-Type: text/plain;
871   charset="iso-8859-1"
872 From: richard <richard@test>
873 To: issue_tracker@your.tracker.email.domain.example
874 Message-Id: <followup_dummy_id>
875 In-Reply-To: <dummy_test_message_id>
876 Subject: Re: [issue1] Testing...
878 Blah blah wrote:
879 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
880 >  skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
883 This is a followup
884 ''')
885         # figure the new message id
886         newmessages = self.db.issue.get(nodeid, 'messages')
887         for msg in messages:
888             newmessages.remove(msg)
889         messageid = newmessages[0]
891         self.compareMessages(self.db.msg.get(messageid, 'content'), expect)
893     def testUserLookup(self):
894         i = self.db.user.create(username='user1', address='user1@foo.com')
895         self.assertEqual(uidFromAddress(self.db, ('', 'user1@foo.com'), 0), i)
896         self.assertEqual(uidFromAddress(self.db, ('', 'USER1@foo.com'), 0), i)
897         i = self.db.user.create(username='user2', address='USER2@foo.com')
898         self.assertEqual(uidFromAddress(self.db, ('', 'USER2@foo.com'), 0), i)
899         self.assertEqual(uidFromAddress(self.db, ('', 'user2@foo.com'), 0), i)
901     def testUserAlternateLookup(self):
902         i = self.db.user.create(username='user1', address='user1@foo.com',
903                                 alternate_addresses='user1@bar.com')
904         self.assertEqual(uidFromAddress(self.db, ('', 'user1@bar.com'), 0), i)
905         self.assertEqual(uidFromAddress(self.db, ('', 'USER1@bar.com'), 0), i)
907     def testUserCreate(self):
908         i = uidFromAddress(self.db, ('', 'user@foo.com'), 1)
909         self.assertNotEqual(uidFromAddress(self.db, ('', 'user@bar.com'), 1), i)
911     def testRFC2822(self):
912         ascii_header = "[issue243] This is a \"test\" - with 'quotation' marks"
913         unicode_header = '[issue244] \xd0\xb0\xd0\xbd\xd0\xb4\xd1\x80\xd0\xb5\xd0\xb9'
914         unicode_encoded = '=?utf-8?q?[issue244]_=D0=B0=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?='
915         self.assertEqual(rfc2822.encode_header(ascii_header), ascii_header)
916         self.assertEqual(rfc2822.encode_header(unicode_header), unicode_encoded)
918     def testRegistrationConfirmation(self):
919         otk = "Aj4euk4LZSAdwePohj90SME5SpopLETL"
920         self.db.otks.set(otk, username='johannes', __time='')
921         self._send_mail('''Content-Type: text/plain;
922   charset="iso-8859-1"
923 From: Chef <chef@bork.bork.bork>
924 To: issue_tracker@your.tracker.email.domain.example
925 Cc: richard@test
926 Message-Id: <dummy_test_message_id>
927 Subject: Re: Complete your registration to Roundup issue tracker\r
928  -- key %s
930 This is a test confirmation of registration.
931 ''' % otk)
932         self.db.user.lookup('johannes')
934     def testFollowupOnNonIssue(self):
935         self.db.keyword.create(name='Foo')
936         self._send_mail('''Content-Type: text/plain;
937   charset="iso-8859-1"
938 From: richard <richard@test>
939 To: issue_tracker@your.tracker.email.domain.example
940 Message-Id: <followup_dummy_id>
941 In-Reply-To: <dummy_test_message_id>
942 Subject: [keyword1] Testing... [name=Bar]
944 ''')        
945         self.assertEqual(self.db.keyword.get('1', 'name'), 'Bar')
947     def testResentFrom(self):
948         nodeid = self._send_mail('''Content-Type: text/plain;
949   charset="iso-8859-1"
950 From: Chef <chef@bork.bork.bork>
951 Resent-From: mary <mary@test>
952 To: issue_tracker@your.tracker.email.domain.example
953 Cc: richard@test
954 Message-Id: <dummy_test_message_id>
955 Subject: [issue] Testing...
957 This is a test submission of a new issue.
958 ''')
959         assert not os.path.exists(SENDMAILDEBUG)
960         l = self.db.issue.get(nodeid, 'nosy')
961         l.sort()
962         self.assertEqual(l, [self.richard_id, self.mary_id])
963         return nodeid
964     
965 def test_suite():
966     suite = unittest.TestSuite()
967     suite.addTest(unittest.makeSuite(MailgwTestCase))
968     return suite
970 if __name__ == '__main__':
971     runner = unittest.TextTestRunner()
972     unittest.main(testRunner=runner)
974 # vim: set filetype=python ts=4 sw=4 et si