new-site/scripts/tests/test_dot_da_binder.py
justin 06e59965cc DOT D&A: instant PDF compliance-program binder (49)
Turn the DOT Drug & Alcohol Compliance Program into an automated
instant-delivery deliverable: when a carrier orders, we generate a
complete, print-ready PDF binder and email it (no admin step).

The binder (dot_da_binder_generator.py) bundles everything a small
carrier needs under 49 CFR Part 382 + Part 40:
  - How to manage the program (DER setup + annual operations)
  - Written drug & alcohol testing policy for employees
  - The six DOT test scenarios + triggers
  - Random testing / consortium (C-TPA) instructions
  - Supervisor reasonable-suspicion training + live/online access
  - Violations, SAP access, return-to-duty / follow-up
  - EAP / rehab / treatment resources (SAMHSA, 988, locator, ODAPC)
  - Recordkeeping retention schedule
  - Ready-to-use forms (acknowledgment, reasonable-suspicion,
    post-accident decision worksheet)
  - Regulation citations
  - Optional state Drug-Free Workplace addendum

Policy-variant selection: FMCSA (Part 382) is the trucking default;
honors an explicit dot_da_mode override for FRA/PHMSA/FTA/FAA/USCG.

New DrugAlcoholProgramHandler returns the binder PDF; slug added to
INSTANT_DELIVERY_SLUGS so job_server emails it automatically. Slug
rerouted from MCS150UpdateHandler (was admin-assisted enrollment) and
re-priced as a discountable own-deliverable (no passthrough cost).

Tests: scripts/tests/test_dot_da_binder.py (FMCSA sections, PHMSA+state
addendum, all-modes render) — passing.
2026-06-02 19:28:58 -05:00

117 lines
4.4 KiB
Python

"""Verify the DOT Drug & Alcohol compliance binder generates correctly.
Renders the binder for the default FMCSA (trucking) variant plus a non-default
DOT mode with a state Drug-Free Workplace addendum, and asserts the PDFs are
multi-page and contain the key deliverable sections the program promises:
written policy, program-management instructions, supervisor training, EAP/SAP
resources, random-testing instructions, recordkeeping, forms, and regulations.
Requires: reportlab, pypdf, and (for text extraction) pdfplumber.
Run from the repo root with a venv that has those installed.
"""
from __future__ import annotations
import importlib.util
import os
import tempfile
_GEN = os.path.abspath(
os.path.join(
os.path.dirname(__file__), "..", "document_gen", "templates",
"dot_da_binder_generator.py",
)
)
_spec = importlib.util.spec_from_file_location("dot_da_binder_generator", _GEN)
_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_mod) # type: ignore[union-attr]
generate_da_binder = _mod.generate_da_binder
MODE_META = _mod.MODE_META
def _extract_text(path: str) -> str:
import pypdf
reader = pypdf.PdfReader(path)
return "\n".join(page.extract_text() or "" for page in reader.pages)
def test_fmcsa_binder_has_all_sections():
with tempfile.TemporaryDirectory() as d:
out = os.path.join(d, "binder.pdf")
path = generate_da_binder(
output_path=out,
carrier_name="Acme Trucking LLC",
dot_number="3456789",
mode="fmcsa",
cdl_drivers=4,
owner_operators=1,
der_name="Jane Owner",
provider_name="Performance West Consortium",
)
assert path and os.path.exists(path)
import pypdf
assert len(pypdf.PdfReader(path).pages) >= 10, "binder should be substantial"
text = _extract_text(path)
# FMCSA-specific facts
assert "49 CFR Part 382" in text
assert "Federal Motor Carrier Safety Administration" in text
assert "Clearinghouse" in text, "FMCSA binder must mention the Clearinghouse"
# The promised deliverables (one assertion each)
for needle in [
"How to Manage Your Program", # program-management instructions
"Written Drug & Alcohol Testing Policy", # written policy
"Random Testing Program", # random testing instructions
"Supervisor", # supervisor training materials
"Substance Abuse Professional", # SAP access
"SAMHSA", # EAP / rehab / treatment resources
"Recordkeeping", # recordkeeping instructions
"Required Compliance Forms", # required forms
"Acknowledgment", # an actual form
"The Regulations", # copies/citations of regulations
]:
assert needle in text, f"missing deliverable section: {needle!r}"
def test_non_fmcsa_mode_and_state_addendum():
with tempfile.TemporaryDirectory() as d:
out = os.path.join(d, "binder_phmsa.pdf")
path = generate_da_binder(
output_path=out,
carrier_name="Pipeline Co",
dot_number="111",
mode="phmsa",
cdl_drivers=3,
state_dfwp="Georgia",
)
assert path and os.path.exists(path)
text = _extract_text(path)
# PHMSA variant swaps in the right rule + agency, no Clearinghouse
assert "49 CFR Part 199" in text
assert "Pipeline and Hazardous Materials Safety Administration" in text
assert "clearinghouse.fmcsa.dot.gov" not in text.lower()
# State addendum present
assert "Georgia Drug-Free Workplace Program" in text
def test_all_modes_render():
with tempfile.TemporaryDirectory() as d:
for mode in MODE_META:
out = os.path.join(d, f"{mode}.pdf")
path = generate_da_binder(
output_path=out,
carrier_name="Test Co",
dot_number="1",
mode=mode,
cdl_drivers=2,
)
assert path and os.path.exists(path), f"mode {mode} failed to render"
if __name__ == "__main__":
test_fmcsa_binder_has_all_sections()
test_non_fmcsa_mode_and_state_addendum()
test_all_modes_render()
print("All DOT D&A binder tests passed.")