web/test/unittest_views_json.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Dec 2015 16:32:57 +0100
changeset 10974 6557833657d6
parent 10700 a6d9d27f4253
permissions -rw-r--r--
a bit of pep8
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:
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    34
            rset = req.execute(
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    35
                '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
    36
            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
    37
            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
    38
            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
    39
8629
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    40
    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
    41
        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
    42
            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
    43
            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
    44
            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
    45
            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
    46
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    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
    48
        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
    49
            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
    50
                             '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
    51
                             '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
    52
            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
    53
            self.assertIsInstance(data, binary_type)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    54
            self.assertEqual(req.headers_out.getRawHeaders('content-type'),
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    55
                             ['application/javascript'])
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    56
            # 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
    57
            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
    58
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
    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
    60
        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
    61
            req.form.update({'callback': 'foo',
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    62
                             # "vid" parameter should be ignored by jsonp controller
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    63
                             'vid': 'table',
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    64
                             '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
    65
                             '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
    66
            data = self.ctrl_publish(req, ctrl='jsonp')
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    67
            self.assertEqual(req.headers_out.getRawHeaders('content-type'),
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    68
                             ['application/javascript'])
9810
51ebb1b925cb [webtests/views_json] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8704
diff changeset
    69
            # 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
    70
            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
    71
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    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
    73
        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
    74
            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
    75
            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
    76
            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
    77
            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
    78
            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
    79
10659
5b670cb3ae00 [test] use unicode for rql queries (6/7)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10397
diff changeset
    80
            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
    81
            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
    82
            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
    83
            self.assertEqual(data, [])
b68088082280 [web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents: 9864
diff changeset
    84
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    86
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
    87
    anonymize = False
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10659
diff changeset
    88
    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
    89
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10700
diff changeset
    90
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
if __name__ == '__main__':
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    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
    93
    unittest_main()