web/test/unittest_views_json.py
author Rémi Cardona <remi.cardona@logilab.fr>
Thu, 10 Dec 2015 17:57:55 +0100
changeset 10966 8909593f46d4
parent 10700 a6d9d27f4253
child 10974 6557833657d6
permissions -rw-r--r--
[toolsutils] Small fixes to fill_templated_file() * Use context managers to close _both_ files * Use io.open to enforce text mode on both python 2 and 3
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/>.
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    19
from six import binary_type
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    20
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
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
    22
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
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
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
    25
    anonymize = True
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    26
    res_jsonp_data = b'[["guests", 1]]'
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    27
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    28
    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
    29
        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
    30
        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
    31
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    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
    33
        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
    34
            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
    35
            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
    36
            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
    37
            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
    38
8629
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    39
    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
    40
        with self.admin_access.web_request() as req:
10659
5b670cb3ae00 [test] use unicode for rql queries (6/7)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10397
diff changeset
    41
            rset = req.execute(u'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
    42
            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
    43
            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
    44
            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
    45
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    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
    47
        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
    48
            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
    49
                             '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
    50
                             '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
    51
            data = self.ctrl_publish(req, ctrl='jsonp')
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    52
            self.assertIsInstance(data, binary_type)
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    53
            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
    54
            # because jsonp anonymizes data, only 'guests' group should be found
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    55
            self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')')
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    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
    58
        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
    59
            req.form.update({'callback': 'foo',
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    60
                             '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
    61
                             '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
    62
                             '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
    63
            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
    64
            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
    65
            # result should be plain json, not the table view
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    66
            self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')')
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
    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
    69
        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
    70
            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
    71
            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
    72
            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
    73
            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
    74
            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
    75
10659
5b670cb3ae00 [test] use unicode for rql queries (6/7)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10397
diff changeset
    76
            rset = req.execute(u'Any G WHERE G is CWGroup, G name "foo"')
10013
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    77
            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
    78
            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
    79
            self.assertEqual(data, [])
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    80
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    82
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
    83
    anonymize = False
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    84
    res_jsonp_data = b'[["guests", 1], ["managers", 1]]'
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    85
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
if __name__ == '__main__':
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    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
    88
    unittest_main()