mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 13:33:24 +00:00
typing: Use assertions for responses when appropriate.
This is part of #18777.
This commit is contained in:
@@ -34,6 +34,7 @@ from django.db.migrations.executor import MigrationExecutor
|
||||
from django.db.migrations.state import StateApps
|
||||
from django.db.utils import IntegrityError
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.http.response import StreamingHttpResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart
|
||||
from 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)
|
||||
|
||||
|
||||
@@ -2,7 +2,18 @@ import cProfile
|
||||
import logging
|
||||
import time
|
||||
import 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 settings
|
||||
from 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_content
|
||||
content = None
|
||||
else:
|
||||
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 None
|
||||
response.setdefault("Content-Language", language)
|
||||
|
||||
# An additional responsibility of our override of this middleware is to save the user's language
|
||||
|
||||
@@ -12,6 +12,7 @@ from unittest.mock import patch
|
||||
import botocore.exceptions
|
||||
import orjson
|
||||
from django.conf import settings
|
||||
from django.http.response import StreamingHttpResponse
|
||||
from django.utils.timezone import now as timezone_now
|
||||
from django_sendfile.utils import _get_sendfile
|
||||
from 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
|
||||
|
||||
Reference in New Issue
Block a user