sobjects/test/unittest_supervising.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
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 import re
       
    20 
       
    21 from logilab.common.testlib import unittest_main
       
    22 from cubicweb.devtools.testlib import CubicWebTC
       
    23 
       
    24 from cubicweb.sobjects.supervising import SendMailOp, SupervisionMailOp
       
    25 
       
    26 
       
    27 class SupervisingTC(CubicWebTC):
       
    28 
       
    29     def setup_database(self):
       
    30         with self.admin_access.client_cnx() as cnx:
       
    31             cnx.create_entity('Card', title=u"une news !", content=u"cubicweb c'est beau")
       
    32             card = cnx.create_entity('Card', title=u"une autre news !", content=u"cubicweb c'est beau")
       
    33             cnx.create_entity('Bookmark', title=u"un signet !", path=u"view?vid=index")
       
    34             cnx.create_entity('Comment', content=u"Yo !", comments=card)
       
    35             cnx.commit()
       
    36         self.vreg.config.global_set_option('supervising-addrs', 'test@logilab.fr')
       
    37 
       
    38 
       
    39     def test_supervision(self):
       
    40         # do some modification
       
    41         with self.admin_access.repo_cnx() as cnx:
       
    42             user = cnx.execute('INSERT CWUser X: X login "toto", X upassword "sosafe", X in_group G '
       
    43                                 'WHERE G name "users"').get_entity(0, 0)
       
    44             cnx.execute('SET X last_login_time NOW WHERE X eid %(x)s', {'x': user.eid})
       
    45             cnx.execute('DELETE Card B WHERE B title "une news !"')
       
    46             cnx.execute('SET X bookmarked_by U WHERE X is Bookmark, U eid %(x)s', {'x': user.eid})
       
    47             cnx.execute('SET X content "duh?" WHERE X is Comment')
       
    48             cnx.execute('DELETE Comment C WHERE C comments Y, Y is Card, Y title "une autre news !"')
       
    49             # check only one supervision email operation
       
    50             sentops = [op for op in cnx.pending_operations
       
    51                        if isinstance(op, SupervisionMailOp)]
       
    52             self.assertEqual(len(sentops), 1)
       
    53             # check view content
       
    54             op = sentops[0]
       
    55             view = sentops[0]._get_view()
       
    56             self.assertEqual(view.recipients(), ['test@logilab.fr'])
       
    57             self.assertEqual(view.subject(), '[data supervision] changes summary')
       
    58             data = view.render(changes=cnx.transaction_data.get('pendingchanges')).strip()
       
    59             data = re.sub('#\d+', '#EID', data)
       
    60             data = re.sub('/\d+', '/EID', data)
       
    61             self.assertMultiLineEqual('''user admin has made the following change(s):
       
    62 
       
    63 * added cwuser #EID (toto)
       
    64   http://testing.fr/cubicweb/cwuser/toto
       
    65 
       
    66 * added relation in_group from cwuser #EID to cwgroup #EID
       
    67 
       
    68 * deleted card #EID (une news !)
       
    69 
       
    70 * added relation bookmarked_by from bookmark #EID to cwuser #EID
       
    71 
       
    72 * updated comment #EID (duh?)
       
    73   http://testing.fr/cubicweb/comment/EID
       
    74 
       
    75 * deleted comment #EID (duh?)''',
       
    76                               data)
       
    77             # check prepared email
       
    78             op._prepare_email()
       
    79             self.assertEqual(len(op.to_send), 1)
       
    80             self.assertTrue(op.to_send[0][0])
       
    81             self.assertEqual(op.to_send[0][1], ['test@logilab.fr'])
       
    82             cnx.commit()
       
    83             # some other changes #######
       
    84             user.cw_adapt_to('IWorkflowable').fire_transition('deactivate')
       
    85             sentops = [op for op in cnx.pending_operations
       
    86                        if isinstance(op, SupervisionMailOp)]
       
    87             self.assertEqual(len(sentops), 1)
       
    88             # check view content
       
    89             op = sentops[0]
       
    90             view = sentops[0]._get_view()
       
    91             data = view.render(changes=cnx.transaction_data.get('pendingchanges')).strip()
       
    92             data = re.sub('#\d+', '#EID', data)
       
    93             data = re.sub('/\d+', '/EID', data)
       
    94             self.assertMultiLineEqual('''user admin has made the following change(s):
       
    95 
       
    96 * changed state of cwuser #EID (toto)
       
    97   from state activated to state deactivated
       
    98   http://testing.fr/cubicweb/cwuser/toto''',
       
    99                               data)
       
   100 
       
   101     def test_nonregr1(self):
       
   102         with self.admin_access.repo_cnx() as cnx:
       
   103             # do some unlogged modification
       
   104             cnx.execute('SET X last_login_time NOW WHERE X eid %(x)s', {'x': cnx.user.eid})
       
   105             cnx.commit() # no crash
       
   106 
       
   107 
       
   108 if __name__ == '__main__':
       
   109     unittest_main()