web/test/unittest_views_json.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 10 Apr 2014 10:59:08 +0200
changeset 9677 f0130c270793
parent 8704 933f9693c53d
child 9810 51ebb1b925cb
permissions -rw-r--r--
[test] Fix regressions from 96549de9dd70 "[test] use assertIn where appropriate"
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 -*-
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
     2
# copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
7989
db76e8aaec29 [test] restore support for python < 2.6
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7876
diff changeset
    21
from cubicweb.utils import json
7876
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
from cubicweb.web.application import anonymized_request
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    24
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
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
    26
    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
    27
    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
    28
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    29
    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
    30
        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
    31
        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
    32
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
    def test_json_rsetexport(self):
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
        req = self.request()
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
        rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN')
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
        data = self.view('jsonexport', rset)
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
8704
933f9693c53d [testlib] make test_view load the json data
Julien Cristau <julien.cristau@logilab.fr>
parents: 8629
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):
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    41
        req = self.request()
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    42
        rset = req.execute('Any X WHERE X is CWUser, X login "foobarbaz"')
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    43
        data = self.view('jsonexport', rset)
3ae893f9ec84 [json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8601
diff changeset
    44
        self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
8704
933f9693c53d [testlib] make test_view load the json data
Julien Cristau <julien.cristau@logilab.fr>
parents: 8629
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):
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        req = self.request()
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        req.form.update({'callback': 'foo',
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
                         'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN',
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
                         })
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        data = self.ctrl_publish(req, ctrl='jsonp')
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        # because jsonp anonymizes data, only 'guests' group should be found
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    55
        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
    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):
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        req = self.request()
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        req.form.update({'callback': 'foo',
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
                         'vid': 'table', # <-- this parameter should be ignored by jsonp controller
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
                         'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN',
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
                         })
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        data = self.ctrl_publish(req, ctrl='jsonp')
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        # result should be plain json, not the table view
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    66
        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
    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):
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        req = self.request()
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        rset = req.execute('Any G ORDERBY GN WHERE G is CWGroup, G name GN')
8704
933f9693c53d [testlib] make test_view load the json data
Julien Cristau <julien.cristau@logilab.fr>
parents: 8629
diff changeset
    71
        data = self.view('ejsonexport', rset)
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        self.assertEqual(data[0]['name'], 'guests')
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        self.assertEqual(data[1]['name'], 'managers')
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
8601
1a6000ff2080 [web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 7989
diff changeset
    77
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
    78
    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
    79
    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
    80
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
if __name__ == '__main__':
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    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
    83
    unittest_main()