web/test/unittest_views_json.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 20 Mar 2014 10:41:22 +0100
changeset 10075 136b5f995f8e
parent 10013 b68088082280
child 10397 1ce4594f9cf4
permissions -rw-r--r--
Provide sufficient context to check 'delete' permission in AjaxEditRelationCtxComponent Call rdef.check only when both fromeid and toeid are available. Though only call it once (for the first encountered related entity). Factorize a bit to keep handling of CSS/JS addition the same. Closes #3670209.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     1
# -*- coding: utf-8 -*-
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
     2
# copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     4
#
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     5
# This file is part of CubicWeb.
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     6
#
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    10
# any later version.
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    11
#
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    15
# details.
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    16
#
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
from cubicweb.devtools.testlib import CubicWebTC
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    21
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
class JsonViewsTC(CubicWebTC):
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    23
    anonymize = True
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    24
    res_jsonp_data = '[["guests", 1]]'
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    25
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    26
    def setUp(self):
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    27
        super(JsonViewsTC, self).setUp()
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    28
        self.config.global_set_option('anonymize-jsonp-queries', self.anonymize)
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    def test_json_rsetexport(self):
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    31
        with self.admin_access.web_request() as req:
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    32
            rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN')
9864
f60a80592224 [webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9810
diff changeset
    33
            data = self.view('jsonexport', rset, req=req)
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    34
            self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    35
            self.assertListEqual(data, [["guests", 1], ["managers", 1]])
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
8629
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    37
    def test_json_rsetexport_empty_rset(self):
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    38
        with self.admin_access.web_request() as req:
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    39
            rset = req.execute('Any X WHERE X is CWUser, X login "foobarbaz"')
9864
f60a80592224 [webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9810
diff changeset
    40
            data = self.view('jsonexport', rset, req=req)
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    41
            self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    42
            self.assertListEqual(data, [])
8629
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    43
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    def test_json_rsetexport_with_jsonp(self):
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    45
        with self.admin_access.web_request() as req:
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    46
            req.form.update({'callback': 'foo',
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    47
                             'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN '
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    48
                             'WHERE X in_group G, G name GN'})
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    49
            data = self.ctrl_publish(req, ctrl='jsonp')
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    50
            self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    51
            # because jsonp anonymizes data, only 'guests' group should be found
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    52
            self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data)
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    def test_json_rsetexport_with_jsonp_and_bad_vid(self):
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    55
        with self.admin_access.web_request() as req:
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    56
            req.form.update({'callback': 'foo',
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    57
                             'vid': 'table', # <-- this parameter should be ignored by jsonp controller
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    58
                             'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN '
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    59
                             'WHERE X in_group G, G name GN'})
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    60
            data = self.ctrl_publish(req, ctrl='jsonp')
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    61
            self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    62
            # result should be plain json, not the table view
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    63
            self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data)
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
    def test_json_ersetexport(self):
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    66
        with self.admin_access.web_request() as req:
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    67
            rset = req.execute('Any G ORDERBY GN WHERE G is CWGroup, G name GN')
9864
f60a80592224 [webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9810
diff changeset
    68
            data = self.view('ejsonexport', rset, req=req)
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    69
            self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    70
            self.assertEqual(data[0]['name'], 'guests')
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    71
            self.assertEqual(data[1]['name'], 'managers')
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
10013
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    73
            rset = req.execute('Any G WHERE G is CWGroup, G name "foo"')
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    74
            data = self.view('ejsonexport', rset, req=req)
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    75
            self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    76
            self.assertEqual(data, [])
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    77
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    79
class NotAnonymousJsonViewsTC(JsonViewsTC):
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    80
    anonymize = False
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    81
    res_jsonp_data = '[["guests", 1], ["managers", 1]]'
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    82
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
if __name__ == '__main__':
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    from logilab.common.testlib import unittest_main
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    unittest_main()