1 # -*- coding: utf-8 -*- |
|
2 # copyright 2014 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/>. |
|
19 from six import binary_type |
|
20 |
|
21 from cubicweb.devtools.testlib import CubicWebTC |
|
22 |
|
23 |
|
24 class JsonViewsTC(CubicWebTC): |
|
25 anonymize = True |
|
26 res_jsonp_data = b'[["guests", 1]]' |
|
27 |
|
28 def setUp(self): |
|
29 super(JsonViewsTC, self).setUp() |
|
30 self.config.global_set_option('anonymize-jsonp-queries', self.anonymize) |
|
31 |
|
32 def test_json_rsetexport(self): |
|
33 with self.admin_access.web_request() as req: |
|
34 rset = req.execute( |
|
35 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN WHERE X in_group G, G name GN') |
|
36 data = self.view('jsonexport', rset, req=req) |
|
37 self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json']) |
|
38 self.assertListEqual(data, [["guests", 1], ["managers", 1]]) |
|
39 |
|
40 def test_json_rsetexport_empty_rset(self): |
|
41 with self.admin_access.web_request() as req: |
|
42 rset = req.execute(u'Any X WHERE X is CWUser, X login "foobarbaz"') |
|
43 data = self.view('jsonexport', rset, req=req) |
|
44 self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json']) |
|
45 self.assertListEqual(data, []) |
|
46 |
|
47 def test_json_rsetexport_with_jsonp(self): |
|
48 with self.admin_access.web_request() as req: |
|
49 req.form.update({'callback': u'foo', |
|
50 'rql': u'Any GN,COUNT(X) GROUPBY GN ORDERBY GN ' |
|
51 'WHERE X in_group G, G name GN'}) |
|
52 data = self.ctrl_publish(req, ctrl='jsonp') |
|
53 self.assertIsInstance(data, binary_type) |
|
54 self.assertEqual(req.headers_out.getRawHeaders('content-type'), |
|
55 ['application/javascript']) |
|
56 # because jsonp anonymizes data, only 'guests' group should be found |
|
57 self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')') |
|
58 |
|
59 def test_json_rsetexport_with_jsonp_and_bad_vid(self): |
|
60 with self.admin_access.web_request() as req: |
|
61 req.form.update({'callback': 'foo', |
|
62 # "vid" parameter should be ignored by jsonp controller |
|
63 'vid': 'table', |
|
64 'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN ' |
|
65 'WHERE X in_group G, G name GN'}) |
|
66 data = self.ctrl_publish(req, ctrl='jsonp') |
|
67 self.assertEqual(req.headers_out.getRawHeaders('content-type'), |
|
68 ['application/javascript']) |
|
69 # result should be plain json, not the table view |
|
70 self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')') |
|
71 |
|
72 def test_json_ersetexport(self): |
|
73 with self.admin_access.web_request() as req: |
|
74 rset = req.execute('Any G ORDERBY GN WHERE G is CWGroup, G name GN') |
|
75 data = self.view('ejsonexport', rset, req=req) |
|
76 self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json']) |
|
77 self.assertEqual(data[0]['name'], 'guests') |
|
78 self.assertEqual(data[1]['name'], 'managers') |
|
79 |
|
80 rset = req.execute(u'Any G WHERE G is CWGroup, G name "foo"') |
|
81 data = self.view('ejsonexport', rset, req=req) |
|
82 self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json']) |
|
83 self.assertEqual(data, []) |
|
84 |
|
85 |
|
86 class NotAnonymousJsonViewsTC(JsonViewsTC): |
|
87 anonymize = False |
|
88 res_jsonp_data = b'[["guests", 1], ["managers", 1]]' |
|
89 |
|
90 |
|
91 if __name__ == '__main__': |
|
92 from logilab.common.testlib import unittest_main |
|
93 unittest_main() |
|