web/test/unittest_views_basecontrollers.py
changeset 8267 486386d9f836
parent 8128 0a927fe4541b
child 8311 76a44a0d7f4b
equal deleted inserted replaced
8266:704dda5c07f7 8267:486386d9f836
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """cubicweb.web.views.basecontrollers unit tests"""
    18 """cubicweb.web.views.basecontrollers unit tests"""
    19 
    19 
    20 from __future__ import with_statement
    20 from __future__ import with_statement
    21 
    21 
       
    22 from urlparse import urlsplit, urlunsplit, urljoin
       
    23 # parse_qs is deprecated in cgi and has been moved to urlparse in Python 2.6
       
    24 try:
       
    25     from urlparse import parse_qs as url_parse_query
       
    26 except ImportError:
       
    27     from cgi import parse_qs as url_parse_query
    22 from logilab.common.testlib import unittest_main, mock_object
    28 from logilab.common.testlib import unittest_main, mock_object
    23 from logilab.common.decorators import monkeypatch
    29 from logilab.common.decorators import monkeypatch
    24 
    30 
    25 from cubicweb import Binary, NoSelectableObject, ValidationError
    31 from cubicweb import Binary, NoSelectableObject, ValidationError
    26 from cubicweb.view import STRICT_DOCTYPE
    32 from cubicweb.view import STRICT_DOCTYPE
    30 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError, RemoteCallFailed
    36 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError, RemoteCallFailed
    31 from cubicweb.entities.authobjs import CWUser
    37 from cubicweb.entities.authobjs import CWUser
    32 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
    38 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
    33 from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize
    39 from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize
    34 from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction
    40 from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction
       
    41 import cubicweb.transaction as tx
    35 
    42 
    36 u = unicode
    43 u = unicode
    37 
    44 
    38 def req_form(user):
    45 def req_form(user):
    39     return {'eid': [str(user.eid)],
    46     return {'eid': [str(user.eid)],
   766         def js_foo(self):
   773         def js_foo(self):
   767             return 12
   774             return 12
   768         res, req = self.remote_call('foo')
   775         res, req = self.remote_call('foo')
   769         self.assertEqual(res, '12')
   776         self.assertEqual(res, '12')
   770 
   777 
       
   778 
       
   779 
       
   780 
       
   781 
       
   782 class UndoControllerTC(CubicWebTC):
       
   783 
       
   784     def setup_database(self):
       
   785         req = self.request()
       
   786         self.session.undo_actions = True
       
   787         self.toto = self.create_user(req, 'toto', password='toto', groups=('users',),
       
   788                                      commit=False)
       
   789         self.txuuid_toto = self.commit()
       
   790         self.toto_email = self.session.create_entity('EmailAddress',
       
   791                                        address=u'toto@logilab.org',
       
   792                                        reverse_use_email=self.toto)
       
   793         self.txuuid_toto_email = self.commit()
       
   794 
       
   795     def test_no_such_transaction(self):
       
   796         req = self.request()
       
   797         txuuid = u"12345acbd"
       
   798         req.form['txuuid'] = txuuid
       
   799         controller = self.vreg['controllers'].select('undo', req)
       
   800         with self.assertRaises(tx.NoSuchTransaction) as cm:
       
   801             result = controller.publish(rset=None)
       
   802         self.assertEqual(cm.exception.txuuid, txuuid)
       
   803 
       
   804     def assertURLPath(self, url, expected_path, expected_params=None):
       
   805         """ This assert that the path part of `url` matches  expected path
       
   806 
       
   807         TODO : implement assertion on the expected_params too
       
   808         """
       
   809         req = self.request()
       
   810         scheme, netloc, path, query, fragment = urlsplit(url)
       
   811         query_dict = url_parse_query(query)
       
   812         expected_url = urljoin(req.base_url(), expected_path)
       
   813         self.assertEqual( urlunsplit((scheme, netloc, path, None, None)), expected_url)
       
   814 
       
   815     def test_redirect_redirectpath(self):
       
   816         "Check that the potential __redirectpath is honored"
       
   817         req = self.request()
       
   818         txuuid = self.txuuid_toto_email
       
   819         req.form['txuuid'] = txuuid
       
   820         rpath = "toto"
       
   821         req.form['__redirectpath'] = rpath
       
   822         controller = self.vreg['controllers'].select('undo', req)
       
   823         with self.assertRaises(Redirect) as cm:
       
   824             result = controller.publish(rset=None)
       
   825         self.assertURLPath(cm.exception.location, rpath)
       
   826 
       
   827     def test_redirect_default(self):
       
   828         req = self.request()
       
   829         txuuid = self.txuuid_toto_email
       
   830         req.form['txuuid'] = txuuid
       
   831         req.session.data['breadcrumbs'] = [ urljoin(req.base_url(), path)
       
   832                                             for path in ('tata', 'toto',)]
       
   833         controller = self.vreg['controllers'].select('undo', req)
       
   834         with self.assertRaises(Redirect) as cm:
       
   835             result = controller.publish(rset=None)
       
   836         self.assertURLPath(cm.exception.location, 'toto')
       
   837 
       
   838 
   771 if __name__ == '__main__':
   839 if __name__ == '__main__':
   772     unittest_main()
   840     unittest_main()