Reuse existing files as previews, icons or featured images #161

Merged
Anna Sirota merged 11 commits from files-allow-reuse-between-ext into main 2024-06-04 12:23:26 +02:00
Showing only changes of commit 347cc0c852 - Show all commits

View File

@ -479,6 +479,68 @@ class UpdateTest(CheckFilePropertiesMixin, TestCase):
size_bytes=30177,
)
def test_update_icon_existing_file_linked(self):
file = FileFactory(
type=File.TYPES.IMAGE,
original_hash='sha256:ee3a015c51e35a237755713ec578334efa9ed8870af65b708f591f9254ff4472',
hash='sha256:ee3a015c51e35a237755713ec578334efa9ed8870af65b708f591f9254ff4472',
source='test_icon_0001.png',
)
extension = create_approved_version().extension
images_count_before = File.objects.filter(type=File.TYPES.IMAGE).count()
self.assertIsNone(extension.icon)
self.assertEqual(file.icon_of.count(), 0)
old_user = file.user
url = extension.get_manage_url()
user = extension.authors.first()
self.client.force_login(user)
with open(TEST_FILES_DIR / 'test_icon_0001.png', 'rb') as fp:
files = {'icon-source': fp}
response = self.client.post(url, {**POST_DATA, **files})
self.assertEqual(response.status_code, 302)
# No new files were created: the existing one was linked to the extension instead
self.assertEqual(File.objects.filter(type=File.TYPES.IMAGE).count(), images_count_before)
extension.refresh_from_db()
self.assertEqual(extension.icon, file)
self.assertEqual(file.icon_of.count(), 1)
file.refresh_from_db()
self.assertEqual(file.user, old_user)
def test_update_icon_existing_file_linked_to_multiple_extensions(self):
file = FileFactory(
type=File.TYPES.IMAGE,
original_hash='sha256:ee3a015c51e35a237755713ec578334efa9ed8870af65b708f591f9254ff4472',
hash='sha256:ee3a015c51e35a237755713ec578334efa9ed8870af65b708f591f9254ff4472',
source='test_icon_0001.png',
)
extension = create_approved_version().extension
another_extension = create_approved_version(extension__icon=file).extension
images_count_before = File.objects.filter(type=File.TYPES.IMAGE).count()
self.assertIsNone(extension.icon)
self.assertEqual(another_extension.icon_id, file.pk)
old_user = file.user
url = extension.get_manage_url()
user = extension.authors.first()
self.client.force_login(user)
with open(TEST_FILES_DIR / 'test_icon_0001.png', 'rb') as fp:
files = {'icon-source': fp}
response = self.client.post(url, {**POST_DATA, **files})
self.assertEqual(response.status_code, 302)
# No new files were created: the existing one was linked to the extension instead
self.assertEqual(File.objects.filter(type=File.TYPES.IMAGE).count(), images_count_before)
extension.refresh_from_db()
another_extension.refresh_from_db()
self.assertEqual(extension.icon, file)
self.assertEqual(another_extension.icon, file)
# File is referenced as a preview by both extensions
self.assertEqual(file.icon_of.count(), 2)
file.refresh_from_db()
self.assertEqual(file.user, old_user)
def test_update_featured_image_changes_expected_file_fields(self):
extension = create_approved_version(
extension__featured_image=ImageFactory(