sobjects/test/unittest_notification.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Sat, 01 Aug 2009 17:21:19 +0200
changeset 2637 07103211e511
parent 2058 7ef12c03447c
child 2650 18aec79ec3a3
permissions -rw-r--r--
R [test] update and fix deprecation warnings

# -*- coding: iso-8859-1 -*-
"""

:organization: Logilab
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
from socket import gethostname

from logilab.common.testlib import unittest_main, TestCase
from cubicweb.devtools.apptest import EnvBasedTC

from cubicweb.sobjects.notification 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.failIfEqual(msgid1, msgid2)
        self.failIf('&' in msgid1)
        self.failIf('=' in msgid1)
        self.failIf('/' in msgid1)
        self.failIf('+' in msgid1)
        values = parse_message_id(msgid1, 'testapp')
        self.failUnless(values)
        # parse_message_id should work with or without surrounding <>
        self.failUnlessEqual(values, parse_message_id(msgid1[1:-1], 'testapp'))
        self.failUnlessEqual(values['eid'], '21')
        self.failUnless('timestamp' in values)
        self.failUnlessEqual(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.failUnlessEqual(values, {'eid': '21'})

    def test_parse_message_doesnt_raise(self):
        self.failUnlessEqual(parse_message_id('oijioj@bla.bla', 'tesapp'), None)
        self.failUnlessEqual(parse_message_id('oijioj@bla', 'tesapp'), None)
        self.failUnlessEqual(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.assertNotEquals(msgid1, '<@testapp.%s>' % gethostname())


class RecipientsFinderTC(EnvBasedTC):
    def test(self):
        urset = self.execute('CWUser X WHERE X login "admin"')
        self.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X '
                     'WHERE U eid %(x)s', {'x': urset[0][0]})
        self.execute('INSERT CWProperty X: X pkey "ui.language", X value "fr", X for_user U '
                     'WHERE U eid %(x)s', {'x': urset[0][0]})
        self.commit() # commit so that admin get its properties updated
        finder = self.vreg.select('components', 'recipients_finder', self.request(),
                                  rset=urset)
        self.set_option('default-recipients-mode', 'none')
        self.assertEquals(finder.recipients(), [])
        self.set_option('default-recipients-mode', 'users')
        self.assertEquals(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.assertEquals(finder.recipients(), [('abcd@logilab.fr', 'en'), ('efgh@logilab.fr', 'en')])


class StatusChangeViewsTC(EnvBasedTC):

    def test_status_change_view(self):
        req = self.session()
        u = self.create_user('toto', req=req)
        assert u.req
        self.execute('SET X in_state S WHERE X eid %s, S name "deactivated"' % u.eid)
        v = self.vreg.select('views', 'notif_status_change', req, rset=u.rset, row=0)
        content = v.render(row=0, comment='yeah',
                           previous_state='activated',
                           current_state='deactivated')
        # remove date
        self.assertEquals(content,
                          '''
admin changed status from <activated> to <deactivated> for entity
'toto'

yeah

url: http://testing.fr/cubicweb/cwuser/toto
''')
        self.assertEquals(v.subject(), 'status changed cwuser #%s (admin)' % u.eid)

if __name__ == '__main__':
    unittest_main()