[test] Gather tests about mail module
authorDenis Laxalde <denis.laxalde@logilab.fr>
Tue, 01 Dec 2015 14:44:55 +0100
changeset 11083 4b18b1027d02
parent 11082 0cdd92261c4a
child 11084 ffd615e626f9
[test] Gather tests about mail module From sobjects/test/unittest_notification.py to test/unittest_mail.py where other tests for mail.py are already. Some style fixes along the way.
sobjects/test/unittest_notification.py
test/unittest_mail.py
--- a/sobjects/test/unittest_notification.py	Tue Jan 05 10:36:17 2016 +0100
+++ b/sobjects/test/unittest_notification.py	Tue Dec 01 14:44:55 2015 +0100
@@ -1,4 +1,3 @@
-# -*- coding: iso-8859-1 -*-
 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
@@ -16,47 +15,10 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+"""Tests for notification sobjects"""
 
-from socket import gethostname
-
-from logilab.common.testlib import unittest_main, TestCase
 from cubicweb.devtools.testlib import CubicWebTC, MAILBOX
 
-from cubicweb.mail import construct_message_id, parse_message_id
-
-class MessageIdTC(TestCase):
-    def test_base(self):
-        msgid1 = construct_message_id('testapp', 21)
-        msgid2 = construct_message_id('testapp', 21)
-        self.assertNotEqual(msgid1, msgid2)
-        self.assertNotIn('&', msgid1)
-        self.assertNotIn('=', msgid1)
-        self.assertNotIn('/', msgid1)
-        self.assertNotIn('+', msgid1)
-        values = parse_message_id(msgid1, 'testapp')
-        self.assertTrue(values)
-        # parse_message_id should work with or without surrounding <>
-        self.assertEqual(values, parse_message_id(msgid1[1:-1], 'testapp'))
-        self.assertEqual(values['eid'], '21')
-        self.assertIn('timestamp', values)
-        self.assertEqual(parse_message_id(msgid1[1:-1], 'anotherapp'), None)
-
-    def test_notimestamp(self):
-        msgid1 = construct_message_id('testapp', 21, False)
-        msgid2 = construct_message_id('testapp', 21, False)
-        values = parse_message_id(msgid1, 'testapp')
-        self.assertEqual(values, {'eid': '21'})
-
-    def test_parse_message_doesnt_raise(self):
-        self.assertEqual(parse_message_id('oijioj@bla.bla', 'tesapp'), None)
-        self.assertEqual(parse_message_id('oijioj@bla', 'tesapp'), None)
-        self.assertEqual(parse_message_id('oijioj', 'tesapp'), None)
-
-
-    def test_nonregr_empty_message_id(self):
-        for eid in (1, 12, 123, 1234):
-            msgid1 = construct_message_id('testapp', eid, 12)
-            self.assertNotEqual(msgid1, '<@testapp.%s>' % gethostname())
 
 class NotificationTC(CubicWebTC):
 
@@ -67,7 +29,7 @@
                         'WHERE U eid %(x)s', {'x': urset[0][0]})
             req.execute('INSERT CWProperty X: X pkey "ui.language", X value "fr", X for_user U '
                         'WHERE U eid %(x)s', {'x': urset[0][0]})
-            req.cnx.commit() # commit so that admin get its properties updated
+            req.cnx.commit()  # commit so that admin get its properties updated
             finder = self.vreg['components'].select('recipients_finder',
                                                     req, rset=urset)
             self.set_option('default-recipients-mode', 'none')
@@ -76,7 +38,8 @@
             self.assertEqual(finder.recipients(), [(u'admin@logilab.fr', 'fr')])
             self.set_option('default-recipients-mode', 'default-dest-addrs')
             self.set_option('default-dest-addrs', 'abcd@logilab.fr, efgh@logilab.fr')
-            self.assertEqual(list(finder.recipients()), [('abcd@logilab.fr', 'en'), ('efgh@logilab.fr', 'en')])
+            self.assertEqual(list(finder.recipients()),
+                             [('abcd@logilab.fr', 'en'), ('efgh@logilab.fr', 'en')])
 
     def test_status_change_view(self):
         with self.admin_access.web_request() as req:
