mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-19 14:08:47 -04:00
Added light polling endpoint (#2006)
* Added light polling endpoint * Import linting * CICD checks updated
This commit is contained in:
@@ -1215,16 +1215,16 @@ def test_get_scheduled_job_stats(admin_request):
|
||||
}
|
||||
|
||||
|
||||
def test_get_job_status_returns_lightweight_response(admin_request, sample_job):
|
||||
"""Test that the new status endpoint returns only essential fields."""
|
||||
def test_get_job_status_returns_light_response(admin_request, sample_job):
|
||||
"""Test that the status endpoint returns only required fields."""
|
||||
job_id = str(sample_job.id)
|
||||
service_id = sample_job.service.id
|
||||
|
||||
sample_job.notification_count = 5
|
||||
|
||||
create_notification(job=sample_job, status=NotificationStatus.SENT)
|
||||
create_notification(job=sample_job, status=NotificationStatus.SENT)
|
||||
create_notification(job=sample_job, status=NotificationStatus.DELIVERED)
|
||||
create_notification(job=sample_job, status=NotificationStatus.FAILED)
|
||||
create_notification(job=sample_job, status=NotificationStatus.PENDING)
|
||||
create_notification(job=sample_job, status=NotificationStatus.CREATED)
|
||||
|
||||
resp_json = admin_request.get(
|
||||
"job.get_job_status",
|
||||
@@ -1233,42 +1233,112 @@ def test_get_job_status_returns_lightweight_response(admin_request, sample_job):
|
||||
)
|
||||
|
||||
assert set(resp_json.keys()) == {
|
||||
"sent_count",
|
||||
"failed_count",
|
||||
"pending_count",
|
||||
"total_count",
|
||||
"job_status",
|
||||
"processing_finished",
|
||||
"total",
|
||||
"delivered",
|
||||
"failed",
|
||||
"pending",
|
||||
"finished",
|
||||
}
|
||||
|
||||
# Verify counts are correct
|
||||
assert resp_json["sent_count"] == 2
|
||||
assert resp_json["failed_count"] == 1
|
||||
assert resp_json["pending_count"] == 2
|
||||
assert resp_json["total_count"] == sample_job.notification_count
|
||||
assert resp_json["job_status"] == sample_job.job_status
|
||||
assert resp_json["processing_finished"] == (
|
||||
sample_job.processing_finished is not None
|
||||
)
|
||||
assert resp_json["total"] == 5
|
||||
assert resp_json["delivered"] == 2 # sent + delivered
|
||||
assert resp_json["failed"] == 1
|
||||
assert resp_json["pending"] == 2 # total - delivered - failed
|
||||
assert resp_json["finished"] is False
|
||||
|
||||
|
||||
def test_get_job_status_caches_response(admin_request, sample_job, mocker):
|
||||
"""Test that the status endpoint uses caching."""
|
||||
def test_get_job_status_counts_all_delivered_statuses(admin_request, sample_job):
|
||||
"""Test that delivered count includes both 'delivered' and 'sent' statuses."""
|
||||
job_id = str(sample_job.id)
|
||||
service_id = sample_job.service.id
|
||||
|
||||
sample_job.notification_count = 4
|
||||
|
||||
create_notification(job=sample_job, status=NotificationStatus.SENT)
|
||||
create_notification(job=sample_job, status=NotificationStatus.SENT)
|
||||
create_notification(job=sample_job, status=NotificationStatus.DELIVERED)
|
||||
create_notification(job=sample_job, status=NotificationStatus.DELIVERED)
|
||||
|
||||
mock_redis_get = mocker.patch("app.job.rest.redis_store.get", return_value=None)
|
||||
mock_redis_set = mocker.patch("app.job.rest.redis_store.set")
|
||||
|
||||
# First request should cache the result
|
||||
admin_request.get(
|
||||
resp_json = admin_request.get(
|
||||
"job.get_job_status",
|
||||
service_id=service_id,
|
||||
job_id=job_id,
|
||||
)
|
||||
|
||||
cache_key = f"job_status:{service_id}:{job_id}"
|
||||
mock_redis_get.assert_called_once_with(cache_key)
|
||||
mock_redis_set.assert_called_once()
|
||||
assert resp_json["delivered"] == 4
|
||||
assert resp_json["failed"] == 0
|
||||
assert resp_json["pending"] == 0
|
||||
|
||||
|
||||
def test_get_job_status_counts_all_failed_statuses(admin_request, sample_job):
|
||||
"""Test that failed count includes all failure status types."""
|
||||
job_id = str(sample_job.id)
|
||||
service_id = sample_job.service.id
|
||||
|
||||
sample_job.notification_count = 6
|
||||
|
||||
create_notification(job=sample_job, status=NotificationStatus.FAILED)
|
||||
create_notification(job=sample_job, status=NotificationStatus.TECHNICAL_FAILURE)
|
||||
create_notification(job=sample_job, status=NotificationStatus.TEMPORARY_FAILURE)
|
||||
create_notification(job=sample_job, status=NotificationStatus.PERMANENT_FAILURE)
|
||||
create_notification(job=sample_job, status=NotificationStatus.VALIDATION_FAILED)
|
||||
create_notification(job=sample_job, status=NotificationStatus.VIRUS_SCAN_FAILED)
|
||||
|
||||
resp_json = admin_request.get(
|
||||
"job.get_job_status",
|
||||
service_id=service_id,
|
||||
job_id=job_id,
|
||||
)
|
||||
|
||||
assert resp_json["delivered"] == 0
|
||||
assert resp_json["failed"] == 6
|
||||
assert resp_json["pending"] == 0
|
||||
|
||||
|
||||
def test_get_job_status_finished_when_processing_complete_and_no_pending(
|
||||
admin_request, sample_job
|
||||
):
|
||||
"""Test that finished is True only when processing_finished is set and pending is 0."""
|
||||
from app.utils import utc_now
|
||||
|
||||
job_id = str(sample_job.id)
|
||||
service_id = sample_job.service.id
|
||||
|
||||
sample_job.notification_count = 2
|
||||
sample_job.processing_finished = utc_now()
|
||||
|
||||
create_notification(job=sample_job, status=NotificationStatus.DELIVERED)
|
||||
create_notification(job=sample_job, status=NotificationStatus.DELIVERED)
|
||||
|
||||
resp_json = admin_request.get(
|
||||
"job.get_job_status",
|
||||
service_id=service_id,
|
||||
job_id=job_id,
|
||||
)
|
||||
|
||||
assert resp_json["pending"] == 0
|
||||
assert resp_json["finished"] is True
|
||||
|
||||
|
||||
def test_get_job_status_not_finished_when_pending_exists(admin_request, sample_job):
|
||||
"""Test that finished is False when there are still pending notifications."""
|
||||
from app.utils import utc_now
|
||||
|
||||
job_id = str(sample_job.id)
|
||||
service_id = sample_job.service.id
|
||||
|
||||
sample_job.notification_count = 5
|
||||
sample_job.processing_finished = utc_now()
|
||||
|
||||
create_notification(job=sample_job, status=NotificationStatus.DELIVERED)
|
||||
|
||||
resp_json = admin_request.get(
|
||||
"job.get_job_status",
|
||||
service_id=service_id,
|
||||
job_id=job_id,
|
||||
)
|
||||
|
||||
assert resp_json["pending"] == 4
|
||||
assert (
|
||||
resp_json["finished"] is False
|
||||
) # Still has pending even though processing_finished is set
|
||||
|
||||
Reference in New Issue
Block a user