from datetime import timedelta import pytest from fastapi.testclient import TestClient from app.config import get_settings from app.models import LoginAttempt, UserSession, utcnow from tests.conftest import login WRONG = "wrong password!!" # --- Origin ------------------------------------------------------------------- def test_foreign_origin_is_rejected_on_login(client, make_user): make_user("anna") response = client.post( "/api/auth/login", json={"username": "anna", "password": "correct horse battery"}, headers={"Origin": "https://evil.example"}, ) assert response.status_code == 403 assert "lc_session" not in client.cookies def test_null_origin_is_rejected(client, make_user): make_user("anna") response = client.post( "/api/auth/login", json={"username": "anna", "password": "correct horse battery"}, headers={"Origin": "null"}, ) assert response.status_code == 403 def test_allowed_origin_is_accepted(client, make_user): make_user("anna") response = client.post( "/api/auth/login", json={"username": "anna", "password": "correct horse battery"}, headers={"Origin": "https://testserver"}, ) assert response.status_code == 200 def test_get_requests_ignore_origin(client): response = client.get("/api/health", headers={"Origin": "https://evil.example"}) assert response.status_code == 200 # --- CSRF --------------------------------------------------------------------- def test_state_change_without_csrf_token_is_rejected(client, make_user): make_user("anna") login(client, "anna") del client.headers["X-CSRF-Token"] assert client.post("/api/auth/logout").status_code == 403 assert client.get("/api/auth/me").status_code == 200 # still logged in def test_state_change_with_wrong_csrf_token_is_rejected(client, make_user): make_user("anna") login(client, "anna") response = client.post("/api/auth/logout", headers={"X-CSRF-Token": "forged"}) assert response.status_code == 403 def test_csrf_token_of_other_session_is_rejected(client, make_user): make_user("anna") make_user("bert") with TestClient(client.app, base_url="https://testserver") as bert: login(bert, "bert") login(client, "anna") response = client.post( "/api/auth/logout", headers={"X-CSRF-Token": bert.cookies["lc_csrf"]} ) assert response.status_code == 403 # --- Lockout ------------------------------------------------------------------ def test_user_is_locked_after_repeated_failures(client, make_user): make_user("anna") for _ in range(5): assert login(client, "anna", WRONG).status_code == 401 blocked = login(client, "anna") # even the correct password assert blocked.status_code == 429 assert blocked.headers["Retry-After"] == "900" def test_lockout_applies_to_unknown_users_too(client): for _ in range(5): login(client, "ghost", WRONG) assert login(client, "ghost", WRONG).status_code == 429 def test_lockout_is_case_insensitive(client, make_user): make_user("anna") for name in ["anna", "ANNA", "Anna", "aNNa", "annA"]: login(client, name, WRONG) assert login(client, "anna").status_code == 429 def test_lockout_expires_after_window(client, make_user, db): make_user("anna") for _ in range(5): login(client, "anna", WRONG) db.query(LoginAttempt).update({LoginAttempt.attempted_at: utcnow() - timedelta(minutes=16)}) db.commit() assert login(client, "anna").status_code == 200 def test_blocked_attempts_do_not_extend_lockout(client, make_user, db): make_user("anna") for _ in range(5): login(client, "anna", WRONG) for _ in range(3): login(client, "anna", WRONG) assert db.query(LoginAttempt).count() == 5 def test_successful_login_resets_counter(client, make_user): make_user("anna") for _ in range(4): login(client, "anna", WRONG) assert login(client, "anna").status_code == 200 client.post("/api/auth/logout") for _ in range(4): assert login(client, "anna", WRONG).status_code == 401 assert login(client, "anna").status_code == 200 def test_ip_lockout_disabled_by_default(client, make_user): make_user("anna") for i in range(25): login(client, f"user{i}", WRONG) assert login(client, "anna").status_code == 200 @pytest.fixture def ip_lockout(monkeypatch, app_env): monkeypatch.setenv("LOGIN_IP_LOCKOUT", "true") get_settings.cache_clear() def test_ip_lockout_when_enabled(ip_lockout, client, make_user): make_user("anna") for i in range(20): login(client, f"user{i}", WRONG) assert login(client, "anna").status_code == 429 # --- Cleanup ------------------------------------------------------------------ def test_cleanup_removes_expired_data(client, make_user, db): from app.main import _cleanup make_user("anna") login(client, "anna") login(client, "anna", WRONG) db.query(UserSession).update({UserSession.expires_at: utcnow() - timedelta(seconds=1)}) db.query(LoginAttempt).update({LoginAttempt.attempted_at: utcnow() - timedelta(days=8)}) db.commit() _cleanup() assert db.query(UserSession).count() == 0 assert db.query(LoginAttempt).count() == 0