test/unittest_req.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 18 Nov 2016 17:08:35 +0100
brancholdstable
changeset 11843 c0a49a137217
parent 8309 48ef505aa9f9
child 9348 eacd02792332
permissions -rw-r--r--
Closing "oldstable" branch We now release-based branch naming.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7888
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
7888
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    18
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
from logilab.common.testlib import TestCase, unittest_main
7888
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    20
from cubicweb import ObjectNotFound
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
from cubicweb.req import RequestSessionBase
6321
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    22
from cubicweb.devtools.testlib import CubicWebTC
6747
63bf61e4e130 [security] fix access control problem
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    23
from cubicweb import Unauthorized
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
7888
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    25
class RequestTC(TestCase):
6321
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    26
    def test_rebuild_url(self):
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
        rebuild_url = RequestSessionBase(None).rebuild_url
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6321
diff changeset
    28
        self.assertEqual(rebuild_url('http://logilab.fr?__message=pouet', __message='hop'),
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
                          'http://logilab.fr?__message=hop')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6321
diff changeset
    30
        self.assertEqual(rebuild_url('http://logilab.fr', __message='hop'),
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
                          'http://logilab.fr?__message=hop')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6321
diff changeset
    32
        self.assertEqual(rebuild_url('http://logilab.fr?vid=index', __message='hop'),
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
                          'http://logilab.fr?__message=hop&vid=index')
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
6321
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    35
    def test_build_url(self):
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    36
        req = RequestSessionBase(None)
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    37
        req.from_controller = lambda : 'view'
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    38
        req.relative_path = lambda includeparams=True: None
8309
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8190
diff changeset
    39
        req.base_url = lambda secure=None: 'http://testing.fr/cubicweb/'
6321
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    40
        self.assertEqual(req.build_url(), u'http://testing.fr/cubicweb/view')
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    41
        self.assertEqual(req.build_url(None), u'http://testing.fr/cubicweb/view')
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    42
        self.assertEqual(req.build_url('one'), u'http://testing.fr/cubicweb/one')
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    43
        self.assertEqual(req.build_url(param='ok'), u'http://testing.fr/cubicweb/view?param=ok')
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    44
        self.assertRaises(AssertionError, req.build_url, 'one', 'two not allowed')
6595
00cd0b273cf5 [test] fix test to follow recent changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    45
        self.assertRaises(AssertionError, req.build_url, 'view', test=None)
6321
a8a33679f4dd [req] build_url: detect None values in arguments and raise ValueError
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5424
diff changeset
    46
6747
63bf61e4e130 [security] fix access control problem
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    47
    def test_ensure_no_rql(self):
63bf61e4e130 [security] fix access control problem
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    48
        req = RequestSessionBase(None)
63bf61e4e130 [security] fix access control problem
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    49
        self.assertEqual(req.ensure_ro_rql('Any X WHERE X is CWUser'), None)
63bf61e4e130 [security] fix access control problem
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    50
        self.assertEqual(req.ensure_ro_rql('  Any X WHERE X is CWUser  '), None)
63bf61e4e130 [security] fix access control problem
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    51
        self.assertRaises(Unauthorized, req.ensure_ro_rql, 'SET X login "toto" WHERE X is CWUser')
63bf61e4e130 [security] fix access control problem
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    52
        self.assertRaises(Unauthorized, req.ensure_ro_rql, '   SET X login "toto" WHERE X is CWUser   ')
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
7888
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    54
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    55
class RequestCWTC(CubicWebTC):
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    56
    def test_view_catch_ex(self):
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    57
        req = self.request()
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    58
        rset = self.execute('CWUser X WHERE X login "hop"')
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    59
        self.assertEqual(req.view('oneline', rset, 'null'), '')
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    60
        self.assertRaises(ObjectNotFound, req.view, 'onelinee', rset, 'null')
e257b4476454 [req] .view should not catch [Object|Registry]NotFound (which is a bug), only NoSelectableObject (closes #1973681)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
    61
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
if __name__ == '__main__':
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
    unittest_main()