Code

Extract _send_mail method, it was duplicated all around the test code.
[roundup.git] / test / test_mailgw.py
index 918f431396773b0077205e62d9b2b63796714d64..4a1991ec776cc39408f8af9ca6b13f6c4b82eda1 100644 (file)
@@ -8,42 +8,50 @@
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 #
-# $Id: test_mailgw.py,v 1.34 2002-12-10 00:11:16 richard Exp $
+# $Id: test_mailgw.py,v 1.59 2003-11-03 19:08:41 jlgijsbers Exp $
 
-import unittest, cStringIO, tempfile, os, shutil, errno, imp, sys, difflib
+import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822
 
-# Note: Should parse emails according to RFC2822 instead of performing a
-# literal string comparision.  Parsing the messages allows the tests to work for
-# any legal serialization of an email.
-#try :
-#    import email
-#except ImportError :
-#    import rfc822 as email
+from cStringIO import StringIO
 
-from roundup.mailgw import MailGW, Unauthorized
-from roundup import init, instance
+if not os.environ.has_key('SENDMAILDEBUG'):
+    os.environ['SENDMAILDEBUG'] = 'mail-test.log'
+SENDMAILDEBUG = os.environ['SENDMAILDEBUG']
 
-# TODO: make this output only enough equal lines for context, not all of
-# them
+from roundup.mailgw import MailGW, Unauthorized, uidFromAddress, parseContent
+from roundup import init, instance, rfc2822
+
+
+class Message(rfc822.Message):
+    """String-based Message class with equivalence test."""
+    def __init__(self, s):
+        rfc822.Message.__init__(self, StringIO(s.strip()))
+        
+    def __eq__(self, other):
+        del self['date'], other['date']
+
+        return (self.dict == other.dict and
+                self.fp.read() == other.fp.read()) 
+
+# TODO: Do a semantic diff instead of a straight text diff when a test fails.
 class DiffHelper:
+    def compareMessages(self, s2, s1):
+        """Compare messages for semantic equivalence."""
+        if not Message(s2) == Message(s1):    
+            self.compareStrings(s2, s1)
+    
     def compareStrings(self, s2, s1):
         '''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
            the first to be the "original" but in the calls in this file,
            the second arg is the original. Ho hum.
         '''
-        if s1 == s2:
+        # we have to special-case the Date: header here 'cos we can't test
+        # for it properly
+        l1=s1.strip().split('\n')
+        l2=[x for x in s2.strip().split('\n') if not x.startswith('Date: ')]
+        if l1 == l2:
             return
 
-        # under python2.[12] we allow a difference of one trailing empty line.
-        if sys.version_info[0:2] == (2,1):
-            if s1+'\n' == s2:
-                return
-        if sys.version_info[0:2] == (2,2):
-            if s1 == s2+'\n':
-                return
-        
-        l1=s1.split('\n')
-        l2=s2.split('\n')
         s = difflib.SequenceMatcher(None, l1, l2)
         res = ['Generated message not correct (diff follows):']
         for value, s1s, s1e, s2s, s2e in s.get_opcodes():
@@ -74,10 +82,13 @@ class MailgwTestCase(unittest.TestCase, DiffHelper):
         except OSError, error:
             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
         # create the instance
-        init.install(self.dirname, 'classic', 'anydbm')
+        init.install(self.dirname, 'templates/classic')
+        init.write_select_db(self.dirname, 'anydbm')
         init.initialise(self.dirname, 'sekrit')
+        
         # check we can load the package
         self.instance = instance.open(self.dirname)
+
         # and open the database
         self.db = self.instance.open('admin')
         self.db.user.create(username='Chef', address='chef@bork.bork.bork',
@@ -91,16 +102,41 @@ class MailgwTestCase(unittest.TestCase, DiffHelper):
             realname='John Doe')
 
     def tearDown(self):
