web/test/unittest_views_basecontrollers.py
brancholdstable
changeset 5422 0865e1e90674
parent 5421 8167de96c523
child 5423 e15abfdcce38
child 5424 8ecbcbff9777
equal deleted inserted replaced
4985:02b52bf9f5f8 5422:0865e1e90674
       
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
     1 """cubicweb.web.views.basecontrollers unit tests
    18 """cubicweb.web.views.basecontrollers unit tests
     2 
    19 
     3 :organization: Logilab
       
     4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
       
     7 """
    20 """
     8 import simplejson
       
     9 
    21 
    10 from logilab.common.testlib import unittest_main, mock_object
    22 from logilab.common.testlib import unittest_main, mock_object
    11 
    23 
    12 from cubicweb import Binary, NoSelectableObject, ValidationError
    24 from cubicweb import Binary, NoSelectableObject, ValidationError
    13 from cubicweb.view import STRICT_DOCTYPE
    25 from cubicweb.view import STRICT_DOCTYPE
    14 from cubicweb.devtools.testlib import CubicWebTC
    26 from cubicweb.devtools.testlib import CubicWebTC
    15 from cubicweb.uilib import rql_for_eid
    27 from cubicweb.uilib import rql_for_eid
    16 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError
    28 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError, json
    17 from cubicweb.entities.authobjs import CWUser
    29 from cubicweb.entities.authobjs import CWUser
    18 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
    30 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
    19 u = unicode
    31 u = unicode
    20 
    32 
    21 def req_form(user):
    33 def req_form(user):
    49                     'login-subject:X': u'admin',
    61                     'login-subject:X': u'admin',
    50                     'upassword-subject:X': u'toto',
    62                     'upassword-subject:X': u'toto',
    51                     'upassword-subject-confirm:X': u'toto',
    63                     'upassword-subject-confirm:X': u'toto',
    52                     }
    64                     }
    53         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
    65         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
    54         self.assertEquals(ex.errors, {'login': 'the value "admin" is already used, use another one'})
    66         self.assertEquals(ex.errors, {'login-subject': 'the value "admin" is already used, use another one'})
    55 
    67 
    56     def test_user_editing_itself(self):
    68     def test_user_editing_itself(self):
    57         """checking that a manager user can edit itself
    69         """checking that a manager user can edit itself
    58         """
    70         """
    59         user = self.user()
    71         user = self.user()
   217                     '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
   229                     '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
   218                     'amount-subject:X': u'-10',
   230                     'amount-subject:X': u'-10',
   219                     'described_by_test-subject:X': u(feid),
   231                     'described_by_test-subject:X': u(feid),
   220                 }
   232                 }
   221         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
   233         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
   222         self.assertEquals(ex.errors, {'amount': 'value [0;100] constraint failed for value -10'})
   234         self.assertEquals(ex.errors, {'amount-subject': 'value [0;100] constraint failed for value -10'})
   223         req = self.request()
   235         req = self.request()
   224         req.form = {'eid': ['X'],
   236         req.form = {'eid': ['X'],
   225                     '__type:X': 'Salesterm',
   237                     '__type:X': 'Salesterm',
   226                     '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
   238                     '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
   227                     'amount-subject:X': u'110',
   239                     'amount-subject:X': u'110',
   228                     'described_by_test-subject:X': u(feid),
   240                     'described_by_test-subject:X': u(feid),
   229                     }
   241                     }
   230         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
   242         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
   231         self.assertEquals(ex.errors, {'amount': 'value [0;100] constraint failed for value 110'})
   243         self.assertEquals(ex.errors, {'amount-subject': 'value [0;100] constraint failed for value 110'})
   232         req = self.request()
   244         req = self.request()
   233         req.form = {'eid': ['X'],
   245         req.form = {'eid': ['X'],
   234                     '__type:X': 'Salesterm',
   246                     '__type:X': 'Salesterm',
   235                     '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
   247                     '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
   236                     'amount-subject:X': u'10',
   248                     'amount-subject:X': u'10',
   341         eid = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
   353         eid = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
   342         req.form = {'eid': u(eid), '__type:%s'%eid: 'BlogEntry',
   354         req.form = {'eid': u(eid), '__type:%s'%eid: 'BlogEntry',
   343                     '__action_delete': ''}
   355                     '__action_delete': ''}
   344         path, params = self.expect_redirect_publish(req, 'edit')
   356         path, params = self.expect_redirect_publish(req, 'edit')
   345         self.assertEquals(path, 'blogentry')
   357         self.assertEquals(path, 'blogentry')
   346         self.assertEquals(params, {u'__message': u'entity deleted'})
   358         self.assertIn('_cwmsgid', params)
   347         eid = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
   359         eid = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
   348         self.execute('SET X use_email E WHERE E eid %(e)s, X eid %(x)s',
   360         self.execute('SET X use_email E WHERE E eid %(e)s, X eid %(x)s',
   349                      {'x': self.session.user.eid, 'e': eid}, 'x')
   361                      {'x': self.session.user.eid, 'e': eid}, 'x')
   350         self.commit()
   362         self.commit()
   351         req = req
   363         req = req
   352         req.form = {'eid': u(eid), '__type:%s'%eid: 'EmailAddress',
   364         req.form = {'eid': u(eid), '__type:%s'%eid: 'EmailAddress',
   353                     '__action_delete': ''}
   365                     '__action_delete': ''}
   354         path, params = self.expect_redirect_publish(req, 'edit')
   366         path, params = self.expect_redirect_publish(req, 'edit')
   355         self.assertEquals(path, 'cwuser/admin')
   367         self.assertEquals(path, 'cwuser/admin')
   356         self.assertEquals(params, {u'__message': u'entity deleted'})
   368         self.assertIn('_cwmsgid', params)
   357         eid1 = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
   369         eid1 = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
   358         eid2 = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
   370         eid2 = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
   359         req = self.request()
   371         req = self.request()
   360         req.form = {'eid': [u(eid1), u(eid2)],
   372         req.form = {'eid': [u(eid1), u(eid2)],
   361                     '__type:%s'%eid1: 'BlogEntry',
   373                     '__type:%s'%eid1: 'BlogEntry',
   362                     '__type:%s'%eid2: 'EmailAddress',
   374                     '__type:%s'%eid2: 'EmailAddress',
   363                     '__action_delete': ''}
   375                     '__action_delete': ''}
   364         path, params = self.expect_redirect_publish(req, 'edit')
   376         path, params = self.expect_redirect_publish(req, 'edit')
   365         self.assertEquals(path, 'view')
   377         self.assertEquals(path, 'view')
   366         self.assertEquals(params, {u'__message': u'entities deleted'})
   378         self.assertIn('_cwmsgid', params)
   367 
   379 
   368     def test_nonregr_eetype_etype_editing(self):
   380     def test_nonregr_eetype_etype_editing(self):
   369         """non-regression test checking that a manager user can edit a CWEType entity
   381         """non-regression test checking that a manager user can edit a CWEType entity
   370         """
   382         """
   371         groupeids = sorted(eid for eid, in self.execute('CWGroup G WHERE G name in ("managers", "users")'))
   383         groupeids = sorted(eid for eid, in self.execute('CWGroup G WHERE G name in ("managers", "users")'))
   428                     'address-subject:Y': u'',
   440                     'address-subject:Y': u'',
   429                     'alias-subject:Y': u'',
   441                     'alias-subject:Y': u'',
   430                     'use_email-object:Y': 'X',
   442                     'use_email-object:Y': 'X',
   431                     }
   443                     }
   432         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
   444         ex = self.assertRaises(ValidationError, self.ctrl_publish, req)
   433         self.assertEquals(ex.errors, {'address': u'required attribute'})
   445         self.assertEquals(ex.errors, {'address-subject': u'required field'})
   434 
   446 
   435     def test_nonregr_copy(self):
   447     def test_nonregr_copy(self):
   436         user = self.user()
   448         user = self.user()
   437         req = self.request()
   449         req = self.request()
   438         req.form = {'__maineid' : 'X', 'eid': 'X',
   450         req.form = {'__maineid' : 'X', 'eid': 'X',
   496 
   508 
   497 class ReportBugControllerTC(CubicWebTC):
   509 class ReportBugControllerTC(CubicWebTC):
   498 
   510 
   499     def test_usable_by_guets(self):
   511     def test_usable_by_guets(self):
   500         self.login('anon')
   512         self.login('anon')
   501         self.vreg['controllers'].select('reportbug', self.request())
   513         self.assertRaises(NoSelectableObject,
       
   514                           self.vreg['controllers'].select, 'reportbug', self.request())
       
   515         self.vreg['controllers'].select('reportbug', self.request(description='hop'))
   502 
   516 
   503 
   517 
   504 class SendMailControllerTC(CubicWebTC):
   518 class SendMailControllerTC(CubicWebTC):
   505 
   519 
   506     def test_not_usable_by_guets(self):
   520     def test_not_usable_by_guets(self):
       
   521         self.assertRaises(NoSelectableObject,
       
   522                           self.vreg['controllers'].select, 'sendmail', self.request())
       
   523         self.vreg['controllers'].select('sendmail',
       
   524                                         self.request(subject='toto',
       
   525                                                      recipient='toto@logilab.fr',
       
   526                                                      mailbody='hop'))
   507         self.login('anon')
   527         self.login('anon')
   508         self.assertRaises(NoSelectableObject,
   528         self.assertRaises(NoSelectableObject,
   509                           self.vreg['controllers'].select, 'sendmail', self.request())
   529                           self.vreg['controllers'].select, 'sendmail', self.request())
   510 
   530 
   511 
   531 
   540 
   560 
   541 #     def test_json_exec(self):
   561 #     def test_json_exec(self):
   542 #         rql = 'Any T,N WHERE T is Tag, T name N'
   562 #         rql = 'Any T,N WHERE T is Tag, T name N'
   543 #         ctrl = self.ctrl(self.request(mode='json', rql=rql, pageid='123'))
   563 #         ctrl = self.ctrl(self.request(mode='json', rql=rql, pageid='123'))
   544 #         self.assertEquals(ctrl.publish(),
   564 #         self.assertEquals(ctrl.publish(),
   545 #                           simplejson.dumps(self.execute(rql).rows))
   565 #                           json.dumps(self.execute(rql).rows))
   546 
   566 
   547     def test_remote_add_existing_tag(self):
   567     def test_remote_add_existing_tag(self):
   548         self.remote_call('tag_entity', self.john.eid, ['python'])
   568         self.remote_call('tag_entity', self.john.eid, ['python'])
   549         self.assertUnorderedIterableEquals([tname for tname, in self.execute('Any N WHERE T is Tag, T name N')],
   569         self.assertUnorderedIterableEquals([tname for tname, in self.execute('Any N WHERE T is Tag, T name N')],
   550                              ['python', 'cubicweb'])
   570                              ['python', 'cubicweb'])
   617 
   637 
   618 
   638 
   619     # silly tests
   639     # silly tests
   620     def test_external_resource(self):
   640     def test_external_resource(self):
   621         self.assertEquals(self.remote_call('external_resource', 'RSS_LOGO')[0],
   641         self.assertEquals(self.remote_call('external_resource', 'RSS_LOGO')[0],
   622                           simplejson.dumps(self.request().external_resource('RSS_LOGO')))
   642                           json.dumps(self.request().external_resource('RSS_LOGO')))
   623     def test_i18n(self):
   643     def test_i18n(self):
   624         self.assertEquals(self.remote_call('i18n', ['bimboom'])[0],
   644         self.assertEquals(self.remote_call('i18n', ['bimboom'])[0],
   625                           simplejson.dumps(['bimboom']))
   645                           json.dumps(['bimboom']))
   626 
   646 
   627     def test_format_date(self):
   647     def test_format_date(self):
   628         self.assertEquals(self.remote_call('format_date', '2007-01-01 12:00:00')[0],
   648         self.assertEquals(self.remote_call('format_date', '2007-01-01 12:00:00')[0],
   629                           simplejson.dumps('2007/01/01'))
   649                           json.dumps('2007/01/01'))
   630 
   650 
   631 
   651 
   632 
   652 
   633 
   653 
   634 if __name__ == '__main__':
   654 if __name__ == '__main__':