afterhours-shift-manager/tests/ring_scheduler/test_handler.py

62 lines
2.1 KiB
Python
Raw Normal View History

Add pytest suite and wire it into CI (#85) (#86) * Add pytest suite and wire it into CI Stands up the first automated tests for the repo (151 tests) and turns on the CI test step. - Lift slack-bot handlers out of create_app() closures to module level so they're unit-testable; create_app is now a thin Bolt-wiring layer. No behavior change (handler entrypoints and create_app signature unchanged). - tests/ mirrors src/: shared layer (schedule, blocks, 3CX client, ring_scheduler, secrets) + all four Lambdas (pay math, drop/swap/pick/ admin/register/rate, pickup button, roster sync, queue scheduler). - All boundaries mocked: DynamoDB/SES/Secrets via moto, 3CX HTTP via responses, Slack via fakes, time via freezegun. No real network/AWS. - pyproject.toml pytest config (pythonpath=src/shared, importlib mode); per-package conftest loads each app.py under a unique name to avoid the four-app.py collision. tests/requirements.txt for test-only deps. - ci.yaml: run-tests: true (reusable workflow auto-installs deps) and lint the tests dir too. - README Testing section. Closes #85 * Add least-privilege permissions block to CI workflow Resolves the CodeQL actions/missing-workflow-permissions alert: the CI workflow now restricts GITHUB_TOKEN to contents: read (it only checks out, lints, and runs tests). * Stop logging extension numbers in 3CX queue updates Resolves 3 high CodeQL py/clear-text-logging-sensitive-data alerts: the queue/ring-group forwarding logs no longer include the routed extension values (closed/holiday/extension). Non-sensitive context (resource id, queue number) is retained.
2026-06-01 19:07:08 -04:00
"""Tests for the ring-scheduler Lambda handler."""
from unittest.mock import MagicMock
import pytest
from freezegun import freeze_time
from shared.schedule import FALLBACK_EXTENSION
# Monday 2026-06-01 08:00 ET — weekday valid_hours is {8}, so the guard passes
# without force; active shift is night.
MON_0800 = "2026-06-01 12:00:00"
@pytest.fixture
def env(monkeypatch):
monkeypatch.setenv("QUEUE_NUMBER", "800")
monkeypatch.setenv("TCX_SECRET_PREFIX", "afterhours-shift-manager/3cx-")
@pytest.fixture
def routing(ringscheduler_app, monkeypatch):
spy = MagicMock(name="update_queue_routing", return_value={"queue": "800"})
monkeypatch.setattr(ringscheduler_app, "update_queue_routing", spy)
monkeypatch.setattr(ringscheduler_app, "get_secret", lambda _id: "secret")
return spy
@freeze_time(MON_0800)
def test_override_extension_bypasses_schedule(ringscheduler_app, env, routing):
result = ringscheduler_app.handler({"override_extension": "199"}, None)
assert routing.call_args.kwargs["extension"] == "199"
assert result["shift_type"] == "night"
@freeze_time(MON_0800)
def test_resolves_assigned_extension(ringscheduler_app, schedule, seed, env, routing):
seed.weekly("Monday", "114", "Alice")
ringscheduler_app.handler({}, None)
assert routing.call_args.kwargs["extension"] == "114"
assert routing.call_args.kwargs["queue_number"] == "800"
@freeze_time(MON_0800)
def test_unassigned_uses_fallback(ringscheduler_app, schedule, seed, env, routing):
ringscheduler_app.handler({}, None)
assert routing.call_args.kwargs["extension"] == FALLBACK_EXTENSION
@freeze_time(MON_0800)
def test_routing_failure_returns_error(ringscheduler_app, schedule, env, routing):
routing.side_effect = RuntimeError("3CX down")
result = ringscheduler_app.handler({"override_extension": "199"}, None)
assert result["error"] is True
@freeze_time("2026-06-01 13:00:00") # 09:00 ET — not a valid scheduler hour
def test_skips_on_wrong_hour(ringscheduler_app, env, routing):
result = ringscheduler_app.handler({}, None)
assert result == {"skipped": True}
routing.assert_not_called()