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.66 2004-03-22 07:45:40 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, __version__
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()))
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 = []
44 for key in new.keys():
45 if key.lower() == 'x-roundup-version':
46 # version changes constantly, so handle it specially
47 if new[key] != __version__:
48 res.append(' %s: %s != %s' % (key, __version__,
49 new[key]))
50 elif new[key] != old[key]:
51 res.append(' %s: %s != %s' % (key, old[key], new[key]))
53 body_diff = self.compareStrings(new.fp.read(), old.fp.read())
54 if body_diff:
55 res.append('')
56 res.extend(body_diff)
58 if res:
59 res.insert(0, 'Generated message not correct (diff follows):')
60 raise AssertionError, '\n'.join(res)
62 def compareStrings(self, s2, s1):
63 '''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
64 the first to be the "original" but in the calls in this file,
65 the second arg is the original. Ho hum.
66 '''
67 l1 = s1.strip().split('\n')
68 l2 = s2.strip().split('\n')
69 if l1 == l2:
70 return
71 s = difflib.SequenceMatcher(None, l1, l2)
72 res = []
73 for value, s1s, s1e, s2s, s2e in s.get_opcodes():
74 if value == 'equal':
75 for i in range(s1s, s1e):
76 res.append(' %s'%l1[i])
77 elif value == 'delete':
78 for i in range(s1s, s1e):
79 res.append('- %s'%l1[i])
80 elif value == 'insert':
81 for i in range(s2s, s2e):
82 res.append('+ %s'%l2[i])
83 elif value == 'replace':
84 for i, j in zip(range(s1s, s1e), range(s2s, s2e)):
85 res.append('- %s'%l1[i])
86 res.append('+ %s'%l2[j])
88 return res
90 class MailgwTestCase(unittest.TestCase, DiffHelper):
91 count = 0
92 schema = 'classic'
93 def setUp(self):
94 MailgwTestCase.count = MailgwTestCase.count + 1
95 self.dirname = '_test_mailgw_%s'%self.count
96 try:
97 shutil.rmtree(self.dirname)
98 except OSError, error:
99 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
100 # create the instance
101 init.install(self.dirname, 'templates/classic')
102 init.write_select_db(self.dirname, 'anydbm')
103 init.initialise(self.dirname, 'sekrit')
105 # check we can load the package
106 self.instance = instance.open(self.dirname)
108 # and open the database
109 self.db = self.instance.open('admin')
110 self.chef_id = self.db.user.create(username='Chef',
111 address='chef@bork.bork.bork', realname='Bork, Chef', roles='User')
112 self.richard_id = self.db.user.create(username='richard',
113 address='richard@test', roles='User')
114 self.mary_id = self.db.user.create(username='mary', address='mary@test',
115 roles='User', realname='Contrary, Mary')
116 self.john_id = self.db.user.create(username='john', address='john@test',
117 alternate_addresses='jondoe@test\njohn.doe@test', roles='User',
118 realname='John Doe')
120 def tearDown(self):
121 if os.path.exists(SENDMAILDEBUG):
122 os.remove(SENDMAILDEBUG)
123 self.db.close()
124 try:
125 shutil.rmtree(self.dirname)
126 except OSError, error:
127 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
129 def _handle_mail(self, message):
130 handler = self.instance.MailGW(self.instance, self.db)
131 handler.trapExceptions = 0
132 return handler.main(StringIO(message))
134 def _get_mail(self):
135 f = open(SENDMAILDEBUG)
136 try:
137 return f.read()
138 finally:
139 f.close()
141 def testEmptyMessage(self):
142 nodeid = self._handle_mail('''Content-Type: text/plain;
143 charset="iso-8859-1"
144 From: Chef <chef@bork.bork.bork>
145 To: issue_tracker@your.tracker.email.domain.example
146 Cc: richard@test
147 Reply-To: chef@bork.bork.bork
148 Message-Id: <dummy_test_message_id>
149 Subject: [issue] Testing...
151 ''')
152 assert not os.path.exists(SENDMAILDEBUG)
153 self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...')
155 def doNewIssue(self):
156 nodeid = self._handle_mail('''Content-Type: text/plain;
157 charset="iso-8859-1"
158 From: Chef <chef@bork.bork.bork>
159 To: issue_tracker@your.tracker.email.domain.example
160 Cc: richard@test
161 Message-Id: <dummy_test_message_id>
162 Subject: [issue] Testing...
164 This is a test submission of a new issue.
165 ''')
166 assert not os.path.exists(SENDMAILDEBUG)
167 l = self.db.issue.get(nodeid, 'nosy')
168 l.sort()
169 self.assertEqual(l, [self.chef_id, self.richard_id])
170 return nodeid
172 def testNewIssue(self):
173 self.doNewIssue()
175 def testNewIssueNosy(self):
176 self.instance.config.ADD_AUTHOR_TO_NOSY = 'yes'
177 nodeid = self._handle_mail('''Content-Type: text/plain;
178 charset="iso-8859-1"
179 From: Chef <chef@bork.bork.bork>
180 To: issue_tracker@your.tracker.email.domain.example
181 Cc: richard@test
182 Message-Id: <dummy_test_message_id>
183 Subject: [issue] Testing...
185 This is a test submission of a new issue.
186 ''')
187 assert not os.path.exists(SENDMAILDEBUG)
188 l = self.db.issue.get(nodeid, 'nosy')
189 l.sort()
190 self.assertEqual(l, [self.chef_id, self.richard_id])
192 def testAlternateAddress(self):
193 self._handle_mail('''Content-Type: text/plain;
194 charset="iso-8859-1"
195 From: John Doe <john.doe@test>
196 To: issue_tracker@your.tracker.email.domain.example
197 Message-Id: <dummy_test_message_id>
198 Subject: [issue] Testing...
200 This is a test submission of a new issue.
201 ''')
202 userlist = self.db.user.list()
203 assert not os.path.exists(SENDMAILDEBUG)
204 self.assertEqual(userlist, self.db.user.list(),
205 "user created when it shouldn't have been")
207 def testNewIssueNoClass(self):
208 self._handle_mail('''Content-Type: text/plain;
209 charset="iso-8859-1"
210 From: Chef <chef@bork.bork.bork>
211 To: issue_tracker@your.tracker.email.domain.example
212 Cc: richard@test
213 Message-Id: <dummy_test_message_id>
214 Subject: Testing...
216 This is a test submission of a new issue.
217 ''')
218 assert not os.path.exists(SENDMAILDEBUG)
220 def testNewIssueAuthMsg(self):
221 # TODO: fix the damn config - this is apalling
222 self.db.config.MESSAGES_TO_AUTHOR = 'yes'
223 self._handle_mail('''Content-Type: text/plain;
224 charset="iso-8859-1"
225 From: Chef <chef@bork.bork.bork>
226 To: issue_tracker@your.tracker.email.domain.example
227 Message-Id: <dummy_test_message_id>
228 Subject: [issue] Testing... [nosy=mary; assignedto=richard]
230 This is a test submission of a new issue.
231 ''')
232 self.compareMessages(self._get_mail(),
233 '''FROM: roundup-admin@your.tracker.email.domain.example
234 TO: chef@bork.bork.bork, mary@test, richard@test
235 Content-Type: text/plain; charset=utf-8
236 Subject: [issue1] Testing...
237 To: chef@bork.bork.bork, mary@test, richard@test
238 From: "Bork, Chef" <issue_tracker@your.tracker.email.domain.example>
239 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
240 MIME-Version: 1.0
241 Message-Id: <dummy_test_message_id>
242 X-Roundup-Name: Roundup issue tracker
243 X-Roundup-Loop: hello
244 Content-Transfer-Encoding: quoted-printable
247 New submission from Bork, Chef <chef@bork.bork.bork>:
249 This is a test submission of a new issue.
251 ----------
252 assignedto: richard
253 messages: 1
254 nosy: Chef, mary, richard
255 status: unread
256 title: Testing...
258 _______________________________________________________________________
259 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
260 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
261 _______________________________________________________________________
262 ''')
264 # BUG
265 # def testMultipart(self):
266 # '''With more than one part'''
267 # see MultipartEnc tests: but if there is more than one part
268 # we return a multipart/mixed and the boundary contains
269 # the ip address of the test machine.
271 # BUG should test some binary attamchent too.
273 def testSimpleFollowup(self):
274 self.doNewIssue()
275 self._handle_mail('''Content-Type: text/plain;
276 charset="iso-8859-1"
277 From: mary <mary@test>
278 To: issue_tracker@your.tracker.email.domain.example
279 Message-Id: <followup_dummy_id>
280 In-Reply-To: <dummy_test_message_id>
281 Subject: [issue1] Testing...
283 This is a second followup
284 ''')
285 self.compareMessages(self._get_mail(),
286 '''FROM: roundup-admin@your.tracker.email.domain.example
287 TO: chef@bork.bork.bork, richard@test
288 Content-Type: text/plain; charset=utf-8
289 Subject: [issue1] Testing...
290 To: chef@bork.bork.bork, richard@test
291 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
292 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
293 MIME-Version: 1.0
294 Message-Id: <followup_dummy_id>
295 In-Reply-To: <dummy_test_message_id>
296 X-Roundup-Name: Roundup issue tracker
297 X-Roundup-Loop: hello
298 Content-Transfer-Encoding: quoted-printable
301 Contrary, Mary <mary@test> added the comment:
303 This is a second followup
305 ----------
306 status: unread -> chatting
308 _______________________________________________________________________
309 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
310 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
311 _______________________________________________________________________
312 ''')
314 def testFollowup(self):
315 self.doNewIssue()
317 self._handle_mail('''Content-Type: text/plain;
318 charset="iso-8859-1"
319 From: richard <richard@test>
320 To: issue_tracker@your.tracker.email.domain.example
321 Message-Id: <followup_dummy_id>
322 In-Reply-To: <dummy_test_message_id>
323 Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
325 This is a followup
326 ''')
327 l = self.db.issue.get('1', 'nosy')
328 l.sort()
329 self.assertEqual(l, [self.chef_id, self.richard_id, self.mary_id,
330 self.john_id])
332 self.compareMessages(self._get_mail(),
333 '''FROM: roundup-admin@your.tracker.email.domain.example
334 TO: chef@bork.bork.bork, john@test, mary@test
335 Content-Type: text/plain; charset=utf-8
336 Subject: [issue1] Testing...
337 To: chef@bork.bork.bork, john@test, mary@test
338 From: richard <issue_tracker@your.tracker.email.domain.example>
339 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
340 MIME-Version: 1.0
341 Message-Id: <followup_dummy_id>
342 In-Reply-To: <dummy_test_message_id>
343 X-Roundup-Name: Roundup issue tracker
344 X-Roundup-Loop: hello
345 Content-Transfer-Encoding: quoted-printable
348 richard <richard@test> added the comment:
350 This is a followup
352 ----------
353 assignedto: -> mary
354 nosy: +john, mary
355 status: unread -> chatting
357 _______________________________________________________________________
358 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
359 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
360 _______________________________________________________________________
361 ''')
363 def testFollowupTitleMatch(self):
364 self.doNewIssue()
365 self._handle_mail('''Content-Type: text/plain;
366 charset="iso-8859-1"
367 From: richard <richard@test>
368 To: issue_tracker@your.tracker.email.domain.example
369 Message-Id: <followup_dummy_id>
370 In-Reply-To: <dummy_test_message_id>
371 Subject: Re: Testing... [assignedto=mary; nosy=+john]
373 This is a followup
374 ''')
375 self.compareMessages(self._get_mail(),
376 '''FROM: roundup-admin@your.tracker.email.domain.example
377 TO: chef@bork.bork.bork, john@test, mary@test
378 Content-Type: text/plain; charset=utf-8
379 Subject: [issue1] Testing...
380 To: chef@bork.bork.bork, john@test, mary@test
381 From: richard <issue_tracker@your.tracker.email.domain.example>
382 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
383 MIME-Version: 1.0
384 Message-Id: <followup_dummy_id>
385 In-Reply-To: <dummy_test_message_id>
386 X-Roundup-Name: Roundup issue tracker
387 X-Roundup-Loop: hello
388 Content-Transfer-Encoding: quoted-printable
391 richard <richard@test> added the comment:
393 This is a followup
395 ----------
396 assignedto: -> mary
397 nosy: +john, mary
398 status: unread -> chatting
400 _______________________________________________________________________
401 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
402 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
403 _______________________________________________________________________
404 ''')
406 def testFollowupNosyAuthor(self):
407 self.doNewIssue()
408 self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
409 self._handle_mail('''Content-Type: text/plain;
410 charset="iso-8859-1"
411 From: john@test
412 To: issue_tracker@your.tracker.email.domain.example
413 Message-Id: <followup_dummy_id>
414 In-Reply-To: <dummy_test_message_id>
415 Subject: [issue1] Testing...
417 This is a followup
418 ''')
420 self.compareMessages(self._get_mail(),
421 '''FROM: roundup-admin@your.tracker.email.domain.example
422 TO: chef@bork.bork.bork, richard@test
423 Content-Type: text/plain; charset=utf-8
424 Subject: [issue1] Testing...
425 To: chef@bork.bork.bork, richard@test
426 From: John Doe <issue_tracker@your.tracker.email.domain.example>
427 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
428 MIME-Version: 1.0
429 Message-Id: <followup_dummy_id>
430 In-Reply-To: <dummy_test_message_id>
431 X-Roundup-Name: Roundup issue tracker
432 X-Roundup-Loop: hello
433 Content-Transfer-Encoding: quoted-printable
436 John Doe <john@test> added the comment:
438 This is a followup
440 ----------
441 nosy: +john
442 status: unread -> chatting
444 _______________________________________________________________________
445 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
446 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
447 _______________________________________________________________________
449 ''')
451 def testFollowupNosyRecipients(self):
452 self.doNewIssue()
453 self.db.config.ADD_RECIPIENTS_TO_NOSY = 'yes'
454 self._handle_mail('''Content-Type: text/plain;
455 charset="iso-8859-1"
456 From: richard@test
457 To: issue_tracker@your.tracker.email.domain.example
458 Cc: john@test
459 Message-Id: <followup_dummy_id>
460 In-Reply-To: <dummy_test_message_id>
461 Subject: [issue1] Testing...
463 This is a followup
464 ''')
465 self.compareMessages(self._get_mail(),
466 '''FROM: roundup-admin@your.tracker.email.domain.example
467 TO: chef@bork.bork.bork
468 Content-Type: text/plain; charset=utf-8
469 Subject: [issue1] Testing...
470 To: chef@bork.bork.bork
471 From: richard <issue_tracker@your.tracker.email.domain.example>
472 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
473 MIME-Version: 1.0
474 Message-Id: <followup_dummy_id>
475 In-Reply-To: <dummy_test_message_id>
476 X-Roundup-Name: Roundup issue tracker
477 X-Roundup-Loop: hello
478 Content-Transfer-Encoding: quoted-printable
481 richard <richard@test> added the comment:
483 This is a followup
485 ----------
486 nosy: +john
487 status: unread -> chatting
489 _______________________________________________________________________
490 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
491 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
492 _______________________________________________________________________
494 ''')
496 def testFollowupNosyAuthorAndCopy(self):
497 self.doNewIssue()
498 self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
499 self.db.config.MESSAGES_TO_AUTHOR = 'yes'
500 self._handle_mail('''Content-Type: text/plain;
501 charset="iso-8859-1"
502 From: john@test
503 To: issue_tracker@your.tracker.email.domain.example
504 Message-Id: <followup_dummy_id>
505 In-Reply-To: <dummy_test_message_id>
506 Subject: [issue1] Testing...
508 This is a followup
509 ''')
510 self.compareMessages(self._get_mail(),
511 '''FROM: roundup-admin@your.tracker.email.domain.example
512 TO: chef@bork.bork.bork, john@test, richard@test
513 Content-Type: text/plain; charset=utf-8
514 Subject: [issue1] Testing...
515 To: chef@bork.bork.bork, john@test, richard@test
516 From: John Doe <issue_tracker@your.tracker.email.domain.example>
517 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
518 MIME-Version: 1.0
519 Message-Id: <followup_dummy_id>
520 In-Reply-To: <dummy_test_message_id>
521 X-Roundup-Name: Roundup issue tracker
522 X-Roundup-Loop: hello
523 Content-Transfer-Encoding: quoted-printable
526 John Doe <john@test> added the comment:
528 This is a followup
530 ----------
531 nosy: +john
532 status: unread -> chatting
534 _______________________________________________________________________
535 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
536 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
537 _______________________________________________________________________
539 ''')
541 def testFollowupNoNosyAuthor(self):
542 self.doNewIssue()
543 self.instance.config.ADD_AUTHOR_TO_NOSY = 'no'
544 self._handle_mail('''Content-Type: text/plain;
545 charset="iso-8859-1"
546 From: john@test
547 To: issue_tracker@your.tracker.email.domain.example
548 Message-Id: <followup_dummy_id>
549 In-Reply-To: <dummy_test_message_id>
550 Subject: [issue1] Testing...
552 This is a followup
553 ''')
554 self.compareMessages(self._get_mail(),
555 '''FROM: roundup-admin@your.tracker.email.domain.example
556 TO: chef@bork.bork.bork, richard@test
557 Content-Type: text/plain; charset=utf-8
558 Subject: [issue1] Testing...
559 To: chef@bork.bork.bork, richard@test
560 From: John Doe <issue_tracker@your.tracker.email.domain.example>
561 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
562 MIME-Version: 1.0
563 Message-Id: <followup_dummy_id>
564 In-Reply-To: <dummy_test_message_id>
565 X-Roundup-Name: Roundup issue tracker
566 X-Roundup-Loop: hello
567 Content-Transfer-Encoding: quoted-printable
570 John Doe <john@test> added the comment:
572 This is a followup
574 ----------
575 status: unread -> chatting
577 _______________________________________________________________________
578 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
579 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
580 _______________________________________________________________________
582 ''')
584 def testFollowupNoNosyRecipients(self):
585 self.doNewIssue()
586 self.instance.config.ADD_RECIPIENTS_TO_NOSY = 'no'
587 self._handle_mail('''Content-Type: text/plain;
588 charset="iso-8859-1"
589 From: richard@test
590 To: issue_tracker@your.tracker.email.domain.example
591 Cc: john@test
592 Message-Id: <followup_dummy_id>
593 In-Reply-To: <dummy_test_message_id>
594 Subject: [issue1] Testing...
596 This is a followup
597 ''')
598 self.compareMessages(self._get_mail(),
599 '''FROM: roundup-admin@your.tracker.email.domain.example
600 TO: chef@bork.bork.bork
601 Content-Type: text/plain; charset=utf-8
602 Subject: [issue1] Testing...
603 To: chef@bork.bork.bork
604 From: richard <issue_tracker@your.tracker.email.domain.example>
605 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
606 MIME-Version: 1.0
607 Message-Id: <followup_dummy_id>
608 In-Reply-To: <dummy_test_message_id>
609 X-Roundup-Name: Roundup issue tracker
610 X-Roundup-Loop: hello
611 Content-Transfer-Encoding: quoted-printable
614 richard <richard@test> added the comment:
616 This is a followup
618 ----------
619 status: unread -> chatting
621 _______________________________________________________________________
622 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
623 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
624 _______________________________________________________________________
626 ''')
628 def testFollowupEmptyMessage(self):
629 self.doNewIssue()
631 self._handle_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... [assignedto=mary; nosy=+john]
639 ''')
640 l = self.db.issue.get('1', 'nosy')
641 l.sort()
642 self.assertEqual(l, [self.chef_id, self.richard_id, self.mary_id,
643 self.john_id])
645 # should be no file created (ie. no message)
646 assert not os.path.exists(SENDMAILDEBUG)
648 def testNosyRemove(self):
649 self.doNewIssue()
651 self._handle_mail('''Content-Type: text/plain;
652 charset="iso-8859-1"
653 From: richard <richard@test>
654 To: issue_tracker@your.tracker.email.domain.example
655 Message-Id: <followup_dummy_id>
656 In-Reply-To: <dummy_test_message_id>
657 Subject: [issue1] Testing... [nosy=-richard]
659 ''')
660 l = self.db.issue.get('1', 'nosy')
661 l.sort()
662 self.assertEqual(l, [self.chef_id])
664 # NO NOSY MESSAGE SHOULD BE SENT!
665 assert not os.path.exists(SENDMAILDEBUG)
667 def testNewUserAuthor(self):
668 # first without the permission
669 # heh... just ignore the API for a second ;)
670 self.db.security.role['anonymous'].permissions=[]
671 anonid = self.db.user.lookup('anonymous')
672 self.db.user.set(anonid, roles='Anonymous')
674 self.db.security.hasPermission('Email Registration', anonid)
675 l = self.db.user.list()
676 l.sort()
677 message = '''Content-Type: text/plain;
678 charset="iso-8859-1"
679 From: fubar <fubar@bork.bork.bork>
680 To: issue_tracker@your.tracker.email.domain.example
681 Message-Id: <dummy_test_message_id>
682 Subject: [issue] Testing...
684 This is a test submission of a new issue.
685 '''
686 self.assertRaises(Unauthorized, self._handle_mail, message)
687 m = self.db.user.list()
688 m.sort()
689 self.assertEqual(l, m)
691 # now with the permission
692 p = self.db.security.getPermission('Email Registration')
693 self.db.security.role['anonymous'].permissions=[p]
694 self._handle_mail(message)
695 m = self.db.user.list()
696 m.sort()
697 self.assertNotEqual(l, m)
699 def testEnc01(self):
700 self.doNewIssue()
701 self._handle_mail('''Content-Type: text/plain;
702 charset="iso-8859-1"
703 From: mary <mary@test>
704 To: issue_tracker@your.tracker.email.domain.example
705 Message-Id: <followup_dummy_id>
706 In-Reply-To: <dummy_test_message_id>
707 Subject: [issue1] Testing...
708 Content-Type: text/plain;
709 charset="iso-8859-1"
710 Content-Transfer-Encoding: quoted-printable
712 A message with encoding (encoded oe =F6)
714 ''')
715 self.compareMessages(self._get_mail(),
716 '''FROM: roundup-admin@your.tracker.email.domain.example
717 TO: chef@bork.bork.bork, richard@test
718 Content-Type: text/plain; charset=utf-8
719 Subject: [issue1] Testing...
720 To: chef@bork.bork.bork, richard@test
721 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
722 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
723 MIME-Version: 1.0
724 Message-Id: <followup_dummy_id>
725 In-Reply-To: <dummy_test_message_id>
726 X-Roundup-Name: Roundup issue tracker
727 X-Roundup-Loop: hello
728 Content-Transfer-Encoding: quoted-printable
731 Contrary, Mary <mary@test> added the comment:
733 A message with encoding (encoded oe =C3=B6)
735 ----------
736 status: unread -> chatting
738 _______________________________________________________________________
739 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
740 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
741 _______________________________________________________________________
742 ''')
745 def testMultipartEnc01(self):
746 self.doNewIssue()
747 self._handle_mail('''Content-Type: text/plain;
748 charset="iso-8859-1"
749 From: mary <mary@test>
750 To: issue_tracker@your.tracker.email.domain.example
751 Message-Id: <followup_dummy_id>
752 In-Reply-To: <dummy_test_message_id>
753 Subject: [issue1] Testing...
754 Content-Type: multipart/mixed;
755 boundary="----_=_NextPart_000_01"
757 This message is in MIME format. Since your mail reader does not understand
758 this format, some or all of this message may not be legible.
760 ------_=_NextPart_000_01
761 Content-Type: text/plain;
762 charset="iso-8859-1"
763 Content-Transfer-Encoding: quoted-printable
765 A message with first part encoded (encoded oe =F6)
767 ''')
768 self.compareMessages(self._get_mail(),
769 '''FROM: roundup-admin@your.tracker.email.domain.example
770 TO: chef@bork.bork.bork, richard@test
771 Content-Type: text/plain; charset=utf-8
772 Subject: [issue1] Testing...
773 To: chef@bork.bork.bork, richard@test
774 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
775 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
776 MIME-Version: 1.0
777 Message-Id: <followup_dummy_id>
778 In-Reply-To: <dummy_test_message_id>
779 X-Roundup-Name: Roundup issue tracker
780 X-Roundup-Loop: hello
781 Content-Transfer-Encoding: quoted-printable
784 Contrary, Mary <mary@test> added the comment:
786 A message with first part encoded (encoded oe =C3=B6)
788 ----------
789 status: unread -> chatting
791 _______________________________________________________________________
792 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
793 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
794 _______________________________________________________________________
795 ''')
797 def testContentDisposition(self):
798 self.doNewIssue()
799 self._handle_mail('''Content-Type: text/plain;
800 charset="iso-8859-1"
801 From: mary <mary@test>
802 To: issue_tracker@your.tracker.email.domain.example
803 Message-Id: <followup_dummy_id>
804 In-Reply-To: <dummy_test_message_id>
805 Subject: [issue1] Testing...
806 Content-Type: multipart/mixed; boundary="bCsyhTFzCvuiizWE"
807 Content-Disposition: inline
810 --bCsyhTFzCvuiizWE
811 Content-Type: text/plain; charset=us-ascii
812 Content-Disposition: inline
814 test attachment binary
816 --bCsyhTFzCvuiizWE
817 Content-Type: application/octet-stream
818 Content-Disposition: attachment; filename="main.dvi"
820 xxxxxx
822 --bCsyhTFzCvuiizWE--
823 ''')
824 messages = self.db.issue.get('1', 'messages')
825 messages.sort()
826 file = self.db.msg.get(messages[-1], 'files')[0]
827 self.assertEqual(self.db.file.get(file, 'name'), 'main.dvi')
829 def testFollowupStupidQuoting(self):
830 self.doNewIssue()
832 self._handle_mail('''Content-Type: text/plain;
833 charset="iso-8859-1"
834 From: richard <richard@test>
835 To: issue_tracker@your.tracker.email.domain.example
836 Message-Id: <followup_dummy_id>
837 In-Reply-To: <dummy_test_message_id>
838 Subject: Re: "[issue1] Testing... "
840 This is a followup
841 ''')
842 self.compareMessages(self._get_mail(),
843 '''FROM: roundup-admin@your.tracker.email.domain.example
844 TO: chef@bork.bork.bork
845 Content-Type: text/plain; charset=utf-8
846 Subject: [issue1] Testing...
847 To: chef@bork.bork.bork
848 From: richard <issue_tracker@your.tracker.email.domain.example>
849 Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
850 MIME-Version: 1.0
851 Message-Id: <followup_dummy_id>
852 In-Reply-To: <dummy_test_message_id>
853 X-Roundup-Name: Roundup issue tracker
854 X-Roundup-Loop: hello
855 Content-Transfer-Encoding: quoted-printable
858 richard <richard@test> added the comment:
860 This is a followup
862 ----------
863 status: unread -> chatting
865 _______________________________________________________________________
866 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
867 <http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
868 _______________________________________________________________________
869 ''')
871 def testEmailQuoting(self):
872 self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'no'
873 self.innerTestQuoting('''This is a followup
874 ''')
876 def testEmailQuotingRemove(self):
877 self.instance.config.EMAIL_KEEP_QUOTED_TEXT = 'yes'
878 self.innerTestQuoting('''Blah blah wrote:
879 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
880 > skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
881 >
883 This is a followup
884 ''')
886 def innerTestQuoting(self, expect):
887 nodeid = self.doNewIssue()
889 messages = self.db.issue.get(nodeid, 'messages')
891 self._handle_mail('''Content-Type: text/plain;
892 charset="iso-8859-1"
893 From: richard <richard@test>
894 To: issue_tracker@your.tracker.email.domain.example
895 Message-Id: <followup_dummy_id>
896 In-Reply-To: <dummy_test_message_id>
897 Subject: Re: [issue1] Testing...
899 Blah blah wrote:
900 > Blah bklaskdfj sdf asdf jlaskdf skj sdkfjl asdf
901 > skdjlkjsdfalsdkfjasdlfkj dlfksdfalksd fj
902 >
904 This is a followup
905 ''')
906 # figure the new message id
907 newmessages = self.db.issue.get(nodeid, 'messages')
908 for msg in messages:
909 newmessages.remove(msg)
910 messageid = newmessages[0]
912 self.compareMessages(self.db.msg.get(messageid, 'content'), expect)
914 def testUserLookup(self):
915 i = self.db.user.create(username='user1', address='user1@foo.com')
916 self.assertEqual(uidFromAddress(self.db, ('', 'user1@foo.com'), 0), i)
917 self.assertEqual(uidFromAddress(self.db, ('', 'USER1@foo.com'), 0), i)
918 i = self.db.user.create(username='user2', address='USER2@foo.com')
919 self.assertEqual(uidFromAddress(self.db, ('', 'USER2@foo.com'), 0), i)
920 self.assertEqual(uidFromAddress(self.db, ('', 'user2@foo.com'), 0), i)
922 def testUserAlternateLookup(self):
923 i = self.db.user.create(username='user1', address='user1@foo.com',
924 alternate_addresses='user1@bar.com')
925 self.assertEqual(uidFromAddress(self.db, ('', 'user1@bar.com'), 0), i)
926 self.assertEqual(uidFromAddress(self.db, ('', 'USER1@bar.com'), 0), i)
928 def testUserCreate(self):
929 i = uidFromAddress(self.db, ('', 'user@foo.com'), 1)
930 self.assertNotEqual(uidFromAddress(self.db, ('', 'user@bar.com'), 1), i)
932 def testRFC2822(self):
933 ascii_header = "[issue243] This is a \"test\" - with 'quotation' marks"
934 unicode_header = '[issue244] \xd0\xb0\xd0\xbd\xd0\xb4\xd1\x80\xd0\xb5\xd0\xb9'
935 unicode_encoded = '=?utf-8?q?[issue244]_=D0=B0=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?='
936 self.assertEqual(rfc2822.encode_header(ascii_header), ascii_header)
937 self.assertEqual(rfc2822.encode_header(unicode_header), unicode_encoded)
939 def testRegistrationConfirmation(self):
940 otk = "Aj4euk4LZSAdwePohj90SME5SpopLETL"
941 self.db.getOTKManager().set(otk, username='johannes')
942 self._handle_mail('''Content-Type: text/plain;
943 charset="iso-8859-1"
944 From: Chef <chef@bork.bork.bork>
945 To: issue_tracker@your.tracker.email.domain.example
946 Cc: richard@test
947 Message-Id: <dummy_test_message_id>
948 Subject: Re: Complete your registration to Roundup issue tracker\r
949 -- key %s
951 This is a test confirmation of registration.
952 ''' % otk)
953 self.db.user.lookup('johannes')
955 def testFollowupOnNonIssue(self):
956 self.db.keyword.create(name='Foo')
957 self._handle_mail('''Content-Type: text/plain;
958 charset="iso-8859-1"
959 From: richard <richard@test>
960 To: issue_tracker@your.tracker.email.domain.example
961 Message-Id: <followup_dummy_id>
962 In-Reply-To: <dummy_test_message_id>
963 Subject: [keyword1] Testing... [name=Bar]
965 ''')
966 self.assertEqual(self.db.keyword.get('1', 'name'), 'Bar')
968 def testResentFrom(self):
969 nodeid = self._handle_mail('''Content-Type: text/plain;
970 charset="iso-8859-1"
971 From: Chef <chef@bork.bork.bork>
972 Resent-From: mary <mary@test>
973 To: issue_tracker@your.tracker.email.domain.example
974 Cc: richard@test
975 Message-Id: <dummy_test_message_id>
976 Subject: [issue] Testing...
978 This is a test submission of a new issue.
979 ''')
980 assert not os.path.exists(SENDMAILDEBUG)
981 l = self.db.issue.get(nodeid, 'nosy')
982 l.sort()
983 self.assertEqual(l, [self.richard_id, self.mary_id])
984 return nodeid
987 def testDejaVu(self):
988 self.assertRaises(IgnoreLoop, self._handle_mail,
989 '''Content-Type: text/plain;
990 charset="iso-8859-1"
991 From: Chef <chef@bork.bork.bork>
992 X-Roundup-Loop: hello
993 To: issue_tracker@your.tracker.email.domain.example
994 Cc: richard@test
995 Message-Id: <dummy_test_message_id>
996 Subject: Re: [issue] Testing...
998 Hi, I've been mis-configured to loop messages back to myself.
999 ''')
1001 def testItsBulkStupid(self):
1002 self.assertRaises(IgnoreBulk, self._handle_mail,
1003 '''Content-Type: text/plain;
1004 charset="iso-8859-1"
1005 From: Chef <chef@bork.bork.bork>
1006 Precedence: bulk
1007 To: issue_tracker@your.tracker.email.domain.example
1008 Cc: richard@test
1009 Message-Id: <dummy_test_message_id>
1010 Subject: Re: [issue] Testing...
1012 Hi, I'm on holidays, and this is a dumb auto-responder.
1013 ''')
1015 def test_suite():
1016 suite = unittest.TestSuite()
1017 suite.addTest(unittest.makeSuite(MailgwTestCase))
1018 return suite
1020 if __name__ == '__main__':
1021 runner = unittest.TextTestRunner()
1022 unittest.main(testRunner=runner)
1024 # vim: set filetype=python ts=4 sw=4 et si