test_tasks.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 = baker.make("releases.Release", organization=self.organization)
  21. release_file = baker.make(
  22. "sourcecode.DebugSymbolBundle",
  23. file__blob=file_blob,
  24. organization=self.organization,
  25. release=release,
  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. "sourcecode.DebugSymbolBundle",
  33. file__blob=file_blob,
  34. organization=self.organization,
  35. release=release,
  36. )
  37. cleanup_old_files()
  38. self.assertEqual(FileBlob.objects.count(), 1)
  39. self.assertEqual(File.objects.count(), 2)
  40. release_file.file.delete()
  41. with freeze_time(now() + timedelta(days=settings.GLITCHTIP_MAX_FILE_LIFE_DAYS)):
  42. cleanup_old_files()
  43. self.assertEqual(FileBlob.objects.count(), 0)
  44. self.assertEqual(File.objects.count(), 0)