|
| 1 | +import os |
| 2 | +from datetime import timedelta |
| 3 | + |
| 4 | +from django.core.cache import cache |
| 5 | +from django.core.files.base import ContentFile |
| 6 | +from django.utils import timezone |
| 7 | +from django.test import TestCase |
| 8 | + |
| 9 | +from kpi.models.import_export_task import ( |
| 10 | + ImportExportStatusChoices, |
| 11 | + SubmissionExportTask |
| 12 | +) |
| 13 | +from kpi.tasks import cleanup_anonymous_exports |
| 14 | +from kpi.utils.object_permission import get_anonymous_user |
| 15 | + |
| 16 | + |
| 17 | +class AnonymousExportCleanupTestCase(TestCase): |
| 18 | + def _create_export_task( |
| 19 | + self, status=ImportExportStatusChoices.COMPLETE, minutes_old=60 |
| 20 | + ): |
| 21 | + export = SubmissionExportTask() |
| 22 | + export.user = get_anonymous_user() |
| 23 | + export.status = status |
| 24 | + export.data = {'type': 'xls', 'source': 'test'} |
| 25 | + export.save() |
| 26 | + |
| 27 | + if minutes_old > 0: |
| 28 | + past_time = timezone.now() - timedelta(minutes=minutes_old) |
| 29 | + SubmissionExportTask.objects.filter(uid=export.uid).update( |
| 30 | + date_created=past_time |
| 31 | + ) |
| 32 | + export.refresh_from_db() |
| 33 | + return export |
| 34 | + |
| 35 | + def test_exports_older_than_30_minutes_are_deleted(self): |
| 36 | + # Export older than 30 min - should be deleted |
| 37 | + old_export = self._create_export_task(minutes_old=31) |
| 38 | + |
| 39 | + # Export newer than 30 min - should be kept |
| 40 | + recent_export = self._create_export_task(minutes_old=29) |
| 41 | + |
| 42 | + cleanup_anonymous_exports() |
| 43 | + self.assertFalse( |
| 44 | + SubmissionExportTask.objects.filter(uid=old_export.uid).exists() |
| 45 | + ) |
| 46 | + self.assertTrue( |
| 47 | + SubmissionExportTask.objects.filter(uid=recent_export.uid).exists() |
| 48 | + ) |
| 49 | + |
| 50 | + def test_export_result_file_is_deleted_from_storage(self): |
| 51 | + """ |
| 52 | + Test that export files are deleted from storage |
| 53 | + """ |
| 54 | + export = self._create_export_task(minutes_old=60) |
| 55 | + |
| 56 | + # Create actual file in storage |
| 57 | + file_content = ContentFile( |
| 58 | + b'PK\x03\x04' + |
| 59 | + b'{"data": "export"}' * 100, |
| 60 | + name='test_export.xlsx' |
| 61 | + ) |
| 62 | + export.result.save(f'test_export_{export.uid}.xlsx', file_content, save=True) |
| 63 | + export.refresh_from_db() |
| 64 | + |
| 65 | + storage = export.result.storage |
| 66 | + file_path = storage.path(export.result.name) |
| 67 | + self.assertTrue(os.path.exists(file_path)) |
| 68 | + self.assertTrue(SubmissionExportTask.objects.filter(uid=export.uid).exists()) |
| 69 | + |
| 70 | + cleanup_anonymous_exports() |
| 71 | + |
| 72 | + self.assertFalse(os.path.exists(file_path)) |
| 73 | + self.assertFalse(SubmissionExportTask.objects.filter(uid=export.uid).exists()) |
| 74 | + |
| 75 | + def test_processing_exports_are_not_deleted(self): |
| 76 | + """ |
| 77 | + Test that exports with PROCESSING status are never deleted |
| 78 | + """ |
| 79 | + processing_export = self._create_export_task( |
| 80 | + status=ImportExportStatusChoices.PROCESSING, |
| 81 | + minutes_old=100 |
| 82 | + ) |
| 83 | + |
| 84 | + cleanup_anonymous_exports() |
| 85 | + self.assertTrue( |
| 86 | + SubmissionExportTask.objects.filter( |
| 87 | + uid=processing_export.uid |
| 88 | + ).exists() |
| 89 | + ) |
| 90 | + |
| 91 | + def test_cache_lock_prevents_concurrent_execution(self): |
| 92 | + """ |
| 93 | + Test that cache lock prevents concurrent task execution |
| 94 | + """ |
| 95 | + for i in range(5): |
| 96 | + self._create_export_task(minutes_old=60) |
| 97 | + |
| 98 | + cache_key = 'cleanup_anonymous_exports:lock' |
| 99 | + lock_timeout = 15 * 60 |
| 100 | + |
| 101 | + # Acquire lock manually (simulate first task running) |
| 102 | + lock = cache.lock(cache_key, timeout=lock_timeout + 60) |
| 103 | + lock.acquire(blocking=False) |
| 104 | + |
| 105 | + try: |
| 106 | + # Task should return early without deleting |
| 107 | + cleanup_anonymous_exports() |
| 108 | + |
| 109 | + # Verify no exports were deleted |
| 110 | + remaining = SubmissionExportTask.objects.filter( |
| 111 | + user__username='AnonymousUser' |
| 112 | + ).count() |
| 113 | + self.assertEqual(remaining, 5) |
| 114 | + |
| 115 | + finally: |
| 116 | + lock.release() |
0 commit comments