"""
Пароль, выданный без почты, не едет в адресе страницы (24.09.2026).
"""

import pathlib

ROOT = pathlib.Path(__file__).resolve().parents[2]
WEB = (ROOT / "engine" / "timesheet" / "webapp.py").read_text(encoding="utf-8")
CARD = (ROOT / "engine" / "timesheet" / "templates" / "staff_card.html").read_text(encoding="utf-8")
SQL = (ROOT / "sql" / "78_one_time_note.sql").read_text(encoding="utf-8")


def test_the_password_is_not_put_into_the_message():
    body = WEB[WEB.index("def _deliver_password("):]
    body = body[:body.index("\n\n\n")]
    #  В тексте для адреса пароля нет — он уходит в одноразовую записку
    assert "показан один раз: {password}" not in body
    assert '_note_put(conn, f"Пароль для {email}: {password}")' in body


def test_every_caller_passes_only_the_note_number():
    assert WEB.count("note, token = _deliver_password(") == 3
    assert WEB.count("note=token") == 3
    assert 'parts.append(f"note={quote(note)}")' in WEB


def test_the_note_is_read_once_and_only_by_its_author():
    assert "DELETE FROM app.one_time_note WHERE token = %s" in WEB
    assert "RETURNING body" in WEB
    assert "USING (user_id = current_user_id())" in SQL
    assert "GRANT SELECT, INSERT, DELETE ON one_time_note TO ooucr_app" in SQL
    #  Без аудита: иначе пароль осел бы в журнале «было → стало»
    assert "audit_trigger" not in SQL


def test_the_page_with_the_password_is_not_cached():
    assert 'resp.headers["Cache-Control"] = "no-store"' in WEB
    assert "{% if secret_note %}" in CARD
