aboutsummaryrefslogtreecommitdiff
path: root/tests/views/test_session.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/views/test_session.py')
-rw-r--r--tests/views/test_session.py46
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