Skip to content

Commit 65c052a

Browse files
committed
Callback API test code paths
- Legacy-Id: 9305
1 parent a1a85d4 commit 65c052a

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

ietf/api/tests.py

Lines changed: 33 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,52 @@ 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
50+
# To be able to eval JSON, we need to have three more symbols
51+
true = True
52+
false = False
53+
null = None
54+
r = cbclient.get(api_url + '?callback=identity')
55+
code = compile(r.content, '<string>', 'eval')
56+
# Make sure it is just a call with the identity function
57+
self.assertTrue(len(code.co_names) == 1, "The callback API returned "
58+
"code which uses more symbols than just the given \'identity\' "
59+
"callback function: %s" % ', '.join(code.co_names))
60+
self.assertTrue(code.co_names[0] == 'identity', "The callback API "
61+
"returned code with a different symbol than the given "
62+
"\'identity\' callback function: %s" % code.co_names[0])
63+
# After all these checks, I think calling eval is "safe"
64+
# Fingers crossed!
65+
callback_dict = eval(code)
66+
self.assertEqual(callback_dict, json_dict, "The callback API returned "
67+
"a different dictionary than the json API")
68+
4669
def test_all_model_resources_exist(self):
4770
client = Client(Accept='application/json')
4871
r = client.get("/api/v1")
4972
top = json.loads(r.content)
73+
self._assertCallbackReturnsSameJSON("/api/v1", top)
5074
for name in self.apps:
5175
app = self.apps[name]
5276
self.assertEqual("/api/v1/%s/"%name, top[name]["list_endpoint"])
5377
r = client.get(top[name]["list_endpoint"])
5478
self.assertValidJSONResponse(r)
5579
app_resources = json.loads(r.content)
80+
self._assertCallbackReturnsSameJSON("/api/v1/%s/"%name, app_resources)
5681
model_list = models.get_models(app.models)
5782
for model in model_list:
5883
if not model._meta.model_name in app_resources.keys():
5984
#print("There doesn't seem to be any resource for model %s.models.%s"%(app.__name__,model.__name__,))
6085
self.assertIn(model._meta.model_name, app_resources.keys(),
6186
"There doesn't seem to be any API resource for model %s.models.%s"%(app.__name__,model.__name__,))
6287

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

0 commit comments

Comments
 (0)