Účty, schvalování žáků v /admin a obnova hesla přes SMTP (#11)
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:
1 parent
c413e884d0
commit
5fcfe72272
36 files changed
+4921
-3
No files matched your search
@@ -0,0 +1,615 @@
|
||||
"""Integrační testy účtů, bootstrapu, session, CSRF, rate limitu a rolí."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Flask
|
||||
from flask.testing import FlaskClient
|
||||
|
||||
from poznavacky import auth, create_app
|
||||
from poznavacky import db as db_module
|
||||
from poznavacky.security import RateLimiter, hash_password, verify_password
|
||||
|
||||
_CSRF_RE = re.compile(r'name="csrf_token"\s+value="([^"]+)"')
|
||||
|
||||
|
||||
def _extract_csrf(html: str) -> str:
|
||||
match = _CSRF_RE.search(html)
|
||||
assert match is not None, "CSRF token nebyl nalezen v HTML formuláři."
|
||||
return match.group(1)
|
||||
|
||||
|
||||
def _get_csrf(client: FlaskClient, path: str = "/prihlaseni") -> str:
|
||||
resp = client.get(path)
|
||||
assert resp.status_code == 200
|
||||
return _extract_csrf(resp.get_data(as_text=True))
|
||||
|
||||
|
||||
def _insert_class(
|
||||
db: sqlite3.Connection,
|
||||
nazev: str,
|
||||
rocnik: int,
|
||||
*,
|
||||
aktivni: int = 1,
|
||||
) -> int:
|
||||
cur = db.execute(
|
||||
"INSERT INTO tridy (nazev, rocnik, aktivni) VALUES (?, ?, ?)",
|
||||
(nazev, rocnik, aktivni),
|
||||
)
|
||||
db.commit()
|
||||
assert cur.lastrowid is not None
|
||||
return int(cur.lastrowid)
|
||||
|
||||
|
||||
def _insert_user(
|
||||
db: sqlite3.Connection,
|
||||
*,
|
||||
email: str,
|
||||
password: str,
|
||||
jmeno: str = "Petr",
|
||||
prijmeni: str = "Ukazkovy",
|
||||
prezdivka: str | None = None,
|
||||
role: str = "zak",
|
||||
stav: str = "ceka_na_schvaleni",
|
||||
trida_id: int | None = None,
|
||||
) -> int:
|
||||
cur = db.execute(
|
||||
"""
|
||||
INSERT INTO uzivatele (
|
||||
email, heslo_hash, jmeno, prijmeni, prezdivka,
|
||||
role, stav, trida_id
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
email.lower(),
|
||||
hash_password(password),
|
||||
jmeno,
|
||||
prijmeni,
|
||||
prezdivka,
|
||||
role,
|
||||
stav,
|
||||
trida_id,
|
||||
),
|
||||
)
|
||||
db.commit()
|
||||
assert cur.lastrowid is not None
|
||||
return int(cur.lastrowid)
|
||||
|
||||
|
||||
def _login(
|
||||
client: FlaskClient,
|
||||
email: str,
|
||||
password: str,
|
||||
*,
|
||||
follow_redirects: bool = False,
|
||||
):
|
||||
token = _get_csrf(client, "/prihlaseni")
|
||||
return client.post(
|
||||
"/prihlaseni",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": email,
|
||||
"heslo": password,
|
||||
},
|
||||
follow_redirects=follow_redirects,
|
||||
)
|
||||
|
||||
|
||||
def test_bootstrap_admin_account_runs_only_on_empty_table(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
data_dir = tmp_path / "data"
|
||||
env = {
|
||||
"POZNAVACKY_SECRET_KEY": "klic-bootstrap",
|
||||
"POZNAVACKY_DATA_DIR": str(data_dir),
|
||||
"POZNAVACKY_ADMIN_EMAIL": " Ucitelka@Example.Test ",
|
||||
"POZNAVACKY_ADMIN_PASSWORD": "silne-heslo-ucitelky",
|
||||
}
|
||||
|
||||
app1 = create_app(environ=env)
|
||||
conn = db_module.connect(app1.config["DB_PATH"])
|
||||
try:
|
||||
rows = conn.execute("SELECT * FROM uzivatele").fetchall()
|
||||
assert len(rows) == 1
|
||||
admin = rows[0]
|
||||
assert admin["email"] == "ucitelka@example.test"
|
||||
assert admin["role"] == "ucitelka"
|
||||
assert admin["stav"] == "schvaleny"
|
||||
assert admin["schvaleno_at"] is not None
|
||||
assert admin["heslo_hash"].startswith("scrypt:")
|
||||
assert verify_password(admin["heslo_hash"], "silne-heslo-ucitelky")
|
||||
|
||||
# Simulace změny hesla učitelky před dalším startem.
|
||||
new_hash = hash_password("nove-zmenene-heslo")
|
||||
conn.execute(
|
||||
"UPDATE uzivatele SET heslo_hash = ? WHERE id = ?",
|
||||
(new_hash, admin["id"]),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# Opakovaný start nesmí vytvořit duplikát ani přepsat heslo.
|
||||
app2 = create_app(environ=env)
|
||||
conn = db_module.connect(app2.config["DB_PATH"])
|
||||
try:
|
||||
rows = conn.execute("SELECT * FROM uzivatele").fetchall()
|
||||
assert len(rows) == 1
|
||||
assert verify_password(rows[0]["heslo_hash"], "nove-zmenene-heslo")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_student_registration_success_and_validation(
|
||||
client: FlaskClient,
|
||||
db: sqlite3.Connection,
|
||||
) -> None:
|
||||
active_id = _insert_class(db, "6.Z", 6, aktivni=1)
|
||||
inactive_id = _insert_class(db, "9.Z", 9, aktivni=0)
|
||||
|
||||
# Deaktivovaná třída není v registračním formuláři.
|
||||
reg_page = client.get("/registrace")
|
||||
html = reg_page.get_data(as_text=True)
|
||||
assert "6.Z" in html
|
||||
assert "9.Z" not in html
|
||||
token = _extract_csrf(html)
|
||||
|
||||
# Úspěšná registrace.
|
||||
resp = client.post(
|
||||
"/registrace",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": " Zak.Novy@Example.Test ",
|
||||
"heslo": "heslo1234",
|
||||
"jmeno": "Anna",
|
||||
"prijmeni": "Novotna",
|
||||
"prezdivka": "Vrabec",
|
||||
"trida_id": str(active_id),
|
||||
},
|
||||
follow_redirects=True,
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
assert "čeká na schválení" in resp.get_data(as_text=True).lower()
|
||||
|
||||
row = db.execute(
|
||||
"SELECT * FROM uzivatele WHERE email = ?",
|
||||
("zak.novy@example.test",),
|
||||
).fetchone()
|
||||
assert row is not None
|
||||
assert row["role"] == "zak"
|
||||
assert row["stav"] == "ceka_na_schvaleni"
|
||||
assert row["trida_id"] == active_id
|
||||
assert row["prezdivka"] == "Vrabec"
|
||||
assert row["heslo_hash"].startswith("scrypt:")
|
||||
assert "heslo1234" not in row["heslo_hash"]
|
||||
|
||||
# Duplicitní e-mail s jinou velikostí písmen je odmítnut.
|
||||
token = _get_csrf(client, "/registrace")
|
||||
dup_resp = client.post(
|
||||
"/registrace",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "ZAK.NOVY@example.test",
|
||||
"heslo": "dalsiheslo123",
|
||||
"jmeno": "Jan",
|
||||
"prijmeni": "Test",
|
||||
"trida_id": str(active_id),
|
||||
},
|
||||
)
|
||||
assert dup_resp.status_code == 400
|
||||
assert db.execute("SELECT COUNT(*) FROM uzivatele").fetchone()[0] == 1
|
||||
|
||||
# Krátké heslo je odmítnuto.
|
||||
short_resp = client.post(
|
||||
"/registrace",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "kratky@example.test",
|
||||
"heslo": "1234567",
|
||||
"jmeno": "Jan",
|
||||
"prijmeni": "Test",
|
||||
"trida_id": str(active_id),
|
||||
},
|
||||
)
|
||||
assert short_resp.status_code == 400
|
||||
|
||||
# Neaktivní třída je odmítnuta.
|
||||
inactive_resp = client.post(
|
||||
"/registrace",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "neaktivni@example.test",
|
||||
"heslo": "dlouheheslo",
|
||||
"jmeno": "Jan",
|
||||
"prijmeni": "Test",
|
||||
"trida_id": str(inactive_id),
|
||||
},
|
||||
)
|
||||
assert inactive_resp.status_code == 400
|
||||
assert db.execute("SELECT COUNT(*) FROM uzivatele").fetchone()[0] == 1
|
||||
|
||||
|
||||
def test_registration_without_any_active_classes_sets_null_class(
|
||||
client: FlaskClient,
|
||||
db: sqlite3.Connection,
|
||||
) -> None:
|
||||
token = _get_csrf(client, "/registrace")
|
||||
resp = client.post(
|
||||
"/registrace",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "bez.tridy@example.test",
|
||||
"heslo": "heslo1234",
|
||||
"jmeno": "Karel",
|
||||
"prijmeni": "Testovy",
|
||||
"trida_id": "",
|
||||
},
|
||||
follow_redirects=False,
|
||||
)
|
||||
assert resp.status_code == 302
|
||||
row = db.execute(
|
||||
"SELECT trida_id, stav FROM uzivatele WHERE email = ?",
|
||||
("bez.tridy@example.test",),
|
||||
).fetchone()
|
||||
assert row is not None
|
||||
assert row["trida_id"] is None
|
||||
assert row["stav"] == "ceka_na_schvaleni"
|
||||
|
||||
|
||||
def test_login_logout_cookie_attributes_and_blocked_states(
|
||||
client: FlaskClient,
|
||||
db: sqlite3.Connection,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
_insert_user(
|
||||
db,
|
||||
email="cekajici@example.test",
|
||||
password="heslo-cekajici",
|
||||
stav="ceka_na_schvaleni",
|
||||
)
|
||||
_insert_user(
|
||||
db,
|
||||
email="zamitnuty@example.test",
|
||||
password="heslo-zamitnuty",
|
||||
stav="zamitnuty",
|
||||
)
|
||||
_insert_user(
|
||||
db,
|
||||
email="deaktivovany@example.test",
|
||||
password="heslo-deaktivovany",
|
||||
stav="deaktivovany",
|
||||
)
|
||||
|
||||
# Čekající žák se přihlásí a cookie má HttpOnly + SameSite=Lax.
|
||||
resp = _login(client, "cekajici@example.test", "heslo-cekajici")
|
||||
assert resp.status_code == 302
|
||||
cookies = resp.headers.getlist("Set-Cookie")
|
||||
assert any("HttpOnly" in c and "SameSite=Lax" in c for c in cookies)
|
||||
|
||||
assert client.get("/profil").status_code == 200
|
||||
|
||||
# Odhlášení vyčistí session.
|
||||
logout_token = _get_csrf(client, "/profil")
|
||||
out = client.post("/odhlaseni", data={"csrf_token": logout_token})
|
||||
assert out.status_code == 302
|
||||
assert client.get("/profil").status_code == 302
|
||||
|
||||
# Zamítnutý a deaktivovaný účet se nepřihlásí ani se správným heslem.
|
||||
for email, pwd in [
|
||||
("zamitnuty@example.test", "heslo-zamitnuty"),
|
||||
("deaktivovany@example.test", "heslo-deaktivovany"),
|
||||
]:
|
||||
denied = _login(client, email, pwd)
|
||||
assert denied.status_code == 403
|
||||
assert client.get("/profil").status_code == 302
|
||||
|
||||
# HTTPS aplikace nastavuje atribut Secure.
|
||||
https_app = create_app(
|
||||
{
|
||||
"TESTING": True,
|
||||
"POZNAVACKY_SECRET_KEY": "https-klic",
|
||||
"POZNAVACKY_DATA_DIR": str(tmp_path / "https-data"),
|
||||
"POZNAVACKY_BASE_URL": "https://poznavacky.example.test",
|
||||
},
|
||||
environ={},
|
||||
)
|
||||
https_conn = db_module.connect(https_app.config["DB_PATH"])
|
||||
try:
|
||||
_insert_user(
|
||||
https_conn,
|
||||
email="https@example.test",
|
||||
password="heslo-https",
|
||||
stav="schvaleny",
|
||||
)
|
||||
finally:
|
||||
https_conn.close()
|
||||
|
||||
https_client = https_app.test_client()
|
||||
https_get = https_client.get(
|
||||
"/prihlaseni",
|
||||
base_url="https://poznavacky.example.test",
|
||||
)
|
||||
token = _extract_csrf(https_get.get_data(as_text=True))
|
||||
https_resp = https_client.post(
|
||||
"/prihlaseni",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "https@example.test",
|
||||
"heslo": "heslo-https",
|
||||
},
|
||||
base_url="https://poznavacky.example.test",
|
||||
)
|
||||
assert https_resp.status_code == 302
|
||||
https_cookies = https_resp.headers.getlist("Set-Cookie")
|
||||
assert any(
|
||||
"Secure" in c and "HttpOnly" in c and "SameSite=Lax" in c
|
||||
for c in https_cookies
|
||||
)
|
||||
|
||||
|
||||
def test_session_invalidated_on_state_or_password_change(
|
||||
client: FlaskClient,
|
||||
db: sqlite3.Connection,
|
||||
) -> None:
|
||||
user_id = _insert_user(
|
||||
db,
|
||||
email="session@example.test",
|
||||
password="puvodni-heslo",
|
||||
stav="schvaleny",
|
||||
)
|
||||
login_resp = _login(client, "session@example.test", "puvodni-heslo")
|
||||
assert login_resp.status_code == 302
|
||||
assert client.get("/profil").status_code == 200
|
||||
|
||||
# Změna hesla v DB okamžitě zneplatní existující session.
|
||||
db.execute(
|
||||
"UPDATE uzivatele SET heslo_hash = ? WHERE id = ?",
|
||||
(hash_password("jine-heslo-123"), user_id),
|
||||
)
|
||||
db.commit()
|
||||
assert client.get("/profil").status_code == 302
|
||||
|
||||
# Znovu přihlásit novým heslem a pak deaktivovat účet.
|
||||
relogin = _login(client, "session@example.test", "jine-heslo-123")
|
||||
assert relogin.status_code == 302
|
||||
assert client.get("/profil").status_code == 200
|
||||
|
||||
db.execute(
|
||||
"UPDATE uzivatele SET stav = 'deaktivovany' WHERE id = ?",
|
||||
(user_id,),
|
||||
)
|
||||
db.commit()
|
||||
assert client.get("/profil").status_code == 302
|
||||
|
||||
|
||||
def test_authorization_matrix_and_profile_nickname_update(
|
||||
app: Flask,
|
||||
client: FlaskClient,
|
||||
db: sqlite3.Connection,
|
||||
) -> None:
|
||||
@app.get("/_test-teacher-only")
|
||||
@auth.teacher_required
|
||||
def _teacher_probe() -> str:
|
||||
return "ok-teacher"
|
||||
|
||||
cls_id = _insert_class(db, "7.Z", 7)
|
||||
_insert_user(
|
||||
db,
|
||||
email="pending@example.test",
|
||||
password="heslo-pending",
|
||||
jmeno="Anna",
|
||||
prijmeni="Novotna",
|
||||
stav="ceka_na_schvaleni",
|
||||
trida_id=cls_id,
|
||||
)
|
||||
_insert_user(
|
||||
db,
|
||||
email="approved@example.test",
|
||||
password="heslo-approved",
|
||||
jmeno="Petr",
|
||||
prijmeni="Ukazkovy",
|
||||
prezdivka="Sokolik",
|
||||
stav="schvaleny",
|
||||
trida_id=cls_id,
|
||||
)
|
||||
_insert_user(
|
||||
db,
|
||||
email="teacher@example.test",
|
||||
password="heslo-teacher",
|
||||
role="ucitelka",
|
||||
stav="schvaleny",
|
||||
)
|
||||
|
||||
# Nepřihlášený uživatel.
|
||||
assert auth.can_view_leaderboards(None) is False
|
||||
assert client.get("/profil").status_code == 302
|
||||
assert client.get("/trida").status_code == 302
|
||||
assert client.get("/_test-teacher-only").status_code == 302
|
||||
|
||||
# Neschválený žák: profil ano + úprava přezdívky, žebříček/učitelka 403.
|
||||
_login(client, "pending@example.test", "heslo-pending")
|
||||
prof = client.get("/profil")
|
||||
assert prof.status_code == 200
|
||||
body = prof.get_data(as_text=True)
|
||||
assert "Čeká na schválení" in body
|
||||
assert "Anna N." in body
|
||||
assert client.get("/trida").status_code == 403
|
||||
assert client.get("/_test-teacher-only").status_code == 403
|
||||
|
||||
csrf = _extract_csrf(body)
|
||||
upd = client.post(
|
||||
"/profil",
|
||||
data={"csrf_token": csrf, "prezdivka": "Sova"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
assert upd.status_code == 200
|
||||
assert "Sova" in upd.get_data(as_text=True)
|
||||
|
||||
# Schválený žák: profil + /trida ano, učitelský endpoint 403.
|
||||
_login(client, "approved@example.test", "heslo-approved")
|
||||
assert client.get("/profil").status_code == 200
|
||||
cls_resp = client.get("/trida")
|
||||
assert cls_resp.status_code == 200
|
||||
assert "Sokolik" in cls_resp.get_data(as_text=True)
|
||||
assert client.get("/_test-teacher-only").status_code == 403
|
||||
|
||||
# Schválená učitelka: vše povoleno.
|
||||
_login(client, "teacher@example.test", "heslo-teacher")
|
||||
assert client.get("/profil").status_code == 200
|
||||
assert client.get("/trida").status_code == 200
|
||||
assert client.get("/_test-teacher-only").status_code == 200
|
||||
|
||||
|
||||
def test_csrf_protection_blocks_missing_and_allows_header_token(
|
||||
client: FlaskClient,
|
||||
db: sqlite3.Connection,
|
||||
) -> None:
|
||||
_insert_user(
|
||||
db,
|
||||
email="csrf@example.test",
|
||||
password="heslo-csrf",
|
||||
stav="schvaleny",
|
||||
)
|
||||
|
||||
# POST bez CSRF tokenu je odmítnut 403.
|
||||
for path in ("/prihlaseni", "/registrace", "/odhlaseni"):
|
||||
resp = client.post(path, data={"email": "csrf@example.test"})
|
||||
assert resp.status_code == 403
|
||||
|
||||
# Hlavička X-CSRF-Token projde.
|
||||
token = _get_csrf(client, "/prihlaseni")
|
||||
resp = client.post(
|
||||
"/prihlaseni",
|
||||
data={"email": "csrf@example.test", "heslo": "heslo-csrf"},
|
||||
headers={"X-CSRF-Token": token},
|
||||
)
|
||||
assert resp.status_code == 302
|
||||
|
||||
|
||||
def test_rate_limiting_blocks_and_recovers_with_clock(
|
||||
app: Flask,
|
||||
client: FlaskClient,
|
||||
db: sqlite3.Connection,
|
||||
) -> None:
|
||||
now = [1000.0]
|
||||
app.extensions["rate_limiter"] = RateLimiter(clock=lambda: now[0])
|
||||
app.config["RATE_LIMITS"] = {
|
||||
"login": (2, 60),
|
||||
"register": (1, 300),
|
||||
"password_reset": (2, 300),
|
||||
}
|
||||
|
||||
_insert_user(
|
||||
db,
|
||||
email="limit@example.test",
|
||||
password="spravne-heslo",
|
||||
stav="schvaleny",
|
||||
)
|
||||
|
||||
token = _get_csrf(client, "/prihlaseni")
|
||||
for _ in range(2):
|
||||
bad = client.post(
|
||||
"/prihlaseni",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "limit@example.test",
|
||||
"heslo": "spatne-heslo",
|
||||
},
|
||||
)
|
||||
assert bad.status_code == 401
|
||||
|
||||
# 3. pokus i se správným heslem vrátí 429 a nepřihlásí.
|
||||
blocked = client.post(
|
||||
"/prihlaseni",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "limit@example.test",
|
||||
"heslo": "spravne-heslo",
|
||||
},
|
||||
)
|
||||
assert blocked.status_code == 429
|
||||
assert int(blocked.headers["Retry-After"]) >= 1
|
||||
assert client.get("/profil").status_code == 302
|
||||
|
||||
# Po uplynutí okna je přihlášení opět povoleno.
|
||||
now[0] = 1061.0
|
||||
ok = client.post(
|
||||
"/prihlaseni",
|
||||
data={
|
||||
"csrf_token": token,
|
||||
"email": "limit@example.test",
|
||||
"heslo": "spravne-heslo",
|
||||
},
|
||||
)
|
||||
assert ok.status_code == 302
|
||||
|
||||
# Rate limit na registraci.
|
||||
reg_token = _get_csrf(client, "/registrace")
|
||||
first_reg = client.post(
|
||||
"/registrace",
|
||||
data={
|
||||
"csrf_token": reg_token,
|
||||
"email": "prvni.reg@example.test",
|
||||
"heslo": "heslo1234",
|
||||
"jmeno": "A",
|
||||
"prijmeni": "B",
|
||||
},
|
||||
)
|
||||
assert first_reg.status_code == 302
|
||||
second_reg = client.post(
|
||||
"/registrace",
|
||||
data={
|
||||
"csrf_token": reg_token,
|
||||
"email": "druha.reg@example.test",
|
||||
"heslo": "heslo1234",
|
||||
"jmeno": "C",
|
||||
"prijmeni": "D",
|
||||
},
|
||||
)
|
||||
assert second_reg.status_code == 429
|
||||
row = db.execute(
|
||||
"SELECT id FROM uzivatele WHERE email = ?",
|
||||
("druha.reg@example.test",),
|
||||
).fetchone()
|
||||
assert row is None
|
||||
|
||||
|
||||
def test_trust_proxy_uses_forwarded_client_ip(tmp_path: Path) -> None:
|
||||
app = create_app(
|
||||
{
|
||||
"TESTING": True,
|
||||
"POZNAVACKY_SECRET_KEY": "proxy-klic",
|
||||
"POZNAVACKY_DATA_DIR": str(tmp_path / "proxy-data"),
|
||||
"POZNAVACKY_TRUST_PROXY": 1,
|
||||
},
|
||||
environ={},
|
||||
)
|
||||
app.config["RATE_LIMITS"]["login"] = (1, 60)
|
||||
client = app.test_client()
|
||||
token = _get_csrf(client, "/prihlaseni")
|
||||
|
||||
first = client.post(
|
||||
"/prihlaseni",
|
||||
data={"csrf_token": token, "email": "a@example.test", "heslo": "x"},
|
||||
headers={"X-Forwarded-For": "198.51.100.1"},
|
||||
)
|
||||
assert first.status_code == 401
|
||||
|
||||
# Stejná klientská IP přes proxy narazí na limit.
|
||||
second = client.post(
|
||||
"/prihlaseni",
|
||||
data={"csrf_token": token, "email": "a@example.test", "heslo": "x"},
|
||||
headers={"X-Forwarded-For": "198.51.100.1"},
|
||||
)
|
||||
assert second.status_code == 429
|
||||
|
||||
# Jiná klientská IP přes proxy projde.
|
||||
other = client.post(
|
||||
"/prihlaseni",
|
||||
data={"csrf_token": token, "email": "a@example.test", "heslo": "x"},
|
||||
headers={"X-Forwarded-For": "198.51.100.2"},
|
||||
)
|
||||
assert other.status_code == 401
|
||||
Reference in new issue
Block a user