web/test/unittest_views_json.py
author David Douard <david.douard@logilab.fr>
Tue, 19 Jul 2016 13:50:56 +0200
branch3.20
changeset 11399 5149a4d4cf54
parent 10397 1ce4594f9cf4
child 10659 5b670cb3ae00
permissions -rw-r--r--
[captcha] improve a bit the redability of generated captcha images (closes #13500378)
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:
10397
1ce4594f9cf4 [web/json] Controller.publish must return encoded data
Julien Cristau <julien.cristau@logilab.fr>
parents: 10013
diff changeset
    46
            req.form.update({'callback': u'foo',
1ce4594f9cf4 [web/json] Controller.publish must return encoded data
Julien Cristau <julien.cristau@logilab.fr>
parents: 10013
diff changeset
    47
                             'rql': u'Any GN,COUNT(X) GROUPBY GN ORDERBY GN '
9810
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')
10397
1ce4594f9cf4 [web/json] Controller.publish must return encoded data
Julien Cristau <julien.cristau@logilab.fr>
parents: 10013
diff changeset
    50
            self.assertIsInstance(data, str)
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    51
            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
    52
            # 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
    53
            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
    54
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
    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
    56
        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
    57
            req.form.update({'callback': 'foo',
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    58
                             '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
    59
                             '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
    60
                             '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
    61
            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
    62
            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
    63
            # 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
    64
            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
    65
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    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
    67
        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
    68
            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
    69
            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
    70
            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
    71
            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
    72
            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
    73
10013
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    74
            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
    75
            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
    76
            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
    77
            self.assertEqual(data, [])
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    78
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    80
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
    81
    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
    82
    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
    83
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
if __name__ == '__main__':
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    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
    86
    unittest_main()