-        if os.path.exists(os.environ['SENDMAILDEBUG']):
-            os.remove(os.environ['SENDMAILDEBUG'])
+        if os.path.exists(SENDMAILDEBUG):
+            os.remove(SENDMAILDEBUG)
         self.db.close()
         try:
             shutil.rmtree(self.dirname)
         except OSError, error:
             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
 
+    def _send_mail(self, message):
+        handler = self.instance.MailGW(self.instance, self.db)
+        handler.trapExceptions = 0
+        return handler.main(StringIO(message))
+        
+    def _get_mail(self):
+        f = open(SENDMAILDEBUG)
+        try:
+            return f.read()
+        finally:
+            f.close()
+
+    def testEmptyMessage(self):
+        nodeid = self._send_mail('''Content-Type: text/plain;
+  charset="iso-8859-1"
+From: Chef <chef@bork.bork.bork>
+To: issue_tracker@your.tracker.email.domain.example
+Cc: richard@test
+Message-Id: <dummy_test_message_id>
+Subject: [issue] Testing...
+
+''')
+        assert not os.path.exists(SENDMAILDEBUG)
+        self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...')
+
     def doNewIssue(self):
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        nodeid = self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: Chef <chef@bork.bork.bork>
 To: issue_tracker@your.tracker.email.domain.example
@@ -110,12 +146,7 @@ Subject: [issue] Testing...
 
 This is a test submission of a new issue.
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        nodeid = handler.main(message)
-        if os.path.exists(os.environ['SENDMAILDEBUG']):
-            error = open(os.environ['SENDMAILDEBUG']).read()
-            self.assertEqual('no error', error)
+        assert not os.path.exists(SENDMAILDEBUG)
         l = self.db.issue.get(nodeid, 'nosy')
         l.sort()
         self.assertEqual(l, ['3', '4'])
@@ -126,7 +157,7 @@ This is a test submission of a new issue.
 
     def testNewIssueNosy(self):
         self.instance.config.ADD_AUTHOR_TO_NOSY = 'yes'
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        nodeid = self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: Chef <chef@bork.bork.bork>
 To: issue_tracker@your.tracker.email.domain.example
@@ -136,18 +167,13 @@ Subject: [issue] Testing...
 
 This is a test submission of a new issue.
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        nodeid = handler.main(message)
-        if os.path.exists(os.environ['SENDMAILDEBUG']):
-            error = open(os.environ['SENDMAILDEBUG']).read()
-            self.assertEqual('no error', error)
+        assert not os.path.exists(SENDMAILDEBUG)
         l = self.db.issue.get(nodeid, 'nosy')
         l.sort()
         self.assertEqual(l, ['3', '4'])
 
     def testAlternateAddress(self):
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: John Doe <john.doe@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -156,18 +182,13 @@ Subject: [issue] Testing...
 
 This is a test submission of a new issue.
 ''')
-        userlist = self.db.user.list()
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-        if os.path.exists(os.environ['SENDMAILDEBUG']):
-            error = open(os.environ['SENDMAILDEBUG']).read()
-            self.assertEqual('no error', error)
+        userlist = self.db.user.list()        
+        assert not os.path.exists(SENDMAILDEBUG)
         self.assertEqual(userlist, self.db.user.list(),
             "user created when it shouldn't have been")
 
     def testNewIssueNoClass(self):
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: Chef <chef@bork.bork.bork>
 To: issue_tracker@your.tracker.email.domain.example
@@ -177,15 +198,12 @@ Subject: Testing...
 
 This is a test submission of a new issue.
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-        if os.path.exists(os.environ['SENDMAILDEBUG']):
-            error = open(os.environ['SENDMAILDEBUG']).read()
-            self.assertEqual('no error', error)
+        assert not os.path.exists(SENDMAILDEBUG)
 
     def testNewIssueAuthMsg(self):
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        # TODO: fix the damn config - this is apalling
+        self.db.config.MESSAGES_TO_AUTHOR = 'yes'
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: Chef <chef@bork.bork.bork>
 To: issue_tracker@your.tracker.email.domain.example
@@ -194,16 +212,10 @@ Subject: [issue] Testing... [nosy=mary; assignedto=richard]
 
 This is a test submission of a new issue.
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        # TODO: fix the damn config - this is apalling
-        self.db.config.MESSAGES_TO_AUTHOR = 'yes'
-        handler.main(message)
-
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, mary@test, richard@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, mary@test, richard@test
 From: "Bork, Chef" <issue_tracker@your.tracker.email.domain.example>
@@ -211,6 +223,7 @@ Reply-To: Roundup issue tracker <issue_tracker@your.tracker.email.domain.example
 MIME-Version: 1.0
 Message-Id: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -218,7 +231,6 @@ New submission from Bork, Chef <chef@bork.bork.bork>:
 
 This is a test submission of a new issue.
 
-
 ----------
 assignedto: richard
 messages: 1
@@ -227,7 +239,7 @@ status: unread
 title: Testing...
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 ''')
 
