Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

7.4 KB
Raw Blame
"""Tests for per-user account lockout."""

from datetime import datetime, timedelta, timezone
from unittest.mock import patch

from local_deep_research.security.account_lockout import AccountLockoutManager


class TestAccountLockout:
    """Unit tests for AccountLockoutManager."""

    def _make_manager(self, threshold=3, lockout_minutes=15):
        return AccountLockoutManager(
            threshold=threshold, lockout_minutes=lockout_minutes
        )

    def test_not_locked_initially(self):
        mgr = self._make_manager()
        assert mgr.is_locked("alice") is False

    def test_not_locked_below_threshold(self):
        mgr = self._make_manager(threshold=3)
        mgr.record_failure("alice")
        mgr.record_failure("alice")
        assert mgr.is_locked("alice") is False

    def test_locked_at_threshold(self):
        mgr = self._make_manager(threshold=3)
        for _ in range(3):
            mgr.record_failure("alice")
        assert mgr.is_locked("alice") is True

    def test_success_resets_lockout(self):
        mgr = self._make_manager(threshold=3)
        for _ in range(3):
            mgr.record_failure("alice")
        assert mgr.is_locked("alice") is True
        mgr.record_success("alice")
        assert mgr.is_locked("alice") is False

    def test_counter_resets_after_success(self):
        mgr = self._make_manager(threshold=10)
        for _ in range(9):
            mgr.record_failure("alice")
        mgr.record_success("alice")
        # After reset, 9 more failures should not lock (threshold is 10)
        for _ in range(9):
            mgr.record_failure("alice")
        assert mgr.is_locked("alice") is False

    def test_independent_users(self):
        mgr = self._make_manager(threshold=3)
        for _ in range(3):
            mgr.record_failure("alice")
        assert mgr.is_locked("alice") is True
        assert mgr.is_locked("bob") is False

    def test_success_on_nonexistent_user_is_noop(self):
        mgr = self._make_manager()
        # Should not raise
        mgr.record_success("nonexistent")
        assert mgr.is_locked("nonexistent") is False

    def test_lockout_expires_after_duration(self):
        mgr = self._make_manager(threshold=3, lockout_minutes=15)
        now = datetime(2026, 1, 1, 12, 0, 0, tzinfo=timezone.utc)

        with patch(
            "local_deep_research.security.account_lockout.datetime"
        ) as mock_dt:
            mock_dt.now.return_value = now
            mock_dt.side_effect = lambda *a, **kw: datetime(*a, **kw)
            for _ in range(3):
                mgr.record_failure("alice")

        # Still locked 14 minutes later
        with patch(
            "local_deep_research.security.account_lockout.datetime"
        ) as mock_dt:
            mock_dt.now.return_value = now + timedelta(minutes=14)
            assert mgr.is_locked("alice") is True

        # Unlocked at exactly 15 minutes
        with patch(
            "local_deep_research.security.account_lockout.datetime"
        ) as mock_dt:
            mock_dt.now.return_value = now + timedelta(minutes=15)
            assert mgr.is_locked("alice") is False

    def test_a_single_failure_past_the_cap_evicts_the_oldest_one(self):
        """Single-failure counters are the only ones a spray of throwaway
        usernames creates for one request each; they are capped, oldest
        out first."""
        mgr = self._make_manager(threshold=5)
        mgr._MAX_SINGLE_FAILURES = 5
        for i in range(6):
            mgr.record_failure(f"u{i}")

        assert list(mgr._state) == [f"u{i}" for i in range(1, 6)]

    def test_a_counter_with_two_failures_is_never_evicted(self):
        mgr = self._make_manager(threshold=5)
        mgr._MAX_SINGLE_FAILURES = 5
        for _ in range(2):
            mgr.record_failure("victim")
        for i in range(100):
            mgr.record_failure(f"spray{i}")

        assert mgr._state["victim"]["count"] == 2
        assert len(mgr._state) == 6

    def test_active_lockouts_are_never_evicted(self):
        """There is no last-resort clear: a table full of lockouts keeps
        every one of them, and a new username is still tracked."""
        mgr = self._make_manager(threshold=3)
        mgr._MAX_SINGLE_FAILURES = 5
        for i in range(50):
            for _ in range(3):
                mgr.record_failure(f"locked{i}")
        for i in range(100):
            mgr.record_failure(f"spray{i}")
        for _ in range(3):
            mgr.record_failure("late")

        assert all(mgr.is_locked(f"locked{i}") for i in range(50))
        assert mgr.is_locked("late") is True

    def test_a_one_failure_lockout_is_not_evicted_as_a_single(self):
        """With a threshold of one, the first failure is already a lockout,
        and lockouts are never evicted."""
        mgr = self._make_manager(threshold=1)
        mgr._MAX_SINGLE_FAILURES = 5
        mgr.record_failure("victim")
        for i in range(20):
            mgr.record_failure(f"spray{i}")

        assert mgr.is_locked("victim") is True

    def test_a_login_clears_the_single_failure_record_too(self):
        mgr = self._make_manager(threshold=5)
        mgr._MAX_SINGLE_FAILURES = 5
        mgr.record_failure("alice")
        mgr.record_success("alice")
        for i in range(20):
            mgr.record_failure(f"spray{i}")

        assert len(mgr._state) == 5

    def test_an_old_counter_behind_a_recent_one_still_expires(self):
        """A failure moves its counter to the recent end of the table, so
        the expired counters are the ones at its front."""
        mgr = self._make_manager(threshold=5, lockout_minutes=15)
        now = datetime(2026, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
        with patch(
            "local_deep_research.security.account_lockout.datetime"
        ) as mock_dt:
            for minute, username in ((0, "alice"), (1, "bob"), (10, "alice")):
                mock_dt.now.return_value = now + timedelta(minutes=minute)
                mgr.record_failure(username)
            mock_dt.now.return_value = now + timedelta(minutes=16)
            mgr.record_failure("bob")

        assert mgr._state["bob"]["count"] == 1
        assert mgr._state["alice"]["count"] == 2

    def test_a_counter_forgets_failures_older_than_the_window(self):
        """A counter lasts one lockout window from its last failure, the
        same as a lockout, so failures an attacker banked earlier stop
        counting."""
        mgr = self._make_manager(threshold=3, lockout_minutes=15)
        now = datetime(2026, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
        with patch(
            "local_deep_research.security.account_lockout.datetime"
        ) as mock_dt:
            mock_dt.now.return_value = now
            for _ in range(2):
                mgr.record_failure("alice")
            mock_dt.now.return_value = now + timedelta(minutes=15)
            mgr.record_failure("alice")
            assert mgr.is_locked("alice") is False
            assert mgr._state["alice"]["count"] == 1

    def test_failures_inside_the_window_keep_counting(self):
        mgr = self._make_manager(threshold=3, lockout_minutes=15)
        now = datetime(2026, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
        with patch(
            "local_deep_research.security.account_lockout.datetime"
        ) as mock_dt:
            for minute in (0, 14, 28):
                mock_dt.now.return_value = now + timedelta(minutes=minute)
                mgr.record_failure("alice")
            assert mgr.is_locked("alice") is True