@@ -99,5 +62,7 @@
             self.assertEqual(email.subject,
                              'status changed CWUser #%s (admin)' % u.eid)
 
+
 if __name__ == '__main__':
+    from logilab.common.testlib import unittest_main
     unittest_main()
--- a/test/unittest_mail.py	Tue Jan 05 10:36:17 2016 +0100
+++ b/test/unittest_mail.py	Tue Dec 01 14:44:55 2015 +0100
@@ -16,19 +16,18 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""unit tests for module cubicweb.mail
-
-"""
+"""unit tests for module cubicweb.mail"""
 
 import os
 import re
+from socket import gethostname
 import sys
+from unittest import TestCase
 
-from logilab.common.testlib import unittest_main
 from logilab.common.umessage import message_from_string
 
 from cubicweb.devtools.testlib import CubicWebTC
-from cubicweb.mail import format_mail
+from cubicweb.mail import format_mail, construct_message_id, parse_message_id
 
 
 def getlogin():
@@ -74,7 +73,6 @@
         self.assertEqual(msg.get('reply-to'), u'oim <oim@logilab.fr>, BimBam <bim@boum.fr>')
         self.assertEqual(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 €')
@@ -99,7 +97,6 @@
         self.assertEqual(msg.get('reply-to'), u'oîm <oim@logilab.fr>')
         self.assertEqual(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', '')
@@ -125,18 +122,20 @@
         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)
+                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
+                          config=self.config)
         msg = message_from_string(msg.as_string())
         self.assertEqual(msg.get('from'), u'cubicweb-test <cubicweb-test@logilab.fr>')
         self.assertEqual(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>')
         # 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)
+                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
+                          config=self.config)
         msg = message_from_string(msg.as_string())
         self.assertEqual(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>')
-        self.assertEqual(msg.get('reply-to'), u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>')
+        self.assertEqual(
+            msg.get('reply-to'),
+            u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>')
         # anonymous notification: only name specified
         msg = format_mail({'name': u'tutu'},
                           ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
@@ -146,6 +145,41 @@
         self.assertEqual(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>')
 
 
+class MessageIdTC(TestCase):
+
+    def test_base(self):
+        msgid1 = construct_message_id('testapp', 21)
+        msgid2 = construct_message_id('testapp', 21)
+        self.assertNotEqual(msgid1, msgid2)
+        self.assertNotIn('&', msgid1)
+        self.assertNotIn('=', msgid1)
+        self.assertNotIn('/', msgid1)
+        self.assertNotIn('+', msgid1)
+        values = parse_message_id(msgid1, 'testapp')
+        self.assertTrue(values)
+        # parse_message_id should work with or without surrounding <>
+        self.assertEqual(values, parse_message_id(msgid1[1:-1], 'testapp'))
+        self.assertEqual(values['eid'], '21')
+        self.assertIn('timestamp', values)
+        self.assertEqual(parse_message_id(msgid1[1:-1], 'anotherapp'), None)
+
+    def test_notimestamp(self):
+        msgid1 = construct_message_id('testapp', 21, False)
+        construct_message_id('testapp', 21, False)
+        values = parse_message_id(msgid1, 'testapp')
+        self.assertEqual(values, {'eid': '21'})
+
+    def test_parse_message_doesnt_raise(self):
+        self.assertEqual(parse_message_id('oijioj@bla.bla', 'tesapp'), None)
+        self.assertEqual(parse_message_id('oijioj@bla', 'tesapp'), None)
+        self.assertEqual(parse_message_id('oijioj', 'tesapp'), None)
+
+    def test_nonregr_empty_message_id(self):
+        for eid in (1, 12, 123, 1234):
+            msgid1 = construct_message_id('testapp', eid, 12)
+            self.assertNotEqual(msgid1, '<@testapp.%s>' % gethostname())
+
 
 if __name__ == '__main__':
+    from logilab.common.testlib import unittest_main
     unittest_main()