cubicweb/test/unittest_crypto.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 06 Aug 2019 14:26:17 +0200
branch3.26
changeset 12719 9fb4a71f119d
parent 12615 7abe23cbfda1
child 12682 da36da3f89f1
permissions -rw-r--r--
[py3] Pass bytes as "msg" to smtplib.SMTP.sendmail() When passing a unicode string to smtplib.SMTP.sendmail() as "msg" argument, there is an implicit bytes encoding using "ascii" encoding in python3. Of course this does not work if the string contains non-ASCII characters. In fact, config's sendmails method intent to pass bytes to smtplib.SMTP.sendmail() as it uses msg.as_string() method. Unfortunately, in python3, this method returns a unicode string whereas it returns a bytes string in python2; we thus fix this by calling as_bytes() method on python3. As there is no "as_bytes" method in python2, we need to handle python2 compatibility by hand and either call as_string() or as_bytes(). In testlib, where we mock smtplib.SMTP, we need to keep the "msg" argument of Email class (defined in testlib as well) a unicode string. Otherwise, it fails to be parsed by email.message_from_string() (from stdlib) if it is bytes on python3.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12615
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     1
from unittest import TestCase
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     2
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     3
from cubicweb import crypto
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     4
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     5
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     6
class CryptoTC(TestCase):
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     7
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     8
    def test_encrypt_decrypt_roundtrip(self):
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     9
        data = {'a': u'ah', 'b': [1, 2]}
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    10
        seed = 'ssss'
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    11
        crypted = crypto.encrypt(data, seed)
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    12
        decrypted = crypto.decrypt(crypted, seed)
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    13
        self.assertEqual(decrypted, data)
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    14
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    15
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    16
if __name__ == '__main__':
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    17
    import unittest
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    18
    unittest.main()