cubicweb/web/test/test_views.py
author Laura Médioni <laura.medioni@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 03 Nov 2016 15:00:01 +0100
changeset 11794 d8830e2bd2e0
parent 11681 b23d58050076
permissions -rw-r--r--
[req,web] Make it possible to handle page language from URL prefix Adding a short language prefix to URL (like "/en" or "/fr") changes the language the pages are displayed in. This prefix is kept during navigation. This way it is not necessary to do language negotiation, nor to use user preferences to determine which language to apply. This behavior is controlled through a new configuration option "language-mode", which replaces "language-negociation" option and which values may be "http-negotiation", "url-prefix" or "" (to disable language setting and force using "ui.language" property). Migration from previous option is not handled because I could not manage to have it working (users will get prompted with the configuration file diff anyways). Add some tests checking various scenarios. Closes #15743487
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
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: 4491
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""automatic tests"""
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    19
from cubicweb.devtools.testlib import AutoPopulateTest, AutomaticWebTest
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 824
diff changeset
    20
from cubicweb.view import AnyRsetView
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
7007
3804794b1ccd [web test] ensure we're using proper instance home
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6639
diff changeset
    22
class AutomaticWebTest(AutomaticWebTest):
3804794b1ccd [web test] ensure we're using proper instance home
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6639
diff changeset
    23
    application_rql = [
3804794b1ccd [web test] ensure we're using proper instance home
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6639
diff changeset
    24
        'Any L,F WHERE E is CWUser, E login L, E firstname F',
3804794b1ccd [web test] ensure we're using proper instance home
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6639
diff changeset
    25
        'Any L,F,E WHERE E is CWUser, E login L, E firstname F',
3804794b1ccd [web test] ensure we're using proper instance home
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6639
diff changeset
    26
        'Any COUNT(X) WHERE X is CWUser',
3804794b1ccd [web test] ensure we're using proper instance home
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6639
diff changeset
    27
        ]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
8961
cc1a0aad580c [test/view] exclude some Blog entity from automatic view testing
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    29
    def to_test_etypes(self):
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    30
        # We do not really want to test cube views here. So we can drop testing
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    31
        # some EntityType. The two Blog types below require the sioc cube that
8961
cc1a0aad580c [test/view] exclude some Blog entity from automatic view testing
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    32
        # we do not want to add as a dependency.
cc1a0aad580c [test/view] exclude some Blog entity from automatic view testing
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    33
        etypes = super(AutomaticWebTest, self).to_test_etypes()
11681
b23d58050076 Merge cubicweb-pyramid cube
Yann Voté <yann.vote@logilab.fr>
parents: 11057
diff changeset
    34
        etypes -= set(('Blog', 'BlogEntry', 'CWSession'))
8961
cc1a0aad580c [test/view] exclude some Blog entity from automatic view testing
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    35
        return etypes
cc1a0aad580c [test/view] exclude some Blog entity from automatic view testing
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    36
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
class SomeView(AnyRsetView):
3408
c92170fca813 [api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2773
diff changeset
    39
    __regid__ = 'someview'
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    40
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    def call(self):
3474
020fc867f7b2 [api] use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3408
diff changeset
    42
        self._cw.add_js('spam.js')
020fc867f7b2 [api] use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3408
diff changeset
    43
        self._cw.add_js('spam.js')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
    46
class ManualCubicWebTCs(AutoPopulateTest):
6639
90ae7eb3460f [test] cleanup: don't need expensive auto_populate(), use a single test class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    47
90ae7eb3460f [test] cleanup: don't need expensive auto_populate(), use a single test class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    48
    def test_regr_copy_view(self):
90ae7eb3460f [test] cleanup: don't need expensive auto_populate(), use a single test class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    49
        """regression test: make sure we can ask a copy of a
90ae7eb3460f [test] cleanup: don't need expensive auto_populate(), use a single test class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    50
        composite entity
90ae7eb3460f [test] cleanup: don't need expensive auto_populate(), use a single test class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    51
        """
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    52
        with self.admin_access.web_request() as req:
10655
13081d3f7c99 [test] use unicode for rql queries (2/7)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10282
diff changeset
    53
            rset = req.execute(u'CWUser X WHERE X login "admin"')
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    54
            self.view('copy', rset, req=req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    def test_sortable_js_added(self):
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    57
        with self.admin_access.web_request() as req:
10282
f96093ad32e4 [web/test] fix test_views
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
    58
            # sortable.js should not be included by default
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    59
            rset = req.execute('CWUser X')
10726
bb91f581c389 [py3k] PageInfo.source contains bytes
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10655
diff changeset
    60
            self.assertNotIn(b'jquery.tablesorter.js', self.view('oneline', rset, req=req).source)
10282
f96093ad32e4 [web/test] fix test_views
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
    61
f96093ad32e4 [web/test] fix test_views
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
    62
        with self.admin_access.web_request() as req:
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    63
            # but should be included by the tableview
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    64
            rset = req.execute('Any P,F,S LIMIT 1 WHERE P is CWUser, P firstname F, P surname S')
10726
bb91f581c389 [py3k] PageInfo.source contains bytes
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10655
diff changeset
    65
            self.assertIn(b'jquery.tablesorter.js', self.view('table', rset, req=req).source)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    def test_js_added_only_once(self):
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    68
        with self.admin_access.web_request() as req:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    69
            self.vreg._loadedmods[__name__] = {}
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    70
            self.vreg.register(SomeView)
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    71
            rset = req.execute('CWUser X')
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    72
            source = self.view('someview', rset, req=req).source
10726
bb91f581c389 [py3k] PageInfo.source contains bytes
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10655
diff changeset
    73
            self.assertEqual(source.count(b'spam.js'), 1)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    def test_unrelateddivs(self):
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    76
        with self.admin_access.client_cnx() as cnx:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    77
            group = cnx.create_entity('CWGroup', name=u'R&D')
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    78
            cnx.commit()
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    79
        with self.admin_access.web_request(relation='in_group_subject') as req:
10655
13081d3f7c99 [test] use unicode for rql queries (2/7)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10282
diff changeset
    80
            rset = req.execute(u'Any X WHERE X is CWUser, X login "admin"')
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 8961
diff changeset
    81
            self.view('unrelateddivs', rset, req=req)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    82
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    from logilab.common.testlib import unittest_main
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    unittest_main()