diff -r 000000000000 -r b97547f5f1fa common/test/unittest_mail.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/test/unittest_mail.py Wed Nov 05 15:52:50 2008 +0100 @@ -0,0 +1,122 @@ +# -*- coding: utf-8 -*- +"""unit tests for module cubicweb.common.mail""" + +import os +import pwd + +from logilab.common.testlib import unittest_main +from logilab.common.umessage import message_from_string + +from cubicweb.devtools.apptest import EnvBasedTC +from cubicweb.common.mail import format_mail + + +def getlogin(): + """avoid usinng os.getlogin() because of strange tty / stdin problems + (man 3 getlogin) + Another solution would be to use $LOGNAME, $USER or $USERNAME + """ + return pwd.getpwuid(os.getuid())[0] + + +class EmailTC(EnvBasedTC): + + def test_format_mail(self): + self.set_option('sender-addr', 'bim@boum.fr') + self.set_option('sender-name', 'BimBam') + + mail = format_mail({'name': 'oim', 'email': 'oim@logilab.fr'}, + ['test@logilab.fr'], u'un petit cöucou', u'bïjour', + config=self.config) + self.assertLinesEquals(mail.as_string(), """\ +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: base64 +Subject: =?utf-8?q?b=C3=AFjour?= +From: =?utf-8?q?oim?= +Reply-to: =?utf-8?q?oim?= , =?utf-8?q?BimBam?= +X-CW: data +To: test@logilab.fr + +dW4gcGV0aXQgY8O2dWNvdQ== +""") + msg = message_from_string(mail.as_string()) + self.assertEquals(msg.get('subject'), u'bïjour') + self.assertEquals(msg.get('from'), u'oim ') + self.assertEquals(msg.get('to'), u'test@logilab.fr') + self.assertEquals(msg.get('reply-to'), u'oim , BimBam ') + self.assertEquals(msg.get_payload(decode=True), u'un petit cöucou') + + + def test_format_mail_euro(self): + mail = format_mail({'name': u'oîm', 'email': u'oim@logilab.fr'}, + ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') + self.assertLinesEquals(mail.as_string(), """\ +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: base64 +Subject: =?utf-8?b?YsOvam91ciDigqw=?= +From: =?utf-8?q?o=C3=AEm?= +Reply-to: =?utf-8?q?o=C3=AEm?= +To: test@logilab.fr + +dW4gcGV0aXQgY8O2dWNvdSDigqw= +""") + msg = message_from_string(mail.as_string()) + self.assertEquals(msg.get('subject'), u'bïjour €') + self.assertEquals(msg.get('from'), u'oîm ') + self.assertEquals(msg.get('to'), u'test@logilab.fr') + self.assertEquals(msg.get('reply-to'), u'oîm ') + self.assertEquals(msg.get_payload(decode=True), u'un petit cöucou €') + + + def test_format_mail_from_reply_to(self): + # no sender-name, sender-addr in the configuration + self.set_option('sender-name', '') + self.set_option('sender-addr', '') + msg = format_mail({'name': u'', 'email': u''}, + ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', + config=self.config) + self.assertEquals(msg.get('from'), u'') + self.assertEquals(msg.get('reply-to'), None) + msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, + ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', + config=self.config) + msg = message_from_string(msg.as_string()) + self.assertEquals(msg.get('from'), u'tutu ') + self.assertEquals(msg.get('reply-to'), u'tutu ') + msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, + ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') + msg = message_from_string(msg.as_string()) + self.assertEquals(msg.get('from'), u'tutu ') + self.assertEquals(msg.get('reply-to'), u'tutu ') + # set sender name and address as expected + self.set_option('sender-name', 'cubicweb-test') + self.set_option('sender-addr', 'cubicweb-test@logilab.fr') + # anonymous notification: no name and no email specified + msg = format_mail({'name': u'', 'email': u''}, + ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', + config=self.config) + msg = message_from_string(msg.as_string()) + self.assertEquals(msg.get('from'), u'cubicweb-test ') + self.assertEquals(msg.get('reply-to'), u'cubicweb-test ') + # anonymous notification: only email specified + msg = format_mail({'email': u'tutu@logilab.fr'}, + ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', + config=self.config) + msg = message_from_string(msg.as_string()) + self.assertEquals(msg.get('from'), u'cubicweb-test ') + self.assertEquals(msg.get('reply-to'), u'cubicweb-test , cubicweb-test ') + # anonymous notification: only name specified + msg = format_mail({'name': u'tutu'}, + ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', + config=self.config) + msg = message_from_string(msg.as_string()) + self.assertEquals(msg.get('from'), u'tutu ') + self.assertEquals(msg.get('reply-to'), u'tutu ') + + + +if __name__ == '__main__': + unittest_main() +