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 slack-bot module-level helpers."""
|
|
|
|
|
|
2026-09-25 17:56:46 +00:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
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
|
|
|
from freezegun import freeze_time
|
|
|
|
|
|
|
|
|
|
# Monday 2026-06-01 08:00 ET → weekday, active shift is night.
|
|
|
|
|
MON = "2026-06-01 12:00:00"
|
|
|
|
|
SAT_DATE = "2026-06-06"
|
|
|
|
|
|
|
|
|
|
|
2026-06-01 19:32:40 -04:00
|
|
|
class TestShiftTiming:
|
|
|
|
|
def test_shift_start_weekday_night_is_5pm(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._shift_start("2026-06-03", "night").hour == 17
|
|
|
|
|
|
|
|
|
|
def test_shift_start_weekend_day_is_8am(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._shift_start("2026-06-06", "day").hour == 8
|
|
|
|
|
|
|
|
|
|
@freeze_time("2026-06-01 22:00:00") # 18:00 ET
|
|
|
|
|
def test_shift_started_true_after_start(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._shift_started("2026-06-01", "night") is True
|
|
|
|
|
|
|
|
|
|
@freeze_time(MON) # 08:00 ET
|
|
|
|
|
def test_shift_started_false_before_start(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._shift_started("2026-06-01", "night") is False
|
|
|
|
|
|
|
|
|
|
@freeze_time(MON)
|
|
|
|
|
def test_within_drop_lock_for_today(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._within_drop_lock("2026-06-01", "night") is True
|
|
|
|
|
|
|
|
|
|
@freeze_time(MON) # tomorrow 17:00 is ~33h away
|
|
|
|
|
def test_not_within_lock_tomorrow_morning(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._within_drop_lock("2026-06-02", "night") is False
|
|
|
|
|
|
|
|
|
|
@freeze_time("2026-06-01 22:00:00") # 18:00 ET, tomorrow 17:00 now <24h
|
|
|
|
|
def test_within_lock_once_under_24h(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._within_drop_lock("2026-06-02", "night") is True
|
|
|
|
|
|
|
|
|
|
|
2026-09-25 17:56:46 +00:00
|
|
|
def _save_pay(schedule, week_key, week_start):
|
|
|
|
|
schedule.save_pay_record(
|
|
|
|
|
week_key,
|
|
|
|
|
{
|
|
|
|
|
"week_start": week_start,
|
|
|
|
|
"breakdown": [
|
|
|
|
|
{
|
|
|
|
|
"date": week_start,
|
|
|
|
|
"date_label": "day",
|
|
|
|
|
"day": "Day",
|
|
|
|
|
"name": "Alice",
|
|
|
|
|
"extension": "114",
|
|
|
|
|
"rate": 50,
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"totals": {
|
|
|
|
|
"Alice": {
|
|
|
|
|
"shifts": 1,
|
|
|
|
|
"total": 50,
|
|
|
|
|
"extension": "114",
|
|
|
|
|
"rate": 50,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestShowPay:
|
|
|
|
|
@freeze_time("2026-06-08 12:00:00") # Monday 08:00 ET
|
|
|
|
|
def test_falls_back_to_legacy_monday_key(self, slackbot_app, schedule):
|
|
|
|
|
_save_pay(schedule, "2026-06-01", "2026-06-01")
|
|
|
|
|
respond = MagicMock()
|
|
|
|
|
slackbot_app._show_pay(respond, schedule)
|
|
|
|
|
header = respond.call_args.kwargs["blocks"][0]["text"]["text"]
|
|
|
|
|
assert "Jun 1 to Jun 7" in header
|
|
|
|
|
|
|
|
|
|
@freeze_time("2026-06-14 12:00:00") # Sunday 08:00 ET
|
|
|
|
|
def test_sunday_shows_prior_close_before_monday_job(self, slackbot_app, schedule):
|
|
|
|
|
# Monday 2026-06-08 saved the week of Sun 2026-05-31. The next close
|
|
|
|
|
# (Sun 2026-06-07) is not written until Monday 2026-06-15.
|
|
|
|
|
_save_pay(schedule, "2026-05-31", "2026-05-31")
|
|
|
|
|
respond = MagicMock()
|
|
|
|
|
slackbot_app._show_pay(respond, schedule)
|
|
|
|
|
header = respond.call_args.kwargs["blocks"][0]["text"]["text"]
|
|
|
|
|
assert "May 31 to Jun 6" in header
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
class TestIsToday:
|
|
|
|
|
@freeze_time(MON)
|
|
|
|
|
def test_true_for_today(self, slackbot_app):
|
|
|
|
|
assert slackbot_app.is_today("2026-06-01") is True
|
|
|
|
|
|
|
|
|
|
@freeze_time(MON)
|
|
|
|
|
def test_false_for_other_day(self, slackbot_app):
|
|
|
|
|
assert slackbot_app.is_today("2026-06-02") is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestIsActiveShiftType:
|
|
|
|
|
@freeze_time(MON)
|
|
|
|
|
def test_weekday_active_is_night(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._is_active_shift_type("night") is True
|
|
|
|
|
assert slackbot_app._is_active_shift_type("day") is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestShiftTypeLabel:
|
|
|
|
|
def test_weekday_has_no_label(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._shift_type_label("Wednesday", "night") == ""
|
|
|
|
|
|
|
|
|
|
def test_weekend_day(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._shift_type_label("Saturday", "day") == " (Day)"
|
|
|
|
|
|
|
|
|
|
def test_weekend_night(self, slackbot_app):
|
|
|
|
|
assert slackbot_app._shift_type_label("Saturday", "night") == " (Night)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestFindEmployeeShift:
|
|
|
|
|
def test_weekday_night_match(self, slackbot_app, schedule, seed):
|
|
|
|
|
seed.weekly("Wednesday", "114", "Alice")
|
|
|
|
|
found = slackbot_app._find_employee_shift(
|
|
|
|
|
schedule, "2026-06-03", "Wednesday", "114"
|
|
|
|
|
)
|
|
|
|
|
assert found == ("114", "Alice", "weekly", "night")
|
|
|
|
|
|
|
|
|
|
def test_weekend_day_match(self, slackbot_app, schedule, seed):
|
|
|
|
|
seed.weekly("Saturday", "200", "Alice", shift_type="day")
|
|
|
|
|
found = slackbot_app._find_employee_shift(schedule, SAT_DATE, "Saturday", "200")
|
|
|
|
|
assert found == ("200", "Alice", "weekly", "day")
|
|
|
|
|
|
|
|
|
|
def test_weekend_night_match(self, slackbot_app, schedule, seed):
|
|
|
|
|
seed.weekly("Saturday", "201", "Bob", shift_type="night")
|
|
|
|
|
found = slackbot_app._find_employee_shift(schedule, SAT_DATE, "Saturday", "201")
|
|
|
|
|
assert found == ("201", "Bob", "weekly", "night")
|
|
|
|
|
|
|
|
|
|
def test_not_assigned_returns_none(self, slackbot_app, schedule, seed):
|
|
|
|
|
seed.weekly("Wednesday", "114", "Alice")
|
|
|
|
|
assert (
|
|
|
|
|
slackbot_app._find_employee_shift(
|
|
|
|
|
schedule, "2026-06-03", "Wednesday", "999"
|
|
|
|
|
)
|
|
|
|
|
is None
|
|
|
|
|
)
|