Code

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