web/test/unittest_application.py
author Julien Cristau <julien.cristau@logilab.fr>
Mon, 18 May 2015 16:36:26 +0200
changeset 10438 f27a489994e8
parent 10356 a009a31fb1ea
child 10604 d4bf85db41f2
child 11122 fedcb69982af
permissions -rw-r--r--
[devtools] make test db name for postgresql process-specific In case we run two tests concurrently on the same cluster, better not use the same db name for both.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     1
# copyright 2003-2014 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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
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: 5038
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5584
c1823448f81d [web] disallow authenticated users to access to the login form (closes #914873)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    18
"""unit tests for cubicweb.web.application"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
import base64, Cookie
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    21
import httplib
2661
f8df42c9da6b [vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2267
diff changeset
    22
f8df42c9da6b [vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2267
diff changeset
    23
from logilab.common.testlib import TestCase, unittest_main
6849
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
    24
from logilab.common.decorators import clear_cache, classproperty
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
9862
f97a65c29d82 [webtests/application] remove unused imports
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9861
diff changeset
    26
from cubicweb import AuthenticationError
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    27
from cubicweb import view
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    28
from cubicweb.devtools.testlib import CubicWebTC, real_error_handling
2661
f8df42c9da6b [vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2267
diff changeset
    29
from cubicweb.devtools.fake import FakeRequest
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
    30
from cubicweb.web import LogOut, Redirect, INTERNAL_FIELD_VALUE
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
from cubicweb.web.views.basecontrollers import ViewController
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7791
diff changeset
    32
from cubicweb.web.application import anonymized_request
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9069
diff changeset
    33
from cubicweb import repoapi
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
class FakeMapping:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    """emulates a mapping module"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    def __init__(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        self.ENTITIES_MAP = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        self.ATTRIBUTES_MAP = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        self.RELATIONS_MAP = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
class MockCursor:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    def __init__(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        self.executed = []
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5038
diff changeset
    45
    def execute(self, rql, args=None, build_descr=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        args = args or {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        self.executed.append(rql % args)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
class FakeController(ViewController):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
    def __init__(self, form=None):
3462
3a79fecdd2b4 [magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3293
diff changeset
    53
        self._cw = FakeRequest()
3a79fecdd2b4 [magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3293
diff changeset
    54
        self._cw.form = form or {}
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
    55
        self._cursor = MockCursor()
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
    56
        self._cw.execute = self._cursor.execute
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    def new_cursor(self):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
    59
        self._cursor = MockCursor()
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
    60
        self._cw.execute = self._cursor.execute
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
    def set_form(self, form):
3462
3a79fecdd2b4 [magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3293
diff changeset
    63
        self._cw.form = form
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
class RequestBaseTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    def setUp(self):
3462
3a79fecdd2b4 [magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3293
diff changeset
    68
        self._cw = FakeRequest()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
    69
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    def test_list_arg(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        """tests the list_arg() function"""
3462
3a79fecdd2b4 [magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3293
diff changeset
    73
        list_arg = self._cw.list_form_param
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    74
        self.assertEqual(list_arg('arg3', {}), [])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
        d = {'arg1' : "value1",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
             'arg2' : ('foo', INTERNAL_FIELD_VALUE,),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
             'arg3' : ['bar']}
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    78
        self.assertEqual(list_arg('arg1', d, True), ['value1'])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    79
        self.assertEqual(d, {'arg2' : ('foo', INTERNAL_FIELD_VALUE), 'arg3' : ['bar'],})
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    80
        self.assertEqual(list_arg('arg2', d, True), ['foo'])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    81
        self.assertEqual({'arg3' : ['bar'],}, d)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    82
        self.assertEqual(list_arg('arg3', d), ['bar',])
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    83
        self.assertEqual({'arg3' : ['bar'],}, d)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    def test_from_controller(self):
3462
3a79fecdd2b4 [magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3293
diff changeset
    87
        self._cw.vreg['controllers'] = {'view': 1, 'login': 1}
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    88
        self.assertEqual(self._cw.from_controller(), 'view')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
        req = FakeRequest(url='project?vid=list')
3265
96c8363b8f64 test update, no more a FakeVReg, needs to hack it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
    90
        req.vreg['controllers'] = {'view': 1, 'login': 1}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        # this assertion is just to make sure that relative_path can be
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        # correctly computed as it is used in from_controller()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    93
        self.assertEqual(req.relative_path(False), 'project')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    94
        self.assertEqual(req.from_controller(), 'view')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        # test on a valid non-view controller
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        req = FakeRequest(url='login?x=1&y=2')
3265
96c8363b8f64 test update, no more a FakeVReg, needs to hack it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
    97
        req.vreg['controllers'] = {'view': 1, 'login': 1}
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    98
        self.assertEqual(req.relative_path(False), 'login')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
    99
        self.assertEqual(req.from_controller(), 'login')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   101
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
class UtilsTC(TestCase):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    """test suite for misc application utilities"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    def setUp(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        self.ctrl = FakeController()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   107
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
    #def test_which_mapping(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
    #    """tests which mapping is used (application or core)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
    #    init_mapping()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    #    from cubicweb.common import mapping
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   112
    #    self.assertEqual(mapping.MAPPING_USED, 'core')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    #    sys.modules['mapping'] = FakeMapping()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    #    init_mapping()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   115
    #    self.assertEqual(mapping.MAPPING_USED, 'application')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    #    del sys.modules['mapping']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
    def test_execute_linkto(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        """tests the execute_linkto() function"""
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   120
        self.assertEqual(self.ctrl.execute_linkto(), None)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   121
        self.assertEqual(self.ctrl._cursor.executed,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
                          [])
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   123
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:object',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
                              'eid': 8})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
        self.ctrl.execute_linkto()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   127
        self.assertEqual(self.ctrl._cursor.executed,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
                          ['SET Y works_for X WHERE X eid 8, Y eid %s' % i
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
                           for i in (12, 13, 14)])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        self.ctrl.new_cursor()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:subject',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
                              'eid': 8})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        self.ctrl.execute_linkto()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   135
        self.assertEqual(self.ctrl._cursor.executed,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
                          ['SET X works_for Y WHERE X eid 8, Y eid %s' % i
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
                           for i in (12, 13, 14)])
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   138
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        self.ctrl.new_cursor()
3462
3a79fecdd2b4 [magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3293
diff changeset
   141
        self.ctrl._cw.form = {'__linkto' : 'works_for:12_13_14:object'}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        self.ctrl.execute_linkto(eid=8)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   143
        self.assertEqual(self.ctrl._cursor.executed,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
                          ['SET Y works_for X WHERE X eid 8, Y eid %s' % i
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
                           for i in (12, 13, 14)])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        self.ctrl.new_cursor()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        self.ctrl.set_form({'__linkto' : 'works_for:12_13_14:subject'})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
        self.ctrl.execute_linkto(eid=8)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   150
        self.assertEqual(self.ctrl._cursor.executed,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
                          ['SET X works_for Y WHERE X eid 8, Y eid %s' % i
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
                           for i in (12, 13, 14)])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
   155
class ApplicationTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
6849
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   157
    @classproperty
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   158
    def config(cls):
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   159
        try:
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   160
            return cls.__dict__['_config']
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   161
        except KeyError:
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   162
            config = super(ApplicationTC, cls).config
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   163
            config.global_set_option('allow-email-login', True)
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   164
            return config
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   165
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
    def test_cnx_user_groups_sync(self):
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   167
        with self.admin_access.client_cnx() as cnx:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   168
            user = cnx.user
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   169
            self.assertEqual(user.groups, set(('managers',)))
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   170
            cnx.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   171
            user = cnx.user
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   172
            self.assertEqual(user.groups, set(('managers',)))
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   173
            cnx.commit()
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   174
            user = cnx.user
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   175
            self.assertEqual(user.groups, set(('managers', 'guests')))
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   176
            # cleanup
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   177
            cnx.execute('DELETE X in_group G WHERE X eid %s, G name "guests"' % user.eid)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   178
            cnx.commit()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   179
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
    def test_publish_validation_error(self):
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   181
        with self.admin_access.web_request() as req:
9861
e90ff9d594e5 [webtests/application] .user(...) really wants a request object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9674
diff changeset
   182
            user = self.user(req)
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   183
            eid = unicode(user.eid)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   184
            req.form = {
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   185
                'eid':       eid,
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   186
                '__type:'+eid:    'CWUser', '_cw_entity_fields:'+eid: 'login-subject',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   187
                'login-subject:'+eid:     '', # ERROR: no login specified
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   188
                 # just a sample, missing some necessary information for real life
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   189
                '__errorurl': 'view?vid=edition...'
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   190
                }
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   191
            path, params = self.expect_redirect_handle_request(req, 'edit')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   192
            forminfo = req.session.data['view?vid=edition...']
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   193
            eidmap = forminfo['eidmap']
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   194
            self.assertEqual(eidmap, {})
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   195
            values = forminfo['values']
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   196
            self.assertEqual(values['login-subject:'+eid], '')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   197
            self.assertEqual(values['eid'], eid)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   198
            error = forminfo['error']
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   199
            self.assertEqual(error.entity, user.eid)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   200
            self.assertEqual(error.errors['login-subject'], 'required field')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   203
    def test_validation_error_dont_loose_subentity_data_ctrl(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
        """test creation of two linked entities
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   205
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   206
        error occurs on the web controller
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   207
        """
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   208
        with self.admin_access.web_request() as req:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   209
            # set Y before X to ensure both entities are edited, not only X
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   210
            req.form = {'eid': ['Y', 'X'], '__maineid': 'X',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   211
                        '__type:X': 'CWUser', '_cw_entity_fields:X': 'login-subject',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   212
                        # missing required field
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   213
                        'login-subject:X': u'',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   214
                        # but email address is set
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   215
                        '__type:Y': 'EmailAddress', '_cw_entity_fields:Y': 'address-subject',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   216
                        'address-subject:Y': u'bougloup@logilab.fr',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   217
                        'use_email-object:Y': 'X',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   218
                        # necessary to get validation error handling
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   219
                        '__errorurl': 'view?vid=edition...',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   220
                        }
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   221
            path, params = self.expect_redirect_handle_request(req, 'edit')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   222
            forminfo = req.session.data['view?vid=edition...']
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   223
            self.assertEqual(set(forminfo['eidmap']), set('XY'))
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   224
            self.assertEqual(forminfo['eidmap']['X'], None)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   225
            self.assertIsInstance(forminfo['eidmap']['Y'], int)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   226
            self.assertEqual(forminfo['error'].entity, 'X')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   227
            self.assertEqual(forminfo['error'].errors,
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   228
                              {'login-subject': 'required field'})
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   229
            self.assertEqual(forminfo['values'], req.form)
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   230
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   231
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   232
    def test_validation_error_dont_loose_subentity_data_repo(self):
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   233
        """test creation of two linked entities
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   234
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   235
        error occurs on the repository
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   236
        """
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   237
        with self.admin_access.web_request() as req:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   238
            # set Y before X to ensure both entities are edited, not only X
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   239
            req.form = {'eid': ['Y', 'X'], '__maineid': 'X',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   240
                        '__type:X': 'CWUser', '_cw_entity_fields:X': 'login-subject,upassword-subject',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   241
                        # already existent user
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   242
                        'login-subject:X': u'admin',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   243
                        'upassword-subject:X': u'admin', 'upassword-subject-confirm:X': u'admin',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   244
                        '__type:Y': 'EmailAddress', '_cw_entity_fields:Y': 'address-subject',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   245
                        'address-subject:Y': u'bougloup@logilab.fr',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   246
                        'use_email-object:Y': 'X',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   247
                        # necessary to get validation error handling
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   248
                        '__errorurl': 'view?vid=edition...',
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   249
                        }
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   250
            path, params = self.expect_redirect_handle_request(req, 'edit')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   251
            forminfo = req.session.data['view?vid=edition...']
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   252
            self.assertEqual(set(forminfo['eidmap']), set('XY'))
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   253
            self.assertIsInstance(forminfo['eidmap']['X'], int)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   254
            self.assertIsInstance(forminfo['eidmap']['Y'], int)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   255
            self.assertEqual(forminfo['error'].entity, forminfo['eidmap']['X'])
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   256
            self.assertEqual(forminfo['error'].errors,
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   257
                              {'login-subject': u'the value "admin" is already used, use another one'})
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   258
            self.assertEqual(forminfo['values'], req.form)
5038
90493551b1eb [form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   259
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   260
    def test_ajax_view_raise_arbitrary_error(self):
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   261
        class ErrorAjaxView(view.View):
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   262
            __regid__ = 'test.ajax.error'
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   263
            def call(self):
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   264
                raise Exception('whatever')
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   265
        with self.temporary_appobjects(ErrorAjaxView):
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   266
            with real_error_handling(self.app) as app:
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   267
                with self.admin_access.web_request(vid='test.ajax.error') as req:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   268
                    req.ajax_request = True
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   269
                    page = app.handle_request(req, '')
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   270
        self.assertEqual(httplib.INTERNAL_SERVER_ERROR,
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   271
                         req.status_out)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   272
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
    def _test_cleaned(self, kwargs, injected, cleaned):
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   274
        with self.admin_access.web_request(**kwargs) as req:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   275
            page = self.app_handle_request(req, 'view')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   276
            self.assertNotIn(injected, page)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   277
            self.assertIn(cleaned, page)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   278
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
    def test_nonregr_script_kiddies(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
        """test against current script injection"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
        injected = '<i>toto</i>'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
        cleaned = 'toto'
10080
bc7c3b0f439b [web] stop accepting the magic __message form parameter
Julien Cristau <julien.cristau@logilab.fr>
parents: 9882
diff changeset
   283
        for kwargs in ({'vid': injected},
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
                       {'vtitle': injected},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
                       ):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
            yield self._test_cleaned, kwargs, injected, cleaned
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   287
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
    def test_site_wide_eproperties_sync(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
        # XXX work in all-in-one configuration but not in twisted for instance
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
        # in which case we need a kindof repo -> http server notification
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
        # protocol
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
        vreg = self.app.vreg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
        # default value
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   294
        self.assertEqual(vreg.property_value('ui.language'), 'en')
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   295
        with self.admin_access.client_cnx() as cnx:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   296
            cnx.execute('INSERT CWProperty X: X value "fr", X pkey "ui.language"')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   297
            self.assertEqual(vreg.property_value('ui.language'), 'en')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   298
            cnx.commit()
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   299
            self.assertEqual(vreg.property_value('ui.language'), 'fr')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   300
            cnx.execute('SET X value "de" WHERE X pkey "ui.language"')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   301
            self.assertEqual(vreg.property_value('ui.language'), 'fr')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   302
            cnx.commit()
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   303
            self.assertEqual(vreg.property_value('ui.language'), 'de')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   304
            cnx.execute('DELETE CWProperty X WHERE X pkey "ui.language"')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   305
            self.assertEqual(vreg.property_value('ui.language'), 'de')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   306
            cnx.commit()
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   307
            self.assertEqual(vreg.property_value('ui.language'), 'en')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
    # authentication tests ####################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   310
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3524
diff changeset
   311
    def test_http_auth_no_anon(self):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   312
        req, origsession = self.init_authentication('http')
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3524
diff changeset
   313
        self.assertAuthFailure(req)
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8695
diff changeset
   314
        self.app.handle_request(req, 'login')
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8695
diff changeset
   315
        self.assertEqual(401, req.status_out)
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8695
diff changeset
   316
        clear_cache(req, 'get_authorization')
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   317
        authstr = base64.encodestring('%s:%s' % (self.admlogin, self.admpassword))
7224
e5833657c646 [testlib] make a clear distinction between input / output HTTP headers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7163
diff changeset
   318
        req.set_request_header('Authorization', 'basic %s' % authstr)
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   319
        self.assertAuthSuccess(req, origsession)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   320
        self.assertRaises(LogOut, self.app_handle_request, req, 'logout')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   321
        self.assertEqual(len(self.open_sessions), 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    def test_cookie_auth_no_anon(self):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   324
        req, origsession = self.init_authentication('cookie')
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3524
diff changeset
   325
        self.assertAuthFailure(req)
8311
76a44a0d7f4b [login] split authentication logic from post authentication logic (closes #2200755)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7876
diff changeset
   326
        try:
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8695
diff changeset
   327
            form = self.app.handle_request(req, 'login')
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   328
        except Redirect as redir:
8311
76a44a0d7f4b [login] split authentication logic from post authentication logic (closes #2200755)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7876
diff changeset
   329
            self.fail('anonymous user should get login form')
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8695
diff changeset
   330
        clear_cache(req, 'get_authorization')
9674
96549de9dd70 [test] use assertIn where appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 9642
diff changeset
   331
        self.assertIn('__login', form)
96549de9dd70 [test] use assertIn where appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 9642
diff changeset
   332
        self.assertIn('__password', form)
9039
488255d1cf3b [testlib] rework request building in init_authentication
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   333
        self.assertFalse(req.cnx) # Mock cnx are False
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   334
        req.form['__login'] = self.admlogin
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   335
        req.form['__password'] = self.admpassword
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   336
        self.assertAuthSuccess(req, origsession)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   337
        self.assertRaises(LogOut, self.app_handle_request, req, 'logout')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   338
        self.assertEqual(len(self.open_sessions), 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
1490
6b024694d493 add allow-email-login option
Florent <florent@secondweb.fr>
parents: 1489
diff changeset
   340
    def test_login_by_email(self):
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   341
        with self.admin_access.client_cnx() as cnx:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   342
            login = cnx.user.login
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   343
            address = login + u'@localhost'
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   344
            cnx.execute('INSERT EmailAddress X: X address %(address)s, U primary_email X '
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   345
                        'WHERE U login %(login)s', {'address': address, 'login': login})
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   346
            cnx.commit()
6849
5a0c2cfc19bf [repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   347
        # # option allow-email-login not set
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   348
        req, origsession = self.init_authentication('cookie')
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   349
        # req.form['__login'] = address
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   350
        # req.form['__password'] = self.admpassword
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   351
        # self.assertAuthFailure(req)
1490
6b024694d493 add allow-email-login option
Florent <florent@secondweb.fr>
parents: 1489
diff changeset
   352
        # option allow-email-login set
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9069
diff changeset
   353
        #origsession.login = address
1490
6b024694d493 add allow-email-login option
Florent <florent@secondweb.fr>
parents: 1489
diff changeset
   354
        self.set_option('allow-email-login', True)
1489
08acef58ad08 add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents: 1398
diff changeset
   355
        req.form['__login'] = address
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   356
        req.form['__password'] = self.admpassword
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   357
        self.assertAuthSuccess(req, origsession)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   358
        self.assertRaises(LogOut, self.app_handle_request, req, 'logout')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   359
        self.assertEqual(len(self.open_sessions), 0)
1489
08acef58ad08 add a test regarding login with a primary email
Florent <florent@secondweb.fr>
parents: 1398
diff changeset
   360
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
    def _reset_cookie(self, req):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
        # preparing the suite of the test
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
        # set session id in cookie
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
        cookie = Cookie.SimpleCookie()
6797
90d687bd4c52 [web test] fix authentication test: session cookie should now be computed by the session handler (changed in cw 3.10.6)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6796
diff changeset
   365
        sessioncookie = self.app.session_handler.session_cookie(req)
90d687bd4c52 [web test] fix authentication test: session cookie should now be computed by the session handler (changed in cw 3.10.6)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6796
diff changeset
   366
        cookie[sessioncookie] = req.session.sessionid
7224
e5833657c646 [testlib] make a clear distinction between input / output HTTP headers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7163
diff changeset
   367
        req.set_request_header('Cookie', cookie[sessioncookie].OutputString(),
e5833657c646 [testlib] make a clear distinction between input / output HTTP headers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7163
diff changeset
   368
                               raw=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
        clear_cache(req, 'get_authorization')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   370
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3524
diff changeset
   371
    def _test_auth_anon(self, req):
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   372
        asession = self.app.get_session(req)
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9069
diff changeset
   373
        # important otherwise _reset_cookie will not use the right session
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10331
diff changeset
   374
        req.set_cnx(repoapi.Connection(asession))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   375
        self.assertEqual(len(self.open_sessions), 1)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   376
        self.assertEqual(asession.login, 'anon')
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7584
diff changeset
   377
        self.assertTrue(asession.anonymous_session)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3524
diff changeset
   378
        self._reset_cookie(req)
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3524
diff changeset
   379
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   380
    def _test_anon_auth_fail(self, req):
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9069
diff changeset
   381
        self.assertEqual(1, len(self.open_sessions))
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   382
        session = self.app.get_session(req)
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9069
diff changeset
   383
        # important otherwise _reset_cookie will not use the right session
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10331
diff changeset
   384
        req.set_cnx(repoapi.Connection(session))
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   385
        self.assertEqual(req.message, 'authentication failure')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   386
        self.assertEqual(req.session.anonymous_session, True)
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9069
diff changeset
   387
        self.assertEqual(1, len(self.open_sessions))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
        self._reset_cookie(req)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   389
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
    def test_http_auth_anon_allowed(self):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   391
        req, origsession = self.init_authentication('http', 'anon')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
        self._test_auth_anon(req)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
        authstr = base64.encodestring('toto:pouet')
7224
e5833657c646 [testlib] make a clear distinction between input / output HTTP headers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7163
diff changeset
   394
        req.set_request_header('Authorization', 'basic %s' % authstr)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
        self._test_anon_auth_fail(req)
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   396
        authstr = base64.encodestring('%s:%s' % (self.admlogin, self.admpassword))
7224
e5833657c646 [testlib] make a clear distinction between input / output HTTP headers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7163
diff changeset
   397
        req.set_request_header('Authorization', 'basic %s' % authstr)
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   398
        self.assertAuthSuccess(req, origsession)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   399
        self.assertRaises(LogOut, self.app_handle_request, req, 'logout')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5584
diff changeset
   400
        self.assertEqual(len(self.open_sessions), 0)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1490
diff changeset
   401
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
    def test_cookie_auth_anon_allowed(self):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   403
        req, origsession = self.init_authentication('cookie', 'anon')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
        self._test_auth_anon(req)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
        req.form['__login'] = 'toto'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
        req.form['__password'] = 'pouet'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
        self._test_anon_auth_fail(req)
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   408
        req.form['__login'] = self.admlogin
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6797
diff changeset
   409
        req.form['__password'] = self.admpassword
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   410
        self.assertAuthSuccess(req, origsession)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   411
        self.assertRaises(LogOut, self.app_handle_request, req, 'logout')
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9069
diff changeset
   412
        self.assertEqual(0, len(self.open_sessions))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   413
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7791
diff changeset
   414
    def test_anonymized_request(self):
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   415
        with self.admin_access.web_request() as req:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   416
            self.assertEqual(self.admlogin, req.session.user.login)
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   417
            # admin should see anon + admin
9642
9a42eaeea263 [web/test] avoid deprecation warnings from find_entities()
Julien Cristau <julien.cristau@logilab.fr>
parents: 9641
diff changeset
   418
            self.assertEqual(2, len(list(req.find('CWUser'))))
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   419
            with anonymized_request(req):
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   420
                self.assertEqual('anon', req.session.login, 'anon')
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   421
                # anon should only see anon user
9642
9a42eaeea263 [web/test] avoid deprecation warnings from find_entities()
Julien Cristau <julien.cristau@logilab.fr>
parents: 9641
diff changeset
   422
                self.assertEqual(1, len(list(req.find('CWUser'))))
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   423
            self.assertEqual(self.admlogin, req.session.login)
9642
9a42eaeea263 [web/test] avoid deprecation warnings from find_entities()
Julien Cristau <julien.cristau@logilab.fr>
parents: 9641
diff changeset
   424
            self.assertEqual(2, len(list(req.find('CWUser'))))
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7791
diff changeset
   425
3523
16880e7ee3fa don't accept None to avoid error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3265
diff changeset
   426
    def test_non_regr_optional_first_var(self):
9641
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   427
        with self.admin_access.web_request() as req:
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   428
            # expect a rset with None in [0][0]
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   429
            req.form['rql'] = 'rql:Any OV1, X WHERE X custom_workflow OV1?'
4147cc32371d [web/test] port unittest_application to new 3.19 API
Julien Cristau <julien.cristau@logilab.fr>
parents: 9478
diff changeset
   430
            self.app_handle_request(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   431
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   432
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   434
    unittest_main()