meal-order-manager/tests/test_worker.py
Adam Moussa f48a82c476
Some checks are pending
Deploy API / Resolve target (push) Waiting to run
Deploy API / Deploy API to (push) Blocked by required conditions
feat(api): serve meals on ECS Fargate instead of Lambda (PLAT-215) (#199)
* feat(api): serve meals on ECS Fargate instead of Lambda

Keep the Flask app always-on with in-process jobs so CloudFront no longer fronts a cold-start API Gateway.

* fix(jobs): run delayed close and reminder deliveries

Wall-clock skip windows dropped the only weekly SQS attempt when Scheduler already fired in Eastern time. Dev schedules stay disabled.

* fix(api): return JSON objects and stop logging job payloads

Flask now jsonify-s handler dicts so API responses are not HTML, and the worker logs only event and status.

* fix(ci): restore the reusable workflow so the required check is named ci / ci

Inlining the job reported `ci` instead of the org ruleset's `ci / ci`.

* fix(secrets): drop unused os import so ruff check passes

* style: apply ruff format so ci-python-app lint passes

* fix(infra): give meals its own VPC because prod has none

* chore(security): re-key ALB SG checkov suppression after vpc.tf
2026-09-21 19:34:24 +00:00

53 lines
1.6 KiB
Python

"""SQS worker deletes completed jobs and leaves skipped messages for retry."""
import json
from unittest.mock import MagicMock, patch
from server import worker
def _one_message_then_stop(body: dict):
def receive_message(**_kwargs):
worker._stop(None, None)
return {
"Messages": [
{
"ReceiptHandle": "rh-1",
"Body": json.dumps(body),
}
]
}
return receive_message
@patch("server.worker.boto3.client")
@patch("server.worker.run_job")
def test_worker_does_not_delete_skipped_jobs(mock_run, mock_client):
sqs = MagicMock()
mock_client.return_value = sqs
sqs.receive_message.side_effect = _one_message_then_stop({"event": "close"})
mock_run.return_value = {"status": "skipped", "reason": "outside window"}
with patch.dict("os.environ", {"JOBS_QUEUE_URL": "https://sqs.example/jobs"}):
worker._running = True
worker.main()
sqs.delete_message.assert_not_called()
@patch("server.worker.boto3.client")
@patch("server.worker.run_job")
def test_worker_deletes_completed_jobs(mock_run, mock_client):
sqs = MagicMock()
mock_client.return_value = sqs
sqs.receive_message.side_effect = _one_message_then_stop({"event": "close"})
mock_run.return_value = {"status": "closed", "week": "2026-W19"}
with patch.dict("os.environ", {"JOBS_QUEUE_URL": "https://sqs.example/jobs"}):
worker._running = True
worker.main()
sqs.delete_message.assert_called_once_with(
QueueUrl="https://sqs.example/jobs", ReceiptHandle="rh-1"
)