Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.addCleanup(setattr, settings, 'DEBUG', True)
resp = self.res.handle('list')
self.assertEqual(resp['Content-Type'], 'application/json')
self.assertEqual(resp.status_code, 401)
resp_json = json.loads(resp.content.decode('utf-8'))
self.assertEqual(resp_json, {
'error': 'Unauthorized.',
})
self.assertFalse('traceback' in resp_json)
# Last, with bubble_exceptions.
class Bubbly(DjTestResource):
def bubble_exceptions(self):
return True
with self.assertRaises(Unauthorized) as cm:
bubb = Bubbly()
bubb.request = FakeHttpRequest('DELETE')
bubb.handle('list')
almost identical to Resource.handle, except
the way we handle the return value of view_method.
"""
method = self.request_method()
try:
if not method in self.http_methods.get(endpoint, {}):
raise MethodNotImplemented(
"Unsupported method '{}' for {} endpoint.".format(
method,
endpoint
)
)
if not self.is_authenticated():
raise Unauthorized()
self.data = self.deserialize(method, endpoint, self.request_body())
view_method = getattr(self, self.http_methods[endpoint][method])
data = view_method(*args, **kwargs)
if is_future(data):
# need to check if the view_method is a generator or not
data = yield data
serialized = self.serialize(method, endpoint, data)
except Exception as err:
raise gen.Return(self.handle_error(err))
status = self.status_map.get(self.http_methods[endpoint][method], OK)
raise gen.Return(self.build_response(serialized, status=status))
self.endpoint = endpoint
method = self.request_method()
try:
# Use ``.get()`` so we can also dodge potentially incorrect
# ``endpoint`` errors as well.
if not method in self.http_methods.get(endpoint, {}):
raise MethodNotImplemented(
"Unsupported method '{}' for {} endpoint.".format(
method,
endpoint
)
)
if not self.is_authenticated():
raise Unauthorized()
self.data = self.deserialize(method, endpoint, self.request_body())
view_method = getattr(self, self.http_methods[endpoint][method])
data = view_method(*args, **kwargs)
serialized = self.serialize(method, endpoint, data)
except Exception as err:
return self.handle_error(err)
status = self.status_map.get(self.http_methods[endpoint][method], OK)
return self.build_response(serialized, status=status)
def get(self, survey_id):
"""GET the main survey view.
Render survey page for given survey id, embed JSON into to template so
browser can cache survey in HTML.
Raises tornado http error.
@survey_id: Requested survey id.
"""
try:
survey = get_survey_for_handler(self, survey_id)
except Unauthorized:
return auth_redirect(self)
except SurveyAccessForbidden:
raise tornado.web.HTTPError(403)
# pass in the revisit url
self.render(
'view_enumerate.html',
current_user_model=self.current_user_model,
survey=survey,
revisit_url=options.revisit_url
)