test_tasks.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.shortcuts import reverse
  4. from django.utils.timezone import now
  5. from freezegun import freeze_time
  6. from model_bakery import baker
  7. from glitchtip.test_utils.test_case import GlitchTipTestCase
  8. from ..models import File, FileBlob
  9. from ..tasks import cleanup_old_files
  10. from .test_views import generate_file
  11. class TasksTestCase(GlitchTipTestCase):
  12. def setUp(self):
  13. self.create_user_and_project()
  14. self.url = reverse(
  15. "chunk-upload", kwargs={"organization_slug": self.organization}
  16. )
  17. def test_cleanup_old_files(self):
  18. file = generate_file()
  19. data = {"file_gzip": file}
  20. self.client.post(self.url, data)
  21. file_blob = FileBlob.objects.first()
  22. release_file = baker.make(
  23. "releases.ReleaseFile",
  24. file__blob=file_blob,
  25. project__organization=self.organization,
  26. )
  27. cleanup_old_files()
  28. self.assertEqual(FileBlob.objects.count(), 1)
  29. self.assertEqual(File.objects.count(), 1)
  30. with freeze_time(now() + timedelta(days=settings.GLITCHTIP_MAX_FILE_LIFE_DAYS)):
  31. release_file = baker.make(
  32. "releases.ReleaseFile",
  33. file__blob=file_blob,
  34. project__organization=self.organization,
  35. )
  36. cleanup_old_files()
  37. self.assertEqual(FileBlob.objects.count(), 1)
  38. self.assertEqual(File.objects.count(), 2)
  39. release_file.file.delete()
  40. with freeze_time(now() + timedelta(days=settings.GLITCHTIP_MAX_FILE_LIFE_DAYS)):
  41. cleanup_old_files()
  42. self.assertEqual(FileBlob.objects.count(), 0)
  43. self.assertEqual(File.objects.count(), 0)