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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504"""Tests for approval service."""
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock, MagicMock
import pytest
from src.services.approval_service import ApprovalService
from src.services.job_service import JobService
from src.services.queue_service import QueueService
from src.shared.constants.queues import APPROVAL_TIMEOUT_KEY
from src.shared.constants.statuses import (
STATUS_DENIED,
STATUS_PROCESSING,
STATUS_PROCESSING_QUEUED,
)
@pytest.fixture
def mock_redis_client():
"""Mock Redis client.
Uses MagicMock as container with AsyncMock for async methods.
This prevents unawaited coroutine warnings when tests set return_value.
"""
mock = MagicMock()
mock.set = AsyncMock(return_value=True)
mock.zrem = AsyncMock(return_value=1)
mock.delete = AsyncMock(return_value=1)
return mock
@pytest.fixture
def mock_s3_client():
"""Mock S3 client."""
client = AsyncMock()
client.exceptions = MagicMock()
client.exceptions.NoSuchKey = type("NoSuchKey", (Exception,), {})
return client
@pytest.fixture
def mock_job_service():
"""Mock JobService."""
return AsyncMock(spec=JobService)
@pytest.fixture
def mock_queue_service():
"""Mock QueueService."""
return AsyncMock(spec=QueueService)
@pytest.fixture
def mock_storage_service():
"""Mock StorageService."""
return AsyncMock()
@pytest.fixture
def mock_s3_url_service():
"""Mock S3URLService."""
return MagicMock()
@pytest.fixture
def approval_service(
mock_redis_client, mock_s3_client, mock_job_service, mock_queue_service, mock_storage_service, mock_s3_url_service
):
"""ApprovalService instance with mocked dependencies."""
return ApprovalService(
redis_client=mock_redis_client,
s3_client=mock_s3_client,
job_service=mock_job_service,
queue_service=mock_queue_service,
storage_service=mock_storage_service,
s3_url_service=mock_s3_url_service,
)
# Token Validation Tests
@pytest.mark.asyncio
async def test_validate_approval_token_valid(approval_service, mock_redis_client, mock_job_service):
"""Test successful token validation."""
# Arrange
token = "valid-token-abc123"
job_id = "550e8400-e29b-41d4-a716-446655440010"
expires_at = (datetime.now(UTC) + timedelta(hours=2)).isoformat()
# Mock O(1) token lookup
mock_job_service.get_job_by_approval_token.return_value = {
"job_id": job_id,
"approval_token": token,
"approval_expires_at": expires_at,
"status": "awaiting_approval",
}
# Act
result = await approval_service.validate_approval_token(token)
# Assert
assert result is not None
assert result["job_id"] == job_id
mock_job_service.get_job_by_approval_token.assert_called_once_with(token)
@pytest.mark.asyncio
async def test_validate_approval_token_expired(approval_service, mock_redis_client, mock_job_service):
"""Test expired token validation."""
# Arrange
token = "expired-token-xyz"
expires_at = (datetime.now(UTC) - timedelta(hours=1)).isoformat() # Past
# Mock O(1) token lookup
mock_job_service.get_job_by_approval_token.return_value = {
"job_id": "550e8400-e29b-41d4-a716-446655440011",
"approval_token": token,
"approval_expires_at": expires_at,
}
# Act
result = await approval_service.validate_approval_token(token)
# Assert
assert result is None
@pytest.mark.asyncio
async def test_validate_approval_token_not_found(approval_service, mock_redis_client, mock_job_service):
"""Test token validation when no matching job found."""
# Arrange
token = "nonexistent-token"
# Mock O(1) token lookup returning None (not found)
mock_job_service.get_job_by_approval_token.return_value = None
# Act
result = await approval_service.validate_approval_token(token)
# Assert
assert result is None
@pytest.mark.asyncio
async def test_validate_approval_token_missing_expires_at(approval_service, mock_redis_client, mock_job_service):
"""Test token validation when job missing expires_at field."""
# Arrange
token = "token-missing-expiry"
# Mock O(1) token lookup
mock_job_service.get_job_by_approval_token.return_value = {
"job_id": "550e8400-e29b-41d4-a716-446655440012",
"approval_token": token,
# Missing approval_expires_at
}
# Act
result = await approval_service.validate_approval_token(token)
# Assert
assert result is None
@pytest.mark.asyncio
async def test_validate_approval_token_handles_string_keys(approval_service, mock_redis_client, mock_job_service):
"""Test token validation uses O(1) lookup (no longer needs to handle key formats)."""
# Arrange
token = "valid-token"
expires_at = (datetime.now(UTC) + timedelta(hours=2)).isoformat()
# Mock O(1) token lookup
mock_job_service.get_job_by_approval_token.return_value = {
"job_id": "550e8400-e29b-41d4-a716-446655440013",
"approval_token": token,
"approval_expires_at": expires_at,
}
# Act
result = await approval_service.validate_approval_token(token)
# Assert
assert result is not None
assert result["job_id"] == "550e8400-e29b-41d4-a716-446655440013"
# Approval Decision Tests
@pytest.mark.asyncio
async def test_process_approval_decision_approved(
approval_service, mock_redis_client, mock_job_service, mock_queue_service
):
"""Test processing approved decision triggers inline processing."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440001"
s3_key = "temp/approved-doc.pdf"
mock_job_service.get_job.return_value = {"job_id": job_id, "s3_key": s3_key, "status": "awaiting_approval"}
# Act
await approval_service.process_approval_decision(
job_id=job_id,
decision="approved",
justification="Instructor contact info is acceptable",
reviewed_by="faculty@uic.edu",
)
# Assert
mock_redis_client.zrem.assert_called_once_with(APPROVAL_TIMEOUT_KEY, job_id)
# Processing is now triggered inline via DocumentProcessingService, not enqueued
# So we verify the status update instead of queue enqueue
mock_job_service.update_job_status.assert_called()
status_call = mock_job_service.update_job_status.call_args
assert status_call[0][1] == STATUS_PROCESSING
@pytest.mark.asyncio
async def test_process_approval_decision_denied(approval_service, mock_redis_client, mock_job_service, mock_s3_client):
"""Test processing denied decision."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440014"
s3_key = "temp/denied-doc.pdf"
mock_job_service.get_job.return_value = {"job_id": job_id, "s3_key": s3_key, "status": "awaiting_approval"}
mock_s3_client.delete_object.return_value = {}
# Act
await approval_service.process_approval_decision(
job_id=job_id,
decision="denied",
justification="Contains student PII - cannot process",
reviewed_by="admin@uic.edu",
)
# Assert
mock_redis_client.zrem.assert_called_once_with(APPROVAL_TIMEOUT_KEY, job_id)
mock_s3_client.delete_object.assert_called_once()
mock_job_service.update_job_status.assert_called_once()
status_call = mock_job_service.update_job_status.call_args
assert status_call[0][1] == STATUS_DENIED
@pytest.mark.asyncio
async def test_process_approval_decision_job_not_found(approval_service, mock_job_service):
"""Test processing decision when job doesn't exist."""
# Arrange
mock_job_service.get_job.return_value = None
# Act & Assert
with pytest.raises(ValueError, match="Job .* not found"):
await approval_service.process_approval_decision(
job_id="550e8400-e29b-41d4-a716-446655440015",
decision="approved",
justification="Test",
reviewed_by="test@test.com",
)
@pytest.mark.asyncio
async def test_process_approval_decision_missing_s3_key(approval_service, mock_job_service):
"""Test processing decision when job missing s3_key."""
# Arrange
mock_job_service.get_job.return_value = {
"job_id": "550e8400-e29b-41d4-a716-446655440016"
# Missing s3_key
}
# Act & Assert
with pytest.raises(ValueError, match="missing s3_key"):
await approval_service.process_approval_decision(
job_id="550e8400-e29b-41d4-a716-446655440016",
decision="approved",
justification="Test",
reviewed_by="test@test.com",
)
@pytest.mark.asyncio
async def test_process_approval_stores_decision_metadata(
approval_service, mock_redis_client, mock_job_service, mock_queue_service
):
"""Test that approval decision stores metadata correctly."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440003"
justification = "Test justification for approval"
reviewed_by = "reviewer@uic.edu"
mock_job_service.get_job.return_value = {"job_id": job_id, "s3_key": "temp/test.pdf", "status": "awaiting_approval"}
# Act
await approval_service.process_approval_decision(
job_id=job_id, decision="approved", justification=justification, reviewed_by=reviewed_by
)
# Assert - check that update_job_status was called with approval_decision metadata
mock_job_service.update_job_status.assert_called()
# Find the call that includes approval_decision (may be called multiple times)
approval_call = None
for call in mock_job_service.update_job_status.call_args_list:
if "approval_decision" in call[1]:
approval_call = call
break
assert approval_call is not None, "update_job_status should be called with approval_decision"
approval_metadata = approval_call[1]["approval_decision"]
assert approval_metadata["decision"] == "approved"
assert approval_metadata["justification"] == justification
assert approval_metadata["reviewed_by"] == reviewed_by
assert "reviewed_at" in approval_metadata
@pytest.mark.asyncio
async def test_process_denial_continues_on_cleanup_failure(
approval_service, mock_redis_client, mock_job_service, mock_s3_client
):
"""Test that denial continues even if S3 cleanup fails."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440017"
mock_job_service.get_job.return_value = {"job_id": job_id, "s3_key": "temp/fail.pdf", "status": "awaiting_approval"}
# Simulate S3 cleanup failure
mock_s3_client.delete_object.side_effect = Exception("S3 error")
# Act - Should not raise exception
await approval_service.process_approval_decision(
job_id=job_id, decision="denied", justification="Test denial", reviewed_by="test@test.com"
)
# Assert - Status still updated despite cleanup failure
mock_job_service.update_job_status.assert_called_once()
status_call = mock_job_service.update_job_status.call_args
assert status_call[0][1] == STATUS_DENIED
# Instant Response Methods Tests (quick_approve, quick_deny)
@pytest.mark.asyncio
async def test_quick_approve_sets_processing_queued_status(approval_service, mock_job_service):
"""Test quick_approve sets status to processing_queued immediately."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440018"
# Act
await approval_service.quick_approve(job_id)
# Assert
mock_job_service.update_job_status.assert_called_once()
call_args = mock_job_service.update_job_status.call_args
assert call_args[0][0] == job_id
assert call_args[0][1] == STATUS_PROCESSING_QUEUED
assert "approved_at" in call_args[1]
@pytest.mark.asyncio
async def test_quick_deny_sets_denied_status(approval_service, mock_job_service):
"""Test quick_deny sets status to denied immediately."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440019"
# Act
await approval_service.quick_deny(job_id)
# Assert
mock_job_service.update_job_status.assert_called_once()
call_args = mock_job_service.update_job_status.call_args
assert call_args[0][0] == job_id
assert call_args[0][1] == STATUS_DENIED
assert "denied_at" in call_args[1]
# Background Processing Methods Tests
@pytest.mark.asyncio
async def test_process_approval_background_triggers_processing(
approval_service, mock_redis_client, mock_job_service, mock_queue_service
):
"""Test background approval triggers inline processing."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440020"
s3_key = "temp/bg-test.pdf"
mock_redis_client.set.return_value = True # Lock acquired
mock_job_service.get_job.return_value = {
"job_id": job_id,
"s3_key": s3_key,
"status": STATUS_PROCESSING_QUEUED,
}
# Act
await approval_service.process_approval_background(
job_id=job_id,
s3_key=s3_key,
justification="Background test approval",
reviewed_by="bg-test@uic.edu",
)
# Assert
mock_redis_client.zrem.assert_called_once_with(APPROVAL_TIMEOUT_KEY, job_id)
# Processing is now triggered inline via DocumentProcessingService, not enqueued
# Verify final status update to STATUS_PROCESSING
mock_job_service.update_job_status.assert_called()
status_call = mock_job_service.update_job_status.call_args
assert status_call[0][1] == STATUS_PROCESSING
@pytest.mark.asyncio
async def test_process_approval_background_skips_if_lock_not_acquired(
approval_service, mock_redis_client, mock_job_service, mock_queue_service
):
"""Test background approval skips processing if lock already held."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440021"
mock_redis_client.set.return_value = False # Lock NOT acquired
# Act
await approval_service.process_approval_background(
job_id=job_id,
s3_key="temp/test.pdf",
justification="Test",
reviewed_by="test@test.com",
)
# Assert - Should not enqueue or update status
mock_queue_service.enqueue.assert_not_called()
mock_job_service.update_job_status.assert_not_called()
@pytest.mark.asyncio
async def test_process_approval_background_skips_if_wrong_status(
approval_service, mock_redis_client, mock_job_service, mock_queue_service
):
"""Test background approval skips if job status changed."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440022"
mock_redis_client.set.return_value = True # Lock acquired
mock_job_service.get_job.return_value = {
"job_id": job_id,
"s3_key": "temp/test.pdf",
"status": STATUS_PROCESSING, # Already processing (not processing_queued)
}
# Act
await approval_service.process_approval_background(
job_id=job_id,
s3_key="temp/test.pdf",
justification="Test",
reviewed_by="test@test.com",
)
# Assert - Should not enqueue or update status
mock_queue_service.enqueue.assert_not_called()
mock_job_service.update_job_status.assert_not_called()
@pytest.mark.asyncio
async def test_process_denial_background_cleans_up_s3(
approval_service, mock_redis_client, mock_job_service, mock_s3_client
):
"""Test background denial cleans up S3 files."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440023"
s3_key = "temp/bg-deny-test.pdf"
mock_s3_client.delete_object.return_value = {}
# Act
await approval_service.process_denial_background(
job_id=job_id,
s3_key=s3_key,
justification="Background denial test",
reviewed_by="bg-deny@uic.edu",
)
# Assert
mock_redis_client.zrem.assert_called_once_with(APPROVAL_TIMEOUT_KEY, job_id)
mock_s3_client.delete_object.assert_called_once()
# Verify status update with decision metadata
status_call = mock_job_service.update_job_status.call_args
assert status_call[0][1] == STATUS_DENIED
assert "denial_decision" in status_call[1]
denial_metadata = status_call[1]["denial_decision"]
assert denial_metadata["decision"] == "denied"
assert denial_metadata["justification"] == "Background denial test"
assert denial_metadata["reviewed_by"] == "bg-deny@uic.edu"
@pytest.mark.asyncio
async def test_process_denial_background_continues_on_s3_failure(
approval_service, mock_redis_client, mock_job_service, mock_s3_client
):
"""Test background denial continues even if S3 cleanup fails."""
# Arrange
job_id = "550e8400-e29b-41d4-a716-446655440024"
mock_s3_client.delete_object.side_effect = Exception("S3 error")
# Act - Should not raise
await approval_service.process_denial_background(
job_id=job_id,
s3_key="temp/fail.pdf",
justification="Test",
reviewed_by="test@test.com",
)
# Assert - Status still updated
mock_job_service.update_job_status.assert_called_once()