14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
15 # details. |
14 # details. |
16 # |
15 # |
17 # You should have received a copy of the GNU Lesser General Public License along |
16 # 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/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """Tests for notification sobjects""" |
19 |
19 |
20 from socket import gethostname |
|
21 |
|
22 from logilab.common.testlib import unittest_main, TestCase |
|
23 from cubicweb.devtools.testlib import CubicWebTC, MAILBOX |
20 from cubicweb.devtools.testlib import CubicWebTC, MAILBOX |
24 |
21 |
25 from cubicweb.mail import construct_message_id, parse_message_id |
|
26 |
|
27 class MessageIdTC(TestCase): |
|
28 def test_base(self): |
|
29 msgid1 = construct_message_id('testapp', 21) |
|
30 msgid2 = construct_message_id('testapp', 21) |
|
31 self.assertNotEqual(msgid1, msgid2) |
|
32 self.assertNotIn('&', msgid1) |
|
33 self.assertNotIn('=', msgid1) |
|
34 self.assertNotIn('/', msgid1) |
|
35 self.assertNotIn('+', msgid1) |
|
36 values = parse_message_id(msgid1, 'testapp') |
|
37 self.assertTrue(values) |
|
38 # parse_message_id should work with or without surrounding <> |
|
39 self.assertEqual(values, parse_message_id(msgid1[1:-1], 'testapp')) |
|
40 self.assertEqual(values['eid'], '21') |
|
41 self.assertIn('timestamp', values) |
|
42 self.assertEqual(parse_message_id(msgid1[1:-1], 'anotherapp'), None) |
|
43 |
|
44 def test_notimestamp(self): |
|
45 msgid1 = construct_message_id('testapp', 21, False) |
|
46 msgid2 = construct_message_id('testapp', 21, False) |
|
47 values = parse_message_id(msgid1, 'testapp') |
|
48 self.assertEqual(values, {'eid': '21'}) |
|
49 |
|
50 def test_parse_message_doesnt_raise(self): |
|
51 self.assertEqual(parse_message_id('oijioj@bla.bla', 'tesapp'), None) |
|
52 self.assertEqual(parse_message_id('oijioj@bla', 'tesapp'), None) |
|
53 self.assertEqual(parse_message_id('oijioj', 'tesapp'), None) |
|
54 |
|
55 |
|
56 def test_nonregr_empty_message_id(self): |
|
57 for eid in (1, 12, 123, 1234): |
|
58 msgid1 = construct_message_id('testapp', eid, 12) |
|
59 self.assertNotEqual(msgid1, '<@testapp.%s>' % gethostname()) |
|
60 |
22 |
61 class NotificationTC(CubicWebTC): |
23 class NotificationTC(CubicWebTC): |
62 |
24 |
63 def test_recipients_finder(self): |
25 def test_recipients_finder(self): |
64 with self.admin_access.web_request() as req: |
26 with self.admin_access.web_request() as req: |
65 urset = req.execute('CWUser X WHERE X login "admin"') |
27 urset = req.execute('CWUser X WHERE X login "admin"') |
66 req.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X ' |
28 req.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X ' |
67 'WHERE U eid %(x)s', {'x': urset[0][0]}) |
29 'WHERE U eid %(x)s', {'x': urset[0][0]}) |
68 req.execute('INSERT CWProperty X: X pkey "ui.language", X value "fr", X for_user U ' |
30 req.execute('INSERT CWProperty X: X pkey "ui.language", X value "fr", X for_user U ' |
69 'WHERE U eid %(x)s', {'x': urset[0][0]}) |
31 'WHERE U eid %(x)s', {'x': urset[0][0]}) |
70 req.cnx.commit() # commit so that admin get its properties updated |
32 req.cnx.commit() # commit so that admin get its properties updated |
71 finder = self.vreg['components'].select('recipients_finder', |
33 finder = self.vreg['components'].select('recipients_finder', |
72 req, rset=urset) |
34 req, rset=urset) |
73 self.set_option('default-recipients-mode', 'none') |
35 self.set_option('default-recipients-mode', 'none') |
74 self.assertEqual(finder.recipients(), []) |
36 self.assertEqual(finder.recipients(), []) |
75 self.set_option('default-recipients-mode', 'users') |
37 self.set_option('default-recipients-mode', 'users') |
76 self.assertEqual(finder.recipients(), [(u'admin@logilab.fr', 'fr')]) |
38 self.assertEqual(finder.recipients(), [(u'admin@logilab.fr', 'fr')]) |
77 self.set_option('default-recipients-mode', 'default-dest-addrs') |
39 self.set_option('default-recipients-mode', 'default-dest-addrs') |
78 self.set_option('default-dest-addrs', 'abcd@logilab.fr, efgh@logilab.fr') |
40 self.set_option('default-dest-addrs', 'abcd@logilab.fr, efgh@logilab.fr') |
79 self.assertEqual(list(finder.recipients()), [('abcd@logilab.fr', 'en'), ('efgh@logilab.fr', 'en')]) |
41 self.assertEqual(list(finder.recipients()), |
|
42 [('abcd@logilab.fr', 'en'), ('efgh@logilab.fr', 'en')]) |
80 |
43 |
81 def test_status_change_view(self): |
44 def test_status_change_view(self): |
82 with self.admin_access.web_request() as req: |
45 with self.admin_access.web_request() as req: |
83 u = self.create_user(req, 'toto') |
46 u = self.create_user(req, 'toto') |
84 iwfable = u.cw_adapt_to('IWorkflowable') |
47 iwfable = u.cw_adapt_to('IWorkflowable') |