@@ -242,7 +254,7 @@ _______________________________________________________________________
 
     def testSimpleFollowup(self):
         self.doNewIssue()
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: mary <mary@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -252,13 +264,10 @@ Subject: [issue1] Testing...
 
 This is a second followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, richard@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, richard@test
 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
@@ -267,6 +276,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -274,19 +284,18 @@ Contrary, Mary <mary@test> added the comment:
 
 This is a second followup
 
-
 ----------
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 ''')
 
     def testFollowup(self):
         self.doNewIssue()
 
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: richard <richard@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -296,17 +305,14 @@ Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
         l = self.db.issue.get('1', 'nosy')
         l.sort()
         self.assertEqual(l, ['3', '4', '5', '6'])
 
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, john@test, mary@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, john@test, mary@test
 From: richard <issue_tracker@your.tracker.email.domain.example>
@@ -315,6 +321,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -322,20 +329,19 @@ richard <richard@test> added the comment:
 
 This is a followup
 
-
 ----------
 assignedto:  -> mary
 nosy: +john, mary
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 ''')
 
     def testFollowupTitleMatch(self):
         self.doNewIssue()
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: richard <richard@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -345,14 +351,10 @@ Subject: Re: Testing... [assignedto=mary; nosy=+john]
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, john@test, mary@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, john@test, mary@test
 From: richard <issue_tracker@your.tracker.email.domain.example>
@@ -361,6 +363,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -368,21 +371,20 @@ richard <richard@test> added the comment:
 
 This is a followup
 
-
 ----------
 assignedto:  -> mary
 nosy: +john, mary
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 ''')
 
     def testFollowupNosyAuthor(self):
         self.doNewIssue()
         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: john@test
 To: issue_tracker@your.tracker.email.domain.example
@@ -392,14 +394,11 @@ Subject: [issue1] Testing...
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
 
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, richard@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, richard@test
 From: John Doe <issue_tracker@your.tracker.email.domain.example>
@@ -408,6 +407,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -415,13 +415,12 @@ John Doe <john@test> added the comment:
 
 This is a followup
 
-
 ----------
 nosy: +john
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 
 ''')
@@ -429,7 +428,7 @@ _______________________________________________________________________
     def testFollowupNosyRecipients(self):
         self.doNewIssue()
         self.db.config.ADD_RECIPIENTS_TO_NOSY = 'yes'
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: richard@test
 To: issue_tracker@your.tracker.email.domain.example
@@ -440,14 +439,10 @@ Subject: [issue1] Testing...
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork
 From: richard <issue_tracker@your.tracker.email.domain.example>
@@ -456,6 +451,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -463,13 +459,12 @@ richard <richard@test> added the comment:
 
 This is a followup
 
-
 ----------
 nosy: +john
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 
 ''')
@@ -478,7 +473,7 @@ _______________________________________________________________________
         self.doNewIssue()
         self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
         self.db.config.MESSAGES_TO_AUTHOR = 'yes'
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: john@test
 To: issue_tracker@your.tracker.email.domain.example
@@ -488,14 +483,10 @@ Subject: [issue1] Testing...
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, john@test, richard@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, john@test, richard@test
 From: John Doe <issue_tracker@your.tracker.email.domain.example>
@@ -504,6 +495,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -511,13 +503,12 @@ John Doe <john@test> added the comment:
 
 This is a followup
 
-
 ----------
 nosy: +john
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 
 ''')
