Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
test_classes: Extract assert_streaming_content helper.
Anders Kaseorgba7ea7cc809a
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.pyindex 98976d21fdf..a4ff5485ad6 100644--- a/zerver/lib/test_classes.py+++ b/zerver/lib/test_classes.py@@ -924,8 +924,7 @@ Output:return [subscription.user_profile for subscription in subscriptions]- def assert_url_serves_contents_of_file(self, url: str, result: bytes) -> None:- response = self.client_get(url)+ def assert_streaming_content(self, response: HttpResponse, result: bytes) -> None:assert isinstance(response, StreamingHttpResponse)data = b"".join(response.streaming_content)self.assertEqual(result, data)diff --git a/zerver/tests/test_realm_export.py b/zerver/tests/test_realm_export.pyindex 29b33717cc7..4c26cfd8a75 100644--- a/zerver/tests/test_realm_export.py+++ b/zerver/tests/test_realm_export.py@@ -141,7 +141,7 @@ class RealmExportTest(ZulipTestCase):export_path = orjson.loads(extra_data).get("export_path")response = self.client_get(export_path)self.assertEqual(response.status_code, 200)- self.assert_url_serves_contents_of_file(export_path, b"zulip!")+ self.assert_streaming_content(response, b"zulip!")result = self.client_get("/json/export/realm")self.assert_json_success(result)diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex 4e00ca306f9..62ab5f2d51b 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -102,13 +102,11 @@ 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)+ self.assert_streaming_content(response, b"zulip!")# Files uploaded through the API should be accessible via the web clientself.login("hamlet")- self.assert_url_serves_contents_of_file(uri, b"zulip!")+ self.assert_streaming_content(self.client_get(uri), b"zulip!")def test_mobile_api_endpoint(self) -> None:"""@@ -136,9 +134,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)+ self.assert_streaming_content(response, b"zulip!")def test_upload_file_with_supplied_mimetype(self) -> None:"""@@ -212,7 +208,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):# In the future, local file requests will follow the same style as S3# requests; they will be first authenthicated and redirected- self.assert_url_serves_contents_of_file(uri, b"zulip!")+ self.assert_streaming_content(self.client_get(uri), b"zulip!")# check if DB has attachment marked as unclaimedentry = Attachment.objects.get(file_name="zulip.txt")@@ -235,7 +231,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):# The generated URL has a token authorizing the requestor to access the file# without being logged in.self.logout()- self.assert_url_serves_contents_of_file(url_only_url, b"zulip!")+ self.assert_streaming_content(self.client_get(url_only_url), b"zulip!")# The original uri shouldn't work when logged out:result = self.client_get(uri)self.assertEqual(result.status_code, 401)@@ -276,7 +272,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):url_only_url = data["url"]self.logout()- self.assert_url_serves_contents_of_file(url_only_url, b"zulip!")+ self.assert_streaming_content(self.client_get(url_only_url), b"zulip!")# After over 60 seconds, the token should become invalid:with mock.patch("django.core.signing.time.time", return_value=start_time + 61):@@ -601,9 +597,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.assert_streaming_content(response, b"zulip!")self.logout()# Confirm other cross-realm users can't read it.@@ -641,9 +635,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.assert_streaming_content(response, b"zulip!")self.logout()self.assert_length(queries, 5)@@ -652,9 +644,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.assert_streaming_content(response, b"zulip!")self.logout()self.assert_length(queries, 6)@@ -708,9 +698,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.assert_streaming_content(response, b"zulip!")self.logout()self.assert_length(queries, 5)@@ -719,9 +707,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.assert_streaming_content(response, b"zulip!")self.logout()self.assert_length(queries, 6)@@ -730,9 +716,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.assert_streaming_content(response, b"zulip!")self.logout()# It takes a few extra queries to verify access because of shared history.self.assert_length(queries, 9)@@ -794,9 +778,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.assert_streaming_content(response, b"zulip!")# If we were accidentally one query per message, this would be 20+self.assert_length(queries, 9)@@ -827,9 +809,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.assert_streaming_content(response, b"zulip!")self.logout()def test_serve_local(self) -> None: