6 commits - pykolab/translit.py tests/functional tests/unit wallace/__init__.py wallace/modules.py

Jeroen van Meeuwen vanmeeuwen at kolabsys.com
Fri Feb 8 21:04:30 CET 2013


 pykolab/translit.py                                          |    9 
 tests/functional/test_wallace/test_002_footer.py             |    8 
 tests/functional/test_wallace/test_003_nonascii_subject.py   |  126 +++++++++++
 tests/functional/test_wallace/test_004_nonascii_addresses.py |  126 +++++++++++
 tests/unit/test-010-transliterate.py                         |    7 
 wallace/__init__.py                                          |    4 
 wallace/modules.py                                           |   10 
 7 files changed, 282 insertions(+), 8 deletions(-)

New commits:
commit d80a04b456bdfafa64822fe5f310ff0dc332de6e
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri Feb 8 21:03:44 2013 +0100

    Sending out the message requires nul characters (where do they come from?) be stripped

diff --git a/wallace/modules.py b/wallace/modules.py
index a20d80e..040fe62 100644
--- a/wallace/modules.py
+++ b/wallace/modules.py
@@ -252,8 +252,8 @@ X-Wallace-Result: REJECT
 def cb_action_ACCEPT(module, filepath):
     log.info(_("Accepting message in %s (by module %s)") % (filepath, module))
     _message = json.load(open(filepath, 'r'))
+    message = message_from_string(_message['data'])
 
-    message = message_from_string("%s" %(str(_message['data'])))
     sender = _message['from']
     recipients = _message['to']
 
@@ -266,7 +266,13 @@ def cb_action_ACCEPT(module, filepath):
         smtp.sendmail(
                 sender,
                 recipients,
-                message.as_string()
+                # - Make sure we do not send this as binary.
+                # - Second, strip NUL characters - I don't know where they
+                #   come from (TODO)
+                # - Third, a character return is inserted somewhere. It
+                #   divides the body from the headers - and we don't like (TODO)
+                #unicode(message.as_string()).replace('\0', '').lstrip()
+                message.as_string().encode('utf-8').replace('\0','').lstrip()
             )
 
     except smtplib.SMTPDataError, errmsg:


commit 03254dd34f7da4780c4fe356a1af2f956398df11
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri Feb 8 21:03:25 2013 +0100

    Make sure the tests clean up after themselves

diff --git a/tests/functional/test_wallace/test_002_footer.py b/tests/functional/test_wallace/test_002_footer.py
index f9568c2..f888eb0 100644
--- a/tests/functional/test_wallace/test_002_footer.py
+++ b/tests/functional/test_wallace/test_002_footer.py
@@ -54,10 +54,10 @@ class TestWallaceFooter(unittest.TestCase):
         from tests.functional.synchronize import synchronize_once
         synchronize_once()
 
-#    @classmethod
-#    def teardown_class(self, *args, **kw):
-#        from tests.functional.purge_users import purge_users
-#        purge_users()
+    @classmethod
+    def teardown_class(self, *args, **kw):
+        from tests.functional.purge_users import purge_users
+        purge_users()
 
     def check_message_delivered(self, subject):
         imap = IMAP()
diff --git a/tests/functional/test_wallace/test_003_nonascii_subject.py b/tests/functional/test_wallace/test_003_nonascii_subject.py
index 06028ce..ff21aee 100644
--- a/tests/functional/test_wallace/test_003_nonascii_subject.py
+++ b/tests/functional/test_wallace/test_003_nonascii_subject.py
@@ -43,10 +43,10 @@ class TestWallaceNonASCIISubject(unittest.TestCase):
         from tests.functional.synchronize import synchronize_once
         synchronize_once()
 
-#    @classmethod
-#    def teardown_class(self, *args, **kw):
-#        from tests.functional.purge_users import purge_users
-#        purge_users()
+    @classmethod
+    def teardown_class(self, *args, **kw):
+        from tests.functional.purge_users import purge_users
+        purge_users()
 
     def check_message_delivered(self, subject):
         imap = IMAP()
