from datetime import timedelta from io import StringIO from django.core.management import call_command from django.test import TestCase from django.utils import timezone from .. import test from ...categories.models import Category from ...conf import settings from ..management.commands import clearattachments from ..models import Attachment, AttachmentType class ClearAttachmentsTests(TestCase): def test_no_attachments_sync(self): """command works when there are no attachments""" command = clearattachments.Command() out = StringIO() call_command(command, stdout=out) command_output = out.getvalue().strip() self.assertEqual(command_output, "No attachments were found") def test_attachments_sync(self): """command synchronizes attachments""" filetype = AttachmentType.objects.order_by("id").last() # create 5 expired orphaned attachments cutoff = timezone.now() - timedelta( minutes=settings.MISAGO_ATTACHMENT_ORPHANED_EXPIRE ) cutoff -= timedelta(minutes=5) for _ in range(5): Attachment.objects.create( secret=Attachment.generate_new_secret(), filetype=filetype, size=1000, uploaded_on=cutoff, uploader_name="User", uploader_slug="user", filename="testfile_%s.zip" % (Attachment.objects.count() + 1), ) # create 5 expired non-orphaned attachments category = Category.objects.get(slug="first-category") post = test.post_thread(category).first_post for _ in range(5): Attachment.objects.create( secret=Attachment.generate_new_secret(), filetype=filetype, size=1000, uploaded_on=cutoff, post=post, uploader_name="User", uploader_slug="user", filename="testfile_%s.zip" % (Attachment.objects.count() + 1), ) # create 5 fresh orphaned attachments for _ in range(5): Attachment.objects.create( secret=Attachment.generate_new_secret(), filetype=filetype, size=1000, uploader_name="User", uploader_slug="user", filename="testfile_%s.zip" % (Attachment.objects.count() + 1), ) command = clearattachments.Command() out = StringIO() call_command(command, stdout=out) command_output = out.getvalue().splitlines()[-1].strip() self.assertEqual(command_output, "Cleared 5 attachments") self.assertEqual(Attachment.objects.count(), 10)