diff options
| author | Dennis Fink | 2026-11-13 19:22:33 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 19:22:33 +0100 |
| commit | 5c1eb15f00e20ece0a4ff8e7e1b4a5ab51e54232 (patch) | |
| tree | 04b442019424db67aa8ca23c5e652d12136f9fe7 /tests/views | |
| parent | c421d269313d6623926954935a680fe4acb1dc86 (diff) | |
| download | webmentions-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 'tests/views')
| -rw-r--r-- | tests/views/test_session.py | 46 |
1 files changed, 22 insertions, 24 deletions
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 |