diff --git a/tests/functional/test_wallace/test_004_nonascii_addresses.py b/tests/functional/test_wallace/test_004_nonascii_addresses.py
index b37e746..45df30e 100644
--- a/tests/functional/test_wallace/test_004_nonascii_addresses.py
+++ b/tests/functional/test_wallace/test_004_nonascii_addresses.py
@@ -43,10 +43,10 @@ class TestWallaceNonASCIIAddresses(unittest.TestCase):
         from tests.functional.synchronize import synchronize_once
         synchronize_once()
 
-#    @classmethod
-#    def teardown_class(self, *args, **kw):
-#        from tests.functional.purge_users import purge_users
-#        purge_users()
+    @classmethod
+    def teardown_class(self, *args, **kw):
+        from tests.functional.purge_users import purge_users
+        purge_users()
 
     def check_message_delivered(self, subject):
         imap = IMAP()


commit 735b07582b67fd06bfbb27a27f8aa3a2cda99702
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri Feb 8 20:58:07 2013 +0100

    Add more functional and unit tests for internationalization

diff --git a/tests/functional/test_wallace/test_003_nonascii_subject.py b/tests/functional/test_wallace/test_003_nonascii_subject.py
new file mode 100644
index 0000000..06028ce
--- /dev/null
+++ b/tests/functional/test_wallace/test_003_nonascii_subject.py
@@ -0,0 +1,126 @@
+# *-* encoding: utf-8 *-*
+from email.header import Header
+from email import message_from_string
+from email.MIMEMultipart import MIMEMultipart
+from email.MIMEBase import MIMEBase
+from email.MIMEImage import MIMEImage
+from email.MIMEText import MIMEText
+from email.Utils import COMMASPACE, formatdate
+from email import Encoders
+import os
+import smtplib
+import time
+import unittest
+
+import pykolab
+from pykolab import wap_client
+from pykolab.auth import Auth
+from pykolab.imap import IMAP
+
+conf = pykolab.getConf()
+
+class TestWallaceNonASCIISubject(unittest.TestCase):
+
+    @classmethod
+    def setup_class(self, *args, **kw):
+        from tests.functional.purge_users import purge_users
+        purge_users()
+
+        self.user = {
+                'local': 'john.doe',
+                'domain': 'example.org'
+            }
+
+        self.send_to = 'john.doe at example.org'
+        self.send_from = 'john.doe at example.org'
+
+        self.message_to = '"Doe, John" <%s>' % (self.send_to)
+        self.message_from = '"Doe, John" <%s>' % (self.send_from)
+
+        from tests.functional.user_add import user_add
+        user_add("John", "Doe")
+        time.sleep(2)
+        from tests.functional.synchronize import synchronize_once
+        synchronize_once()
+
+#    @classmethod
+#    def teardown_class(self, *args, **kw):
+#        from tests.functional.purge_users import purge_users
+#        purge_users()
+
+    def check_message_delivered(self, subject):
+        imap = IMAP()
+        imap.connect()
+        imap.set_acl("user/john.doe at example.org", "cyrus-admin", "lrs")
+        imap.imap.m.select("user/john.doe at example.org")
+
+        found = False
+        max_tries = 20
+
+        while not found and max_tries > 0:
+            max_tries -= 1
+
+            typ, data = imap.imap.m.search(None, 'ALL')
+            for num in data[0].split():
+                typ, msg = imap.imap.m.fetch(num, '(RFC822)')
+                _msg = message_from_string(msg[0][1])
+                if _msg['Subject'] == subject:
+                    found = True
+
+            time.sleep(1)
+
+        return found
+
+    def message_standard_params(self, subject, msg):
+        msg['From'] = self.message_from
+        msg['To'] = self.message_to
+
+        msg['Subject'] = subject
+        msg['Date'] = formatdate(localtime=True)
+
+        return msg
+
+    def send_message(self, msg, _to=None, _from=None):
+        smtp = smtplib.SMTP('localhost', 10026)
+
+        if _to == None:
+            _to = self.send_to
+
+        if _from == None:
+            _from = self.send_from
+
+        smtp.sendmail(_from, _to, msg.as_string())
+
+    def test_001_inbox_created(self):
+        imap = IMAP()
+        imap.connect()
+
+        folders = imap.lm('user/%(local)s@%(domain)s' % (self.user))
+        self.assertEqual(len(folders), 1)
+        
+    def test_002_send_nonascii_subject(self):
+        subject = Header(u"test_002_nonascii_subject chwała")
+        body = "This is a test message"
+        msg = MIMEBase("text", "plain")
+        msg = self.message_standard_params(subject, msg)
+
+        msg.set_payload(body)
+
+        self.send_message(msg)
+
+        if not self.check_message_delivered(subject):
+            raise Exception
+
+    def test_003_send_nonascii_subject(self):
+        subject = Header(u"test_003_nonascii_subject Тест")
+        body = "This is a test message"
+        msg = MIMEBase("text", "plain")
+        msg = self.message_standard_params(subject, msg)
+
+        msg.set_payload(body)
+
+        self.send_message(msg)
+
+        if not self.check_message_delivered(subject):
+            raise Exception
+
diff --git a/tests/functional/test_wallace/test_004_nonascii_addresses.py b/tests/functional/test_wallace/test_004_nonascii_addresses.py
new file mode 100644
index 0000000..b37e746
--- /dev/null
+++ b/tests/functional/test_wallace/test_004_nonascii_addresses.py
@@ -0,0 +1,126 @@
+# *-* encoding: utf-8 *-*
+from email.header import Header
+from email import message_from_string
+from email.MIMEMultipart import MIMEMultipart
+from email.MIMEBase import MIMEBase
+from email.MIMEImage import MIMEImage
+from email.MIMEText import MIMEText
+from email.Utils import COMMASPACE, formatdate
+from email import Encoders
+import os
+import smtplib
+import time
+import unittest
+
+import pykolab
+from pykolab import wap_client
+from pykolab.auth import Auth
+from pykolab.imap import IMAP
+
+conf = pykolab.getConf()
+
+class TestWallaceNonASCIIAddresses(unittest.TestCase):
+
+    @classmethod
+    def setup_class(self, *args, **kw):
+        from tests.functional.purge_users import purge_users
+        purge_users()
+
+        self.user = {
+                'local': 'nikolaj.rimskij-korsakov',
+                'domain': 'example.org'
+            }
+
+        self.send_to = 'nikolaj.rimskij-korsakov at example.org'
+        self.send_from = 'nikolaj.rimskij-korsakov at example.org'
+
+        self.message_to = '"Римский-Корсаков, Николай" <%s>' % (self.send_to)
+        self.message_from = '"Римский-Корсаков, Николай" <%s>' % (self.send_from)
+
+        from tests.functional.user_add import user_add
+        user_add("Николай", "Римский-Корсаков", preferredlanguage='ru_RU')
+        time.sleep(2)
+        from tests.functional.synchronize import synchronize_once
+        synchronize_once()
+
+#    @classmethod
+#    def teardown_class(self, *args, **kw):
+#        from tests.functional.purge_users import purge_users
+#        purge_users()
+
+    def check_message_delivered(self, subject):
+        imap = IMAP()
+        imap.connect()
+        imap.set_acl("user/nikolaj.rimskij-korsakov at example.org", "cyrus-admin", "lrs")
+        imap.imap.m.select("user/nikolaj.rimskij-korsakov at example.org")
+
+        found = False
+        max_tries = 20
+
+        while not found and max_tries > 0:
+            max_tries -= 1
+
+            typ, data = imap.imap.m.search(None, 'ALL')
+            for num in data[0].split():
+                typ, msg = imap.imap.m.fetch(num, '(RFC822)')
+                _msg = message_from_string(msg[0][1])
+                if _msg['Subject'] == subject:
+                    found = True
+
+            time.sleep(1)
+
+        return found
+
+    def message_standard_params(self, subject, msg):
+        msg['From'] = Header(self.message_from)
+        msg['To'] = Header(self.message_to)
+
+        msg['Subject'] = subject
+        msg['Date'] = formatdate(localtime=True)
+
+        return msg
+
+    def send_message(self, msg, _to=None, _from=None):
+        smtp = smtplib.SMTP('localhost', 10026)
+
+        if _to == None:
+            _to = self.send_to
+
+        if _from == None:
+            _from = self.send_from
+
+        smtp.sendmail(_from, _to, msg.as_string())
+
+    def test_001_inbox_created(self):
+        imap = IMAP()
+        imap.connect()
+
+        folders = imap.lm('user/%(local)s@%(domain)s' % (self.user))
+        self.assertEqual(len(folders), 1)
+        
+    def test_002_send_nonascii_addresses(self):
+        subject = Header(u"test_002_nonascii_addresses")
+        body = "This is a test message"
+        msg = MIMEBase("text", "plain")
+        msg = self.message_standard_params(subject, msg)
+
+        msg.set_payload(body)
+
+        self.send_message(msg)
+
+        if not self.check_message_delivered(subject):
+            raise Exception
+
+    def test_003_send_nonascii_subject(self):
+        subject = Header(u"test_003_nonascii_subject Тест")
+        body = "This is a test message"
+        msg = MIMEBase("text", "plain")
+        msg = self.message_standard_params(subject, msg)
+
+        msg.set_payload(body)
+
+        self.send_message(msg)
+
+        if not self.check_message_delivered(subject):
+            raise Exception
+
diff --git a/tests/unit/test-010-transliterate.py b/tests/unit/test-010-transliterate.py
index 3c98a1a..e6b9000 100644
--- a/tests/unit/test-010-transliterate.py
+++ b/tests/unit/test-010-transliterate.py
@@ -99,5 +99,12 @@ class TestTransliteration(unittest.TestCase):
         self.assertEqual('Yuliya', utils.translate(givenname, preferredlanguage))
         self.assertEqual('Yolkina', utils.translate(surname, preferredlanguage))
 
