1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308"""Unit tests for S3CleanupService."""
from datetime import UTC
import pytest
from botocore.exceptions import ClientError
from src.config import settings
from src.services.s3_cleanup_service import S3CleanupService
@pytest.fixture
def cleanup_service(mock_s3_client):
"""Create cleanup service with mock client."""
return S3CleanupService(
s3_client=mock_s3_client,
temp_bucket=settings.s3_temp_bucket,
)
class TestDeleteTempFile:
"""Tests for delete_temp_file method."""
@pytest.mark.asyncio
async def test_delete_success(self, cleanup_service, mock_s3_client):
"""Test successful file deletion."""
mock_s3_client.delete_object.return_value = None
result = await cleanup_service.delete_temp_file("temp/job123/file.pdf")
assert result is True
mock_s3_client.delete_object.assert_called_once_with(
Bucket=settings.s3_temp_bucket,
Key="temp/job123/file.pdf"
)
@pytest.mark.asyncio
async def test_delete_failure(self, cleanup_service, mock_s3_client):
"""Test handling of deletion failure - best effort, no exception raised."""
mock_s3_client.delete_object.side_effect = Exception("S3 error")
result = await cleanup_service.delete_temp_file("temp/file.pdf")
assert result is False # Returns False instead of raising exception
class TestCleanupTempFilesForJob:
"""Tests for cleanup_temp_files_for_job method."""
@pytest.mark.asyncio
async def test_cleanup_single_file(self, cleanup_service, mock_s3_client, mocker):
"""Test cleanup of single PDF file for job."""
# Mock paginator
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [
{
'Contents': [
{'Key': 'temp/job123.pdf'}
]
}
]
# Mock batch delete
mock_s3_client.delete_objects.return_value = {
'Deleted': [{'Key': 'temp/job123.pdf'}]
}
# Execute
count = await cleanup_service.cleanup_temp_files_for_job("job123")
# Verify
assert count == 1
mock_s3_client.delete_objects.assert_called_once()
@pytest.mark.asyncio
async def test_cleanup_multiple_files(self, cleanup_service, mock_s3_client, mocker):
"""Test cleanup of multiple files for job."""
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [
{
'Contents': [
{'Key': 'temp/job456/file1.pdf'},
{'Key': 'temp/job456/file2.pdf'},
{'Key': 'temp/job456/metadata.json'}
]
}
]
mock_s3_client.delete_objects.return_value = {
'Deleted': [
{'Key': 'temp/job456/file1.pdf'},
{'Key': 'temp/job456/file2.pdf'},
{'Key': 'temp/job456/metadata.json'}
]
}
count = await cleanup_service.cleanup_temp_files_for_job("job456")
assert count == 3
@pytest.mark.asyncio
async def test_cleanup_no_files(self, cleanup_service, mock_s3_client, mocker):
"""Test cleanup when no files exist for job."""
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [{}] # No Contents key
count = await cleanup_service.cleanup_temp_files_for_job("job789")
assert count == 0
mock_s3_client.delete_objects.assert_not_called()
@pytest.mark.asyncio
async def test_cleanup_batch_delete_large_set(self, cleanup_service, mock_s3_client, mocker):
"""Test batch deletion with >1000 files (multiple batches)."""
# Create 1500 files to test batching
files = [{'Key': f'temp/job999/file{i}.pdf'} for i in range(1500)]
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [{'Contents': files}]
# Mock delete_objects to return deleted count
def delete_side_effect(**kwargs):
return {'Deleted': kwargs['Delete']['Objects']}
mock_s3_client.delete_objects.side_effect = delete_side_effect
count = await cleanup_service.cleanup_temp_files_for_job("job999")
assert count == 1500
# Should be called twice (1000 + 500)
assert mock_s3_client.delete_objects.call_count == 2
@pytest.mark.asyncio
async def test_cleanup_handles_errors(self, cleanup_service, mock_s3_client, mocker):
"""Test error handling during cleanup."""
from fastapi import HTTPException
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.side_effect = ClientError(
{"Error": {"Code": "AccessDenied"}},
"ListObjectsV2"
)
with pytest.raises(HTTPException) as exc:
await cleanup_service.cleanup_temp_files_for_job("job-error")
assert exc.value.status_code == 500
class TestListTempFiles:
"""Tests for list_temp_files method."""
@pytest.mark.asyncio
async def test_list_old_files(self, cleanup_service, mock_s3_client, mocker):
"""Test listing files older than threshold."""
from datetime import datetime, timedelta
now = datetime.now(UTC)
old_time = now - timedelta(hours=48) # 48 hours ago
recent_time = now - timedelta(hours=12) # 12 hours ago
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [
{
'Contents': [
{'Key': 'temp/old1.pdf', 'Size': 1024, 'LastModified': old_time},
{'Key': 'temp/old2.pdf', 'Size': 2048, 'LastModified': old_time},
{'Key': 'temp/recent.pdf', 'Size': 512, 'LastModified': recent_time}
]
}
]
# List files older than 24 hours
old_files = await cleanup_service.list_temp_files(older_than_hours=24)
assert len(old_files) == 2
assert old_files[0]['key'] == 'temp/old1.pdf'
assert old_files[1]['key'] == 'temp/old2.pdf'
assert all(f['age_hours'] >= 24 for f in old_files)
@pytest.mark.asyncio
async def test_list_no_old_files(self, cleanup_service, mock_s3_client, mocker):
"""Test when no files exceed age threshold."""
from datetime import datetime, timedelta
now = datetime.now(UTC)
recent_time = now - timedelta(hours=6)
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [
{
'Contents': [
{'Key': 'temp/recent.pdf', 'Size': 1024, 'LastModified': recent_time}
]
}
]
old_files = await cleanup_service.list_temp_files(older_than_hours=24)
assert len(old_files) == 0
@pytest.mark.asyncio
async def test_list_empty_bucket(self, cleanup_service, mock_s3_client, mocker):
"""Test listing when bucket is empty."""
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [{}]
old_files = await cleanup_service.list_temp_files()
assert len(old_files) == 0
@pytest.mark.asyncio
async def test_list_handles_timezone_naive(self, cleanup_service, mock_s3_client, mocker):
"""Test handling of timezone-naive datetimes."""
from datetime import UTC, datetime, timedelta
# Create timezone-naive datetime (simulates some S3 responses)
naive_time = datetime.now(UTC) - timedelta(hours=48)
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.return_value = [
{
'Contents': [
{'Key': 'temp/naive.pdf', 'Size': 1024, 'LastModified': naive_time}
]
}
]
# Should not raise exception
old_files = await cleanup_service.list_temp_files(older_than_hours=24)
assert len(old_files) == 1
@pytest.mark.asyncio
async def test_list_handles_errors(self, cleanup_service, mock_s3_client, mocker):
"""Test error handling during listing."""
from fastapi import HTTPException
mock_paginator = mocker.MagicMock()
mock_s3_client.get_paginator.return_value = mock_paginator
mock_paginator.paginate.side_effect = Exception("Network error")
with pytest.raises(HTTPException) as exc:
await cleanup_service.list_temp_files()
assert exc.value.status_code == 500
class TestDeleteFromS3:
"""Tests for delete_from_s3 method."""
@pytest.mark.asyncio
async def test_delete_success(self, cleanup_service, mock_s3_client):
"""Test successful deletion."""
mock_s3_client.delete_object.return_value = None
success = await cleanup_service.delete_from_s3("bucket", "key")
assert success is True
mock_s3_client.delete_object.assert_called_once_with(
Bucket="bucket",
Key="key"
)
@pytest.mark.asyncio
async def test_delete_idempotent(self, cleanup_service, mock_s3_client):
"""Test idempotent deletion (file already deleted)."""
error = ClientError(
{"Error": {"Code": "NoSuchKey"}},
"DeleteObject"
)
mock_s3_client.delete_object.side_effect = error
# Should still return True (idempotent)
success = await cleanup_service.delete_from_s3("bucket", "missing")
assert success is True
@pytest.mark.asyncio
async def test_delete_other_errors(self, cleanup_service, mock_s3_client):
"""Test handling of non-NoSuchKey errors."""
error = ClientError(
{"Error": {"Code": "AccessDenied"}},
"DeleteObject"
)
mock_s3_client.delete_object.side_effect = error
success = await cleanup_service.delete_from_s3("bucket", "key")
assert success is False
@pytest.mark.asyncio
async def test_delete_generic_exception(self, cleanup_service, mock_s3_client):
"""Test handling of generic exceptions."""
mock_s3_client.delete_object.side_effect = Exception("Network timeout")
success = await cleanup_service.delete_from_s3("bucket", "key")
assert success is False