OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
"""Tests for flows_service bulk_update_flows with Langflow backup creation."""
import sys
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
ROOT = Path(__file__).resolve().parent.parent.parent.parent
SRC = ROOT / "src"
if str(SRC) not in sys.path:
sys.path.insert(0, str(SRC))
from services.flows_service import FlowsService # noqa: E402
@pytest.mark.asyncio
async def test_arbitrary_provider_uses_openai_compatible_langflow_component():
service = FlowsService()
update = AsyncMock(return_value={"flow": "retrieval", "success": True})
with patch.object(service, "_update_provider_components", update):
result = await service.change_langflow_model_value(
"gemini",
llm_model="gemini-2.5-pro",
force_llm_update=True,
flow_configs=[{"name": "retrieval", "flow_id": "flow-1"}],
)
assert result["success"] is True
update.assert_awaited_once_with(
{"name": "retrieval", "flow_id": "flow-1"},
"openai",
embedding_model=None,
llm_model="gemini-2.5-pro",
force_embedding_update=False,
force_llm_update=True,
)
@pytest.mark.asyncio
async def test_bulk_update_flows_creates_backup_flow_in_langflow():
service = FlowsService()
mock_get_response = MagicMock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {
"id": "flow-retrieval-123",
"name": "Custom Retrieval Flow",
"locked": False,
"data": {"nodes": []},
}
mock_post_response = MagicMock()
mock_post_response.status_code = 201
mock_post_response.json.return_value = {
"id": "backup-flow-999",
"name": "Backup - Custom Retrieval Flow (2026-07-23 20:00)",
}
async def mock_langflow_request(method, url, json=None, **kwargs):
if method == "GET":
return mock_get_response
elif method == "POST":
# Verify the posted backup payload
assert "id" not in json
assert json["name"].startswith("Backup - Custom Retrieval Flow")
assert json["locked"] is False
return mock_post_response
return MagicMock(status_code=404)
with (
patch("services.flows_service.clients.langflow_request", side_effect=mock_langflow_request),
patch.object(
service, "_backup_flow", new_callable=AsyncMock, return_value="/tmp/backup.json"
),
patch.object(
service,
"_reset_langflow_flow_locked",
new_callable=AsyncMock,
return_value={"success": True},
),
):
results = await service.bulk_update_flows(["retrieval"], backup_custom=True)
assert len(results) == 1
res = results[0]
assert res["flow_type"] == "retrieval"
assert res["success"] is True
assert res["backup_flow_id"] == "backup-flow-999"
@pytest.mark.asyncio
async def test_bulk_update_flows_aborts_reset_on_backup_failure():
service = FlowsService()
mock_get_response = MagicMock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {
"id": "flow-retrieval-123",
"name": "Custom Retrieval Flow",
"locked": False,
"data": {"nodes": []},
}
mock_post_response = MagicMock()
mock_post_response.status_code = 500
async def mock_langflow_request(method, url, json=None, **kwargs):
if method == "GET":
return mock_get_response
elif method == "POST":
return mock_post_response
return MagicMock(status_code=404)
reset_mock = AsyncMock(return_value={"success": True})
with (
patch("services.flows_service.clients.langflow_request", side_effect=mock_langflow_request),
patch.object(service, "_backup_flow", new_callable=AsyncMock, return_value=None),
patch.object(service, "_reset_langflow_flow_locked", reset_mock),
):
results = await service.bulk_update_flows(["retrieval"], backup_custom=True)
assert len(results) == 1
res = results[0]
assert res["flow_type"] == "retrieval"
assert res["success"] is False
assert "Backup failed" in res["error"]
assert reset_mock.call_count == 0
@pytest.mark.asyncio
async def test_bulk_update_flows_saves_local_file_backup_for_default_flows():
service = FlowsService()
mock_get_response = MagicMock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {
"id": "flow-retrieval-123",
"name": "Default Retrieval Flow",
"locked": True,
"data": {"nodes": []},
}
backup_mock = AsyncMock(return_value="/tmp/backup_default.json")
reset_mock = AsyncMock(return_value={"success": True})
with (
patch("services.flows_service.clients.langflow_request", return_value=mock_get_response),
patch.object(service, "_backup_flow", backup_mock),
patch.object(service, "_reset_langflow_flow_locked", reset_mock),
):
results = await service.bulk_update_flows(["retrieval"], backup_custom=True)
assert len(results) == 1
assert results[0]["success"] is True
assert backup_mock.call_count == 1
assert reset_mock.call_count == 1
@pytest.mark.asyncio
async def test_bulk_update_flows_aborts_when_preflight_get_fails():
"""Verify issue (2a): pre-flight GET failure aborts update without attempting reset."""
service = FlowsService()
mock_get_response = MagicMock()
mock_get_response.status_code = 500
reset_mock = AsyncMock(return_value={"success": True})
with (
patch("services.flows_service.clients.langflow_request", return_value=mock_get_response),
patch.object(service, "_reset_langflow_flow_locked", reset_mock),
):
results = await service.bulk_update_flows(["retrieval"], backup_custom=True)
assert len(results) == 1
res = results[0]
assert res["flow_type"] == "retrieval"
assert res["success"] is False
assert "Pre-flight check failed" in res["error"]
assert reset_mock.call_count == 0
def test_per_user_dismissal_isolation():
"""Verify issue (2c): dismissal for user A does not dismiss for user B."""
service = FlowsService()
service.dismiss_flows_updates(["retrieval"], user_id="user_A")
assert "user_A" in service._dismissed_updates
assert "retrieval" in service._dismissed_updates["user_A"]
assert (
"user_B" not in service._dismissed_updates
or "retrieval" not in service._dismissed_updates.get("user_B", set())
)
@pytest.mark.asyncio
async def test_bulk_update_flows_dismissal_cleared_only_on_success():
"""Verify issue (4c): dismissal state is preserved on failure and cleared on success."""
service = FlowsService()
# Pre-dismiss update for user_A
service.dismiss_flows_updates(["retrieval"], user_id="user_A")
assert "retrieval" in service._dismissed_updates["user_A"]
mock_get_fail = MagicMock(status_code=500)
# 1. Update fails (pre-flight GET returns 500) -> dismissal remains intact
with patch("services.flows_service.clients.langflow_request", return_value=mock_get_fail):
results = await service.bulk_update_flows(["retrieval"], backup_custom=True)
assert results[0]["success"] is False
assert "retrieval" in service._dismissed_updates.get("user_A", set())
# 2. Update succeeds -> dismissal is cleared
mock_get_ok = MagicMock(status_code=200)
mock_get_ok.json.return_value = {
"id": "flow-retrieval-123",
"name": "Retrieval Flow",
"locked": True,
"data": {"nodes": []},
}
with (
patch("services.flows_service.clients.langflow_request", return_value=mock_get_ok),
patch.object(service, "_backup_flow", AsyncMock(return_value="/tmp/backup.json")),
patch.object(
service, "_reset_langflow_flow_locked", AsyncMock(return_value={"success": True})
),
):
results = await service.bulk_update_flows(["retrieval"], backup_custom=True)
assert results[0]["success"] is True
assert "retrieval" not in service._dismissed_updates.get("user_A", set())
@pytest.mark.asyncio
async def test_ensure_flows_exist_does_not_auto_update_on_startup():
"""Verify that existing flows are skipped on startup and never auto-updated."""
service = FlowsService()
mock_get_ok = MagicMock(status_code=200)
mock_get_ok.json.return_value = {
"id": "flow-retrieval-123",
"name": "Retrieval Flow",
"locked": True,
"updated_at": "2020-01-01T00:00:00Z",
}
reset_mock = AsyncMock()
backup_mock = AsyncMock()
with (
patch("services.flows_service.clients.langflow_request", return_value=mock_get_ok),
patch.object(service, "_backup_flow", backup_mock),
patch.object(service, "_reset_langflow_flow_locked", reset_mock),
):
created = await service.ensure_flows_exist()
assert created == set()
assert backup_mock.call_count == 0
assert reset_mock.call_count == 0
@pytest.mark.asyncio
async def test_get_flows_updates_available_includes_all_flows():
"""Verify that get_flows_updates_available surfaces updates regardless of locked status."""
service = FlowsService()
mock_update_info = {
"flow_type": "retrieval",
"flow_id": "flow-retrieval-123",
"is_custom": False,
}
with patch.object(service, "_check_flow_update", AsyncMock(return_value=mock_update_info)):
updates = await service.get_flows_updates_available()
assert len(updates) == 4
assert all(u["flow_type"] in ["nudges", "retrieval", "ingest", "url_ingest"] for u in updates)
@pytest.mark.asyncio
async def test_flow_update_uses_source_mtime_tag_not_langflow_updated_at(tmp_path):
service = FlowsService()
flow_path = tmp_path / "flow.json"
flow_path.write_text('{"id": "flow-1", "tags": ["rag"]}', encoding="utf-8")
source_mtime = flow_path.stat().st_mtime_ns
with patch.object(service, "_find_flow_file_by_id", return_value=str(flow_path)):
update = await service._check_flow_update(
"retrieval",
"flow-1",
{
"locked": True,
"tags": [f"openrag:file-mtime:{source_mtime - 1}"],
"updated_at": "2099-01-01T00:00:00Z",
},
)
assert update == {"flow_type": "retrieval", "flow_id": "flow-1", "is_custom": False}
@pytest.mark.asyncio
async def test_flow_update_is_not_available_when_source_mtime_matches_tag(tmp_path):
service = FlowsService()
flow_path = tmp_path / "flow.json"
flow_path.write_text('{"id": "flow-1", "tags": ["rag"]}', encoding="utf-8")
source_mtime = flow_path.stat().st_mtime_ns
with patch.object(service, "_find_flow_file_by_id", return_value=str(flow_path)):
update = await service._check_flow_update(
"retrieval",
"flow-1",
{
"locked": False,
"tags": [f"openrag:file-mtime:{source_mtime}"],
"updated_at": "1970-01-01T00:00:00Z",
},
)
assert update is None
@pytest.mark.asyncio
async def test_flow_update_does_not_upgrade_when_stored_mtime_is_newer(tmp_path):
service = FlowsService()
flow_path = tmp_path / "flow.json"
flow_path.write_text('{"id": "flow-1", "tags": ["rag"]}', encoding="utf-8")
source_mtime = flow_path.stat().st_mtime_ns
with patch.object(service, "_find_flow_file_by_id", return_value=str(flow_path)):
update = await service._check_flow_update(
"retrieval", "flow-1", {"tags": [f"openrag:file-mtime:{source_mtime + 1}"]}
)
assert update is None
@pytest.mark.asyncio
async def test_flow_update_detects_stored_flow_without_a_mtime_tag(tmp_path):
service = FlowsService()
flow_path = tmp_path / "flow.json"
flow_path.write_text('{"id": "flow-1", "tags": ["rag"]}', encoding="utf-8")
with patch.object(service, "_find_flow_file_by_id", return_value=str(flow_path)):
update = await service._check_flow_update(
"retrieval", "flow-1", {"locked": False, "tags": ["rag"]}
)
assert update == {"flow_type": "retrieval", "flow_id": "flow-1", "is_custom": True}
def test_stamp_flow_with_file_mtime_replaces_previous_mtime_tag(tmp_path):
service = FlowsService()
flow_path = tmp_path / "flow.json"
flow_path.write_text('{"id": "flow-1", "tags": ["rag"]}', encoding="utf-8")
flow_data = {
"id": "flow-1",
"tags": ["rag", "openrag:file-mtime:1", "openrag:file-mtime:2"],
}
service._stamp_flow_with_file_mtime(flow_data, str(flow_path))
assert flow_data["tags"] == [
"rag",
f"openrag:file-mtime:{flow_path.stat().st_mtime_ns}",
]
@pytest.mark.asyncio
async def test_reset_stamps_flow_with_source_mtime(tmp_path, monkeypatch):
service = FlowsService()
flow_path = tmp_path / "flow.json"
flow_path.write_text('{"id": "flow-1", "tags": ["rag"]}', encoding="utf-8")
source_mtime = flow_path.stat().st_mtime_ns
response = MagicMock(status_code=200)
response.json.return_value = {}
uploaded_flow: dict = {}
async def request(method, url, json=None, **kwargs):
assert method == "PUT"
assert url == "/api/v1/flows/flow-1"
uploaded_flow.update(json)
return response
monkeypatch.setattr("services.flows_service.LANGFLOW_URL", "http://langflow")
with (
patch.object(service, "_find_flow_file_by_id", return_value=str(flow_path)),
patch.object(service, "_unlock_flow", AsyncMock()),
patch.object(service, "_lock_flow", AsyncMock()),
patch("services.flows_service.clients.langflow_request", side_effect=request),
):
result = await service._reset_langflow_flow_locked("retrieval", "flow-1")
assert result["success"] is True
assert uploaded_flow["tags"] == ["rag", f"openrag:file-mtime:{source_mtime}"]
@pytest.mark.asyncio
async def test_ensure_flows_exist_stamps_created_flow_with_source_mtime(tmp_path, monkeypatch):
service = FlowsService()
flow_path = tmp_path / "flow.json"
flow_path.write_text('{"id": "flow-1", "tags": ["rag"]}', encoding="utf-8")
source_mtime = flow_path.stat().st_mtime_ns
uploaded_flow: dict = {}
async def request(method, url, json=None, **kwargs):
if method == "GET":
return MagicMock(status_code=404)
assert method == "PUT"
assert url == "/api/v1/flows/flow-1"
uploaded_flow.update(json)
return MagicMock(status_code=201)
monkeypatch.setattr("services.flows_service.NUDGES_FLOW_ID", "flow-1")
monkeypatch.setattr("services.flows_service.LANGFLOW_CHAT_FLOW_ID", "")
monkeypatch.setattr("services.flows_service.LANGFLOW_INGEST_FLOW_ID", "")
monkeypatch.setattr("services.flows_service.LANGFLOW_URL_INGEST_FLOW_ID", "")
with (
patch.object(service, "_find_flow_file_by_id", return_value=str(flow_path)),
patch("services.flows_service.clients.langflow_request", side_effect=request),
):
created = await service.ensure_flows_exist()
assert created == {"nudges"}
assert uploaded_flow["tags"] == ["rag", f"openrag:file-mtime:{source_mtime}"]