request: Move tornado_handler to ZulipRequestNotes.

This commit is contained in:
PIG208
2021-07-09 21:30:06 +08:00
committed by Tim Abbott
parent 742c17399e
commit 5167a93229
5 changed files with 12 additions and 6 deletions

View File

@@ -809,14 +809,14 @@ def internal_notify_view(is_tornado_view: bool) -> Callable[[ViewFuncT], ViewFun
) -> HttpResponse:
if not authenticate_notify(request):
raise AccessDeniedError()
is_tornado_request = hasattr(request, "_tornado_handler")
request_notes = get_request_notes(request)
is_tornado_request = request_notes.tornado_handler is not None
# These next 2 are not security checks; they are internal
# assertions to help us find bugs.
if is_tornado_view and not is_tornado_request:
raise RuntimeError("Tornado notify view called with no Tornado handler")
if not is_tornado_view and is_tornado_request:
raise RuntimeError("Django notify view called with Tornado handler")
request_notes = get_request_notes(request)
request_notes.requestor_for_logs = "internal"
return view_func(request, *args, **kwargs)

View File

@@ -323,6 +323,7 @@ class HostRequestMock(HttpRequest):
request_notes_map[self] = ZulipRequestNotes(
log_data={},
tornado_handler=DummyHandler(),
)
@property

View File

@@ -54,7 +54,7 @@ from zerver.lib.request import (
)
from zerver.lib.response import json_response, json_success
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import HostRequestMock
from zerver.lib.test_helpers import DummyHandler, HostRequestMock
from zerver.lib.user_agent import parse_user_agent
from zerver.lib.users import get_api_key
from zerver.lib.utils import generate_api_key, has_api_key_format
@@ -1535,7 +1535,7 @@ class TestInternalNotifyView(ZulipTestCase):
with self.assertRaises(RuntimeError):
self.internal_notify(True, request)
request._tornado_handler = "set"
get_request_notes(request).tornado_handler = DummyHandler()
with self.settings(SHARED_SECRET=secret):
self.assertTrue(authenticate_notify(request))
self.assertEqual(self.internal_notify(True, request), self.BORING_RESULT)

View File

@@ -111,9 +111,12 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
signals.request_started.send(sender=self.__class__)
request = WSGIRequest(environ)
# We do the import during runtime to avoid cyclic dependency
from zerver.lib.request import get_request_notes
# Provide a way for application code to access this handler
# given the HttpRequest object.
request._tornado_handler = self
get_request_notes(request).tornado_handler = self
return request

View File

@@ -109,7 +109,9 @@ def get_events_backend(
raise JsonableError(_("User not authorized for this query"))
# Extract the Tornado handler from the request
handler: AsyncDjangoHandler = request._tornado_handler
tornado_handler = get_request_notes(request).tornado_handler
assert tornado_handler is not None
handler: AsyncDjangoHandler = tornado_handler
if user_client is None:
valid_user_client = request.client