web/test/unittest_views_json.py
branchstable
changeset 8601 1a6000ff2080
parent 7989 db76e8aaec29
child 8629 3ae893f9ec84
equal deleted inserted replaced
8600:d74addac92bb 8601:1a6000ff2080
       
     1 # -*- coding: utf-8 -*-
       
     2 # copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This file is part of CubicWeb.
       
     6 #
       
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     8 # terms of the GNU Lesser General Public License as published by the Free
       
     9 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    10 # any later version.
       
    11 #
       
    12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    15 # details.
       
    16 #
       
    17 # You should have received a copy of the GNU Lesser General Public License along
       
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
     1 from cubicweb.devtools.testlib import CubicWebTC
    19 from cubicweb.devtools.testlib import CubicWebTC
     2 
    20 
     3 from cubicweb.utils import json
    21 from cubicweb.utils import json
     4 
    22 
       
    23 from cubicweb.web.application import anonymized_request
       
    24 
     5 class JsonViewsTC(CubicWebTC):
    25 class JsonViewsTC(CubicWebTC):
       
    26     anonymize = True
       
    27     res_jsonp_data = '[["guests", 1]]'
       
    28 
       
    29     def setUp(self):
       
    30         super(JsonViewsTC, self).setUp()
       
    31         self.config.global_set_option('anonymize-jsonp-queries', self.anonymize)
     6 
    32 
     7     def test_json_rsetexport(self):
    33     def test_json_rsetexport(self):
     8         req = self.request()
    34         req = self.request()
     9         rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN')
    35         rset = req.execute('Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN')
    10         data = self.view('jsonexport', rset)
    36         data = self.view('jsonexport', rset)
    17                          'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN',
    43                          'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN',
    18                          })
    44                          })
    19         data = self.ctrl_publish(req, ctrl='jsonp')
    45         data = self.ctrl_publish(req, ctrl='jsonp')
    20         self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
    46         self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
    21         # because jsonp anonymizes data, only 'guests' group should be found
    47         # because jsonp anonymizes data, only 'guests' group should be found
    22         self.assertEqual(data, 'foo([["guests", 1]])')
    48         self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data)
    23 
    49 
    24     def test_json_rsetexport_with_jsonp_and_bad_vid(self):
    50     def test_json_rsetexport_with_jsonp_and_bad_vid(self):
    25         req = self.request()
    51         req = self.request()
    26         req.form.update({'callback': 'foo',
    52         req.form.update({'callback': 'foo',
    27                          'vid': 'table', # <-- this parameter should be ignored by jsonp controller
    53                          'vid': 'table', # <-- this parameter should be ignored by jsonp controller
    28                          'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN',
    54                          'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN',
    29                          })
    55                          })
    30         data = self.ctrl_publish(req, ctrl='jsonp')
    56         data = self.ctrl_publish(req, ctrl='jsonp')
    31         self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
    57         self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
    32         # result should be plain json, not the table view
    58         # result should be plain json, not the table view
    33         self.assertEqual(data, 'foo([["guests", 1]])')
    59         self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data)
    34 
    60 
    35     def test_json_ersetexport(self):
    61     def test_json_ersetexport(self):
    36         req = self.request()
    62         req = self.request()
    37         rset = req.execute('Any G ORDERBY GN WHERE G is CWGroup, G name GN')
    63         rset = req.execute('Any G ORDERBY GN WHERE G is CWGroup, G name GN')
    38         data = json.loads(self.view('ejsonexport', rset))
    64         data = json.loads(self.view('ejsonexport', rset))
    39         self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
    65         self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
    40         self.assertEqual(data[0]['name'], 'guests')
    66         self.assertEqual(data[0]['name'], 'guests')
    41         self.assertEqual(data[1]['name'], 'managers')
    67         self.assertEqual(data[1]['name'], 'managers')
    42 
    68 
    43 
    69 
       
    70 class NotAnonymousJsonViewsTC(JsonViewsTC):
       
    71     anonymize = False
       
    72     res_jsonp_data = '[["guests", 1], ["managers", 1]]'
       
    73 
    44 if __name__ == '__main__':
    74 if __name__ == '__main__':
    45     from logilab.common.testlib import unittest_main
    75     from logilab.common.testlib import unittest_main
    46     unittest_main()
    76     unittest_main()