cubicweb/sobjects/test/unittest_notification.py
changeset 11057 0b59724cb3f2
parent 11011 94bbf6d7eaf8
child 11129 97095348b3ee
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # -*- coding: iso-8859-1 -*-
       
     2 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This file is part of CubicWeb.
       
     6 #
       
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     8 # terms of the GNU Lesser General Public License as published by the Free
       
     9 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    10 # any later version.
       
    11 #
       
    12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    15 # details.
       
    16 #
       
    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/>.
       
    19 
       
    20 from socket import gethostname
       
    21 
       
    22 from logilab.common.testlib import unittest_main, TestCase
       
    23 from cubicweb.devtools.testlib import CubicWebTC, MAILBOX
       
    24 
       
    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 
       
    61 class NotificationTC(CubicWebTC):
       
    62 
       
    63     def test_recipients_finder(self):
       
    64         with self.admin_access.web_request() as req:
       
    65             urset = req.execute('CWUser X WHERE X login "admin"')
       
    66             req.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X '
       
    67                         '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 '
       
    69                         'WHERE U eid %(x)s', {'x': urset[0][0]})
       
    70             req.cnx.commit() # commit so that admin get its properties updated
       
    71             finder = self.vreg['components'].select('recipients_finder',
       
    72                                                     req, rset=urset)
       
    73             self.set_option('default-recipients-mode', 'none')
       
    74             self.assertEqual(finder.recipients(), [])
       
    75             self.set_option('default-recipients-mode', 'users')
       
    76             self.assertEqual(finder.recipients(), [(u'admin@logilab.fr', 'fr')])
       
    77             self.set_option('default-recipients-mode', 'default-dest-addrs')
       
    78             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')])
       
    80 
       
    81     def test_status_change_view(self):
       
    82         with self.admin_access.web_request() as req:
       
    83             u = self.create_user(req, 'toto')
       
    84             iwfable = u.cw_adapt_to('IWorkflowable')
       
    85             iwfable.fire_transition('deactivate', comment=u'yeah')
       
    86             self.assertFalse(MAILBOX)
       
    87             req.cnx.commit()
       
    88             self.assertEqual(len(MAILBOX), 1)
       
    89             email = MAILBOX[0]
       
    90             self.assertEqual(email.content,
       
    91                              '''
       
    92 admin changed status from <activated> to <deactivated> for entity
       
    93 'toto'
       
    94 
       
    95 yeah
       
    96 
       
    97 url: http://testing.fr/cubicweb/cwuser/toto
       
    98 ''')
       
    99             self.assertEqual(email.subject,
       
   100                              'status changed CWUser #%s (admin)' % u.eid)
       
   101 
       
   102 if __name__ == '__main__':
       
   103     unittest_main()