Skip to content

Commit 550f69c

Browse files
committed
Merged in [9305] from willem@nlnetlabs.nl:
Callback API test code paths - Legacy-Id: 9315 Note: SVN reference [9305] has been migrated to Git commit 65c052a
2 parents 0d4bceb + 2012364 commit 550f69c

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

ietf/api/tests.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.utils.importlib import import_module
88
from django.db import models
99

10+
from tastypie.exceptions import BadRequest
1011
from tastypie.test import ResourceTestCase
1112

1213
import debug # pyflakes:ignore
@@ -43,20 +44,53 @@ def test_api_top_level(self):
4344
self.assertIn(name, resource_list,
4445
"Expected a REST API resource for %s, but didn't find one" % name)
4546

47+
def _assertCallbackReturnsSameJSON(self, api_url, json_dict):
48+
cbclient = Client(Accept='text/javascript')
49+
identity = lambda x: x # pyflakes:ignore
50+
51+
# To be able to eval JSON, we need to have three more symbols
52+
# They are used indirectly
53+
true = True # pyflakes:ignore
54+
false = False # pyflakes:ignore
55+
null = None # pyflakes:ignore
56+
r = cbclient.get(api_url + '?callback=identity')
57+
code = compile(r.content, '<string>', 'eval')
58+
# Make sure it is just a call with the identity function
59+
self.assertTrue(len(code.co_names) == 1, "The callback API returned "
60+
"code which uses more symbols than just the given \'identity\' "
61+
"callback function: %s" % ', '.join(code.co_names))
62+
self.assertTrue(code.co_names[0] == 'identity', "The callback API "
63+
"returned code with a different symbol than the given "
64+
"\'identity\' callback function: %s" % code.co_names[0])
65+
# After all these checks, I think calling eval is "safe"
66+
# Fingers crossed!
67+
callback_dict = eval(code)
68+
self.assertEqual(callback_dict, json_dict, "The callback API returned "
69+
"a different dictionary than the json API")
70+
4671
def test_all_model_resources_exist(self):
4772
client = Client(Accept='application/json')
4873
r = client.get("/api/v1")
4974
top = json.loads(r.content)
75+
self._assertCallbackReturnsSameJSON("/api/v1", top)
5076
for name in self.apps:
5177
app = self.apps[name]
5278
self.assertEqual("/api/v1/%s/"%name, top[name]["list_endpoint"])
5379
r = client.get(top[name]["list_endpoint"])
5480
self.assertValidJSONResponse(r)
5581
app_resources = json.loads(r.content)
82+
self._assertCallbackReturnsSameJSON("/api/v1/%s/"%name, app_resources)
5683
model_list = models.get_models(app.models)
5784
for model in model_list:
5885
if not model._meta.model_name in app_resources.keys():
5986
#print("There doesn't seem to be any resource for model %s.models.%s"%(app.__name__,model.__name__,))
6087
self.assertIn(model._meta.model_name, app_resources.keys(),
6188
"There doesn't seem to be any API resource for model %s.models.%s"%(app.__name__,model.__name__,))
6289

90+
def test_invalid_jsonp_callback_value(self):
91+
try:
92+
Client(Accept='text/javascript').get("/api/v1?callback=$.23")
93+
except BadRequest:
94+
return
95+
self.assertTrue(False,
96+
"The callback API accepted an invalid JSONP callback name")

0 commit comments

Comments
 (0)