web/test/unittest_views_basecontrollers.py
changeset 8128 0a927fe4541b
parent 7791 31bb51ea5485
child 8267 486386d9f836
equal deleted inserted replaced
8125:7070250bf50d 8128:0a927fe4541b
    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 logilab.common.testlib import unittest_main, mock_object
    22 from logilab.common.testlib import unittest_main, mock_object
       
    23 from logilab.common.decorators import monkeypatch
    23 
    24 
    24 from cubicweb import Binary, NoSelectableObject, ValidationError
    25 from cubicweb import Binary, NoSelectableObject, ValidationError
    25 from cubicweb.view import STRICT_DOCTYPE
    26 from cubicweb.view import STRICT_DOCTYPE
    26 from cubicweb.devtools.testlib import CubicWebTC
    27 from cubicweb.devtools.testlib import CubicWebTC
    27 from cubicweb.utils import json_dumps
    28 from cubicweb.utils import json_dumps
    28 from cubicweb.uilib import rql_for_eid
    29 from cubicweb.uilib import rql_for_eid
    29 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError
    30 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError, RemoteCallFailed
    30 from cubicweb.entities.authobjs import CWUser
    31 from cubicweb.entities.authobjs import CWUser
    31 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
    32 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
       
    33 from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize
       
    34 from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction
       
    35 
    32 u = unicode
    36 u = unicode
    33 
    37 
    34 def req_form(user):
    38 def req_form(user):
    35     return {'eid': [str(user.eid)],
    39     return {'eid': [str(user.eid)],
    36             '_cw_entity_fields:%s' % user.eid: '_cw_generic_field',
    40             '_cw_entity_fields:%s' % user.eid: '_cw_generic_field',
   555         self.assertRaises(NoSelectableObject,
   559         self.assertRaises(NoSelectableObject,
   556                           self.vreg['controllers'].select, 'sendmail', self.request())
   560                           self.vreg['controllers'].select, 'sendmail', self.request())
   557 
   561 
   558 
   562 
   559 
   563 
   560 class JSONControllerTC(CubicWebTC):
   564 class AjaxControllerTC(CubicWebTC):
       
   565     tested_controller = 'ajax'
   561 
   566 
   562     def ctrl(self, req=None):
   567     def ctrl(self, req=None):
   563         req = req or self.request(url='http://whatever.fr/')
   568         req = req or self.request(url='http://whatever.fr/')
   564         return self.vreg['controllers'].select('json', req)
   569         return self.vreg['controllers'].select(self.tested_controller, req)
   565 
   570 
   566     def setup_database(self):
   571     def setup_database(self):
   567         req = self.request()
   572         req = self.request()
   568         self.pytag = req.create_entity('Tag', name=u'python')
   573         self.pytag = req.create_entity('Tag', name=u'python')
   569         self.cubicwebtag = req.create_entity('Tag', name=u'cubicweb')
   574         self.cubicwebtag = req.create_entity('Tag', name=u'cubicweb')
   677 
   682 
   678     def test_format_date(self):
   683     def test_format_date(self):
   679         self.assertEqual(self.remote_call('format_date', '2007-01-01 12:00:00')[0],
   684         self.assertEqual(self.remote_call('format_date', '2007-01-01 12:00:00')[0],
   680                           json_dumps('2007/01/01'))
   685                           json_dumps('2007/01/01'))
   681 
   686 
   682 
   687     def test_ajaxfunc_noparameter(self):
   683 
   688         @ajaxfunc
       
   689         def foo(self, x, y):
       
   690             return 'hello'
       
   691         self.assertTrue(issubclass(foo, AjaxFunction))
       
   692         self.assertEqual(foo.__regid__, 'foo')
       
   693         self.assertEqual(foo.check_pageid, False)
       
   694         self.assertEqual(foo.output_type, None)
       
   695         req = self.request()
       
   696         f = foo(req)
       
   697         self.assertEqual(f(12, 13), 'hello')
       
   698 
       
   699     def test_ajaxfunc_checkpageid(self):
       
   700         @ajaxfunc( check_pageid=True)
       
   701         def foo(self, x, y):
       
   702             pass
       
   703         self.assertTrue(issubclass(foo, AjaxFunction))
       
   704         self.assertEqual(foo.__regid__, 'foo')
       
   705         self.assertEqual(foo.check_pageid, True)
       
   706         self.assertEqual(foo.output_type, None)
       
   707         # no pageid
       
   708         req = self.request()
       
   709         f = foo(req)
       
   710         self.assertRaises(RemoteCallFailed, f, 12, 13)
       
   711 
       
   712     def test_ajaxfunc_json(self):
       
   713         @ajaxfunc(output_type='json')
       
   714         def foo(self, x, y):
       
   715             return x + y
       
   716         self.assertTrue(issubclass(foo, AjaxFunction))
       
   717         self.assertEqual(foo.__regid__, 'foo')
       
   718         self.assertEqual(foo.check_pageid, False)
       
   719         self.assertEqual(foo.output_type, 'json')
       
   720         # no pageid
       
   721         req = self.request()
       
   722         f = foo(req)
       
   723         self.assertEqual(f(12, 13), '25')
       
   724 
       
   725 
       
   726 class JSonControllerTC(AjaxControllerTC):
       
   727     # NOTE: this class performs the same tests as AjaxController but with
       
   728     #       deprecated 'json' controller (i.e. check backward compatibility)
       
   729     tested_controller = 'json'
       
   730 
       
   731     def setUp(self):
       
   732         super(JSonControllerTC, self).setUp()
       
   733         self.exposed_remote_funcs = [fname for fname in dir(JSonController)
       
   734                                      if fname.startswith('js_')]
       
   735 
       
   736     def tearDown(self):
       
   737         super(JSonControllerTC, self).tearDown()
       
   738         for funcname in dir(JSonController):
       
   739             # remove functions added dynamically during tests
       
   740             if funcname.startswith('js_') and funcname not in self.exposed_remote_funcs:
       
   741                 delattr(JSonController, funcname)
       
   742 
       
   743     def test_monkeypatch_jsoncontroller(self):
       
   744         self.assertRaises(RemoteCallFailed, self.remote_call, 'foo')
       
   745         @monkeypatch(JSonController)
       
   746         def js_foo(self):
       
   747             return u'hello'
       
   748         res, req = self.remote_call('foo')
       
   749         self.assertEqual(res, u'hello')
       
   750 
       
   751     def test_monkeypatch_jsoncontroller_xhtmlize(self):
       
   752         self.assertRaises(RemoteCallFailed, self.remote_call, 'foo')
       
   753         @monkeypatch(JSonController)
       
   754         @xhtmlize
       
   755         def js_foo(self):
       
   756             return u'hello'
       
   757         res, req = self.remote_call('foo')
       
   758         self.assertEqual(res,
       
   759                          '<?xml version="1.0"?>\n' + STRICT_DOCTYPE +
       
   760                          u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">hello</div>')
       
   761 
       
   762     def test_monkeypatch_jsoncontroller_jsonize(self):
       
   763         self.assertRaises(RemoteCallFailed, self.remote_call, 'foo')
       
   764         @monkeypatch(JSonController)
       
   765         @jsonize
       
   766         def js_foo(self):
       
   767             return 12
       
   768         res, req = self.remote_call('foo')
       
   769         self.assertEqual(res, '12')
   684 
   770 
   685 if __name__ == '__main__':
   771 if __name__ == '__main__':
   686     unittest_main()
   772     unittest_main()