@@ -525,7 +516,7 @@ _______________________________________________________________________
     def testFollowupNoNosyAuthor(self):
         self.doNewIssue()
         self.instance.config.ADD_AUTHOR_TO_NOSY = 'no'
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: john@test
 To: issue_tracker@your.tracker.email.domain.example
@@ -535,14 +526,10 @@ Subject: [issue1] Testing...
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, richard@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, richard@test
 From: John Doe <issue_tracker@your.tracker.email.domain.example>
@@ -551,6 +538,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -558,12 +546,11 @@ John Doe <john@test> added the comment:
 
 This is a followup
 
-
 ----------
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 
 ''')
@@ -571,7 +558,7 @@ _______________________________________________________________________
     def testFollowupNoNosyRecipients(self):
         self.doNewIssue()
         self.instance.config.ADD_RECIPIENTS_TO_NOSY = 'no'
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: richard@test
 To: issue_tracker@your.tracker.email.domain.example
@@ -582,14 +569,10 @@ Subject: [issue1] Testing...
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork
 From: richard <issue_tracker@your.tracker.email.domain.example>
@@ -598,6 +581,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -605,20 +589,38 @@ richard <richard@test> added the comment:
 
 This is a followup
 
-
 ----------
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 
 ''')
 
+    def testFollowupEmptyMessage(self):
+        self.doNewIssue()
+
+        self._send_mail('''Content-Type: text/plain;
+  charset="iso-8859-1"
+From: richard <richard@test>
+To: issue_tracker@your.tracker.email.domain.example
+Message-Id: <followup_dummy_id>
+In-Reply-To: <dummy_test_message_id>
+Subject: [issue1] Testing... [assignedto=mary; nosy=+john]
+
+''')
+        l = self.db.issue.get('1', 'nosy')
+        l.sort()
+        self.assertEqual(l, ['3', '4', '5', '6'])
+
+        # should be no file created (ie. no message)
+        assert not os.path.exists(SENDMAILDEBUG)
+
     def testNosyRemove(self):
         self.doNewIssue()
 
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: richard <richard@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -627,27 +629,24 @@ In-Reply-To: <dummy_test_message_id>
 Subject: [issue1] Testing... [nosy=-richard]
 
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
         l = self.db.issue.get('1', 'nosy')
         l.sort()
         self.assertEqual(l, ['3'])
 
         # NO NOSY MESSAGE SHOULD BE SENT!
-        self.assert_(not os.path.exists(os.environ['SENDMAILDEBUG']))
+        assert not os.path.exists(SENDMAILDEBUG)
 
     def testNewUserAuthor(self):
         # first without the permission
         # heh... just ignore the API for a second ;)
-        self.db.security.role['Anonymous'].permissions=[]
+        self.db.security.role['anonymous'].permissions=[]
         anonid = self.db.user.lookup('anonymous')
         self.db.user.set(anonid, roles='Anonymous')
 
         self.db.security.hasPermission('Email Registration', anonid)
         l = self.db.user.list()
         l.sort()
-        s = '''Content-Type: text/plain;
+        message = '''Content-Type: text/plain;
   charset="iso-8859-1"
 From: fubar <fubar@bork.bork.bork>
 To: issue_tracker@your.tracker.email.domain.example