+    def test_009_raw_decode(self):
+        raw_str = r"Николай"
+        self.assertEqual('Николай', raw_str.decode("string_escape"))
+
+        raw_str = r"raw"
+        self.assertEqual('raw', raw_str.decode("string_escape"))
+
 if __name__ == '__main__':
     unittest.main()


commit 394c40ea1ce96a403e322ed12cc5059bd8012a47
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri Feb 8 20:56:02 2013 +0100

    Let's use utf-8 as the common denominator between ordinals

diff --git a/pykolab/translit.py b/pykolab/translit.py
index ac4d2d7..119675f 100644
--- a/pykolab/translit.py
+++ b/pykolab/translit.py
@@ -17,6 +17,11 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 #
 
+import pykolab
+from pykolab.translate import _
+
+log = pykolab.getLogger('pykolab.translit')
+
 locale_translit_map = {
         'ru_RU': 'cyrillic'
     }
@@ -97,9 +102,11 @@ def transliterate(_input, lang, _output_expected=None):
 
     _output = ''
 
-    for char in _input:
+    for char in _input.decode('utf-8'):
         if translit_map[_translit_name].has_key(char):
             _output += translit_map[_translit_name][char]
+        elif char in [repr(x) for x in translit_map[_translit_name].keys()]:
+            _output += translit_map[_translit_name][[char in [raw(x) for x in translit_map[_translit_name].keys()]][0]]
         else:
             _output += char
 


commit df61e5c367ce0915bcedb4842bf8783cdd5b116b
Merge: f145450 8565c4c
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri Feb 8 17:20:26 2013 +0100

    Merge branch 'master' of ssh://git.kolab.org/git/pykolab



commit f14545082062c204dab2504d5e51e7adb9ee1fe5
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri Feb 8 13:28:16 2013 +0100

    If we ensure it is stored as ascii, anything non-ascii gets written out as the ascii equivalent of the unicode character set (\u1234).
    
    See: http://docs.python.org/2/library/json.html
    
    """
    If ensure_ascii is True (the default), all non-ASCII characters in the output
    are escaped with \uXXXX sequences, and the result is a str instance
    consisting of ASCII characters only.
    """

diff --git a/wallace/__init__.py b/wallace/__init__.py
index 1e81eaa..56d7173 100644
--- a/wallace/__init__.py
+++ b/wallace/__init__.py
@@ -247,7 +247,9 @@ class WallaceDaemon(object):
                         'from': mailfrom,
                         'to': rcpttos,
                         'data': data
-                   }
+                   },
+                ensure_ascii=True,
+                indent=4
             )
 
         (fp, filename) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/")





More information about the commits mailing list