tests: Update ZulipTestCase.tearDown to remove local uploads.

Previously, tests that exercised code paths that added local
uploads did not always clean up `settings.LOCAL_UPLOADS_DIR`
after the test was complete.

Updates the `ZulipTestCase` class to remove any local uploads
in the unique `settings.LOCAL_UPLOADS_DIR` in `tearDown` for
all tests.
This commit is contained in:
Lauryn Menard
2023-03-24 16:43:53 +01:00
committed by Tim Abbott
parent a6fd41e012
commit 7b225245c0
3 changed files with 7 additions and 39 deletions

View File

@@ -159,6 +159,11 @@ class ZulipTestCase(TestCase):
flush_per_request_caches()
translation.activate(settings.LANGUAGE_CODE)
# Clean up local uploads directory after tests:
assert settings.LOCAL_UPLOADS_DIR is not None
if os.path.exists(settings.LOCAL_UPLOADS_DIR):
shutil.rmtree(settings.LOCAL_UPLOADS_DIR)
# Clean up after using fakeldap in LDAP tests:
if hasattr(self, "mock_ldap") and hasattr(self, "mock_initialize"):
if self.mock_ldap is not None:

View File

@@ -2,7 +2,6 @@ import datetime
import io
import os
import re
import shutil
import time
import urllib
from io import StringIO
@@ -67,12 +66,6 @@ from zerver.models import (
)
def destroy_uploads() -> None:
assert settings.LOCAL_UPLOADS_DIR is not None
if os.path.exists(settings.LOCAL_UPLOADS_DIR):
shutil.rmtree(settings.LOCAL_UPLOADS_DIR)
class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
def test_rest_endpoint(self) -> None:
"""
@@ -347,7 +340,8 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
result = self.client_post("/json/user_uploads", {"file": fp})
response_dict = self.assert_json_success(result)
destroy_uploads()
assert settings.LOCAL_UPLOADS_DIR is not None
self.rm_tree(settings.LOCAL_UPLOADS_DIR)
response = self.client_get(response_dict["uri"])
self.assertEqual(response.status_code, 404)
@@ -960,10 +954,6 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
check_xsend_links("áéБД.pdf", "%C3%A1%C3%A9%D0%91%D0%94.pdf")
check_xsend_links("zulip", "zulip", 'filename="zulip"')
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()
class AvatarTest(UploadSerializeMixin, ZulipTestCase):
def test_get_avatar_field(self) -> None:
@@ -1406,10 +1396,6 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
result = self.client_post("/json/users/me/avatar", {"file": fp})
self.assert_json_error(result, "Uploaded file is larger than the allowed limit of 0 MiB")
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()
class EmojiTest(UploadSerializeMixin, ZulipTestCase):
# While testing GIF resizing, we can't test if the final GIF has the same
@@ -1475,10 +1461,6 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
self.assertFalse(is_animated)
assert no_still_data is None
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()
class RealmIconTest(UploadSerializeMixin, ZulipTestCase):
def test_multiple_upload_failure(self) -> None:
@@ -1617,10 +1599,6 @@ class RealmIconTest(UploadSerializeMixin, ZulipTestCase):
result = self.client_post("/json/realm/icon", {"file": fp})
self.assert_json_error(result, "Uploaded file is larger than the allowed limit of 0 MiB")
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()
class RealmLogoTest(UploadSerializeMixin, ZulipTestCase):
night = False
@@ -1820,10 +1798,6 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase):
)
self.assert_json_error(result, "Uploaded file is larger than the allowed limit of 0 MiB")
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()
class RealmNightLogoTest(RealmLogoTest):
# Run the same tests as for RealmLogoTest, just with dark theme enabled

View File

@@ -1,6 +1,5 @@
import os
import re
import shutil
import urllib
from io import StringIO
from urllib.parse import urlparse
@@ -39,12 +38,6 @@ from zerver.models import (
)
def destroy_uploads() -> None:
assert settings.LOCAL_UPLOADS_DIR is not None
if os.path.exists(settings.LOCAL_UPLOADS_DIR):
shutil.rmtree(settings.LOCAL_UPLOADS_DIR)
class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
def test_upload_message_attachment(self) -> None:
user_profile = self.example_user("hamlet")
@@ -269,7 +262,3 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
)
path_id = urllib.parse.urlparse(uri).path
self.assertEqual(delete_export_tarball(path_id), path_id)
def tearDown(self) -> None:
destroy_uploads()
super().tearDown()