Code

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