cubicweb/test/unittest_mail.py
changeset 11129 97095348b3ee
parent 11083 4b18b1027d02
parent 11057 0b59724cb3f2
equal deleted inserted replaced
11128:9b4de34ad394 11129:97095348b3ee
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    15 # details.
    15 # details.
    16 #
    16 #
    17 # You should have received a copy of the GNU Lesser General Public License along
    17 # You should have received a copy of the GNU Lesser General Public License along
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    19 """unit tests for module cubicweb.mail
    19 """unit tests for module cubicweb.mail"""
    20 
       
    21 """
       
    22 
    20 
    23 import os
    21 import os
    24 import re
    22 import re
       
    23 from socket import gethostname
    25 import sys
    24 import sys
       
    25 from unittest import TestCase
    26 
    26 
    27 from logilab.common.testlib import unittest_main
       
    28 from logilab.common.umessage import message_from_string
    27 from logilab.common.umessage import message_from_string
    29 
    28 
    30 from cubicweb.devtools.testlib import CubicWebTC
    29 from cubicweb.devtools.testlib import CubicWebTC
    31 from cubicweb.mail import format_mail
    30 from cubicweb.mail import format_mail, construct_message_id, parse_message_id
    32 
    31 
    33 
    32 
    34 def getlogin():
    33 def getlogin():
    35     """avoid using os.getlogin() because of strange tty / stdin problems
    34     """avoid using os.getlogin() because of strange tty / stdin problems
    36     (man 3 getlogin)
    35     (man 3 getlogin)
    72         self.assertEqual(msg.get('from'), u'oim <oim@logilab.fr>')
    71         self.assertEqual(msg.get('from'), u'oim <oim@logilab.fr>')
    73         self.assertEqual(msg.get('to'), u'test@logilab.fr')
    72         self.assertEqual(msg.get('to'), u'test@logilab.fr')
    74         self.assertEqual(msg.get('reply-to'), u'oim <oim@logilab.fr>, BimBam <bim@boum.fr>')
    73         self.assertEqual(msg.get('reply-to'), u'oim <oim@logilab.fr>, BimBam <bim@boum.fr>')
    75         self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou')
    74         self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou')
    76 
    75 
    77 
       
    78     def test_format_mail_euro(self):
    76     def test_format_mail_euro(self):
    79         mail = format_mail({'name': u'oîm', 'email': u'oim@logilab.fr'},
    77         mail = format_mail({'name': u'oîm', 'email': u'oim@logilab.fr'},
    80                            ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €')
    78                            ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €')
    81         result = mail.as_string()
    79         result = mail.as_string()
    82         result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE)
    80         result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE)
    96         self.assertEqual(msg.get('subject'), u'bïjour €')
    94         self.assertEqual(msg.get('subject'), u'bïjour €')
    97         self.assertEqual(msg.get('from'), u'oîm <oim@logilab.fr>')
    95         self.assertEqual(msg.get('from'), u'oîm <oim@logilab.fr>')
    98         self.assertEqual(msg.get('to'), u'test@logilab.fr')
    96         self.assertEqual(msg.get('to'), u'test@logilab.fr')
    99         self.assertEqual(msg.get('reply-to'), u'oîm <oim@logilab.fr>')
    97         self.assertEqual(msg.get('reply-to'), u'oîm <oim@logilab.fr>')
   100         self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou €')
    98         self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou €')
   101 
       
   102 
    99 
   103     def test_format_mail_from_reply_to(self):
   100     def test_format_mail_from_reply_to(self):
   104         # no sender-name, sender-addr in the configuration
   101         # no sender-name, sender-addr in the configuration
   105         self.set_option('sender-name', '')
   102         self.set_option('sender-name', '')
   106         self.set_option('sender-addr', '')
   103         self.set_option('sender-addr', '')
   123         # set sender name and address as expected
   120         # set sender name and address as expected
   124         self.set_option('sender-name', 'cubicweb-test')
   121         self.set_option('sender-name', 'cubicweb-test')
   125         self.set_option('sender-addr', 'cubicweb-test@logilab.fr')
   122         self.set_option('sender-addr', 'cubicweb-test@logilab.fr')
   126         # anonymous notification: no name and no email specified
   123         # anonymous notification: no name and no email specified
   127         msg = format_mail({'name': u'', 'email': u''},
   124         msg = format_mail({'name': u'', 'email': u''},
   128                            ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
   125                           ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
   129                            config=self.config)
   126                           config=self.config)
   130         msg = message_from_string(msg.as_string())
   127         msg = message_from_string(msg.as_string())
   131         self.assertEqual(msg.get('from'), u'cubicweb-test <cubicweb-test@logilab.fr>')
   128         self.assertEqual(msg.get('from'), u'cubicweb-test <cubicweb-test@logilab.fr>')
   132         self.assertEqual(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>')
   129         self.assertEqual(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>')
   133         # anonymous notification: only email specified
   130         # anonymous notification: only email specified
   134         msg = format_mail({'email': u'tutu@logilab.fr'},
   131         msg = format_mail({'email': u'tutu@logilab.fr'},
   135                            ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
   132                           ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
   136                            config=self.config)
   133                           config=self.config)
   137         msg = message_from_string(msg.as_string())
   134         msg = message_from_string(msg.as_string())
   138         self.assertEqual(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>')
   135         self.assertEqual(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>')
   139         self.assertEqual(msg.get('reply-to'), u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>')
   136         self.assertEqual(
       
   137             msg.get('reply-to'),
       
   138             u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>')
   140         # anonymous notification: only name specified
   139         # anonymous notification: only name specified
   141         msg = format_mail({'name': u'tutu'},
   140         msg = format_mail({'name': u'tutu'},
   142                           ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
   141                           ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
   143                           config=self.config)
   142                           config=self.config)
   144         msg = message_from_string(msg.as_string())
   143         msg = message_from_string(msg.as_string())
   145         self.assertEqual(msg.get('from'), u'tutu <cubicweb-test@logilab.fr>')
   144         self.assertEqual(msg.get('from'), u'tutu <cubicweb-test@logilab.fr>')
   146         self.assertEqual(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>')
   145         self.assertEqual(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>')
   147 
   146 
   148 
   147 
       
   148 class MessageIdTC(TestCase):
       
   149 
       
   150     def test_base(self):
       
   151         msgid1 = construct_message_id('testapp', 21)
       
   152         msgid2 = construct_message_id('testapp', 21)
       
   153         self.assertNotEqual(msgid1, msgid2)
       
   154         self.assertNotIn('&', msgid1)
       
   155         self.assertNotIn('=', msgid1)
       
   156         self.assertNotIn('/', msgid1)
       
   157         self.assertNotIn('+', msgid1)
       
   158         values = parse_message_id(msgid1, 'testapp')
       
   159         self.assertTrue(values)
       
   160         # parse_message_id should work with or without surrounding <>
       
   161         self.assertEqual(values, parse_message_id(msgid1[1:-1], 'testapp'))
       
   162         self.assertEqual(values['eid'], '21')
       
   163         self.assertIn('timestamp', values)
       
   164         self.assertEqual(parse_message_id(msgid1[1:-1], 'anotherapp'), None)
       
   165 
       
   166     def test_notimestamp(self):
       
   167         msgid1 = construct_message_id('testapp', 21, False)
       
   168         construct_message_id('testapp', 21, False)
       
   169         values = parse_message_id(msgid1, 'testapp')
       
   170         self.assertEqual(values, {'eid': '21'})
       
   171 
       
   172     def test_parse_message_doesnt_raise(self):
       
   173         self.assertEqual(parse_message_id('oijioj@bla.bla', 'tesapp'), None)
       
   174         self.assertEqual(parse_message_id('oijioj@bla', 'tesapp'), None)
       
   175         self.assertEqual(parse_message_id('oijioj', 'tesapp'), None)
       
   176 
       
   177     def test_nonregr_empty_message_id(self):
       
   178         for eid in (1, 12, 123, 1234):
       
   179             msgid1 = construct_message_id('testapp', eid, 12)
       
   180             self.assertNotEqual(msgid1, '<@testapp.%s>' % gethostname())
       
   181 
   149 
   182 
   150 if __name__ == '__main__':
   183 if __name__ == '__main__':
       
   184     from logilab.common.testlib import unittest_main
   151     unittest_main()
   185     unittest_main()