tests: Call client methods with explicit keyword arguments.

This is a prep commit for tightening the types for our wrapped test
client.

The callers of the test client methods are refactored to either call
them without unpacking at all or create a TypedDict for the keyword
arguments to be unpacked. This allows the type checker to know exactly what
keys are present and their corresponding type.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li
2022-06-14 16:34:47 -04:00
committed by Tim Abbott
parent a3a0545aac
commit 4e518a3852
5 changed files with 27 additions and 19 deletions

View File

@@ -5121,8 +5121,9 @@ class TestZulipRemoteUserBackend(DesktopFlowTestingLib, ZulipTestCase):
self.assertEqual(parsed_url.query, "param1=value1&params=value2")
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/", {}, HTTP_USER_AGENT="ZulipElectron/5.0.0"
)
self.verify_desktop_flow_app_page(result)
def test_login_success(self) -> None:

View File

@@ -1,6 +1,6 @@
import copy
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, Dict, Iterator, Mapping
from typing import TYPE_CHECKING, Any, Dict, Iterator, TypedDict
from unittest import mock
import orjson
@@ -14,6 +14,10 @@ if TYPE_CHECKING:
from django.test.client import _MonkeyPatchedWSGIResponse as TestHttpResponse
class SCIMHeadersDict(TypedDict):
HTTP_AUTHORIZATION: str
class SCIMTestCase(ZulipTestCase):
def setUp(self) -> None:
super().setUp()
@@ -22,7 +26,7 @@ class SCIMTestCase(ZulipTestCase):
realm=self.realm, name=settings.SCIM_CONFIG["zulip"]["scim_client_name"]
)
def scim_headers(self) -> Mapping[str, str]:
def scim_headers(self) -> SCIMHeadersDict:
return {"HTTP_AUTHORIZATION": f"Bearer {settings.SCIM_CONFIG['zulip']['bearer_token']}"}
def generate_user_schema(self, user_profile: UserProfile) -> Dict[str, Any]:

View File

@@ -69,15 +69,18 @@ def generate_all_emails(request: HttpRequest) -> HttpResponse:
realm = get_realm("zulip")
other_realm = Realm.objects.exclude(string_id="zulip").first()
user = get_user_by_delivery_email(registered_email, realm)
host_kwargs = {"HTTP_HOST": realm.host}
# Password reset emails
# active account in realm
result = client.post("/accounts/password/reset/", {"email": registered_email}, **host_kwargs)
result = client.post(
"/accounts/password/reset/", {"email": registered_email}, HTTP_HOST=realm.host
)
assert result.status_code == 302
# deactivated user
change_user_is_active(user, False)
result = client.post("/accounts/password/reset/", {"email": registered_email}, **host_kwargs)
result = client.post(
"/accounts/password/reset/", {"email": registered_email}, HTTP_HOST=realm.host
)
assert result.status_code == 302
change_user_is_active(user, True)
# account on different realm
@@ -88,16 +91,16 @@ def generate_all_emails(request: HttpRequest) -> HttpResponse:
assert result.status_code == 302
# no account anywhere
result = client.post(
"/accounts/password/reset/", {"email": unregistered_email_1}, **host_kwargs
"/accounts/password/reset/", {"email": unregistered_email_1}, HTTP_HOST=realm.host
)
assert result.status_code == 302
# Confirm account email
result = client.post("/accounts/home/", {"email": unregistered_email_1}, **host_kwargs)
result = client.post("/accounts/home/", {"email": unregistered_email_1}, HTTP_HOST=realm.host)
assert result.status_code == 302
# Find account email
result = client.post("/accounts/find/", {"emails": registered_email}, **host_kwargs)
result = client.post("/accounts/find/", {"emails": registered_email}, HTTP_HOST=realm.host)
assert result.status_code == 302
# New login email
@@ -113,7 +116,7 @@ def generate_all_emails(request: HttpRequest) -> HttpResponse:
"invite_expires_in_minutes": invite_expires_in_minutes,
"stream_ids": orjson.dumps([stream.id]).decode(),
},
**host_kwargs,
HTTP_HOST=realm.host,
)
assert result.status_code == 200
@@ -122,7 +125,7 @@ def generate_all_emails(request: HttpRequest) -> HttpResponse:
"/json/settings",
urllib.parse.urlencode({"email": "hamlets-new@zulip.com"}),
content_type="application/x-www-form-urlencoded",
**host_kwargs,
HTTP_HOST=realm.host,
)
assert result.status_code == 200

View File

@@ -80,11 +80,12 @@ Requester Bob <requester-bob@example.com> updated [ticket #11](http://test1234zz
"""
self.url = self.build_webhook_url()
payload = self.get_body("unknown_payload")
kwargs = {
"HTTP_AUTHORIZATION": self.encode_email(self.test_user.email),
"content_type": "application/x-www-form-urlencoded",
}
result = self.client_post(self.url, payload, **kwargs)
result = self.client_post(
self.url,
payload,
HTTP_AUTHORIZATION=self.encode_email(self.test_user.email),
content_type="application/x-www-form-urlencoded",
)
self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result)

View File

@@ -77,10 +77,9 @@ class PagerDutyHookTests(WebhookTestCase):
self.check_webhook("mp_fail", "Incident 48219", expected_message)
def test_unsupported_webhook_event(self) -> None:
post_params = dict(content_type="application/json")
for version in range(1, 4):
payload = self.get_body(f"unsupported_v{version}")
result = self.client_post(self.url, payload, **post_params)
result = self.client_post(self.url, payload, content_type="application/json")
self.assert_json_error(
result,
"The 'incident.unsupported' event isn't currently supported by the PagerDuty webhook",