test/unittest_req.py
author Rémi Cardona <remi.cardona@logilab.fr>
Mon, 14 Apr 2014 12:27:37 +0200
branchstable
changeset 9702 c2108dbfb508
parent 9386 a33da516028f
child 9654 f788e5e7e6ea
permissions -rw-r--r--
an URI -> a URI
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
9348
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    21
from cubicweb.req import RequestSessionBase, FindEntityError
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):
9386
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    56
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    57
    def test_base_url(self):
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    58
        base_url = self.config['base-url']
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    59
        self.assertEqual(self.session.base_url(), base_url)
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    60
        assert 'https-url' not in self.config
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    61
        self.assertEqual(self.session.base_url(secure=True), base_url)
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    62
        secure_base_url = base_url.replace('http', 'https')
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    63
        self.config.global_set_option('https-url', secure_base_url)
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    64
        self.assertEqual(self.session.base_url(secure=True), secure_base_url)
a33da516028f [req] Return base-url in case https-url is None in base_url()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9348
diff changeset
    65
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
    66
    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
    67
        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
    68
        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
    69
        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
    70
        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
    71
9348
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    72
    def test_find_one_entity(self):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    73
        self.request().create_entity(
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    74
            'CWUser', login=u'cdevienne', upassword=u'cdevienne',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    75
            surname=u'de Vienne', firstname=u'Christophe',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    76
            in_group=self.request().find('CWGroup', name=u'users').one())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    77
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    78
        self.request().create_entity(
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    79
            'CWUser', login=u'adim', upassword='adim', surname=u'di mascio',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    80
            firstname=u'adrien',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    81
            in_group=self.request().find('CWGroup', name=u'users').one())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    82
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    83
        u = self.request().find_one_entity('CWUser', login=u'cdevienne')
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    84
        self.assertEqual(u.firstname, u"Christophe")
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    85
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    86
        with self.assertRaises(FindEntityError):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    87
            self.request().find_one_entity('CWUser', login=u'patanok')
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    88
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    89
        with self.assertRaises(FindEntityError):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    90
            self.request().find_one_entity('CWUser')
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    91
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    92
    def test_find_entities(self):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    93
        self.request().create_entity(
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    94
            'CWUser', login=u'cdevienne', upassword=u'cdevienne',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    95
            surname=u'de Vienne', firstname=u'Christophe',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    96
            in_group=self.request().find('CWGroup', name=u'users').one())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    97
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    98
        self.request().create_entity(
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
    99
            'CWUser', login=u'adim', upassword='adim', surname=u'di mascio',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   100
            firstname=u'adrien',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   101
            in_group=self.request().find('CWGroup', name=u'users').one())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   102
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   103
        l = list(self.request().find_entities('CWUser', login=u'cdevienne'))
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   104
        self.assertEqual(1, len(l))
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   105
        self.assertEqual(l[0].firstname, u"Christophe")
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   106
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   107
        l = list(self.request().find_entities('CWUser', login=u'patanok'))
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   108
        self.assertEqual(0, len(l))
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   109
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   110
        l = list(self.request().find_entities('CWUser'))
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   111
        self.assertEqual(4, len(l))
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   112
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   113
    def test_find(self):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   114
        self.request().create_entity(
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   115
            'CWUser', login=u'cdevienne', upassword=u'cdevienne',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   116
            surname=u'de Vienne', firstname=u'Christophe',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   117
            in_group=self.request().find('CWGroup', name=u'users').one())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   118
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   119
        self.request().create_entity(
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   120
            'CWUser', login=u'adim', upassword='adim', surname=u'di mascio',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   121
            firstname=u'adrien',
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   122
            in_group=self.request().find('CWGroup', name=u'users').one())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   123
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   124
        u = self.request().find('CWUser', login=u'cdevienne').one()
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   125
        self.assertEqual(u.firstname, u"Christophe")
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   126
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   127
        users = list(self.request().find('CWUser').entities())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   128
        self.assertEqual(len(users), 4)
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   129
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   130
        groups = list(
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   131
            self.request().find('CWGroup', reverse_in_group=u).entities())
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   132
        self.assertEqual(len(groups), 1)
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   133
        self.assertEqual(groups[0].name, u'users')
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   134
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   135
        users = self.request().find('CWUser', in_group=groups[0]).entities()
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   136
        users = list(users)
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   137
        self.assertEqual(len(users), 2)
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   138
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   139
        with self.assertRaises(AssertionError):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   140
            self.request().find('CWUser', chapeau=u"melon")
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   141
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   142
        with self.assertRaises(AssertionError):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   143
            self.request().find('CWUser', reverse_buddy=users[0])
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   144
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   145
        with self.assertRaises(NotImplementedError):
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   146
            self.request().find('CWUser', in_group=[1, 2])
eacd02792332 [req] New method: RequestSessionBase.find().
Christophe de Vienne <cdevienne@gmail.com>
parents: 8309
diff changeset
   147
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
if __name__ == '__main__':
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
    unittest_main()