~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""``POST /news/api/subscriptions/{id}/run`` and the two update paths.
Ported from ``tests/news/test_flask_api_coverage_gaps.py`` and
``tests/news/test_flask_api_deep_coverage.py``, deleted with
``news/flask_api.py``. Successor: ``web/routers/news_flask_api.py``.
``run_subscription_now`` and ``_update_subscription_folder_sync`` had NO
Python test on the branch — the only mention of either path anywhere in
``tests/`` is a frozen auth-census row and a 404-only probe. Between them
they carry the manual-run schedule bookkeeping and the active/paused
translation the scheduler keys on, and both are properties that a passing
200 hides completely.
WHY DIRECT CALLS RATHER THAN HTTP
---------------------------------
Both handlers read ORM rows out of the per-user encrypted database and
their interesting behaviour is what they do to those rows, not what they
return. Driving them over HTTP would require seeding a real SQLCipher file
and would still leave the assertions unable to see the row. Same pattern,
and same premise guard, as
``tests/news/test_news_api_contract_restored.py``. The ``field_mapping``
half at the bottom does go over HTTP, since there the observable is the
service call.
"""
import asyncio
from contextlib import contextmanager
from datetime import datetime, timezone
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.news.constants import (
COMPACT_SUBSCRIPTION_INPUT_FIELDS,
COMPACT_SUBSCRIPTION_READONLY_ECHO_FIELDS,
COMPACT_SUBSCRIPTION_RESPONSE_FIELDS,
)
from local_deep_research.web.routers import news_flask_api
SESSION_CONTEXT = "local_deep_research.database.session_context"
SUBSCRIPTION_RUNNER = "local_deep_research.news.subscription_runner"
NEWS_CORE_UTILS = "local_deep_research.news.core.utils"
SETTINGS_MANAGER = "local_deep_research.settings.manager"
API = "local_deep_research.web.routers.news_flask_api.api"
USERNAME = "alice"
SUB_ID = "sub-1"
NEXT_REFRESH = datetime(2026, 8, 25, tzinfo=timezone.utc)
# ``PUT /news/api/subscription/subscriptions/{id}`` -- the "compact" update
# route -- used to run a blind ``setattr`` loop over the whole request body,
# so it accepted every field the *other* update route
# (``PUT /subscriptions/{id}``) accepts (query_or_topic, name,
# model_provider, search_iterations, ...), protected columns (id, user_id,
# created_at), and custom_endpoint -- which it merely SSRF-checked rather
# than reject. It is now restricted to an explicit allowlist (folder,
# folder_id, name, notes, refresh_interval_minutes, is_active, status) via
# ``normalize_compact_subscription_update``. Read-only fields echoed in the
# route's own PUT response (id, next_refresh, last_refresh) are accepted
# but never written, so a client can PUT that response back unchanged.
# Other unsupported fields are rejected before database access; writable
# fields retain type/range checks.
COMPACT_UPDATE_REJECTED_PAYLOADS = [
*[
pytest.param(
{field: value},
f"Unsupported subscription update fields: {field}",
id=f"unsupported-{field}",
)
for field, value in (
("query_or_topic", "new topic"),
("model_provider", "OPENAI"),
("search_iterations", 5),
("questions_per_iteration", 5),
# Previously reached storage via the blind setattr loop and was
# only SSRF-checked (#5603), not rejected outright.
("custom_endpoint", "http://169.254.169.254/latest/meta-data/"),
("user_id", "mallory"),
("created_at", "1999-01-01"),
)
],
pytest.param(
{"folder_id": "f1", "user_id": "mallory"},
"Unsupported subscription update fields: user_id",
id="mixed-allowed-and-unsupported",
),
pytest.param(
{"status": "expired"},
"status must be 'active' or 'paused'",
id="status-invalid-value",
),
pytest.param(
{"status": 1},
"status must be 'active' or 'paused'",
id="status-wrong-type",
),
*[
pytest.param(
{"refresh_interval_minutes": value},
"refresh_interval_minutes must be an integer between 1 and 10080",
id=f"refresh-{label}",
)
for label, value in (
("zero", 0),
("negative", -1),
("over-limit", 10081),
("string", "60"),
("bool", True),
("null", None),
)
],
*[
pytest.param(
{field: 123},
f"{field} must be a string or null",
id=f"{field}-wrong-type",
)
for field in ("folder", "folder_id", "name", "notes")
],
pytest.param(
{"name": "x" * 256},
"name exceeds maximum length of 255 characters",
id="name-too-long",
),
]
# ===========================================================================
# Premise guard
# ===========================================================================
@pytest.mark.parametrize(
"path,method,endpoint",
[
(
"/news/api/subscriptions/{subscription_id}/run",
"POST",
"run_subscription_now",
),
(
"/news/api/subscription/subscriptions/{subscription_id}",
"PUT",
"update_subscription_folder",
),
],
)
def test_the_handlers_under_test_are_the_ones_mounted(path, method, endpoint):
matches = [
route
for route in news_flask_api.router.routes
if route.path == path and method in route.methods
]
assert len(matches) == 1, f"{method} {path} is not mounted exactly once"
assert matches[0].endpoint.__name__ == endpoint
# ===========================================================================
# POST /news/api/subscriptions/{id}/run
# ===========================================================================
def _subscription_row(sub_id=SUB_ID, next_refresh=NEXT_REFRESH):
sub = MagicMock(name=f"subscription-{sub_id}")
sub.id = sub_id
sub.name = "Fusion digest"
sub.query_or_topic = "fusion energy"
sub.refresh_interval_minutes = 60
sub.next_refresh = next_refresh
sub.model_provider = "ollama"
sub.model = "llama3"
sub.search_strategy = "source-based"
sub.search_engine = "searxng"
sub.custom_endpoint = None
return sub
def _run(rows, start_result, request=None):
"""Call the real ``run_subscription_now``.
``rows`` is consumed one per ``.first()`` — the handler reads the
subscription once before the run and once after, and the second read is
what the compare-and-set compares against.
Returns ``(response, session, advance_mock, request_data_mock)``.
"""
session = MagicMock(name="user_db_session")
remaining = list(rows)
session.query.return_value.filter.return_value.first.side_effect = lambda: (
remaining.pop(0) if remaining else None
)
@contextmanager
def fake_db_session(*args, **kwargs):
yield session
with (
patch(
f"{SESSION_CONTEXT}.get_user_db_session",
side_effect=fake_db_session,
),
patch(
f"{SUBSCRIPTION_RUNNER}.build_subscription_request_data",
side_effect=lambda **kwargs: dict(kwargs),
) as build,
patch(f"{SUBSCRIPTION_RUNNER}.advance_refresh_schedule") as advance,
patch(
f"{NEWS_CORE_UTILS}.get_local_date_string",
return_value="2026-08-25",
),
patch(f"{SETTINGS_MANAGER}.SettingsManager", return_value=MagicMock()),
patch.object(
news_flask_api,
"_start_research_in_process",
return_value=start_result,
),
):
response = news_flask_api.run_subscription_now(
SUB_ID, request or MagicMock(), username=USERNAME
)
return response, session, advance, build
def _body(response):
"""The JSON payload of a handler return value (dict or JSONResponse)."""
import json
if hasattr(response, "body"):
return json.loads(bytes(response.body).decode())
return response
class TestRunSubscriptionNow:
def test_an_absent_subscription_is_a_404(self):
response, _, advance, _ = _run([None], {"status": "success"})
assert response.status_code == 404
assert _body(response) == {"error": "Subscription not found"}
assert not advance.called
def test_a_successful_run_reports_the_research_id_and_progress_url(self):
response, _, _, _ = _run(
[_subscription_row(), _subscription_row()],
{"status": "success", "research_id": "res-9"},
)
assert _body(response) == {
"status": "success",
"message": "Research started",
"research_id": "res-9",
"url": "/progress/res-9",
}
def test_a_queued_run_counts_as_success(self):
"""``_start_research_sync`` reports "queued" when the concurrency
limiter defers the run; treating that as a failure would tell the
user their manual run did not happen when it did."""
response, _, advance, _ = _run(
[_subscription_row(), _subscription_row()],
{"status": "queued", "research_id": "res-10"},
)
assert _body(response)["status"] == "success"
assert advance.call_count == 1
def test_a_successful_run_advances_the_schedule_and_commits(self):
"""Without the advance, a subscription that was also overdue is
re-run by the scheduler while this manual run is still in flight."""
response, session, advance, _ = _run(
[_subscription_row(), _subscription_row()],
{"status": "success", "research_id": "res-9"},
)
assert advance.call_count == 1
assert session.commit.called
def test_the_advance_is_skipped_when_the_run_already_reset_it(self):
"""Compare-and-set: a fast-failing run resets ``next_refresh`` on the
worker thread. Advancing anyway clobbers that reset and re-hides the
failed subscription for a full interval."""
reset = _subscription_row(
next_refresh=datetime(2020, 1, 1, tzinfo=timezone.utc)
)
response, _, advance, _ = _run(
[_subscription_row(), reset],
{"status": "success", "research_id": "res-9"},
)
assert advance.call_count == 0, (
"advance_refresh_schedule clobbered a next_refresh the run had "
"already reset"
)
assert _body(response)["status"] == "success"
def test_a_subscription_deleted_mid_run_is_not_advanced(self):
response, _, advance, _ = _run(
[_subscription_row(), None],
{"status": "success", "research_id": "res-9"},
)
assert advance.call_count == 0
assert _body(response)["status"] == "success"
def test_the_saved_model_config_is_read_off_the_orm_row(self):
"""The manual run must honour the subscription's saved model, not
the trimmed ``api.get_subscriptions()`` dict (which drops
model_provider/model/search_strategy/search_engine) and not the
user's current defaults."""
_, _, _, build = _run(
[_subscription_row(), _subscription_row()],
{"status": "success", "research_id": "res-9"},
)
kwargs = build.call_args.kwargs
assert kwargs["model_provider"] == "ollama"
assert kwargs["model"] == "llama3"
assert kwargs["search_strategy"] == "source-based"
assert kwargs["search_engine"] == "searxng"
assert kwargs["query_template"] == "fusion energy"
assert kwargs["subscription_id"] == SUB_ID
assert kwargs["triggered_by"] == "manual"
def test_a_failure_reports_message_in_preference_to_error(self):
"""``start_research`` populates ``message`` on failure; preferring
``error`` yields the fixed fallback string for every real failure."""
response, _, advance, _ = _run(
[_subscription_row()],
{
"status": "error",
"message": "Ollama is not reachable",
"error": "Failed to start research",
},
)
assert _body(response) == {"error": "Ollama is not reachable"}
assert not advance.called
def test_a_failure_falls_back_to_error_then_to_a_fixed_string(self):
no_message, _, _, _ = _run(
[_subscription_row()],
{"status": "error", "error": "capacity reached"},
)
assert _body(no_message) == {"error": "capacity reached"}
neither, _, _, _ = _run([_subscription_row()], {"status": "error"})
assert _body(neither) == {"error": "Failed to start research"}
def test_the_real_status_code_is_propagated_not_flattened_to_500(self):
"""``_start_research_in_process`` preserves the in-process handler's
400/409/429 under ``_http_status``. Collapsing them to 500 turns a
duplicate-run rejection into "the server is broken"."""
for http_status in (400, 409, 429):
response, _, _, _ = _run(
[_subscription_row()],
{
"status": "error",
"message": "already running",
"_http_status": http_status,
},
)
assert response.status_code == http_status, (
f"_http_status {http_status} was flattened to "
f"{response.status_code}"
)
def test_a_failure_with_no_http_status_is_a_500(self):
response, _, _, _ = _run(
[_subscription_row()], {"status": "error", "message": "boom"}
)
assert response.status_code == 500
def test_an_unexpected_exception_is_a_scrubbed_500(self):
session = MagicMock()
session.query.side_effect = RuntimeError(
"no such table: news_subscriptions in /db/alice.db"
)
@contextmanager
def fake_db_session(*args, **kwargs):
yield session
with patch(
f"{SESSION_CONTEXT}.get_user_db_session",
side_effect=fake_db_session,
):
response = news_flask_api.run_subscription_now(
SUB_ID, MagicMock(), username=USERNAME
)
assert response.status_code == 500
assert _body(response) == {
"error": "An error occurred while running subscription"
}
assert "alice.db" not in str(_body(response))
def test_start_research_in_process_forwards_the_session_id():
"""``_start_research_sync`` needs the session id to reach the caller's
encrypted database. Dropping it does not raise — the run just fails
later with "session expired", far from the cause."""
request = MagicMock()
request.base_url = "http://testserver/"
request.session = {"session_id": "sess-abc"}
with patch(
"local_deep_research.web.routers.research._start_research_sync",
return_value={"status": "success", "research_id": "r1"},
) as spy:
news_flask_api._start_research_in_process(
request, {"query": "q"}, USERNAME
)
args = spy.call_args.args
assert args[1] == USERNAME
assert args[3] == "sess-abc", (
f"session_id was not forwarded to _start_research_sync: {args!r}"
)
# ===========================================================================
# PUT /news/api/subscription/subscriptions/{id} -- the is_active translation
# ===========================================================================
def _folder_update(data, sub=None):
"""Run ``_update_subscription_folder_sync`` against one ORM row."""
session = MagicMock(name="user_db_session")
session.query.return_value.filter_by.return_value.first.return_value = sub
@contextmanager
def fake_db_session(*args, **kwargs):
yield session
with patch(
"local_deep_research.web.routers.news_flask_api.get_user_db_session",
side_effect=fake_db_session,
):
result = news_flask_api._update_subscription_folder_sync(
data, SUB_ID, USERNAME
)
return result, session
class _JsonRequest:
def __init__(self, data):
self.data = data
async def json(self):
return self.data
def _sub_row(status="active"):
sub = MagicMock(name="subscription")
sub.id = SUB_ID
sub.name = "Fusion digest"
sub.status = status
sub.folder_id = None
sub.refresh_interval_minutes = 60
sub.next_refresh = None
sub.last_refresh = None
return sub
class TestSubscriptionFolderUpdate:
"""``status`` is the source of truth the scheduler keys on; ``is_active``
is the legacy column. A body of ``{"is_active": false}`` that flipped
only the legacy column would leave the subscription running while the UI
showed it paused — and the response is a 200 either way."""
def test_is_active_false_pauses_the_subscription(self):
sub = _sub_row(status="active")
result, session = _folder_update({"is_active": False}, sub)
assert sub.status == "paused"
assert result["status"] == "paused"
assert result["is_active"] is False
assert session.commit.called
def test_is_active_true_activates_the_subscription(self):
sub = _sub_row(status="paused")
result, _ = _folder_update({"is_active": True}, sub)
assert sub.status == "active"
assert result["status"] == "active"
assert result["is_active"] is True
@pytest.mark.parametrize(
"is_active",
["false", "true", 0, 1, None, [], {}],
ids=[
"string-false",
"string-true",
"zero",
"one",
"null",
"list",
"object",
],
)
def test_non_boolean_is_active_is_rejected_before_database_access(
self, is_active
):
result, session = _folder_update({"is_active": is_active})
assert result.status_code == 400
assert _body(result) == {"error": "is_active must be a boolean"}
session.query.assert_not_called()
@pytest.mark.parametrize("data,message", COMPACT_UPDATE_REJECTED_PAYLOADS)
def test_unsupported_or_invalid_fields_are_rejected_before_database_access(
self, data, message
):
result, session = _folder_update(data)
assert result.status_code == 400
assert _body(result) == {"error": message}
session.query.assert_not_called()
@pytest.mark.parametrize("data,message", COMPACT_UPDATE_REJECTED_PAYLOADS)
def test_async_route_rejects_before_endpoint_resolution_or_offload(
self, data, message
):
# ``_reject_custom_endpoint_async`` is no longer wired into this
# route at all -- ``custom_endpoint`` is now rejected outright as an
# unsupported field, so there is nothing left to SSRF-check here.
with patch.object(news_flask_api, "run_db_sync") as offload:
result = asyncio.run(
news_flask_api.update_subscription_folder(
request=_JsonRequest(data),
subscription_id=SUB_ID,
username=USERNAME,
)
)
assert result.status_code == 400
assert _body(result) == {"error": message}
offload.assert_not_called()
@pytest.mark.parametrize(
"data",
[
# search_iterations / questions_per_iteration are not part of
# the compact allowlist (they stay on the full
# ``PUT /subscriptions/{id}`` route) -- only
# refresh_interval_minutes is exercised here.
pytest.param(
{"refresh_interval_minutes": 1},
id="minimum",
),
pytest.param(
{"refresh_interval_minutes": 10080},
id="maximum",
),
],
)
def test_numeric_boundaries_are_persisted_exactly(self, data):
sub = _sub_row()
result, session = _folder_update(data, sub)
for field, value in data.items():
assert getattr(sub, field) == value
assert (
result["refresh_interval_minutes"]
== data["refresh_interval_minutes"]
)
session.commit.assert_called_once_with()
def test_an_explicit_status_wins(self):
sub = _sub_row(status="active")
_folder_update({"status": "paused"}, sub)
assert sub.status == "paused"
def test_a_plain_field_update_returns_200_not_500(self):
"""``NewsSubscription`` has no ``to_dict()`` — the response dict is
hand-rolled, so it is one renamed attribute away from an
AttributeError 500."""
sub = _sub_row()
result, _ = _folder_update({"folder_id": "folder-9"}, sub)
assert sub.folder_id == "folder-9"
assert result["id"] == SUB_ID
assert result["name"] == "Fusion digest"
assert result["folder_id"] == "folder-9"
assert result["is_active"] is True
def test_response_key_set_matches_the_hand_kept_constant(self):
"""``COMPACT_SUBSCRIPTION_RESPONSE_FIELDS`` (news/constants.py) is
hand-kept in sync with the response dict literal in
``_update_subscription_folder_sync`` -- ``NewsSubscription`` has no
``to_dict()`` to introspect. Pin the two together here so a field
added to one and not the other is caught instead of silently
drifting (news/constants.py:28-36)."""
sub = _sub_row()
result, _ = _folder_update({"folder_id": "folder-9"}, sub)
assert set(result) == COMPACT_SUBSCRIPTION_RESPONSE_FIELDS
def test_input_fields_are_disjoint_from_the_readonly_echo_set(self):
"""A field cannot be simultaneously writable and merely-tolerated:
``COMPACT_SUBSCRIPTION_READONLY_ECHO_FIELDS`` is derived as
``COMPACT_SUBSCRIPTION_RESPONSE_FIELDS -
COMPACT_SUBSCRIPTION_INPUT_FIELDS`` in news/constants.py, which
guarantees this by construction -- pinned here so a future edit
that hardcodes the echo set instead of deriving it cannot silently
reintroduce an overlap."""
assert COMPACT_SUBSCRIPTION_INPUT_FIELDS.isdisjoint(
COMPACT_SUBSCRIPTION_READONLY_ECHO_FIELDS
)
@pytest.mark.parametrize(
"echoed",
[
{"id": "hijacked"},
{"next_refresh": "1999-01-01T00:00:00+00:00"},
{"last_refresh": "1999-01-01T00:00:00+00:00"},
{
"id": "hijacked",
"next_refresh": "1999-01-01T00:00:00+00:00",
"last_refresh": "1999-01-01T00:00:00+00:00",
},
],
ids=["id", "next-refresh", "last-refresh", "all-echoes"],
)
def test_readonly_response_fields_are_ignored_without_blocking_updates(
self, echoed
):
"""``id``/``next_refresh``/``last_refresh`` are the route's own PUT
response fields it never writes -- unlike ``name``, which the full
``PUT /subscriptions/{id}`` route's ``field_mapping`` has always let
a caller set, so this route treats it as an ordinary writable
field, not a read-only echo (covered separately below)."""
sub = _sub_row()
original = {
"id": sub.id,
"next_refresh": sub.next_refresh,
"last_refresh": sub.last_refresh,
}
data = {**echoed, "folder_id": "folder-9"}
# Drive the async boundary and the real sync implementation together:
# neither may reject response echoes, and neither may persist them.
async def run_sync(function, payload, subscription_id, username):
assert function is news_flask_api._update_subscription_folder_sync
assert subscription_id == SUB_ID
assert username == USERNAME
result, session = _folder_update(payload, sub)
session.commit.assert_called_once_with()
return result
with patch.object(news_flask_api, "run_db_sync", side_effect=run_sync):
result = asyncio.run(
news_flask_api.update_subscription_folder(
request=_JsonRequest(data),
subscription_id=SUB_ID,
username=USERNAME,
)
)
assert sub.folder_id == "folder-9"
assert result["folder_id"] == "folder-9"
for field, value in original.items():
assert getattr(sub, field) == value
assert result[field] == value
assert data == {**echoed, "folder_id": "folder-9"}
def test_a_rename_through_the_compact_route_is_written(self):
"""``name`` is on the compact route's allowlist (unlike ``id``/
``next_refresh``/``last_refresh`` above): a body naming it along
with another writable field writes both, matching the full
``PUT /subscriptions/{id}`` route's ``field_mapping`` behaviour."""
sub = _sub_row()
async def run_sync(function, payload, subscription_id, username):
result, session = _folder_update(payload, sub)
session.commit.assert_called_once_with()
return result
with patch.object(news_flask_api, "run_db_sync", side_effect=run_sync):
result = asyncio.run(
news_flask_api.update_subscription_folder(
request=_JsonRequest({"name": "Renamed", "folder": "A"}),
subscription_id=SUB_ID,
username=USERNAME,
)
)
assert sub.name == "Renamed"
assert sub.folder == "A"
assert result["name"] == "Renamed"
def test_protected_columns_reject_the_whole_update_without_mutation(self):
"""Unsupported user_id/created_at reject the whole body, including
allowed folder_id and the otherwise tolerated read-only id echo."""
sub = _sub_row()
sub.user_id = "alice"
sub.created_at = "2020-01-01"
result, session = _folder_update(
{
"id": "hijacked",
"user_id": "mallory",
"created_at": "1999-01-01",
"folder_id": "f1",
},
sub,
)
assert result.status_code == 400
assert sub.id == SUB_ID
assert sub.user_id == "alice"
assert sub.created_at == "2020-01-01"
assert sub.folder_id is None
session.query.assert_not_called()
def test_an_absent_subscription_is_a_404(self):
result, _ = _folder_update({"folder_id": "f1"}, None)
assert result.status_code == 404
assert _body(result) == {"error": "Subscription not found"}
def test_a_new_interval_recomputes_next_refresh_from_last_refresh(self):
sub = _sub_row()
sub.last_refresh = datetime(2026, 8, 25, 12, 0, tzinfo=timezone.utc)
_folder_update({"refresh_interval_minutes": 30}, sub)
assert sub.next_refresh == datetime(
2026, 8, 25, 12, 30, tzinfo=timezone.utc
)
def test_a_never_run_subscription_schedules_from_now(self):
sub = _sub_row()
sub.last_refresh = None
before = datetime.now(timezone.utc)
_folder_update({"refresh_interval_minutes": 30}, sub)
delta = (sub.next_refresh - before).total_seconds()
assert 29 * 60 <= delta <= 31 * 60, sub.next_refresh