tests: Migrate mocked Request to HostMockRequest.

This migrates some mocked Request class and mocked request achieved
with namedtuple in test_decorators and test_mirror_users to use the
refactored HostMockRequest.

Since weakref cannot be used with namedtuple, this old way of mocking a
request object should be migrated to using HostRequestMock. Only after
this change we can extract client from the request object and store it
via ZulipRequestNotes.
This commit is contained in:
PIG208
2021-07-09 23:40:21 +08:00
committed by Tim Abbott
parent 8eb2c3ffdb
commit 26875cdf0b
3 changed files with 83 additions and 140 deletions

View File

@@ -29,6 +29,7 @@ import ldap
import orjson
from boto3.resources.base import ServiceResource
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.db.migrations.state import StateApps
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.http.request import QueryDict
@@ -58,6 +59,7 @@ from zerver.models import (
)
from zerver.tornado.handlers import AsyncDjangoHandler, allocate_handler_id
from zerver.worker import queue_processors
from zilencer.models import RemoteZulipServer
from zproject.backends import ExternalAuthDataDict, ExternalAuthResult
if TYPE_CHECKING:
@@ -289,10 +291,11 @@ class HostRequestMock(HttpRequest):
def __init__(
self,
post_data: Dict[str, Any] = {},
user_profile: Optional[UserProfile] = None,
user_profile: Optional[Union[UserProfile, AnonymousUser, RemoteZulipServer]] = None,
host: str = settings.EXTERNAL_HOST,
client_name: Optional[str] = None,
meta_data: Optional[Dict[str, Any]] = None,
tornado_handler: Optional[AsyncDjangoHandler] = DummyHandler(),
) -> None:
self.host = host
self.GET = QueryDict(mutable=True)
@@ -323,7 +326,7 @@ class HostRequestMock(HttpRequest):
request_notes_map[self] = ZulipRequestNotes(
log_data={},
tornado_handler=DummyHandler(),
tornado_handler=tornado_handler,
)
@property