Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

test_helpers: Fix instrument_url decorator type.

Anders Kaseorga329b538d28f
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index 567d0f00d49..e1cf9e02feb 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -304,7 +304,7 @@ class StripeTestCase(ZulipTestCase):
host_args = {}
if realm is not None: # nocoverage: TODO
host_args['HTTP_HOST'] = realm.host
- response = self.client_get("/upgrade/", **host_args)
+ response = self.client_get("/upgrade/", {}, **host_args)
params: Dict[str, Any] = {
'schedule': 'annual',
'signed_seat_count': self.get_signed_seat_count_from_response(response),
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py
index 1ca9aed554a..4324842c8c3 100644
--- a/zerver/lib/test_classes.py
+++ b/zerver/lib/test_classes.py
@@ -164,8 +164,15 @@ class ZulipTestCase(TestCase):
url = url.replace("/json/", "/").replace("/api/v1/", "/")
return (url, data)
- def validate_api_response_openapi(self, url: str, method: str, result: HttpResponse, data: Dict[str, Any],
- http_headers: Dict[str, Any], intentionally_undocumented: Optional[bool] = False) -> None:
+ def validate_api_response_openapi(
+ self,
+ url: str,
+ method: str,
+ result: HttpResponse,
+ data: Union[str, bytes, Dict[str, Any]],
+ http_headers: Dict[str, Any],
+ intentionally_undocumented: bool = False,
+ ) -> None:
"""
Validates all API responses received by this test against Zulip's API documentation,
declared in zerver/openapi/zulip.yaml. This powerful test lets us use Zulip's
@@ -256,7 +263,13 @@ class ZulipTestCase(TestCase):
return django_client.head(url, encoded, **kwargs)
@instrument_url
- def client_post(self, url: str, info: Dict[str, Any]={}, intentionally_undocumented: bool=False, **kwargs: Any) -> HttpResponse:
+ def client_post(
+ self,
+ url: str,
+ info: Union[str, bytes, Dict[str, Any]] = {},
+ **kwargs: Any,
+ ) -> HttpResponse:
+ intentionally_undocumented: bool = kwargs.pop("intentionally_undocumented", False)
django_client = self.client # see WRAPPER_COMMENT
self.set_http_headers(kwargs)
result = django_client.post(url, info, **kwargs)
@@ -278,7 +291,8 @@ class ZulipTestCase(TestCase):
return match.func(req)
@instrument_url
- def client_get(self, url: str, info: Dict[str, Any]={}, intentionally_undocumented: bool=False, **kwargs: Any) -> HttpResponse:
+ def client_get(self, url: str, info: Dict[str, Any] = {}, **kwargs: Any) -> HttpResponse:
+ intentionally_undocumented: bool = kwargs.pop("intentionally_undocumented", False)
django_client = self.client # see WRAPPER_COMMENT
self.set_http_headers(kwargs)
result = django_client.get(url, info, **kwargs)
@@ -832,7 +846,7 @@ class ZulipTestCase(TestCase):
self.assertEqual(x["name"], y.name)
def send_json_payload(self, user_profile: UserProfile, url: str,
- payload: Union[str, Dict[str, Any]],
+ payload: Union[bytes, str, Dict[str, Any]],
stream_name: Optional[str]=None, **post_params: Any) -> Message:
if stream_name is not None:
self.subscribe(user_profile, stream_name)
diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py
index 04727accbbb..5e0e5192022 100644
--- a/zerver/lib/test_helpers.py
+++ b/zerver/lib/test_helpers.py
@@ -19,6 +19,7 @@ from typing import (
Tuple,
TypeVar,
Union,
+ cast,
)
from unittest import mock
@@ -315,7 +316,7 @@ class MockPythonResponse:
INSTRUMENTING = os.environ.get('TEST_INSTRUMENT_URL_COVERAGE', '') == 'TRUE'
INSTRUMENTED_CALLS: List[Dict[str, Any]] = []
-UrlFuncT = Callable[..., HttpResponse] # TODO: make more specific
+UrlFuncT = TypeVar("UrlFuncT", bound=Callable[..., HttpResponse]) # TODO: make more specific
def append_instrumentation_data(data: Dict[str, Any]) -> None:
INSTRUMENTED_CALLS.append(data)
@@ -324,7 +325,7 @@ def instrument_url(f: UrlFuncT) -> UrlFuncT:
if not INSTRUMENTING: # nocoverage -- option is always enabled; should we remove?
return f
else:
- def wrapper(self: 'ZulipTestCase', url: str, info: Dict[str, Any]={},
+ def wrapper(self: 'ZulipTestCase', url: str, info: object = {},
**kwargs: Any) -> HttpResponse:
start = time.time()
result = f(self, url, info, **kwargs)
@@ -345,7 +346,7 @@ def instrument_url(f: UrlFuncT) -> UrlFuncT:
test_name=test_name,
kwargs=kwargs))
return result
- return wrapper
+ return cast(UrlFuncT, wrapper) # https://github.com/python/mypy/issues/1927
def write_instrumentation_reports(full_suite: bool, include_webhooks: bool) -> None:
if INSTRUMENTING:
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 6041b0b9a68..d94dfbee56b 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -3640,7 +3640,7 @@ class TestZulipRemoteUserBackend(DesktopFlowTestingLib, ZulipTestCase):
def test_start_remote_user_sso_with_desktop_app(self) -> None:
headers = dict(HTTP_USER_AGENT="ZulipElectron/5.0.0")
- result = self.client_get('/accounts/login/start/sso/', **headers)
+ result = self.client_get('/accounts/login/start/sso/', {}, **headers)
self.verify_desktop_flow_app_page(result)
def test_login_success(self) -> None:
@@ -4451,7 +4451,9 @@ class TestLDAP(ZulipLDAPTestCase):
# Verify avatar gets created
self.assertEqual(user_profile.avatar_source, UserProfile.AVATAR_FROM_USER)
- result = self.client_get(avatar_url(user_profile))
+ url = avatar_url(user_profile)
+ assert url is not None
+ result = self.client_get(url)
self.assertEqual(result.status_code, 200)
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
diff --git a/zerver/tests/test_tornado.py b/zerver/tests/test_tornado.py
index 38c724422ac..d5849f2a35b 100644
--- a/zerver/tests/test_tornado.py
+++ b/zerver/tests/test_tornado.py
@@ -31,7 +31,7 @@ class TornadoWebTestCase(AsyncHTTPTestCase, ZulipTestCase):
def get_app(self) -> Application:
return create_tornado_application(9993)
- def client_get(self, path: str, **kwargs: Any) -> HTTPResponse:
+ def tornado_client_get(self, path: str, **kwargs: Any) -> HTTPResponse:
self.add_session_cookie(kwargs)
kwargs['skip_user_agent'] = True
self.set_http_headers(kwargs)
@@ -77,8 +77,11 @@ class TornadoWebTestCase(AsyncHTTPTestCase, ZulipTestCase):
kwargs['headers'] = headers
def create_queue(self, **kwargs: Any) -> str:
- response = self.client_get('/json/events?dont_block=true', subdomain="zulip",
- skip_user_agent=True)
+ response = self.tornado_client_get(
+ '/json/events?dont_block=true',
+ subdomain="zulip",
+ skip_user_agent=True,
+ )
self.assertEqual(response.code, 200)
body = ujson.loads(response.body)
self.assertEqual(body['events'], [])