Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typing: Use assertions for responses when appropriate.
PIG208fa09404dd718
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.pyindex 79c40946367..eeda185b174 100644--- a/zerver/lib/test_classes.py+++ b/zerver/lib/test_classes.py@@ -34,6 +34,7 @@ from django.db.migrations.executor import MigrationExecutorfrom django.db.migrations.state import StateAppsfrom django.db.utils import IntegrityErrorfrom django.http import HttpRequest, HttpResponse+from django.http.response import StreamingHttpResponsefrom django.test import TestCasefrom django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipartfrom django.test.testcases import SerializeMixin@@ -852,6 +853,7 @@ Output:def assert_url_serves_contents_of_file(self, url: str, result: bytes) -> None:response = self.client_get(url)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(result, data)diff --git a/zerver/middleware.py b/zerver/middleware.pyindex e058e50a2c5..7c89c0c4c38 100644--- a/zerver/middleware.py+++ b/zerver/middleware.py@@ -2,7 +2,18 @@ import cProfileimport loggingimport timeimport traceback-from typing import Any, AnyStr, Callable, Dict, Iterable, List, MutableMapping, Optional, Tuple+from typing import (+ Any,+ AnyStr,+ Callable,+ Dict,+ Iterable,+ Iterator,+ List,+ MutableMapping,+ Optional,+ Tuple,+)from django.conf import settingsfrom django.conf.urls.i18n import is_language_prefix_patterns_used@@ -394,7 +405,8 @@ class LogRequests(MiddlewareMixin):requestor_for_logs = "unauth@{}".format(get_subdomain(request) or "root")if response.streaming:- content_iter = response.streaming_content+ assert isinstance(response, StreamingHttpResponse)+ content_iter: Optional[Iterator[bytes]] = response.streaming_contentcontent = Noneelse:content = response.content@@ -499,6 +511,7 @@ class LocaleMiddleware(DjangoLocaleMiddleware):i18n_patterns_used, _ = is_language_prefix_patterns_used(urlconf)if not (i18n_patterns_used and language_from_path):patch_vary_headers(response, ("Accept-Language",))+ assert language is not Noneresponse.setdefault("Content-Language", language)# An additional responsibility of our override of this middleware is to save the user's languagediff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex 6fb394fad06..2f5c6146153 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -12,6 +12,7 @@ from unittest.mock import patchimport botocore.exceptionsimport orjsonfrom django.conf import settings+from django.http.response import StreamingHttpResponsefrom django.utils.timezone import now as timezone_nowfrom django_sendfile.utils import _get_sendfilefrom PIL import Image@@ -100,6 +101,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):self.logout()response = self.api_get(self.example_user("hamlet"), uri)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)@@ -133,6 +135,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):response = self.client_get(uri, {"api_key": get_api_key(user_profile)})self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)@@ -596,6 +599,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):self.login_user(user_1)response = self.client_get(uri, subdomain=test_subdomain)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)self.logout()@@ -635,6 +639,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):with queries_captured() as queries:response = self.client_get(uri)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)self.logout()@@ -645,6 +650,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):with queries_captured() as queries:response = self.client_get(uri)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)self.logout()@@ -700,6 +706,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):with queries_captured() as queries:response = self.client_get(uri)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)self.logout()@@ -710,6 +717,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):with queries_captured() as queries:response = self.client_get(uri)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)self.logout()@@ -720,6 +728,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):with queries_captured() as queries:response = self.client_get(uri)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)self.logout()@@ -783,6 +792,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):with queries_captured() as queries:response = self.client_get(uri)self.assertEqual(response.status_code, 200)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)# If we were accidentally one query per message, this would be 20+@@ -815,6 +825,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):for user in subscribed_users + unsubscribed_users:self.login_user(user)response = self.client_get(uri)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(b"zulip!", data)self.logout()@@ -1124,6 +1135,7 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):if rfname is not None:response = self.client_get(url)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))@@ -1376,6 +1388,7 @@ class RealmIconTest(UploadSerializeMixin, ZulipTestCase):if rfname is not None:response = self.client_get(url)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))@@ -1547,6 +1560,7 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase):if rfname is not None:response = self.client_get(logo_url)+ assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)# size should be 100 x 100 because thumbnail keeps aspect ratio# while trying to fit in a 800 x 100 box without losing part of the image