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