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.

6.3 KB
Raw Blame
"""
Tests for utilities/request_context.py.

The contextvar-backed username/session_id store is what lets legacy
service code (token_counter, news.api, metrics.database, etc.) discover
the current user under FastAPI without a Flask request context. These
tests pin its semantics so a regression there shows up loudly instead of
manifesting as a sea of 500s on metrics/news routes.
"""

import asyncio
import contextvars
import threading
from unittest.mock import Mock

import pytest

from local_deep_research.utilities.request_context import (
    get_current_session_id,
    get_current_username,
    request_user,
    reset_request_user,
    set_request_user,
)
from local_deep_research.utilities import (
    request_context as request_context_module,
)


class TestSetReset:
    def test_default_is_none(self):
        # Run in a fresh thread so we are not seeing leakage from another test.
        out: dict = {}

        def _check():
            out["u"] = get_current_username()
            out["s"] = get_current_session_id()

        t = threading.Thread(target=_check)
        t.start()
        t.join()
        assert out["u"] is None
        assert out["s"] is None

    def test_set_then_get(self):
        tokens = set_request_user("alice", "sess-1")
        try:
            assert get_current_username() == "alice"
            assert get_current_session_id() == "sess-1"
        finally:
            reset_request_user(tokens)

    def test_reset_restores_previous(self):
        tokens = set_request_user("outer", "outer-s")
        try:
            inner = set_request_user("inner", "inner-s")
            try:
                assert get_current_username() == "inner"
                assert get_current_session_id() == "inner-s"
            finally:
                reset_request_user(inner)
            assert get_current_username() == "outer"
            assert get_current_session_id() == "outer-s"
        finally:
            reset_request_user(tokens)

    def test_reset_tokens_from_another_context_does_not_escape(self):
        """A token belongs to the Context in which it was created.

        Middleware cleanup can run after an async context hand-off; resetting
        there raises a real ``ValueError`` from ``ContextVar``.  The defensive
        reset helper must contain it without corrupting either context.
        """
        before = (get_current_username(), get_current_session_id())
        tokens = set_request_user("owner", "owner-session")
        foreign = contextvars.Context()

        def _reset_from_foreign_context():
            own_tokens = set_request_user("foreign", "foreign-session")
            try:
                # Both handles were made in the owning context, so both real
                # ContextVar.reset calls raise ValueError and must be swallowed.
                reset_request_user(tokens)
                assert get_current_username() == "foreign"
                assert get_current_session_id() == "foreign-session"
            finally:
                reset_request_user(own_tokens)

        try:
            foreign.run(_reset_from_foreign_context)
            assert get_current_username() == "owner"
            assert get_current_session_id() == "owner-session"
        finally:
            reset_request_user(tokens)

        assert (get_current_username(), get_current_session_id()) == before

    def test_session_reset_is_attempted_when_username_reset_fails(
        self, monkeypatch
    ):
        """The two reset operations are deliberately independent."""
        username_var = Mock()
        username_var.reset.side_effect = LookupError("stale username token")
        session_var = Mock()
        monkeypatch.setattr(
            request_context_module, "_username_var", username_var
        )
        monkeypatch.setattr(
            request_context_module, "_session_id_var", session_var
        )

        reset_request_user(("username-token", "session-token"))

        username_var.reset.assert_called_once_with("username-token")
        session_var.reset.assert_called_once_with("session-token")


class TestContextManager:
    def test_request_user_sets_inside_block(self):
        with request_user("bob", "sess-bob"):
            assert get_current_username() == "bob"
            assert get_current_session_id() == "sess-bob"
        # Cleared after block
        assert get_current_username() is None or get_current_username() != "bob"

    def test_request_user_clears_on_exception(self):
        with pytest.raises(ValueError):
            with request_user("carol", "sess-c"):
                assert get_current_username() == "carol"
                raise ValueError("boom")
        # Should NOT leak "carol" into the surrounding scope
        assert get_current_username() != "carol"


class TestThreadIsolation:
    def test_threads_have_separate_username(self):
        """Setting in one thread must not leak to another."""
        observed: dict = {}
        ready = threading.Event()
        proceed = threading.Event()

        def _worker():
            with request_user("worker-user"):
                ready.set()
                proceed.wait(timeout=1)
                observed["worker"] = get_current_username()

        t = threading.Thread(target=_worker)
        t.start()
        ready.wait(timeout=1)

        # Main thread should NOT see "worker-user"
        assert get_current_username() != "worker-user"

        proceed.set()
        t.join(timeout=2)
        assert observed["worker"] == "worker-user"


class TestAsyncIsolation:
    @pytest.mark.asyncio
    async def test_async_tasks_have_separate_username(self):
        """contextvars copy-on-task-spawn — sibling tasks must not interfere."""

        results: dict = {}

        async def _task(name: str):
            with request_user(name):
                # Yield to event loop so other tasks run interleaved
                await asyncio.sleep(0.01)
                results[name] = get_current_username()

        await asyncio.gather(_task("alpha"), _task("beta"), _task("gamma"))

        assert results == {"alpha": "alpha", "beta": "beta", "gamma": "gamma"}


class TestFlaskFallback:
    def test_returns_none_when_no_flask_context(self):
        # Make sure the contextvar is empty.
        tokens = set_request_user(None, None)
        try:
            assert get_current_username() is None
            assert get_current_session_id() is None
        finally:
            reset_request_user(tokens)