test_clearattachments.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from datetime import timedelta
  2. from io import StringIO
  3. from django.core.management import call_command
  4. from django.test import TestCase
  5. from django.utils import timezone
  6. from misago.categories.models import Category
  7. from misago.conf import settings
  8. from misago.threads import testutils
  9. from misago.threads.management.commands import clearattachments
  10. from misago.threads.models import Attachment, AttachmentType
  11. class ClearAttachmentsTests(TestCase):
  12. def test_no_attachments_sync(self):
  13. """command works when there are no attachments"""
  14. command = clearattachments.Command()
  15. out = StringIO()
  16. call_command(command, stdout=out)
  17. command_output = out.getvalue().strip()
  18. self.assertEqual(command_output, "No attachments were found")
  19. def test_attachments_sync(self):
  20. """command synchronizes attachments"""
  21. filetype = AttachmentType.objects.order_by('id').last()
  22. # create 5 expired orphaned attachments
  23. cutoff = timezone.now() - timedelta(minutes=settings.MISAGO_ATTACHMENT_ORPHANED_EXPIRE)
  24. cutoff -= timedelta(minutes=5)
  25. for _ in range(5):
  26. Attachment.objects.create(
  27. secret=Attachment.generate_new_secret(),
  28. filetype=filetype,
  29. size=1000,
  30. uploaded_on=cutoff,
  31. uploader_name='bob',
  32. uploader_slug='bob',
  33. filename='testfile_%s.zip' % (Attachment.objects.count() + 1),
  34. )
  35. # create 5 expired non-orphaned attachments
  36. category = Category.objects.get(slug='first-category')
  37. post = testutils.post_thread(category).first_post
  38. for _ in range(5):
  39. Attachment.objects.create(
  40. secret=Attachment.generate_new_secret(),
  41. filetype=filetype,
  42. size=1000,
  43. uploaded_on=cutoff,
  44. post=post,
  45. uploader_name='bob',
  46. uploader_slug='bob',
  47. filename='testfile_%s.zip' % (Attachment.objects.count() + 1),
  48. )
  49. # create 5 fresh orphaned attachments
  50. for _ in range(5):
  51. Attachment.objects.create(
  52. secret=Attachment.generate_new_secret(),
  53. filetype=filetype,
  54. size=1000,
  55. uploader_name='bob',
  56. uploader_slug='bob',
  57. filename='testfile_%s.zip' % (Attachment.objects.count() + 1),
  58. )
  59. command = clearattachments.Command()
  60. out = StringIO()
  61. call_command(command, stdout=out)
  62. command_output = out.getvalue().splitlines()[-1].strip()
  63. self.assertEqual(command_output, "Cleared 5 attachments")
  64. self.assertEqual(Attachment.objects.count(), 10)