cubicweb/web/test/unittest_views_basecontrollers.py
changeset 11897 2ceb0bfa4b3f
parent 11725 904ee9cd0cf9
child 12043 b8d2e6b9f548
equal deleted inserted replaced
11892:08cf02efc7ce 11897:2ceb0bfa4b3f
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    25 import lxml
    25 import lxml
    26 
    26 
    27 from logilab.common.testlib import unittest_main
    27 from logilab.common.testlib import unittest_main
    28 from logilab.common.decorators import monkeypatch
    28 from logilab.common.decorators import monkeypatch
    29 
    29 
    30 from cubicweb import Binary, NoSelectableObject, ValidationError, AuthenticationError
    30 from cubicweb import Binary, NoSelectableObject, ValidationError, transaction as tx
    31 from cubicweb.schema import RRQLExpression
    31 from cubicweb.schema import RRQLExpression
       
    32 from cubicweb.predicates import is_instance
    32 from cubicweb.devtools.testlib import CubicWebTC
    33 from cubicweb.devtools.testlib import CubicWebTC
    33 from cubicweb.devtools.webtest import CubicWebTestTC
    34 from cubicweb.devtools.webtest import CubicWebTestTC
    34 from cubicweb.devtools.httptest import CubicWebServerTC
    35 from cubicweb.devtools.httptest import CubicWebServerTC
    35 from cubicweb.utils import json_dumps
    36 from cubicweb.utils import json_dumps
    36 from cubicweb.uilib import rql_for_eid
    37 from cubicweb.uilib import rql_for_eid
    37 from cubicweb.web import Redirect, RemoteCallFailed, http_headers
    38 from cubicweb.web import Redirect, RemoteCallFailed, http_headers, formfields as ff
    38 import cubicweb.server.session
       
    39 from cubicweb.server.session import Connection
       
    40 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
    39 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
    41 from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize
    40 from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize
    42 from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction
    41 from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction
    43 import cubicweb.transaction as tx
    42 from cubicweb.server.session import Connection
    44 from cubicweb.server.hook import Hook, Operation
    43 from cubicweb.server.hook import Hook, Operation
    45 from cubicweb.predicates import is_instance
       
    46 
    44 
    47 
    45 
    48 class ViewControllerTC(CubicWebTestTC):
    46 class ViewControllerTC(CubicWebTestTC):
    49     def test_view_ctrl_with_valid_cache_headers(self):
    47     def test_view_ctrl_with_valid_cache_headers(self):
    50         now = time.time()
    48         now = time.time()
   614                 blogentry2 = req.find('BlogEntry', title=u'entry1-copy').one()
   612                 blogentry2 = req.find('BlogEntry', title=u'entry1-copy').one()
   615                 # entry_of should not be copied
   613                 # entry_of should not be copied
   616                 self.assertEqual(len(blogentry2.entry_of), 0)
   614                 self.assertEqual(len(blogentry2.entry_of), 0)
   617             finally:
   615             finally:
   618                 blogentry.__class__.cw_skip_copy_for = []
   616                 blogentry.__class__.cw_skip_copy_for = []
       
   617 
       
   618     def test_avoid_multiple_process_posted(self):
       
   619         # test that when some entity is being created and data include non-inlined relations, the
       
   620         # values for this relation are stored for later usage, without calling twice field's
       
   621         # process_form method, which may be unexpected for custom fields
       
   622 
       
   623         orig_process_posted = ff.RelationField.process_posted
       
   624 
       
   625         def count_process_posted(self, form):
       
   626             res = list(orig_process_posted(self, form))
       
   627             nb_process_posted_calls[0] += 1
       
   628             return res
       
   629 
       
   630         ff.RelationField.process_posted = count_process_posted
       
   631 
       
   632         try:
       
   633             with self.admin_access.web_request() as req:
       
   634                 gueid = req.execute('CWGroup G WHERE G name "users"')[0][0]
       
   635                 req.form = {
       
   636                     'eid': 'X',
       
   637                     '__type:X': 'CWUser',
       
   638                     '_cw_entity_fields:X': 'login-subject,upassword-subject,in_group-subject',
       
   639                     'login-subject:X': u'adim',
       
   640                     'upassword-subject:X': u'toto', 'upassword-subject-confirm:X': u'toto',
       
   641                     'in_group-subject:X': repr(gueid),
       
   642                 }
       
   643                 nb_process_posted_calls = [0]
       
   644                 self.expect_redirect_handle_request(req, 'edit')
       
   645                 self.assertEqual(nb_process_posted_calls[0], 1)
       
   646                 user = req.find('CWUser', login=u'adim').one()
       
   647                 self.assertEqual(set(g.eid for g in user.in_group), set([gueid]))
       
   648                 req.form = {
       
   649                     'eid': ['X', 'Y'],
       
   650                     '__type:X': 'CWUser',
       
   651                     '_cw_entity_fields:X': 'login-subject,upassword-subject,in_group-subject',
       
   652                     'login-subject:X': u'dlax',
       
   653                     'upassword-subject:X': u'toto', 'upassword-subject-confirm:X': u'toto',
       
   654                     'in_group-subject:X': repr(gueid),
       
   655 
       
   656                     '__type:Y': 'EmailAddress',
       
   657                     '_cw_entity_fields:Y': 'address-subject,use_email-object',
       
   658                     'address-subject:Y': u'dlax@cw.org',
       
   659                     'use_email-object:Y': 'X',
       
   660                 }
       
   661                 nb_process_posted_calls = [0]
       
   662                 self.expect_redirect_handle_request(req, 'edit')
       
   663                 self.assertEqual(nb_process_posted_calls[0], 3)  # 3 = 1 (in_group) + 2 (use_email)
       
   664                 user = req.find('CWUser', login=u'dlax').one()
       
   665                 self.assertEqual(set(e.address for e in user.use_email), set(['dlax@cw.org']))
       
   666 
       
   667         finally:
       
   668             ff.RelationField.process_posted = orig_process_posted
   619 
   669 
   620     def test_nonregr_eetype_etype_editing(self):
   670     def test_nonregr_eetype_etype_editing(self):
   621         """non-regression test checking that a manager user can edit a CWEType entity
   671         """non-regression test checking that a manager user can edit a CWEType entity
   622         """
   672         """
   623         with self.admin_access.web_request() as req:
   673         with self.admin_access.web_request() as req:
   666             self.assertTrue(path.startswith('blogentry/'))
   716             self.assertTrue(path.startswith('blogentry/'))
   667             eid = path.split('/')[1]
   717             eid = path.split('/')[1]
   668             e = req.execute('Any C, T WHERE C eid %(x)s, C content T', {'x': eid}).get_entity(0, 0)
   718             e = req.execute('Any C, T WHERE C eid %(x)s, C content T', {'x': eid}).get_entity(0, 0)
   669             self.assertEqual(e.title, '"13:03:40"')
   719             self.assertEqual(e.title, '"13:03:40"')
   670             self.assertEqual(e.content, '"13:03:43"')
   720             self.assertEqual(e.content, '"13:03:43"')
   671 
       
   672 
   721 
   673     def test_nonregr_multiple_empty_email_addr(self):
   722     def test_nonregr_multiple_empty_email_addr(self):
   674         with self.admin_access.web_request() as req:
   723         with self.admin_access.web_request() as req:
   675             gueid = req.execute('CWGroup G WHERE G name "users"')[0][0]
   724             gueid = req.execute('CWGroup G WHERE G name "users"')[0][0]
   676             req.form = {'eid': ['X', 'Y'],
   725             req.form = {'eid': ['X', 'Y'],