Účty, schvalování žáků v /admin a obnova hesla přes SMTP (#11)
CI / test (push) Successful in 8s
CI / docker (push) Successful in 4s

Implementuje účty, role, schvalování žáků v `/admin` a obnovu hesla přes SMTP podle OpenSpec změny `ucty-schvalovani-reset-hesla`.

- Registrace žáků (`/registrace`), přihlášení (`/prihlaseni`), odhlášení (`/odhlaseni`), profil (`/profil`) a chráněný třídní pohled (`/trida`).
- Hashování hesel (`scrypt`), podepsaná session cookie (`HttpOnly`, `SameSite=Lax`, za HTTPS `Secure`), okamžité zneplatnění session při změně hesla či zablokování účtu, globální CSRF kontrola, in-memory rate limiting a volitelný `ProxyFix`.
- Atomická inicializace prvního účtu učitelky z `POZNAVACKY_ADMIN_EMAIL` / `POZNAVACKY_ADMIN_PASSWORD` při startu nad prázdnou tabulkou `uzivatele`.
- Administrace `/admin`, `/admin/tridy` a `/admin/zaci` (správa tříd, fronta schvalování, schválení, zamítnutí, přeřazení, deaktivace/aktivace s ochranou účtů učitelky).
- Obnova zapomenutého hesla (`/obnova-hesla`, `/obnova-hesla/<token>`) s jednorázovým SHA-256 hashovaným tokenem (migrace `0002_token_hash_index.sql`), anti-enumerací a SMTP odesílačem (STARTTLS / `SMTP_SSL`).
- Synchronizovány specifikace `user-auth`, `student-approval`, `password-reset` do `openspec/specs/` a změna archivována.
- Ověřeno offline: `.venv/bin/pytest -W error` (52 passed) a `.venv/bin/ruff check .`.

Closes #2

Reviewed-on: #11
Co-authored-by: David Spáčil <spacildavi@gmail.com>
This commit was merged in pull request #11.
This commit is contained in:
david-spacil authored and David Spáčil committed 2026-11-26 19:40:26 +01:00
1 parent c413e884d0
commit 5fcfe72272
36 files changed
+4921 -3

No files matched your search

+135
View File
@@ -0,0 +1,135 @@
"""Testy SMTP odesílače (STARTTLS, SMTP_SSL, chyby bez úniku tajemství)."""
from __future__ import annotations
import logging
import smtplib
from email.message import EmailMessage
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from poznavacky.mailer import MailDeliveryError, SmtpMailer
def test_smtp_mailer_starttls_and_auth() -> None:
fake_client = MagicMock()
fake_cm = MagicMock()
fake_cm.__enter__.return_value = fake_client
with patch("poznavacky.mailer.smtplib.SMTP", return_value=fake_cm) as cls:
mailer = SmtpMailer(
host="mail.example.test",
port=587,
user="noreply@example.test",
password="tajne-smtp-heslo",
sender="Poznavacky <noreply@example.test>",
use_tls=True,
)
mailer.send_email(
to="zak@example.test",
subject="Obnova hesla",
body="Odkaz: https://poznavacky.example.test/obnova-hesla/tok123",
)
cls.assert_called_once()
assert cls.call_args.args == ("mail.example.test", 587)
fake_client.starttls.assert_called_once()
fake_client.login.assert_called_once_with(
"noreply@example.test",
"tajne-smtp-heslo",
)
fake_client.send_message.assert_called_once()
sent_msg: EmailMessage = fake_client.send_message.call_args.args[0]
assert sent_msg["From"] == "Poznavacky <noreply@example.test>"
assert sent_msg["To"] == "zak@example.test"
assert sent_msg["Subject"] == "Obnova hesla"
assert "tok123" in sent_msg.get_content()
def test_smtp_mailer_uses_smtp_ssl_on_port_465() -> None:
fake_client = MagicMock()
fake_cm = MagicMock()
fake_cm.__enter__.return_value = fake_client
with patch(
"poznavacky.mailer.smtplib.SMTP_SSL",
return_value=fake_cm,
) as ssl_cls:
mailer = SmtpMailer(
host="mail.example.test",
port=465,
user="",
password="",
sender="noreply@example.test",
use_tls=True,
)
mailer.send_email(
to="zak@example.test",
subject="Test SSL",
body="Obsah zprávy",
)
ssl_cls.assert_called_once()
fake_client.starttls.assert_not_called()
fake_client.login.assert_not_called()
fake_client.send_message.assert_called_once()
def test_smtp_mailer_missing_host_raises_without_secrets(
caplog: pytest.LogCaptureFixture,
) -> None:
mailer = SmtpMailer(
host="",
port=587,
user="u@example.test",
password="SUPER-SECRET-SMTP-PASSWORD",
sender="noreply@example.test",
)
with caplog.at_level(logging.WARNING):
with pytest.raises(MailDeliveryError):
mailer.send_email(
to="zak@example.test",
subject="Obnova",
body="TAJNY-RESET-TOKEN-XYZ",
)
assert "SUPER-SECRET-SMTP-PASSWORD" not in caplog.text
assert "TAJNY-RESET-TOKEN-XYZ" not in caplog.text
def test_smtp_mailer_failure_logs_safely_without_password_or_token(
caplog: pytest.LogCaptureFixture,
) -> None:
secret_pass = "VELMI-TAJNE-HESLO-SMTP"
secret_token = "SUROVY-TOKEN-123456"
def _boom(*args: Any, **kwargs: Any) -> Any:
raise smtplib.SMTPAuthenticationError(
535,
f"Auth failed for {secret_pass}".encode(),
)
with (
patch("poznavacky.mailer.smtplib.SMTP", side_effect=_boom),
caplog.at_level(logging.ERROR),
):
mailer = SmtpMailer(
host="mail.example.test",
port=587,
user="u@example.test",
password=secret_pass,
sender="noreply@example.test",
)
with pytest.raises(MailDeliveryError) as exc_info:
mailer.send_email(
to="zak@example.test",
subject="Obnova",
body=f"https://example.test/obnova-hesla/{secret_token}",
)
assert secret_pass not in caplog.text
assert secret_token not in caplog.text
assert secret_pass not in str(exc_info.value)
assert secret_token not in str(exc_info.value)