test_tasks.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.urls 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_api import generate_file
  11. class TasksTestCase(GlitchTipTestCase):
  12. def setUp(self):
  13. self.create_user_and_project()
  14. self.url = reverse("api:chunk_upload", args=[self.organization.slug])
  15. def test_cleanup_old_files(self):
  16. file = generate_file()
  17. data = {"file_gzip": file}
  18. self.client.post(self.url, data)
  19. file_blob = FileBlob.objects.first()
  20. release_file = baker.make(
  21. "releases.ReleaseFile",
  22. file__blob=file_blob,
  23. project__organization=self.organization,
  24. )
  25. cleanup_old_files()
  26. self.assertEqual(FileBlob.objects.count(), 1)
  27. self.assertEqual(File.objects.count(), 1)
  28. with freeze_time(now() + timedelta(days=settings.GLITCHTIP_MAX_FILE_LIFE_DAYS)):
  29. release_file = baker.make(
  30. "releases.ReleaseFile",
  31. file__blob=file_blob,
  32. project__organization=self.organization,
  33. )
  34. cleanup_old_files()
  35. self.assertEqual(FileBlob.objects.count(), 1)
  36. self.assertEqual(File.objects.count(), 2)
  37. release_file.file.delete()
  38. with freeze_time(now() + timedelta(days=settings.GLITCHTIP_MAX_FILE_LIFE_DAYS)):
  39. cleanup_old_files()
  40. self.assertEqual(FileBlob.objects.count(), 0)
  41. self.assertEqual(File.objects.count(), 0)