aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDennis Fink2026-11-13 19:22:33 +0100
committerDennis Fink2026-11-13 19:22:33 +0100
commit5c1eb15f00e20ece0a4ff8e7e1b4a5ab51e54232 (patch)
tree04b442019424db67aa8ca23c5e652d12136f9fe7 /tests
parentc421d269313d6623926954935a680fe4acb1dc86 (diff)
downloadwebmentions-ssg-5c1eb15f00e20ece0a4ff8e7e1b4a5ab51e54232.tar.gz
webmentions-ssg-5c1eb15f00e20ece0a4ff8e7e1b4a5ab51e54232.zip
refactor(core): simplify login password verification
Replace the module-level dummy user with a lazily cached password hash so unknown usernames still perform a password check without hashing during every import. Use one linear verification path for existing, missing, and empty passwords.
Diffstat (limited to '')
-rw-r--r--tests/test_auth.py23
-rw-r--r--tests/views/test_session.py46
2 files changed, 44 insertions, 25 deletions
diff --git a/tests/test_auth.py b/tests/test_auth.py
index 34df8ce..e026e45 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -7,7 +7,7 @@
import pytest
from flask import Flask
-from webmentions_ssg import db
+from webmentions_ssg import auth, db
from webmentions_ssg.auth import AUTH, is_safe_next_url, load_user, validate_password
from webmentions_ssg.models import User
@@ -22,6 +22,27 @@ def test_user_loader_is_registered(app: Flask) -> None:
assert AUTH._user_callback is load_user
+def test_dummy_password_hash_is_lazily_cached(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ generated_passwords: list[str] = []
+
+ def fake_generate_password_hash(password: str) -> str:
+ generated_passwords.append(password)
+ return "dummy-hash"
+
+ auth.dummy_password_hash.cache_clear()
+ try:
+ with monkeypatch.context() as patcher:
+ patcher.setattr(auth, "generate_password_hash", fake_generate_password_hash)
+ assert generated_passwords == []
+ assert auth.dummy_password_hash() == "dummy-hash"
+ assert auth.dummy_password_hash() == "dummy-hash"
+ assert len(generated_passwords) == 1
+ finally:
+ auth.dummy_password_hash.cache_clear()
+
+
def test_load_user_returns_matching_user(app: Flask) -> None:
with app.app_context():
user = User(username="account", password="secret")
diff --git a/tests/views/test_session.py b/tests/views/test_session.py
index 0c4de9f..d89f98e 100644
--- a/tests/views/test_session.py
+++ b/tests/views/test_session.py
@@ -140,51 +140,49 @@ def test_login_redirects_authenticated_user(app: Flask, client: FlaskClient) ->
@pytest.mark.parametrize("username", ["admin", "unknown-user"])
+@pytest.mark.parametrize("password", ["wrong-password", None])
def test_login_always_checks_password(
app: Flask,
client: FlaskClient,
views_module: ModuleType,
monkeypatch: pytest.MonkeyPatch,
username: str,
+ password: str | None,
) -> None:
with app.app_context():
- db.session.add(User(username="admin", password="test"))
+ user = User(username="admin", password="test")
+ db.session.add(user)
db.session.commit()
+ real_password_hash = user.password_hash
mock_login_form(
- views_module,
- monkeypatch,
- valid=True,
- username=username,
- password="wrong-password",
+ views_module, monkeypatch, valid=True, username=username, password=password
)
- original_check_password = User.check_password
- checked_users: list[User] = []
+ original_check_password_hash = views_module.check_password_hash
+ checked_passwords: list[tuple[str, str]] = []
- def spy_check_password(self: User, password: str) -> bool:
- checked_users.append(self)
- return original_check_password(self, password)
+ def spy_check_password_hash(password_hash: str, password: str) -> bool:
+ checked_passwords.append((password_hash, password))
+ return original_check_password_hash(password_hash, password)
- monkeypatch.setattr(User, "check_password", spy_check_password)
+ monkeypatch.setattr(views_module, "check_password_hash", spy_check_password_hash)
response = client.post("/login")
assert response.status_code == 302
assert response.headers["Location"].endswith("/login")
- assert len(checked_users) == 1
-
- if username == "admin":
- assert checked_users[0].username == "admin"
- else:
- assert checked_users[0] is views_module.DUMMY_USER
+ expected_hash = (
+ real_password_hash if username == "admin" else views_module.dummy_password_hash()
+ )
+ assert checked_passwords == [(expected_hash, password or "")]
with client.session_transaction() as session:
assert "_user_id" not in session
assert ("danger", "Invalid username or password") in session["_flashes"]
-def test_login_rejects_dummy_user_even_if_password_matches(
+def test_login_rejects_unknown_user_even_if_password_matches(
client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch
) -> None:
mock_login_form(
@@ -195,19 +193,19 @@ def test_login_rejects_dummy_user_even_if_password_matches(
password="anything",
)
- checked_users: list[User] = []
+ checked_passwords: list[tuple[str, str]] = []
- def mock_check_password(self: User, password: str) -> bool:
- checked_users.append(self)
+ def mock_check_password_hash(password_hash: str, password: str) -> bool:
+ checked_passwords.append((password_hash, password))
return True
- monkeypatch.setattr(User, "check_password", mock_check_password)
+ monkeypatch.setattr(views_module, "check_password_hash", mock_check_password_hash)
response = client.post("/login")
assert response.status_code == 302
assert response.headers["Location"].endswith("/login")
- assert checked_users == [views_module.DUMMY_USER]
+ assert checked_passwords == [(views_module.dummy_password_hash(), "anything")]
with client.session_transaction() as session:
assert "_user_id" not in session