@@ -656,28 +655,22 @@ Subject: [issue] Testing...
 
 This is a test submission of a new issue.
 '''
-        message = cStringIO.StringIO(s)
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        self.assertRaises(Unauthorized, handler.main, message)
+        self.assertRaises(Unauthorized, self._send_mail, message)
         m = self.db.user.list()
         m.sort()
         self.assertEqual(l, m)
 
         # now with the permission
         p = self.db.security.getPermission('Email Registration')
-        self.db.security.role['Anonymous'].permissions=[p]
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        message = cStringIO.StringIO(s)
-        handler.main(message)
+        self.db.security.role['anonymous'].permissions=[p]
+        self._send_mail(message)
         m = self.db.user.list()
         m.sort()
         self.assertNotEqual(l, m)
 
     def testEnc01(self):
         self.doNewIssue()
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: mary <mary@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -691,13 +684,10 @@ Content-Transfer-Encoding: quoted-printable
 A message with encoding (encoded oe =F6)
 
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, richard@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, richard@test
 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
@@ -706,25 +696,26 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
 Contrary, Mary <mary@test> added the comment:
 
-A message with encoding (encoded oe =F6)
+A message with encoding (encoded oe =C3=B6)
 
 ----------
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 ''')
 
 
     def testMultipartEnc01(self):
         self.doNewIssue()
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: mary <mary@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -745,13 +736,10 @@ Content-Transfer-Encoding: quoted-printable
 A message with first part encoded (encoded oe =F6)
 
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork, richard@test
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork, richard@test
 From: "Contrary, Mary" <issue_tracker@your.tracker.email.domain.example>
@@ -760,25 +748,58 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
 Contrary, Mary <mary@test> added the comment:
 
-A message with first part encoded (encoded oe =F6)
+A message with first part encoded (encoded oe =C3=B6)
 
 ----------
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 ''')
 
+    def testContentDisposition(self):
+        self.doNewIssue()
+        self._send_mail('''Content-Type: text/plain;
+  charset="iso-8859-1"
+From: mary <mary@test>
+To: issue_tracker@your.tracker.email.domain.example
+Message-Id: <followup_dummy_id>
+In-Reply-To: <dummy_test_message_id>
+Subject: [issue1] Testing...
+Content-Type: multipart/mixed; boundary="bCsyhTFzCvuiizWE" 
+Content-Disposition: inline 
+--bCsyhTFzCvuiizWE 
+Content-Type: text/plain; charset=us-ascii 
+Content-Disposition: inline 
+
+test attachment binary 
+
+--bCsyhTFzCvuiizWE 
+Content-Type: application/octet-stream 
+Content-Disposition: attachment; filename="main.dvi" 
+
+xxxxxx 
+
+--bCsyhTFzCvuiizWE--
+''')
+        messages = self.db.issue.get('1', 'messages')
+        messages.sort()
+        file = self.db.msg.get(messages[-1], 'files')[0]
+        self.assertEqual(self.db.file.get(file, 'name'), 'main.dvi')
+
     def testFollowupStupidQuoting(self):
         self.doNewIssue()
 
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: richard <richard@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -788,14 +809,10 @@ Subject: Re: "[issue1] Testing... "
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-
-        self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
+        self.compareMessages(self._get_mail(),
 '''FROM: roundup-admin@your.tracker.email.domain.example
 TO: chef@bork.bork.bork
-Content-Type: text/plain
+Content-Type: text/plain; charset=utf-8
 Subject: [issue1] Testing...
 To: chef@bork.bork.bork
 From: richard <issue_tracker@your.tracker.email.domain.example>
@@ -804,6 +821,7 @@ MIME-Version: 1.0
 Message-Id: <followup_dummy_id>
 In-Reply-To: <dummy_test_message_id>
 X-Roundup-Name: Roundup issue tracker
+X-Roundup-Loop: hello
 Content-Transfer-Encoding: quoted-printable
 
 
@@ -811,12 +829,11 @@ richard <richard@test> added the comment:
 
 This is a followup
 
-
 ----------
 status: unread -> chatting
 _______________________________________________________________________
 Roundup issue tracker <issue_tracker@your.tracker.email.domain.example>
-http://your.tracker.url.example/issue1
+<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
 _______________________________________________________________________
 ''')
 
@@ -840,7 +857,7 @@ This is a followup
 
         messages = self.db.issue.get(nodeid, 'messages')
 
-        message = cStringIO.StringIO('''Content-Type: text/plain;
+        self._send_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
 From: richard <richard@test>
 To: issue_tracker@your.tracker.email.domain.example
