web/test/unittest_application.py
changeset 11127 6464edfa95bb
parent 10890 504a67206fdc
parent 11122 fedcb69982af
equal deleted inserted replaced
11113:7f83e6a5acc3 11127:6464edfa95bb
    24 from six.moves.http_cookies import SimpleCookie
    24 from six.moves.http_cookies import SimpleCookie
    25 
    25 
    26 from logilab.common.testlib import TestCase, unittest_main
    26 from logilab.common.testlib import TestCase, unittest_main
    27 from logilab.common.decorators import clear_cache, classproperty
    27 from logilab.common.decorators import clear_cache, classproperty
    28 
    28 
    29 from cubicweb import AuthenticationError
       
    30 from cubicweb import view
    29 from cubicweb import view
    31 from cubicweb.devtools.testlib import CubicWebTC, real_error_handling
    30 from cubicweb.devtools.testlib import CubicWebTC, real_error_handling
    32 from cubicweb.devtools.fake import FakeRequest
    31 from cubicweb.devtools.fake import FakeRequest
    33 from cubicweb.web import LogOut, Redirect, INTERNAL_FIELD_VALUE
    32 from cubicweb.web import LogOut, Redirect, INTERNAL_FIELD_VALUE
    34 from cubicweb.web.views.basecontrollers import ViewController
    33 from cubicweb.web.views.basecontrollers import ViewController
   258             self.assertEqual(forminfo['error'].entity, forminfo['eidmap']['X'])
   257             self.assertEqual(forminfo['error'].entity, forminfo['eidmap']['X'])
   259             self.assertEqual(forminfo['error'].errors,
   258             self.assertEqual(forminfo['error'].errors,
   260                               {'login-subject': u'the value "admin" is already used, use another one'})
   259                               {'login-subject': u'the value "admin" is already used, use another one'})
   261             self.assertEqual(forminfo['values'], req.form)
   260             self.assertEqual(forminfo['values'], req.form)
   262 
   261 
       
   262     def _edit_parent(self, dir_eid, parent_eid, role='subject',
       
   263                      etype='Directory', **kwargs):
       
   264         parent_eid = parent_eid or '__cubicweb_internal_field__'
       
   265         with self.admin_access.web_request() as req:
       
   266             req.form = {
       
   267                 'eid': unicode(dir_eid),
       
   268                 '__maineid': unicode(dir_eid),
       
   269                 '__type:%s' % dir_eid: etype,
       
   270                 'parent-%s:%s' % (role, dir_eid): parent_eid,
       
   271             }
       
   272             req.form.update(kwargs)
       
   273             req.form['_cw_entity_fields:%s' % dir_eid] = ','.join(
       
   274                 ['parent-%s' % role] +
       
   275                 [key.split(':')[0]
       
   276                  for key in kwargs.keys()
       
   277                  if not key.startswith('_')])
       
   278             self.expect_redirect_handle_request(req)
       
   279 
       
   280     def _edit_in_version(self, ticket_eid, version_eid, **kwargs):
       
   281         version_eid = version_eid or '__cubicweb_internal_field__'
       
   282         with self.admin_access.web_request() as req:
       
   283             req.form = {
       
   284                 'eid': unicode(ticket_eid),
       
   285                 '__maineid': unicode(ticket_eid),
       
   286                 '__type:%s' % ticket_eid: 'Ticket',
       
   287                 'in_version-subject:%s' % ticket_eid: version_eid,
       
   288             }
       
   289             req.form.update(kwargs)
       
   290             req.form['_cw_entity_fields:%s' % ticket_eid] = ','.join(
       
   291                 ['in_version-subject'] +
       
   292                 [key.split(':')[0]
       
   293                  for key in kwargs.keys()
       
   294                  if not key.startswith('_')])
       
   295             self.expect_redirect_handle_request(req)
       
   296 
       
   297     def test_create_and_link_directories(self):
       
   298         with self.admin_access.web_request() as req:
       
   299             req.form = {
       
   300                 'eid': (u'A', u'B'),
       
   301                 '__maineid': u'A',
       
   302                 '__type:A': 'Directory',
       
   303                 '__type:B': 'Directory',
       
   304                 'parent-subject:B': u'A',
       
   305                 'name-subject:A': u'topd',
       
   306                 'name-subject:B': u'subd',
       
   307                 '_cw_entity_fields:A': 'name-subject',
       
   308                 '_cw_entity_fields:B': 'parent-subject,name-subject',
       
   309             }
       
   310             self.expect_redirect_handle_request(req)
       
   311 
       
   312         with self.admin_access.repo_cnx() as cnx:
       
   313             self.assertTrue(cnx.find('Directory', name=u'topd'))
       
   314             self.assertTrue(cnx.find('Directory', name=u'subd'))
       
   315             self.assertEqual(1, cnx.execute(
       
   316                 'Directory SUBD WHERE SUBD parent TOPD,'
       
   317                 ' SUBD name "subd", TOPD name "topd"').rowcount)
       
   318 
       
   319     def test_create_subentity(self):
       
   320         with self.admin_access.repo_cnx() as cnx:
       
   321             topd = cnx.create_entity('Directory', name=u'topd')
       
   322             cnx.commit()
       
   323 
       
   324         with self.admin_access.web_request() as req:
       
   325             req.form = {
       
   326                 'eid': (unicode(topd.eid), u'B'),
       
   327                 '__maineid': unicode(topd.eid),
       
   328                 '__type:%s' % topd.eid: 'Directory',
       
   329                 '__type:B': 'Directory',
       
   330                 'parent-object:%s' % topd.eid: u'B',
       
   331                 'name-subject:B': u'subd',
       
   332                 '_cw_entity_fields:%s' % topd.eid: 'parent-object',
       
   333                 '_cw_entity_fields:B': 'name-subject',
       
   334             }
       
   335             self.expect_redirect_handle_request(req)
       
   336 
       
   337         with self.admin_access.repo_cnx() as cnx:
       
   338             self.assertTrue(cnx.find('Directory', name=u'topd'))
       
   339             self.assertTrue(cnx.find('Directory', name=u'subd'))
       
   340             self.assertEqual(1, cnx.execute(
       
   341                 'Directory SUBD WHERE SUBD parent TOPD,'
       
   342                 ' SUBD name "subd", TOPD name "topd"').rowcount)
       
   343 
       
   344     def test_subject_subentity_removal(self):
       
   345         """Editcontroller: detaching a composite relation removes the subentity
       
   346         (edit from the subject side)
       
   347         """
       
   348         with self.admin_access.repo_cnx() as cnx:
       
   349             topd = cnx.create_entity('Directory', name=u'topd')
       
   350             sub1 = cnx.create_entity('Directory', name=u'sub1', parent=topd)
       
   351             sub2 = cnx.create_entity('Directory', name=u'sub2', parent=topd)
       
   352             cnx.commit()
       
   353 
       
   354         attrs = {'name-subject:%s' % sub1.eid: ''}
       
   355         self._edit_parent(sub1.eid, parent_eid=None, **attrs)
       
   356 
       
   357         with self.admin_access.repo_cnx() as cnx:
       
   358             self.assertTrue(cnx.find('Directory', eid=topd.eid))
       
   359             self.assertFalse(cnx.find('Directory', eid=sub1.eid))
       
   360             self.assertTrue(cnx.find('Directory', eid=sub2.eid))
       
   361 
       
   362     def test_object_subentity_removal(self):
       
   363         """Editcontroller: detaching a composite relation removes the subentity
       
   364         (edit from the object side)
       
   365         """
       
   366         with self.admin_access.repo_cnx() as cnx:
       
   367             topd = cnx.create_entity('Directory', name=u'topd')
       
   368             sub1 = cnx.create_entity('Directory', name=u'sub1', parent=topd)
       
   369             sub2 = cnx.create_entity('Directory', name=u'sub2', parent=topd)
       
   370             cnx.commit()
       
   371 
       
   372         self._edit_parent(topd.eid, parent_eid=sub1.eid, role='object')
       
   373 
       
   374         with self.admin_access.repo_cnx() as cnx:
       
   375             self.assertTrue(cnx.find('Directory', eid=topd.eid))
       
   376             self.assertTrue(cnx.find('Directory', eid=sub1.eid))
       
   377             self.assertFalse(cnx.find('Directory', eid=sub2.eid))
       
   378 
       
   379     def test_reparent_subentity(self):
       
   380         "Editcontroller: re-parenting a subentity does not remove it"
       
   381         with self.admin_access.repo_cnx() as cnx:
       
   382             top1 = cnx.create_entity('Directory', name=u'top1')
       
   383             top2 = cnx.create_entity('Directory', name=u'top2')
       
   384             subd = cnx.create_entity('Directory', name=u'subd', parent=top1)
       
   385             cnx.commit()
       
   386 
       
   387         self._edit_parent(subd.eid, parent_eid=top2.eid)
       
   388 
       
   389         with self.admin_access.repo_cnx() as cnx:
       
   390             self.assertTrue(cnx.find('Directory', eid=top1.eid))
       
   391             self.assertTrue(cnx.find('Directory', eid=top2.eid))
       
   392             self.assertTrue(cnx.find('Directory', eid=subd.eid))
       
   393             self.assertEqual(
       
   394                 cnx.find('Directory', eid=subd.eid).one().parent[0], top2)
       
   395 
       
   396     def test_reparent_subentity_inlined(self):
       
   397         """Editcontroller: re-parenting a subentity does not remove it
       
   398         (inlined case)"""
       
   399         with self.admin_access.repo_cnx() as cnx:
       
   400             version1 = cnx.create_entity('Version', name=u'version1')
       
   401             version2 = cnx.create_entity('Version', name=u'version2')
       
   402             ticket = cnx.create_entity('Ticket', title=u'ticket',
       
   403                                        in_version=version1)
       
   404             cnx.commit()
       
   405 
       
   406         self._edit_in_version(ticket.eid, version_eid=version2.eid)
       
   407 
       
   408         with self.admin_access.repo_cnx() as cnx:
       
   409             self.assertTrue(cnx.find('Version', eid=version1.eid))
       
   410             self.assertTrue(cnx.find('Version', eid=version2.eid))
       
   411             self.assertTrue(cnx.find('Ticket', eid=ticket.eid))
       
   412             self.assertEqual(
       
   413                 cnx.find('Ticket', eid=ticket.eid).one().in_version[0], version2)
       
   414 
       
   415     def test_subject_mixed_composite_subentity_removal_1(self):
       
   416         """Editcontroller: detaching several subentities respects each rdef's
       
   417         compositeness - Remove non composite
       
   418         """
       
   419         with self.admin_access.repo_cnx() as cnx:
       
   420             topd = cnx.create_entity('Directory', name=u'topd')
       
   421             fs = cnx.create_entity('Filesystem', name=u'/tmp')
       
   422             subd = cnx.create_entity('Directory', name=u'subd',
       
   423                                      parent=(topd, fs))
       
   424             cnx.commit()
       
   425 
       
   426         self._edit_parent(subd.eid, parent_eid=topd.eid)
       
   427 
       
   428         with self.admin_access.repo_cnx() as cnx:
       
   429             self.assertTrue(cnx.find('Directory', eid=topd.eid))
       
   430             self.assertTrue(cnx.find('Directory', eid=subd.eid))
       
   431             self.assertTrue(cnx.find('Filesystem', eid=fs.eid))
       
   432             self.assertEqual(cnx.find('Directory', eid=subd.eid).one().parent,
       
   433                              [topd,])
       
   434 
       
   435     def test_subject_mixed_composite_subentity_removal_2(self):
       
   436         """Editcontroller: detaching several subentities respects each rdef's
       
   437         compositeness - Remove composite
       
   438         """
       
   439         with self.admin_access.repo_cnx() as cnx:
       
   440             topd = cnx.create_entity('Directory', name=u'topd')
       
   441             fs = cnx.create_entity('Filesystem', name=u'/tmp')
       
   442             subd = cnx.create_entity('Directory', name=u'subd',
       
   443                                      parent=(topd, fs))
       
   444             cnx.commit()
       
   445 
       
   446         self._edit_parent(subd.eid, parent_eid=fs.eid)
       
   447 
       
   448         with self.admin_access.repo_cnx() as cnx:
       
   449             self.assertTrue(cnx.find('Directory', eid=topd.eid))
       
   450             self.assertFalse(cnx.find('Directory', eid=subd.eid))
       
   451             self.assertTrue(cnx.find('Filesystem', eid=fs.eid))
       
   452 
       
   453     def test_object_mixed_composite_subentity_removal_1(self):
       
   454         """Editcontroller: detaching several subentities respects each rdef's
       
   455         compositeness - Remove non composite
       
   456         """
       
   457         with self.admin_access.repo_cnx() as cnx:
       
   458             topd = cnx.create_entity('Directory', name=u'topd')
       
   459             fs = cnx.create_entity('Filesystem', name=u'/tmp')
       
   460             subd = cnx.create_entity('Directory', name=u'subd',
       
   461                                      parent=(topd, fs))
       
   462             cnx.commit()
       
   463 
       
   464         self._edit_parent(fs.eid, parent_eid=None, role='object',
       
   465                           etype='Filesystem')
       
   466 
       
   467         with self.admin_access.repo_cnx() as cnx:
       
   468             self.assertTrue(cnx.find('Directory', eid=topd.eid))
       
   469             self.assertTrue(cnx.find('Directory', eid=subd.eid))
       
   470             self.assertTrue(cnx.find('Filesystem', eid=fs.eid))
       
   471             self.assertEqual(cnx.find('Directory', eid=subd.eid).one().parent,
       
   472                              [topd,])
       
   473 
       
   474     def test_object_mixed_composite_subentity_removal_2(self):
       
   475         """Editcontroller: detaching several subentities respects each rdef's
       
   476         compositeness - Remove composite
       
   477         """
       
   478         with self.admin_access.repo_cnx() as cnx:
       
   479             topd = cnx.create_entity('Directory', name=u'topd')
       
   480             fs = cnx.create_entity('Filesystem', name=u'/tmp')
       
   481             subd = cnx.create_entity('Directory', name=u'subd',
       
   482                                      parent=(topd, fs))
       
   483             cnx.commit()
       
   484 
       
   485         self._edit_parent(topd.eid, parent_eid=None, role='object')
       
   486 
       
   487         with self.admin_access.repo_cnx() as cnx:
       
   488             self.assertTrue(cnx.find('Directory', eid=topd.eid))
       
   489             self.assertFalse(cnx.find('Directory', eid=subd.eid))
       
   490             self.assertTrue(cnx.find('Filesystem', eid=fs.eid))
       
   491 
       
   492     def test_delete_mandatory_composite(self):
       
   493         with self.admin_access.repo_cnx() as cnx:
       
   494             perm = cnx.create_entity('DirectoryPermission')
       
   495             mydir = cnx.create_entity('Directory', name=u'dir',
       
   496                                       has_permission=perm)
       
   497             cnx.commit()
       
   498 
       
   499         with self.admin_access.web_request() as req:
       
   500             dir_eid = unicode(mydir.eid)
       
   501             perm_eid = unicode(perm.eid)
       
   502             req.form = {
       
   503                 'eid': [dir_eid, perm_eid],
       
   504                 '__maineid' : dir_eid,
       
   505                 '__type:%s' % dir_eid: 'Directory',
       
   506                 '__type:%s' % perm_eid: 'DirectoryPermission',
       
   507                 '_cw_entity_fields:%s' % dir_eid: '',
       
   508                 '_cw_entity_fields:%s' % perm_eid: 'has_permission-object',
       
   509                 'has_permission-object:%s' % perm_eid: '',
       
   510                 }
       
   511             path, _params = self.expect_redirect_handle_request(req, 'edit')
       
   512             self.assertTrue(req.find('Directory', eid=mydir.eid))
       
   513             self.assertFalse(req.find('DirectoryPermission', eid=perm.eid))
       
   514 
   263     def test_ajax_view_raise_arbitrary_error(self):
   515     def test_ajax_view_raise_arbitrary_error(self):
   264         class ErrorAjaxView(view.View):
   516         class ErrorAjaxView(view.View):
   265             __regid__ = 'test.ajax.error'
   517             __regid__ = 'test.ajax.error'
   266             def call(self):
   518             def call(self):
   267                 raise Exception('whatever')
   519                 raise Exception('whatever')