Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: use real marketplace and Nacos responses (#43385)

Asuka Minato716629f15165
diff --git a/api/tests/unit_tests/configs/test_nacos_http_client.py b/api/tests/unit_tests/configs/test_nacos_http_client.py
index 855a1a8acc1..5712ddb077e 100644
--- a/api/tests/unit_tests/configs/test_nacos_http_client.py
+++ b/api/tests/unit_tests/configs/test_nacos_http_client.py
@@ -1,17 +1,15 @@
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
import httpx
from configs.remote_settings_sources.nacos.http_request import NacosHttpClient
-def _ok_response(text: str = "ok", json_data: dict | None = None) -> MagicMock:
- response = MagicMock()
- response.text = text
- response.raise_for_status.return_value = None
+def _ok_response(text: str = "ok", json_data: dict | None = None) -> httpx.Response:
+ request = httpx.Request("GET", "http://nacos.test")
if json_data is not None:
- response.json.return_value = json_data
- return response
+ return httpx.Response(200, json=json_data, request=request)
+ return httpx.Response(200, text=text, request=request)
def test_http_request_passes_bounded_timeout():
diff --git a/api/tests/unit_tests/core/helper/test_marketplace.py b/api/tests/unit_tests/core/helper/test_marketplace.py
index a587584ae19..141ff71d133 100644
--- a/api/tests/unit_tests/core/helper/test_marketplace.py
+++ b/api/tests/unit_tests/core/helper/test_marketplace.py
@@ -1,7 +1,6 @@
from collections.abc import Callable
-from types import SimpleNamespace
-from unittest.mock import MagicMock
+import httpx
import pytest
from pytest_mock import MockerFixture
@@ -13,6 +12,11 @@ from core.helper.marketplace import (
get_plugin_pkg_url,
record_install_plugin_event,
)
+from core.plugin.entities.marketplace import MarketplacePluginSnapshot
+
+
+def _response(payload: dict[str, object]) -> httpx.Response:
+ return httpx.Response(200, json=payload, request=httpx.Request("POST", "https://marketplace.test"))
def test_get_plugin_pkg_url_contains_unique_identifier() -> None:
@@ -45,22 +49,19 @@ def test_batch_fetch_plugin_by_ids_returns_empty_for_empty_input(mocker: MockerF
def test_batch_fetch_plugin_by_ids_returns_plugins_from_response(mocker: MockerFixture) -> None:
- response = MagicMock()
- response.json.return_value = {"data": {"plugins": [{"id": "p1"}]}}
- response.raise_for_status.return_value = None
+ response = _response({"data": {"plugins": [{"id": "p1"}]}})
+ check_status = mocker.spy(response, "raise_for_status")
post_mock = mocker.patch("core.helper.marketplace.httpx.post", return_value=response)
plugins = batch_fetch_plugin_by_ids(["p1"])
assert plugins == [{"id": "p1"}]
post_mock.assert_called_once()
- response.raise_for_status.assert_called_once()
+ check_status.assert_called_once()
def test_batch_fetch_plugin_by_ids_rejects_invalid_plugins_response(mocker: MockerFixture) -> None:
- response = MagicMock()
- response.json.return_value = {"data": {"plugins": ["p1"]}}
- response.raise_for_status.return_value = None
+ response = _response({"data": {"plugins": ["p1"]}})
mocker.patch("core.helper.marketplace.httpx.post", return_value=response)
with pytest.raises(ValueError, match="plugins list"):
@@ -75,9 +76,8 @@ def test_batch_fetch_plugin_manifests_returns_empty_for_empty_input(mocker: Mock
def test_batch_fetch_plugin_manifests_validates_and_returns_plugins(mocker: MockerFixture) -> None:
- response = MagicMock()
- response.raise_for_status.return_value = None
- response.json.return_value = {"data": {"plugins": [{"id": "p1"}, {"id": "p2"}]}}
+ response = _response({"data": {"plugins": [{"id": "p1"}, {"id": "p2"}]}})
+ check_status = mocker.spy(response, "raise_for_status")
post_mock = mocker.patch("core.helper.marketplace.httpx.post", return_value=response)
validate_mock = mocker.patch(
"core.helper.marketplace.MarketplacePluginDeclaration.model_validate",
@@ -89,48 +89,47 @@ def test_batch_fetch_plugin_manifests_validates_and_returns_plugins(mocker: Mock
assert result == ["manifest-1", "manifest-2"]
post_mock.assert_called_once()
assert validate_mock.call_count == 2
- response.raise_for_status.assert_called_once()
+ check_status.assert_called_once()
def test_record_install_plugin_event_posts_and_checks_status(mocker: MockerFixture) -> None:
- response = MagicMock()
- response.raise_for_status.return_value = None
+ response = _response({})
+ check_status = mocker.spy(response, "raise_for_status")
post_mock = mocker.patch("core.helper.marketplace.httpx.post", return_value=response)
record_install_plugin_event("plugin.a")
post_mock.assert_called_once()
- response.raise_for_status.assert_called_once()
+ check_status.assert_called_once()
def test_fetch_global_plugin_manifest_caches_each_plugin(mocker: MockerFixture) -> None:
- response = MagicMock()
- response.raise_for_status.return_value = None
- response.json.return_value = {"plugins": [{"id": "a"}, {"id": "b"}]}
+ snapshots = [
+ MarketplacePluginSnapshot(
+ org="test",
+ name=name,
+ latest_version="1.0.0",
+ latest_package_identifier=f"test/{name}:1.0.0@checksum",
+ latest_package_url=f"https://marketplace.test/{name}",
+ )
+ for name in ("a", "b")
+ ]
+ response = _response({"plugins": [snapshot.model_dump() for snapshot in snapshots]})
mocker.patch("core.helper.marketplace.httpx.get", return_value=response)
- snapshot_a = SimpleNamespace(plugin_id="plugin-a", model_dump_json=lambda: '{"id":"a"}')
- snapshot_b = SimpleNamespace(plugin_id="plugin-b", model_dump_json=lambda: '{"id":"b"}')
- validate_mock = mocker.patch(
- "core.helper.marketplace.MarketplacePluginSnapshot.model_validate",
- side_effect=[snapshot_a, snapshot_b],
- )
setex_mock = mocker.patch("core.helper.marketplace.redis_client.setex")
cached_count = fetch_global_plugin_manifest("prefix:", 60)
assert cached_count == 2
- assert validate_mock.call_count == 2
- setex_mock.assert_any_call(name="prefix:plugin-a", time=60, value='{"id":"a"}')
- setex_mock.assert_any_call(name="prefix:plugin-b", time=60, value='{"id":"b"}')
+ for snapshot in snapshots:
+ setex_mock.assert_any_call(name=f"prefix:{snapshot.plugin_id}", time=60, value=snapshot.model_dump_json())
def test_fetch_global_plugin_manifest_reports_zero_for_empty_snapshot(mocker: MockerFixture) -> None:
"""An empty snapshot must be reported so callers can fall back instead of upgrading nothing."""
empty_plugins: list[dict[str, object]] = []
- response = MagicMock()
- response.raise_for_status.return_value = None
- response.json.return_value = {"metadata": {"plugin_count": 0}, "plugins": empty_plugins}
+ response = _response({"metadata": {"plugin_count": 0}, "plugins": empty_plugins})
mocker.patch("core.helper.marketplace.httpx.get", return_value=response)
setex_mock = mocker.patch("core.helper.marketplace.redis_client.setex")
@@ -141,9 +140,7 @@ def test_fetch_global_plugin_manifest_reports_zero_for_empty_snapshot(mocker: Mo
def test_fetch_global_plugin_manifest_reports_zero_when_plugins_key_missing(mocker: MockerFixture) -> None:
- response = MagicMock()
- response.raise_for_status.return_value = None
- response.json.return_value = {"metadata": {"plugin_count": 0}}
+ response = _response({"metadata": {"plugin_count": 0}})
mocker.patch("core.helper.marketplace.httpx.get", return_value=response)
setex_mock = mocker.patch("core.helper.marketplace.redis_client.setex")