@@ -855,22 +872,75 @@ Blah blah wrote:
 
 This is a followup
 ''')
-        handler = self.instance.MailGW(self.instance, self.db)
-        handler.trapExceptions = 0
-        handler.main(message)
-
         # figure the new message id
         newmessages = self.db.issue.get(nodeid, 'messages')
         for msg in messages:
             newmessages.remove(msg)
         messageid = newmessages[0]
 
-        self.compareStrings(self.db.msg.get(messageid, 'content'), expect)
+        self.compareMessages(self.db.msg.get(messageid, 'content'), expect)
+
+    def testUserLookup(self):
+        i = self.db.user.create(username='user1', address='user1@foo.com')
+        self.assertEqual(uidFromAddress(self.db, ('', 'user1@foo.com'), 0), i)
+        self.assertEqual(uidFromAddress(self.db, ('', 'USER1@foo.com'), 0), i)
+        i = self.db.user.create(username='user2', address='USER2@foo.com')
+        self.assertEqual(uidFromAddress(self.db, ('', 'USER2@foo.com'), 0), i)
+        self.assertEqual(uidFromAddress(self.db, ('', 'user2@foo.com'), 0), i)
+
+    def testUserAlternateLookup(self):
+        i = self.db.user.create(username='user1', address='user1@foo.com',
+                                alternate_addresses='user1@bar.com')
+        self.assertEqual(uidFromAddress(self.db, ('', 'user1@bar.com'), 0), i)
+        self.assertEqual(uidFromAddress(self.db, ('', 'USER1@bar.com'), 0), i)
+
+    def testUserCreate(self):
+        i = uidFromAddress(self.db, ('', 'user@foo.com'), 1)
+        self.assertNotEqual(uidFromAddress(self.db, ('', 'user@bar.com'), 1), i)
+
+    def testRFC2822(self):
+        ascii_header = "[issue243] This is a \"test\" - with 'quotation' marks"
+        unicode_header = '[issue244] \xd0\xb0\xd0\xbd\xd0\xb4\xd1\x80\xd0\xb5\xd0\xb9'
+        unicode_encoded = '=?utf-8?q?[issue244]_=D0=B0=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?='
+        self.assertEqual(rfc2822.encode_header(ascii_header), ascii_header)
+        self.assertEqual(rfc2822.encode_header(unicode_header), unicode_encoded)
+
+    def testRegistrationConfirmation(self):
+        otk = "Aj4euk4LZSAdwePohj90SME5SpopLETL"
+        self.db.otks.set(otk, username='johannes', __time='')
+        self._send_mail('''Content-Type: text/plain;
+  charset="iso-8859-1"
+From: Chef <chef@bork.bork.bork>
+To: issue_tracker@your.tracker.email.domain.example
+Cc: richard@test
+Message-Id: <dummy_test_message_id>
+Subject: Re: Complete your registration to Roundup issue tracker\r
+ -- key %s
 
-def suite():
-    l = [unittest.makeSuite(MailgwTestCase),
-    ]
-    return unittest.TestSuite(l)
+This is a test confirmation of registration.
+''' % otk)
+        self.db.user.lookup('johannes')
 
+    def testFollowupOnNonIssue(self):
+        self.db.keyword.create(name='Foo')
+        self._send_mail('''Content-Type: text/plain;
+  charset="iso-8859-1"
+From: richard <richard@test>
+To: issue_tracker@your.tracker.email.domain.example
+Message-Id: <followup_dummy_id>
+In-Reply-To: <dummy_test_message_id>
+Subject: [keyword1] Testing... [name=Bar]
+
+''')        
+        self.assertEqual(self.db.keyword.get('1', 'name'), 'Bar')
+    
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(MailgwTestCase))
+    return suite
+
+if __name__ == '__main__':
+    runner = unittest.TextTestRunner()
+    unittest.main(testRunner=runner)
 
 # vim: set filetype=python ts=4 sw=4 et si