Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

refactor: remove py test cases (#20312)

Jin Haiadf2e0c079ab
diff --git a/test/testcases/test_admin_api/conftest.py b/test/testcases/test_admin_api/conftest.py
deleted file mode 100644
index 45c9875f70..0000000000
--- a/test/testcases/test_admin_api/conftest.py
+++ /dev/null
@@ -1,120 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import os
-import urllib.parse
-from typing import Any
-
-import pytest
-import requests
-from configs import VERSION
-
-# Admin API runs on port 9381
-ADMIN_HOST_ADDRESS = os.getenv("ADMIN_HOST_ADDRESS", "http://127.0.0.1:9381")
-
-UNAUTHORIZED_ERROR_MESSAGE = "<!doctype html>\n<html lang=en>\n<title>401 unauthorized</title>\n<h1>unauthorized</h1>\n<p>the server could not verify that you are authorized to access the url requested. you either supplied the wrong credentials (e.g. a bad password), or your browser doesn&#39;t understand how to supply the credentials required.</p>\n"
-
-# password is "admin"
-ENCRYPTED_ADMIN_PASSWORD: str = """WBPsJbL/W+1HN+hchm5pgu1YC3yMEb/9MFtsanZrpKEE9kAj4u09EIIVDtIDZhJOdTjz5pp5QW9TwqXBfQ2qzDqVJiwK7HGcNsoPi4wQPCmnLo0fs62QklMlg7l1Q7fjGRgV+KWtvNUce2PFzgrcAGDqRIuA/slSclKUEISEiK4z62rdDgvHT8LyuACuF1lPUY5wV0m/MbmGijRJlgvglAF8BX0BP8rQr8wZeaJdcnAy/keuODCjltMZDL06tYluN7HoiU+qlhBB+ltqG411oO/+vVhBgWsuVVOHd8uMjJEL320GUWUicprDUZvjlLaSSqVyyOiRMHpqAE9eHEecWg=="""
-
-
-def admin_login(session: requests.Session, email: str = "[email protected]", password: str = "admin") -> str:
- """Helper function to login as admin and return authorization token"""
- url: str = f"{ADMIN_HOST_ADDRESS}/api/{VERSION}/admin/login"
- response: requests.Response = session.post(url, json={"email": email, "password": ENCRYPTED_ADMIN_PASSWORD})
- res_json: dict[str, Any] = response.json()
- if res_json.get("code") != 0:
- raise Exception(res_json.get("message"))
- # Admin login uses session cookies and Authorization header
- # Set Authorization header for subsequent requests
- auth: str = response.headers.get("Authorization", "")
- if auth:
- session.headers.update({"Authorization": auth})
- return auth
-
-
[email protected](scope="session")
-def admin_session() -> requests.Session:
- """Fixture to create an admin session with login"""
- session: requests.Session = requests.Session()
- try:
- admin_login(session)
- except Exception as e:
- pytest.skip(f"Admin login failed: {e}")
- return session
-
-
-def generate_user_api_key(session: requests.Session, user_name: str) -> dict[str, Any]:
- """Helper function to generate API key for a user
-
- Returns:
- Dict containing the full API response with keys: code, message, data
- """
- url: str = f"{ADMIN_HOST_ADDRESS}/api/{VERSION}/admin/users/{user_name}/new_token"
- response: requests.Response = session.post(url)
-
- # Some error responses (e.g., 401) may return HTML instead of JSON.
- try:
- res_json: dict[str, Any] = response.json()
- except requests.exceptions.JSONDecodeError:
- return {
- "code": response.status_code,
- "message": response.text,
- "data": None,
- }
- return res_json
-
-
-def get_user_api_key(session: requests.Session, username: str) -> dict[str, Any]:
- """Helper function to get API keys for a user
-
- Returns:
- Dict containing the full API response with keys: code, message, data
- """
- url: str = f"{ADMIN_HOST_ADDRESS}/api/{VERSION}/admin/users/{username}/token_list"
- response: requests.Response = session.get(url)
-
- try:
- res_json: dict[str, Any] = response.json()
- except requests.exceptions.JSONDecodeError:
- return {
- "code": response.status_code,
- "message": response.text,
- "data": None,
- }
- return res_json
-
-
-def delete_user_api_key(session: requests.Session, username: str, token: str) -> dict[str, Any]:
- """Helper function to delete an API key for a user
-
- Returns:
- Dict containing the full API response with keys: code, message, data
- """
- # URL encode the token to handle special characters
- encoded_token: str = urllib.parse.quote(token, safe="")
- url: str = f"{ADMIN_HOST_ADDRESS}/api/{VERSION}/admin/users/{username}/token/{encoded_token}"
- response: requests.Response = session.delete(url)
-
- try:
- res_json: dict[str, Any] = response.json()
- except requests.exceptions.JSONDecodeError:
- return {
- "code": response.status_code,
- "message": response.text,
- "data": None,
- }
- return res_json
diff --git a/test/testcases/test_admin_api/test_user_api_key_management/test_delete_user_api_key.py b/test/testcases/test_admin_api/test_user_api_key_management/test_delete_user_api_key.py
deleted file mode 100644
index 8e5c8a6732..0000000000
--- a/test/testcases/test_admin_api/test_user_api_key_management/test_delete_user_api_key.py
+++ /dev/null
@@ -1,191 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-from typing import Any
-
-import pytest
-import requests
-
-from conftest import delete_user_api_key, generate_user_api_key, get_user_api_key, UNAUTHORIZED_ERROR_MESSAGE
-from common.constants import RetCode
-from configs import EMAIL, HOST_ADDRESS, PASSWORD, VERSION
-
-
-class TestDeleteUserApiKey:
- @pytest.mark.p2
- def test_delete_user_api_key_success(self, admin_session: requests.Session) -> None:
- """Test successfully deleting an API key for a user"""
- user_name: str = EMAIL
-
- # Generate an API key first
- generate_response: dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response.get("code") == RetCode.SUCCESS, f"Generate should succeed, got code {generate_response.get('code')}"
- generated_key: dict[str, Any] = generate_response["data"]
- token: str = generated_key["token"]
-
- # Delete the API key
- delete_response: dict[str, Any] = delete_user_api_key(admin_session, user_name, token)
-
- # Verify response
- assert delete_response.get("code") == RetCode.SUCCESS, f"Delete should succeed, got code {delete_response.get('code')}"
- assert "message" in delete_response, "Response should contain message"
- message: str = delete_response.get("message", "")
- assert message == "API key deleted successfully", f"Message should indicate success, got: {message}"
-
- @pytest.mark.p2
- def test_user_api_key_removed_from_list_after_deletion(self, admin_session: requests.Session) -> None:
- """Test that deleted API key is removed from the list"""
- user_name: str = EMAIL
-
- # Generate an API key
- generate_response: dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response.get("code") == RetCode.SUCCESS, f"Generate should succeed, got code {generate_response.get('code')}"
- generated_key: dict[str, Any] = generate_response["data"]
- token: str = generated_key["token"]
-
- # Verify the key exists in the list
- get_response_before: dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response_before.get("code") == RetCode.SUCCESS, f"Get should succeed, got code {get_response_before.get('code')}"
- api_keys_before: list[dict[str, Any]] = get_response_before["data"]
- token_found_before: bool = any(key.get("token") == token for key in api_keys_before)
- assert token_found_before, "Generated API key should be in the list before deletion"
-
- # Delete the API key
- delete_response: dict[str, Any] = delete_user_api_key(admin_session, user_name, token)
- assert delete_response.get("code") == RetCode.SUCCESS, f"Delete should succeed, got code {delete_response.get('code')}"
-
- # Verify the key is no longer in the list
- get_response_after: dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response_after.get("code") == RetCode.SUCCESS, f"Get should succeed, got code {get_response_after.get('code')}"
- api_keys_after: list[dict[str, Any]] = get_response_after["data"]
- token_found_after: bool = any(key.get("token") == token for key in api_keys_after)
- assert not token_found_after, "Deleted API key should not be in the list after deletion"
-
- @pytest.mark.p2
- def test_delete_user_api_key_response_structure(self, admin_session: requests.Session) -> None:
- """Test that delete_user_api_key returns correct response structure"""
- user_name: str = EMAIL
-
- # Generate an API key
- generate_response: dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response.get("code") == RetCode.SUCCESS, f"Generate should succeed, got code {generate_response.get('code')}"
- token: str = generate_response["data"]["token"]
-
- # Delete the API key
- delete_response: dict[str, Any] = delete_user_api_key(admin_session, user_name, token)
-
- # Verify response structure
- assert delete_response.get("code") == RetCode.SUCCESS, f"Response code should be {RetCode.SUCCESS}, got {delete_response.get('code')}"
- assert "message" in delete_response, "Response should contain message"
- # Data can be None for delete operations
- assert "data" in delete_response, "Response should contain data field"
-
- @pytest.mark.p2
- def test_delete_user_api_key_twice(self, admin_session: requests.Session) -> None:
- """Test that deleting the same token twice behaves correctly"""
- user_name: str = EMAIL
-
- # Generate an API key
- generate_response: dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response.get("code") == RetCode.SUCCESS, f"Generate should succeed, got code {generate_response.get('code')}"
- token: str = generate_response["data"]["token"]
-
- # Delete the API key first time
- delete_response1: dict[str, Any] = delete_user_api_key(admin_session, user_name, token)
- assert delete_response1.get("code") == RetCode.SUCCESS, f"First delete should succeed, got code {delete_response1.get('code')}"
-
- # Try to delete the same token again
- delete_response2: dict[str, Any] = delete_user_api_key(admin_session, user_name, token)
-
- # Second delete should fail since token no longer exists
- assert delete_response2.get("code") == RetCode.NOT_FOUND, "Second delete should fail for already deleted token"
- assert "message" in delete_response2, "Response should contain message"
-
- @pytest.mark.p2
- def test_delete_user_api_key_with_nonexistent_token(self, admin_session: requests.Session) -> None:
- """Test deleting a non-existent API key fails"""
- user_name: str = EMAIL
- nonexistent_token: str = "ragflow-nonexistent-token-12345"
-
- # Try to delete a non-existent token
- delete_response: dict[str, Any] = delete_user_api_key(admin_session, user_name, nonexistent_token)
-
- # Should return error
- assert delete_response.get("code") == RetCode.NOT_FOUND, "Delete should fail for non-existent token"
- assert "message" in delete_response, "Response should contain message"
- message: str = delete_response.get("message", "")
- assert message == "API key not found or could not be deleted", f"Message should indicate token not found, got: {message}"
-
- @pytest.mark.p2
- def test_delete_user_api_key_with_nonexistent_user(self, admin_session: requests.Session) -> None:
- """Test deleting API key for non-existent user fails"""
- nonexistent_user: str = "[email protected]"
- token: str = "ragflow-test-token-12345"
-
- # Try to delete token for non-existent user
- delete_response: dict[str, Any] = delete_user_api_key(admin_session, nonexistent_user, token)
-
- # Should return error
- assert delete_response.get("code") == RetCode.NOT_FOUND, "Delete should fail for non-existent user"
- assert "message" in delete_response, "Response should contain message"
- message: str = delete_response.get("message", "")
- expected_message: str = f"User '{nonexistent_user}' not found"
- assert message == expected_message, f"Message should indicate user not found, got: {message}"
-
- @pytest.mark.p2
- def test_delete_user_api_key_wrong_user_token(self, admin_session: requests.Session) -> None:
- """Test that deleting a token belonging to another user fails"""
- user_name: str = EMAIL
-
- # create second user
- url: str = HOST_ADDRESS + f"/api/{VERSION}/users"
- user2_email: str = "[email protected]"
- register_data: dict[str, str] = {"email": user2_email, "nickname": "qa2", "password": PASSWORD}
- res: Any = requests.post(url=url, json=register_data)
- res: dict[str, Any] = res.json()
- if res.get("code") != 0 and "has already registered" not in res.get("message"):
- raise Exception(f"Failed to create second user: {res.get('message')}")
-
- # Generate a token for the test user
- generate_response: dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response.get("code") == RetCode.SUCCESS, f"Generate should succeed, got code {generate_response.get('code')}"
- token: str = generate_response["data"]["token"]
-
- # Try to delete with the second username
- delete_response: dict[str, Any] = delete_user_api_key(admin_session, user2_email, token)
-
- # Should fail because user doesn't exist or token doesn't belong to that user
- assert delete_response.get("code") == RetCode.NOT_FOUND, "Delete should fail for wrong user"
- assert "message" in delete_response, "Response should contain message"
- message: str = delete_response.get("message", "")
- expected_message: str = "API key not found or could not be deleted"
- assert message == expected_message, f"Message should indicate user not found, got: {message}"
-
- @pytest.mark.p3
- def test_delete_user_api_key_without_auth(self) -> None:
- """Test that deleting API key without admin auth fails"""
- session: requests.Session = requests.Session()
- user_name: str = EMAIL
- token: str = "ragflow-test-token-12345"
-
- response: dict[str, Any] = delete_user_api_key(session, user_name, token)
-
- # Verify error response
- assert response.get("code") == RetCode.UNAUTHORIZED, "Response code should indicate error"
- assert "message" in response, "Response should contain message"
- message: str = response.get("message", "").lower()
- # The message is an HTML string indicating unauthorized user.
- assert message == UNAUTHORIZED_ERROR_MESSAGE
diff --git a/test/testcases/test_admin_api/test_user_api_key_management/test_generate_user_api_key.py b/test/testcases/test_admin_api/test_user_api_key_management/test_generate_user_api_key.py
deleted file mode 100644
index 5385889246..0000000000
--- a/test/testcases/test_admin_api/test_user_api_key_management/test_generate_user_api_key.py
+++ /dev/null
@@ -1,232 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-from typing import Any, Dict, List
-
-import pytest
-import requests
-
-from common.constants import RetCode
-from conftest import generate_user_api_key, get_user_api_key, UNAUTHORIZED_ERROR_MESSAGE
-from configs import EMAIL
-
-
-class TestGenerateUserApiKey:
- @pytest.mark.p2
- def test_generate_user_api_key_success(self, admin_session: requests.Session) -> None:
- """Test successfully generating API key for a user"""
- # Use the test user email (get_user_details expects email)
- user_name: str = EMAIL
-
- # Generate API key
- response: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
-
- # Verify response code, message, and data
- assert response.get("code") == RetCode.SUCCESS, f"Response code should be {RetCode.SUCCESS}, got {response.get('code')}"
- assert "message" in response, "Response should contain message"
- assert "data" in response, "Response should contain data"
- assert response.get("data") is not None, "API key generation should return data"
-
- result: Dict[str, Any] = response["data"]
-
- # Verify response structure
- assert "tenant_id" in result, "Response should contain tenant_id"
- assert "token" in result, "Response should contain token"
- assert "beta" in result, "Response should contain beta"
- assert "create_time" in result, "Response should contain create_time"
- assert "create_date" in result, "Response should contain create_date"
-
- # Verify token format (should start with "ragflow-")
- token: str = result["token"]
- assert isinstance(token, str), "Token should be a string"
- assert len(token) > 0, "Token should not be empty"
-
- # Verify beta is independently generated
- beta: str = result["beta"]
- assert isinstance(beta, str), "Beta should be a string"
- assert len(beta) == 32, "Beta should be 32 characters"
- # Beta should be independent from token (not derived from it)
- if token.startswith("ragflow-"):
- token_without_prefix: str = token.replace("ragflow-", "")[:32]
- assert beta != token_without_prefix, "Beta should be independently generated, not derived from token"
-
- @pytest.mark.p2
- def test_generate_user_api_key_appears_in_list(self, admin_session: requests.Session) -> None:
- """Test that generated API key appears in get_user_api_key list"""
- user_name: str = EMAIL
-
- # Generate API key
- generate_response: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response.get("code") == RetCode.SUCCESS, f"Generate should succeed, got code {generate_response.get('code')}"
- generated_key: Dict[str, Any] = generate_response["data"]
- token: str = generated_key["token"]
-
- # Get all API keys for the user
- get_response: Dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response.get("code") == RetCode.SUCCESS, f"Get should succeed, got code {get_response.get('code')}"
- api_keys: List[Dict[str, Any]] = get_response["data"]
-
- # Verify the generated key is in the list
- assert len(api_keys) > 0, "User should have at least one API key"
- token_found: bool = any(key.get("token") == token for key in api_keys)
- assert token_found, "Generated API key should appear in the list"
-
- @pytest.mark.p2
- def test_generate_user_api_key_response_structure(self, admin_session: requests.Session) -> None:
- """Test that generate_user_api_key returns correct response structure"""
- user_name: str = EMAIL
-
- response: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
-
- # Verify response code, message, and data
- assert response.get("code") == RetCode.SUCCESS, f"Response code should be {RetCode.SUCCESS}, got {response.get('code')}"
- assert "message" in response, "Response should contain message"
- assert "data" in response, "Response should contain data"
-
- result: Dict[str, Any] = response["data"]
-
- # Verify all required fields
- assert "tenant_id" in result, "Response should have tenant_id"
- assert "token" in result, "Response should have token"
- assert "beta" in result, "Response should have beta"
- assert "create_time" in result, "Response should have create_time"
- assert "create_date" in result, "Response should have create_date"
- assert "update_time" in result, "Response should have update_time"
- assert "update_date" in result, "Response should have update_date"
-
- # Verify field types
- assert isinstance(result["tenant_id"], str), "tenant_id should be string"
- assert isinstance(result["token"], str), "token should be string"
- assert isinstance(result["beta"], str), "beta should be string"
- assert isinstance(result["create_time"], (int, type(None))), "create_time should be int or None"
- assert isinstance(result["create_date"], (str, type(None))), "create_date should be string or None"
-
- @pytest.mark.p2
- def test_generate_user_api_key_multiple_times(self, admin_session: requests.Session) -> None:
- """Test generating multiple API keys for the same user"""
- user_name: str = EMAIL
-
- # Generate first API key
- response1: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response1.get("code") == RetCode.SUCCESS, f"First generate should succeed, got code {response1.get('code')}"
- key1: Dict[str, Any] = response1["data"]
- token1: str = key1["token"]
-
- # Generate second API key
- response2: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response2.get("code") == RetCode.SUCCESS, f"Second generate should succeed, got code {response2.get('code')}"
- key2: Dict[str, Any] = response2["data"]
- token2: str = key2["token"]
-
- # Tokens should be different
- assert token1 != token2, "Multiple API keys should have different tokens"
-
- # Both should appear in the list
- get_response: Dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response.get("code") == RetCode.SUCCESS, f"Get should succeed, got code {get_response.get('code')}"
- api_keys: List[Dict[str, Any]] = get_response["data"]
- tokens: List[str] = [key.get("token") for key in api_keys]
- assert token1 in tokens, "First token should be in the list"
- assert token2 in tokens, "Second token should be in the list"
-
- @pytest.mark.p2
- def test_generate_user_api_key_nonexistent_user(self, admin_session: requests.Session) -> None:
- """Test generating API key for non-existent user fails"""
- response: Dict[str, Any] = generate_user_api_key(admin_session, "nonexistent_user_12345")
-
- # Verify error response
- assert response.get("code") == RetCode.NOT_FOUND, "Response code should indicate error"
- assert "message" in response, "Response should contain message"
- message: str = response.get("message", "")
- assert message == "User not found!", f"Message should indicate user not found, got: {message}"
-
- @pytest.mark.p2
- def test_generate_user_api_key_tenant_id_consistency(self, admin_session: requests.Session) -> None:
- """Test that generated API keys have consistent tenant_id"""
- user_name: str = EMAIL
-
- # Generate multiple API keys
- response1: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response1.get("code") == RetCode.SUCCESS, f"First generate should succeed, got code {response1.get('code')}"
- key1: Dict[str, Any] = response1["data"]
-
- response2: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response2.get("code") == RetCode.SUCCESS, f"Second generate should succeed, got code {response2.get('code')}"
- key2: Dict[str, Any] = response2["data"]
-
- # Tenant IDs should be the same for the same user
- assert key1["tenant_id"] == key2["tenant_id"], "Same user should have same tenant_id"
-
- @pytest.mark.p2
- def test_generate_user_api_key_token_format(self, admin_session: requests.Session) -> None:
- """Test that generated API key has correct format"""
- user_name: str = EMAIL
-
- response: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response.get("code") == RetCode.SUCCESS, f"Response code should be {RetCode.SUCCESS}, got {response.get('code')}"
- result: Dict[str, Any] = response["data"]
- token: str = result["token"]
-
- # Token should be a non-empty string
- assert isinstance(token, str), "Token should be a string"
- assert len(token) > 0, "Token should not be empty"
-
- # Beta should be independently generated (32 chars, not derived from token)
- beta: str = result["beta"]
- assert isinstance(beta, str), "Beta should be a string"
- assert len(beta) == 32, "Beta should be 32 characters"
- # Beta should be independent from token (not derived from it)
- if token.startswith("ragflow-"):
- token_without_prefix: str = token.replace("ragflow-", "")[:32]
- assert beta != token_without_prefix, "Beta should be independently generated, not derived from token"
-
- @pytest.mark.p1
- def test_generate_user_api_key_without_auth(self) -> None:
- """Test that generating API key without admin auth fails"""
- session: requests.Session = requests.Session()
- user_name: str = EMAIL
-
- response: Dict[str, Any] = generate_user_api_key(session, user_name)
-
- # Verify error response
- assert response.get("code") == RetCode.UNAUTHORIZED, "Response code should indicate error"
- assert "message" in response, "Response should contain message"
- message: str = response.get("message", "").lower()
- # The message is an HTML string indicating unauthorized user .
- assert message == UNAUTHORIZED_ERROR_MESSAGE
-
- @pytest.mark.p3
- def test_generate_user_api_key_timestamp_fields(self, admin_session: requests.Session) -> None:
- """Test that generated API key has correct timestamp fields"""
- user_name: str = EMAIL
-
- response: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response.get("code") == RetCode.SUCCESS, f"Response code should be {RetCode.SUCCESS}, got {response.get('code')}"
- result: Dict[str, Any] = response["data"]
-
- # create_time should be a timestamp (int)
- create_time: Any = result.get("create_time")
- assert create_time is None or isinstance(create_time, int), "create_time should be int or None"
- if create_time is not None:
- assert create_time > 0, "create_time should be positive"
-
- # create_date should be a date string
- create_date: Any = result.get("create_date")
- assert create_date is None or isinstance(create_date, str), "create_date should be string or None"
-
- # update_time and update_date should be None for new keys
- assert result.get("update_time") is None, "update_time should be None for new keys"
- assert result.get("update_date") is None, "update_date should be None for new keys"
diff --git a/test/testcases/test_admin_api/test_user_api_key_management/test_get_user_api_key.py b/test/testcases/test_admin_api/test_user_api_key_management/test_get_user_api_key.py
deleted file mode 100644
index 3829976fb7..0000000000
--- a/test/testcases/test_admin_api/test_user_api_key_management/test_get_user_api_key.py
+++ /dev/null
@@ -1,169 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-from typing import Any, Dict, List
-
-import pytest
-import requests
-
-from conftest import generate_user_api_key, get_user_api_key, UNAUTHORIZED_ERROR_MESSAGE
-from common.constants import RetCode
-from configs import EMAIL
-
-
-class TestGetUserApiKey:
- @pytest.mark.p2
- def test_get_user_api_key_success(self, admin_session: requests.Session) -> None:
- """Test successfully getting API keys for a user with correct response structure"""
- user_name: str = EMAIL
-
- # Generate a test API key first
- generate_response: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response["code"] == RetCode.SUCCESS, generate_response
- generated_key: Dict[str, Any] = generate_response["data"]
- generated_token: str = generated_key["token"]
-
- # Get all API keys for the user
- get_response: Dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response["code"] == RetCode.SUCCESS, get_response
- assert "message" in get_response, "Response should contain message"
- assert "data" in get_response, "Response should contain data"
-
- api_keys: List[Dict[str, Any]] = get_response["data"]
-
- # Verify response is a list with at least one key
- assert isinstance(api_keys, list), "API keys should be returned as a list"
- assert len(api_keys) > 0, "User should have at least one API key"
-
- # Verify structure of each API key
- for key in api_keys:
- assert isinstance(key, dict), "Each API key should be a dictionary"
- assert "token" in key, "API key should contain token"
- assert "beta" in key, "API key should contain beta"
- assert "tenant_id" in key, "API key should contain tenant_id"
- assert "create_date" in key, "API key should contain create_date"
-
- # Verify field types
- assert isinstance(key["token"], str), "token should be string"
- assert isinstance(key["beta"], str), "beta should be string"
- assert isinstance(key["tenant_id"], str), "tenant_id should be string"
- assert isinstance(key.get("create_date"), (str, type(None))), "create_date should be string or None"
- assert isinstance(key.get("update_date"), (str, type(None))), "update_date should be string or None"
-
- # Verify the generated key is in the list
- token_found: bool = any(key.get("token") == generated_token for key in api_keys)
- assert token_found, "Generated API key should appear in the list"
-
- @pytest.mark.p2
- def test_get_user_api_key_nonexistent_user(self, admin_session: requests.Session) -> None:
- """Test getting API keys for non-existent user fails"""
- nonexistent_user: str = "nonexistent_user_12345"
- response: Dict[str, Any] = get_user_api_key(admin_session, nonexistent_user)
-
- assert response["code"] == RetCode.NOT_FOUND, response
- assert "message" in response, "Response should contain message"
- message: str = response["message"]
- expected_message: str = f"User '{nonexistent_user}' not found"
- assert message == expected_message, f"Message should indicate user not found, got: {message}"
-
- @pytest.mark.p2
- def test_get_user_api_key_empty_username(self, admin_session: requests.Session) -> None:
- """Test getting API keys with empty username"""
- response: Dict[str, Any] = get_user_api_key(admin_session, "")
-
- # Empty username should either return error or empty list
- if response["code"] == RetCode.SUCCESS:
- assert "data" in response, "Response should contain data"
- api_keys: List[Dict[str, Any]] = response["data"]
- assert isinstance(api_keys, list), "Should return a list"
- assert len(api_keys) == 0, "Empty username should return empty list"
- else:
- assert "message" in response, "Error response should contain message"
- assert len(response["message"]) > 0, "Error message should not be empty"
-
- @pytest.mark.p2
- def test_get_user_api_key_token_uniqueness(self, admin_session: requests.Session) -> None:
- """Test that all API keys in the list have unique tokens"""
- user_name: str = EMAIL
-
- # Generate multiple API keys
- response1: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response1["code"] == RetCode.SUCCESS, response1
- response2: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response2["code"] == RetCode.SUCCESS, response2
-
- # Get all API keys
- get_response: Dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response["code"] == RetCode.SUCCESS, get_response
- api_keys: List[Dict[str, Any]] = get_response["data"]
-
- # Verify all tokens are unique
- tokens: List[str] = [key.get("token") for key in api_keys if key.get("token")]
- assert len(tokens) == len(set(tokens)), "All API keys should have unique tokens"
-
- @pytest.mark.p2
- def test_get_user_api_key_tenant_id_consistency(self, admin_session: requests.Session) -> None:
- """Test that all API keys for a user have the same tenant_id"""
- user_name: str = EMAIL
-
- # Generate multiple API keys
- response1: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response1["code"] == RetCode.SUCCESS, response1
- response2: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert response2["code"] == RetCode.SUCCESS, response2
-
- # Get all API keys
- get_response: Dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response["code"] == RetCode.SUCCESS, get_response
- api_keys: List[Dict[str, Any]] = get_response["data"]
-
- # Verify all keys have the same tenant_id
- tenant_ids: List[str] = [key.get("tenant_id") for key in api_keys if key.get("tenant_id")]
- if len(tenant_ids) > 0:
- assert all(tid == tenant_ids[0] for tid in tenant_ids), "All API keys should have the same tenant_id"
-
- @pytest.mark.p2
- def test_get_user_api_key_beta_format(self, admin_session: requests.Session) -> None:
- """Test that beta field in API keys has correct format (32 characters)"""
- user_name: str = EMAIL
-
- # Generate a test API key
- generate_response: Dict[str, Any] = generate_user_api_key(admin_session, user_name)
- assert generate_response["code"] == RetCode.SUCCESS, generate_response
-
- # Get all API keys
- get_response: Dict[str, Any] = get_user_api_key(admin_session, user_name)
- assert get_response["code"] == RetCode.SUCCESS, get_response
- api_keys: List[Dict[str, Any]] = get_response["data"]
-
- # Verify beta format for all keys
- for key in api_keys:
- beta: str = key.get("beta", "")
- assert isinstance(beta, str), "beta should be a string"
- assert len(beta) == 32, f"beta should be 32 characters, got {len(beta)}"
-
- @pytest.mark.p3
- def test_get_user_api_key_without_auth(self) -> None:
- """Test that getting API keys without admin auth fails"""
- session: requests.Session = requests.Session()
- user_name: str = EMAIL
-
- response: Dict[str, Any] = get_user_api_key(session, user_name)
-
- assert response["code"] == RetCode.UNAUTHORIZED, response
- assert "message" in response, "Response should contain message"
- message: str = response["message"].lower()
- assert message == UNAUTHORIZED_ERROR_MESSAGE
diff --git a/test/testcases/test_common_data_source/test_rss_connector_unit.py b/test/testcases/test_common_data_source/test_rss_connector_unit.py
deleted file mode 100644
index 39585bcbd1..0000000000
--- a/test/testcases/test_common_data_source/test_rss_connector_unit.py
+++ /dev/null
@@ -1,120 +0,0 @@
-from datetime import datetime, timezone
-import importlib
-import sys
-from pathlib import Path
-from types import SimpleNamespace
-from types import ModuleType
-
-import pytest
-
-import common
-
-repo_root = Path(__file__).resolve().parents[3]
-data_source_pkg = ModuleType("common.data_source")
-data_source_pkg.__path__ = [str(repo_root / "common" / "data_source")]
-sys.modules["common.data_source"] = data_source_pkg
-setattr(common, "data_source", data_source_pkg)
-
-DocumentSource = importlib.import_module("common.data_source.config").DocumentSource
-RSSConnector = importlib.import_module("common.data_source.rss_connector").RSSConnector
-
-
-class _FakeResponse:
- def __init__(self, content: bytes = b"feed") -> None:
- self.content = content
-
- def raise_for_status(self) -> None:
- return None
-
-
-def _mock_feed(*entries, bozo=False, bozo_exception=None):
- return SimpleNamespace(
- entries=list(entries),
- bozo=bozo,
- bozo_exception=bozo_exception,
- )
-
-
-def test_validate_connector_settings_rejects_invalid_feed_url():
- connector = RSSConnector(feed_url="ftp://example.com/feed.xml")
-
- with pytest.raises(ValueError, match="valid http or https URL"):
- connector.validate_connector_settings()
-
-
-def test_validate_connector_settings_rejects_empty_feed(monkeypatch):
- monkeypatch.setattr("common.data_source.rss_connector.requests.get", lambda *_args, **_kwargs: _FakeResponse())
- monkeypatch.setattr(
- "common.data_source.rss_connector.feedparser.parse",
- lambda _content: _mock_feed(),
- )
-
- connector = RSSConnector(feed_url="https://example.com/feed.xml")
-
- with pytest.raises(ValueError, match="contains no entries"):
- connector.validate_connector_settings()
-
-
-def test_load_from_state_builds_documents(monkeypatch):
- monkeypatch.setattr("common.data_source.rss_connector.requests.get", lambda *_args, **_kwargs: _FakeResponse())
- monkeypatch.setattr(
- "common.data_source.rss_connector.feedparser.parse",
- lambda _content: _mock_feed(
- {
- "id": "entry-1",
- "link": "https://example.com/posts/1",
- "title": "Post One",
- "content": [{"value": "<p>Hello <b>world</b></p>"}],
- "author": "Alice",
- "tags": [{"term": "news"}, {"term": "product"}],
- "updated": "Tue, 02 Jan 2024 15:04:05 GMT",
- }
- ),
- )
-
- connector = RSSConnector(feed_url="https://example.com/feed.xml")
- batch = next(connector.load_from_state())
-
- assert len(batch) == 1
- doc = batch[0]
- assert doc.source == DocumentSource.RSS
- assert doc.semantic_identifier == "Post One"
- assert doc.extension == ".txt"
- assert doc.metadata == {
- "feed_url": "https://example.com/feed.xml",
- "link": "https://example.com/posts/1",
- "author": "Alice",
- "categories": ["news", "product"],
- }
- assert "Hello" in doc.blob.decode("utf-8")
- assert "world" in doc.blob.decode("utf-8")
-
-
-def test_poll_source_filters_entries_by_timestamp(monkeypatch):
- monkeypatch.setattr("common.data_source.rss_connector.requests.get", lambda *_args, **_kwargs: _FakeResponse())
- monkeypatch.setattr(
- "common.data_source.rss_connector.feedparser.parse",
- lambda _content: _mock_feed(
- {
- "id": "entry-1",
- "title": "Older",
- "summary": "older summary",
- "updated": "Mon, 01 Jan 2024 00:00:00 GMT",
- },
- {
- "id": "entry-2",
- "title": "Newer",
- "summary": "new summary",
- "updated": "Tue, 02 Jan 2024 00:00:00 GMT",
- },
- ),
- )
-
- connector = RSSConnector(feed_url="https://example.com/feed.xml")
- start = datetime(2024, 1, 1, tzinfo=timezone.utc).timestamp()
- end = datetime(2024, 1, 2, tzinfo=timezone.utc).timestamp()
-
- batches = list(connector.poll_source(start, end))
-
- assert len(batches) == 1
- assert [doc.semantic_identifier for doc in batches[0]] == ["Newer"]
diff --git a/test/testcases/test_http_api/common.py b/test/testcases/test_http_api/common.py
deleted file mode 100644
index 3eff15f5b9..0000000000
--- a/test/testcases/test_http_api/common.py
+++ /dev/null
@@ -1,519 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from pathlib import Path
-
-import requests
-from configs import HOST_ADDRESS, VERSION
-from requests_toolbelt import MultipartEncoder
-from utils.file_utils import create_txt_file
-
-HEADERS = {"Content-Type": "application/json"}
-DATASETS_API_URL = f"/api/{VERSION}/datasets"
-FILE_API_URL = f"/api/{VERSION}/datasets/{{dataset_id}}/documents"
-FILE_PARSE_API_URL = f"/api/{VERSION}/datasets/{{dataset_id}}/documents/parse"
-FILE_STOP_PARSE_API_URL = f"/api/{VERSION}/datasets/{{dataset_id}}/documents/stop"
-CHUNK_API_URL = f"/api/{VERSION}/datasets/{{dataset_id}}/documents/{{document_id}}/chunks"
-CHAT_ASSISTANT_API_URL = f"/api/{VERSION}/chats"
-SESSION_WITH_CHAT_ASSISTANT_API_URL = f"/api/{VERSION}/chats/{{chat_id}}/sessions"
-SESSION_WITH_AGENT_API_URL = f"/api/{VERSION}/agents/{{agent_id}}/sessions"
-AGENT_API_URL = f"/api/{VERSION}/agents"
-RETRIEVAL_API_URL = f"/api/{VERSION}/retrieval"
-
-
-# DATASET MANAGEMENT
-def create_dataset(auth, payload=None, *, headers=HEADERS, data=None):
- res = requests.post(url=f"{HOST_ADDRESS}{DATASETS_API_URL}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def list_datasets(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_API_URL}", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def update_dataset(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.put(url=f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def delete_datasets(auth, payload=None, *, headers=HEADERS, data=None):
- """
- Delete datasets.
- The endpoint is DELETE /api/{VERSION}/datasets with payload {"ids": [...]}
- This is the standard SDK REST API endpoint for dataset deletion.
- """
- res = requests.delete(url=f"{HOST_ADDRESS}{DATASETS_API_URL}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def delete_all_datasets(auth, *, page_size=100):
- return delete_datasets(auth, {"ids": None, "delete_all": True})
-
-
-def batch_create_datasets(auth, num):
- ids = []
- for i in range(num):
- res = create_dataset(auth, {"name": f"dataset_{i}"})
- ids.append(res["data"]["id"])
- return ids
-
-
-# FILE MANAGEMENT WITHIN DATASET
-def upload_documents(auth, dataset_id, files_path=None):
- url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
-
- if files_path is None:
- files_path = []
-
- fields = []
- file_objects = []
- try:
- for fp in files_path:
- p = Path(fp)
- f = p.open("rb")
- fields.append(("file", (p.name, f)))
- file_objects.append(f)
- m = MultipartEncoder(fields=fields)
-
- res = requests.post(
- url=url,
- headers={"Content-Type": m.content_type},
- auth=auth,
- data=m,
- )
- return res.json()
- finally:
- for f in file_objects:
- f.close()
-
-
-def download_document(auth, dataset_id, document_id, save_path):
- url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
- res = requests.get(url=url, auth=auth, stream=True)
- try:
- # available for unauthed downloads
- if res.status_code in (200, 401):
- with open(save_path, "wb") as f:
- for chunk in res.iter_content(chunk_size=8192):
- f.write(chunk)
- finally:
- res.close()
-
- return res
-
-
-def list_documents(auth, dataset_id, params=None):
- url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def update_document(auth, dataset_id, document_id, payload=None):
- url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
- res = requests.patch(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_documents(auth, dataset_id, payload=None):
- url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
- res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_all_documents(auth, dataset_id, *, page_size=100):
- return delete_documents(auth, dataset_id, {"ids": None, "delete_all": True})
-
-
-def parse_documents(auth, dataset_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{FILE_PARSE_API_URL}".format(dataset_id=dataset_id)
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def stop_parse_documents(auth, dataset_id, payload=None):
- url = f"{HOST_ADDRESS}{FILE_STOP_PARSE_API_URL}".format(dataset_id=dataset_id)
- res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def bulk_upload_documents(auth, dataset_id, num, tmp_path):
- fps = []
- for i in range(num):
- fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
- fps.append(fp)
- res = upload_documents(auth, dataset_id, fps)
- document_ids = []
- for document in res["data"]:
- document_ids.append(document["id"])
- return document_ids
-
-
-# CHUNK MANAGEMENT WITHIN DATASET
-def add_chunk(auth, dataset_id, document_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def list_chunks(auth, dataset_id, document_id, params=None):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def get_chunk(auth, dataset_id, document_id, chunk_id):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}/{chunk_id}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def update_chunk(auth, dataset_id, document_id, chunk_id, payload=None):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}/{chunk_id}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.patch(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_chunks(auth, dataset_id, document_id, payload=None):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_all_chunks(auth, dataset_id, document_id, *, page_size=100):
- return delete_chunks(auth, dataset_id, document_id, {"chunk_ids": None, "delete_all": True})
-
-
-def retrieval_chunks(auth, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{RETRIEVAL_API_URL}"
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def batch_add_chunks(auth, dataset_id, document_id, num):
- chunk_ids = []
- for i in range(num):
- res = add_chunk(auth, dataset_id, document_id, {"content": f"chunk test {i}"})
- chunk_ids.append(res["data"]["chunk"]["id"])
- return chunk_ids
-
-
-# CHAT ASSISTANT MANAGEMENT
-def create_chat_assistant(auth, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{CHAT_ASSISTANT_API_URL}"
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def list_chat_assistants(auth, params=None):
- url = f"{HOST_ADDRESS}{CHAT_ASSISTANT_API_URL}"
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def get_chat_assistant(auth, chat_assistant_id):
- url = f"{HOST_ADDRESS}{CHAT_ASSISTANT_API_URL}/{chat_assistant_id}"
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def update_chat_assistant(auth, chat_assistant_id, payload=None):
- url = f"{HOST_ADDRESS}{CHAT_ASSISTANT_API_URL}/{chat_assistant_id}"
- res = requests.put(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def patch_chat_assistant(auth, chat_assistant_id, payload=None):
- url = f"{HOST_ADDRESS}{CHAT_ASSISTANT_API_URL}/{chat_assistant_id}"
- res = requests.patch(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_chat_assistants(auth, payload=None):
- url = f"{HOST_ADDRESS}{CHAT_ASSISTANT_API_URL}"
- res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_all_chat_assistants(auth, *, page_size=100):
- return delete_chat_assistants(auth, {"ids": None, "delete_all": True})
-
-
-def batch_create_chat_assistants(auth, num):
- chat_assistant_ids = []
- for i in range(num):
- res = create_chat_assistant(auth, {"name": f"test_chat_assistant_{i}", "dataset_ids": []})
- chat_assistant_ids.append(res["data"]["id"])
- return chat_assistant_ids
-
-
-# SESSION MANAGEMENT
-def create_session_with_chat_assistant(auth, chat_assistant_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{SESSION_WITH_CHAT_ASSISTANT_API_URL}".format(chat_id=chat_assistant_id)
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def list_session_with_chat_assistants(auth, chat_assistant_id, params=None):
- url = f"{HOST_ADDRESS}{SESSION_WITH_CHAT_ASSISTANT_API_URL}".format(chat_id=chat_assistant_id)
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def update_session_with_chat_assistant(auth, chat_assistant_id, session_id, payload=None):
- url = f"{HOST_ADDRESS}{SESSION_WITH_CHAT_ASSISTANT_API_URL}/{session_id}".format(chat_id=chat_assistant_id)
- res = requests.patch(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_session_with_chat_assistants(auth, chat_assistant_id, payload=None):
- url = f"{HOST_ADDRESS}{SESSION_WITH_CHAT_ASSISTANT_API_URL}".format(chat_id=chat_assistant_id)
- res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_all_sessions_with_chat_assistant(auth, chat_assistant_id, *, page_size=100):
- return delete_session_with_chat_assistants(auth, chat_assistant_id, {"ids": None, "delete_all": True})
-
-
-def batch_add_sessions_with_chat_assistant(auth, chat_assistant_id, num):
- session_ids = []
- for i in range(num):
- res = create_session_with_chat_assistant(auth, chat_assistant_id, {"name": f"session_with_chat_assistant_{i}"})
- session_ids.append(res["data"]["id"])
- return session_ids
-
-
-# DATASET GRAPH AND TASKS
-def knowledge_graph(auth, dataset_id, params=None):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/graph"
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def delete_knowledge_graph(auth, dataset_id, payload=None):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/graph"
- if payload is None:
- res = requests.delete(url=url, headers=HEADERS, auth=auth)
- else:
- res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def metadata_summary(auth, dataset_id, params=None):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/metadata/summary"
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def metadata_batch_update(auth, dataset_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/metadata/update"
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def update_documents_metadata(auth, dataset_id, payload=None):
- """New unified API for updating document metadata.
-
- Uses PATCH method at /api/v1/datasets/{dataset_id}/documents/metadatas
- """
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/documents/metadatas"
- res = requests.patch(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-# CHAT COMPLETIONS AND RELATED QUESTIONS
-def related_questions(auth, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}/api/{VERSION}/searchbots/related_questions"
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-# AGENT MANAGEMENT AND SESSIONS
-def create_agent(auth, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{AGENT_API_URL}"
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def list_agents(auth, params=None):
- url = f"{HOST_ADDRESS}{AGENT_API_URL}"
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def delete_agent(auth, agent_id):
- url = f"{HOST_ADDRESS}{AGENT_API_URL}/{agent_id}"
- res = requests.delete(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def create_agent_session(auth, agent_id, payload=None, params=None):
- url = f"{HOST_ADDRESS}{SESSION_WITH_AGENT_API_URL}".format(agent_id=agent_id)
- res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload, params=params)
- return res.json()
-
-
-def list_agent_sessions(auth, agent_id, params=None):
- url = f"{HOST_ADDRESS}{SESSION_WITH_AGENT_API_URL}".format(agent_id=agent_id)
- res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
- return res.json()
-
-
-def delete_agent_sessions(auth, agent_id, payload=None):
- url = f"{HOST_ADDRESS}{SESSION_WITH_AGENT_API_URL}".format(agent_id=agent_id)
- res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_all_agent_sessions(auth, agent_id, *, page_size=100):
- return delete_agent_sessions(auth, agent_id, {"ids": None, "delete_all": True})
-
-
-def agent_completions(auth, agent_id, payload=None):
- url = f"{HOST_ADDRESS}{AGENT_API_URL}/chat/completions"
- body = {"agent_id": agent_id}
- if payload:
- body.update(payload)
- res = requests.post(url=url, headers=HEADERS, auth=auth, json=body)
- return res.json()
-
-
-def chat_completions(auth, chat_id=None, payload=None):
- """
- Send a question/message to a chat assistant and get completion.
-
- Args:
- auth: Authentication object
- chat_id: Chat assistant ID
- payload: Dictionary containing:
- - messages: list (required) - Conversation messages
- - stream: bool (optional) - Whether to stream responses, default False
- - session_id: str (optional) - Session ID for conversation context
-
- Returns:
- Response JSON with answer data
- """
- url = f"{HOST_ADDRESS}/api/{VERSION}/chat/completions"
- payload = dict(payload or {})
- if chat_id:
- payload.setdefault("chat_id", chat_id)
- if "question" in payload and "messages" not in payload:
- payload["messages"] = [{"role": "user", "content": payload.pop("question")}]
- res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def chat_completions_openai(auth, chat_id, payload=None, *, headers=HEADERS):
- """
- Send a request to the OpenAI-compatible chat completions endpoint.
-
- Args:
- auth: Authentication object
- chat_id: Chat assistant ID
- payload: Dictionary in OpenAI chat completions format containing:
- - messages: list (required) - List of message objects with 'role' and 'content'
- - stream: bool (optional) - Whether to stream responses, default False
-
- Returns:
- Response JSON in OpenAI chat completions format with usage information
- """
- url = f"{HOST_ADDRESS}/api/{VERSION}/openai/{chat_id}/chat/completions"
- payload = dict(payload or {})
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-# NEW DATASET ENDPOINTS
-def get_dataset(auth, dataset_id, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}"
- res = requests.get(url=url, headers=headers, auth=auth)
- return res.json()
-
-
-def get_ingestion_summary(auth, dataset_id, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/ingestions/summary"
- res = requests.get(url=url, headers=headers, auth=auth)
- return res.json()
-
-
-def list_ingestion_logs(auth, dataset_id, params=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/ingestions"
- res = requests.get(url=url, headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def get_ingestion_log(auth, dataset_id, log_id, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/ingestions/{log_id}"
- res = requests.get(url=url, headers=headers, auth=auth)
- return res.json()
-
-
-def run_index(auth, dataset_id, index_type, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/index"
- params = {"type": index_type}
- res = requests.post(url=url, headers=headers, auth=auth, json=payload, params=params)
- return res.json()
-
-
-def trace_index(auth, dataset_id, index_type, params=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/index"
- all_params = {"type": index_type}
- if params:
- all_params.update(params)
- res = requests.get(url=url, headers=headers, auth=auth, params=all_params)
- return res.json()
-
-
-def delete_index(auth, dataset_id, index_type, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/{index_type}"
- res = requests.delete(url=url, headers=headers, auth=auth)
- return res.json()
-
-
-def list_tags(auth, dataset_id, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/tags"
- res = requests.get(url=url, headers=headers, auth=auth)
- return res.json()
-
-
-def aggregate_tags(auth, dataset_ids, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/tags/aggregation"
- res = requests.get(url=url, headers=headers, auth=auth, params={"dataset_ids": ",".join(dataset_ids)})
- return res.json()
-
-
-def delete_tags(auth, dataset_id, tags, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/tags"
- res = requests.delete(url=url, headers=headers, auth=auth, json={"tags": tags})
- return res.json()
-
-
-def rename_tag(auth, dataset_id, from_tag, to_tag, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/tags"
- res = requests.put(url=url, headers=headers, auth=auth, json={"from_tag": from_tag, "to_tag": to_tag})
- return res.json()
-
-
-def get_flattened_metadata(auth, dataset_ids, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/metadata/flattened"
- res = requests.get(url=url, headers=headers, auth=auth, params={"dataset_ids": ",".join(dataset_ids)})
- return res.json()
-
-
-def search_dataset(auth, dataset_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/search"
- res = requests.post(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
diff --git a/test/testcases/test_http_api/conftest.py b/test/testcases/test_http_api/conftest.py
deleted file mode 100644
index 9fdb2803a1..0000000000
--- a/test/testcases/test_http_api/conftest.py
+++ /dev/null
@@ -1,164 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from time import sleep
-
-import pytest
-from common import (
- batch_add_chunks,
- batch_create_chat_assistants,
- batch_create_datasets,
- bulk_upload_documents,
- delete_all_chat_assistants,
- delete_all_datasets,
- delete_all_sessions_with_chat_assistant,
- list_documents,
- parse_documents,
-)
-from libs.auth import RAGFlowHttpApiAuth
-from utils import wait_for
-from utils.file_utils import (
- create_docx_file,
- create_eml_file,
- create_excel_file,
- create_html_file,
- create_image_file,
- create_json_file,
- create_md_file,
- create_pdf_file,
- create_ppt_file,
- create_txt_file,
-)
-
-
-@wait_for(200, 1, "Document parsing timeout")
-def condition(_auth, _dataset_id):
- res = list_documents(_auth, _dataset_id)
- for doc in res["data"]["docs"]:
- if doc["run"] != "DONE":
- return False
- return True
-
-
-def generate_test_files(request, tmp_path):
- file_creators = {
- "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
- "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
- "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
- "image": (tmp_path / "ragflow_test.png", create_image_file),
- "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
- "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
- "md": (tmp_path / "ragflow_test.md", create_md_file),
- "json": (tmp_path / "ragflow_test.json", create_json_file),
- "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
- "html": (tmp_path / "ragflow_test.html", create_html_file),
- }
-
- files = {}
- for file_type, (file_path, creator_func) in file_creators.items():
- if request.param in ["", file_type]:
- creator_func(file_path)
- files[file_type] = file_path
- return files
-
-
[email protected](scope="class")
-def ragflow_tmp_dir(request, tmp_path_factory):
- class_name = request.cls.__name__
- return tmp_path_factory.mktemp(class_name)
-
-
[email protected](scope="session")
-def HttpApiAuth(token):
- return RAGFlowHttpApiAuth(token)
-
-
[email protected](scope="function")
-def clear_datasets(request, HttpApiAuth):
- def cleanup():
- delete_all_datasets(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
-
[email protected](scope="function")
-def clear_chat_assistants(request, HttpApiAuth):
- def cleanup():
- delete_all_chat_assistants(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
-
[email protected](scope="function")
-def clear_session_with_chat_assistants(request, HttpApiAuth, add_chat_assistants):
- def cleanup():
- for chat_assistant_id in chat_assistant_ids:
- delete_all_sessions_with_chat_assistant(HttpApiAuth, chat_assistant_id)
-
- request.addfinalizer(cleanup)
-
- _, _, chat_assistant_ids = add_chat_assistants
-
-
[email protected](scope="class")
-def add_dataset(request, HttpApiAuth):
- def cleanup():
- delete_all_datasets(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
- dataset_ids = batch_create_datasets(HttpApiAuth, 1)
- return dataset_ids[0]
-
-
[email protected](scope="function")
-def add_dataset_func(request, HttpApiAuth):
- def cleanup():
- delete_all_datasets(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
- return batch_create_datasets(HttpApiAuth, 1)[0]
-
-
[email protected](scope="class")
-def add_document(HttpApiAuth, add_dataset, ragflow_tmp_dir):
- dataset_id = add_dataset
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, 1, ragflow_tmp_dir)
- return dataset_id, document_ids[0]
-
-
[email protected](scope="class")
-def add_chunks(HttpApiAuth, add_document):
- dataset_id, document_id = add_document
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": [document_id]})
- condition(HttpApiAuth, dataset_id)
- chunk_ids = batch_add_chunks(HttpApiAuth, dataset_id, document_id, 4)
- sleep(1) # issues/6487
- return dataset_id, document_id, chunk_ids
-
-
[email protected](scope="class")
-def add_chat_assistants(request, HttpApiAuth, add_document):
- def cleanup():
- delete_all_chat_assistants(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
- dataset_id, document_id = add_document
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": [document_id]})
- condition(HttpApiAuth, dataset_id)
- return dataset_id, document_id, batch_create_chat_assistants(HttpApiAuth, 5)
diff --git a/test/testcases/test_http_api/test_chat_assistant_management/conftest.py b/test/testcases/test_http_api/test_chat_assistant_management/conftest.py
deleted file mode 100644
index 330732db6d..0000000000
--- a/test/testcases/test_http_api/test_chat_assistant_management/conftest.py
+++ /dev/null
@@ -1,49 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import batch_create_chat_assistants, delete_all_chat_assistants, get_chat_assistant, list_documents, parse_documents
-from utils import wait_for
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_auth, _dataset_id):
- res = list_documents(_auth, _dataset_id)
- for doc in res["data"]["docs"]:
- if doc["run"] != "DONE":
- return False
- return True
-
-
[email protected](scope="function")
-def add_chat_assistants_func(request, HttpApiAuth, add_document):
- def cleanup():
- delete_all_chat_assistants(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
- dataset_id, document_id = add_document
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": [document_id]})
- condition(HttpApiAuth, dataset_id)
- return dataset_id, document_id, batch_create_chat_assistants(HttpApiAuth, 5)
-
-
[email protected](scope="function")
-def chat_assistant_llm_model_type(HttpApiAuth, add_chat_assistants_func):
- _, _, chat_assistant_ids = add_chat_assistants_func
- res = get_chat_assistant(HttpApiAuth, chat_assistant_ids[0])
- if res.get("code") == 0 and res.get("data"):
- return res["data"].get("llm_setting", {}).get("model_type", "chat")
- return "chat"
diff --git a/test/testcases/test_http_api/test_chat_assistant_management/test_chat_sdk_routes_unit.py b/test/testcases/test_http_api/test_chat_assistant_management/test_chat_sdk_routes_unit.py
deleted file mode 100644
index 78a067bf69..0000000000
--- a/test/testcases/test_http_api/test_chat_assistant_management/test_chat_sdk_routes_unit.py
+++ /dev/null
@@ -1,1167 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import sys
-from copy import deepcopy
-from enum import Enum
-from functools import wraps
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _DummyArgs(dict):
- def get(self, key, default=None):
- return super().get(key, default)
-
- def getlist(self, key):
- value = self.get(key, [])
- if value is None:
- return []
- if isinstance(value, list):
- return value
- return [value]
-
-
-class _StubHeaders:
- def __init__(self):
- self._items = []
-
- def add_header(self, key, value):
- self._items.append((key, value))
-
- def get(self, key, default=None):
- for existing_key, value in reversed(self._items):
- if existing_key == key:
- return value
- return default
-
-
-class _StubResponse:
- def __init__(self, body=None, mimetype=None, content_type=None):
- self.body = body
- self.mimetype = mimetype
- self.content_type = content_type
- self.headers = _StubHeaders()
-
-
-class _DummyUploadFile:
- def __init__(self, filename):
- self.filename = filename
- self.saved_path = None
-
- async def save(self, path):
- self.saved_path = path
-
-
-def _passthrough_login_required(func):
- @wraps(func)
- async def _wrapper(*args, **kwargs):
- return await func(*args, **kwargs)
-
- return _wrapper
-
-
-class _DummyKB:
- def __init__(self, kid="kb-1", embd_id="embd@factory", chunk_num=1, name="Dataset A", status="1"):
- self.id = kid
- self.embd_id = embd_id
- self.chunk_num = chunk_num
- self.name = name
- self.status = status
-
-
-class _DummyDialogRecord:
- def __init__(self, data=None):
- self._data = data or {
- "id": "chat-1",
- "name": "chat-name",
- "description": "desc",
- "icon": "icon.png",
- "kb_ids": ["kb-1"],
- "llm_id": "glm-4",
- "llm_setting": {"temperature": 0.1},
- "prompt_config": {
- "system": "Answer with {knowledge}",
- "parameters": [{"key": "knowledge", "optional": False}],
- "prologue": "hello",
- "quote": True,
- },
- "similarity_threshold": 0.2,
- "vector_similarity_weight": 0.3,
- "top_n": 6,
- "top_k": 1024,
- "rerank_id": "",
- "meta_data_filter": {},
- "tenant_id": "tenant-1",
- }
-
- def to_dict(self):
- return deepcopy(self._data)
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-async def _collect_stream(body):
- items = []
- if hasattr(body, "__aiter__"):
- async for item in body:
- if isinstance(item, bytes):
- item = item.decode("utf-8")
- items.append(item)
- else:
- for item in body:
- if isinstance(item, bytes):
- item = item.decode("utf-8")
- items.append(item)
- return items
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_chat_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
- module_name = "test_chat_restful_routes_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "chat_api.py"
-
- quart_mod = ModuleType("quart")
- quart_mod.request = SimpleNamespace(args=_DummyArgs())
- quart_mod.Response = _StubResponse
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_pkg = ModuleType("api.apps")
- apps_pkg.__path__ = [str(repo_root / "api" / "apps")]
- apps_pkg.current_user = SimpleNamespace(id="tenant-1")
- apps_pkg.login_required = _passthrough_login_required
- monkeypatch.setitem(sys.modules, "api.apps", apps_pkg)
- api_pkg.apps = apps_pkg
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- common_constants_mod = ModuleType("common.constants")
-
- class _StubLLMType(str, Enum):
- CHAT = "chat"
- VISION = "vision"
- RERANK = "rerank"
- ASR = "asr"
- TTS = "tts"
-
- class _StubRetCode(int, Enum):
- SUCCESS = 0
- DATA_ERROR = 102
- AUTHENTICATION_ERROR = 109
-
- class _StubStatusEnum(str, Enum):
- VALID = "1"
- INVALID = "0"
-
- common_constants_mod.LLMType = _StubLLMType
- common_constants_mod.RetCode = _StubRetCode
- common_constants_mod.StatusEnum = _StubStatusEnum
- # Import pure-Python constants from the real module (no heavy deps)
- from common.constants import MAXIMUM_PAGE_NUMBER as _MPN, MAXIMUM_TASK_PAGE_NUMBER as _MTPN
-
- common_constants_mod.MAXIMUM_PAGE_NUMBER = _MPN
- common_constants_mod.MAXIMUM_TASK_PAGE_NUMBER = _MTPN
- monkeypatch.setitem(sys.modules, "common.constants", common_constants_mod)
-
- misc_utils_mod = ModuleType("common.misc_utils")
- misc_utils_mod.get_uuid = lambda: "generated-chat-id"
-
- async def _thread_pool_exec(func, *args, **kwargs):
- return func(*args, **kwargs)
-
- misc_utils_mod.thread_pool_exec = _thread_pool_exec
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
-
- dialog_service_mod = ModuleType("api.db.services.dialog_service")
-
- class _StubDialogService:
- model = SimpleNamespace(
- _meta=SimpleNamespace(
- fields={
- "id": None,
- "tenant_id": None,
- "name": None,
- "description": None,
- "icon": None,
- "kb_ids": None,
- "llm_id": None,
- "llm_setting": None,
- "prompt_config": None,
- "similarity_threshold": None,
- "vector_similarity_weight": None,
- "top_n": None,
- "top_k": None,
- "rerank_id": None,
- "meta_data_filter": None,
- "created_by": None,
- "create_time": None,
- "create_date": None,
- "update_time": None,
- "update_date": None,
- "status": None,
- }
- )
- )
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def get_by_id(_chat_id):
- return False, None
-
- @staticmethod
- def update_by_id(_chat_id, _payload):
- return True
-
- @staticmethod
- def get_by_tenant_ids(*_args, **_kwargs):
- return [], 0
-
- dialog_service_mod.DialogService = _StubDialogService
- dialog_service_mod.async_ask = lambda *_args, **_kwargs: None
- dialog_service_mod.async_chat = lambda *_args, **_kwargs: None
- dialog_service_mod.gen_mindmap = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.db.services.dialog_service", dialog_service_mod)
-
- conversation_service_mod = ModuleType("api.db.services.conversation_service")
-
- class _StubConversationService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_list(*_args, **_kwargs):
- return []
-
- @staticmethod
- def get_by_id(_session_id):
- return False, None
-
- @staticmethod
- def update_by_id(_session_id, _payload):
- return True
-
- @staticmethod
- def delete_by_id(_session_id):
- return True
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- conversation_service_mod.ConversationService = _StubConversationService
- conversation_service_mod.structure_answer = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.conversation_service", conversation_service_mod)
-
- kb_service_mod = ModuleType("api.db.services.knowledgebase_service")
-
- class _StubKnowledgebaseService:
- @staticmethod
- def accessible(**_kwargs):
- return []
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_by_id(_kb_id):
- return False, None
-
- kb_service_mod.KnowledgebaseService = _StubKnowledgebaseService
- kb_service_mod.validate_dataset_embedding_models = lambda _kbs: None
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_service_mod)
-
- tenant_llm_service_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _StubTenantLLMService:
- @staticmethod
- def split_model_name_and_factory(model_name):
- if model_name and "@" in model_name:
- return tuple(model_name.split("@", 1))
- return model_name, None
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_api_key(*_args, **_kwargs):
- return SimpleNamespace(id=1)
-
- tenant_llm_service_mod.TenantLLMService = _StubTenantLLMService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_service_mod)
-
- llm_service_mod = ModuleType("api.db.services.llm_service")
-
- class _StubLLMBundle:
- def __init__(self, *_args, **_kwargs):
- pass
-
- llm_service_mod.LLMBundle = _StubLLMBundle
- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)
-
- search_service_mod = ModuleType("api.db.services.search_service")
- search_service_mod.SearchService = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "api.db.services.search_service", search_service_mod)
-
- tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
- tenant_model_service_mod.get_model_config_from_provider_instance = lambda *_args, **_kwargs: {}
- tenant_model_service_mod.resolve_model_config = lambda *_args, **_kwargs: {}
- tenant_model_service_mod.get_tenant_default_model_by_type = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
-
- user_service_mod = ModuleType("api.db.services.user_service")
-
- class _StubTenantService:
- @staticmethod
- def get_by_id(_tenant_id):
- return True, SimpleNamespace(llm_id="glm-4")
-
- class _StubUserTenantService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- user_service_mod.UserService = type("UserService", (), {})
- user_service_mod.TenantService = _StubTenantService
- user_service_mod.UserTenantService = _StubUserTenantService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- chunk_feedback_service_mod = ModuleType("api.db.services.chunk_feedback_service")
-
- class _StubChunkFeedbackService:
- @staticmethod
- def apply_feedback(**_kwargs):
- return {"success_count": 0, "fail_count": 0, "chunk_ids": []}
-
- chunk_feedback_service_mod.ChunkFeedbackService = _StubChunkFeedbackService
- monkeypatch.setitem(sys.modules, "api.db.services.chunk_feedback_service", chunk_feedback_service_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- def _check_duplicate_ids(ids, label):
- counts = {}
- for item in ids or []:
- counts[item] = counts.get(item, 0) + 1
- duplicate_messages = [f"Duplicate {label} ids: {item}" for item, count in counts.items() if count > 1]
- return list(set(ids or [])), duplicate_messages
-
- api_utils_mod.check_duplicate_ids = _check_duplicate_ids
- api_utils_mod.get_data_error_result = lambda message="": {"code": 102, "data": None, "message": message}
- api_utils_mod.get_json_result = lambda data=None, message="", code=0: {"code": code, "data": data, "message": message}
- api_utils_mod.get_request_json = lambda: _AwaitableValue({})
- api_utils_mod.server_error_response = lambda ex: {"code": 500, "data": None, "message": str(ex)}
- api_utils_mod.validate_request = lambda *_args, **_kwargs: lambda func: func
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = [str(repo_root / "rag")]
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
-
- rag_prompts_pkg = ModuleType("rag.prompts")
- rag_prompts_pkg.__path__ = [str(repo_root / "rag" / "prompts")]
- monkeypatch.setitem(sys.modules, "rag.prompts", rag_prompts_pkg)
-
- rag_prompts_generator_mod = ModuleType("rag.prompts.generator")
- rag_prompts_generator_mod.chunks_format = lambda reference: reference.get("chunks", []) if isinstance(reference, dict) else []
- monkeypatch.setitem(sys.modules, "rag.prompts.generator", rag_prompts_generator_mod)
-
- rag_prompts_template_mod = ModuleType("rag.prompts.template")
- rag_prompts_template_mod.load_prompt = lambda *_args, **_kwargs: ""
- monkeypatch.setitem(sys.modules, "rag.prompts.template", rag_prompts_template_mod)
-
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def _set_request_json(monkeypatch, module, payload):
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(deepcopy(payload)))
-
-
-def test_create_chat_uses_direct_chat_fields(monkeypatch):
- module = _load_chat_module(monkeypatch)
- saved = {}
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "name": "chat-a",
- "icon": "icon.png",
- "dataset_ids": ["kb-1"],
- "llm_id": "glm-4",
- "llm_setting": {"temperature": 0.8},
- "prompt_config": {
- "system": "Answer with {knowledge}",
- "parameters": [{"key": "knowledge", "optional": False}],
- "prologue": "Hi",
- },
- "vector_similarity_weight": 0.25,
- },
- )
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [_DummyKB()])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, _DummyKB()))
- monkeypatch.setattr(module.TenantLLMService, "split_model_name_and_factory", lambda model: (model.split("@")[0], "factory"))
- monkeypatch.setattr(module.TenantLLMService, "query", lambda **_kwargs: [SimpleNamespace(id="llm-1")])
-
- def _save(**kwargs):
- saved.update(kwargs)
- return True
-
- monkeypatch.setattr(module.DialogService, "save", _save)
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(saved)))
-
- res = _run(module.create.__wrapped__())
-
- assert res["code"] == 0
- assert saved["kb_ids"] == ["kb-1"]
- assert saved["prompt_config"]["prologue"] == "Hi"
- assert saved["llm_id"] == "glm-4"
- assert saved["llm_setting"]["temperature"] == 0.8
- assert res["data"]["dataset_ids"] == ["kb-1"]
- assert res["data"]["kb_names"] == ["Dataset A"]
- assert "kb_ids" not in res["data"]
- assert "prompt" not in res["data"]
- assert "llm" not in res["data"]
- assert "avatar" not in res["data"]
-
-
-def test_create_chat_blank_name_is_treated_as_missing(monkeypatch):
- module = _load_chat_module(monkeypatch)
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "name": " ",
- "dataset_ids": [],
- },
- )
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
-
- res = _run(module.create.__wrapped__())
-
- assert res["code"] == 102
- assert res["message"] == "`name` is required"
-
-
-def test_create_chat_accepts_provider_scoped_rerank_id(monkeypatch):
- module = _load_chat_module(monkeypatch)
- saved = {}
- query_calls = []
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "name": "chat-a",
- "icon": "icon.png",
- "dataset_ids": ["kb-1"],
- "llm_id": "glm-4@ZHIPU-AI",
- "llm_setting": {"temperature": 0.8},
- "prompt_config": {
- "system": "Answer with {knowledge}",
- "parameters": [{"key": "knowledge", "optional": False}],
- "prologue": "Hi",
- },
- "rerank_id": "custom-reranker@OpenAI",
- "vector_similarity_weight": 0.25,
- },
- )
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4@ZHIPU-AI")))
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [_DummyKB()])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, _DummyKB()))
-
- def _split_model_name_and_factory(model_name):
- return {
- "glm-4@ZHIPU-AI": ("glm-4", "ZHIPU-AI"),
- "custom-reranker@OpenAI": ("custom-reranker", "OpenAI"),
- }.get(model_name, (model_name, None))
-
- def _query(**kwargs):
- query_calls.append(kwargs)
- if kwargs == {
- "tenant_id": "tenant-1",
- "llm_name": "glm-4",
- "llm_factory": "ZHIPU-AI",
- "model_type": "chat",
- }:
- return [SimpleNamespace(id="llm-1")]
- if kwargs == {
- "tenant_id": "tenant-1",
- "llm_name": "custom-reranker",
- "llm_factory": "OpenAI",
- "model_type": "rerank",
- }:
- return [SimpleNamespace(id="rerank-1")]
- return []
-
- monkeypatch.setattr(module.TenantLLMService, "split_model_name_and_factory", _split_model_name_and_factory)
- monkeypatch.setattr(module.TenantLLMService, "query", _query)
-
- def _save(**kwargs):
- saved.update(kwargs)
- return True
-
- monkeypatch.setattr(module.DialogService, "save", _save)
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(saved)))
-
- res = _run(module.create.__wrapped__())
-
- assert res["code"] == 0
- assert saved["rerank_id"] == "custom-reranker@OpenAI"
- assert {
- "tenant_id": "tenant-1",
- "llm_name": "custom-reranker",
- "llm_factory": "OpenAI",
- "model_type": "rerank",
- } in query_calls
-
-
-def test_create_chat_allows_default_knowledge_placeholder_without_sources(monkeypatch):
- module = _load_chat_module(monkeypatch)
- saved = {}
-
- _set_request_json(monkeypatch, module, {"name": "chat-a"})
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.TenantLLMService, "get_api_key", lambda *_args, **_kwargs: SimpleNamespace(id=1))
-
- def _save(**kwargs):
- saved.update(kwargs)
- return True
-
- monkeypatch.setattr(module.DialogService, "save", _save)
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(saved)))
-
- res = _run(module.create.__wrapped__())
-
- assert res["code"] == 0
- assert saved["kb_ids"] == []
- assert saved["prompt_config"]["system"].find("{knowledge}") >= 0
- assert saved["prompt_config"]["parameters"] == [{"key": "knowledge", "optional": False}]
-
-
-def test_create_chat_uses_tenant_default_llm_when_llm_id_is_null(monkeypatch):
- module = _load_chat_module(monkeypatch)
- saved = {}
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "name": "chat-a",
- "dataset_ids": ["kb-1"],
- "llm_id": None,
- "llm_setting": {"temperature": 0.8},
- "prompt_config": {
- "system": "Answer with {knowledge}",
- "parameters": [{"key": "knowledge", "optional": False}],
- },
- },
- )
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [_DummyKB()])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, _DummyKB()))
- monkeypatch.setattr(module.TenantLLMService, "get_api_key", lambda *_args, **_kwargs: SimpleNamespace(id=1))
-
- def _save(**kwargs):
- saved.update(kwargs)
- return True
-
- monkeypatch.setattr(module.DialogService, "save", _save)
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(saved)))
-
- res = _run(module.create.__wrapped__())
-
- assert res["code"] == 0
- assert saved["llm_id"] == "glm-4"
- assert saved["llm_setting"]["temperature"] == 0.8
-
-
-def test_patch_chat_merges_prompt_and_llm_settings(monkeypatch):
- module = _load_chat_module(monkeypatch)
- updated = {}
- existing = _DummyDialogRecord().to_dict()
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "prompt_config": {"prologue": "updated opener"},
- "llm_setting": {"temperature": 0.9},
- },
- )
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [SimpleNamespace(id="chat-1")])
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(existing)))
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
-
- def _update(_chat_id, payload):
- updated.update(payload)
- return True
-
- monkeypatch.setattr(module.DialogService, "update_by_id", _update)
-
- res = _run(module.patch_chat.__wrapped__("chat-1"))
-
- assert res["code"] == 0
- assert updated["prompt_config"]["system"] == "Answer with {knowledge}"
- assert updated["prompt_config"]["prologue"] == "updated opener"
- assert updated["llm_setting"]["temperature"] == 0.9
-
-
-def test_patch_chat_drops_response_only_fields_before_update(monkeypatch):
- module = _load_chat_module(monkeypatch)
- updated = {}
- existing = _DummyDialogRecord().to_dict()
- payload = {
- "name": "renamed-chat",
- "description": existing["description"],
- "icon": existing["icon"],
- "dataset_ids": existing["kb_ids"],
- "kb_names": ["Dataset A"],
- "llm_id": existing["llm_id"],
- "llm_setting": existing["llm_setting"],
- "prompt_config": existing["prompt_config"],
- "similarity_threshold": existing["similarity_threshold"],
- "vector_similarity_weight": existing["vector_similarity_weight"],
- "top_n": existing["top_n"],
- "top_k": existing["top_k"],
- "rerank_id": existing["rerank_id"],
- }
-
- _set_request_json(monkeypatch, module, payload)
- monkeypatch.setattr(
- module.DialogService,
- "query",
- lambda **kwargs: [] if "name" in kwargs else [SimpleNamespace(id="chat-1")],
- )
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(existing)))
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [_DummyKB()])
- monkeypatch.setattr(module.TenantLLMService, "split_model_name_and_factory", lambda model: (model.split("@")[0], "factory"))
- monkeypatch.setattr(module.TenantLLMService, "query", lambda **_kwargs: [SimpleNamespace(id="llm-1")])
-
- def _update(_chat_id, req):
- updated.update(req)
- return True
-
- monkeypatch.setattr(module.DialogService, "update_by_id", _update)
-
- res = _run(module.patch_chat.__wrapped__("chat-1"))
-
- assert res["code"] == 0
- assert updated["name"] == "renamed-chat"
- assert "kb_names" not in updated
-
-
-def test_update_chat_allows_knowledge_placeholder_without_sources(monkeypatch):
- module = _load_chat_module(monkeypatch)
- existing = _DummyDialogRecord().to_dict()
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "name": "chat-name",
- "description": "desc",
- "icon": "icon.png",
- "dataset_ids": [],
- "llm_id": "glm-4",
- "llm_setting": {"temperature": 0.1},
- "prompt_config": {
- "system": "Answer with {knowledge}",
- "parameters": [{"key": "knowledge", "optional": False}],
- "prologue": "hello",
- "quote": True,
- },
- "similarity_threshold": 0.2,
- "vector_similarity_weight": 0.3,
- "top_n": 6,
- "top_k": 1024,
- "rerank_id": "",
- },
- )
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [SimpleNamespace(id="chat-1")])
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(existing)))
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
- monkeypatch.setattr(module.TenantLLMService, "split_model_name_and_factory", lambda model: (model.split("@")[0], "factory"))
- monkeypatch.setattr(module.TenantLLMService, "query", lambda **_kwargs: [SimpleNamespace(id="llm-1")])
- updated = {}
-
- def _update(_chat_id, payload):
- updated.update(payload)
- return True
-
- monkeypatch.setattr(module.DialogService, "update_by_id", _update)
-
- res = _run(module.update_chat.__wrapped__("chat-1"))
-
- assert res["code"] == 0
- assert updated["prompt_config"]["system"] == "Answer with {knowledge}"
-
-
-def test_list_chats_returns_old_business_fields(monkeypatch):
- module = _load_chat_module(monkeypatch)
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=SimpleNamespace(
- get=lambda key, default=None: {
- "keywords": "",
- "page": 1,
- "page_size": 20,
- "orderby": "create_time",
- "desc": "true",
- }.get(key, default),
- getlist=lambda _key: [],
- )
- ),
- )
- monkeypatch.setattr(
- module.DialogService,
- "get_by_tenant_ids",
- lambda *_args, **_kwargs: (
- [_DummyDialogRecord().to_dict()],
- 1,
- ),
- )
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, _DummyKB()))
-
- res = _run(module.list_chats.__wrapped__())
-
- assert res["code"] == 0
- chat = res["data"]["chats"][0]
- assert chat["icon"] == "icon.png"
- assert chat["dataset_ids"] == ["kb-1"]
- assert chat["kb_names"] == ["Dataset A"]
- assert "kb_ids" not in chat
- assert chat["prompt_config"]["prologue"] == "hello"
- assert "dataset_names" not in chat
- assert "prompt" not in chat
- assert "llm" not in chat
-
-
-def test_list_chats_keeps_zero_pagination_semantics(monkeypatch):
- module = _load_chat_module(monkeypatch)
- calls = []
-
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=SimpleNamespace(
- get=lambda key, default=None: {
- "keywords": "",
- "page": 0,
- "page_size": 0,
- "orderby": "create_time",
- "desc": "true",
- }.get(key, default),
- getlist=lambda _key: [],
- )
- ),
- )
-
- def _get_by_tenant_ids(_owner_ids, _user_id, page_number, items_per_page, *_args, **_kwargs):
- calls.append((page_number, items_per_page))
- return ([_DummyDialogRecord().to_dict()], 1)
-
- monkeypatch.setattr(module.DialogService, "get_by_tenant_ids", _get_by_tenant_ids)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, _DummyKB()))
-
- res = _run(module.list_chats.__wrapped__())
-
- assert res["code"] == 0
- assert calls[-1] == (0, 0)
- assert len(res["data"]["chats"]) == 1
-
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=SimpleNamespace(
- get=lambda key, default=None: {
- "keywords": "",
- "page": 0,
- "page_size": 2,
- "orderby": "create_time",
- "desc": "true",
- }.get(key, default),
- getlist=lambda _key: [],
- )
- ),
- )
-
- res = _run(module.list_chats.__wrapped__())
-
- assert res["code"] == 0
- assert calls[-1] == (0, 2)
- assert len(res["data"]["chats"]) == 1
-
-
-def test_chat_session_create_and_update_guard_matrix_unit(monkeypatch):
- module = _load_chat_module(monkeypatch)
-
- _set_request_json(monkeypatch, module, {"name": "session"})
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
- res = _run(module.create_session.__wrapped__("chat-1"))
- assert res["message"] == "no authorization"
-
- dia = SimpleNamespace(prompt_config={"prologue": "hello"})
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [dia])
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, dia))
- monkeypatch.setattr(module.ConversationService, "save", lambda **_kwargs: None)
- monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (False, None))
- res = _run(module.create_session.__wrapped__("chat-1"))
- assert "Fail to create a session" in res["message"]
-
- _set_request_json(monkeypatch, module, {})
- monkeypatch.setattr(module.ConversationService, "query", lambda **_kwargs: [])
- res = _run(module.update_session.__wrapped__("chat-1", "session-1"))
- assert res["message"] == "Session not found!"
-
- monkeypatch.setattr(module.ConversationService, "query", lambda **_kwargs: [SimpleNamespace(id="session-1")])
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
- res = _run(module.update_session.__wrapped__("chat-1", "session-1"))
- assert res["message"] == "no authorization"
-
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [SimpleNamespace(id="chat-1")])
- _set_request_json(monkeypatch, module, {"message": []})
- res = _run(module.update_session.__wrapped__("chat-1", "session-1"))
- assert "`messages` cannot be changed" in res["message"]
-
- _set_request_json(monkeypatch, module, {"reference": []})
- res = _run(module.update_session.__wrapped__("chat-1", "session-1"))
- assert "`reference` cannot be changed" in res["message"]
-
- _set_request_json(monkeypatch, module, {"name": ""})
- res = _run(module.update_session.__wrapped__("chat-1", "session-1"))
- assert "`name` can not be empty" in res["message"]
-
- _set_request_json(monkeypatch, module, {"name": "renamed"})
- monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_args, **_kwargs: False)
- res = _run(module.update_session.__wrapped__("chat-1", "session-1"))
- assert res["message"] == "Session not found!"
-
-
-def test_chat_session_list_projection_unit(monkeypatch):
- module = _load_chat_module(monkeypatch)
-
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=SimpleNamespace(
- get=lambda key, default=None: {
- "page": 1,
- "page_size": 30,
- "orderby": "create_time",
- "desc": "true",
- "id": None,
- "name": None,
- "user_id": None,
- }.get(key, default)
- )
- ),
- )
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [SimpleNamespace(id="chat-1")])
- monkeypatch.setattr(
- module.ConversationService,
- "get_list",
- lambda *_args, **_kwargs: [
- {
- "id": "session-1",
- "dialog_id": "chat-1",
- "message": [{"role": "assistant", "content": "hello"}],
- "reference": [],
- }
- ],
- )
-
- res = _run(module.list_sessions.__wrapped__("chat-1"))
- assert res["data"][0]["chat_id"] == "chat-1"
- assert res["data"][0]["messages"][0]["content"] == "hello"
-
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=SimpleNamespace(
- get=lambda key, default=None: {
- "page": 1,
- "page_size": 0,
- "orderby": "create_time",
- "desc": "true",
- "id": None,
- "name": None,
- "user_id": None,
- }.get(key, default)
- )
- ),
- )
- res = _run(module.list_sessions.__wrapped__("chat-1"))
- assert res["data"] == []
-
-
-def test_chat_session_delete_routes_partial_duplicate_unit(monkeypatch):
- module = _load_chat_module(monkeypatch)
-
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [SimpleNamespace(id="chat-1")])
- _set_request_json(monkeypatch, module, {})
- res = _run(module.delete_sessions.__wrapped__("chat-1"))
- assert res["code"] == 0
-
- monkeypatch.setattr(module.ConversationService, "delete_by_id", lambda *_args, **_kwargs: True)
-
- def _conversation_query(**kwargs):
- if "dialog_id" in kwargs and "id" not in kwargs:
- return [SimpleNamespace(id="seed")]
- if kwargs.get("id") == "ok":
- return [SimpleNamespace(id="ok")]
- return []
-
- monkeypatch.setattr(module.ConversationService, "query", _conversation_query)
-
- _set_request_json(monkeypatch, module, {"ids": ["ok", "bad"]})
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- res = _run(module.delete_sessions.__wrapped__("chat-1"))
- assert res["code"] == 0
- assert res["data"]["success_count"] == 1
- assert res["data"]["errors"] == ["The chat doesn't own the session bad"]
-
- _set_request_json(monkeypatch, module, {"ids": ["bad"]})
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- res = _run(module.delete_sessions.__wrapped__("chat-1"))
- assert res["message"] == "The chat doesn't own the session bad"
-
- _set_request_json(monkeypatch, module, {"ids": ["ok", "ok"]})
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (["ok"], ["Duplicate session ids: ok"]))
- res = _run(module.delete_sessions.__wrapped__("chat-1"))
- assert res["code"] == 0
- assert res["data"]["success_count"] == 1
- assert res["data"]["errors"] == ["Duplicate session ids: ok"]
-
-
-def test_chat_audio_transcription_routes_unit(monkeypatch):
- module = _load_chat_module(monkeypatch)
- monkeypatch.setattr(module, "Response", _StubResponse)
- monkeypatch.setattr(module.tempfile, "mkstemp", lambda suffix: (11, f"/tmp/audio{suffix}"))
- monkeypatch.setattr(module.os, "close", lambda _fd: None)
-
- def _set_request(form, files):
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(form=_AwaitableValue(form), files=_AwaitableValue(files)),
- )
-
- _set_request({"stream": "false"}, {})
- res = _run(module.transcription.__wrapped__())
- assert "Missing 'file' in multipart form-data" in res["message"]
-
- _set_request({"stream": "false"}, {"file": _DummyUploadFile("bad.txt")})
- res = _run(module.transcription.__wrapped__())
- assert "Unsupported audio format: .txt" in res["message"]
-
- _set_request({"stream": "false"}, {"file": _DummyUploadFile("audio.wav")})
- monkeypatch.setattr(
- module,
- "get_tenant_default_model_by_type",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(LookupError("Tenant not found!")),
- )
- res = _run(module.transcription.__wrapped__())
- assert res["message"] == "Tenant not found!"
-
- _set_request({"stream": "false"}, {"file": _DummyUploadFile("audio.wav")})
- monkeypatch.setattr(
- module,
- "get_tenant_default_model_by_type",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(Exception("No default ASR model is set")),
- )
- res = _run(module.transcription.__wrapped__())
- assert res["message"] == "No default ASR model is set"
-
- class _SyncASR:
- def transcription(self, _path):
- return "transcribed text"
-
- def stream_transcription(self, _path):
- return []
-
- _set_request({"stream": "false"}, {"file": _DummyUploadFile("audio.wav")})
- monkeypatch.setattr(module, "get_tenant_default_model_by_type", lambda *_args, **_kwargs: {"llm_name": "asr-x"})
- monkeypatch.setattr(module, "LLMBundle", lambda *_args, **_kwargs: _SyncASR())
- monkeypatch.setattr(module.os, "remove", lambda _path: (_ for _ in ()).throw(RuntimeError("cleanup fail")))
- res = _run(module.transcription.__wrapped__())
- assert res["code"] == 0
- assert res["data"]["text"] == "transcribed text"
-
- class _StreamASR:
- def transcription(self, _path):
- return ""
-
- def stream_transcription(self, _path):
- yield {"event": "partial", "text": "hello"}
-
- _set_request({"stream": "true"}, {"file": _DummyUploadFile("audio.wav")})
- monkeypatch.setattr(module, "LLMBundle", lambda *_args, **_kwargs: _StreamASR())
- monkeypatch.setattr(module.os, "remove", lambda _path: None)
- resp = _run(module.transcription.__wrapped__())
- assert isinstance(resp, _StubResponse)
- assert resp.content_type == "text/event-stream"
- chunks = _run(_collect_stream(resp.body))
- assert any('"event": "partial"' in chunk for chunk in chunks)
-
- class _ErrorASR:
- def transcription(self, _path):
- return ""
-
- def stream_transcription(self, _path):
- raise RuntimeError("stream asr boom")
-
- _set_request({"stream": "true"}, {"file": _DummyUploadFile("audio.wav")})
- monkeypatch.setattr(module, "LLMBundle", lambda *_args, **_kwargs: _ErrorASR())
- monkeypatch.setattr(module.os, "remove", lambda _path: (_ for _ in ()).throw(RuntimeError("cleanup boom")))
- resp = _run(module.transcription.__wrapped__())
- chunks = _run(_collect_stream(resp.body))
- assert any("stream asr boom" in chunk for chunk in chunks)
-
-
-def test_chat_audio_speech_routes_unit(monkeypatch):
- module = _load_chat_module(monkeypatch)
- monkeypatch.setattr(module, "Response", _StubResponse)
- _set_request_json(monkeypatch, module, {"text": "A。B"})
-
- monkeypatch.setattr(
- module,
- "get_tenant_default_model_by_type",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(LookupError("Tenant not found!")),
- )
- res = _run(module.tts.__wrapped__())
- assert res["message"] == "Tenant not found!"
-
- monkeypatch.setattr(
- module,
- "get_tenant_default_model_by_type",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(Exception("No default TTS model is set")),
- )
- res = _run(module.tts.__wrapped__())
- assert res["message"] == "No default TTS model is set"
-
- class _TTSOk:
- def tts(self, txt):
- if not txt:
- return []
- yield f"chunk-{txt}".encode("utf-8")
-
- monkeypatch.setattr(module, "get_tenant_default_model_by_type", lambda *_args, **_kwargs: {"llm_name": "tts-x"})
- monkeypatch.setattr(module, "LLMBundle", lambda *_args, **_kwargs: _TTSOk())
- resp = _run(module.tts.__wrapped__())
- assert resp.mimetype == "audio/mpeg"
- assert resp.headers.get("Cache-Control") == "no-cache"
- assert resp.headers.get("Connection") == "keep-alive"
- assert resp.headers.get("X-Accel-Buffering") == "no"
- chunks = _run(_collect_stream(resp.body))
- assert any("chunk-A" in chunk for chunk in chunks)
- assert any("chunk-B" in chunk for chunk in chunks)
-
- class _TTSErr:
- def tts(self, _txt):
- raise RuntimeError("tts boom")
-
- monkeypatch.setattr(module, "LLMBundle", lambda *_args, **_kwargs: _TTSErr())
- resp = _run(module.tts.__wrapped__())
- chunks = _run(_collect_stream(resp.body))
- assert any('"code": 500' in chunk and "**ERROR**: tts boom" in chunk for chunk in chunks)
diff --git a/test/testcases/test_http_api/test_chat_assistant_management/test_create_chat_assistant.py b/test/testcases/test_http_api/test_chat_assistant_management/test_create_chat_assistant.py
deleted file mode 100644
index b49e8caf34..0000000000
--- a/test/testcases/test_http_api/test_chat_assistant_management/test_create_chat_assistant.py
+++ /dev/null
@@ -1,292 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import pytest
-from common import create_chat_assistant
-from configs import CHAT_ASSISTANT_NAME_LIMIT, INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = create_chat_assistant(invalid_auth)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
[email protected]("clear_chat_assistants")
-class TestChatAssistantCreate:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"name": "valid_name"}, 0, ""),
- pytest.param({"name": "a" * (CHAT_ASSISTANT_NAME_LIMIT + 1)}, 102, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": 1}, 100, "", marks=pytest.mark.skip(reason="issues/")),
- ({"name": ""}, 102, "`name` is required"),
- ({"name": "duplicated_name"}, 102, "duplicated chat name in creating chat"),
- ({"name": "case insensitive"}, 102, "duplicated chat name in creating chat"),
- ],
- )
- def test_name(self, HttpApiAuth, add_chunks, payload, expected_code, expected_message):
- payload["dataset_ids"] = [] # issues/
- if payload["name"] == "duplicated_name":
- create_chat_assistant(HttpApiAuth, payload)
- elif payload["name"] == "case insensitive":
- create_chat_assistant(HttpApiAuth, {"name": payload["name"].upper()})
-
- res = create_chat_assistant(HttpApiAuth, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- assert res["data"]["name"] == payload["name"]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "dataset_ids, expected_code, expected_message",
- [
- ([], 0, ""),
- (lambda r: [r], 0, ""),
- (["invalid_dataset_id"], 102, "You don't own the dataset invalid_dataset_id"),
- ("invalid_dataset_id", 102, "`dataset_ids` should be a list."),
- ],
- )
- def test_dataset_ids(self, HttpApiAuth, add_chunks, dataset_ids, expected_code, expected_message):
- dataset_id, _, _ = add_chunks
- payload = {"name": "ragflow test"}
- if callable(dataset_ids):
- payload["dataset_ids"] = dataset_ids(dataset_id)
- else:
- payload["dataset_ids"] = dataset_ids
-
- res = create_chat_assistant(HttpApiAuth, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- assert res["data"]["name"] == payload["name"]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_avatar(self, HttpApiAuth, tmp_path):
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {"name": "avatar_test", "icon": encode_avatar(fn), "dataset_ids": []}
- res = create_chat_assistant(HttpApiAuth, payload)
- assert res["code"] == 0
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "llm, expected_code, expected_message",
- [
- ({}, 0, ""),
- ({"model_name": "glm-4"}, 0, ""),
- ({"model_name": "unknown"}, 102, "`llm_id` unknown doesn't exist"),
- ({"temperature": 0}, 0, ""),
- ({"temperature": 1}, 0, ""),
- pytest.param({"temperature": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"temperature": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"temperature": "a"}, 0, "", marks=pytest.mark.skip),
- ({"top_p": 0}, 0, ""),
- ({"top_p": 1}, 0, ""),
- pytest.param({"top_p": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_p": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_p": "a"}, 0, "", marks=pytest.mark.skip),
- ({"presence_penalty": 0}, 0, ""),
- ({"presence_penalty": 1}, 0, ""),
- pytest.param({"presence_penalty": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": "a"}, 0, "", marks=pytest.mark.skip),
- ({"frequency_penalty": 0}, 0, ""),
- ({"frequency_penalty": 1}, 0, ""),
- pytest.param({"frequency_penalty": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": "a"}, 0, "", marks=pytest.mark.skip),
- ({"max_token": 0}, 0, ""),
- ({"max_token": 1024}, 0, ""),
- pytest.param({"max_token": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"max_token": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"max_token": "a"}, 0, "", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, 0, "", marks=pytest.mark.skip),
- ],
- )
- def test_llm(self, HttpApiAuth, add_chunks, llm, expected_code, expected_message):
- dataset_id, _, _ = add_chunks
- payload = {"name": "llm_test", "dataset_ids": [dataset_id]}
- if "model_name" in llm:
- payload["llm_id"] = llm["model_name"]
- if any(k != "model_name" for k in llm):
- payload["llm_setting"] = {k: v for k, v in llm.items() if k != "model_name"}
- res = create_chat_assistant(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- if llm:
- for k, v in llm.items():
- if k == "model_name":
- assert res["data"]["llm_id"] == v
- else:
- assert res["data"]["llm_setting"][k] == v
- else:
- assert res["data"]["llm_id"] == "glm-4-flash@ZHIPU-AI"
- assert res["data"]["llm_setting"] == {}
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "prompt, expected_code, expected_message",
- [
- ({}, 0, ""),
- ({"similarity_threshold": 0}, 0, ""),
- ({"similarity_threshold": 1}, 0, ""),
- pytest.param({"similarity_threshold": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"similarity_threshold": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"similarity_threshold": "a"}, 0, "", marks=pytest.mark.skip),
- ({"keywords_similarity_weight": 0}, 0, ""),
- ({"keywords_similarity_weight": 1}, 0, ""),
- pytest.param({"keywords_similarity_weight": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"keywords_similarity_weight": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"keywords_similarity_weight": "a"}, 0, "", marks=pytest.mark.skip),
- ({"variables": []}, 0, ""),
- ({"top_n": 0}, 0, ""),
- ({"top_n": 1}, 0, ""),
- pytest.param({"top_n": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_n": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_n": "a"}, 0, "", marks=pytest.mark.skip),
- ({"empty_response": "Hello World"}, 0, ""),
- ({"empty_response": ""}, 0, ""),
- ({"empty_response": "!@#$%^&*()"}, 0, ""),
- ({"empty_response": "中文测试"}, 0, ""),
- pytest.param({"empty_response": 123}, 0, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": True}, 0, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": " "}, 0, "", marks=pytest.mark.skip),
- ({"opener": "Hello World"}, 0, ""),
- ({"opener": ""}, 0, ""),
- ({"opener": "!@#$%^&*()"}, 0, ""),
- ({"opener": "中文测试"}, 0, ""),
- pytest.param({"opener": 123}, 0, "", marks=pytest.mark.skip),
- pytest.param({"opener": True}, 0, "", marks=pytest.mark.skip),
- pytest.param({"opener": " "}, 0, "", marks=pytest.mark.skip),
- ({"show_quote": True}, 0, ""),
- ({"show_quote": False}, 0, ""),
- ({"prompt": "Hello World {knowledge}"}, 0, ""),
- ({"prompt": "{knowledge}"}, 0, ""),
- ({"prompt": "!@#$%^&*() {knowledge}"}, 0, ""),
- ({"prompt": "中文测试 {knowledge}"}, 0, ""),
- ({"prompt": "Hello World"}, 0, ""),
- ({"prompt": "Hello World", "variables": []}, 0, ""),
- pytest.param({"prompt": 123}, 100, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
- pytest.param({"prompt": True}, 100, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, 0, "", marks=pytest.mark.skip),
- ],
- )
- def test_prompt(self, HttpApiAuth, add_chunks, prompt, expected_code, expected_message):
- dataset_id, _, _ = add_chunks
- payload = {"name": "prompt_test", "dataset_ids": [dataset_id]}
- prompt_config = {}
- for k, v in prompt.items():
- if k == "keywords_similarity_weight":
- payload["vector_similarity_weight"] = 1 - v
- elif k == "variables":
- prompt_config["parameters"] = v
- elif k == "opener":
- prompt_config["prologue"] = v
- elif k == "show_quote":
- prompt_config["quote"] = v
- elif k == "prompt":
- prompt_config["system"] = v
- elif k == "rerank_model":
- payload["rerank_id"] = v
- elif k in {"empty_response"}:
- prompt_config[k] = v
- else:
- payload[k] = v
- if prompt_config:
- payload["prompt_config"] = prompt_config
- res = create_chat_assistant(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- if prompt:
- for k, v in prompt.items():
- if k == "keywords_similarity_weight":
- assert res["data"]["vector_similarity_weight"] == 1 - v
- elif k == "variables":
- expected_parameters = v
- if not v and "{knowledge}" in res["data"]["prompt_config"]["system"]:
- expected_parameters = [{"key": "knowledge", "optional": False}]
- assert res["data"]["prompt_config"]["parameters"] == expected_parameters
- elif k == "opener":
- assert res["data"]["prompt_config"]["prologue"] == v
- elif k == "show_quote":
- assert res["data"]["prompt_config"]["quote"] == v
- elif k == "prompt":
- assert res["data"]["prompt_config"]["system"] == v
- elif k == "rerank_model":
- assert res["data"]["rerank_id"] == v
- elif k == "empty_response":
- assert res["data"]["prompt_config"]["empty_response"] == v
- else:
- assert res["data"][k] == v
- else:
- assert res["data"]["similarity_threshold"] == 0.1
- assert res["data"]["vector_similarity_weight"] == 0.3
- assert res["data"]["top_n"] == 6
- assert res["data"]["rerank_id"] == ""
- assert res["data"]["prompt_config"]["parameters"] == [{"key": "knowledge", "optional": False}]
- assert res["data"]["prompt_config"]["empty_response"] == "Sorry! No relevant content was found in the knowledge base!"
- assert res["data"]["prompt_config"]["prologue"] == "Hi! I'm your assistant. What can I do for you?"
- assert res["data"]["prompt_config"]["quote"] is True
- assert (
- res["data"]["prompt_config"]["system"]
- == 'You are an intelligent assistant. Please summarize the content of the dataset to answer the question. Please list the data in the dataset and answer in detail. When all dataset content is irrelevant to the question, your answer must include the sentence "The answer you are looking for is not found in the dataset!" Answers need to consider chat history.\n Here is the knowledge base:\n {knowledge}\n The above is the knowledge base.'
- )
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- def test_create_additional_guards_p2(self, HttpApiAuth):
- tenant_payload = {"name": "guard-tenant-id", "dataset_ids": [], "tenant_id": "tenant-should-not-pass"}
- res = create_chat_assistant(HttpApiAuth, tenant_payload)
- assert res["code"] == 102
- assert res["message"] == "`tenant_id` must not be provided"
-
- rerank_payload = {
- "name": "guard-rerank-id",
- "dataset_ids": [],
- "rerank_id": "unknown-rerank-model",
- }
- res = create_chat_assistant(HttpApiAuth, rerank_payload)
- assert res["code"] == 102
- assert "`rerank_id` unknown-rerank-model doesn't exist" in res["message"]
-
-
-class TestChatAssistantCreate2:
- @pytest.mark.p2
- def test_unparsed_document(self, HttpApiAuth, add_document):
- dataset_id, _ = add_document
- payload = {"name": "prompt_test", "dataset_ids": [dataset_id]}
- res = create_chat_assistant(HttpApiAuth, payload)
- assert res["code"] == 102
- assert "doesn't own parsed file" in res["message"]
diff --git a/test/testcases/test_http_api/test_chat_assistant_management/test_delete_chat_assistants.py b/test/testcases/test_http_api/test_chat_assistant_management/test_delete_chat_assistants.py
deleted file mode 100644
index 92589965a2..0000000000
--- a/test/testcases/test_http_api/test_chat_assistant_management/test_delete_chat_assistants.py
+++ /dev/null
@@ -1,140 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_create_chat_assistants, delete_chat_assistants, list_chat_assistants
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = delete_chat_assistants(invalid_auth)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestChatAssistantsDelete:
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message, remaining",
- [
- pytest.param(None, 0, "", 5, marks=pytest.mark.p3),
- pytest.param({"ids": []}, 0, "", 5, marks=pytest.mark.p3),
- pytest.param({"ids": ["invalid_id"]}, 102, "Chat(invalid_id) not found.", 5, marks=pytest.mark.p3),
- pytest.param({"ids": ["\n!?。;!?\"'"]}, 102, """Chat(\n!?。;!?"\') not found.""", 5, marks=pytest.mark.p3),
- pytest.param("not json", 100, "AttributeError(\"'str' object has no attribute 'get'\")", 5, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r[:1]}, 0, "", 4, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r}, 0, "", 0, marks=pytest.mark.p1),
- ],
- )
- def test_basic_scenarios(self, HttpApiAuth, add_chat_assistants_func, payload, expected_code, expected_message, remaining):
- _, _, chat_assistant_ids = add_chat_assistants_func
- if callable(payload):
- payload = payload(chat_assistant_ids)
- res = delete_chat_assistants(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if res["code"] != 0:
- assert res["message"] == expected_message
-
- res = list_chat_assistants(HttpApiAuth)
- assert len(res["data"]["chats"]) == remaining
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, marks=pytest.mark.p1),
- pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_delete_partial_invalid_id(self, HttpApiAuth, add_chat_assistants_func, payload):
- _, _, chat_assistant_ids = add_chat_assistants_func
- if callable(payload):
- payload = payload(chat_assistant_ids)
- res = delete_chat_assistants(HttpApiAuth, payload)
- assert res["code"] == 0
- assert res["data"]["errors"][0] == "Chat(invalid_id) not found."
- assert res["data"]["success_count"] == 5
-
- res = list_chat_assistants(HttpApiAuth)
- assert len(res["data"]["chats"]) == 0
-
- @pytest.mark.p3
- def test_repeated_deletion(self, HttpApiAuth, add_chat_assistants_func):
- _, _, chat_assistant_ids = add_chat_assistants_func
- res = delete_chat_assistants(HttpApiAuth, {"ids": chat_assistant_ids})
- assert res["code"] == 0
-
- res = delete_chat_assistants(HttpApiAuth, {"ids": chat_assistant_ids})
- assert res["code"] == 102
- assert "not found" in res["message"]
-
- @pytest.mark.p3
- def test_duplicate_deletion(self, HttpApiAuth, add_chat_assistants_func):
- _, _, chat_assistant_ids = add_chat_assistants_func
- res = delete_chat_assistants(HttpApiAuth, {"ids": chat_assistant_ids + chat_assistant_ids})
- assert res["code"] == 0
- assert "Duplicate chat ids" in res["data"]["errors"][0]
- assert res["data"]["success_count"] == 5
-
- res = list_chat_assistants(HttpApiAuth)
- assert res["code"] == 0
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, HttpApiAuth):
- count = 100
- ids = batch_create_chat_assistants(HttpApiAuth, count)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(delete_chat_assistants, HttpApiAuth, {"ids": ids[i : i + 1]}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_delete_10k(self, HttpApiAuth):
- ids = batch_create_chat_assistants(HttpApiAuth, 1_000)
- res = delete_chat_assistants(HttpApiAuth, {"ids": ids})
- assert res["code"] == 0
-
- res = list_chat_assistants(HttpApiAuth)
- assert len(res["data"]["chats"]) == 0
-
- @pytest.mark.p2
- def test_delete_all_errors_no_success_p2(self, HttpApiAuth, add_chat_assistants_func):
- delete_payload = {"ids": ["missing-1", "missing-2"]}
- res = delete_chat_assistants(HttpApiAuth, delete_payload)
- assert res["code"] == 102
- assert "Chat(missing-1) not found." in res["message"]
- assert "Chat(missing-2) not found." in res["message"]
-
- @pytest.mark.p2
- def test_delete_duplicate_partial_success_p2(self, HttpApiAuth, add_chat_assistants_func):
- _, _, chat_assistant_ids = add_chat_assistants_func
- payload = {"ids": [chat_assistant_ids[0], chat_assistant_ids[0]]}
- res = delete_chat_assistants(HttpApiAuth, payload)
- assert res["code"] == 0
- assert res["data"]["success_count"] == 1
- assert "Duplicate chat ids" in res["data"]["errors"][0]
diff --git a/test/testcases/test_http_api/test_chat_assistant_management/test_list_chat_assistants.py b/test/testcases/test_http_api/test_chat_assistant_management/test_list_chat_assistants.py
deleted file mode 100644
index 08b41e810f..0000000000
--- a/test/testcases/test_http_api/test_chat_assistant_management/test_list_chat_assistants.py
+++ /dev/null
@@ -1,312 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import delete_datasets, get_chat_assistant, list_chat_assistants
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import is_sorted
-
-
-def _chat_list(res):
- return res["data"]["chats"]
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = list_chat_assistants(invalid_auth)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
[email protected]("add_chat_assistants")
-class TestChatAssistantsList:
- @pytest.mark.p1
- def test_default(self, HttpApiAuth):
- res = list_chat_assistants(HttpApiAuth)
- assert res["code"] == 0
- assert len(_chat_list(res)) == 5
- assert res["data"]["total"] == 5
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 0, 5, ""),
- ({"page": 0, "page_size": 2}, 0, 5, ""),
- ({"page": 2, "page_size": 2}, 0, 2, ""),
- ({"page": 3, "page_size": 2}, 0, 1, ""),
- ({"page": "3", "page_size": 2}, 0, 1, ""),
- pytest.param(
- {"page": -1, "page_size": 2},
- 100,
- 0,
- "1064",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page": "a", "page_size": 2},
- 100,
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page(self, HttpApiAuth, params, expected_code, expected_page_size, expected_message):
- res = list_chat_assistants(HttpApiAuth, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(_chat_list(res)) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page_size": None}, 0, 5, ""),
- ({"page_size": 0}, 0, 5, ""),
- ({"page_size": 1}, 0, 5, ""),
- ({"page_size": 6}, 0, 5, ""),
- ({"page_size": "1"}, 0, 5, ""),
- pytest.param(
- {"page_size": -1},
- 100,
- 0,
- "1064",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page_size": "a"},
- 100,
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page_size(
- self,
- HttpApiAuth,
- params,
- expected_code,
- expected_page_size,
- expected_message,
- ):
- res = list_chat_assistants(HttpApiAuth, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(_chat_list(res)) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"orderby": None}, 0, lambda r: is_sorted(_chat_list(r), "create_time", True), ""),
- ({"orderby": "create_time"}, 0, lambda r: is_sorted(_chat_list(r), "create_time", True), ""),
- ({"orderby": "update_time"}, 0, lambda r: is_sorted(_chat_list(r), "update_time", True), ""),
- pytest.param(
- {"orderby": "name", "desc": "False"},
- 0,
- lambda r: is_sorted(_chat_list(r), "name", False),
- "",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"orderby": "unknown"},
- 102,
- 0,
- "orderby should be create_time or update_time",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_orderby(
- self,
- HttpApiAuth,
- params,
- expected_code,
- assertions,
- expected_message,
- ):
- res = list_chat_assistants(HttpApiAuth, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"desc": None}, 0, lambda r: is_sorted(_chat_list(r), "create_time", True), ""),
- ({"desc": "true"}, 0, lambda r: is_sorted(_chat_list(r), "create_time", True), ""),
- ({"desc": "True"}, 0, lambda r: is_sorted(_chat_list(r), "create_time", True), ""),
- ({"desc": True}, 0, lambda r: is_sorted(_chat_list(r), "create_time", True), ""),
- ({"desc": "false"}, 0, lambda r: is_sorted(_chat_list(r), "create_time", False), ""),
- ({"desc": "False"}, 0, lambda r: is_sorted(_chat_list(r), "create_time", False), ""),
- ({"desc": False}, 0, lambda r: is_sorted(_chat_list(r), "create_time", False), ""),
- ({"desc": "False", "orderby": "update_time"}, 0, lambda r: is_sorted(_chat_list(r), "update_time", False), ""),
- pytest.param(
- {"desc": "unknown"},
- 102,
- 0,
- "desc should be true or false",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_desc(
- self,
- HttpApiAuth,
- params,
- expected_code,
- assertions,
- expected_message,
- ):
- res = list_chat_assistants(HttpApiAuth, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_num, expected_message",
- [
- ({"keywords": None}, 0, 5, ""),
- ({"keywords": ""}, 0, 5, ""),
- ({"keywords": "test_chat_assistant_1"}, 0, 1, ""),
- ({"keywords": "unknown"}, 0, 0, ""),
- ],
- )
- def test_keywords(self, HttpApiAuth, params, expected_code, expected_num, expected_message):
- res = list_chat_assistants(HttpApiAuth, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if params["keywords"] in [None, ""]:
- assert len(_chat_list(res)) == expected_num
- else:
- assert len(_chat_list(res)) == expected_num
- if expected_num:
- assert _chat_list(res)[0]["name"] == params["keywords"]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "chat_assistant_id, expected_code, expected_message",
- [
- (lambda r: r[0], 0, ""),
- ("unknown", 109, "no authorization"),
- ],
- )
- def test_get_chat_assistant(
- self,
- HttpApiAuth,
- add_chat_assistants,
- chat_assistant_id,
- expected_code,
- expected_message,
- ):
- _, _, chat_assistant_ids = add_chat_assistants
- chat_id = chat_assistant_id(chat_assistant_ids) if callable(chat_assistant_id) else chat_assistant_id
- res = get_chat_assistant(HttpApiAuth, chat_id)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert res["data"]["id"] == chat_id
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "chat_assistant_id, keywords, expected_code, expected_num, expected_message",
- [
- (lambda r: r[0], "test_chat_assistant_0", 0, 1, ""),
- (lambda r: r[0], "test_chat_assistant_1", 0, 1, ""),
- (lambda r: r[0], "unknown", 0, 0, ""),
- ],
- )
- def test_get_and_keywords_are_separate_lookups(
- self,
- HttpApiAuth,
- add_chat_assistants,
- chat_assistant_id,
- keywords,
- expected_code,
- expected_num,
- expected_message,
- ):
- _, _, chat_assistant_ids = add_chat_assistants
- chat_id = chat_assistant_id(chat_assistant_ids) if callable(chat_assistant_id) else chat_assistant_id
-
- get_res = get_chat_assistant(HttpApiAuth, chat_id)
- list_res = list_chat_assistants(HttpApiAuth, params={"keywords": keywords})
-
- assert get_res["code"] == expected_code
- assert list_res["code"] == expected_code
- if expected_code == 0:
- assert len(_chat_list(list_res)) == expected_num
- else:
- assert get_res["message"] == expected_message
-
- @pytest.mark.p3
- def test_concurrent_list(self, HttpApiAuth):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_chat_assistants, HttpApiAuth) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_invalid_params(self, HttpApiAuth):
- params = {"a": "b"}
- res = list_chat_assistants(HttpApiAuth, params=params)
- assert res["code"] == 0
- assert len(_chat_list(res)) == 5
-
- @pytest.mark.p2
- def test_list_chats_after_deleting_associated_dataset(self, HttpApiAuth, add_chat_assistants):
- dataset_id, _, _ = add_chat_assistants
- res = delete_datasets(HttpApiAuth, {"ids": [dataset_id]})
- assert res["code"] == 0
-
- res = list_chat_assistants(HttpApiAuth)
- assert res["code"] == 0
- assert len(_chat_list(res)) == 5
-
- @pytest.mark.p2
- def test_desc_false_parse_branch_p2(self, HttpApiAuth):
- res = list_chat_assistants(HttpApiAuth, params={"desc": "False", "orderby": "create_time"})
- assert res["code"] == 0
- assert is_sorted(_chat_list(res), "create_time", False)
diff --git a/test/testcases/test_http_api/test_chat_assistant_management/test_update_chat_assistant.py b/test/testcases/test_http_api/test_chat_assistant_management/test_update_chat_assistant.py
deleted file mode 100644
index a6f69191fd..0000000000
--- a/test/testcases/test_http_api/test_chat_assistant_management/test_update_chat_assistant.py
+++ /dev/null
@@ -1,291 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import create_chat_assistant, get_chat_assistant, patch_chat_assistant, update_chat_assistant
-from configs import CHAT_ASSISTANT_NAME_LIMIT, INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = update_chat_assistant(invalid_auth, "chat_assistant_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestChatAssistantUpdate:
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param({"name": "valid_name"}, 0, "", marks=pytest.mark.p1),
- pytest.param({"name": "a" * (CHAT_ASSISTANT_NAME_LIMIT + 1)}, 102, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": 1}, 100, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": ""}, 102, "`name` cannot be empty", marks=pytest.mark.p3),
- pytest.param({"name": "test_chat_assistant_1"}, 102, "duplicated chat name", marks=pytest.mark.p3),
- pytest.param({"name": "TEST_CHAT_ASSISTANT_1"}, 102, "duplicated chat name", marks=pytest.mark.p3),
- ],
- )
- def test_name(self, HttpApiAuth, add_chat_assistants_func, payload, expected_code, expected_message):
- _, _, chat_assistant_ids = add_chat_assistants_func
-
- res = patch_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- res = get_chat_assistant(HttpApiAuth, chat_assistant_ids[0])
- assert res["data"]["name"] == payload.get("name")
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.parametrize(
- "dataset_ids, expected_code, expected_message",
- [
- pytest.param([], 0, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param(lambda r: [r], 0, "", marks=pytest.mark.p1),
- pytest.param(["invalid_dataset_id"], 102, "You don't own the dataset invalid_dataset_id", marks=pytest.mark.p3),
- pytest.param("invalid_dataset_id", 102, "`dataset_ids` should be a list.", marks=pytest.mark.p3),
- ],
- )
- def test_dataset_ids(self, HttpApiAuth, add_chat_assistants_func, dataset_ids, expected_code, expected_message):
- dataset_id, _, chat_assistant_ids = add_chat_assistants_func
- payload = {"name": "ragflow test"}
- if callable(dataset_ids):
- payload["dataset_ids"] = dataset_ids(dataset_id)
- else:
- payload["dataset_ids"] = dataset_ids
-
- res = update_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- res = get_chat_assistant(HttpApiAuth, chat_assistant_ids[0])
- assert res["data"]["name"] == payload.get("name")
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_avatar(self, HttpApiAuth, add_chat_assistants_func, tmp_path):
- dataset_id, _, chat_assistant_ids = add_chat_assistants_func
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {"name": "avatar_test", "icon": encode_avatar(fn), "dataset_ids": [dataset_id]}
- res = update_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
- assert res["code"] == 0
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "llm, expected_code, expected_message",
- [
- ({}, 0, ""),
- ({"llm_id": "glm-4"}, 0, ""),
- ({"llm_id": "unknown"}, 102, "`llm_id` unknown doesn't exist"),
- ({"temperature": 0}, 0, ""),
- ({"temperature": 1}, 0, ""),
- pytest.param({"temperature": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"temperature": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"temperature": "a"}, 0, "", marks=pytest.mark.skip),
- ({"top_p": 0}, 0, ""),
- ({"top_p": 1}, 0, ""),
- pytest.param({"top_p": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_p": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_p": "a"}, 0, "", marks=pytest.mark.skip),
- ({"presence_penalty": 0}, 0, ""),
- ({"presence_penalty": 1}, 0, ""),
- pytest.param({"presence_penalty": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": "a"}, 0, "", marks=pytest.mark.skip),
- ({"frequency_penalty": 0}, 0, ""),
- ({"frequency_penalty": 1}, 0, ""),
- pytest.param({"frequency_penalty": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": "a"}, 0, "", marks=pytest.mark.skip),
- ({"max_token": 0}, 0, ""),
- ({"max_token": 1024}, 0, ""),
- pytest.param({"max_token": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"max_token": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"max_token": "a"}, 0, "", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, 0, "", marks=pytest.mark.skip),
- ],
- )
- def test_llm(self, HttpApiAuth, add_chat_assistants_func, chat_assistant_llm_model_type, llm, expected_code, expected_message):
- dataset_id, _, chat_assistant_ids = add_chat_assistants_func
- llm_setting = {k: v for k, v in llm.items() if k != "llm_id"}
- llm_setting.setdefault("model_type", chat_assistant_llm_model_type)
-
- payload = {"name": "llm_test", "dataset_ids": [dataset_id]}
- if "llm_id" in llm:
- payload["llm_id"] = llm["llm_id"]
- payload["llm_setting"] = llm_setting
-
- res = update_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- res = get_chat_assistant(HttpApiAuth, chat_assistant_ids[0])
- for k, v in llm.items():
- if k == "llm_id":
- assert res["data"]["llm_id"] == v
- else:
- assert res["data"]["llm_setting"][k] == v
- else:
- assert expected_message in res["message"]
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "prompt, expected_code, expected_message",
- [
- ({}, 0, ""),
- ({"similarity_threshold": 0}, 0, ""),
- ({"similarity_threshold": 1}, 0, ""),
- pytest.param({"similarity_threshold": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"similarity_threshold": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"similarity_threshold": "a"}, 0, "", marks=pytest.mark.skip),
- ({"vector_similarity_weight": 0}, 0, ""),
- ({"vector_similarity_weight": 1}, 0, ""),
- pytest.param({"vector_similarity_weight": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"vector_similarity_weight": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"vector_similarity_weight": "a"}, 0, "", marks=pytest.mark.skip),
- ({"parameters": []}, 0, ""),
- ({"top_n": 0}, 0, ""),
- ({"top_n": 1}, 0, ""),
- pytest.param({"top_n": -1}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_n": 10}, 0, "", marks=pytest.mark.skip),
- pytest.param({"top_n": "a"}, 0, "", marks=pytest.mark.skip),
- ({"empty_response": "Hello World"}, 0, ""),
- ({"empty_response": ""}, 0, ""),
- ({"empty_response": "!@#$%^&*()"}, 0, ""),
- ({"empty_response": "中文测试"}, 0, ""),
- pytest.param({"empty_response": 123}, 0, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": True}, 0, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": " "}, 0, "", marks=pytest.mark.skip),
- ({"prologue": "Hello World"}, 0, ""),
- ({"prologue": ""}, 0, ""),
- ({"prologue": "!@#$%^&*()"}, 0, ""),
- ({"prologue": "中文测试"}, 0, ""),
- pytest.param({"prologue": 123}, 0, "", marks=pytest.mark.skip),
- pytest.param({"prologue": True}, 0, "", marks=pytest.mark.skip),
- pytest.param({"prologue": " "}, 0, "", marks=pytest.mark.skip),
- ({"quote": True}, 0, ""),
- ({"quote": False}, 0, ""),
- ({"system": "Hello World {knowledge}"}, 0, ""),
- ({"system": "{knowledge}"}, 0, ""),
- ({"system": "!@#$%^&*() {knowledge}"}, 0, ""),
- ({"system": "中文测试 {knowledge}"}, 0, ""),
- ({"system": "Hello World"}, 0, ""),
- ({"system": "Hello World", "parameters": []}, 0, ""),
- pytest.param({"system": 123}, 100, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
- pytest.param({"system": True}, 100, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, 0, "", marks=pytest.mark.skip),
- ],
- )
- def test_prompt(self, HttpApiAuth, add_chat_assistants_func, prompt, expected_code, expected_message):
- dataset_id, _, chat_assistant_ids = add_chat_assistants_func
-
- _PROMPT_CONFIG_KEYS = {"prologue", "quote", "system", "parameters", "empty_response"}
-
- payload = {"name": "prompt_test", "dataset_ids": [dataset_id]}
- prompt_config = {}
- for k, v in prompt.items():
- if k in _PROMPT_CONFIG_KEYS:
- prompt_config[k] = v
- else:
- payload[k] = v
- if prompt_config:
- payload["prompt_config"] = prompt_config
-
- res = update_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- if not prompt:
- return
- res = get_chat_assistant(HttpApiAuth, chat_assistant_ids[0])
- for k, v in prompt.items():
- if k in _PROMPT_CONFIG_KEYS:
- assert res["data"]["prompt_config"][k] == v
- else:
- assert res["data"][k] == v
- else:
- assert expected_message in res["message"]
-
- @pytest.mark.p2
- def test_update_mapping_and_validation_branches_p2(self, HttpApiAuth, add_chat_assistants_func, chat_assistant_llm_model_type):
- dataset_id, _, chat_assistant_ids = add_chat_assistants_func
- chat_id = chat_assistant_ids[0]
-
- # Auth: non-owned chat returns 109 "no authorization"
- res = patch_chat_assistant(HttpApiAuth, "invalid-chat-id", {"name": "anything"})
- assert res["code"] == 109
- assert res["message"] == "no authorization"
-
- # PATCH: toggle quote via prompt_config
- res = patch_chat_assistant(HttpApiAuth, chat_id, {"prompt_config": {"quote": False}})
- assert res["code"] == 0
-
- # PATCH: invalid llm_id
- res = patch_chat_assistant(
- HttpApiAuth,
- chat_id,
- {"llm_id": "unknown-llm-model", "llm_setting": {"model_type": chat_assistant_llm_model_type}},
- )
- assert res["code"] == 102
- assert "`llm_id` unknown-llm-model doesn't exist" in res["message"]
-
- # PATCH: invalid rerank_id
- res = patch_chat_assistant(HttpApiAuth, chat_id, {"rerank_id": "unknown-rerank-model"})
- assert res["code"] == 102
- assert "`rerank_id` unknown-rerank-model doesn't exist" in res["message"]
-
- # PATCH: empty name
- res = patch_chat_assistant(HttpApiAuth, chat_id, {"name": ""})
- assert res["code"] == 102
- assert res["message"] == "`name` cannot be empty"
-
- # PATCH: duplicate name
- res = patch_chat_assistant(HttpApiAuth, chat_id, {"name": "test_chat_assistant_1"})
- assert res["code"] == 102
- assert res["message"] == "duplicated chat name"
-
- # PATCH: prompt_config without placeholder is allowed
- res = patch_chat_assistant(
- HttpApiAuth,
- chat_id,
- {"prompt_config": {"system": "No required placeholder", "parameters": [{"key": "knowledge", "optional": False}]}},
- )
- assert res["code"] == 0
-
- # PATCH: icon (was "avatar" in old SDK)
- res = patch_chat_assistant(HttpApiAuth, chat_id, {"icon": "raw-avatar-value"})
- assert res["code"] == 0
- listed = get_chat_assistant(HttpApiAuth, chat_id)
- assert listed["code"] == 0
- assert listed["data"]["icon"] == "raw-avatar-value"
-
- @pytest.mark.p2
- def test_update_unparsed_dataset_guard_p2(self, HttpApiAuth, add_dataset_func, clear_chat_assistants):
- dataset_id = add_dataset_func
- create_res = create_chat_assistant(HttpApiAuth, {"name": "update-unparsed-target", "dataset_ids": []})
- assert create_res["code"] == 0
-
- chat_id = create_res["data"]["id"]
- res = patch_chat_assistant(HttpApiAuth, chat_id, {"dataset_ids": [dataset_id]})
- assert res["code"] == 102
- assert "doesn't own parsed file" in res["message"]
diff --git a/test/testcases/test_http_api/test_chat_management/conftest.py b/test/testcases/test_http_api/test_chat_management/conftest.py
deleted file mode 100644
index cf64a5889b..0000000000
--- a/test/testcases/test_http_api/test_chat_management/conftest.py
+++ /dev/null
@@ -1,42 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import pytest
-from common import create_dataset, delete_datasets
-
-
[email protected](scope="class")
-def add_table_parser_dataset(HttpApiAuth, request):
- """
- Fixture to create a table parser dataset for testing.
- Automatically cleans up after tests complete (deletes dataset and table).
- Note: field_map is automatically generated by the table parser when processing files.
- """
- dataset_payload = {
- "name": "test_table_parser_dataset",
- "chunk_method": "table", # table parser
- }
- res = create_dataset(HttpApiAuth, dataset_payload)
- assert res["code"] == 0, f"Failed to create dataset: {res}"
- dataset_id = res["data"]["id"]
-
- def cleanup():
- delete_datasets(HttpApiAuth, {"ids": [dataset_id]})
-
- request.addfinalizer(cleanup)
-
- return dataset_id
diff --git a/test/testcases/test_http_api/test_chat_management/test_table_parser_dataset_chat.py b/test/testcases/test_http_api/test_chat_management/test_table_parser_dataset_chat.py
deleted file mode 100644
index 71eb487700..0000000000
--- a/test/testcases/test_http_api/test_chat_management/test_table_parser_dataset_chat.py
+++ /dev/null
@@ -1,309 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-import re
-import tempfile
-
-import pytest
-from utils import wait_for
-
-from common import (
- chat_completions,
- create_chat_assistant,
- create_session_with_chat_assistant,
- delete_chat_assistants,
- list_documents,
- parse_documents,
- upload_documents,
-)
-
-
-@wait_for(200, 1, "Document parsing timeout")
-def wait_for_parsing_completion(auth, dataset_id, document_id=None):
- """
- Wait for document parsing to complete.
-
- Args:
- auth: Authentication object
- dataset_id: Dataset ID
- document_id: Optional specific document ID to wait for
-
- Returns:
- bool: True if parsing is complete, False otherwise
- """
- res = list_documents(auth, dataset_id)
- docs = res["data"]["docs"]
-
- if document_id is None:
- # Wait for all documents to complete
- for doc in docs:
- status = doc.get("run", "UNKNOWN")
- if status != "DONE":
- # print(f"[DEBUG] Document {doc.get('name', 'unknown')} status: {status}, progress: {doc.get('progress', 0)}%, msg: {doc.get('progress_msg', '')}")
- return False
- return True
- else:
- # Wait for specific document
- for doc in docs:
- if doc["id"] == document_id:
- status = doc.get("run", "UNKNOWN")
- # print(f"[DEBUG] Document {doc.get('name', 'unknown')} status: {status}, progress: {doc.get('progress', 0)}%, msg: {doc.get('progress_msg', '')}")
- if status == "DONE":
- return True
- elif status == "FAILED":
- pytest.fail(f"Document parsing failed: {doc}")
- return False
- return False
-
-
-# Test data
-TEST_EXCEL_DATA = [
- ["employee_id", "name", "department", "salary"],
- ["E001", "Alice Johnson", "Engineering", "95000"],
- ["E002", "Bob Smith", "Marketing", "65000"],
- ["E003", "Carol Williams", "Engineering", "88000"],
- ["E004", "David Brown", "Sales", "72000"],
- ["E005", "Eva Davis", "HR", "68000"],
- ["E006", "Frank Miller", "Engineering", "102000"],
-]
-
-TEST_EXCEL_DATA_2 = [
- ["product", "price", "category"],
- ["Laptop", "999", "Electronics"],
- ["Mouse", "29", "Electronics"],
- ["Desk", "299", "Furniture"],
- ["Chair", "199", "Furniture"],
- ["Monitor", "399", "Electronics"],
- ["Keyboard", "79", "Electronics"],
-]
-
-DEFAULT_CHAT_PROMPT = "You are a helpful assistant that answers questions about table data using SQL queries.\n\nHere is the knowledge base:\n{knowledge}\n\nUse this information to answer questions."
-
-
[email protected]("add_table_parser_dataset")
-class TestTableParserDatasetChat:
- """
- Test table parser dataset chat functionality with Infinity backend.
-
- Verifies that:
- 1. Excel files are uploaded and parsed correctly into table parser datasets
- 2. Chat assistants can query the parsed table data via SQL
- 3. Different types of queries work
- """
-
- @pytest.fixture(autouse=True)
- def setup_chat_assistant(self, HttpApiAuth, add_table_parser_dataset, request):
- """
- Setup fixture that runs before each test method.
- Creates chat assistant once and reuses it across all test cases.
- """
- # Only setup once (first time)
- if not hasattr(self.__class__, "chat_id"):
- self.__class__.dataset_id = add_table_parser_dataset
- self.__class__.auth = HttpApiAuth
-
- # Upload and parse Excel files once for all tests
- self._upload_and_parse_excel(HttpApiAuth, add_table_parser_dataset)
-
- # Create a single chat assistant and session for all tests
- chat_id, session_id = self._create_chat_assistant_with_session(HttpApiAuth, add_table_parser_dataset)
- self.__class__.chat_id = chat_id
- self.__class__.session_id = session_id
-
- # Store the total number of parametrize cases
- mark = request.node.get_closest_marker("parametrize")
- if mark:
- # Get the number of test cases from parametrize
- param_values = mark.args[1]
- self.__class__._total_tests = len(param_values)
- else:
- self.__class__._total_tests = 1
-
- yield
-
- # Teardown: cleanup chat assistant after all tests
- # Use a class-level counter to track tests
- if not hasattr(self.__class__, "_test_counter"):
- self.__class__._test_counter = 0
- self.__class__._test_counter += 1
-
- # Cleanup after all parametrize tests complete
- if self.__class__._test_counter >= self.__class__._total_tests:
- self._teardown_chat_assistant()
-
- def _teardown_chat_assistant(self):
- """Teardown method to clean up chat assistant."""
- if hasattr(self.__class__, "chat_id") and self.__class__.chat_id:
- try:
- delete_chat_assistants(self.__class__.auth, {"ids": [self.__class__.chat_id]})
- except Exception as e:
- print(f"[Teardown] Warning: Failed to delete chat assistant: {e}")
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "question, expected_answer_pattern",
- [
- ("show me column of product", r"\|product\|Source"),
- ("which product has price 79", r"Keyboard"),
- ("How many rows in the dataset?", r"rows|count\(\*\)"),
- ("Show me all employees in Engineering department", r"(Alice|Carol|Frank)"),
- ],
- )
- def test_table_parser_dataset_chat(self, question, expected_answer_pattern):
- """
- Test that table parser dataset chat works correctly.
- """
- # Use class-level attributes (set by setup fixture)
- answer = self._ask_question(self.__class__.auth, self.__class__.chat_id, self.__class__.session_id, question)
-
- # Verify answer matches expected pattern if provided
- if expected_answer_pattern:
- self._assert_answer_matches_pattern(answer, expected_answer_pattern)
- else:
- # Just verify we got a non-empty answer
- assert answer and len(answer) > 0, "Expected non-empty answer"
-
- @staticmethod
- def _upload_and_parse_excel(auth, dataset_id):
- """
- Upload 2 Excel files and wait for parsing to complete.
-
- Returns:
- list: The document IDs of the uploaded files
-
- Raises:
- AssertionError: If upload or parsing fails
- """
- excel_file_paths = []
- document_ids = []
- try:
- # Create 2 temporary Excel files
- excel_file_paths.append(TestTableParserDatasetChat._create_temp_excel_file(TEST_EXCEL_DATA))
- excel_file_paths.append(TestTableParserDatasetChat._create_temp_excel_file(TEST_EXCEL_DATA_2))
-
- # Upload documents
- res = upload_documents(auth, dataset_id, excel_file_paths)
- assert res["code"] == 0, f"Failed to upload documents: {res}"
-
- for doc in res["data"]:
- document_ids.append(doc["id"])
-
- # Start parsing for all documents
- parse_payload = {"document_ids": document_ids}
- res = parse_documents(auth, dataset_id, parse_payload)
- assert res["code"] == 0, f"Failed to start parsing: {res}"
-
- # Wait for parsing completion for all documents
- for doc_id in document_ids:
- wait_for_parsing_completion(auth, dataset_id, doc_id)
-
- return document_ids
-
- finally:
- # Clean up temporary files
- for excel_file_path in excel_file_paths:
- if excel_file_path:
- os.unlink(excel_file_path)
-
- @staticmethod
- def _create_temp_excel_file(data):
- """
- Create a temporary Excel file with the given table test data.
-
- Args:
- data: List of lists containing the Excel data
-
- Returns:
- str: Path to the created temporary file
- """
- from openpyxl import Workbook
-
- f = tempfile.NamedTemporaryFile(mode="wb", suffix=".xlsx", delete=False)
- f.close()
-
- wb = Workbook()
- ws = wb.active
-
- # Write test data to the worksheet
- for row_idx, row_data in enumerate(data, start=1):
- for col_idx, value in enumerate(row_data, start=1):
- ws.cell(row=row_idx, column=col_idx, value=value)
-
- wb.save(f.name)
- return f.name
-
- @staticmethod
- def _create_chat_assistant_with_session(auth, dataset_id):
- """
- Create a chat assistant and session for testing.
-
- Returns:
- tuple: (chat_id, session_id)
- """
- import uuid
-
- chat_payload = {
- "name": f"test_table_parser_dataset_chat_{uuid.uuid4().hex[:8]}",
- "dataset_ids": [dataset_id],
- "prompt_config": {
- "system": DEFAULT_CHAT_PROMPT,
- "parameters": [
- {
- "key": "knowledge",
- "optional": True,
- "value": "Use the table data to answer questions with SQL queries.",
- }
- ],
- },
- }
-
- res = create_chat_assistant(auth, chat_payload)
- assert res["code"] == 0, f"Failed to create chat assistant: {res}"
- chat_id = res["data"]["id"]
-
- res = create_session_with_chat_assistant(auth, chat_id, {"name": f"test_session_{uuid.uuid4().hex[:8]}"})
- assert res["code"] == 0, f"Failed to create session: {res}"
- session_id = res["data"]["id"]
-
- return chat_id, session_id
-
- def _ask_question(self, auth, chat_id, session_id, question):
- """
- Send a question to the chat assistant and return the answer.
-
- Returns:
- str: The assistant's answer
- """
- payload = {
- "question": question,
- "stream": False,
- "session_id": session_id,
- }
-
- res_json = chat_completions(auth, chat_id, payload)
- assert res_json["code"] == 0, f"Chat completion failed: {res_json}"
-
- return res_json["data"]["answer"]
-
- def _assert_answer_matches_pattern(self, answer, pattern):
- """
- Assert that the answer matches the expected pattern.
-
- Args:
- answer: The actual answer from the chat assistant
- pattern: Regular expression pattern to match
- """
- assert re.search(pattern, answer, re.IGNORECASE), f"Answer does not match expected pattern '{pattern}'.\nAnswer: {answer}"
diff --git a/test/testcases/test_http_api/test_chunk_management_within_dataset/conftest.py b/test/testcases/test_http_api/test_chunk_management_within_dataset/conftest.py
deleted file mode 100644
index 0a7990b3ab..0000000000
--- a/test/testcases/test_http_api/test_chunk_management_within_dataset/conftest.py
+++ /dev/null
@@ -1,49 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-from time import sleep
-
-import pytest
-from common import add_chunk, batch_add_chunks, delete_all_chunks
-
-
-def _add_baseline_chunk(auth, dataset_id, document_id):
- add_chunk(auth, dataset_id, document_id, {"content": "ragflow test upload"})
-
-
[email protected](scope="class")
-def add_chunks(HttpApiAuth, add_document):
- dataset_id, document_id = add_document
- _add_baseline_chunk(HttpApiAuth, dataset_id, document_id)
- chunk_ids = batch_add_chunks(HttpApiAuth, dataset_id, document_id, 4)
- sleep(1) # issues/6487
- return dataset_id, document_id, chunk_ids
-
-
[email protected](scope="function")
-def add_chunks_func(request, HttpApiAuth, add_document):
- def cleanup():
- delete_all_chunks(HttpApiAuth, dataset_id, document_id)
-
- request.addfinalizer(cleanup)
-
- dataset_id, document_id = add_document
- _add_baseline_chunk(HttpApiAuth, dataset_id, document_id)
- chunk_ids = batch_add_chunks(HttpApiAuth, dataset_id, document_id, 4)
- # issues/6487
- sleep(1)
- return dataset_id, document_id, chunk_ids
diff --git a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_add_chunk.py b/test/testcases/test_http_api/test_chunk_management_within_dataset/test_add_chunk.py
deleted file mode 100644
index e6e450e746..0000000000
--- a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_add_chunk.py
+++ /dev/null
@@ -1,273 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import add_chunk, delete_documents, list_chunks
-from configs import INVALID_API_TOKEN, INVALID_ID_32
-from libs.auth import RAGFlowHttpApiAuth
-
-
-def validate_chunk_details(dataset_id, document_id, payload, res):
- chunk = res["data"]["chunk"]
- assert chunk["dataset_id"] == dataset_id
- assert chunk["document_id"] == document_id
- assert chunk["content"] == payload["content"]
- if "important_keywords" in payload:
- assert chunk["important_keywords"] == payload["important_keywords"]
- if "questions" in payload:
- assert chunk["questions"] == [str(q).strip() for q in payload.get("questions", []) if str(q).strip()]
- if "tag_kwd" in payload:
- assert chunk["tag_kwd"] == payload["tag_kwd"]
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = add_chunk(invalid_auth, "dataset_id", "document_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestAddChunk:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": None}, 102, "`content` is required"),
- ({"content": ""}, 102, "`content` is required"),
- pytest.param(
- {"content": 1},
- 100,
- """TypeError("unsupported operand type(s) for +: \'int\' and \'str\'")""",
- marks=pytest.mark.skip,
- ),
- ({"content": "a"}, 0, ""),
- ({"content": " "}, 102, "`content` is required"),
- ({"content": "\n!?。;!?\"'"}, 0, ""),
- ],
- )
- def test_content(self, HttpApiAuth, add_document, payload, expected_code, expected_message):
- dataset_id, document_id = add_document
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- chunks_count = res["data"]["doc"]["chunk_count"]
- res = add_chunk(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- validate_chunk_details(dataset_id, document_id, payload, res)
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 1
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": "chunk test", "important_keywords": ["a", "b", "c"]}, 0, ""),
- ({"content": "chunk test", "important_keywords": [""]}, 0, ""),
- (
- {"content": "chunk test", "important_keywords": [1]},
- 100,
- "TypeError('sequence item 0: expected str instance, int found')",
- ),
- ({"content": "chunk test", "important_keywords": ["a", "a"]}, 0, ""),
- ({"content": "chunk test", "important_keywords": "abc"}, 102, "`important_keywords` is required to be a list"),
- ({"content": "chunk test", "important_keywords": 123}, 102, "`important_keywords` is required to be a list"),
- ],
- )
- def test_important_keywords(self, HttpApiAuth, add_document, payload, expected_code, expected_message):
- dataset_id, document_id = add_document
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- chunks_count = res["data"]["doc"]["chunk_count"]
- res = add_chunk(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code, f"Expected code: {expected_code}, got: {res['code']}, message: {res.get('message')}"
- if expected_code == 0:
- validate_chunk_details(dataset_id, document_id, payload, res)
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 1
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": "chunk test", "questions": ["a", "b", "c"]}, 0, ""),
- ({"content": "chunk test", "questions": [""]}, 0, ""),
- ({"content": "chunk test", "questions": [1]}, 100, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"content": "chunk test", "questions": ["a", "a"]}, 0, ""),
- ({"content": "chunk test", "questions": "abc"}, 102, "`questions` is required to be a list"),
- ({"content": "chunk test", "questions": 123}, 102, "`questions` is required to be a list"),
- ],
- )
- def test_questions(self, HttpApiAuth, add_document, payload, expected_code, expected_message):
- dataset_id, document_id = add_document
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- chunks_count = res["data"]["doc"]["chunk_count"]
- res = add_chunk(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- validate_chunk_details(dataset_id, document_id, payload, res)
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 1
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": "chunk test", "tag_kwd": ["tag1", "tag2"]}, 0, ""),
- ({"content": "chunk test", "tag_kwd": [""]}, 0, ""),
- ({"content": "chunk test", "tag_kwd": [1]}, 102, "`tag_kwd` must be a list of strings"),
- ({"content": "chunk test", "tag_kwd": ["tag", "tag"]}, 0, ""),
- ({"content": "chunk test", "tag_kwd": "abc"}, 102, "`tag_kwd` is required to be a list"),
- ({"content": "chunk test", "tag_kwd": 123}, 102, "`tag_kwd` is required to be a list"),
- ],
- )
- def test_tag_kwd(self, HttpApiAuth, add_document, payload, expected_code, expected_message):
- dataset_id, document_id = add_document
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- chunks_count = res["data"]["doc"]["chunk_count"]
- res = add_chunk(HttpApiAuth, dataset_id, document_id, payload)
- if res["code"] != expected_code:
- print(f"\nFAILED! Expected code: {expected_code}, got: {res['code']}, message: {res.get('message')}")
- assert res["code"] == expected_code
- if expected_code == 0:
- validate_chunk_details(dataset_id, document_id, payload, res)
- if res["code"] != 0:
- assert False, res
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 1
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- (INVALID_ID_32, 102, f"You don't own the dataset {INVALID_ID_32}."),
- ],
- )
- def test_invalid_dataset_id(
- self,
- HttpApiAuth,
- add_document,
- dataset_id,
- expected_code,
- expected_message,
- ):
- _, document_id = add_document
- res = add_chunk(HttpApiAuth, dataset_id, document_id, {"content": "a"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- f"you don't own the document {INVALID_ID_32}",
- ),
- ],
- )
- def test_invalid_document_id(self, HttpApiAuth, add_document, document_id, expected_code, expected_message):
- dataset_id, _ = add_document
- res = add_chunk(HttpApiAuth, dataset_id, document_id, {"content": "chunk test"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_repeated_add_chunk(self, HttpApiAuth, add_document):
- payload = {"content": "chunk test"}
- dataset_id, document_id = add_document
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- chunks_count = res["data"]["doc"]["chunk_count"]
- res = add_chunk(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 0
- validate_chunk_details(dataset_id, document_id, payload, res)
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 1
-
- res = add_chunk(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 0
- validate_chunk_details(dataset_id, document_id, payload, res)
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 2
-
- @pytest.mark.p3
- def test_add_chunk_to_deleted_document(self, HttpApiAuth, add_document):
- dataset_id, document_id = add_document
- delete_documents(HttpApiAuth, dataset_id, {"ids": [document_id]})
- res = add_chunk(HttpApiAuth, dataset_id, document_id, {"content": "chunk test"})
- assert res["code"] == 102
- assert res["message"] == f"you don't own the document {document_id}"
-
- @pytest.mark.skip(reason="issues/6411")
- def test_concurrent_add_chunk(self, HttpApiAuth, add_document):
- count = 50
- dataset_id, document_id = add_document
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- chunks_count = res["data"]["doc"]["chunk_count"]
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- add_chunk,
- HttpApiAuth,
- dataset_id,
- document_id,
- {"content": f"chunk test {i}"},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert res["data"]["doc"]["chunk_count"] == chunks_count + count
diff --git a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_delete_chunks.py b/test/testcases/test_http_api/test_chunk_management_within_dataset/test_delete_chunks.py
deleted file mode 100644
index c898cdd6d4..0000000000
--- a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_delete_chunks.py
+++ /dev/null
@@ -1,186 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_add_chunks, delete_chunks, list_chunks
-from configs import INVALID_API_TOKEN, INVALID_ID_32
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = delete_chunks(invalid_auth, "dataset_id", "document_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestChunksDeletion:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- (INVALID_ID_32, 102, f"You don't own the dataset {INVALID_ID_32}."),
- ],
- )
- def test_invalid_dataset_id(self, HttpApiAuth, add_chunks_func, dataset_id, expected_code, expected_message):
- _, document_id, chunk_ids = add_chunks_func
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- (INVALID_ID_32, 102, f"you don't own the document {INVALID_ID_32}"),
- ],
- )
- def test_invalid_document_id(self, HttpApiAuth, add_chunks_func, document_id, expected_code, expected_message):
- dataset_id, _, chunk_ids = add_chunks_func
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"chunk_ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"chunk_ids": r[:1] + ["invalid_id"] + r[1:4]}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"chunk_ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_delete_partial_invalid_id(self, HttpApiAuth, add_chunks_func, payload):
- dataset_id, document_id, chunk_ids = add_chunks_func
- if callable(payload):
- payload = payload(chunk_ids)
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 102
- assert res["message"] == "rm_chunk deleted chunks 4, expect 5"
-
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]["chunks"]) == 1
- assert res["data"]["total"] == 1
-
- @pytest.mark.p3
- def test_repeated_deletion(self, HttpApiAuth, add_chunks_func):
- dataset_id, document_id, chunk_ids = add_chunks_func
- payload = {"chunk_ids": chunk_ids}
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 0
-
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 102
- assert res["message"] == "rm_chunk deleted chunks 0, expect 4"
-
- @pytest.mark.p3
- def test_duplicate_deletion(self, HttpApiAuth, add_chunks_func):
- dataset_id, document_id, chunk_ids = add_chunks_func
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids * 2})
- assert res["code"] == 0
- assert "Duplicate chunk ids" in res["data"]["errors"][0]
- assert res["data"]["success_count"] == 4
-
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]["chunks"]) == 1
- assert res["data"]["total"] == 1
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, HttpApiAuth, add_document):
- count = 100
- dataset_id, document_id = add_document
- chunk_ids = batch_add_chunks(HttpApiAuth, dataset_id, document_id, count)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- delete_chunks,
- HttpApiAuth,
- dataset_id,
- document_id,
- {"chunk_ids": chunk_ids[i : i + 1]},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_delete_1k(self, HttpApiAuth, add_document):
- chunks_num = 1_000
- dataset_id, document_id = add_document
- chunk_ids = batch_add_chunks(HttpApiAuth, dataset_id, document_id, chunks_num)
-
- # issues/6487
- from time import sleep
-
- sleep(1)
-
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
- assert res["code"] == 0
-
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]["chunks"]) == 0
- assert res["data"]["total"] == 0
-
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message, remaining",
- [
- pytest.param(None, 0, "", 5, marks=pytest.mark.p3),
- pytest.param({"chunk_ids": ["invalid_id"]}, 102, "rm_chunk deleted chunks 0, expect 1", 5, marks=pytest.mark.p3),
- pytest.param("not json", 100, """UnboundLocalError("local variable \'duplicate_messages\' referenced before assignment")""", 5, marks=pytest.mark.skip(reason="pull/6376")),
- pytest.param(lambda r: {"chunk_ids": r[:1]}, 0, "", 4, marks=pytest.mark.p3),
- pytest.param(lambda r: {"chunk_ids": r}, 0, "", 1, marks=pytest.mark.p1),
- pytest.param({"chunk_ids": []}, 0, "", 5, marks=pytest.mark.p3),
- ],
- )
- def test_basic_scenarios(
- self,
- HttpApiAuth,
- add_chunks_func,
- payload,
- expected_code,
- expected_message,
- remaining,
- ):
- dataset_id, document_id, chunk_ids = add_chunks_func
- if callable(payload):
- payload = payload(chunk_ids)
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code
- if res["code"] != 0:
- assert res["message"] == expected_message
-
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]["chunks"]) == remaining
- assert res["data"]["total"] == remaining
diff --git a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_list_chunks.py b/test/testcases/test_http_api/test_chunk_management_within_dataset/test_list_chunks.py
deleted file mode 100644
index 6a66aac947..0000000000
--- a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_list_chunks.py
+++ /dev/null
@@ -1,209 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_add_chunks, get_chunk, list_chunks
-from configs import INVALID_API_TOKEN, INVALID_ID_32
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = list_chunks(invalid_auth, "dataset_id", "document_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestChunksList:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 0, 2, ""),
- pytest.param({"page": 0, "page_size": 2}, 100, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- ({"page": 2, "page_size": 2}, 0, 2, ""),
- ({"page": 3, "page_size": 2}, 0, 1, ""),
- ({"page": "3", "page_size": 2}, 0, 1, ""),
- pytest.param({"page": -1, "page_size": 2}, 100, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- pytest.param({"page": "a", "page_size": 2}, 100, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page(self, HttpApiAuth, add_chunks, params, expected_code, expected_page_size, expected_message):
- dataset_id, document_id, _ = add_chunks
- res = list_chunks(HttpApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page_size": None}, 0, 5, ""),
- pytest.param({"page_size": 0}, 0, 5, ""),
- ({"page_size": 1}, 0, 1, ""),
- ({"page_size": 6}, 0, 5, ""),
- ({"page_size": "1"}, 0, 1, ""),
- pytest.param({"page_size": -1}, 0, 5, "", marks=pytest.mark.skip),
- pytest.param({"page_size": "a"}, 100, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page_size(self, HttpApiAuth, add_chunks, params, expected_code, expected_page_size, expected_message):
- dataset_id, document_id, _ = add_chunks
- res = list_chunks(HttpApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"keywords": None}, 5),
- ({"keywords": ""}, 5),
- ({"keywords": "1"}, 1),
- ({"keywords": "chunk"}, 4),
- pytest.param({"keywords": "ragflow"}, 1, marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6509")),
- pytest.param({"keywords": "ragflow"}, 5, marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") != "infinity", reason="issues/6509")),
- ({"keywords": "unknown"}, 0),
- ],
- )
- def test_keywords(self, HttpApiAuth, add_chunks, params, expected_page_size):
- dataset_id, document_id, _ = add_chunks
- res = list_chunks(HttpApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == 0
- assert len(res["data"]["chunks"]) == expected_page_size
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "chunk_id, expected_code, expected_page_size, expected_message",
- [
- (None, 0, 5, ""),
- ("", 0, 5, ""),
- pytest.param(lambda r: r[0], 0, 1, "", marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6499")),
- pytest.param("unknown", 100, 0, """AttributeError("\'NoneType\' object has no attribute \'keys\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_id(
- self,
- HttpApiAuth,
- add_chunks,
- chunk_id,
- expected_code,
- expected_page_size,
- expected_message,
- ):
- dataset_id, document_id, chunk_ids = add_chunks
- if callable(chunk_id):
- params = {"id": chunk_id(chunk_ids)}
- else:
- params = {"id": chunk_id}
- res = list_chunks(HttpApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if params["id"] in [None, ""]:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["data"]["chunks"][0]["id"] == params["id"]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6499")
- def test_get_chunk(self, HttpApiAuth, add_chunks):
- dataset_id, document_id, chunk_ids = add_chunks
- res = get_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0])
- assert res["code"] == 0
- assert res["data"]["id"] == chunk_ids[0]
- assert res["data"]["doc_id"] == document_id
-
- @pytest.mark.p3
- def test_invalid_params(self, HttpApiAuth, add_chunks):
- dataset_id, document_id, _ = add_chunks
- params = {"a": "b"}
- res = list_chunks(HttpApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == 0
- assert len(res["data"]["chunks"]) == 5
-
- @pytest.mark.p3
- def test_concurrent_list(self, HttpApiAuth, add_chunks):
- dataset_id, document_id, _ = add_chunks
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_chunks, HttpApiAuth, dataset_id, document_id) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(len(future.result()["data"]["chunks"]) == 5 for future in futures)
-
- @pytest.mark.p1
- def test_default(self, HttpApiAuth, add_document):
- dataset_id, document_id = add_document
-
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- chunks_count = res["data"]["doc"]["chunk_count"]
- batch_add_chunks(HttpApiAuth, dataset_id, document_id, 31)
- # issues/6487
- from time import sleep
-
- sleep(3)
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- assert res["code"] == 0
- assert len(res["data"]["chunks"]) == 30
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 31
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- (INVALID_ID_32, 102, f"You don't own the dataset {INVALID_ID_32}."),
- ],
- )
- def test_invalid_dataset_id(self, HttpApiAuth, add_chunks, dataset_id, expected_code, expected_message):
- _, document_id, _ = add_chunks
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- f"you don't own the document {INVALID_ID_32}",
- ),
- ],
- )
- def test_invalid_document_id(self, HttpApiAuth, add_chunks, document_id, expected_code, expected_message):
- dataset_id, _, _ = add_chunks
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
diff --git a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_retrieval_chunks.py b/test/testcases/test_http_api/test_chunk_management_within_dataset/test_retrieval_chunks.py
deleted file mode 100644
index f29414b18e..0000000000
--- a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_retrieval_chunks.py
+++ /dev/null
@@ -1,386 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from time import sleep
-
-import pytest
-from common import add_chunk, delete_chunks, retrieval_chunks
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 0, "`Authorization` can't be empty"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 109,
- "Authentication error: API key is invalid!",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = retrieval_chunks(invalid_auth)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestChunksRetrieval:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_page_size, expected_message",
- [
- ({"question": "chunk", "dataset_ids": None}, 0, 4, ""),
- ({"question": "chunk", "document_ids": None}, 102, 0, "`dataset_ids` is required."),
- ({"question": "chunk", "dataset_ids": None, "document_ids": None}, 0, 4, ""),
- ({"question": "chunk"}, 102, 0, "`dataset_ids` is required."),
- ],
- )
- def test_basic_scenarios(self, HttpApiAuth, add_chunks, payload, expected_code, expected_page_size, expected_message):
- dataset_id, document_id, _ = add_chunks
- if "dataset_ids" in payload:
- payload["dataset_ids"] = [dataset_id]
- if "document_ids" in payload:
- payload["document_ids"] = [document_id]
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_page_size, expected_message",
- [
- pytest.param(
- {"page": None, "page_size": 2},
- 100,
- 2,
- """TypeError("int() argument must be a string, a bytes-like object or a real number, not \'NoneType\'")""",
- marks=pytest.mark.skip,
- ),
- pytest.param(
- {"page": 0, "page_size": 2},
- 100,
- 0,
- "ValueError('Search does not support negative slicing.')",
- marks=pytest.mark.skip,
- ),
- ({"page": 2, "page_size": 2}, 0, 2, ""),
- ({"page": 3, "page_size": 2}, 0, 0, ""),
- ({"page": "3", "page_size": 2}, 0, 0, ""),
- pytest.param(
- {"page": -1, "page_size": 2},
- 100,
- 0,
- "ValueError('Search does not support negative slicing.')",
- marks=pytest.mark.skip,
- ),
- pytest.param(
- {"page": "a", "page_size": 2},
- 100,
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_page(self, HttpApiAuth, add_chunks, payload, expected_code, expected_page_size, expected_message):
- dataset_id, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset_id]})
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_page_size, expected_message",
- [
- pytest.param(
- {"page_size": None},
- 100,
- 0,
- """TypeError("int() argument must be a string, a bytes-like object or a real number, not \'NoneType\'")""",
- marks=pytest.mark.skip,
- ),
- # ({"page_size": 0}, 0, 0, ""),
- pytest.param({"page_size": 1}, 0, 1, "", marks=pytest.mark.skip(reason="issues/10692")),
- ({"page_size": 5}, 0, 4, ""),
- pytest.param({"page_size": "1"}, 0, 1, "", marks=pytest.mark.skip(reason="issues/10692")),
- # ({"page_size": -1}, 0, 0, ""),
- pytest.param(
- {"page_size": "a"},
- 100,
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_page_size(self, HttpApiAuth, add_chunks, payload, expected_code, expected_page_size, expected_message):
- dataset_id, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset_id]})
-
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_page_size, expected_message",
- [
- ({"vector_similarity_weight": 0}, 0, 4, ""),
- ({"vector_similarity_weight": 0.5}, 0, 4, ""),
- ({"vector_similarity_weight": 10}, 0, 4, ""),
- pytest.param(
- {"vector_similarity_weight": "a"},
- 100,
- 0,
- """ValueError("could not convert string to float: \'a\'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_vector_similarity_weight(self, HttpApiAuth, add_chunks, payload, expected_code, expected_page_size, expected_message):
- dataset_id, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset_id]})
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_page_size, expected_message",
- [
- ({"top_k": 10}, 0, 4, ""),
- pytest.param(
- {"top_k": 1},
- 0,
- 4,
- "",
- marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") in ["infinity", "opensearch"], reason="Infinity"),
- ),
- pytest.param(
- {"top_k": 1},
- 0,
- 1,
- "",
- marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") in [None, "opensearch", "elasticsearch"], reason="elasticsearch"),
- ),
- ({"top_k": -1}, 100, 4, "must be greater than 0"),
- pytest.param(
- {"top_k": "a"},
- 100,
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_top_k(self, HttpApiAuth, add_chunks, payload, expected_code, expected_page_size, expected_message):
- dataset_id, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset_id]})
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert expected_message in res["message"]
-
- @pytest.mark.skip
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"rerank_id": "BAAI/bge-reranker-v2-m3"}, 0, ""),
- pytest.param({"rerank_id": "unknown"}, 100, "LookupError('Model(unknown) not authorized')", marks=pytest.mark.skip),
- ],
- )
- def test_rerank_id(self, HttpApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset_id]})
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) > 0
- else:
- assert expected_message in res["message"]
-
- @pytest.mark.skip
- @pytest.mark.parametrize(
- "payload, expected_code, expected_page_size, expected_message",
- [
- ({"keyword": True}, 0, 5, ""),
- ({"keyword": "True"}, 0, 5, ""),
- ({"keyword": False}, 0, 5, ""),
- ({"keyword": "False"}, 0, 5, ""),
- ({"keyword": None}, 0, 5, ""),
- ],
- )
- def test_keyword(self, HttpApiAuth, add_chunks, payload, expected_code, expected_page_size, expected_message):
- dataset_id, _, _ = add_chunks
- payload.update({"question": "chunk test", "dataset_ids": [dataset_id]})
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_highlight, expected_message",
- [
- ({"highlight": True}, 0, True, ""),
- ({"highlight": "True"}, 0, True, ""),
- ({"highlight": False}, 0, False, ""),
- ({"highlight": "False"}, 0, False, ""),
- pytest.param({"highlight": None}, 0, False, "", marks=pytest.mark.skip(reason="issues/6648")),
- ],
- )
- def test_highlight(self, HttpApiAuth, add_chunks, payload, expected_code, expected_highlight, expected_message):
- dataset_id, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset_id]})
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == expected_code
- if expected_highlight:
- for chunk in res["data"]["chunks"]:
- assert "highlight" in chunk
- else:
- for chunk in res["data"]["chunks"]:
- assert "highlight" not in chunk
-
- if expected_code != 0:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_invalid_params(self, HttpApiAuth, add_chunks):
- dataset_id, _, _ = add_chunks
- payload = {"question": "chunk", "dataset_ids": [dataset_id], "a": "b"}
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == 0
- assert len(res["data"]["chunks"]) == 4
-
- @pytest.mark.p3
- def test_concurrent_retrieval(self, HttpApiAuth, add_chunks):
- dataset_id, _, _ = add_chunks
- count = 100
- payload = {"question": "chunk", "dataset_ids": [dataset_id]}
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(retrieval_chunks, HttpApiAuth, payload) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
-class TestDeletedChunksNotRetrievable:
- """Regression tests for issue #12520: deleted slices should not appear in retrieval/reference."""
-
- @pytest.mark.p1
- def test_deleted_chunk_not_in_retrieval(self, HttpApiAuth, add_document):
- """
- Test that a deleted chunk is not returned by the retrieval API.
-
- Steps:
- 1. Add a chunk with unique content
- 2. Verify the chunk is retrievable
- 3. Delete the chunk
- 4. Verify the chunk is no longer retrievable
- """
- dataset_id, document_id = add_document
-
- # Add a chunk with unique content that we can search for
- unique_content = "UNIQUE_TEST_CONTENT_12520_REGRESSION"
- res = add_chunk(HttpApiAuth, dataset_id, document_id, {"content": unique_content})
- assert res["code"] == 0, f"Failed to add chunk: {res}"
- chunk_id = res["data"]["chunk"]["id"]
-
- # Wait for indexing to complete
- sleep(2)
-
- # Verify the chunk is retrievable
- payload = {"question": unique_content, "dataset_ids": [dataset_id]}
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == 0, f"Retrieval failed: {res}"
- chunk_ids_before = [c["id"] for c in res["data"]["chunks"]]
- assert chunk_id in chunk_ids_before, f"Chunk {chunk_id} should be retrievable before deletion"
-
- # Delete the chunk
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, {"chunk_ids": [chunk_id]})
- assert res["code"] == 0, f"Failed to delete chunk: {res}"
-
- # Wait for deletion to propagate
- sleep(1)
-
- # Verify the chunk is no longer retrievable
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == 0, f"Retrieval failed after deletion: {res}"
- chunk_ids_after = [c["id"] for c in res["data"]["chunks"]]
- assert chunk_id not in chunk_ids_after, f"Chunk {chunk_id} should NOT be retrievable after deletion"
-
- @pytest.mark.p2
- def test_deleted_chunks_batch_not_in_retrieval(self, HttpApiAuth, add_document):
- """
- Test that multiple deleted chunks are not returned by retrieval.
- """
- dataset_id, document_id = add_document
-
- # Add multiple chunks with unique content
- chunk_ids = []
- for i in range(3):
- unique_content = f"BATCH_DELETE_TEST_CHUNK_{i}_12520"
- res = add_chunk(HttpApiAuth, dataset_id, document_id, {"content": unique_content})
- assert res["code"] == 0, f"Failed to add chunk {i}: {res}"
- chunk_ids.append(res["data"]["chunk"]["id"])
-
- # Wait for indexing
- sleep(2)
-
- # Verify chunks are retrievable
- payload = {"question": "BATCH_DELETE_TEST_CHUNK", "dataset_ids": [dataset_id]}
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == 0
- retrieved_ids_before = [c["id"] for c in res["data"]["chunks"]]
- for cid in chunk_ids:
- assert cid in retrieved_ids_before, f"Chunk {cid} should be retrievable before deletion"
-
- # Delete all chunks
- res = delete_chunks(HttpApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
- assert res["code"] == 0, f"Failed to delete chunks: {res}"
-
- # Wait for deletion to propagate
- sleep(1)
-
- # Verify none of the chunks are retrievable
- res = retrieval_chunks(HttpApiAuth, payload)
- assert res["code"] == 0
- retrieved_ids_after = [c["id"] for c in res["data"]["chunks"]]
- for cid in chunk_ids:
- assert cid not in retrieved_ids_after, f"Chunk {cid} should NOT be retrievable after deletion"
diff --git a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_update_chunk.py b/test/testcases/test_http_api/test_chunk_management_within_dataset/test_update_chunk.py
deleted file mode 100644
index 58664555cc..0000000000
--- a/test/testcases/test_http_api/test_chunk_management_within_dataset/test_update_chunk.py
+++ /dev/null
@@ -1,250 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from random import randint
-
-import pytest
-from common import delete_documents, update_chunk
-from configs import INVALID_API_TOKEN, INVALID_ID_32
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = update_chunk(invalid_auth, "dataset_id", "document_id", "chunk_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestUpdatedChunk:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param({"content": None}, 0, "", marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6509")),
- ({"content": ""}, 102, "`content` is required"),
- pytest.param(
- {"content": 1},
- 100,
- "TypeError('expected string or bytes-like object')",
- marks=pytest.mark.skip,
- ),
- ({"content": "update chunk"}, 0, ""),
- ({"content": " "}, 102, "`content` is required"),
- ({"content": "\n!?。;!?\"'"}, 0, ""),
- ],
- )
- def test_content(self, HttpApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"important_keywords": ["a", "b", "c"]}, 0, ""),
- ({"important_keywords": [""]}, 0, ""),
- ({"important_keywords": [1]}, 100, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"important_keywords": ["a", "a"]}, 0, ""),
- ({"important_keywords": "abc"}, 102, "`important_keywords` should be a list"),
- ({"important_keywords": 123}, 102, "`important_keywords` should be a list"),
- ],
- )
- def test_important_keywords(self, HttpApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"questions": ["a", "b", "c"]}, 0, ""),
- ({"questions": [""]}, 0, ""),
- ({"questions": [1]}, 100, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"questions": ["a", "a"]}, 0, ""),
- ({"questions": "abc"}, 102, "`questions` should be a list"),
- ({"questions": 123}, 102, "`questions` should be a list"),
- ],
- )
- def test_questions(self, HttpApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"tag_kwd": ["tag1", "tag2"]}, 0, ""),
- ({"tag_kwd": [""]}, 0, ""),
- ({"tag_kwd": [1]}, 102, "`tag_kwd` must be a list of strings"),
- ({"tag_kwd": ["tag", "tag"]}, 0, ""),
- ({"tag_kwd": "tag"}, 102, "`tag_kwd` should be a list"),
- ({"tag_kwd": 123}, 102, "`tag_kwd` should be a list"),
- ],
- )
- def test_tag_kwd(self, HttpApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"available": True}, 0, ""),
- pytest.param({"available": "True"}, 100, """ValueError("invalid literal for int() with base 10: \'True\'")""", marks=pytest.mark.skip),
- ({"available": 1}, 0, ""),
- ({"available": False}, 0, ""),
- pytest.param({"available": "False"}, 100, """ValueError("invalid literal for int() with base 10: \'False\'")""", marks=pytest.mark.skip),
- ({"available": 0}, 0, ""),
- ],
- )
- def test_available(
- self,
- HttpApiAuth,
- add_chunks,
- payload,
- expected_code,
- expected_message,
- ):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- pytest.param(INVALID_ID_32, 102, f"You don't own the dataset {INVALID_ID_32}.", marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="infinity")),
- pytest.param(INVALID_ID_32, 102, "Can't find this chunk", marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") in [None, "opensearch", "elasticsearch"], reason="elasticsearch")),
- ],
- )
- def test_invalid_dataset_id(self, HttpApiAuth, add_chunks, dataset_id, expected_code, expected_message):
- _, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0])
- assert res["code"] == expected_code
- assert expected_message in res["message"]
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- f"you don't own the document {INVALID_ID_32}",
- ),
- ],
- )
- def test_invalid_document_id(self, HttpApiAuth, add_chunks, document_id, expected_code, expected_message):
- dataset_id, _, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0])
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "chunk_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- f"Can't find this chunk {INVALID_ID_32}",
- ),
- ],
- )
- def test_invalid_chunk_id(self, HttpApiAuth, add_chunks, chunk_id, expected_code, expected_message):
- dataset_id, document_id, _ = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_id)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_repeated_update_chunk(self, HttpApiAuth, add_chunks):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], {"content": "chunk test 1"})
- assert res["code"] == 0
-
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], {"content": "chunk test 2"})
- assert res["code"] == 0
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"unknown_key": "unknown_value"}, 0, ""),
- ({}, 0, ""),
- pytest.param(None, 100, """TypeError("argument of type \'NoneType\' is not iterable")""", marks=pytest.mark.skip),
- ],
- )
- def test_invalid_params(self, HttpApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6554")
- def test_concurrent_update_chunk(self, HttpApiAuth, add_chunks):
- count = 50
- dataset_id, document_id, chunk_ids = add_chunks
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- update_chunk,
- HttpApiAuth,
- dataset_id,
- document_id,
- chunk_ids[randint(0, 3)],
- {"content": f"update chunk test {i}"},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_update_chunk_to_deleted_document(self, HttpApiAuth, add_chunks):
- dataset_id, document_id, chunk_ids = add_chunks
- delete_documents(HttpApiAuth, dataset_id, {"ids": [document_id]})
- res = update_chunk(HttpApiAuth, dataset_id, document_id, chunk_ids[0])
- assert res["code"] == 102
- assert res["message"] in [f"you don't own the document {document_id}", f"Can't find this chunk {chunk_ids[0]}"]
diff --git a/test/testcases/test_http_api/test_dataset_management/conftest.py b/test/testcases/test_http_api/test_dataset_management/conftest.py
deleted file mode 100644
index 3e03e50b98..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/conftest.py
+++ /dev/null
@@ -1,39 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import pytest
-from common import batch_create_datasets, delete_all_datasets
-
-
[email protected](scope="class")
-def add_datasets(HttpApiAuth, request):
- def cleanup():
- delete_all_datasets(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
- return batch_create_datasets(HttpApiAuth, 5)
-
-
[email protected](scope="function")
-def add_datasets_func(HttpApiAuth, request):
- def cleanup():
- delete_all_datasets(HttpApiAuth)
-
- request.addfinalizer(cleanup)
-
- return batch_create_datasets(HttpApiAuth, 3)
diff --git a/test/testcases/test_http_api/test_dataset_management/test_create_dataset.py b/test/testcases/test_http_api/test_dataset_management/test_create_dataset.py
deleted file mode 100644
index 68787962d2..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_create_dataset.py
+++ /dev/null
@@ -1,749 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from configs import DATASET_NAME_LIMIT, DEFAULT_PARSER_CONFIG, INVALID_API_TOKEN
-from hypothesis import example, given, settings
-from libs.auth import RAGFlowHttpApiAuth
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-from utils.hypothesis_utils import valid_names
-
-from test_http_api.common import create_dataset, delete_all_datasets
-
-
[email protected]("clear_datasets")
-class TestAuthorization:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- ids=["empty_auth", "invalid_api_token"],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = create_dataset(invalid_auth, {"name": "auth_test"})
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestRquest:
- @pytest.mark.p3
- def test_content_type_bad(self, HttpApiAuth):
- BAD_CONTENT_TYPE = "text/xml"
- res = create_dataset(HttpApiAuth, {"name": "bad_content_type"}, headers={"Content-Type": BAD_CONTENT_TYPE})
- assert res["code"] == 101, res
- assert res["message"] == f"Unsupported content type: Expected application/json, got {BAD_CONTENT_TYPE}", res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ("a", "Malformed JSON syntax: Missing commas/brackets or invalid encoding"),
- ('"a"', "Invalid request payload: expected object, got str"),
- ],
- ids=["malformed_json_syntax", "invalid_request_payload_type"],
- )
- def test_payload_bad(self, HttpApiAuth, payload, expected_message):
- res = create_dataset(HttpApiAuth, data=payload)
- assert res["code"] == 101, res
- assert res["message"] == expected_message, res
-
-
[email protected]("clear_datasets")
-class TestCapability:
- @pytest.mark.p3
- def test_create_dataset_1k(self, HttpApiAuth):
- for i in range(1_000):
- payload = {"name": f"dataset_{i}"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, f"Failed to create dataset {i}"
-
- @pytest.mark.p3
- def test_create_dataset_concurrent(self, HttpApiAuth):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(create_dataset, HttpApiAuth, {"name": f"dataset_{i}"}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
[email protected]("clear_datasets")
-class TestDatasetCreate:
- @pytest.mark.p1
- @given(name=valid_names())
- @example("a" * 128)
- @settings(max_examples=20, deadline=None)
- def test_name(self, HttpApiAuth, name):
- delete_all_datasets(HttpApiAuth)
- res = create_dataset(HttpApiAuth, {"name": name})
- assert res["code"] == 0, res
- assert res["data"]["name"] == name, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "String should have at least 1 character"),
- (" ", "String should have at least 1 character"),
- ("a" * (DATASET_NAME_LIMIT + 1), "String should have at most 128 characters"),
- (0, "Input should be a valid string"),
- (None, "Input should be a valid string"),
- ],
- ids=["empty_name", "space_name", "too_long_name", "invalid_name", "None_name"],
- )
- def test_name_invalid(self, HttpApiAuth, name, expected_message):
- payload = {"name": name}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p3
- def test_name_duplicated(self, HttpApiAuth):
- name = "duplicated_name"
- payload = {"name": name}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["name"] == name + "(1)", res
-
- @pytest.mark.p3
- def test_name_case_insensitive(self, HttpApiAuth):
- name = "CaseInsensitive"
- payload = {"name": name.upper()}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- payload = {"name": name.lower()}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["name"] == name.lower() + "(1)", res
-
- @pytest.mark.p2
- def test_avatar(self, HttpApiAuth, tmp_path):
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {
- "name": "avatar",
- "avatar": f"data:image/png;base64,{encode_avatar(fn)}",
- }
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- @pytest.mark.p3
- def test_avatar_exceeds_limit_length(self, HttpApiAuth):
- payload = {"name": "avatar_exceeds_limit_length", "avatar": "a" * 65536}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "String should have at most 65535 characters" in res["message"], res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "name, prefix, expected_message",
- [
- ("empty_prefix", "", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("missing_comma", "data:image/png;base64", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("unsupported_mine_type", "invalid_mine_prefix:image/png;base64,", "invalid MIME prefix format. Must start with 'data:'"),
- ("invalid_mine_type", "data:unsupported_mine_type;base64,", "Unsupported MIME type. Allowed: ['image/jpeg', 'image/png']"),
- ],
- ids=["empty_prefix", "missing_comma", "unsupported_mine_type", "invalid_mine_type"],
- )
- def test_avatar_invalid_prefix(self, HttpApiAuth, tmp_path, name, prefix, expected_message):
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {
- "name": name,
- "avatar": f"{prefix}{encode_avatar(fn)}",
- }
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p3
- def test_avatar_unset(self, HttpApiAuth):
- payload = {"name": "avatar_unset"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["avatar"] is None, res
-
- @pytest.mark.p3
- def test_avatar_none(self, HttpApiAuth):
- payload = {"name": "avatar_none", "avatar": None}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["avatar"] is None, res
-
- @pytest.mark.p2
- def test_description(self, HttpApiAuth):
- payload = {"name": "description", "description": "description"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["description"] == "description", res
-
- @pytest.mark.p3
- def test_description_exceeds_limit_length(self, HttpApiAuth):
- payload = {"name": "description_exceeds_limit_length", "description": "a" * 65536}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "String should have at most 65535 characters" in res["message"], res
-
- @pytest.mark.p3
- def test_description_unset(self, HttpApiAuth):
- payload = {"name": "description_unset"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["description"] is None, res
-
- @pytest.mark.p3
- def test_description_none(self, HttpApiAuth):
- payload = {"name": "description_none", "description": None}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["description"] is None, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("BAAI/bge-small-en-v1.5@Builtin", "BAAI/bge-small-en-v1.5@Builtin"),
- ("embedding-3@ZHIPU-AI", "embedding-3@ZHIPU-AI"),
- ],
- ids=["builtin_baai", "tenant_zhipu"],
- )
- def test_embedding_model(self, HttpApiAuth, name, embedding_model):
- payload = {"name": name, "embedding_model": embedding_model}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["embedding_model"] == embedding_model, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("unknown_llm_name", "unknown@ZHIPU-AI"),
- ("unknown_llm_factory", "embedding-3@unknown"),
- ("tenant_no_auth_default_tenant_llm", "text-embedding-v3@Tongyi-Qianwen"),
- ("tenant_no_auth", "text-embedding-3-small@OpenAI"),
- ],
- ids=["unknown_llm_name", "unknown_llm_factory", "tenant_no_auth_default_tenant_llm", "tenant_no_auth"],
- )
- def test_embedding_model_invalid(self, HttpApiAuth, name, embedding_model):
- payload = {"name": name, "embedding_model": embedding_model}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 102, res
- if "tenant_no_auth" in name:
- assert res["message"] == f"Unauthorized model: <{embedding_model}>", res
- else:
- assert res["message"] == f"Unsupported model: <{embedding_model}>", res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("empty", ""),
- ("space", " "),
- ("missing_at", "BAAI/bge-small-en-v1.5Builtin"),
- ("missing_model_name", "@Builtin"),
- ("missing_provider", "BAAI/bge-small-en-v1.5@"),
- ("whitespace_only_model_name", " @Builtin"),
- ("whitespace_only_provider", "BAAI/bge-small-env1.5@ "),
- ],
- ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],
- )
- def test_embedding_model_format(self, HttpApiAuth, name, embedding_model):
- payload = {"name": name, "embedding_model": embedding_model}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- if name in ["empty", "space", "missing_at"]:
- assert "embedding model identifier must follow <model_name>@<provider> format" in res["message"], res
- else:
- assert "both model_name and provider must be non-empty strings" in res["message"], res
-
- @pytest.mark.p2
- def test_embedding_model_unset(self, HttpApiAuth):
- payload = {"name": "embedding_model_unset"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["embedding_model"] == "BAAI/bge-small-en-v1.5@Builtin", res
-
- @pytest.mark.p2
- def test_embedding_model_none(self, HttpApiAuth):
- payload = {"name": "embedding_model_none", "embedding_model": None}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["embedding_model"] == "BAAI/bge-small-en-v1.5@Builtin", res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, permission",
- [
- ("me", "me"),
- ("team", "team"),
- ],
- ids=["me", "team"],
- )
- def test_permission(self, HttpApiAuth, name, permission):
- payload = {"name": name, "permission": permission}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["permission"] == permission.lower().strip(), res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, permission",
- [
- ("empty", ""),
- ("unknown", "unknown"),
- ("type_error", list()),
- ("me_upercase", "ME"),
- ("team_upercase", "TEAM"),
- ("whitespace", " ME "),
- ],
- ids=["empty", "unknown", "type_error", "me_upercase", "team_upercase", "whitespace"],
- )
- def test_permission_invalid(self, HttpApiAuth, name, permission):
- payload = {"name": name, "permission": permission}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101
- assert "Input should be 'me' or 'team'" in res["message"]
-
- @pytest.mark.p2
- def test_permission_unset(self, HttpApiAuth):
- payload = {"name": "permission_unset"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["permission"] == "me", res
-
- @pytest.mark.p3
- def test_permission_none(self, HttpApiAuth):
- payload = {"name": "permission_none", "permission": None}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "Input should be 'me' or 'team'" in res["message"], res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "name, chunk_method",
- [
- ("naive", "naive"),
- ("book", "book"),
- ("email", "email"),
- ("laws", "laws"),
- ("manual", "manual"),
- ("one", "one"),
- ("paper", "paper"),
- ("picture", "picture"),
- ("presentation", "presentation"),
- ("qa", "qa"),
- ("table", "table"),
- ("tag", "tag"),
- ],
- ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table", "tag"],
- )
- def test_chunk_method(self, HttpApiAuth, name, chunk_method):
- payload = {"name": name, "chunk_method": chunk_method}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["chunk_method"] == chunk_method, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, chunk_method",
- [
- ("empty", ""),
- ("unknown", "unknown"),
- ("type_error", list()),
- ],
- ids=["empty", "unknown", "type_error"],
- )
- def test_chunk_method_invalid(self, HttpApiAuth, name, chunk_method):
- payload = {"name": name, "chunk_method": chunk_method}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in res["message"], res
-
- @pytest.mark.p2
- def test_chunk_method_unset(self, HttpApiAuth):
- payload = {"name": "chunk_method_unset"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["chunk_method"] == "naive", res
-
- @pytest.mark.p3
- def test_chunk_method_none(self, HttpApiAuth):
- payload = {"name": "chunk_method_none", "chunk_method": None}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in res["message"], res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "name, parser_config",
- [
- ("auto_keywords_min", {"auto_keywords": 0}),
- ("auto_keywords_mid", {"auto_keywords": 16}),
- ("auto_keywords_max", {"auto_keywords": 32}),
- ("auto_questions_min", {"auto_questions": 0}),
- ("auto_questions_mid", {"auto_questions": 5}),
- ("auto_questions_max", {"auto_questions": 10}),
- ("chunk_token_num_min", {"chunk_token_num": 1}),
- ("chunk_token_num_mid", {"chunk_token_num": 1024}),
- ("chunk_token_num_max", {"chunk_token_num": 2048}),
- ("delimiter", {"delimiter": "\n"}),
- ("delimiter_space", {"delimiter": " "}),
- ("html4excel_true", {"html4excel": True}),
- ("html4excel_false", {"html4excel": False}),
- ("layout_recognize_DeepDOC", {"layout_recognize": "DeepDOC"}),
- ("layout_recognize_navie", {"layout_recognize": "Plain Text"}),
- ("tag_kb_ids", {"tag_kb_ids": ["1", "2"]}),
- ("topn_tags_min", {"topn_tags": 1}),
- ("topn_tags_mid", {"topn_tags": 5}),
- ("topn_tags_max", {"topn_tags": 10}),
- ("filename_embd_weight_min", {"filename_embd_weight": 0.1}),
- ("filename_embd_weight_mid", {"filename_embd_weight": 0.5}),
- ("filename_embd_weight_max", {"filename_embd_weight": 1.0}),
- ("task_page_size_min", {"task_page_size": 1}),
- ("task_page_size_None", {"task_page_size": None}),
- ("pages", {"pages": [[1, 100]]}),
- ("pages_none", {"pages": None}),
- ("graphrag_true", {"graphrag": {"use_graphrag": True}}),
- ("graphrag_false", {"graphrag": {"use_graphrag": False}}),
- ("graphrag_entity_types", {"graphrag": {"entity_types": ["age", "sex", "height", "weight"]}}),
- ("graphrag_method_general", {"graphrag": {"method": "general"}}),
- ("graphrag_method_light", {"graphrag": {"method": "light"}}),
- ("graphrag_community_true", {"graphrag": {"community": True}}),
- ("graphrag_community_false", {"graphrag": {"community": False}}),
- ("graphrag_resolution_true", {"graphrag": {"resolution": True}}),
- ("graphrag_resolution_false", {"graphrag": {"resolution": False}}),
- ("raptor_true", {"raptor": {"use_raptor": True}}),
- ("raptor_false", {"raptor": {"use_raptor": False}}),
- ("raptor_prompt", {"raptor": {"prompt": "Who are you?"}}),
- ("raptor_max_token_min", {"raptor": {"max_token": 512}}),
- ("raptor_max_token_mid", {"raptor": {"max_token": 1024}}),
- ("raptor_max_token_max", {"raptor": {"max_token": 2048}}),
- ("raptor_clustering_threshold_min", {"raptor": {"clustering_threshold": 0.0}}),
- ("raptor_clustering_threshold_mid", {"raptor": {"clustering_threshold": 0.5}}),
- ("raptor_clustering_threshold_max", {"raptor": {"clustering_threshold": 1.0}}),
- ("raptor_max_cluster_min", {"raptor": {"max_cluster": 1}}),
- ("raptor_max_cluster_mid", {"raptor": {"max_cluster": 512}}),
- ("raptor_max_cluster_max", {"raptor": {"max_cluster": 1024}}),
- ("raptor_random_seed_min", {"raptor": {"random_seed": 0}}),
- ("parent_child_true", {"parent_child": {"use_parent_child": True}}),
- ("parent_child_false", {"parent_child": {"use_parent_child": False}}),
- ("parent_child_delimiter", {"parent_child": {"children_delimiter": "\n\n"}}),
- ("parent_child_delimiter_custom", {"parent_child": {"use_parent_child": True, "children_delimiter": "。"}}),
- ],
- ids=[
- "auto_keywords_min",
- "auto_keywords_mid",
- "auto_keywords_max",
- "auto_questions_min",
- "auto_questions_mid",
- "auto_questions_max",
- "chunk_token_num_min",
- "chunk_token_num_mid",
- "chunk_token_num_max",
- "delimiter",
- "delimiter_space",
- "html4excel_true",
- "html4excel_false",
- "layout_recognize_DeepDOC",
- "layout_recognize_navie",
- "tag_kb_ids",
- "topn_tags_min",
- "topn_tags_mid",
- "topn_tags_max",
- "filename_embd_weight_min",
- "filename_embd_weight_mid",
- "filename_embd_weight_max",
- "task_page_size_min",
- "task_page_size_None",
- "pages",
- "pages_none",
- "graphrag_true",
- "graphrag_false",
- "graphrag_entity_types",
- "graphrag_method_general",
- "graphrag_method_light",
- "graphrag_community_true",
- "graphrag_community_false",
- "graphrag_resolution_true",
- "graphrag_resolution_false",
- "raptor_true",
- "raptor_false",
- "raptor_prompt",
- "raptor_max_token_min",
- "raptor_max_token_mid",
- "raptor_max_token_max",
- "raptor_clustering_threshold_min",
- "raptor_clustering_threshold_mid",
- "raptor_clustering_threshold_max",
- "raptor_max_cluster_min",
- "raptor_max_cluster_mid",
- "raptor_max_cluster_max",
- "raptor_random_seed_min",
- "parent_child_true",
- "parent_child_false",
- "parent_child_delimiter",
- "parent_child_delimiter_custom",
- ],
- )
- def test_parser_config(self, HttpApiAuth, name, parser_config):
- payload = {"name": name, "parser_config": parser_config}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- for k, v in parser_config.items():
- if isinstance(v, dict):
- for kk, vv in v.items():
- assert res["data"]["parser_config"][k][kk] == vv, res
- else:
- assert res["data"]["parser_config"][k] == v, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, parser_config, expected_message",
- [
- ("auto_keywords_min_limit", {"auto_keywords": -1}, "Input should be greater than or equal to 0"),
- ("auto_keywords_max_limit", {"auto_keywords": 33}, "Input should be less than or equal to 32"),
- ("auto_keywords_float_not_allowed", {"auto_keywords": 3.14}, "Input should be a valid integer"),
- ("auto_keywords_type_invalid", {"auto_keywords": "string"}, "Input should be a valid integer"),
- ("auto_questions_min_limit", {"auto_questions": -1}, "Input should be greater than or equal to 0"),
- ("auto_questions_max_limit", {"auto_questions": 11}, "Input should be less than or equal to 10"),
- ("auto_questions_float_not_allowed", {"auto_questions": 3.14}, "Input should be a valid integer"),
- ("auto_questions_type_invalid", {"auto_questions": "string"}, "Input should be a valid integer"),
- ("chunk_token_num_min_limit", {"chunk_token_num": 0}, "Input should be greater than or equal to 1"),
- ("chunk_token_num_max_limit", {"chunk_token_num": 2049}, "Input should be less than or equal to 2048"),
- ("chunk_token_num_float_not_allowed", {"chunk_token_num": 3.14}, "Input should be a valid integer"),
- ("chunk_token_num_type_invalid", {"chunk_token_num": "string"}, "Input should be a valid integer"),
- ("delimiter_empty", {"delimiter": ""}, "String should have at least 1 character"),
- ("html4excel_type_invalid", {"html4excel": "string"}, "Input should be a valid boolean"),
- ("tag_kb_ids_not_list", {"tag_kb_ids": "1,2"}, "Input should be a valid list"),
- ("tag_kb_ids_int_in_list", {"tag_kb_ids": [1, 2]}, "Input should be a valid string"),
- ("topn_tags_min_limit", {"topn_tags": 0}, "Input should be greater than or equal to 1"),
- ("topn_tags_max_limit", {"topn_tags": 11}, "Input should be less than or equal to 10"),
- ("topn_tags_float_not_allowed", {"topn_tags": 3.14}, "Input should be a valid integer"),
- ("topn_tags_type_invalid", {"topn_tags": "string"}, "Input should be a valid integer"),
- ("filename_embd_weight_min_limit", {"filename_embd_weight": -1}, "Input should be greater than or equal to 0"),
- ("filename_embd_weight_max_limit", {"filename_embd_weight": 1.1}, "Input should be less than or equal to 1"),
- ("filename_embd_weight_type_invalid", {"filename_embd_weight": "string"}, "Input should be a valid number"),
- ("task_page_size_min_limit", {"task_page_size": 0}, "Input should be greater than or equal to 1"),
- ("task_page_size_float_not_allowed", {"task_page_size": 3.14}, "Input should be a valid integer"),
- ("task_page_size_type_invalid", {"task_page_size": "string"}, "Input should be a valid integer"),
- ("pages_not_list", {"pages": "1,2"}, "Input should be a valid list"),
- ("pages_not_list_in_list", {"pages": ["1,2"]}, "Input should be a valid list"),
- ("pages_not_int_list", {"pages": [["string1", "string2"]]}, "Input should be a valid integer"),
- ("graphrag_type_invalid", {"graphrag": {"use_graphrag": "string"}}, "Input should be a valid boolean"),
- ("graphrag_entity_types_not_list", {"graphrag": {"entity_types": "1,2"}}, "Input should be a valid list"),
- ("graphrag_entity_types_not_str_in_list", {"graphrag": {"entity_types": [1, 2]}}, "nput should be a valid string"),
- ("graphrag_method_unknown", {"graphrag": {"method": "unknown"}}, "Input should be 'light', 'general' or 'ner'"),
- ("graphrag_method_none", {"graphrag": {"method": None}}, "Input should be 'light', 'general' or 'ner'"),
- ("graphrag_community_type_invalid", {"graphrag": {"community": "string"}}, "Input should be a valid boolean"),
- ("graphrag_resolution_type_invalid", {"graphrag": {"resolution": "string"}}, "Input should be a valid boolean"),
- ("raptor_type_invalid", {"raptor": {"use_raptor": "string"}}, "Input should be a valid boolean"),
- ("raptor_prompt_empty", {"raptor": {"prompt": ""}}, "String should have at least 1 character"),
- ("raptor_prompt_space", {"raptor": {"prompt": " "}}, "String should have at least 1 character"),
- ("raptor_max_token_max_limit", {"raptor": {"max_token": 2049}}, "Input should be less than or equal to 2048"),
- ("raptor_max_token_type_invalid", {"raptor": {"max_token": "string"}}, "Input should be a valid integer"),
- ("raptor_clustering_threshold_min_limit", {"raptor": {"clustering_threshold": -0.1}}, "Input should be greater than or equal to 0"),
- ("raptor_clustering_threshold_max_limit", {"raptor": {"clustering_threshold": 1.1}}, "Input should be less than or equal to 1"),
- ("raptor_clustering_threshold_type_invalid", {"raptor": {"clustering_threshold": "string"}}, "Input should be a valid number"),
- ("raptor_max_cluster_min_limit", {"raptor": {"max_cluster": 0}}, "Input should be greater than or equal to 1"),
- ("raptor_max_cluster_max_limit", {"raptor": {"max_cluster": 1025}}, "Input should be less than or equal to 1024"),
- ("raptor_max_cluster_float_not_allowed", {"raptor": {"max_cluster": 3.14}}, "Input should be a valid integer"),
- ("raptor_max_cluster_type_invalid", {"raptor": {"max_cluster": "string"}}, "Input should be a valid integer"),
- ("raptor_random_seed_min_limit", {"raptor": {"random_seed": -1}}, "Input should be greater than or equal to 0"),
- ("raptor_random_seed_float_not_allowed", {"raptor": {"random_seed": 3.14}}, "Input should be a valid integer"),
- ("raptor_random_seed_type_invalid", {"raptor": {"random_seed": "string"}}, "Input should be a valid integer"),
- ("parser_config_type_invalid", {"delimiter": "a" * 65536}, "Parser config exceeds size limit (max 65,535 characters)"),
- ("parent_child_type_invalid", {"parent_child": {"use_parent_child": "string"}}, "Input should be a valid boolean"),
- ("parent_child_delimiter_empty", {"parent_child": {"children_delimiter": ""}}, "String should have at least 1 character"),
- ],
- ids=[
- "auto_keywords_min_limit",
- "auto_keywords_max_limit",
- "auto_keywords_float_not_allowed",
- "auto_keywords_type_invalid",
- "auto_questions_min_limit",
- "auto_questions_max_limit",
- "auto_questions_float_not_allowed",
- "auto_questions_type_invalid",
- "chunk_token_num_min_limit",
- "chunk_token_num_max_limit",
- "chunk_token_num_float_not_allowed",
- "chunk_token_num_type_invalid",
- "delimiter_empty",
- "html4excel_type_invalid",
- "tag_kb_ids_not_list",
- "tag_kb_ids_int_in_list",
- "topn_tags_min_limit",
- "topn_tags_max_limit",
- "topn_tags_float_not_allowed",
- "topn_tags_type_invalid",
- "filename_embd_weight_min_limit",
- "filename_embd_weight_max_limit",
- "filename_embd_weight_type_invalid",
- "task_page_size_min_limit",
- "task_page_size_float_not_allowed",
- "task_page_size_type_invalid",
- "pages_not_list",
- "pages_not_list_in_list",
- "pages_not_int_list",
- "graphrag_type_invalid",
- "graphrag_entity_types_not_list",
- "graphrag_entity_types_not_str_in_list",
- "graphrag_method_unknown",
- "graphrag_method_none",
- "graphrag_community_type_invalid",
- "graphrag_resolution_type_invalid",
- "raptor_type_invalid",
- "raptor_prompt_empty",
- "raptor_prompt_space",
- "raptor_max_token_max_limit",
- "raptor_max_token_type_invalid",
- "raptor_clustering_threshold_min_limit",
- "raptor_clustering_threshold_max_limit",
- "raptor_clustering_threshold_type_invalid",
- "raptor_max_cluster_min_limit",
- "raptor_max_cluster_max_limit",
- "raptor_max_cluster_float_not_allowed",
- "raptor_max_cluster_type_invalid",
- "raptor_random_seed_min_limit",
- "raptor_random_seed_float_not_allowed",
- "raptor_random_seed_type_invalid",
- "parser_config_type_invalid",
- "parent_child_type_invalid",
- "parent_child_delimiter_empty",
- ],
- )
- def test_parser_config_invalid(self, HttpApiAuth, name, parser_config, expected_message):
- payload = {"name": name, "parser_config": parser_config}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p2
- def test_parser_config_empty(self, HttpApiAuth):
- payload = {"name": "parser_config_empty", "parser_config": {}}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["parser_config"] == DEFAULT_PARSER_CONFIG, res
-
- @pytest.mark.p2
- def test_parser_config_unset(self, HttpApiAuth):
- payload = {"name": "parser_config_unset"}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["parser_config"] == DEFAULT_PARSER_CONFIG, res
-
- @pytest.mark.p3
- def test_parser_config_none(self, HttpApiAuth):
- payload = {"name": "parser_config_none", "parser_config": None}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
- assert res["data"]["parser_config"] == DEFAULT_PARSER_CONFIG, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload",
- [
- {"name": "id", "id": "id"},
- {"name": "tenant_id", "tenant_id": "e57c1966f99211efb41e9e45646e0111"},
- {"name": "created_by", "created_by": "created_by"},
- {"name": "create_date", "create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"name": "create_time", "create_time": 1741671443322},
- {"name": "update_date", "update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"name": "update_time", "update_time": 1741671443339},
- {"name": "document_count", "document_count": 1},
- {"name": "chunk_count", "chunk_count": 1},
- {"name": "token_num", "token_num": 1},
- {"name": "status", "status": "1"},
- {"name": "pagerank", "pagerank": 50},
- {"name": "unknown_field", "unknown_field": "unknown_field"},
- ],
- )
- def test_unsupported_field(self, HttpApiAuth, payload):
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "Extra inputs are not permitted" in res["message"], res
-
-
[email protected]("clear_datasets")
-class TestParserConfigBugFix:
- @pytest.mark.p1
- def test_parser_config_missing_raptor_and_graphrag(self, HttpApiAuth):
- payload = {"name": "test_parser_config_missing_fields", "parser_config": {"chunk_token_num": 1024}}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- parser_config = res["data"]["parser_config"]
- assert "raptor" in parser_config, "raptor field should be present"
- assert "graphrag" in parser_config, "graphrag field should be present"
- assert parser_config["raptor"]["use_raptor"] is False, "raptor.use_raptor should default to False"
- assert parser_config["graphrag"]["use_graphrag"] is False, "graphrag.use_graphrag should default to False"
- assert parser_config["chunk_token_num"] == 1024, "User-provided chunk_token_num should be preserved"
-
- @pytest.mark.p1
- def test_parser_config_with_only_raptor(self, HttpApiAuth):
- payload = {"name": "test_parser_config_only_raptor", "parser_config": {"chunk_token_num": 1024, "raptor": {"use_raptor": True}}}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- parser_config = res["data"]["parser_config"]
- assert parser_config["raptor"]["use_raptor"] is True, "User-provided raptor.use_raptor should be preserved"
- assert "graphrag" in parser_config, "graphrag field should be present"
- assert parser_config["graphrag"]["use_graphrag"] is False, "graphrag.use_graphrag should default to False"
-
- @pytest.mark.p1
- def test_parser_config_with_only_graphrag(self, HttpApiAuth):
- payload = {"name": "test_parser_config_only_graphrag", "parser_config": {"chunk_token_num": 1024, "graphrag": {"use_graphrag": True}}}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- parser_config = res["data"]["parser_config"]
- assert "raptor" in parser_config, "raptor field should be present"
- assert parser_config["raptor"]["use_raptor"] is False, "raptor.use_raptor should default to False"
- assert parser_config["graphrag"]["use_graphrag"] is True, "User-provided graphrag.use_graphrag should be preserved"
-
- @pytest.mark.p1
- def test_parser_config_with_both_fields(self, HttpApiAuth):
- payload = {"name": "test_parser_config_both_fields", "parser_config": {"chunk_token_num": 1024, "raptor": {"use_raptor": True}, "graphrag": {"use_graphrag": True}}}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- parser_config = res["data"]["parser_config"]
- assert parser_config["raptor"]["use_raptor"] is True, "User-provided raptor.use_raptor should be preserved"
- assert parser_config["graphrag"]["use_graphrag"] is True, "User-provided graphrag.use_graphrag should be preserved"
-
- @pytest.mark.p2
- @pytest.mark.parametrize("chunk_method", ["qa", "manual", "paper", "book", "laws", "presentation"])
- def test_parser_config_different_chunk_methods(self, HttpApiAuth, chunk_method):
- payload = {"name": f"test_parser_config_{chunk_method}", "chunk_method": chunk_method, "parser_config": {"chunk_token_num": 512}}
- res = create_dataset(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- parser_config = res["data"]["parser_config"]
- assert "raptor" in parser_config, f"raptor field should be present for {chunk_method}"
- assert "graphrag" in parser_config, f"graphrag field should be present for {chunk_method}"
- assert parser_config["raptor"]["use_raptor"] is False, f"raptor.use_raptor should default to False for {chunk_method}"
- assert parser_config["graphrag"]["use_graphrag"] is False, f"graphrag.use_graphrag should default to False for {chunk_method}"
diff --git a/test/testcases/test_http_api/test_dataset_management/test_delete_datasets.py b/test/testcases/test_http_api/test_dataset_management/test_delete_datasets.py
deleted file mode 100644
index 9381dd4539..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_delete_datasets.py
+++ /dev/null
@@ -1,220 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import uuid
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import (
- batch_create_datasets,
- delete_datasets,
- list_datasets,
-)
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = delete_datasets(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestRquest:
- @pytest.mark.p3
- def test_content_type_bad(self, HttpApiAuth):
- BAD_CONTENT_TYPE = "text/xml"
- res = delete_datasets(HttpApiAuth, headers={"Content-Type": BAD_CONTENT_TYPE})
- assert res["code"] == 101, res
- assert res["message"] == f"Unsupported content type: Expected application/json, got {BAD_CONTENT_TYPE}", res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ("a", "Malformed JSON syntax: Missing commas/brackets or invalid encoding"),
- ('"a"', "Invalid request payload: expected object, got str"),
- ],
- ids=["malformed_json_syntax", "invalid_request_payload_type"],
- )
- def test_payload_bad(self, HttpApiAuth, payload, expected_message):
- res = delete_datasets(HttpApiAuth, data=payload)
- assert res["code"] == 101, res
- assert res["message"] == expected_message, res
-
- @pytest.mark.p3
- def test_payload_unset(self, HttpApiAuth):
- res = delete_datasets(HttpApiAuth, None)
- assert res["code"] == 101, res
- assert res["message"] == "Malformed JSON syntax: Missing commas/brackets or invalid encoding", res
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_delete_dataset_1k(self, HttpApiAuth):
- ids = batch_create_datasets(HttpApiAuth, 1_000)
- res = delete_datasets(HttpApiAuth, {"ids": ids})
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 0, res
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, HttpApiAuth):
- count = 1_000
- ids = batch_create_datasets(HttpApiAuth, count)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(delete_datasets, HttpApiAuth, {"ids": ids[i : i + 1]}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
-class TestDatasetsDelete:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "func, expected_code, remaining",
- [
- (lambda r: {"ids": r[:1]}, 0, 2),
- (lambda r: {"ids": r}, 0, 0),
- ],
- ids=["single_dataset", "multiple_datasets"],
- )
- def test_ids(self, HttpApiAuth, add_datasets_func, func, expected_code, remaining):
- dataset_ids = add_datasets_func
- if callable(func):
- payload = func(dataset_ids)
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == expected_code, res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == remaining, res
-
- @pytest.mark.p2
- @pytest.mark.usefixtures("add_dataset_func")
- def test_ids_empty(self, HttpApiAuth):
- payload = {"ids": []}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 1, res
-
- @pytest.mark.p3
- @pytest.mark.usefixtures("add_datasets_func")
- def test_ids_none(self, HttpApiAuth):
- payload = {"ids": None}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 3, res
-
- @pytest.mark.p2
- @pytest.mark.usefixtures("add_dataset_func")
- def test_id_not_uuid(self, HttpApiAuth):
- payload = {"ids": ["not_uuid"]}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "Invalid UUID format" in res["message"], res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 1, res
-
- @pytest.mark.p3
- @pytest.mark.usefixtures("add_dataset_func")
- def test_id_not_uuid1(self, HttpApiAuth):
- payload = {"ids": [uuid.uuid4().hex]}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.usefixtures("add_dataset_func")
- def test_id_wrong_uuid(self, HttpApiAuth):
- payload = {"ids": ["d94a8dc02c9711f0930f7fbc369eab6d"]}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 1, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "func",
- [
- lambda r: {"ids": ["d94a8dc02c9711f0930f7fbc369eab6d"] + r},
- lambda r: {"ids": r[:1] + ["d94a8dc02c9711f0930f7fbc369eab6d"] + r[1:3]},
- lambda r: {"ids": r + ["d94a8dc02c9711f0930f7fbc369eab6d"]},
- ],
- )
- def test_ids_partial_invalid(self, HttpApiAuth, add_datasets_func, func):
- dataset_ids = add_datasets_func
- if callable(func):
- payload = func(dataset_ids)
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 3, res
-
- @pytest.mark.p2
- def test_ids_duplicate(self, HttpApiAuth, add_datasets_func):
- dataset_ids = add_datasets_func
- payload = {"ids": dataset_ids + dataset_ids}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "Duplicate ids:" in res["message"], res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 3, res
-
- @pytest.mark.p2
- def test_repeated_delete(self, HttpApiAuth, add_datasets_func):
- dataset_ids = add_datasets_func
- payload = {"ids": dataset_ids}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 0, res
-
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p3
- @pytest.mark.usefixtures("add_dataset_func")
- def test_field_unsupported(self, HttpApiAuth):
- payload = {"unknown_field": "unknown_field"}
- res = delete_datasets(HttpApiAuth, payload)
- assert res["code"] == 101, res
- assert "Extra inputs are not permitted" in res["message"], res
-
- res = list_datasets(HttpApiAuth)
- assert len(res["data"]) == 1, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_dify_retrieval_routes_unit.py b/test/testcases/test_http_api/test_dataset_management/test_dify_retrieval_routes_unit.py
deleted file mode 100644
index ada3f86d23..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_dify_retrieval_routes_unit.py
+++ /dev/null
@@ -1,541 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import inspect
-import sys
-from copy import deepcopy
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _DummyKB:
- def __init__(self, tenant_id="tenant-1", embd_id="embd-1", tenant_embd_id="tm-embd-1"):
- self.tenant_id = tenant_id
- self.embd_id = embd_id
- self.tenant_embd_id = tenant_embd_id
-
-
-class _DummyRetriever:
- async def retrieval(self, *_args, **_kwargs):
- return {"chunks": [{"doc_id": "doc-1", "content_with_weight": "chunk-content", "similarity": 0.8, "docnm_kwd": "doc-title", "vector": [0.1]}]}
-
- def retrieval_by_children(self, chunks, _tenant_ids):
- return chunks
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_dify_retrieval_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- # ---- Stub all modules imported by dify_retrieval_api.py BEFORE exec_module ----
- # 1. common and sub-modules (NO __path__ to avoid real imports)
- common_pkg = ModuleType("common")
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- class _DummyRetCode:
- SUCCESS = 0
- ARGUMENT_ERROR = 1
- NOT_FOUND = 2
- SERVER_ERROR = 3
- AUTHENTICATION_ERROR = 4
-
- class _DummyLLMType:
- CHAT = "chat"
- EMBEDDING = "embedding"
-
- common_constants_mod = ModuleType("common.constants")
- common_constants_mod.RetCode = _DummyRetCode()
- common_constants_mod.LLMType = _DummyLLMType()
- monkeypatch.setitem(sys.modules, "common.constants", common_constants_mod)
-
- common_settings_mod = ModuleType("common.settings")
- common_settings_mod.retriever = None
- common_settings_mod.kg_retriever = None
- monkeypatch.setitem(sys.modules, "common.settings", common_settings_mod)
-
- common_metadata_utils_mod = ModuleType("common.metadata_utils")
- common_metadata_utils_mod.meta_filter = lambda *_args, **_kwargs: []
- common_metadata_utils_mod.convert_conditions = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "common.metadata_utils", common_metadata_utils_mod)
-
- # 2. quart + werkzeug (avoid heavy web framework imports)
- qt_mod = ModuleType("quart")
- qt_mod.jsonify = lambda *_args, **_kwargs: None
- qt_mod.request = SimpleNamespace(method="POST", args={})
- monkeypatch.setitem(sys.modules, "quart", qt_mod)
-
- werkzeug_exc_mod = ModuleType("werkzeug.exceptions")
- werkzeug_exc_mod.BadRequest = type("BadRequest", (Exception,), {})
- monkeypatch.setitem(sys.modules, "werkzeug.exceptions", werkzeug_exc_mod)
-
- qt_exc_mod = ModuleType("quart.exceptions")
- qt_exc_mod.BadRequest = type("BadRequest", (Exception,), {})
- monkeypatch.setitem(sys.modules, "quart.exceptions", qt_exc_mod)
-
- # 3. api.db.services.* (avoid Peewee ORM imports)
- api_db_svc_mod = ModuleType("api.db.services")
- api_db_svc_mod.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", api_db_svc_mod)
-
- class _StubDocumentService:
- @staticmethod
- def get_by_ids(*_args, **_kwargs):
- return []
-
- doc_svc_mod = ModuleType("api.db.services.document_service")
- doc_svc_mod.DocumentService = _StubDocumentService
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", doc_svc_mod)
-
- class _StubDocMetadataService:
- @staticmethod
- def get_flatted_meta_by_kbs(*_args, **_kwargs):
- return []
-
- doc_metadata_mod = ModuleType("api.db.services.doc_metadata_service")
- doc_metadata_mod.DocMetadataService = _StubDocMetadataService
- monkeypatch.setitem(sys.modules, "api.db.services.doc_metadata_service", doc_metadata_mod)
-
- class _StubKnowledgebaseService:
- @staticmethod
- def get_by_id(*_args, **_kwargs):
- return True, SimpleNamespace(tenant_id="tenant-1", embd_id="embd-1", tenant_embd_id="tm-embd-1")
-
- @staticmethod
- def accessible(*_args, **_kwargs):
- return True
-
- kb_svc_mod = ModuleType("api.db.services.knowledgebase_service")
- kb_svc_mod.KnowledgebaseService = _StubKnowledgebaseService
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_svc_mod)
-
- # Mock tenant_llm_service for TenantLLMService and TenantService
- tenant_llm_service_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _MockModelConfig:
- def __init__(self, tenant_id, model_name):
- self.tenant_id = tenant_id
- self.llm_name = model_name
- self.llm_factory = "Builtin"
- self.api_key = "fake-api-key"
- self.api_base = "https://api.example.com"
- self.model_type = "chat"
- self.max_tokens = 8192
- self.used_tokens = 0
- self.status = 1
- self.id = 1
-
- def to_dict(self):
- return {
- "tenant_id": self.tenant_id,
- "llm_name": self.llm_name,
- "llm_factory": self.llm_factory,
- "api_key": self.api_key,
- "api_base": self.api_base,
- "model_type": self.model_type,
- "max_tokens": self.max_tokens,
- "used_tokens": self.used_tokens,
- "status": self.status,
- "id": self.id,
- }
-
- class _StubTenantService:
- @staticmethod
- def get_by_id(tenant_id):
- # Return a mock tenant with default model configurations
- return True, SimpleNamespace(id=tenant_id, llm_id="chat-model", embd_id="embd-model", asr_id="asr-model", img2txt_id="img2txt-model", rerank_id="rerank-model", tts_id="tts-model")
-
- class _StubTenantLLMService:
- @staticmethod
- def get_api_key(tenant_id, model_name):
- return _MockModelConfig(tenant_id, model_name)
-
- @staticmethod
- def split_model_name_and_factory(model_name):
- if "@" in model_name:
- parts = model_name.split("@")
- return parts[0], parts[1]
- return model_name, None
-
- tenant_llm_service_mod.TenantService = _StubTenantService
- tenant_llm_service_mod.TenantLLMService = _StubTenantLLMService
-
- class _StubLLMFactoriesService:
- pass
-
- tenant_llm_service_mod.LLMFactoriesService = _StubLLMFactoriesService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_service_mod)
-
- # Mock llm_service for LLMService
- llm_service_mod = ModuleType("api.db.services.llm_service")
-
- class _StubLLM:
- def __init__(self, llm_name):
- self.llm_name = llm_name
- self.is_tools = False
-
- class _StubLLMBundle:
- def __init__(self, tenant_id: str, model_config: dict, lang="Chinese", **kwargs):
- self.tenant_id = tenant_id
- self.model_config = model_config
- self.lang = lang
-
- def encode(self, texts: list):
- import numpy as np
-
- # Return mock embeddings and token usage
- return [np.array([0.1, 0.2, 0.3]) for _ in texts], len(texts) * 10
-
- llm_service_mod.LLMService = SimpleNamespace(query=lambda llm_name: [_StubLLM(llm_name)] if llm_name else [])
- llm_service_mod.LLMBundle = _StubLLMBundle
- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)
-
- # 4. api.utils.api_utils
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.add_tenant_id_to_kwargs = lambda f: f
- api_utils_mod.build_error_result = lambda message, code: {"code": code, "message": message}
- api_utils_mod.get_request_json = lambda: {}
- api_utils_mod.get_json_result = lambda data=None, message="": {"data": data, "message": message}
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- # 5. rag.app.tag
- tag_mod = ModuleType("rag.app.tag")
- tag_mod.label_question = lambda *_args, **_kwargs: []
- monkeypatch.setitem(sys.modules, "rag.app.tag", tag_mod)
-
- # 6. api.apps
- api_apps_mod = ModuleType("api.apps")
- api_apps_mod.current_user = SimpleNamespace(id="tenant-1")
- api_apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", api_apps_mod)
-
- # 7. deepdoc stubs
- deepdoc_pkg = ModuleType("deepdoc")
- deepdoc_parser_pkg = ModuleType("deepdoc.parser")
- deepdoc_parser_pkg.__path__ = []
-
- class _StubPdfParser:
- pass
-
- class _StubExcelParser:
- pass
-
- class _StubDocxParser:
- pass
-
- deepdoc_parser_pkg.PdfParser = _StubPdfParser
- deepdoc_parser_pkg.ExcelParser = _StubExcelParser
- deepdoc_parser_pkg.DocxParser = _StubDocxParser
- deepdoc_pkg.parser = deepdoc_parser_pkg
- monkeypatch.setitem(sys.modules, "deepdoc", deepdoc_pkg)
- monkeypatch.setitem(sys.modules, "deepdoc.parser", deepdoc_parser_pkg)
-
- deepdoc_excel_module = ModuleType("deepdoc.parser.excel_parser")
- deepdoc_excel_module.RAGFlowExcelParser = _StubExcelParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.excel_parser", deepdoc_excel_module)
-
- deepdoc_parser_utils = ModuleType("deepdoc.parser.utils")
- deepdoc_parser_utils.get_text = lambda *_args, **_kwargs: ""
- monkeypatch.setitem(sys.modules, "deepdoc.parser.utils", deepdoc_parser_utils)
- monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))
-
- # 8. api.db.joint_services.tenant_model_service
- class _MockModelConfig2:
- def __init__(self, tenant_id, model_name):
- self.tenant_id = tenant_id
- self.llm_name = model_name
- self.llm_factory = "Builtin"
- self.api_key = "fake-api-key"
- self.api_base = "https://api.example.com"
- self.model_type = "chat"
- self.max_tokens = 8192
- self.used_tokens = 0
- self.status = 1
- self.id = 1
-
- def to_dict(self):
- return {
- "tenant_id": self.tenant_id,
- "llm_name": self.llm_name,
- "llm_factory": self.llm_factory,
- "api_key": self.api_key,
- "api_base": self.api_base,
- "model_type": self.model_type,
- "max_tokens": self.max_tokens,
- "used_tokens": self.used_tokens,
- "status": self.status,
- "id": self.id,
- }
-
- def _get_model_config_by_id(
- tenant_model_id: str,
- allowed_tenant_ids=None,
- requester_tenant_id=None,
- ) -> dict:
- mock_tenant_id = "tenant-1"
- if allowed_tenant_ids is not None:
- if isinstance(allowed_tenant_ids, str):
- allowed_tenant_ids = {allowed_tenant_ids}
- else:
- allowed_tenant_ids = {str(tenant_id) for tenant_id in allowed_tenant_ids if tenant_id}
- if mock_tenant_id not in allowed_tenant_ids and str(requester_tenant_id) != mock_tenant_id:
- raise LookupError(f"Tenant Model with id {tenant_model_id} not authorized")
- return _MockModelConfig2(mock_tenant_id, "model-1").to_dict()
-
- def _get_model_config_from_provider_instance(tenant_id: str, model_type: str, model_name: str):
- if not model_name:
- raise Exception("Model Name is required")
- return _MockModelConfig2(tenant_id, model_name).to_dict()
-
- def _get_tenant_default_model_by_type(tenant_id: str, model_type):
- # Return mock tenant with default model configurations
- return _MockModelConfig2(tenant_id, "chat-model").to_dict()
-
- tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
- tenant_model_service_mod.get_model_config_by_id = _get_model_config_by_id
- tenant_model_service_mod.get_model_config_from_provider_instance = _get_model_config_from_provider_instance
- tenant_model_service_mod.resolve_model_config = _get_model_config_from_provider_instance
- tenant_model_service_mod.get_tenant_default_model_by_type = _get_tenant_default_model_by_type
- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
-
- # ---- Load the real module ----
- module_name = "test_dify_retrieval_routes_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "dify_retrieval_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def _set_request_json(monkeypatch, module, payload):
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(deepcopy(payload)))
-
-
-def test_retrieval_success_with_metadata_and_kg(monkeypatch):
- module = _load_dify_retrieval_module(monkeypatch)
- _set_request_json(
- monkeypatch,
- module,
- {
- "knowledge_id": "kb-1",
- "query": "hello",
- "use_kg": True,
- "retrieval_setting": {"score_threshold": 0.1, "top_k": 3},
- "metadata_condition": {"conditions": [{"name": "author", "comparison_operator": "is", "value": "alice"}], "logic": "and"},
- },
- )
-
- monkeypatch.setattr(module, "jsonify", lambda payload: payload)
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kbs: [{"doc_id": "doc-1"}])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, _DummyKB()))
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda _kb_id, _tenant_id: True)
- monkeypatch.setattr(module, "convert_conditions", lambda cond: cond.get("conditions", []))
- monkeypatch.setattr(module, "meta_filter", lambda *_args, **_kwargs: [])
-
- retriever = _DummyRetriever()
- monkeypatch.setattr(module.settings, "retriever", retriever)
-
- class _DummyKgRetriever:
- async def retrieval(self, *_args, **_kwargs):
- return {
- "doc_id": "doc-2",
- "content_with_weight": "kg-content",
- "similarity": 0.9,
- "docnm_kwd": "kg-title",
- }
-
- monkeypatch.setattr(module.settings, "kg_retriever", _DummyKgRetriever())
- monkeypatch.setattr(
- module.DocumentService,
- "get_by_ids",
- lambda doc_ids, cols=None: [SimpleNamespace(id=doc_id, meta_fields={"origin": f"meta-{doc_id}"}) for doc_id in doc_ids],
- )
- monkeypatch.setattr(module, "label_question", lambda *_args, **_kwargs: [])
-
- res = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert "records" in res, res
- assert len(res["records"]) == 2, res
- top = res["records"][0]
- assert top["title"] == "kg-title", res
- assert top["metadata"]["doc_id"] == "doc-2", res
- assert "score" in top, res
-
-
-def test_retrieval_kb_not_found(monkeypatch):
- module = _load_dify_retrieval_module(monkeypatch)
- _set_request_json(monkeypatch, module, {"knowledge_id": "kb-missing", "query": "hello"})
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kbs: [])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
-
- res = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert res["code"] == module.RetCode.NOT_FOUND, res
- assert "Knowledgebase not found" in res["message"], res
-
-
-def test_retrieval_not_found_exception_mapping(monkeypatch):
- module = _load_dify_retrieval_module(monkeypatch)
- _set_request_json(monkeypatch, module, {"knowledge_id": "kb-1", "query": "hello"})
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kbs: [])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, _DummyKB()))
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda _kb_id, _tenant_id: True)
- monkeypatch.setattr(module, "label_question", lambda *_args, **_kwargs: [])
-
- class _BrokenRetriever:
- async def retrieval(self, *_args, **_kwargs):
- raise RuntimeError("chunk_not_found_error")
-
- monkeypatch.setattr(module.settings, "retriever", _BrokenRetriever())
- monkeypatch.setattr(module.logger, "exception", lambda *args, **kwargs: None)
-
- res = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert res["code"] == module.RetCode.NOT_FOUND, res
- assert "No chunk found" in res["message"], res
-
-
-def test_retrieval_generic_exception_mapping(monkeypatch):
- module = _load_dify_retrieval_module(monkeypatch)
- _set_request_json(monkeypatch, module, {"knowledge_id": "kb-1", "query": "hello"})
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kbs: [])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, _DummyKB()))
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda _kb_id, _tenant_id: True)
- monkeypatch.setattr(module, "label_question", lambda *_args, **_kwargs: [])
-
- class _BrokenRetriever:
- async def retrieval(self, *_args, **_kwargs):
- raise RuntimeError("boom")
-
- monkeypatch.setattr(module.settings, "retriever", _BrokenRetriever())
- monkeypatch.setattr(module.logger, "exception", lambda *args, **kwargs: None)
-
- res = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert res["code"] == module.RetCode.SERVER_ERROR, res
- assert "boom" in res["message"], res
-
-
-def test_read_retrieval_request_from_get_args(monkeypatch):
- module = _load_dify_retrieval_module(monkeypatch)
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- method="GET",
- args={
- "knowledge_id": "kb-1",
- "query": "hello",
- "use_kg": "true",
- "top_k": "12",
- "score_threshold": "0.66",
- },
- ),
- )
-
- req = _run(module._read_retrieval_request())
- assert req["knowledge_id"] == "kb-1", req
- assert req["query"] == "hello", req
- assert req["use_kg"] is True, req
- assert req["retrieval_setting"]["top_k"] == 12, req
- assert req["retrieval_setting"]["score_threshold"] == 0.66, req
-
-
-def test_read_retrieval_request_from_post_json(monkeypatch):
- module = _load_dify_retrieval_module(monkeypatch)
- payload = {"knowledge_id": "kb-1", "query": "hello"}
- monkeypatch.setattr(module, "request", SimpleNamespace(method="POST", args={}))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(payload))
-
- req = _run(module._read_retrieval_request())
- assert req == payload, req
-
-
-def test_retrieval_argument_error_messages(monkeypatch):
- """Guard: distinguish malformed vs missing argument errors."""
- module = _load_dify_retrieval_module(monkeypatch)
-
- # Case 1: malformed numeric options in retrieval_setting
- _set_request_json(
- monkeypatch,
- module,
- {
- "knowledge_id": "kb-1",
- "query": "hello",
- "retrieval_setting": {"top_k": "not-int", "score_threshold": "not-float"},
- },
- )
- res = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
- assert "invalid or malformed arguments:" in res["message"], res
-
- # Case 2: missing required fields (knowledge_id, query)
- _set_request_json(monkeypatch, module, {})
- res_missing = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert res_missing["code"] == module.RetCode.ARGUMENT_ERROR, res_missing
- assert "required arguments are missing:" in res_missing["message"], res_missing
-
- # Case 3: partially missing required field (query)
- _set_request_json(monkeypatch, module, {"knowledge_id": "kb-1"})
- res_missing_query = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert res_missing_query["code"] == module.RetCode.ARGUMENT_ERROR, res_missing_query
- assert "query" in res_missing_query["message"], res_missing_query
-
- # Case 4: retrieval_setting wrong type
- _set_request_json(
- monkeypatch,
- module,
- {"knowledge_id": "kb-1", "query": "hello", "retrieval_setting": "bad-type"},
- )
- res_wrong_type = _run(inspect.unwrap(module.retrieval)("tenant-1"))
- assert res_wrong_type["code"] == module.RetCode.ARGUMENT_ERROR, res_wrong_type
- assert "retrieval_setting must be an object" in res_wrong_type["message"], res_wrong_type
diff --git a/test/testcases/test_http_api/test_dataset_management/test_flattened_metadata.py b/test/testcases/test_http_api/test_dataset_management/test_flattened_metadata.py
deleted file mode 100644
index d67e66ce06..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_flattened_metadata.py
+++ /dev/null
@@ -1,42 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import get_flattened_metadata
-
-
[email protected]("clear_datasets")
-class TestFlattenedMetadata:
- @pytest.mark.p2
- def test_get_flattened_metadata_success(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = get_flattened_metadata(HttpApiAuth, [dataset_id])
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_get_flattened_metadata_multiple_datasets(self, HttpApiAuth, add_datasets_func):
- dataset_ids = add_datasets_func
- res = get_flattened_metadata(HttpApiAuth, dataset_ids)
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_get_flattened_metadata_empty_ids(self, HttpApiAuth):
- res = get_flattened_metadata(HttpApiAuth, [])
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_get_flattened_metadata_invalid_id(self, HttpApiAuth):
- res = get_flattened_metadata(HttpApiAuth, ["invalid_id"])
- assert res["code"] != 0, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_get_dataset.py b/test/testcases/test_http_api/test_dataset_management/test_get_dataset.py
deleted file mode 100644
index 92df5ea679..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_get_dataset.py
+++ /dev/null
@@ -1,45 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import get_dataset
-from libs.auth import RAGFlowHttpApiAuth
-from configs import INVALID_API_TOKEN
-
-
[email protected]("clear_datasets")
-class TestGetDataset:
- @pytest.mark.p2
- def test_get_dataset_success(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = get_dataset(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
- assert res["data"]["id"] == dataset_id, res
-
- @pytest.mark.p2
- def test_get_dataset_invalid_id(self, HttpApiAuth):
- res = get_dataset(HttpApiAuth, "invalid_dataset_id")
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_get_dataset_unauthorized(self, add_dataset_func):
- dataset_id = add_dataset_func
- res = get_dataset(RAGFlowHttpApiAuth(INVALID_API_TOKEN), dataset_id)
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_get_dataset_nonexistent(self, HttpApiAuth):
- res = get_dataset(HttpApiAuth, "0" * 32)
- assert res["code"] != 0, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_index_api.py b/test/testcases/test_http_api/test_dataset_management/test_index_api.py
deleted file mode 100644
index d97691223d..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_index_api.py
+++ /dev/null
@@ -1,166 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import (
- bulk_upload_documents,
- list_documents,
- run_index,
- trace_index,
- delete_index,
-)
-from utils import wait_for
-
-
-@wait_for(200, 1, "Document parsing timeout")
-def _parse_done(auth, dataset_id, document_ids=None):
- res = list_documents(auth, dataset_id)
- if res.get("code") != 0:
- return False
- target_docs = res.get("data", {}).get("docs", [])
- if not target_docs:
- return False
- if document_ids is None:
- return all(doc.get("run") == "DONE" for doc in target_docs)
- target_ids = set(document_ids)
- seen_ids = set()
- for doc in target_docs:
- doc_id = doc.get("id")
- if doc_id in target_ids:
- seen_ids.add(doc_id)
- if doc.get("run") != "DONE":
- return False
- return seen_ids == target_ids
-
-
-@wait_for(60, 1, "Index task creation timeout")
-def _index_task_created(auth, dataset_id, index_type):
- res = trace_index(auth, dataset_id, index_type)
- if res.get("code") != 0:
- return False
- return bool(res.get("data", {}).get("id"))
-
-
[email protected]("clear_datasets")
-class TestRunIndex:
- @pytest.mark.p2
- def test_run_index_graph(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = run_index(HttpApiAuth, dataset_id, "graph")
- assert res["code"] == 0, res
- assert res["data"].get("task_id"), res
-
- @pytest.mark.p2
- def test_run_index_raptor(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = run_index(HttpApiAuth, dataset_id, "raptor")
- assert res["code"] == 0, res
- assert res["data"].get("task_id"), res
-
- @pytest.mark.p2
- def test_run_index_mindmap(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = run_index(HttpApiAuth, dataset_id, "mindmap")
- assert res["code"] == 0, res
- assert res["data"].get("task_id"), res
-
- @pytest.mark.p2
- def test_run_index_invalid_type(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = run_index(HttpApiAuth, dataset_id, "invalid_type")
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_run_index_no_documents(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = run_index(HttpApiAuth, dataset_id, "raptor")
- assert res["code"] == 102, res
-
-
[email protected]("clear_datasets")
-class TestDeleteIndex:
- @pytest.mark.p2
- def test_delete_graph(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = delete_index(HttpApiAuth, dataset_id, "graph")
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_delete_raptor(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = delete_index(HttpApiAuth, dataset_id, "raptor")
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_delete_mindmap(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = delete_index(HttpApiAuth, dataset_id, "mindmap")
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_delete_invalid_type(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = delete_index(HttpApiAuth, dataset_id, "invalid_type")
- assert res["code"] != 0, res
-
-
[email protected]("clear_datasets")
-class TestTraceIndex:
- @pytest.mark.p2
- def test_trace_index_graph(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = run_index(HttpApiAuth, dataset_id, "graph")
- assert res["code"] == 0, res
- _index_task_created(HttpApiAuth, dataset_id, "graph")
- res = trace_index(HttpApiAuth, dataset_id, "graph")
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_trace_index_raptor(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = run_index(HttpApiAuth, dataset_id, "raptor")
- assert res["code"] == 0, res
- _index_task_created(HttpApiAuth, dataset_id, "raptor")
- res = trace_index(HttpApiAuth, dataset_id, "raptor")
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_trace_index_mindmap(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = run_index(HttpApiAuth, dataset_id, "mindmap")
- assert res["code"] == 0, res
- _index_task_created(HttpApiAuth, dataset_id, "mindmap")
- res = trace_index(HttpApiAuth, dataset_id, "mindmap")
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_trace_index_invalid_type(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = trace_index(HttpApiAuth, dataset_id, "invalid_type")
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_trace_index_no_task(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = trace_index(HttpApiAuth, dataset_id, "graph")
- assert res["code"] == 0, res
- assert res["data"] == {}
diff --git a/test/testcases/test_http_api/test_dataset_management/test_ingestion_logs.py b/test/testcases/test_http_api/test_dataset_management/test_ingestion_logs.py
deleted file mode 100644
index 8dcb58c313..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_ingestion_logs.py
+++ /dev/null
@@ -1,71 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import list_ingestion_logs, get_ingestion_log
-
-
[email protected]("clear_datasets")
-class TestListIngestionLogs:
- @pytest.mark.p2
- def test_list_ingestion_logs_success(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = list_ingestion_logs(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
- assert "total" in res["data"], res
- assert "logs" in res["data"], res
-
- @pytest.mark.p2
- def test_list_ingestion_logs_with_pagination(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = list_ingestion_logs(HttpApiAuth, dataset_id, params={"page": 1, "page_size": 10})
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_list_ingestion_logs_invalid_id(self, HttpApiAuth):
- res = list_ingestion_logs(HttpApiAuth, "invalid_id")
- assert res["code"] != 0, res
-
-
[email protected]("clear_datasets")
-class TestGetIngestionLog:
- @pytest.mark.p2
- def test_get_ingestion_log_not_found(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = get_ingestion_log(HttpApiAuth, dataset_id, "nonexistent_log_id")
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_get_ingestion_log_invalid_dataset(self, HttpApiAuth):
- res = get_ingestion_log(HttpApiAuth, "invalid_id", "some_log_id")
- assert res["code"] != 0, res
-
-
[email protected]("clear_datasets")
-class TestListIngestionLogsEdgeCases:
- @pytest.mark.p3
- def test_list_ingestion_logs_abnormal_date_filter(self, HttpApiAuth, add_dataset_func):
- """Test list ingestion logs when create_date_from > create_date_to."""
- dataset_id = add_dataset_func
- res = list_ingestion_logs(
- HttpApiAuth,
- dataset_id,
- params={
- "desc": "false",
- "create_date_from": "2025-02-01",
- "create_date_to": "2025-01-01",
- },
- )
- assert res["code"] != 0, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_ingestion_summary.py b/test/testcases/test_http_api/test_dataset_management/test_ingestion_summary.py
deleted file mode 100644
index 3dc8b7aee6..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_ingestion_summary.py
+++ /dev/null
@@ -1,35 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import get_ingestion_summary
-
-
[email protected]("clear_datasets")
-class TestIngestionSummary:
- @pytest.mark.p2
- def test_ingestion_summary_success(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = get_ingestion_summary(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
- assert "doc_num" in res["data"], res
- assert "chunk_num" in res["data"], res
- assert "token_num" in res["data"], res
- assert "status" in res["data"], res
-
- @pytest.mark.p2
- def test_ingestion_summary_invalid_id(self, HttpApiAuth):
- res = get_ingestion_summary(HttpApiAuth, "invalid_id")
- assert res["code"] != 0, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_knowledge_graph.py b/test/testcases/test_http_api/test_dataset_management/test_knowledge_graph.py
deleted file mode 100644
index f2ada816a5..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_knowledge_graph.py
+++ /dev/null
@@ -1,53 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import delete_knowledge_graph, knowledge_graph
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = knowledge_graph(invalid_auth, "dataset_id")
- assert res["code"] == expected_code
- assert expected_message in res.get("message", "")
-
-
-class TestKnowledgeGraph:
- @pytest.mark.p2
- def test_get_knowledge_graph_empty(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = knowledge_graph(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
- assert "graph" in res["data"], res
- assert "mind_map" in res["data"], res
- assert isinstance(res["data"]["graph"], dict), res
- assert isinstance(res["data"]["mind_map"], dict), res
-
- @pytest.mark.p2
- def test_delete_knowledge_graph(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = delete_knowledge_graph(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
- assert res["data"] is not None, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_list_datasets.py b/test/testcases/test_http_api/test_dataset_management/test_list_datasets.py
deleted file mode 100644
index 146ca9d2da..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_list_datasets.py
+++ /dev/null
@@ -1,342 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import uuid
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import list_datasets
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import is_sorted
-
-
-class TestAuthorization:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = list_datasets(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_concurrent_list(self, HttpApiAuth):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_datasets, HttpApiAuth) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
[email protected]("add_datasets")
-class TestDatasetsList:
- @pytest.mark.p2
- def test_params_unset(self, HttpApiAuth):
- res = list_datasets(HttpApiAuth, None)
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p2
- def test_params_empty(self, HttpApiAuth):
- res = list_datasets(HttpApiAuth, {})
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page": 2, "page_size": 2}, 2),
- ({"page": 3, "page_size": 2}, 1),
- ({"page": 4, "page_size": 2}, 0),
- ({"page": "2", "page_size": 2}, 2),
- ({"page": 1, "page_size": 10}, 5),
- ],
- ids=["normal_middle_page", "normal_last_partial_page", "beyond_max_page", "string_page_number", "full_data_single_page"],
- )
- def test_page(self, HttpApiAuth, params, expected_page_size):
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == expected_page_size, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_code, expected_message",
- [
- ({"page": 0}, 101, "Input should be greater than or equal to 1"),
- ({"page": "a"}, 101, "Input should be a valid integer, unable to parse string as an integer"),
- ],
- ids=["page_0", "page_a"],
- )
- def test_page_invalid(self, HttpApiAuth, params, expected_code, expected_message):
- res = list_datasets(HttpApiAuth, params=params)
- assert res["code"] == expected_code, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p2
- def test_page_none(self, HttpApiAuth):
- params = {"page": None}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page_size": 1}, 1),
- ({"page_size": 3}, 3),
- ({"page_size": 5}, 5),
- ({"page_size": 6}, 5),
- ({"page_size": "1"}, 1),
- ],
- ids=["min_valid_page_size", "medium_page_size", "page_size_equals_total", "page_size_exceeds_total", "string_type_page_size"],
- )
- def test_page_size(self, HttpApiAuth, params, expected_page_size):
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == expected_page_size, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_code, expected_message",
- [
- ({"page_size": 0}, 101, "Input should be greater than or equal to 1"),
- ({"page_size": "a"}, 101, "Input should be a valid integer, unable to parse string as an integer"),
- ],
- )
- def test_page_size_invalid(self, HttpApiAuth, params, expected_code, expected_message):
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == expected_code, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p2
- def test_page_size_none(self, HttpApiAuth):
- params = {"page_size": None}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, assertions",
- [
- ({"orderby": "create_time"}, lambda r: is_sorted(r["data"], "create_time", True)),
- ({"orderby": "update_time"}, lambda r: is_sorted(r["data"], "update_time", True)),
- ],
- ids=["orderby_create_time", "orderby_update_time"],
- )
- def test_orderby(self, HttpApiAuth, params, assertions):
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- if callable(assertions):
- assert assertions(res), res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params",
- [
- {"orderby": ""},
- {"orderby": "unknown"},
- {"orderby": "CREATE_TIME"},
- {"orderby": "UPDATE_TIME"},
- {"orderby": " create_time "},
- ],
- ids=["empty", "unknown", "orderby_create_time_upper", "orderby_update_time_upper", "whitespace"],
- )
- def test_orderby_invalid(self, HttpApiAuth, params):
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 101, res
- assert "Input should be 'create_time' or 'update_time'" in res["message"], res
-
- @pytest.mark.p3
- def test_orderby_none(self, HttpApiAuth):
- params = {"orderby": None}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert is_sorted(res["data"], "create_time", True), res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, assertions",
- [
- ({"desc": True}, lambda r: is_sorted(r["data"], "create_time", True)),
- ({"desc": False}, lambda r: is_sorted(r["data"], "create_time", False)),
- ({"desc": "true"}, lambda r: is_sorted(r["data"], "create_time", True)),
- ({"desc": "false"}, lambda r: is_sorted(r["data"], "create_time", False)),
- ({"desc": 1}, lambda r: is_sorted(r["data"], "create_time", True)),
- ({"desc": 0}, lambda r: is_sorted(r["data"], "create_time", False)),
- ({"desc": "yes"}, lambda r: is_sorted(r["data"], "create_time", True)),
- ({"desc": "no"}, lambda r: is_sorted(r["data"], "create_time", False)),
- ({"desc": "y"}, lambda r: is_sorted(r["data"], "create_time", True)),
- ({"desc": "n"}, lambda r: is_sorted(r["data"], "create_time", False)),
- ],
- ids=["desc=True", "desc=False", "desc=true", "desc=false", "desc=1", "desc=0", "desc=yes", "desc=no", "desc=y", "desc=n"],
- )
- def test_desc(self, HttpApiAuth, params, assertions):
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- if callable(assertions):
- assert assertions(res), res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params",
- [
- {"desc": 3.14},
- {"desc": "unknown"},
- ],
- ids=["empty", "unknown"],
- )
- def test_desc_invalid(self, HttpApiAuth, params):
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 101, res
- assert "Input should be a valid boolean, unable to interpret input" in res["message"], res
-
- @pytest.mark.p3
- def test_desc_none(self, HttpApiAuth):
- params = {"desc": None}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert is_sorted(res["data"], "create_time", True), res
-
- @pytest.mark.p1
- def test_name(self, HttpApiAuth):
- params = {"name": "dataset_1"}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == 1, res
- assert res["data"][0]["name"] == "dataset_1", res
-
- @pytest.mark.p2
- def test_name_wrong(self, HttpApiAuth):
- params = {"name": "wrong name"}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p2
- def test_name_empty(self, HttpApiAuth):
- params = {"name": ""}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p2
- def test_name_none(self, HttpApiAuth):
- params = {"name": None}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p1
- def test_id(self, HttpApiAuth, add_datasets):
- dataset_ids = add_datasets
- params = {"id": dataset_ids[0]}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0
- assert len(res["data"]) == 1
- assert res["data"][0]["id"] == dataset_ids[0]
-
- @pytest.mark.p2
- def test_id_not_uuid(self, HttpApiAuth):
- params = {"id": "not_uuid"}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 101, res
- assert "Invalid UUID format" in res["message"], res
-
- @pytest.mark.p2
- def test_id_not_uuid1(self, HttpApiAuth):
- params = {"id": uuid.uuid4().hex}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p2
- def test_id_wrong_uuid(self, HttpApiAuth):
- params = {"id": "d94a8dc02c9711f0930f7fbc369eab6d"}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p2
- def test_id_empty(self, HttpApiAuth):
- params = {"id": ""}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 101, res
- assert "Invalid UUID format" in res["message"], res
-
- @pytest.mark.p2
- def test_id_none(self, HttpApiAuth):
- params = {"id": None}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "func, name, expected_num",
- [
- (lambda r: r[0], "dataset_0", 1),
- (lambda r: r[0], "dataset_1", 0),
- ],
- ids=["name_and_id_match", "name_and_id_mismatch"],
- )
- def test_name_and_id(self, HttpApiAuth, add_datasets, func, name, expected_num):
- dataset_ids = add_datasets
- if callable(func):
- params = {"id": func(dataset_ids), "name": name}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]) == expected_num, res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, name",
- [
- (lambda r: r[0], "wrong_name"),
- (uuid.uuid1().hex, "dataset_0"),
- ],
- ids=["name", "id"],
- )
- def test_name_and_id_wrong(self, HttpApiAuth, add_datasets, dataset_id, name):
- dataset_ids = add_datasets
- if callable(dataset_id):
- params = {"id": dataset_id(dataset_ids), "name": name}
- else:
- params = {"id": dataset_id, "name": name}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p3
- def test_field_unsupported(self, HttpApiAuth):
- params = {"unknown_field": "unknown_field"}
- res = list_datasets(HttpApiAuth, params)
- assert res["code"] == 101, res
- assert "Extra inputs are not permitted" in res["message"], res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_search.py b/test/testcases/test_http_api/test_dataset_management/test_search.py
deleted file mode 100644
index 63f8ea92e4..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_search.py
+++ /dev/null
@@ -1,83 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import search_dataset, knowledge_graph
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = search_dataset(invalid_auth, "dataset_id", {"question": "test"})
- assert res["code"] == expected_code
- assert expected_message in res.get("message", "")
-
-
-class TestDatasetSearch:
- @pytest.mark.p2
- def test_search_without_question(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = search_dataset(HttpApiAuth, dataset_id, {})
- assert res["code"] == 101, res
-
- @pytest.mark.p2
- def test_search_basic(self, HttpApiAuth, add_chunks):
- dataset_id, document_id, _ = add_chunks
- res = search_dataset(HttpApiAuth, dataset_id, {"question": "chunk"})
- assert res["code"] == 0, res
- assert "chunks" in res["data"], res
-
- @pytest.mark.p2
- def test_search_with_doc_ids(self, HttpApiAuth, add_chunks):
- dataset_id, document_id, _ = add_chunks
- res = search_dataset(HttpApiAuth, dataset_id, {"question": "chunk", "doc_ids": [document_id]})
- assert res["code"] == 0, res
- assert "chunks" in res["data"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code",
- [
- ({"question": "chunk", "page": 1, "size": 2}, 0),
- ({"question": "chunk", "similarity_threshold": 0.5}, 0),
- ({"question": "chunk", "vector_similarity_weight": 0.7}, 0),
- ({"question": "chunk", "top_k": 10}, 0),
- ],
- )
- def test_search_params(self, HttpApiAuth, add_chunks, payload, expected_code):
- dataset_id, _, _ = add_chunks
- res = search_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == expected_code, res
-
-
-class TestDatasetGraph:
- def test_graph_requires_auth(self):
- res = knowledge_graph(None, "dataset_id")
- assert res["code"] == 401
-
- def test_graph_basic(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = knowledge_graph(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_tags.py b/test/testcases/test_http_api/test_dataset_management/test_tags.py
deleted file mode 100644
index 9460cbe7c0..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_tags.py
+++ /dev/null
@@ -1,84 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import (
- list_tags,
- aggregate_tags,
- delete_tags,
- rename_tag,
-)
-
-
[email protected]("clear_datasets")
-class TestListTags:
- @pytest.mark.p2
- def test_list_tags_success(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = list_tags(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_list_tags_invalid_id(self, HttpApiAuth):
- res = list_tags(HttpApiAuth, "invalid_id")
- assert res["code"] != 0, res
-
-
[email protected]("clear_datasets")
-class TestAggregateTags:
- @pytest.mark.p2
- def test_aggregate_tags_success(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = aggregate_tags(HttpApiAuth, [dataset_id])
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_aggregate_tags_multiple_datasets(self, HttpApiAuth, add_datasets_func):
- dataset_ids = add_datasets_func
- res = aggregate_tags(HttpApiAuth, dataset_ids)
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_aggregate_tags_empty_ids(self, HttpApiAuth):
- res = aggregate_tags(HttpApiAuth, [])
- assert res["code"] != 0, res
-
-
[email protected]("clear_datasets")
-class TestDeleteTags:
- @pytest.mark.p2
- def test_delete_tags_missing_body(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = delete_tags(HttpApiAuth, dataset_id, [])
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_delete_tags_invalid_id(self, HttpApiAuth):
- res = delete_tags(HttpApiAuth, "invalid_id", ["tag1"])
- assert res["code"] != 0, res
-
-
[email protected]("clear_datasets")
-class TestRenameTag:
- @pytest.mark.p2
- def test_rename_tag_empty_names(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = rename_tag(HttpApiAuth, dataset_id, "", "")
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_rename_tag_invalid_id(self, HttpApiAuth):
- res = rename_tag(HttpApiAuth, "invalid_id", "old_tag", "new_tag")
- assert res["code"] != 0, res
diff --git a/test/testcases/test_http_api/test_dataset_management/test_update_dataset.py b/test/testcases/test_http_api/test_dataset_management/test_update_dataset.py
deleted file mode 100644
index a2438a5d9a..0000000000
--- a/test/testcases/test_http_api/test_dataset_management/test_update_dataset.py
+++ /dev/null
@@ -1,904 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-import uuid
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import list_datasets, update_dataset
-from configs import DATASET_NAME_LIMIT, INVALID_API_TOKEN
-from hypothesis import HealthCheck, example, given, settings
-from libs.auth import RAGFlowHttpApiAuth
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-from utils.hypothesis_utils import valid_names
-from configs import DEFAULT_PARSER_CONFIG
-
-
-class TestAuthorization:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- ids=["empty_auth", "invalid_api_token"],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = update_dataset(invalid_auth, "dataset_id")
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestRquest:
- @pytest.mark.p3
- def test_bad_content_type(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- BAD_CONTENT_TYPE = "text/xml"
- res = update_dataset(HttpApiAuth, dataset_id, {"name": "bad_content_type"}, headers={"Content-Type": BAD_CONTENT_TYPE})
- assert res["code"] == 101, res
- assert res["message"] == f"Unsupported content type: Expected application/json, got {BAD_CONTENT_TYPE}", res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ("a", "Malformed JSON syntax: Missing commas/brackets or invalid encoding"),
- ('"a"', "Invalid request payload: expected object, got str"),
- ],
- ids=["malformed_json_syntax", "invalid_request_payload_type"],
- )
- def test_payload_bad(self, HttpApiAuth, add_dataset_func, payload, expected_message):
- dataset_id = add_dataset_func
- res = update_dataset(HttpApiAuth, dataset_id, data=payload)
- assert res["code"] == 101, res
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- def test_payload_empty(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = update_dataset(HttpApiAuth, dataset_id, {})
- assert res["code"] == 102, res
- assert res["message"] == "no properties were modified", res
-
- @pytest.mark.p3
- def test_payload_unset(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = update_dataset(HttpApiAuth, dataset_id, None)
- assert res["code"] == 101, res
- assert res["message"] == "Malformed JSON syntax: Missing commas/brackets or invalid encoding", res
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_update_dateset_concurrent(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(update_dataset, HttpApiAuth, dataset_id, {"name": f"dataset_{i}"}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
-class TestDatasetUpdate:
- @pytest.mark.p3
- def test_dataset_id_not_uuid(self, HttpApiAuth):
- payload = {"name": "not uuid"}
- res = update_dataset(HttpApiAuth, "not_uuid", payload)
- assert res["code"] == 101, res
- assert "Invalid UUID format" in res["message"], res
-
- @pytest.mark.p3
- def test_dataset_id_not_uuid1(self, HttpApiAuth):
- payload = {"name": "not uuid1"}
- res = update_dataset(HttpApiAuth, uuid.uuid4().hex, payload)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p3
- def test_dataset_id_wrong_uuid(self, HttpApiAuth):
- payload = {"name": "wrong uuid"}
- res = update_dataset(HttpApiAuth, "d94a8dc02c9711f0930f7fbc369eab6d", payload)
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p1
- @given(name=valid_names())
- @example("a" * 128)
- # Network-bound API call; disable Hypothesis deadline to avoid flaky timeouts.
- @settings(max_examples=20, suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
- def test_name(self, HttpApiAuth, add_dataset_func, name):
- dataset_id = add_dataset_func
- payload = {"name": name}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["name"] == name, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "String should have at least 1 character"),
- (" ", "String should have at least 1 character"),
- ("a" * (DATASET_NAME_LIMIT + 1), "String should have at most 128 characters"),
- (0, "Input should be a valid string"),
- (None, "Input should be a valid string"),
- ],
- ids=["empty_name", "space_name", "too_long_name", "invalid_name", "None_name"],
- )
- def test_name_invalid(self, HttpApiAuth, add_dataset_func, name, expected_message):
- dataset_id = add_dataset_func
- payload = {"name": name}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p3
- def test_name_duplicated(self, HttpApiAuth, add_datasets_func):
- dataset_id = add_datasets_func[0]
- name = "dataset_1"
- payload = {"name": name}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 102, res
- assert res["message"] == f"Dataset name '{name}' already exists", res
-
- @pytest.mark.p3
- def test_name_case_insensitive(self, HttpApiAuth, add_datasets_func):
- dataset_id = add_datasets_func[0]
- name = "DATASET_1"
- payload = {"name": name}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 102, res
- assert res["message"] == f"Dataset name '{name}' already exists", res
-
- @pytest.mark.p2
- def test_language_and_connectors_supported(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {
- "name": "language_connectors_supported",
- "description": "",
- "chunk_method": "naive",
- "language": "English",
- "connectors": [],
- }
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
- assert res["data"]["language"] == "English", res
- assert res["data"]["connectors"] == [], res
-
- @pytest.mark.p2
- def test_avatar(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {
- "avatar": f"data:image/png;base64,{encode_avatar(fn)}",
- }
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["avatar"] == f"data:image/png;base64,{encode_avatar(fn)}", res
-
- @pytest.mark.p3
- def test_avatar_exceeds_limit_length(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"avatar": "a" * 65536}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert "String should have at most 65535 characters" in res["message"], res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "avatar_prefix, expected_message",
- [
- ("", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("data:image/png;base64", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("invalid_mine_prefix:image/png;base64,", "invalid MIME prefix format. Must start with 'data:'"),
- ("data:unsupported_mine_type;base64,", "Unsupported MIME type. Allowed: ['image/jpeg', 'image/png']"),
- ],
- ids=["empty_prefix", "missing_comma", "unsupported_mine_type", "invalid_mine_type"],
- )
- def test_avatar_invalid_prefix(self, HttpApiAuth, add_dataset_func, tmp_path, avatar_prefix, expected_message):
- dataset_id = add_dataset_func
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {"avatar": f"{avatar_prefix}{encode_avatar(fn)}"}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p3
- def test_avatar_none(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"avatar": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["avatar"] is None, res
-
- @pytest.mark.p2
- def test_description(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"description": "description"}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"][0]["description"] == "description"
-
- @pytest.mark.p3
- def test_description_exceeds_limit_length(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"description": "a" * 65536}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert "String should have at most 65535 characters" in res["message"], res
-
- @pytest.mark.p3
- def test_description_none(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"description": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"][0]["description"] is None
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "embedding_model",
- [
- "BAAI/bge-small-en-v1.5@Builtin",
- "embedding-3@ZHIPU-AI",
- ],
- ids=["builtin_baai", "tenant_zhipu"],
- )
- def test_embedding_model(self, HttpApiAuth, add_dataset_func, embedding_model):
- dataset_id = add_dataset_func
- payload = {"embedding_model": embedding_model}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["embedding_model"] == embedding_model, res
-
- @pytest.mark.p1
- def test_embedding_model_with_existing_chunks(self, HttpApiAuth, add_chunks):
- """Embedding model can be changed even when dataset has chunks (chunk_count > 0)."""
- dataset_id, _, _ = add_chunks
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"], res
- dataset = res["data"][0]
- assert dataset.get("chunk_count", 0) > 0, res
-
- current_embedding = dataset["embedding_model"]
- candidates = ["BAAI/bge-small-en-v1.5@Builtin", "embedding-3@ZHIPU-AI"]
- new_embedding = candidates[0] if current_embedding != candidates[0] else candidates[1]
-
- payload = {"embedding_model": new_embedding}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("unknown_llm_name", "unknown@ZHIPU-AI"),
- ("unknown_llm_factory", "embedding-3@unknown"),
- ("tenant_no_auth_default_tenant_llm", "text-embedding-v3@Tongyi-Qianwen"),
- ("tenant_no_auth", "text-embedding-3-small@OpenAI"),
- ],
- ids=["unknown_llm_name", "unknown_llm_factory", "tenant_no_auth_default_tenant_llm", "tenant_no_auth"],
- )
- def test_embedding_model_invalid(self, HttpApiAuth, add_dataset_func, name, embedding_model):
- dataset_id = add_dataset_func
- payload = {"name": name, "embedding_model": embedding_model}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 102, res
- if "tenant_no_auth" in name:
- assert res["message"] == f"Unauthorized model: <{embedding_model}>", res
- else:
- assert res["message"] == f"Unsupported model: <{embedding_model}>", res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("empty", ""),
- ("space", " "),
- ("missing_at", "BAAI/bge-small-en-v1.5Builtin"),
- ("missing_model_name", "@Builtin"),
- ("missing_provider", "BAAI/bge-small-en-v1.5@"),
- ("whitespace_only_model_name", " @Builtin"),
- ("whitespace_only_provider", "BAAI/bge-small-en-v1.5@ "),
- ],
- ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],
- )
- def test_embedding_model_format(self, HttpApiAuth, add_dataset_func, name, embedding_model):
- dataset_id = add_dataset_func
- payload = {"name": name, "embedding_model": embedding_model}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- if name in ["empty", "space", "missing_at"]:
- assert "embedding model identifier must follow <model_name>@<provider> format" in res["message"], res
- else:
- assert "both model_name and provider must be non-empty strings" in res["message"], res
-
- @pytest.mark.p2
- def test_embedding_model_none(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"embedding_model": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["embedding_model"] == "BAAI/bge-small-en-v1.5@Builtin", res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "permission",
- [
- "me",
- "team",
- ],
- ids=["me", "team"],
- )
- def test_permission(self, HttpApiAuth, add_dataset_func, permission):
- dataset_id = add_dataset_func
- payload = {"permission": permission}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["permission"] == permission.lower().strip(), res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "permission",
- [
- "",
- "unknown",
- list(),
- "ME",
- "TEAM",
- " ME ",
- ],
- ids=["empty", "unknown", "type_error", "me_upercase", "team_upercase", "whitespace"],
- )
- def test_permission_invalid(self, HttpApiAuth, add_dataset_func, permission):
- dataset_id = add_dataset_func
- payload = {"permission": permission}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101
- assert "Input should be 'me' or 'team'" in res["message"]
-
- @pytest.mark.p3
- def test_permission_none(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"permission": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert "Input should be 'me' or 'team'" in res["message"], res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "chunk_method",
- [
- "naive",
- "book",
- "email",
- "laws",
- "manual",
- "one",
- "paper",
- "picture",
- "presentation",
- "qa",
- "table",
- "tag",
- ],
- ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table", "tag"],
- )
- def test_chunk_method(self, HttpApiAuth, add_dataset_func, chunk_method):
- dataset_id = add_dataset_func
- payload = {"chunk_method": chunk_method}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["chunk_method"] == chunk_method, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "chunk_method",
- [
- "",
- "unknown",
- list(),
- ],
- ids=["empty", "unknown", "type_error"],
- )
- def test_chunk_method_invalid(self, HttpApiAuth, add_dataset_func, chunk_method):
- dataset_id = add_dataset_func
- payload = {"chunk_method": chunk_method}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in res["message"], res
-
- @pytest.mark.p3
- def test_chunk_method_none(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"chunk_method": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in res["message"], res
-
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="#8208")
- @pytest.mark.p2
- @pytest.mark.parametrize("pagerank", [0, 50, 100], ids=["min", "mid", "max"])
- def test_pagerank(self, HttpApiAuth, add_dataset_func, pagerank):
- dataset_id = add_dataset_func
- payload = {"pagerank": pagerank}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"][0]["pagerank"] == pagerank
-
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="#8208")
- @pytest.mark.p2
- def test_pagerank_set_to_0(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"pagerank": 50}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"][0]["pagerank"] == 50, res
-
- payload = {"pagerank": 0}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"][0]["pagerank"] == 0, res
-
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") != "infinity", reason="#8208")
- @pytest.mark.p2
- def test_pagerank_infinity(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"pagerank": 50}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 102, res
- assert res["message"] == "'pagerank' can only be set when doc_engine is elasticsearch", res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "pagerank, expected_message",
- [
- (-1, "Input should be greater than or equal to 0"),
- (101, "Input should be less than or equal to 100"),
- ],
- ids=["min_limit", "max_limit"],
- )
- def test_pagerank_invalid(self, HttpApiAuth, add_dataset_func, pagerank, expected_message):
- dataset_id = add_dataset_func
- payload = {"pagerank": pagerank}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p3
- def test_pagerank_none(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"pagerank": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert "Input should be a valid integer" in res["message"], res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "parser_config",
- [
- {"auto_keywords": 0},
- {"auto_keywords": 16},
- {"auto_keywords": 32},
- {"auto_questions": 0},
- {"auto_questions": 5},
- {"auto_questions": 10},
- {"chunk_token_num": 1},
- {"chunk_token_num": 1024},
- {"chunk_token_num": 2048},
- {"delimiter": "\n"},
- {"delimiter": " "},
- {"html4excel": True},
- {"html4excel": False},
- {"layout_recognize": "DeepDOC"},
- {"layout_recognize": "Plain Text"},
- {"tag_kb_ids": ["1", "2"]},
- {"topn_tags": 1},
- {"topn_tags": 5},
- {"topn_tags": 10},
- {"filename_embd_weight": 0.1},
- {"filename_embd_weight": 0.5},
- {"filename_embd_weight": 1.0},
- {"task_page_size": 1},
- {"task_page_size": None},
- {"pages": [[1, 100]]},
- {"pages": None},
- {"graphrag": {"use_graphrag": True}},
- {"graphrag": {"use_graphrag": False}},
- {"graphrag": {"entity_types": ["age", "sex", "height", "weight"]}},
- {"graphrag": {"method": "general"}},
- {"graphrag": {"method": "light"}},
- {"graphrag": {"community": True}},
- {"graphrag": {"community": False}},
- {"graphrag": {"resolution": True}},
- {"graphrag": {"resolution": False}},
- {"raptor": {"use_raptor": True}},
- {"raptor": {"use_raptor": False}},
- {"raptor": {"prompt": "Who are you?"}},
- {"raptor": {"max_token": 512}},
- {"raptor": {"max_token": 1024}},
- {"raptor": {"max_token": 2048}},
- {"raptor": {"clustering_threshold": 0.0}},
- {"raptor": {"clustering_threshold": 0.5}},
- {"raptor": {"clustering_threshold": 1.0}},
- {"raptor": {"max_cluster": 1}},
- {"raptor": {"max_cluster": 512}},
- {"raptor": {"max_cluster": 1024}},
- {"raptor": {"random_seed": 0}},
- ],
- ids=[
- "auto_keywords_min",
- "auto_keywords_mid",
- "auto_keywords_max",
- "auto_questions_min",
- "auto_questions_mid",
- "auto_questions_max",
- "chunk_token_num_min",
- "chunk_token_num_mid",
- "chunk_token_num_max",
- "delimiter",
- "delimiter_space",
- "html4excel_true",
- "html4excel_false",
- "layout_recognize_DeepDOC",
- "layout_recognize_navie",
- "tag_kb_ids",
- "topn_tags_min",
- "topn_tags_mid",
- "topn_tags_max",
- "filename_embd_weight_min",
- "filename_embd_weight_mid",
- "filename_embd_weight_max",
- "task_page_size_min",
- "task_page_size_None",
- "pages",
- "pages_none",
- "graphrag_true",
- "graphrag_false",
- "graphrag_entity_types",
- "graphrag_method_general",
- "graphrag_method_light",
- "graphrag_community_true",
- "graphrag_community_false",
- "graphrag_resolution_true",
- "graphrag_resolution_false",
- "raptor_true",
- "raptor_false",
- "raptor_prompt",
- "raptor_max_token_min",
- "raptor_max_token_mid",
- "raptor_max_token_max",
- "raptor_clustering_threshold_min",
- "raptor_clustering_threshold_mid",
- "raptor_clustering_threshold_max",
- "raptor_max_cluster_min",
- "raptor_max_cluster_mid",
- "raptor_max_cluster_max",
- "raptor_random_seed_min",
- ],
- )
- def test_parser_config(self, HttpApiAuth, add_dataset_func, parser_config):
- dataset_id = add_dataset_func
- payload = {"parser_config": parser_config}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- for k, v in parser_config.items():
- if isinstance(v, dict):
- for kk, vv in v.items():
- assert res["data"][0]["parser_config"][k][kk] == vv, res
- else:
- assert res["data"][0]["parser_config"][k] == v, res
-
- @pytest.mark.p1
- def test_parser_config_partial_update_preserves_parent_child(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- parent_child = {"use_parent_child": True, "children_delimiter": "\n\n"}
-
- res = update_dataset(HttpApiAuth, dataset_id, {"parser_config": {"parent_child": parent_child}})
- assert res["code"] == 0, res
-
- res = update_dataset(HttpApiAuth, dataset_id, {"parser_config": {"auto_keywords": 5}})
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- parser_config = res["data"][0]["parser_config"]
- assert parser_config["parent_child"] == parent_child, res
- assert parser_config["children_delimiter"] == parent_child["children_delimiter"], res
- assert parser_config["enable_children"] is True, res
- assert parser_config["auto_keywords"] == 5, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "parser_config, expected_message",
- [
- ({"auto_keywords": -1}, "Input should be greater than or equal to 0"),
- ({"auto_keywords": 33}, "Input should be less than or equal to 32"),
- ({"auto_keywords": 3.14}, "Input should be a valid integer"),
- ({"auto_keywords": "string"}, "Input should be a valid integer"),
- ({"auto_questions": -1}, "Input should be greater than or equal to 0"),
- ({"auto_questions": 11}, "Input should be less than or equal to 10"),
- ({"auto_questions": 3.14}, "Input should be a valid integer"),
- ({"auto_questions": "string"}, "Input should be a valid integer"),
- ({"chunk_token_num": 0}, "Input should be greater than or equal to 1"),
- ({"chunk_token_num": 2049}, "Input should be less than or equal to 2048"),
- ({"chunk_token_num": 3.14}, "Input should be a valid integer"),
- ({"chunk_token_num": "string"}, "Input should be a valid integer"),
- ({"delimiter": ""}, "String should have at least 1 character"),
- ({"html4excel": "string"}, "Input should be a valid boolean"),
- ({"tag_kb_ids": "1,2"}, "Input should be a valid list"),
- ({"tag_kb_ids": [1, 2]}, "Input should be a valid string"),
- ({"topn_tags": 0}, "Input should be greater than or equal to 1"),
- ({"topn_tags": 11}, "Input should be less than or equal to 10"),
- ({"topn_tags": 3.14}, "Input should be a valid integer"),
- ({"topn_tags": "string"}, "Input should be a valid integer"),
- ({"filename_embd_weight": -1}, "Input should be greater than or equal to 0"),
- ({"filename_embd_weight": 1.1}, "Input should be less than or equal to 1"),
- ({"filename_embd_weight": "string"}, "Input should be a valid number"),
- ({"task_page_size": 0}, "Input should be greater than or equal to 1"),
- ({"task_page_size": 3.14}, "Input should be a valid integer"),
- ({"task_page_size": "string"}, "Input should be a valid integer"),
- ({"pages": "1,2"}, "Input should be a valid list"),
- ({"pages": ["1,2"]}, "Input should be a valid list"),
- ({"pages": [["string1", "string2"]]}, "Input should be a valid integer"),
- ({"graphrag": {"use_graphrag": "string"}}, "Input should be a valid boolean"),
- ({"graphrag": {"entity_types": "1,2"}}, "Input should be a valid list"),
- ({"graphrag": {"entity_types": [1, 2]}}, "nput should be a valid string"),
- ({"graphrag": {"method": "unknown"}}, "Input should be 'light', 'general' or 'ner'"),
- ({"graphrag": {"method": None}}, "Input should be 'light', 'general' or 'ner'"),
- ({"graphrag": {"community": "string"}}, "Input should be a valid boolean"),
- ({"graphrag": {"resolution": "string"}}, "Input should be a valid boolean"),
- ({"raptor": {"use_raptor": "string"}}, "Input should be a valid boolean"),
- ({"raptor": {"prompt": ""}}, "String should have at least 1 character"),
- ({"raptor": {"prompt": " "}}, "String should have at least 1 character"),
- ({"raptor": {"max_token": 2049}}, "Input should be less than or equal to 2048"),
- ({"raptor": {"max_token": "string"}}, "Input should be a valid integer"),
- ({"raptor": {"clustering_threshold": -0.1}}, "Input should be greater than or equal to 0"),
- ({"raptor": {"clustering_threshold": 1.1}}, "Input should be less than or equal to 1"),
- ({"raptor": {"clustering_threshold": "string"}}, "Input should be a valid number"),
- ({"raptor": {"max_cluster": 0}}, "Input should be greater than or equal to 1"),
- ({"raptor": {"max_cluster": 1025}}, "Input should be less than or equal to 1024"),
- ({"raptor": {"max_cluster": 3.14}}, "Input should be a valid integer"),
- ({"raptor": {"max_cluster": "string"}}, "Input should be a valid integer"),
- ({"raptor": {"random_seed": -1}}, "Input should be greater than or equal to 0"),
- ({"raptor": {"random_seed": 3.14}}, "Input should be a valid integer"),
- ({"raptor": {"random_seed": "string"}}, "Input should be a valid integer"),
- ({"delimiter": "a" * 65536}, "Parser config exceeds size limit (max 65,535 characters)"),
- ],
- ids=[
- "auto_keywords_min_limit",
- "auto_keywords_max_limit",
- "auto_keywords_float_not_allowed",
- "auto_keywords_type_invalid",
- "auto_questions_min_limit",
- "auto_questions_max_limit",
- "auto_questions_float_not_allowed",
- "auto_questions_type_invalid",
- "chunk_token_num_min_limit",
- "chunk_token_num_max_limit",
- "chunk_token_num_float_not_allowed",
- "chunk_token_num_type_invalid",
- "delimiter_empty",
- "html4excel_type_invalid",
- "tag_kb_ids_not_list",
- "tag_kb_ids_int_in_list",
- "topn_tags_min_limit",
- "topn_tags_max_limit",
- "topn_tags_float_not_allowed",
- "topn_tags_type_invalid",
- "filename_embd_weight_min_limit",
- "filename_embd_weight_max_limit",
- "filename_embd_weight_type_invalid",
- "task_page_size_min_limit",
- "task_page_size_float_not_allowed",
- "task_page_size_type_invalid",
- "pages_not_list",
- "pages_not_list_in_list",
- "pages_not_int_list",
- "graphrag_type_invalid",
- "graphrag_entity_types_not_list",
- "graphrag_entity_types_not_str_in_list",
- "graphrag_method_unknown",
- "graphrag_method_none",
- "graphrag_community_type_invalid",
- "graphrag_resolution_type_invalid",
- "raptor_type_invalid",
- "raptor_prompt_empty",
- "raptor_prompt_space",
- "raptor_max_token_max_limit",
- "raptor_max_token_type_invalid",
- "raptor_clustering_threshold_min_limit",
- "raptor_clustering_threshold_max_limit",
- "raptor_clustering_threshold_type_invalid",
- "raptor_max_cluster_min_limit",
- "raptor_max_cluster_max_limit",
- "raptor_max_cluster_float_not_allowed",
- "raptor_max_cluster_type_invalid",
- "raptor_random_seed_min_limit",
- "raptor_random_seed_float_not_allowed",
- "raptor_random_seed_type_invalid",
- "parser_config_type_invalid",
- ],
- )
- def test_parser_config_invalid(self, HttpApiAuth, add_dataset_func, parser_config, expected_message):
- dataset_id = add_dataset_func
- payload = {"parser_config": parser_config}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert expected_message in res["message"], res
-
- @pytest.mark.p2
- def test_parser_config_empty(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"parser_config": {}}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["parser_config"] == DEFAULT_PARSER_CONFIG, res
-
- @pytest.mark.p3
- def test_parser_config_none(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"parser_config": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"][0]["parser_config"] == DEFAULT_PARSER_CONFIG, res
-
- @pytest.mark.p3
- def test_parser_config_empty_with_chunk_method_change(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"chunk_method": "qa", "parser_config": {}}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["parser_config"] == {
- "raptor": {"use_raptor": False},
- "graphrag": {"use_graphrag": False},
- "image_context_size": 0,
- "table_context_size": 0,
- }, res
-
- @pytest.mark.p3
- def test_parser_config_unset_with_chunk_method_change(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"chunk_method": "qa"}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["parser_config"] == {
- "raptor": {"use_raptor": False},
- "graphrag": {"use_graphrag": False},
- "image_context_size": 0,
- "table_context_size": 0,
- }, res
-
- @pytest.mark.p3
- def test_parser_config_none_with_chunk_method_change(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- payload = {"chunk_method": "qa", "parser_config": None}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["code"] == 0, res
- assert res["data"][0]["parser_config"] == {
- "raptor": {"use_raptor": False},
- "graphrag": {"use_graphrag": False},
- "image_context_size": 0,
- "table_context_size": 0,
- }, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload",
- [
- {"id": "id"},
- {"tenant_id": "e57c1966f99211efb41e9e45646e0111"},
- {"created_by": "created_by"},
- {"create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"create_time": 1741671443322},
- {"update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"update_time": 1741671443339},
- {"document_count": 1},
- {"chunk_count": 1},
- {"token_num": 1},
- {"status": "1"},
- {"unknown_field": "unknown_field"},
- ],
- )
- def test_field_unsupported(self, HttpApiAuth, add_dataset_func, payload):
- dataset_id = add_dataset_func
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 101, res
- assert "Extra inputs are not permitted" in res["message"], res
-
- @pytest.mark.p2
- def test_field_unset(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- original_data = res["data"][0]
-
- payload = {"name": "default_unset"}
- res = update_dataset(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 0, res
-
- res = list_datasets(HttpApiAuth)
- assert res["code"] == 0, res
- assert res["data"][0]["avatar"] == original_data["avatar"], res
- assert res["data"][0]["description"] == original_data["description"], res
- assert res["data"][0]["embedding_model"] == original_data["embedding_model"], res
- assert res["data"][0]["permission"] == original_data["permission"], res
- assert res["data"][0]["chunk_method"] == original_data["chunk_method"], res
- assert res["data"][0]["pagerank"] == original_data["pagerank"], res
- assert res["data"][0]["parser_config"] == original_data["parser_config"], res
diff --git a/test/testcases/test_http_api/test_file_app/test_file_routes.py b/test/testcases/test_http_api/test_file_app/test_file_routes.py
deleted file mode 100644
index af8aa96f95..0000000000
--- a/test/testcases/test_http_api/test_file_app/test_file_routes.py
+++ /dev/null
@@ -1,382 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import sys
-from enum import Enum
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-class _DummyUploadFile:
- def __init__(self, filename, blob=b"blob"):
- self.filename = filename
- self._blob = blob
-
- def read(self):
- return self._blob
-
-
-class _DummyFile:
- def __init__(
- self,
- file_id,
- file_type,
- *,
- tenant_id="tenant1",
- parent_id="pf1",
- location="loc1",
- name="doc.txt",
- source_type="user",
- size=1,
- ):
- self.id = file_id
- self.type = file_type
- self.tenant_id = tenant_id
- self.parent_id = parent_id
- self.location = location
- self.name = name
- self.source_type = source_type
- self.size = size
-
- def to_json(self):
- return {"id": self.id, "name": self.name, "type": self.type}
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _load_file_api_service(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- common_pkg = ModuleType("api.common")
- common_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.common", common_pkg)
-
- permission_mod = ModuleType("api.common.check_team_permission")
- permission_mod.check_file_team_permission = lambda *_args, **_kwargs: True
- monkeypatch.setitem(sys.modules, "api.common.check_team_permission", permission_mod)
- common_pkg.check_team_permission = permission_mod
-
- db_pkg = ModuleType("api.db")
- db_pkg.__path__ = []
-
- class _FileType(Enum):
- FOLDER = "folder"
- VIRTUAL = "virtual"
- DOC = "doc"
- VISUAL = "visual"
-
- db_pkg.FileType = _FileType
- monkeypatch.setitem(sys.modules, "api.db", db_pkg)
- api_pkg.db = db_pkg
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- services_pkg.duplicate_name = lambda _query, **kwargs: kwargs.get("name", "")
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- document_service_mod = ModuleType("api.db.services.document_service")
- document_service_mod.DocumentService = SimpleNamespace(
- get_doc_count=lambda _uid: 0,
- get_by_id=lambda doc_id: (True, SimpleNamespace(id=doc_id)),
- get_tenant_id=lambda _doc_id: "tenant1",
- remove_document=lambda *_args, **_kwargs: True,
- update_by_id=lambda *_args, **_kwargs: True,
- )
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", document_service_mod)
- services_pkg.document_service = document_service_mod
-
- file2doc_mod = ModuleType("api.db.services.file2document_service")
- file2doc_mod.File2DocumentService = SimpleNamespace(
- get_by_file_id=lambda _file_id: [],
- delete_by_file_id=lambda _file_id: None,
- )
- monkeypatch.setitem(sys.modules, "api.db.services.file2document_service", file2doc_mod)
- services_pkg.file2document_service = file2doc_mod
-
- file_service_mod = ModuleType("api.db.services.file_service")
- file_service_mod.FileService = SimpleNamespace(
- get_root_folder=lambda _tenant_id: {"id": "root"},
- get_by_id=lambda file_id: (True, _DummyFile(file_id, _FileType.DOC.value)),
- get_id_list_by_id=lambda _pf_id, _names, _idx, ids: ids,
- create_folder=lambda _file, parent_id, _names, _len_id: SimpleNamespace(id=parent_id, name=str(parent_id)),
- query=lambda **_kwargs: [],
- insert=lambda data: SimpleNamespace(to_json=lambda: data, **data),
- is_parent_folder_exist=lambda _pf_id: True,
- get_by_pf_id=lambda *_args, **_kwargs: ([], 0),
- get_parent_folder=lambda _file_id: SimpleNamespace(to_json=lambda: {"id": "root"}),
- get_all_parent_folders=lambda _file_id: [],
- list_all_files_by_parent_id=lambda _parent_id: [],
- delete=lambda _file: True,
- delete_by_id=lambda _file_id: True,
- update_by_id=lambda *_args, **_kwargs: True,
- get_by_ids=lambda file_ids: [_DummyFile(file_id, _FileType.DOC.value) for file_id in file_ids],
- )
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
- services_pkg.file_service = file_service_mod
-
- file_utils_mod = ModuleType("api.utils.file_utils")
- file_utils_mod.filename_type = lambda _filename: _FileType.DOC.value
- monkeypatch.setitem(sys.modules, "api.utils.file_utils", file_utils_mod)
-
- common_root_mod = ModuleType("common")
- common_root_mod.__path__ = [str(repo_root / "common")]
- common_root_mod.settings = SimpleNamespace(
- STORAGE_IMPL=SimpleNamespace(
- obj_exist=lambda *_args, **_kwargs: False,
- put=lambda *_args, **_kwargs: None,
- rm=lambda *_args, **_kwargs: None,
- move=lambda *_args, **_kwargs: None,
- )
- )
- monkeypatch.setitem(sys.modules, "common", common_root_mod)
-
- constants_mod = ModuleType("common.constants")
-
- class _FileSource:
- KNOWLEDGEBASE = "knowledgebase"
-
- constants_mod.FileSource = _FileSource
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- misc_utils_mod = ModuleType("common.misc_utils")
- misc_utils_mod.get_uuid = lambda: "uuid-1"
-
- async def thread_pool_exec(func, *args, **kwargs):
- return func(*args, **kwargs)
-
- misc_utils_mod.thread_pool_exec = thread_pool_exec
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
-
- module_path = repo_root / "api" / "apps" / "services" / "file_api_service.py"
- spec = importlib.util.spec_from_file_location("api.apps.services.file_api_service", module_path)
- module = importlib.util.module_from_spec(spec)
- monkeypatch.setitem(sys.modules, "api.apps.services.file_api_service", module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_upload_file_requires_existing_folder(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- monkeypatch.setattr(module.FileService, "get_by_id", lambda _file_id: (False, None))
-
- ok, message = _run(module.upload_file("tenant1", "pf1", [_DummyUploadFile("a.txt")]))
- assert ok is False
- assert message == "Can't find this folder!"
-
-
-def test_upload_file_respects_user_limit(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- monkeypatch.setattr(module.FileService, "get_by_id", lambda _file_id: (True, SimpleNamespace(id="pf1", name="pf1")))
- monkeypatch.setattr(module.DocumentService, "get_doc_count", lambda _uid: 1)
- monkeypatch.setenv("MAX_FILE_NUM_PER_USER", "1")
-
- ok, message = _run(module.upload_file("tenant1", "pf1", [_DummyUploadFile("a.txt")]))
- assert ok is False
- assert message == "Exceed the maximum file number of a free user!"
- monkeypatch.delenv("MAX_FILE_NUM_PER_USER", raising=False)
-
-
-def test_upload_file_success_uses_new_service_layer(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- storage_puts = []
-
- monkeypatch.setattr(module.FileService, "get_by_id", lambda _file_id: (True, SimpleNamespace(id="pf1", name="pf1")))
- monkeypatch.setattr(module.FileService, "get_id_list_by_id", lambda *_args, **_kwargs: ["pf1"])
- monkeypatch.setattr(
- module.FileService,
- "create_folder",
- lambda _file, parent_id, _names, _len_id, *_args: SimpleNamespace(id=parent_id),
- )
- monkeypatch.setattr(
- module.settings,
- "STORAGE_IMPL",
- SimpleNamespace(
- obj_exist=lambda *_args, **_kwargs: False,
- put=lambda bucket, location, blob: storage_puts.append((bucket, location, blob)),
- rm=lambda *_args, **_kwargs: None,
- move=lambda *_args, **_kwargs: None,
- ),
- )
-
- ok, data = _run(module.upload_file("tenant1", "pf1", [_DummyUploadFile("a.txt", b"hello")]))
- assert ok is True
- assert data[0]["name"] == "a.txt"
- assert storage_puts == [("pf1", "a.txt", b"hello")]
-
-
-def test_create_folder_rejects_duplicate_name(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- monkeypatch.setattr(module.FileService, "query", lambda **_kwargs: [SimpleNamespace(id="existing")])
-
- ok, message = _run(module.create_folder("tenant1", "dup", "pf1", module.FileType.FOLDER.value))
- assert ok is False
- assert message == "Duplicated folder name in the same folder."
-
-
-def test_create_folder_rejects_slash_in_name(monkeypatch):
- module = _load_file_api_service(monkeypatch)
-
- ok, message = _run(module.create_folder("tenant1", "/", "pf1", module.FileType.FOLDER.value))
- assert ok is False
- assert message == 'Folder name cannot contain "/"'
-
-
-def test_delete_files_checks_team_permission(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- monkeypatch.setattr(
- module.FileService,
- "get_by_id",
- lambda _file_id: (True, _DummyFile("file1", module.FileType.DOC.value)),
- )
- monkeypatch.setattr(module, "check_file_team_permission", lambda *_args, **_kwargs: False)
-
- ok, message = _run(module.delete_files("tenant1", ["file1"]))
- assert ok is False
- assert message == {"success_count": 0, "errors": ["No authorization for file file1"]}
-
-
-def test_move_files_rejects_extension_change_in_new_name(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- monkeypatch.setattr(
- module.FileService,
- "get_by_ids",
- lambda _ids: [_DummyFile("file1", module.FileType.DOC.value, name="a.txt")],
- )
-
- ok, message = _run(module.move_files("tenant1", ["file1"], new_name="a.pdf"))
- assert ok is False
- assert message == "The extension of file can't be changed"
-
-
-def test_move_files_rejects_slash_in_new_name(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- monkeypatch.setattr(
- module.FileService,
- "get_by_ids",
- lambda _ids: [_DummyFile("file1", module.FileType.FOLDER.value, name="old")],
- )
-
- ok, message = _run(module.move_files("tenant1", ["file1"], new_name="a/b"))
- assert ok is False
- assert message == 'Name cannot contain "/"'
-
-
-def test_move_files_handles_dest_and_storage_move(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- moved = []
- updated = []
-
- monkeypatch.setattr(
- module.FileService,
- "get_by_id",
- lambda file_id: (False, None) if file_id == "missing" else (True, _DummyFile(file_id, module.FileType.FOLDER.value, name="dest")),
- )
- monkeypatch.setattr(
- module.FileService,
- "get_by_ids",
- lambda _ids: [_DummyFile("file1", module.FileType.DOC.value, parent_id="src", location="old", name="a.txt")],
- )
- monkeypatch.setattr(
- module.settings,
- "STORAGE_IMPL",
- SimpleNamespace(
- obj_exist=lambda *_args, **_kwargs: False,
- put=lambda *_args, **_kwargs: None,
- rm=lambda *_args, **_kwargs: None,
- move=lambda old_bucket, old_loc, new_bucket, new_loc: moved.append((old_bucket, old_loc, new_bucket, new_loc)),
- ),
- )
- monkeypatch.setattr(module.FileService, "update_by_id", lambda file_id, data: updated.append((file_id, data)) or True)
-
- ok, message = _run(module.move_files("tenant1", ["file1"], "missing"))
- assert ok is False
- assert message == "Parent folder not found!"
-
- ok, data = _run(module.move_files("tenant1", ["file1"], "dest"))
- assert ok is True
- assert data is True
- assert moved == [("src", "old", "dest", "a.txt")]
- assert updated == [("file1", {"parent_id": "dest", "location": "a.txt"})]
-
-
-def test_move_files_renames_in_place_without_storage_move(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- db_updates = []
- doc_updates = []
-
- monkeypatch.setattr(
- module.FileService,
- "get_by_ids",
- lambda _ids: [_DummyFile("file1", module.FileType.DOC.value, parent_id="pf1", name="a.txt")],
- )
- monkeypatch.setattr(module.FileService, "update_by_id", lambda file_id, data: db_updates.append((file_id, data)) or True)
- monkeypatch.setattr(
- module.File2DocumentService,
- "get_by_file_id",
- lambda _file_id: [SimpleNamespace(document_id="doc1")],
- )
- monkeypatch.setattr(module.DocumentService, "update_by_id", lambda doc_id, data: doc_updates.append((doc_id, data)) or True)
-
- ok, data = _run(module.move_files("tenant1", ["file1"], new_name="b.txt"))
- assert ok is True
- assert data is True
- assert db_updates == [("file1", {"name": "b.txt"})]
- assert doc_updates == [("doc1", {"name": "b.txt"})]
-
-
-def test_get_file_content_checks_permission(monkeypatch):
- module = _load_file_api_service(monkeypatch)
- monkeypatch.setattr(module, "check_file_team_permission", lambda *_args, **_kwargs: False)
-
- ok, message = module.get_file_content("tenant1", "file1")
- assert ok is False
- assert message == "no authorization"
-
- monkeypatch.setattr(module, "check_file_team_permission", lambda *_args, **_kwargs: True)
- ok, file = module.get_file_content("tenant1", "file1")
- assert ok is True
- assert file.id == "file1"
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/conftest.py b/test/testcases/test_http_api/test_file_management_within_dataset/conftest.py
deleted file mode 100644
index efbbd5d43a..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/conftest.py
+++ /dev/null
@@ -1,52 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import pytest
-from common import bulk_upload_documents, delete_all_documents
-
-
[email protected](scope="function")
-def add_document_func(request, HttpApiAuth, add_dataset, ragflow_tmp_dir):
- def cleanup():
- delete_all_documents(HttpApiAuth, dataset_id)
-
- request.addfinalizer(cleanup)
-
- dataset_id = add_dataset
- return dataset_id, bulk_upload_documents(HttpApiAuth, dataset_id, 1, ragflow_tmp_dir)[0]
-
-
[email protected](scope="class")
-def add_documents(request, HttpApiAuth, add_dataset, ragflow_tmp_dir):
- def cleanup():
- delete_all_documents(HttpApiAuth, dataset_id)
-
- request.addfinalizer(cleanup)
-
- dataset_id = add_dataset
- return dataset_id, bulk_upload_documents(HttpApiAuth, dataset_id, 5, ragflow_tmp_dir)
-
-
[email protected](scope="function")
-def add_documents_func(request, HttpApiAuth, add_dataset_func, ragflow_tmp_dir):
- def cleanup():
- delete_all_documents(HttpApiAuth, dataset_id)
-
- request.addfinalizer(cleanup)
-
- dataset_id = add_dataset_func
- return dataset_id, bulk_upload_documents(HttpApiAuth, dataset_id, 3, ragflow_tmp_dir)
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_delete_documents.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_delete_documents.py
deleted file mode 100644
index 3716f38c59..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_delete_documents.py
+++ /dev/null
@@ -1,200 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import bulk_upload_documents, delete_documents, list_documents
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = delete_documents(invalid_auth, "dataset_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestDocumentsDeletion:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message, remaining",
- [
- ({}, 102, "should either provide doc ids or set delete_all(true), dataset", 3),
- ({"ids": []}, 102, "should either provide doc ids or set delete_all(true), dataset", 3),
- ({"ids": ["invalid_id"]}, 102, "These documents do not belong to dataset", 3),
- (
- {"ids": ["\n!?。;!?\"'"]},
- 102,
- "These documents do not belong to dataset",
- 3,
- ),
- (
- "not json",
- 101,
- "Invalid request payload: expected object, got str",
- 3,
- ),
- (lambda r: {"ids": r[:1]}, 0, "", 2),
- (lambda r: {"ids": r}, 0, "", 0),
- ],
- )
- def test_basic_scenarios(
- self,
- HttpApiAuth,
- add_documents_func,
- payload,
- expected_code,
- expected_message,
- remaining,
- ):
- dataset_id, document_ids = add_documents_func
- if callable(payload):
- payload = payload(document_ids)
- res = delete_documents(HttpApiAuth, dataset_id, payload)
- assert res["code"] == expected_code
- if res["code"] != 0:
- assert expected_message in res["message"]
-
- res = list_documents(HttpApiAuth, dataset_id)
- assert len(res["data"]["docs"]) == remaining
- assert res["data"]["total"] == remaining
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- ("", 100, "<MethodNotAllowed '405: Method Not Allowed'>"),
- (
- "invalid_dataset_id",
- 102,
- "You don't own the dataset invalid_dataset_id. ",
- ),
- ],
- )
- def test_invalid_dataset_id(self, HttpApiAuth, add_documents_func, dataset_id, expected_code, expected_message):
- _, document_ids = add_documents_func
- res = delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids[:1]})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload",
- [
- lambda r: {"ids": ["invalid_id"] + r},
- lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:3]},
- lambda r: {"ids": r + ["invalid_id"]},
- ],
- )
- def test_delete_partial_invalid_id(self, HttpApiAuth, add_documents_func, payload):
- dataset_id, document_ids = add_documents_func
- if callable(payload):
- payload = payload(document_ids)
- res = delete_documents(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 102
- assert "These documents do not belong to dataset" in res["message"]
-
- res = list_documents(HttpApiAuth, dataset_id)
- assert len(res["data"]["docs"]) == 3
- assert res["data"]["total"] == 3
-
- @pytest.mark.p2
- def test_repeated_deletion(self, HttpApiAuth, add_documents_func):
- dataset_id, document_ids = add_documents_func
- res = delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
- assert res["code"] == 0
-
- res = delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
- assert res["code"] == 102
- assert "or Document not found" in res["message"]
-
- @pytest.mark.p2
- def test_duplicate_deletion(self, HttpApiAuth, add_documents_func):
- dataset_id, document_ids = add_documents_func
- res = delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids + document_ids})
- assert res["code"] == 101, res
- assert "Field: <ids> - Message: <Duplicate ids:" in res["message"]
-
- res = list_documents(HttpApiAuth, dataset_id)
- assert len(res["data"]["docs"]) == 3
- assert res["data"]["total"] == 3
-
- @pytest.mark.p2
- def test_cross_dataset_deletion_is_blocked(self, HttpApiAuth, add_dataset, add_documents_func, tmp_path):
- dataset_id, _document_ids = add_documents_func
- other_dataset_id = add_dataset
- other_document_id = bulk_upload_documents(HttpApiAuth, other_dataset_id, 1, tmp_path)[0]
-
- res = delete_documents(HttpApiAuth, dataset_id, {"ids": [other_document_id]})
- assert res["code"] == 102
- assert f"These documents do not belong to dataset {dataset_id}" in res["message"]
-
- res = list_documents(HttpApiAuth, dataset_id)
- assert len(res["data"]["docs"]) == 3
- assert res["data"]["total"] == 3
-
- res = list_documents(HttpApiAuth, other_dataset_id)
- assert len(res["data"]["docs"]) == 1
- assert res["data"]["total"] == 1
-
-
-def test_concurrent_deletion(HttpApiAuth, add_dataset, tmp_path):
- count = 100
- dataset_id = add_dataset
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, count, tmp_path)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- delete_documents,
- HttpApiAuth,
- dataset_id,
- {"ids": document_ids[i : i + 1]},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
-def test_delete_1k(HttpApiAuth, add_dataset, tmp_path):
- documents_num = 1_000
- dataset_id = add_dataset
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, documents_num, tmp_path)
- res = list_documents(HttpApiAuth, dataset_id)
- assert res["data"]["total"] == documents_num
-
- res = delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
- assert res["code"] == 0
-
- res = list_documents(HttpApiAuth, dataset_id)
- assert res["data"]["total"] == 0
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py
deleted file mode 100644
index 16fde92230..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py
+++ /dev/null
@@ -1,1417 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-import contextlib
-import functools
-import inspect
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import numpy as np
-import pytest
-
-from api.db import FileType
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _DummyFiles(dict):
- def getlist(self, key):
- return self.get(key, [])
-
-
-class _DummyArgs(dict):
- def getlist(self, key):
- v = self.get(key, [])
- if v is None:
- return []
- if isinstance(v, list):
- return v
- return [v]
-
-
-class _DummyDoc:
- def __init__(
- self,
- *,
- doc_id="doc-1",
- kb_id="kb-1",
- name="doc.txt",
- chunk_num=1,
- token_num=2,
- progress=0,
- process_duration=0,
- parser_id="naive",
- doc_type=FileType.OTHER,
- status=True,
- run=0,
- progress_msg="",
- ):
- self.id = doc_id
- self.kb_id = kb_id
- self.name = name
- self.chunk_num = chunk_num
- self.token_num = token_num
- self.progress = progress
- self.process_duration = process_duration
- self.parser_id = parser_id
- self.type = doc_type
- self.status = status
- self.run = run
- self.progress_msg = progress_msg
-
- def to_dict(self):
- return {
- "id": self.id,
- "kb_id": self.kb_id,
- "name": self.name,
- "chunk_num": self.chunk_num,
- "token_num": self.token_num,
- "progress": self.progress,
- "process_duration": self.process_duration,
- "parser_id": self.parser_id,
- "run": self.run,
- "status": self.status,
- }
-
-
-class _ToggleBoolDocList:
- def __init__(self, value):
- self._calls = 0
- self._value = value
-
- def __getitem__(self, item):
- return self._value
-
- def __bool__(self):
- self._calls += 1
- return self._calls == 1
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _load_doc_module(monkeypatch, module_basename="chunk_api"):
- repo_root = Path(__file__).resolve().parents[4]
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- apps_mod = ModuleType("api.apps")
-
- def _login_required(func=None, **_kwargs):
- # Real login_required is used both bare (chunk_api) and as a factory with
- # auth_types=[...] (document_api); the mock must pass through both forms.
- if func is None:
- return lambda inner: inner
- return func
-
- apps_mod.login_required = _login_required
- apps_mod.current_user = SimpleNamespace(id="tenant-1")
- apps_mod.AUTH_JWT = None
- apps_mod.AUTH_API = None
- apps_mod.AUTH_BETA = None
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- common_settings_mod = ModuleType("common.settings")
- common_settings_mod.retriever = SimpleNamespace()
- common_settings_mod.kg_retriever = SimpleNamespace()
- common_settings_mod.STORAGE_IMPL = SimpleNamespace(get=lambda *_args, **_kwargs: b"", rm=lambda *_args, **_kwargs: None)
- monkeypatch.setitem(sys.modules, "common.settings", common_settings_mod)
-
- common_misc_utils_mod = ModuleType("common.misc_utils")
-
- async def _thread_pool_exec(func, *args, **kwargs):
- return func(*args, **kwargs)
-
- common_misc_utils_mod.thread_pool_exec = _thread_pool_exec
- common_misc_utils_mod.get_uuid = lambda: "uuid-1"
- monkeypatch.setitem(sys.modules, "common.misc_utils", common_misc_utils_mod)
-
- common_string_utils_mod = ModuleType("common.string_utils")
- common_string_utils_mod.is_content_empty = lambda content: content is None or not str(content).strip()
- common_string_utils_mod.remove_redundant_spaces = lambda text: " ".join(str(text).split())
- monkeypatch.setitem(sys.modules, "common.string_utils", common_string_utils_mod)
-
- tag_feature_utils_mod = ModuleType("common.tag_feature_utils")
- tag_feature_utils_mod.validate_tag_features = lambda value: value
- monkeypatch.setitem(sys.modules, "common.tag_feature_utils", tag_feature_utils_mod)
-
- class _FakeExpr:
- def __or__(self, other):
- return self
-
- def __and__(self, other):
- return self
-
- class _FakeField:
- def __eq__(self, other):
- return _FakeExpr()
-
- def __ne__(self, other):
- return _FakeExpr()
-
- def is_null(self, value=True):
- return _FakeExpr()
-
- class _StubFreshDoc:
- id = "doc-1"
- kb_id = "kb-1"
- token_num = 2
- chunk_num = 1
- process_duration = 0.0
-
- class _StubDocQuery:
- # Stands in for Document.select().where(...).for_update().first().
- def where(self, *_args, **_kwargs):
- return self
-
- def for_update(self):
- return self
-
- def first(self):
- return _StubDocumentModel.fresh_doc
-
- class _StubDocumentModel:
- id = _FakeField()
- run = _FakeField()
- # The row re-read under lock by _release_doc_counters; tests that assert on
- # the decrement override this to mirror the document's current counters.
- fresh_doc = _StubFreshDoc()
-
- @classmethod
- def select(cls, *_args, **_kwargs):
- return _StubDocQuery()
-
- class _StubTaskModel:
- doc_id = _FakeField()
-
- class _AnyFieldMeta(type):
- def __getattr__(cls, _name):
- return _FakeField()
-
- class _StubModel(metaclass=_AnyFieldMeta):
- pass
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.APIToken = SimpleNamespace(query=lambda **_kwargs: [])
- db_models_mod.Document = _StubDocumentModel
- db_models_mod.Task = _StubTaskModel
- db_models_mod.DB = SimpleNamespace(atomic=lambda: contextlib.nullcontext(), connection_context=lambda: lambda fn: fn)
- # Transitively-loaded real services import assorted model classes (File,
- # Knowledgebase, UserTenant, ...); hand them a permissive stub on demand.
- db_models_mod.__getattr__ = lambda _name: _StubModel
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = [str(repo_root / "api" / "db" / "services")]
- services_pkg.duplicate_name = lambda _query, name="", **_kwargs: name
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- doc_metadata_service_mod = ModuleType("api.db.services.doc_metadata_service")
- doc_metadata_service_mod.DocMetadataService = SimpleNamespace(
- get_flatted_meta_by_kbs=lambda *_args, **_kwargs: [],
- get_metadata_for_documents=lambda *_args, **_kwargs: {},
- )
- monkeypatch.setitem(sys.modules, "api.db.services.doc_metadata_service", doc_metadata_service_mod)
-
- document_service_mod = ModuleType("api.db.services.document_service")
- document_service_mod.DocumentService = SimpleNamespace(
- query=lambda **_kwargs: [],
- accessible=lambda *_args, **_kwargs: True,
- filter_update=lambda *_args, **_kwargs: 0,
- get_by_id=lambda *_args, **_kwargs: (False, None),
- update_by_id=lambda *_args, **_kwargs: True,
- decrement_chunk_num=lambda *_args, **_kwargs: None,
- increment_chunk_num=lambda *_args, **_kwargs: True,
- get_embd_id=lambda *_args, **_kwargs: "",
- get_tenant_embd_id=lambda *_args, **_kwargs: None,
- )
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", document_service_mod)
-
- file2document_service_mod = ModuleType("api.db.services.file2document_service")
- file2document_service_mod.File2DocumentService = SimpleNamespace(
- get_storage_address=lambda **_kwargs: ("", ""),
- )
- monkeypatch.setitem(sys.modules, "api.db.services.file2document_service", file2document_service_mod)
-
- knowledgebase_service_mod = ModuleType("api.db.services.knowledgebase_service")
- knowledgebase_service_mod.KnowledgebaseService = SimpleNamespace(
- accessible=lambda **_kwargs: False,
- get_by_id=lambda *_args, **_kwargs: (False, None),
- get_by_ids=lambda *_args, **_kwargs: [],
- list_documents_by_ids=lambda *_args, **_kwargs: [],
- query=lambda **_kwargs: [],
- )
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", knowledgebase_service_mod)
-
- task_service_mod = ModuleType("api.db.services.task_service")
- task_service_mod.TaskService = SimpleNamespace(filter_delete=lambda *_args, **_kwargs: None, query=lambda **_kwargs: [])
- task_service_mod.cancel_all_task_of = lambda *_args, **_kwargs: None
- task_service_mod.queue_tasks = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.db.services.task_service", task_service_mod)
-
- file_service_mod = ModuleType("api.db.services.file_service")
- file_service_mod.FileService = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
-
- canvas_service_mod = ModuleType("api.db.services.canvas_service")
- canvas_service_mod.UserCanvasService = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "api.db.services.canvas_service", canvas_service_mod)
-
- # document_api imports check_kb_team_permission; stub it so the real module
- # and its user_service/common_service (peewee) import chain stay out.
- check_team_permission_mod = ModuleType("api.common.check_team_permission")
- check_team_permission_mod.check_kb_team_permission = lambda *_args, **_kwargs: True
- check_team_permission_mod.check_file_team_permission = lambda *_args, **_kwargs: True
- monkeypatch.setitem(sys.modules, "api.common.check_team_permission", check_team_permission_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- def _add_tenant_id_to_kwargs(func):
- # Mirror the real decorator's functools.wraps so tests can reach the raw
- # handler through ``route.__wrapped__`` and pass ``tenant_id`` explicitly.
- @functools.wraps(func)
- def wrapper(*args, **kwargs):
- return func(*args, **kwargs)
-
- return wrapper
-
- api_utils_mod.add_tenant_id_to_kwargs = _add_tenant_id_to_kwargs
- api_utils_mod.check_duplicate_ids = lambda ids, _kind="item": (ids, [])
- api_utils_mod.construct_json_result = lambda code=0, message="success", data=None: {"code": code, "message": message, "data": data}
- api_utils_mod.get_error_data_result = lambda message="Sorry! Data missing!", code=102: {"code": code, "message": message}
- api_utils_mod.get_request_json = lambda: _AwaitableValue({})
- api_utils_mod.get_result = lambda code=0, message="", data=None, total=None: {
- key: value for key, value in {"code": code, "message": message, "data": data, "total": total}.items() if value is not None
- }
- api_utils_mod.server_error_response = lambda e: {"code": 500, "message": str(e)}
- api_utils_mod.get_data_error_result = lambda message="Sorry! Data missing!", code=102: {"code": code, "message": message}
- api_utils_mod.get_error_argument_result = lambda message="": {"code": 101, "message": message}
- api_utils_mod.get_json_result = lambda code=0, message="success", data=None, **_kwargs: {"code": code, "message": message, "data": data}
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- image_utils_mod = ModuleType("api.utils.image_utils")
- image_utils_mod.store_chunk_image = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.utils.image_utils", image_utils_mod)
-
- reference_metadata_utils_mod = ModuleType("api.utils.reference_metadata_utils")
- reference_metadata_utils_mod.resolve_reference_metadata_preferences = lambda req, *_args, **_kwargs: (
- bool((req.get("reference_metadata") or {}).get("include")),
- set((req.get("reference_metadata") or {}).get("fields") or []),
- )
-
- def _enrich_chunks_with_document_metadata(chunks, metadata_fields=None):
- for chunk in chunks:
- doc_id = chunk.get("doc_id") or chunk.get("document_id")
- if not doc_id:
- continue
- metadata = doc_metadata_service_mod.DocMetadataService.get_metadata_for_documents([doc_id], chunk.get("kb_id"))
- document_metadata = dict(metadata.get(doc_id, {}))
- if metadata_fields:
- document_metadata = {key: value for key, value in document_metadata.items() if key in metadata_fields}
- if document_metadata:
- chunk["document_metadata"] = document_metadata
-
- reference_metadata_utils_mod.enrich_chunks_with_document_metadata = _enrich_chunks_with_document_metadata
- monkeypatch.setitem(sys.modules, "api.utils.reference_metadata_utils", reference_metadata_utils_mod)
-
- common_metadata_utils_mod = ModuleType("common.metadata_utils")
- common_metadata_utils_mod.convert_conditions = lambda conditions: conditions
- common_metadata_utils_mod.meta_filter = lambda *_args, **_kwargs: []
- common_metadata_utils_mod.turn2jsonschema = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "common.metadata_utils", common_metadata_utils_mod)
-
- rag_app_tag_mod = ModuleType("rag.app.tag")
- rag_app_tag_mod.label_question = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "rag.app.tag", rag_app_tag_mod)
-
- rag_prompts_generator_mod = ModuleType("rag.prompts.generator")
- rag_prompts_generator_mod.cross_languages = lambda *_args, **_kwargs: ""
- rag_prompts_generator_mod.keyword_extraction = lambda *_args, **_kwargs: ""
- monkeypatch.setitem(sys.modules, "rag.prompts.generator", rag_prompts_generator_mod)
-
- rag_nlp_mod = ModuleType("rag.nlp")
- rag_nlp_mod.search = SimpleNamespace(index_name=lambda tenant_id: f"idx_{tenant_id}")
- monkeypatch.setitem(sys.modules, "rag.nlp", rag_nlp_mod)
- monkeypatch.setitem(sys.modules, "rag.nlp.search", rag_nlp_mod.search)
-
- deepdoc_pkg = ModuleType("deepdoc")
- deepdoc_parser_pkg = ModuleType("deepdoc.parser")
- deepdoc_parser_pkg.__path__ = []
-
- class _StubPdfParser:
- pass
-
- class _StubExcelParser:
- pass
-
- class _StubDocxParser:
- pass
-
- deepdoc_parser_pkg.PdfParser = _StubPdfParser
- deepdoc_parser_pkg.ExcelParser = _StubExcelParser
- deepdoc_parser_pkg.DocxParser = _StubDocxParser
- deepdoc_pkg.parser = deepdoc_parser_pkg
- monkeypatch.setitem(sys.modules, "deepdoc", deepdoc_pkg)
- monkeypatch.setitem(sys.modules, "deepdoc.parser", deepdoc_parser_pkg)
-
- deepdoc_excel_module = ModuleType("deepdoc.parser.excel_parser")
- deepdoc_excel_module.RAGFlowExcelParser = _StubExcelParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.excel_parser", deepdoc_excel_module)
- deepdoc_parser_utils = ModuleType("deepdoc.parser.utils")
- deepdoc_parser_utils.get_text = lambda *_args, **_kwargs: ""
- monkeypatch.setitem(sys.modules, "deepdoc.parser.utils", deepdoc_parser_utils)
- monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))
-
- # Mock tenant_llm_service for TenantLLMService and TenantService
- tenant_llm_service_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _MockModelConfig:
- def __init__(self, tenant_id, model_name):
- self.tenant_id = tenant_id
- self.llm_name = model_name
- self.llm_factory = "Builtin"
- self.api_key = "fake-api-key"
- self.api_base = "https://api.example.com"
- self.model_type = "embedding"
- self.max_tokens = 8192
- self.used_tokens = 0
- self.status = 1
- self.id = 1
-
- def to_dict(self):
- return {
- "tenant_id": self.tenant_id,
- "llm_name": self.llm_name,
- "llm_factory": self.llm_factory,
- "api_key": self.api_key,
- "api_base": self.api_base,
- "model_type": self.model_type,
- "max_tokens": self.max_tokens,
- "used_tokens": self.used_tokens,
- "status": self.status,
- "id": self.id,
- }
-
- class _StubTenantService:
- @staticmethod
- def get_by_id(tenant_id):
- return True, SimpleNamespace(id=tenant_id, llm_id="chat-model", embd_id="embd-model", asr_id="asr-model", img2txt_id="img2txt-model", rerank_id="rerank-model", tts_id="tts-model")
-
- class _StubTenantLLMService:
- @staticmethod
- def get_api_key(tenant_id, model_name):
- return _MockModelConfig(tenant_id, model_name)
-
- @staticmethod
- def split_model_name_and_factory(model_name):
- if "@" in model_name:
- parts = model_name.split("@")
- return parts[0], parts[1]
- return model_name, None
-
- @staticmethod
- def get_by_id(tenant_model_id):
- return True, _MockModelConfig("tenant-1", "model-1")
-
- @staticmethod
- def model_instance(model_config):
- class _EmbedModel:
- def encode(self, texts):
- import numpy as np
-
- return [np.array([0.2, 0.8]), np.array([0.3, 0.7])], 1
-
- return _EmbedModel()
-
- tenant_llm_service_mod.TenantService = _StubTenantService
- tenant_llm_service_mod.TenantLLMService = _StubTenantLLMService
-
- class _StubLLMFactoriesService:
- pass
-
- tenant_llm_service_mod.LLMFactoriesService = _StubLLMFactoriesService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_service_mod)
-
- # Mock LLMService
- llm_service_mod = ModuleType("api.db.services.llm_service")
-
- class _StubLLM:
- def __init__(self, llm_name):
- self.llm_name = llm_name
- self.is_tools = False
-
- class _StubLLMBundle:
- def __init__(self, tenant_id: str, model_config: dict, lang="Chinese", **kwargs):
- self.tenant_id = tenant_id
- self.model_config = model_config
- self.lang = lang
-
- def encode(self, texts: list):
- import numpy as np
-
- # Return mock embeddings and token usage
- return [np.array([0.2, 0.8]), np.array([0.3, 0.7])], len(texts) * 10
-
- llm_service_mod.LLMService = SimpleNamespace(query=lambda llm_name: [_StubLLM(llm_name)] if llm_name else [])
- llm_service_mod.LLMBundle = _StubLLMBundle
- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)
-
- # Mock tenant_model_service to ensure it uses mocked services
- tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
-
- class _MockModelConfig2:
- def __init__(self, tenant_id, model_name):
- self.tenant_id = tenant_id
- self.llm_name = model_name
- self.llm_factory = "Builtin"
- self.api_key = "fake-api-key"
- self.api_base = "https://api.example.com"
- self.model_type = "embedding"
- self.max_tokens = 8192
- self.used_tokens = 0
- self.status = 1
- self.id = 1
-
- def to_dict(self):
- return {
- "tenant_id": self.tenant_id,
- "llm_name": self.llm_name,
- "llm_factory": self.llm_factory,
- "api_key": self.api_key,
- "api_base": self.api_base,
- "model_type": self.model_type,
- "max_tokens": self.max_tokens,
- "used_tokens": self.used_tokens,
- "status": self.status,
- "id": self.id,
- }
-
- def _get_model_config_by_id(
- tenant_model_id: str,
- allowed_tenant_ids=None,
- requester_tenant_id=None,
- ) -> dict:
- mock_tenant_id = "tenant-1"
- if allowed_tenant_ids is not None:
- if isinstance(allowed_tenant_ids, str):
- allowed_tenant_ids = {allowed_tenant_ids}
- else:
- allowed_tenant_ids = {str(tenant_id) for tenant_id in allowed_tenant_ids if tenant_id}
- if mock_tenant_id not in allowed_tenant_ids and str(requester_tenant_id) != mock_tenant_id:
- raise LookupError(f"Tenant Model with id {tenant_model_id} not authorized")
- return _MockModelConfig2(mock_tenant_id, "model-1").to_dict()
-
- def _get_model_config_from_provider_instance(tenant_id: str, model_type: str, model_name: str):
- if not model_name:
- raise Exception("Model Name is required")
- return _MockModelConfig2(tenant_id, model_name).to_dict()
-
- def _get_tenant_default_model_by_type(tenant_id: str, model_type):
- # Return mock tenant with default model configurations
- return _MockModelConfig2(tenant_id, "chat-model").to_dict()
-
- def _split_model_name(model_name):
- parts = model_name.rsplit("@", 2)
- if len(parts) == 3:
- return parts[0], parts[1], parts[2]
- if len(parts) == 2:
- return parts[0], "default", parts[1]
- return parts[0], "", ""
-
- tenant_model_service_mod.get_model_config_by_id = _get_model_config_by_id
- tenant_model_service_mod.get_model_config_from_provider_instance = _get_model_config_from_provider_instance
- tenant_model_service_mod.resolve_model_config = _get_model_config_from_provider_instance
- tenant_model_service_mod.get_tenant_default_model_by_type = _get_tenant_default_model_by_type
- tenant_model_service_mod.split_model_name = _split_model_name
- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
-
- if module_basename == "document_api":
- stub_apps_services = ModuleType("api.apps.services")
- stub_apps_services.__path__ = [str(repo_root / "api" / "apps" / "services")]
- monkeypatch.setitem(sys.modules, "api.apps.services", stub_apps_services)
-
- document_api_service_mod = ModuleType("api.apps.services.document_api_service")
- document_api_service_mod.validate_document_update_fields = lambda *_args, **_kwargs: (None, None)
- document_api_service_mod.map_doc_keys = lambda doc: doc.to_dict() if hasattr(doc, "to_dict") else doc
- document_api_service_mod.map_doc_keys_with_run_status = lambda doc, run_status="0": {
- **(doc if isinstance(doc, dict) else doc.to_dict()),
- "run": run_status,
- }
- document_api_service_mod.update_document_name_only = lambda *_args, **_kwargs: None
- document_api_service_mod.update_chunk_method = lambda *_args, **_kwargs: None
- document_api_service_mod.update_document_status_only = lambda *_args, **_kwargs: None
- document_api_service_mod.reset_document_for_reparse = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.apps.services.document_api_service", document_api_service_mod)
- else:
- # chunk_api imports structure_graph_common from api.apps.services; stub
- # it in sys.modules so the real module and its transitive imports are
- # not loaded.
- stub_apps_services = ModuleType("api.apps.services")
- monkeypatch.setitem(sys.modules, "api.apps.services", stub_apps_services)
-
- sgc_mod = ModuleType("api.apps.services.structure_graph_common")
-
- async def _sgc_keyword_subgraph(*_args, **_kwargs):
- return {}, [], []
-
- async def _sgc_build_bucket(*_args, **_kwargs):
- return [], []
-
- sgc_mod.keyword_subgraph = _sgc_keyword_subgraph
- sgc_mod.build_bucket = _sgc_build_bucket
- monkeypatch.setitem(sys.modules, "api.apps.services.structure_graph_common", sgc_mod)
-
- # document_counter_service is a real module (release_reparse_counters); evict
- # any cached copy so it re-imports against this test's freshly-stubbed
- # db_models / document_service rather than a prior test's stubs.
- monkeypatch.delitem(sys.modules, "api.db.services.document_counter_service", raising=False)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / f"{module_basename}.py"
- spec = importlib.util.spec_from_file_location("test_doc_sdk_routes_unit", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- return module
-
-
-def _load_restful_chunk_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
- helper_path = repo_root / "test" / "testcases" / "test_web_api" / "test_chunk_app" / "test_chunk_routes_unit.py"
- spec = importlib.util.spec_from_file_location("test_restful_chunk_route_helpers", helper_path)
- helper = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(helper)
- return helper._load_chunk_api_module(monkeypatch)
-
-
-def _route_core(func):
- return inspect.unwrap(func)
-
-
-def _patch_send_file(monkeypatch, module):
- async def _fake_send_file(file_obj, **kwargs):
- return {
- "file": file_obj,
- "filename": kwargs.get("attachment_filename"),
- "mimetype": kwargs.get("mimetype"),
- }
-
- monkeypatch.setattr(module, "send_file", _fake_send_file)
-
-
-def _patch_storage(monkeypatch, module, *, file_stream=b"abc"):
- storage = SimpleNamespace(get=lambda *_args, **_kwargs: file_stream, rm=lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", storage)
-
-
-def _patch_docstore(monkeypatch, module, **kwargs):
- defaults = {
- "delete": lambda *_args, **_kwargs: 0,
- "update": lambda *_args, **_kwargs: None,
- "get": lambda *_args, **_kwargs: {},
- "insert": lambda *_args, **_kwargs: None,
- "index_exist": lambda *_args, **_kwargs: False,
- }
- defaults.update(kwargs)
- monkeypatch.setattr(module.settings, "docStoreConn", SimpleNamespace(**defaults), raising=False)
-
-
-class TestDocRoutesUnit:
- def test_chunk_positions_validation_error(self, monkeypatch):
- module = _load_restful_chunk_module(monkeypatch)
- with pytest.raises(ValueError) as exc_info:
- module.Chunk(positions=[[1, 2, 3, 4]])
- assert "length of 5" in str(exc_info.value)
-
- def test_download_and_download_doc_errors(self, monkeypatch):
- module = _load_doc_module(monkeypatch, module_basename="document_api")
- _patch_send_file(monkeypatch, module)
- _patch_storage(monkeypatch, module, file_stream=b"")
-
- # download(dataset_id, document_id)
- res = _run(module.download("ds-1", ""))
- assert res["message"] == "Specify document_id please."
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(module.download("ds-1", "doc-1"))
- assert res["message"] == "Document not found!"
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "accessible", lambda *_args, **_kwargs: False)
- res = _run(module.download("ds-1", "doc-1"))
- assert res["message"] == "Document not found!"
-
- monkeypatch.setattr(module.DocumentService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- res = _run(module.download("ds-1", "doc-1"))
- assert "not own the document" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc()])
- monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("b", "n"))
- res = _run(module.download("ds-1", "doc-1"))
- assert res["message"] == "This file is empty."
-
- # download_document(document_id)
- res = _run(module.download_document(""))
- assert res["message"] == "Specify document_id please."
-
- monkeypatch.setattr(module.DocumentService, "accessible", lambda *_args, **_kwargs: False)
- res = _run(module.download_document("doc-1"))
- assert res["message"] == "Document not found!"
-
- monkeypatch.setattr(module.DocumentService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- res = _run(module.download_document("doc-1"))
- assert "not own the document" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc()])
- res = _run(module.download_document("doc-1"))
- assert res["message"] == "This file is empty."
-
- _patch_storage(monkeypatch, module, file_stream=b"abc")
- res = _run(module.download_document("doc-1"))
- assert res["filename"] == "doc.txt"
-
- def test_download_mimetype_from_filename(self, monkeypatch):
- module = _load_doc_module(monkeypatch, module_basename="document_api")
- _patch_send_file(monkeypatch, module)
- _patch_storage(monkeypatch, module, file_stream=b"pdf-bytes")
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(name="report.pdf", doc_type=FileType.PDF)])
- monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("b", "n"))
- res = _run(module.download("ds-1", "doc-1"))
- assert res["filename"] == "report.pdf"
- assert res["mimetype"] == "application/pdf"
-
- def test_parse_branches(self, monkeypatch):
- module = _load_doc_module(monkeypatch)
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(module.parse.__wrapped__("tenant-1", "ds-1"))
- assert "don't own the dataset" in res["message"]
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, SimpleNamespace(tenant_id="tenant-1", pipeline_id=None)))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"document_ids": ["doc-1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- toggle_doc = _ToggleBoolDocList(_DummyDoc(progress=0))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: toggle_doc)
- res = _run(module.parse.__wrapped__("tenant-1", "ds-1"))
- assert "don't own the document" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.RUNNING.value)])
- monkeypatch.setattr(
- module.DocumentService,
- "filter_update",
- lambda *_args, **_kwargs: 0,
- )
- res = _run(module.parse.__wrapped__("tenant-1", "ds-1"))
- assert "currently being processed" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(progress=0)])
- monkeypatch.setattr(module.DocumentService, "filter_update", lambda *_args, **_kwargs: 1)
- monkeypatch.setattr(module.DocumentService, "get_by_id", lambda _id: (True, _DummyDoc()))
- monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("b", "n"))
- _patch_docstore(monkeypatch, module, delete=lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module.TaskService, "filter_delete", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "queue_tasks", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, ["Duplicate document ids: doc-1"]))
- res = _run(module.parse.__wrapped__("tenant-1", "ds-1"))
- assert res["code"] == 0
- assert res["data"]["success_count"] == 1
- assert "Duplicate document ids" in res["data"]["errors"][0]
-
- monkeypatch.setattr(module, "check_duplicate_ids", lambda _ids, _kind: ([], ["Duplicate document ids: doc-1"]))
- res = _run(module.parse.__wrapped__("tenant-1", "ds-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "Duplicate document ids" in res["message"]
-
- def test_parse_and_stop_decrement_kb_counters(self, monkeypatch):
- # Both routes delete the document's chunks, so the knowledgebase aggregate
- # must drop by the document's current counters. Zeroing only the document
- # row leaves that amount stranded in the KB total on every re-parse.
- module = _load_doc_module(monkeypatch)
- # _release_doc_counters re-reads the row under lock; mirror the document's
- # current counters so the decrement is driven by that fresh read.
- monkeypatch.setattr(module.Document, "fresh_doc", SimpleNamespace(id="doc-1", kb_id="kb-1", token_num=70, chunk_num=7, process_duration=1.5))
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, SimpleNamespace(tenant_id="tenant-1", pipeline_id=None)))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"document_ids": ["doc-1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- monkeypatch.setattr(module.DocumentService, "get_by_id", lambda _id: (True, _DummyDoc()))
- monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("b", "n"))
- monkeypatch.setattr(module.TaskService, "filter_delete", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "queue_tasks", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "cancel_all_task_of", lambda *_args, **_kwargs: None)
- _patch_docstore(monkeypatch, module, delete=lambda *_args, **_kwargs: None)
-
- decrements = []
- updates = []
- update_by_id_payloads = []
-
- def _capture_filter_update(_conditions, info):
- updates.append(info)
- return 1
-
- monkeypatch.setattr(module.DocumentService, "update_by_id", lambda _id, info: update_by_id_payloads.append(info) or True)
-
- monkeypatch.setattr(module.DocumentService, "increment_chunk_num", lambda *args: decrements.append(args))
- monkeypatch.setattr(module.DocumentService, "filter_update", _capture_filter_update)
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(token_num=70, chunk_num=7, process_duration=1.5)])
- assert _run(module.parse.__wrapped__("tenant-1", "ds-1"))["code"] == 0
- assert decrements == [("doc-1", "kb-1", -70, -7, -1.5)]
- # The document update must not zero the counters itself; that is exactly
- # what strands the difference in the KB aggregate.
- assert "chunk_num" not in updates[0]
- assert "token_num" not in updates[0]
-
- decrements.clear()
- monkeypatch.setattr(
- module.DocumentService,
- "query",
- lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.RUNNING.value, token_num=70, chunk_num=7, process_duration=1.5)],
- )
- assert _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))["code"] == 0
- assert decrements == [("doc-1", "kb-1", -70, -7, -1.5)]
- # stop_parsing must not zero the counters in its own document update either.
- assert "chunk_num" not in update_by_id_payloads[0]
- assert "token_num" not in update_by_id_payloads[0]
-
- def test_stop_parsing_branches(self, monkeypatch):
- module = _load_doc_module(monkeypatch)
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))
- assert "don't own the dataset" in res["message"]
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, SimpleNamespace(tenant_id="tenant-1", pipeline_id=None)))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
- res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))
- assert "`document_ids` is required" in res["message"]
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"document_ids": ["doc-1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))
- assert "don't own the document" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.DONE.value)])
- monkeypatch.setattr(
- module,
- "cancel_all_task_of",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("cancel_all_task_of must not be called for non-running docs")),
- )
- monkeypatch.setattr(
- module.DocumentService,
- "update_by_id",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("update_by_id must not be called for non-running docs")),
- )
- res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert res["data"]["error_code"] == module.DOC_STOP_PARSING_INVALID_STATE_ERROR_CODE
- assert res["message"] == module.DOC_STOP_PARSING_INVALID_STATE_MESSAGE
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.RUNNING.value)])
- monkeypatch.setattr(module, "cancel_all_task_of", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module.DocumentService, "update_by_id", lambda *_args, **_kwargs: True)
- _patch_docstore(monkeypatch, module, delete=lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, ["Duplicate document ids: doc-1"]))
- res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))
- assert res["code"] == 0
- assert res["data"]["success_count"] == 1
- assert "Duplicate document ids" in res["data"]["errors"][0]
-
- monkeypatch.setattr(module, "check_duplicate_ids", lambda _ids, _kind: ([], ["Duplicate document ids: doc-1"]))
- res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "Duplicate document ids" in res["message"]
-
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(run=module.TaskStatus.RUNNING.value)])
- res = _run(module.stop_parsing.__wrapped__("tenant-1", "ds-1"))
- assert res["code"] == 0
-
- def test_legacy_chunks_parse_uses_dataset_owner_tenant_for_delete(self, monkeypatch):
- module = _load_doc_module(monkeypatch)
- deleted = []
- requester_tenant = "team-member"
- owner_tenant = "dataset-owner"
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_by_id",
- lambda _id: (True, SimpleNamespace(tenant_id=owner_tenant, pipeline_id=None)),
- )
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"document_ids": ["doc-1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- monkeypatch.setattr(
- module.DocumentService,
- "query",
- lambda **_kwargs: [_DummyDoc(doc_id="doc-1", run=module.TaskStatus.UNSTART.value)],
- )
- monkeypatch.setattr(module.DocumentService, "filter_update", lambda *_args, **_kwargs: 1)
- monkeypatch.setattr(module.DocumentService, "get_by_id", lambda _id: (True, _DummyDoc(doc_id="doc-1")))
- monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("b", "n"))
- monkeypatch.setattr(module.TaskService, "filter_delete", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "queue_tasks", lambda *_args, **_kwargs: None)
- _patch_docstore(
- monkeypatch,
- module,
- index_exist=lambda *_args, **_kwargs: True,
- delete=lambda condition, index, kb_id: deleted.append((condition, index, kb_id)),
- )
-
- res = _run(module.parse.__wrapped__(requester_tenant, "ds-1"))
-
- assert res["code"] == 0
- assert deleted == [({"doc_id": "doc-1"}, module.search.index_name(owner_tenant), "kb-1")]
-
- def test_legacy_chunks_stop_uses_dataset_owner_tenant_for_delete(self, monkeypatch):
- module = _load_doc_module(monkeypatch)
- deleted = []
- requester_tenant = "team-member"
- owner_tenant = "dataset-owner"
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_by_id",
- lambda _id: (True, SimpleNamespace(tenant_id=owner_tenant, pipeline_id=None)),
- )
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"document_ids": ["doc-1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- monkeypatch.setattr(
- module.DocumentService,
- "query",
- lambda **_kwargs: [_DummyDoc(doc_id="doc-1", run=module.TaskStatus.RUNNING.value)],
- )
- monkeypatch.setattr(module, "cancel_all_task_of", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module.DocumentService, "update_by_id", lambda *_args, **_kwargs: True)
- _patch_docstore(
- monkeypatch,
- module,
- index_exist=lambda *_args, **_kwargs: True,
- delete=lambda condition, index, kb_id: deleted.append((condition, index, kb_id)),
- )
-
- res = _run(module.stop_parsing.__wrapped__(requester_tenant, "ds-1"))
-
- assert res["code"] == 0
- assert deleted == [({"doc_id": "doc-1"}, module.search.index_name(owner_tenant), "kb-1")]
-
- def test_stop_parse_documents_cleans_partial_chunks(self, monkeypatch):
- module = _load_doc_module(monkeypatch, module_basename="document_api")
- updated = []
- deleted = []
- decrements = []
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"document_ids": ["doc-1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [object()])
- monkeypatch.setattr(
- module.DocumentService,
- "get_by_id",
- lambda _id: (True, _DummyDoc(doc_id="doc-1", run=module.TaskStatus.RUNNING.value)),
- )
- monkeypatch.setattr(module.TaskService, "query", lambda **_kwargs: [SimpleNamespace(progress=0.5)])
- monkeypatch.setattr(module, "cancel_all_task_of", lambda *_args, **_kwargs: None)
- # release_reparse_counters re-reads the row under lock and decrements the
- # KB by the document's partial counts before chunk_num is zeroed below.
- monkeypatch.setattr(
- sys.modules["api.db.db_models"].Document,
- "fresh_doc",
- SimpleNamespace(id="doc-1", kb_id="kb-1", token_num=70, chunk_num=7, process_duration=1.5),
- )
- monkeypatch.setattr(module.DocumentService, "increment_chunk_num", lambda *args: decrements.append(args))
- monkeypatch.setattr(
- module.DocumentService,
- "update_by_id",
- lambda doc_id, info: updated.append((doc_id, info)) or True,
- )
- _patch_docstore(
- monkeypatch,
- module,
- index_exist=lambda *_args, **_kwargs: True,
- delete=lambda condition, index, kb_id: deleted.append((condition, index, kb_id)),
- )
-
- res = _run(module.stop_parse_documents.__wrapped__("tenant-1", "ds-1"))
-
- assert res["code"] == 0
- assert res["data"]["success_count"] == 1
- assert len(updated) == 1
- updated_doc_id, updated_info = updated[0]
- assert updated_doc_id == "doc-1"
- assert updated_info["run"] == str(module.TaskStatus.CANCEL.value)
- assert updated_info["progress"] == 0
- # release_reparse_counters is the sole counter adjustment; the status
- # update must not set chunk_num itself (that would strand KB counts).
- assert "chunk_num" not in updated_info
- # progress_msg carries a timestamped cancellation marker, so match loosely.
- assert "Task stopped by user." in updated_info["progress_msg"]
- # The partial chunk/token counts are released from the KB aggregate via the
- # row re-read under lock.
- assert decrements == [("doc-1", "kb-1", -70, -7, -1.5)]
- assert deleted == [({"doc_id": "doc-1"}, module.search.index_name("tenant-1"), "kb-1")]
-
- deleted.clear()
- _patch_docstore(monkeypatch, module, index_exist=lambda *_args, **_kwargs: False)
- res = _run(module.stop_parse_documents.__wrapped__("tenant-1", "ds-1"))
- assert res["code"] == 0
- assert deleted == []
-
- def test_list_chunks_branches(self, monkeypatch):
- module = _load_restful_chunk_module(monkeypatch)
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(_route_core(module.list_chunks)("tenant-1", "ds-1", "doc-1"))
- assert "don't own the dataset" in res["message"]
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- res = _run(_route_core(module.list_chunks)("tenant-1", "ds-1", "doc-1"))
- assert "don't own the document" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc()])
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs({})))
- _patch_docstore(monkeypatch, module, index_exist=lambda *_args, **_kwargs: False)
- res = _run(_route_core(module.list_chunks)("tenant-1", "ds-1", "doc-1"))
- assert res["code"] == 0
- assert res["data"]["total"] == 0
- assert res["data"]["chunks"] == []
-
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs({"id": "chunk-1"})))
- _patch_docstore(monkeypatch, module, get=lambda *_args, **_kwargs: None)
- res = _run(_route_core(module.list_chunks)("tenant-1", "ds-1", "doc-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "Chunk not found" in res["message"]
-
- _patch_docstore(
- monkeypatch,
- module,
- get=lambda *_args, **_kwargs: {
- "chunk_id": "chunk-1",
- "content_with_weight": "x",
- "doc_id": "other-doc",
- "docnm_kwd": "doc",
- "position_int": [[1, 2, 3, 4, 5]],
- },
- )
- res = _run(_route_core(module.list_chunks)("tenant-1", "ds-1", "doc-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "Chunk not found" in res["message"]
-
- _patch_docstore(
- monkeypatch,
- module,
- get=lambda *_args, **_kwargs: {
- "chunk_id": "chunk-1",
- "content_with_weight": "x",
- "doc_id": "doc-1",
- "docnm_kwd": "doc",
- "position_int": [[1, 2, 3, 4, 5]],
- },
- )
- res = _run(_route_core(module.list_chunks)("tenant-1", "ds-1", "doc-1"))
- assert res["code"] == 0
- assert res["data"]["total"] == 1
- assert res["data"]["chunks"][0]["id"] == "chunk-1"
-
- def test_list_chunks_uses_dataset_owner_index_for_team_dataset(self, monkeypatch):
- module = _load_restful_chunk_module(monkeypatch)
- seen = {}
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_by_id",
- lambda _dataset_id: (True, SimpleNamespace(tenant_id="owner-tenant")),
- )
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc(kb_id="ds-1")])
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs({})))
-
- def _index_exist(index_name, dataset_id):
- seen["index_exist"] = (index_name, dataset_id)
- return True
-
- class _Retriever:
- async def search(self, _query, index_name, dataset_ids, *_args, **_kwargs):
- seen["search"] = (index_name, dataset_ids)
- return SimpleNamespace(total=0, ids=[], field={}, highlight={})
-
- _patch_docstore(monkeypatch, module, index_exist=_index_exist)
- monkeypatch.setattr(module.settings, "retriever", _Retriever())
-
- res = _run(_route_core(module.list_chunks)("member-tenant", "ds-1", "doc-1"))
-
- assert res["code"] == 0
- assert seen["index_exist"] == ("idx-owner-tenant", "ds-1")
- assert seen["search"] == ("idx-owner-tenant", ["ds-1"])
-
- def test_add_chunk_access_guard(self, monkeypatch):
- module = _load_restful_chunk_module(monkeypatch)
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(_route_core(module.add_chunk)("tenant-1", "ds-1", "doc-1"))
- assert "don't own the dataset" in res["message"]
-
- def test_rm_chunk_branches(self, monkeypatch):
- module = _load_restful_chunk_module(monkeypatch)
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(_route_core(module.rm_chunk)("tenant-1", "ds-1", "doc-1"))
- assert "don't own the dataset" in res["message"]
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- res = _run(_route_core(module.rm_chunk)("tenant-1", "ds-1", "doc-1"))
- assert "don't own the document" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc()])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
- _patch_docstore(
- monkeypatch,
- module,
- delete=lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("delete must not run for empty chunk ids")),
- )
- monkeypatch.setattr(module.DocumentService, "decrement_chunk_num", lambda *_args, **_kwargs: None)
- res = _run(_route_core(module.rm_chunk)("tenant-1", "ds-1", "doc-1"))
- assert res["code"] == 0
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"chunk_ids": ["c1", "c1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda _ids, _kind: (["c1"], ["Duplicate chunk ids: c1"]))
- _patch_docstore(monkeypatch, module, delete=lambda *_args, **_kwargs: 1)
- res = _run(_route_core(module.rm_chunk)("tenant-1", "ds-1", "doc-1"))
- assert res["code"] == 0
- assert res["data"]["errors"] == ["Duplicate chunk ids: c1"]
-
- def test_update_chunk_branches(self, monkeypatch):
- module = _load_restful_chunk_module(monkeypatch)
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- _patch_docstore(monkeypatch, module, get=lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("chunk lookup must not run before access check")))
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert "don't own the dataset" in res["message"]
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert "don't own the document" in res["message"]
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc()])
- _patch_docstore(monkeypatch, module, get=lambda *_args, **_kwargs: None)
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert "Can't find this chunk" in res["message"]
-
- _patch_docstore(monkeypatch, module, get=lambda *_args, **_kwargs: {"doc_id": "other-doc", "content_with_weight": "q\na"})
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert "Can't find this chunk" in res["message"]
-
- doc = _DummyDoc(parser_id="naive")
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [doc])
- monkeypatch.setattr(module.rag_tokenizer, "tokenize", lambda text: text or "")
- monkeypatch.setattr(module.rag_tokenizer, "fine_grained_tokenize", lambda text: text or "")
- monkeypatch.setattr(module.rag_tokenizer, "is_chinese", lambda _text: False)
- monkeypatch.setattr(module.DocumentService, "get_embd_id", lambda _doc_id: "embd")
- monkeypatch.setattr(module.DocumentService, "get_tenant_embd_id", lambda _doc_id: "tm-embd-1")
-
- class _EmbedModel:
- def encode(self, _texts):
- return [np.array([0.2, 0.8]), np.array([0.3, 0.7])], 1
-
- monkeypatch.setattr(module.TenantLLMService, "model_instance", lambda *_args, **_kwargs: _EmbedModel())
- _patch_docstore(monkeypatch, module, get=lambda *_args, **_kwargs: {"doc_id": "doc-1", "content_with_weight": "x"}, update=lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"positions": "bad"}))
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert "`positions` should be a list" in res["message"]
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"positions": [[1, 2, 3, 4, 5]]}))
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert res["code"] == 0
-
- qa_doc = _DummyDoc(parser_id=module.ParserType.QA)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [qa_doc])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"content": "no-separator"}))
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert "Q&A must be separated" in res["message"]
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"content": "Q?\nA!"}))
- _patch_docstore(monkeypatch, module, get=lambda *_args, **_kwargs: {"doc_id": "doc-1", "content_with_weight": "Q?\nA!"}, update=lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module, "beAdoc", lambda d, *_args, **_kwargs: d)
- res = _run(_route_core(module.update_chunk)("tenant-1", "ds-1", "doc-1", "chunk-1"))
- assert res["code"] == 0
-
- def test_retrieval_metadata_validation_matrix(self, monkeypatch):
- module = _load_doc_module(monkeypatch)
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"dataset_ids": "bad"}))
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "`dataset_ids` should be a list" in res["message"]
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"dataset_ids": ["ds-1"]}))
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "don't own the dataset" in res["message"]
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_ids", lambda _ids: [SimpleNamespace(embd_id="m1"), SimpleNamespace(embd_id="m2")])
- monkeypatch.setattr(module.TenantLLMService, "split_model_name_and_factory", lambda embd_id: (embd_id, "f"))
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "different embedding models" in res["message"]
-
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_ids", lambda _ids: [SimpleNamespace(embd_id="m1", tenant_id="tenant-1")])
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "`question` is required." in res["message"]
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": " "}),
- )
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert res["code"] == 0
- assert res["data"]["chunks"] == []
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q", "document_ids": "bad"}),
- )
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "`documents` should be a list" in res["message"]
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q", "document_ids": ["not-owned"]}),
- )
- monkeypatch.setattr(module.KnowledgebaseService, "list_documents_by_ids", lambda _ids: ["doc-1"])
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "don't own the document" in res["message"]
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q", "metadata_condition": {"logic": "and"}}),
- )
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kbs: [])
- monkeypatch.setattr(module, "meta_filter", lambda *_args, **_kwargs: [])
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "code" in res
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q", "highlight": "True"}),
- )
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_ids", lambda _ids: [SimpleNamespace(embd_id="m1", tenant_id="tenant-1", tenant_embd_id="tm-embd-1")])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, SimpleNamespace(tenant_id="tenant-1", embd_id="m1", tenant_embd_id="tm-embd-1")))
-
- class _Retriever:
- async def retrieval(self, *_args, **_kwargs):
- return {"chunks": [], "total": 0}
-
- def retrieval_by_children(self, chunks, *_args, **_kwargs):
- return chunks
-
- monkeypatch.setattr(module, "LLMBundle", lambda *_args, **_kwargs: SimpleNamespace())
- monkeypatch.setattr(module, "label_question", lambda *_args, **_kwargs: {})
- monkeypatch.setattr(module.settings, "retriever", _Retriever())
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert res["code"] == 0, res["message"]
- assert res["data"]["chunks"] == []
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q", "highlight": True}),
- )
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert res["code"] == 0
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q", "highlight": "yes"}),
- )
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "`highlight` should be a boolean" in res["message"]
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q", "highlight": 1}),
- )
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "`highlight` should be a boolean" in res["message"]
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q"}),
- )
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (False, None))
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert "Dataset not found!" in res["message"]
-
- feature_calls = {"cross": None, "keyword": None, "retrieval_question": None}
-
- async def _cross_languages(_tenant_id, _dialog, question, langs):
- feature_calls["cross"] = tuple(langs)
- return f"{question}-xl"
-
- async def _keyword_extraction(_chat_mdl, question):
- feature_calls["keyword"] = question
- return "-kw"
-
- class _FeatureRetriever:
- async def retrieval(self, question, *_args, **_kwargs):
- feature_calls["retrieval_question"] = question
- return {
- "chunks": [
- {
- "chunk_id": "c1",
- "content_with_weight": "content",
- "doc_id": "doc-1",
- "kb_id": "ds-1",
- "vector": [1, 2],
- }
- ],
- "total": 1,
- }
-
- async def retrieval_by_toc(self, question, chunks, tenant_ids, _chat_mdl, size):
- assert question == "q-xl-kw"
- assert chunks and tenant_ids
- assert size == 30
- return [
- {
- "chunk_id": "toc-1",
- "content_with_weight": "toc content",
- "doc_id": "doc-toc",
- "kb_id": "ds-1",
- }
- ]
-
- def retrieval_by_children(self, chunks, _tenant_ids):
- return chunks + [
- {
- "chunk_id": "child-1",
- "content_with_weight": "child content",
- "doc_id": "doc-child",
- "kb_id": "ds-1",
- }
- ]
-
- class _FeatureKgRetriever:
- async def retrieval(self, *_args, **_kwargs):
- return {
- "chunk_id": "kg-1",
- "content_with_weight": "kg content",
- "doc_id": "doc-kg",
- "kb_id": "ds-1",
- }
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "dataset_ids": ["ds-1"],
- "question": "q",
- "rerank_id": "rerank-1",
- "cross_languages": ["fr"],
- "keyword": True,
- "toc_enhance": True,
- "use_kg": True,
- "reference_metadata": {"include": True, "fields": ["author"]},
- }
- ),
- )
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _id: (True, SimpleNamespace(tenant_id="tenant-1", embd_id="m1", tenant_embd_id="tm-embd-1")))
- monkeypatch.setattr(module, "cross_languages", _cross_languages)
- monkeypatch.setattr(module, "keyword_extraction", _keyword_extraction)
- monkeypatch.setattr(module.settings, "retriever", _FeatureRetriever())
- monkeypatch.setattr(module.settings, "kg_retriever", _FeatureKgRetriever())
- monkeypatch.setattr(module, "label_question", lambda *_args, **_kwargs: {})
- monkeypatch.setattr(module, "LLMBundle", lambda *_args, **_kwargs: SimpleNamespace())
- monkeypatch.setattr(
- module.DocMetadataService,
- "get_metadata_for_documents",
- lambda _doc_ids, _kb_id: {
- "doc-1": {"author": "alice", "year": "2025"},
- "doc-toc": {"author": "bob"},
- "doc-child": {"author": "carol"},
- "doc-kg": {"author": "kg-author"},
- },
- )
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert res["code"] == 0, res["message"]
- assert feature_calls["cross"] == ("fr",)
- assert feature_calls["keyword"] == "q-xl"
- assert feature_calls["retrieval_question"] == "q-xl-kw"
- assert res["data"]["chunks"][0]["id"] == "kg-1"
- assert res["data"]["chunks"][0]["content"] == "kg content"
- assert res["data"]["chunks"][0]["document_metadata"]["author"] == "kg-author"
- assert any(chunk["id"] == "toc-1" for chunk in res["data"]["chunks"])
- assert any(chunk["id"] == "child-1" for chunk in res["data"]["chunks"])
-
- class _NotFoundRetriever:
- async def retrieval(self, *_args, **_kwargs):
- raise Exception("boom not_found boom")
-
- def retrieval_by_children(self, chunks, *_args, **_kwargs):
- return chunks
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"dataset_ids": ["ds-1"], "question": "q"}),
- )
- monkeypatch.setattr(module.settings, "retriever", _NotFoundRetriever())
- res = _run(module.retrieval_test.__wrapped__("tenant-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "No chunk found! Check the chunk status please!" in res["message"]
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_download_document.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_download_document.py
deleted file mode 100644
index 36c28b12c3..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_download_document.py
+++ /dev/null
@@ -1,178 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import json
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import bulk_upload_documents, download_document, upload_documents
-from configs import INVALID_API_TOKEN, INVALID_ID_32
-from libs.auth import RAGFlowHttpApiAuth
-from requests import codes
-from utils import compare_by_hash
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 0, "`Authorization` can't be empty"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 109,
- "Authentication error: API key is invalid!",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, tmp_path, expected_code, expected_message):
- res = download_document(invalid_auth, "dataset_id", "document_id", tmp_path / "ragflow_tes.txt")
- assert res.status_code == 401
- with (tmp_path / "ragflow_tes.txt").open("r") as f:
- response_json = json.load(f)
- assert response_json["code"] == expected_code
- assert response_json["message"] == expected_message
-
-
- "generate_test_files",
- [
- "docx",
- "excel",
- "ppt",
- "image",
- "pdf",
- "txt",
- "md",
- "json",
- "eml",
- "html",
- ],
- indirect=True,
-)
-def test_file_type_validation(HttpApiAuth, add_dataset, generate_test_files, request):
- dataset_id = add_dataset
- fp = generate_test_files[request.node.callspec.params["generate_test_files"]]
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- document_id = res["data"][0]["id"]
-
- res = download_document(
- HttpApiAuth,
- dataset_id,
- document_id,
- fp.with_stem("ragflow_test_download"),
- )
- assert res.status_code == codes.ok
- assert compare_by_hash(
- fp,
- fp.with_stem("ragflow_test_download"),
- )
-
-
-class TestDocumentDownload:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- f"The dataset not own the document {INVALID_ID_32}.",
- ),
- ],
- )
- def test_invalid_document_id(self, HttpApiAuth, add_documents, tmp_path, document_id, expected_code, expected_message):
- dataset_id, _ = add_documents
- res = download_document(
- HttpApiAuth,
- dataset_id,
- document_id,
- tmp_path / "ragflow_test_download_1.txt",
- )
- assert res.status_code == codes.ok
- with (tmp_path / "ragflow_test_download_1.txt").open("r") as f:
- response_json = json.load(f)
- assert response_json["code"] == expected_code
- assert response_json["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- f"You do not own the dataset {INVALID_ID_32}.",
- ),
- ],
- )
- def test_invalid_dataset_id(self, HttpApiAuth, add_documents, tmp_path, dataset_id, expected_code, expected_message):
- _, document_ids = add_documents
- res = download_document(
- HttpApiAuth,
- dataset_id,
- document_ids[0],
- tmp_path / "ragflow_test_download_1.txt",
- )
- assert res.status_code == codes.ok
- with (tmp_path / "ragflow_test_download_1.txt").open("r") as f:
- response_json = json.load(f)
- assert response_json["code"] == expected_code
- assert response_json["message"] == expected_message
-
- @pytest.mark.p3
- def test_same_file_repeat(self, HttpApiAuth, add_documents, tmp_path, ragflow_tmp_dir):
- num = 5
- dataset_id, document_ids = add_documents
- for i in range(num):
- res = download_document(
- HttpApiAuth,
- dataset_id,
- document_ids[0],
- tmp_path / f"ragflow_test_download_{i}.txt",
- )
- assert res.status_code == codes.ok
- assert compare_by_hash(
- ragflow_tmp_dir / "ragflow_test_upload_0.txt",
- tmp_path / f"ragflow_test_download_{i}.txt",
- )
-
-
-def test_concurrent_download(HttpApiAuth, add_dataset, tmp_path):
- count = 20
- dataset_id = add_dataset
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, count, tmp_path)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- download_document,
- HttpApiAuth,
- dataset_id,
- document_ids[i],
- tmp_path / f"ragflow_test_download_{i}.txt",
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- for i in range(count):
- assert compare_by_hash(
- tmp_path / f"ragflow_test_upload_{i}.txt",
- tmp_path / f"ragflow_test_download_{i}.txt",
- )
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_list_documents.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_list_documents.py
deleted file mode 100644
index b71afa6415..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_list_documents.py
+++ /dev/null
@@ -1,432 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import list_documents
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import is_sorted
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = list_documents(invalid_auth, "dataset_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestDocumentsList:
- @pytest.mark.p1
- def test_default(self, HttpApiAuth, add_documents):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id)
- assert res["code"] == 0
- assert len(res["data"]["docs"]) == 5
- assert res["data"]["total"] == 5
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- ("", 100, "<MethodNotAllowed '405: Method Not Allowed'>"),
- (
- "invalid_dataset_id",
- 102,
- "You don't own the dataset invalid_dataset_id. ",
- ),
- ],
- )
- def test_invalid_dataset_id(self, HttpApiAuth, dataset_id, expected_code, expected_message):
- res = list_documents(HttpApiAuth, dataset_id)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 0, 2, ""),
- ({"page": 1, "page_size": 2}, 0, 2, ""),
- ({"page": 2, "page_size": 2}, 0, 2, ""),
- ({"page": 3, "page_size": 2}, 0, 1, ""),
- ({"page": "3", "page_size": 2}, 0, 1, ""),
- pytest.param(
- {"page": -1, "page_size": 2},
- 100,
- 0,
- "1064",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page": "a", "page_size": 2},
- 100,
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page(
- self,
- HttpApiAuth,
- add_documents,
- params,
- expected_code,
- expected_page_size,
- expected_message,
- ):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["docs"]) == expected_page_size
- assert res["data"]["total"] == 5
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page_size": None}, 0, 5, ""),
- ({"page_size": 1}, 0, 1, ""),
- ({"page_size": 6}, 0, 5, ""),
- ({"page_size": "1"}, 0, 1, ""),
- pytest.param(
- {"page_size": -1},
- 100,
- 0,
- "1064",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page_size": "a"},
- 100,
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page_size(
- self,
- HttpApiAuth,
- add_documents,
- params,
- expected_code,
- expected_page_size,
- expected_message,
- ):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]["docs"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"orderby": None}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"orderby": "create_time"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"orderby": "update_time"}, 0, lambda r: is_sorted(r["data"]["docs"], "update_time", True), ""),
- pytest.param({"orderby": "name", "desc": "False"}, 0, lambda r: is_sorted(r["data"]["docs"], "name", False), "", marks=pytest.mark.skip(reason="issues/5851")),
- pytest.param({"orderby": "unknown"}, 102, 0, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_orderby(
- self,
- HttpApiAuth,
- add_documents,
- params,
- expected_code,
- assertions,
- expected_message,
- ):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"desc": None}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"desc": "true"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"desc": "True"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"desc": True}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- pytest.param({"desc": "false"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", False), "", marks=pytest.mark.skip(reason="issues/5851")),
- ({"desc": "False"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", False), ""),
- ({"desc": False}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", False), ""),
- ({"desc": "False", "orderby": "update_time"}, 0, lambda r: is_sorted(r["data"]["docs"], "update_time", False), ""),
- pytest.param({"desc": "unknown"}, 102, 0, "desc should be true or false", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_desc(
- self,
- HttpApiAuth,
- add_documents,
- params,
- expected_code,
- assertions,
- expected_message,
- ):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_num",
- [
- ({"keywords": None}, 5),
- ({"keywords": ""}, 5),
- ({"keywords": "0"}, 1),
- ({"keywords": "ragflow_test_upload"}, 5),
- ({"keywords": "unknown"}, 0),
- ],
- )
- def test_keywords(self, HttpApiAuth, add_documents, params, expected_num):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == 0
- assert len(res["data"]["docs"]) == expected_num
- assert res["data"]["total"] == expected_num
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_num, expected_message",
- [
- ({"name": None}, 0, 5, ""),
- ({"name": ""}, 0, 5, ""),
- ({"name": "ragflow_test_upload_0.txt"}, 0, 1, ""),
- (
- {"name": "unknown.txt"},
- 102,
- 0,
- "you don't own the document unknown.txt",
- ),
- ],
- )
- def test_name(
- self,
- HttpApiAuth,
- add_documents,
- params,
- expected_code,
- expected_num,
- expected_message,
- ):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if params["name"] in [None, ""]:
- assert len(res["data"]["docs"]) == expected_num
- else:
- assert res["data"]["docs"][0]["name"] == params["name"]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_num, expected_message",
- [
- (None, 0, 5, ""),
- ("", 0, 5, ""),
- (lambda r: r[0], 0, 1, ""),
- ("unknown.txt", 102, 0, "you don't own the document unknown.txt"),
- ],
- )
- def test_id(
- self,
- HttpApiAuth,
- add_documents,
- document_id,
- expected_code,
- expected_num,
- expected_message,
- ):
- dataset_id, document_ids = add_documents
- if callable(document_id):
- params = {"id": document_id(document_ids)}
- else:
- params = {"id": document_id}
- res = list_documents(HttpApiAuth, dataset_id, params=params)
-
- assert res["code"] == expected_code
- if expected_code == 0:
- if params["id"] in [None, ""]:
- assert len(res["data"]["docs"]) == expected_num
- else:
- doc = res["data"]["docs"][0]
- assert doc["id"] == params["id"]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "document_id, name, expected_code, expected_num, expected_message",
- [
- (lambda r: r[0], "ragflow_test_upload_0.txt", 0, 1, ""),
- (lambda r: r[0], "ragflow_test_upload_1.txt", 0, 0, ""),
- (lambda r: r[0], "unknown", 102, 0, "you don't own the document unknown"),
- (
- "id",
- "ragflow_test_upload_0.txt",
- 102,
- 0,
- "you don't own the document id",
- ),
- ],
- )
- def test_name_and_id(
- self,
- HttpApiAuth,
- add_documents,
- document_id,
- name,
- expected_code,
- expected_num,
- expected_message,
- ):
- dataset_id, document_ids = add_documents
- if callable(document_id):
- params = {"id": document_id(document_ids), "name": name}
- else:
- params = {"id": document_id, "name": name}
-
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- if expected_code == 0:
- assert len(res["data"]["docs"]) == expected_num
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_concurrent_list(self, HttpApiAuth, add_documents):
- dataset_id, _ = add_documents
- count = 100
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_documents, HttpApiAuth, dataset_id) for _ in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_invalid_params(self, HttpApiAuth, add_documents):
- dataset_id, _ = add_documents
- params = {"a": "b"}
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == 0
- assert len(res["data"]["docs"]) == 5
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_code, expected_message",
- [
- (
- {"metadata_condition": "{bad json"},
- 102,
- "metadata_condition must be valid JSON",
- ),
- (
- {"metadata_condition": "[1]"},
- 102,
- "metadata_condition must be an object",
- ),
- ],
- )
- def test_metadata_condition_validation(self, HttpApiAuth, add_documents, params, expected_code, expected_message):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
- assert res["code"] == expected_code
- assert expected_message in res["message"]
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_code, expected_total",
- [
- # Filter with create_time_from in the future - should return 0 results
- ({"create_time_from": "9999999999000"}, 0, 0),
- # Filter with create_time_to in the past - should return 0 results
- ({"create_time_to": "1"}, 0, 0),
- # Filter with create_time_from and create_time_to covering all time
- ({"create_time_from": "0", "create_time_to": "9999999999000"}, 0, 5),
- ],
- )
- def test_create_time_filter(self, HttpApiAuth, add_documents, params, expected_code, expected_total):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
-
- assert res["code"] == expected_code
- assert len(res["data"]["docs"]) == expected_total
- assert res["data"]["total"] == 5
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_code, expected_message",
- [
- # Invalid run status - should return error
- ({"run": ["INVALID_STATUS"]}, 102, "Invalid filter run status conditions: INVALID_STATUS"),
- ],
- )
- def test_run_status_filter_invalid(self, HttpApiAuth, add_documents, params, expected_code, expected_message):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
-
- assert res["code"] == expected_code
- assert expected_message in res["message"]
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_size",
- [
- # Invalid run status - should return error
- ({"run": ["UNSTART"]}, 5),
- ],
- )
- def test_run_status_filter_unstart(self, HttpApiAuth, add_documents, params, expected_size):
- dataset_id, _ = add_documents
- res = list_documents(HttpApiAuth, dataset_id, params=params)
-
- assert res["code"] == 0
- assert res["data"]["total"] == expected_size
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_batch_update.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_batch_update.py
deleted file mode 100644
index b4413c6830..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_batch_update.py
+++ /dev/null
@@ -1,389 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-"""
-End-to-end tests for metadata batch update API.
-
-This test file converts the unit test test_metadata_batch_update from test_doc_sdk_routes_unit.py
-to end-to-end tests that call the actual HTTP API.
-"""
-
-import pytest
-from common import (
- update_documents_metadata,
- list_documents,
- delete_documents,
- upload_documents,
-)
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-def _create_and_upload_in_batches(auth, dataset_id, num_docs, tmp_path, batch_size=100):
- """Create and upload documents in batches to avoid too many open files."""
- document_ids = []
- for batch_start in range(0, num_docs, batch_size):
- batch_end = min(batch_start + batch_size, num_docs)
- fps = []
- for i in range(batch_start, batch_end):
- fp = tmp_path / f"ragflow_test_upload_{i}.txt"
- fp.write_text(f"Test document content {i}\n" * 10)
- fps.append(fp)
- res = upload_documents(auth, dataset_id, fps)
- for doc in res["data"]:
- document_ids.append(doc["id"])
- return document_ids
-
-
[email protected](scope="class")
-def dataset_with_docs(request, HttpApiAuth, add_dataset, ragflow_tmp_dir):
- """Create a dataset with test documents and clean up after test class."""
- dataset_id = add_dataset
-
- # Upload test documents
- fps = []
- for i in range(5):
- fp = ragflow_tmp_dir / f"test_doc_{i}.txt"
- fp.write_text(f"Test document content {i}\n" * 10)
- fps.append(fp)
-
- upload_res = upload_documents(HttpApiAuth, dataset_id, fps)
- assert upload_res["code"] == 0, f"Failed to upload documents: {upload_res}"
-
- document_ids = [doc["id"] for doc in upload_res["data"]]
-
- def cleanup():
- delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
-
- request.addfinalizer(cleanup)
-
- return dataset_id, document_ids
-
-
-class TestMetadataBatchUpdate:
- def test_batch_update_metadata(self, HttpApiAuth, add_dataset, ragflow_tmp_dir):
- """
- Test batch_update_metadata via HTTP API.
- This test calls the real batch_update_metadata on the server.
- """
- dataset_id = add_dataset
-
- # Upload documents in batches to avoid too many open files
- document_ids = _create_and_upload_in_batches(HttpApiAuth, dataset_id, 1010, ragflow_tmp_dir)
-
- # Update metadata via batch update API
- updates = [{"key": "author", "value": "new_author"}, {"key": "status", "value": "processed"}]
- res = update_documents_metadata(HttpApiAuth, dataset_id, {"selector": {"document_ids": document_ids}, "updates": updates})
-
- # Verify the API call succeeded
- assert res["code"] == 0, f"Expected code 0, got {res.get('code')}: {res.get('message')}"
- assert res["data"]["updated"] == 1010, f"Expected 1100 documents updated, got {res['data']['updated']}"
-
- # Verify metadata was updated for first and last few sample documents
- sample_ids = document_ids[:5] + document_ids[-5:]
- list_res = list_documents(HttpApiAuth, dataset_id, {"ids": sample_ids})
- assert list_res["code"] == 0
-
- for doc in list_res["data"]["docs"]:
- assert doc["meta_fields"].get("author") == "new_author", f"Expected author='new_author', got {doc['meta_fields'].get('author')}"
- assert doc["meta_fields"].get("status") == "processed", f"Expected status='processed', got {doc['meta_fields'].get('status')}"
-
- # Cleanup
- delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
-
-
-class TestMetadataBatchUpdateValidation:
- """Test validation scenarios for metadata batch update API."""
-
- def test_invalid_auth(self):
- """Test that invalid authentication returns 401."""
- res = update_documents_metadata(
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- "dataset_id",
- {"selector": {"document_ids": []}, "updates": [], "deletes": []},
- )
- assert res["code"] == 401
-
- def test_invalid_dataset_id(self, HttpApiAuth):
- """Test that invalid dataset ID returns error."""
- res = update_documents_metadata(
- HttpApiAuth,
- "invalid_dataset_id",
- {"selector": {"document_ids": []}, "updates": [], "deletes": []},
- )
- assert res["code"] == 102
- assert "You don't own the dataset" in res["message"]
-
- def test_selector_not_object(self, HttpApiAuth, dataset_with_docs):
- """Test that selector must be an object."""
- dataset_id, _ = dataset_with_docs
-
- # Pass selector as a list instead of object
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": [1], "updates": [], "deletes": []},
- )
- assert res["code"] == 102
- assert "selector must be an object" in res["message"]
-
- def test_updates_and_deletes_must_be_lists(self, HttpApiAuth, dataset_with_docs):
- """Test that updates and deletes must be lists."""
- dataset_id, _ = dataset_with_docs
-
- # Pass updates and deletes as objects instead of lists
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {}, "updates": {"key": "value"}, "deletes": []},
- )
- assert res["code"] == 102
- assert "updates and deletes must be lists" in res["message"]
-
- def test_metadata_condition_must_be_object(self, HttpApiAuth, dataset_with_docs):
- """Test that metadata_condition must be an object."""
- dataset_id, _ = dataset_with_docs
-
- # Pass metadata_condition as a list instead of object
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"metadata_condition": [1]}, "updates": [], "deletes": []},
- )
- assert res["code"] == 102
- assert "metadata_condition must be an object" in res["message"]
-
- def test_document_ids_must_be_list(self, HttpApiAuth, dataset_with_docs):
- """Test that document_ids must be a list."""
- dataset_id, _ = dataset_with_docs
-
- # Pass document_ids as a string instead of list
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"document_ids": "doc-1"}, "updates": [], "deletes": []},
- )
- assert res["code"] == 102
- assert "document_ids must be a list" in res["message"]
-
- def test_each_update_requires_key_and_value(self, HttpApiAuth, dataset_with_docs):
- """Test that each update requires key and value."""
- dataset_id, _ = dataset_with_docs
-
- # Pass update without key
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {}, "updates": [{"key": ""}], "deletes": []},
- )
- assert res["code"] == 102
- assert "Each update requires key and value" in res["message"]
-
- def test_each_delete_requires_key(self, HttpApiAuth, dataset_with_docs):
- """Test that each delete requires key."""
- dataset_id, _ = dataset_with_docs
-
- # Pass delete without key
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {}, "updates": [], "deletes": [{"x": "y"}]},
- )
- assert res["code"] == 102
- assert "Each delete requires key" in res["message"]
-
- def test_documents_not_belong_to_dataset(self, HttpApiAuth, dataset_with_docs):
- """Test that documents must belong to the dataset."""
- dataset_id, _ = dataset_with_docs
-
- # Pass document IDs that don't belong to the dataset
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {
- "selector": {"document_ids": ["doc-does-not-exist-1", "doc-does-not-exist-2"]},
- "updates": [{"key": "author", "value": "test"}],
- "deletes": [],
- },
- )
- assert res["code"] == 102
- assert "do not belong to dataset" in res["message"]
-
-
-class TestMetadataBatchUpdateSuccess:
- """Test successful scenarios for metadata batch update API."""
-
- def test_batch_update_by_document_ids(self, HttpApiAuth, dataset_with_docs):
- """Test batch update metadata by document IDs."""
- dataset_id, document_ids = dataset_with_docs
-
- # Update metadata for specific documents
- updates = [{"key": "author", "value": "test_author"}, {"key": "status", "value": "processed"}]
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"document_ids": document_ids}, "updates": updates, "deletes": []},
- )
-
- assert res["code"] == 0, f"Expected code 0, got {res.get('code')}: {res.get('message')}"
- assert res["data"]["updated"] == 5
- assert res["data"]["matched_docs"] == 5
-
- # Verify metadata was updated
- list_res = list_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
- assert list_res["code"] == 0
-
- for doc in list_res["data"]["docs"]:
- assert doc["meta_fields"].get("author") == "test_author"
- assert doc["meta_fields"].get("status") == "processed"
-
- def test_batch_update_with_metadata_condition(self, HttpApiAuth, dataset_with_docs):
- """Test batch update metadata using metadata_condition filter."""
- dataset_id, document_ids = dataset_with_docs
-
- # First, set initial metadata
- updates = [{"key": "category", "value": "test_category"}]
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"document_ids": document_ids}, "updates": updates, "deletes": []},
- )
-
- assert res["code"] == 0
- assert res["data"]["updated"] == 5
- assert res["data"]["matched_docs"] == 5
-
- # Now update only documents with category="test_category"
- updates = [{"key": "author", "value": "filtered_author"}]
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {
- "selector": {
- "document_ids": document_ids,
- "metadata_condition": {"conditions": [{"comparison_operator": "is", "name": "category", "value": "test_category"}]},
- },
- "updates": updates,
- "deletes": [],
- },
- )
-
- assert res["code"] == 0, f"Expected code 0, got {res.get('code')}: {res.get('message')}"
- assert res["data"]["updated"] == 5
- assert res["data"]["matched_docs"] == 5
-
- def test_batch_delete_metadata(self, HttpApiAuth, dataset_with_docs):
- """Test batch delete metadata keys."""
- dataset_id, document_ids = dataset_with_docs
-
- # First, set some metadata
- updates = [{"key": "author", "value": "test_author"}, {"key": "status", "value": "processed"}]
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"document_ids": document_ids}, "updates": updates, "deletes": []},
- )
- assert res["code"] == 0
-
- # Now delete the "author" key
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"document_ids": document_ids}, "updates": [], "deletes": [{"key": "author"}]},
- )
-
- assert res["code"] == 0, f"Expected code 0, got {res.get('code')}: {res.get('message')}"
- assert res["data"]["updated"] == 5
-
- # Verify author was deleted but status remains
- list_res = list_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
- assert list_res["code"] == 0
-
- for doc in list_res["data"]["docs"]:
- assert "author" not in doc["meta_fields"] or doc["meta_fields"].get("author") is None
- assert doc["meta_fields"].get("status") == "processed"
-
- def test_batch_update_and_delete_combined(self, HttpApiAuth, dataset_with_docs):
- """Test batch update and delete metadata in same request."""
- dataset_id, document_ids = dataset_with_docs
-
- # First, set initial metadata
- updates = [{"key": "author", "value": "old_author"}, {"key": "status", "value": "old_status"}]
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"document_ids": document_ids}, "updates": updates, "deletes": []},
- )
- assert res["code"] == 0
-
- # Now update and delete in same request
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {
- "selector": {"document_ids": document_ids},
- "updates": [{"key": "author", "value": "new_author"}],
- "deletes": [{"key": "status"}],
- },
- )
-
- assert res["code"] == 0, f"Expected code 0, got {res.get('code')}: {res.get('message')}"
- assert res["data"]["updated"] == 5
-
- # Verify the changes
- list_res = list_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
- assert list_res["code"] == 0
-
- for doc in list_res["data"]["docs"]:
- assert doc["meta_fields"].get("author") == "new_author"
- assert "status" not in doc["meta_fields"] or doc["meta_fields"].get("status") is None
-
- def test_update_with_empty_document_ids(self, HttpApiAuth, dataset_with_docs):
- """Test that empty document_ids returns success with 0 matched."""
- dataset_id, _ = dataset_with_docs
-
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {"selector": {"document_ids": []}, "updates": [{"key": "author", "value": "test"}], "deletes": []},
- )
-
- assert res["code"] == 0
- assert res["data"]["updated"] == 0
- assert res["data"]["matched_docs"] == 0
-
- def test_update_with_no_matching_metadata_condition(self, HttpApiAuth, dataset_with_docs):
- """Test that non-matching metadata_condition returns 0 matched."""
- dataset_id, document_ids = dataset_with_docs
-
- res = update_documents_metadata(
- HttpApiAuth,
- dataset_id,
- {
- "selector": {
- "document_ids": document_ids,
- "metadata_condition": {"conditions": [{"comparison_operator": "is", "name": "nonexistent_key", "value": "nonexistent_value"}]},
- },
- "updates": [{"key": "author", "value": "test"}],
- "deletes": [],
- },
- )
-
- assert res["code"] == 0
- assert res["data"]["updated"] == 0
- assert res["data"]["matched_docs"] == 0
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_config.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_config.py
deleted file mode 100644
index 549340a470..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_config.py
+++ /dev/null
@@ -1,140 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-import requests
-from configs import HOST_ADDRESS, VERSION, INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from common import HEADERS
-
-DATASETS_API_URL = f"/api/{VERSION}/datasets"
-
-
-def get_dataset_metadata_config(auth, dataset_id, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/metadata/config"
- res = requests.get(url=url, headers=headers, auth=auth)
- return res.json()
-
-
-def update_dataset_metadata_config(auth, dataset_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/metadata/config"
- res = requests.put(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def update_document_metadata_config(auth, dataset_id, document_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/documents/{document_id}/metadata/config"
- res = requests.put(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-class TestDatasetMetadataConfigAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_get_metadata_config_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = get_dataset_metadata_config(invalid_auth, "dataset_id")
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_update_metadata_config_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = update_dataset_metadata_config(invalid_auth, "dataset_id", {})
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("clear_datasets")
-class TestDatasetMetadataConfig:
- @pytest.mark.p2
- def test_get_metadata_config_success(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = get_dataset_metadata_config(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_get_metadata_config_invalid_dataset(self, HttpApiAuth):
- res = get_dataset_metadata_config(HttpApiAuth, "invalid_dataset_id")
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_update_metadata_config_missing_payload(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = update_dataset_metadata_config(HttpApiAuth, dataset_id)
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_update_metadata_config_invalid_dataset(self, HttpApiAuth):
- res = update_dataset_metadata_config(HttpApiAuth, "invalid_dataset_id", {"fields": []})
- assert res["code"] != 0, res
-
-
-class TestDocumentMetadataConfigAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_update_document_metadata_config_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = update_document_metadata_config(invalid_auth, "dataset_id", "document_id", {})
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("clear_datasets")
-class TestDocumentMetadataConfig:
- @pytest.mark.p2
- def test_update_document_metadata_config_not_found(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = update_document_metadata_config(HttpApiAuth, dataset_id, "nonexistent_doc_id", {})
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_update_document_metadata_config_invalid_dataset(self, HttpApiAuth, add_document_func):
- _, doc_id = add_document_func
- res = update_document_metadata_config(HttpApiAuth, "invalid_dataset_id", doc_id, {})
- assert res["code"] != 0, res
-
- @pytest.mark.p2
- def test_update_document_metadata_config_invalid_document(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = update_document_metadata_config(HttpApiAuth, dataset_id, "invalid_doc_id", {})
- assert res["code"] != 0, res
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_retrieval.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_retrieval.py
deleted file mode 100644
index 1afc41bcfd..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_retrieval.py
+++ /dev/null
@@ -1,153 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-"""
-End-to-end test for metadata filtering during retrieval.
-
-Tests that chunks are only retrieved from documents matching the metadata condition.
-"""
-
-import pytest
-import logging
-from common import (
- create_dataset,
- delete_datasets,
- list_documents,
- update_document,
- upload_documents,
- parse_documents,
- retrieval_chunks,
-)
-from utils import wait_for
-
-
-@wait_for(120, 1, "Document parsing timeout")
-def _condition_parsing_complete(_auth, dataset_id):
- res = list_documents(_auth, dataset_id)
- if res["code"] != 0:
- return False
-
- for doc in res["data"]["docs"]:
- status = doc.get("run", "UNKNOWN")
- if status in ("FAIL", "FAILED"):
- pytest.fail(f"Document parsing failed: {doc}")
- return False
- if status != "DONE":
- return False
- return True
-
-
[email protected](scope="function")
-def add_dataset_with_metadata(HttpApiAuth):
- # First create the dataset
- res = create_dataset(HttpApiAuth, {"name": f"test_metadata_{int(__import__('time').time())}", "chunk_method": "naive"})
-
- assert res["code"] == 0, f"Failed to create dataset: {res}"
- dataset_id = res["data"]["id"]
-
- # Then configure metadata via the update_metadata_setting endpoint
- import requests
- from configs import HOST_ADDRESS, VERSION
-
- res = requests.put(
- url=f"{HOST_ADDRESS}/api/{VERSION}/datasets/{dataset_id}/metadata/config",
- headers={"Content-Type": "application/json"},
- auth=HttpApiAuth,
- json={
- "metadata": [
- {"key": "character", "type": "string", "description": "Historical figure name"},
- {"key": "era", "type": "string", "description": "Historical era"},
- {"key": "achievements", "type": "list", "description": "Major achievements"},
- ]
- },
- ).json()
-
- assert res["code"] == 0, f"Failed to configure metadata: {res}"
-
- yield dataset_id
-
- # Cleanup
- delete_datasets(HttpApiAuth, {"ids": [dataset_id]})
-
-
-class TestMetadataWithRetrieval:
- """Test retrieval with metadata filtering."""
-
- def test_retrieval_with_metadata_filter(self, HttpApiAuth, add_dataset_with_metadata, tmp_path):
- """
- Test that retrieval respects metadata filters.
-
- Verifies that chunks are only retrieved from documents matching the metadata condition.
- """
- dataset_id = add_dataset_with_metadata
-
- # Create two documents with different metadata
- content_doc1 = "Document about Zhuge Liang who lived in Three Kingdoms period."
- content_doc2 = "Document about Cao Cao who lived in Late Eastern Han Dynasty."
-
- fp1 = tmp_path / "doc1_zhuge_liang.txt"
- fp2 = tmp_path / "doc2_cao_cao.txt"
-
- with open(fp1, "w", encoding="utf-8") as f:
- f.write(content_doc1)
- with open(fp2, "w", encoding="utf-8") as f:
- f.write(content_doc2)
-
- # Upload both documents
- res = upload_documents(HttpApiAuth, dataset_id, [fp1, fp2])
- assert res["code"] == 0, f"Failed to upload documents: {res}"
-
- doc1_id = res["data"][0]["id"]
- doc2_id = res["data"][1]["id"]
-
- # Add different metadata to each document
- res = update_document(HttpApiAuth, dataset_id, doc1_id, {"meta_fields": {"character": "Zhuge Liang", "era": "Three Kingdoms"}})
- assert res["code"] == 0, f"Failed to update doc1 metadata: {res}"
-
- res = update_document(HttpApiAuth, dataset_id, doc2_id, {"meta_fields": {"character": "Cao Cao", "era": "Late Eastern Han"}})
- assert res["code"] == 0, f"Failed to update doc2 metadata: {res}"
-
- # Parse both documents
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": [doc1_id, doc2_id]})
- assert res["code"] == 0, f"Failed to trigger parsing: {res}"
-
- # Wait for parsing to complete
- assert _condition_parsing_complete(HttpApiAuth, dataset_id), "Parsing timeout"
-
- # Test retrieval WITH metadata filter for "Zhuge Liang"
- res = retrieval_chunks(
- HttpApiAuth,
- {
- "question": "Zhuge Liang",
- "dataset_ids": [dataset_id],
- "metadata_condition": {"logic": "and", "conditions": [{"name": "character", "comparison_operator": "is", "value": "Zhuge Liang"}]},
- },
- )
- assert res["code"] == 0, f"Retrieval with metadata filter failed: {res}"
-
- chunks_with_filter = res["data"]["chunks"]
- doc_ids_with_filter = set(chunk.get("document_id", "") for chunk in chunks_with_filter)
-
- logging.info(f"✓ Retrieved {len(chunks_with_filter)} chunks from documents: {doc_ids_with_filter}")
-
- # Verify that filtered results only contain doc1 (Zhuge Liang)
- if len(chunks_with_filter) > 0:
- assert doc1_id in doc_ids_with_filter, f"Filtered results should contain doc1 (Zhuge Liang), but got: {doc_ids_with_filter}"
- assert doc2_id not in doc_ids_with_filter, f"Filtered results should NOT contain doc2 (Cao Cao), but got: {doc_ids_with_filter}"
- logging.info("Metadata filter correctly excluded chunks from non-matching documents")
- else:
- logging.warning("No chunks retrieved with filter - this might be due to embedding model not configured")
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_summary.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_summary.py
deleted file mode 100644
index bd2ca9beda..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_summary.py
+++ /dev/null
@@ -1,85 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import metadata_summary, update_document
-
-
-def _summary_to_counts(summary):
- counts = {}
- for key, field_data in summary.items():
- # New format: {key: {"type": "...", "values": [[value, count], ...]}}
- pairs = field_data["values"]
- counts[key] = {str(k): v for k, v in pairs}
- return counts
-
-
-class TestMetadataSummary:
- @pytest.mark.p2
- def test_metadata_summary_nonexistent_kb_id(self, HttpApiAuth, add_document_func):
- """
- Call with non-existent dataset
- """
- res = metadata_summary(HttpApiAuth, "0" * 32)
- assert res["code"] == 102, res
-
- @pytest.mark.p2
- def test_metadata_summary_invalid_kb_id(self, HttpApiAuth, add_document_func):
- """Test metadata summary when user doesn't have access to the dataset."""
- kb_id, doc_id = add_document_func
- invalid_kb_id = "invalid_" + kb_id
- # Call with a dataset that the user doesn't have access to
- res = metadata_summary(HttpApiAuth, invalid_kb_id)
- assert res["code"] == 102, res
- assert res["message"] == f"You don't own the dataset {invalid_kb_id}. "
-
- @pytest.mark.p2
- def test_metadata_summary_success(self, HttpApiAuth, add_document_func):
- """Test metadata summary success case"""
- kb_id, doc_id = add_document_func
- # Test successful case
- res = metadata_summary(HttpApiAuth, kb_id)
- assert res["code"] == 0, res
- assert "summary" in res["data"], res
-
- @pytest.mark.p2
- def test_metadata_summary_counts(self, HttpApiAuth, add_documents_func):
- """
- test normal cases
- :param HttpApiAuth:
- :param add_documents_func:
- :return:
- """
- dataset_id, document_ids = add_documents_func
- payloads = [
- {"tags": ["foo", "bar"], "author": "alice"},
- {"tags": ["foo"], "author": "bob"},
- {"tags": ["bar", "baz"], "author": ""},
- ]
- for doc_id, meta_fields in zip(document_ids, payloads):
- res = update_document(HttpApiAuth, dataset_id, doc_id, {"meta_fields": meta_fields})
- assert res["code"] == 0, res
-
- res = metadata_summary(HttpApiAuth, dataset_id)
- assert res["code"] == 0, res
-
- summary = res["data"]["summary"]
- counts = _summary_to_counts(summary)
- assert counts["tags"]["foo"] == 2, counts
- assert counts["tags"]["bar"] == 2, counts
- assert counts["tags"]["baz"] == 1, counts
- assert counts["author"]["alice"] == 1, counts
- assert counts["author"]["bob"] == 1, counts
- assert "None" not in counts["author"], counts
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_parse_documents.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_parse_documents.py
deleted file mode 100644
index 4411cd43cc..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_parse_documents.py
+++ /dev/null
@@ -1,251 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import bulk_upload_documents, delete_documents, list_chunks, list_documents, parse_documents
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import wait_for
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_auth, _dataset_id, _document_ids=None):
- res = list_documents(_auth, _dataset_id)
- target_docs = res["data"]["docs"]
-
- if _document_ids is None:
- for doc in target_docs:
- if doc["run"] != "DONE":
- return False
- return True
-
- target_ids = set(_document_ids)
- for doc in target_docs:
- if doc["id"] in target_ids:
- if doc.get("run") != "DONE":
- return False
- return True
-
-
-def validate_document_details(auth, dataset_id, document_ids):
- # currently list_documents not support search by document id
- for document_id in document_ids:
- res = list_documents(auth, dataset_id, params={"id": document_id})
- doc = res["data"]["docs"][0]
- assert doc["run"] == "DONE"
- assert len(doc["process_begin_at"]) > 0
- assert doc["process_duration"] > 0
- assert doc["progress"] > 0
- assert "Task done" in doc["progress_msg"]
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = parse_documents(invalid_auth, "dataset_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestDocumentsParse:
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param(None, 102, """AttributeError("\'NoneType\' object has no attribute \'get\'")""", marks=pytest.mark.skip),
- pytest.param({"document_ids": []}, 102, "`document_ids` is required", marks=pytest.mark.p1),
- pytest.param({"document_ids": ["invalid_id"]}, 102, "Documents not found: ['invalid_id']", marks=pytest.mark.p3),
- pytest.param({"document_ids": ["\n!?。;!?\"'"]}, 102, """Documents not found: [\'\\n!?。;!?"\\\'\']""", marks=pytest.mark.p3),
- pytest.param("not json", 102, "AttributeError(\"'str' object has no attribute 'get'\")", marks=pytest.mark.skip),
- pytest.param(lambda r: {"document_ids": r[:1]}, 0, "", marks=pytest.mark.p1),
- pytest.param(lambda r: {"document_ids": r}, 0, "", marks=pytest.mark.p1),
- ],
- )
- def test_basic_scenarios(self, HttpApiAuth, add_documents_func, payload, expected_code, expected_message):
- dataset_id, document_ids = add_documents_func
- if callable(payload):
- payload = payload(document_ids)
- res = parse_documents(HttpApiAuth, dataset_id, payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
- if expected_code == 0:
- condition(HttpApiAuth, dataset_id, payload["document_ids"])
- validate_document_details(HttpApiAuth, dataset_id, payload["document_ids"])
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- ("", 102, "You don't own the dataset ."),
- (
- "invalid_dataset_id",
- 102,
- "You don't own the dataset invalid_dataset_id.",
- ),
- ],
- )
- def test_invalid_dataset_id(
- self,
- HttpApiAuth,
- add_documents_func,
- dataset_id,
- expected_code,
- expected_message,
- ):
- _, document_ids = add_documents_func
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"document_ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"document_ids": r[:1] + ["invalid_id"] + r[1:3]}, marks=pytest.mark.p1),
- pytest.param(lambda r: {"document_ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_parse_partial_invalid_document_id(self, HttpApiAuth, add_documents_func, payload):
- dataset_id, document_ids = add_documents_func
- if callable(payload):
- payload = payload(document_ids)
- res = parse_documents(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 102
- assert res["message"] == "Documents not found: ['invalid_id']"
-
- condition(HttpApiAuth, dataset_id)
-
- validate_document_details(HttpApiAuth, dataset_id, document_ids)
-
- @pytest.mark.p3
- def test_repeated_parse(self, HttpApiAuth, add_documents_func):
- dataset_id, document_ids = add_documents_func
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0
-
- condition(HttpApiAuth, dataset_id)
-
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0
-
- @pytest.mark.p3
- def test_duplicate_parse(self, HttpApiAuth, add_documents_func):
- dataset_id, document_ids = add_documents_func
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids + document_ids})
- assert res["code"] == 0
- assert "Duplicate document ids" in res["data"]["errors"][0]
- assert res["data"]["success_count"] == 3
-
- condition(HttpApiAuth, dataset_id)
-
- validate_document_details(HttpApiAuth, dataset_id, document_ids)
-
- @pytest.mark.p2
- def test_chunks_retrievable_after_parse_status_done(self, HttpApiAuth, add_dataset_func, ragflow_tmp_dir):
- @wait_for(30, 0.1, "Document parsing timeout")
- def wait_until_done(ids):
- r = list_documents(HttpApiAuth, dataset_id)
- target_ids = set(ids)
- for doc in r["data"]["docs"]:
- if doc["id"] in target_ids and doc.get("run") != "DONE":
- return False
- return True
-
- dataset_id = add_dataset_func
-
- # if there is a bug it can be non-deterministic, so repeat 10 times
- iterations = 10
- for i in range(1, iterations + 1):
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, 1, ragflow_tmp_dir)
-
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0, f"parse_documents failed: {res}"
-
- wait_until_done(document_ids)
-
- for document_id in document_ids:
- res = list_chunks(HttpApiAuth, dataset_id, document_id)
- assert res["code"] == 0, f"list_chunks failed: {res}"
- assert res["data"]["doc"]["chunk_count"] > 0, f"Document {document_id} has run=DONE but chunk_count is 0"
- assert len(res["data"]["chunks"]) > 0, f"Document {document_id} has run=DONE but no chunks returned"
-
- delete_documents(HttpApiAuth, dataset_id, {"ids": document_ids})
-
-
-def test_parse_100_files(HttpApiAuth, add_dataset_func, tmp_path):
- @wait_for(200, 1, "Document parsing timeout")
- def condition(_auth, _dataset_id, _document_num):
- res = list_documents(_auth, _dataset_id, {"page_size": _document_num})
- for doc in res["data"]["docs"]:
- if doc["run"] != "DONE":
- return False
- return True
-
- document_num = 100
- dataset_id = add_dataset_func
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, document_num, tmp_path)
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0
-
- condition(HttpApiAuth, dataset_id, document_num)
-
- validate_document_details(HttpApiAuth, dataset_id, document_ids)
-
-
-def test_concurrent_parse(HttpApiAuth, add_dataset_func, tmp_path):
- @wait_for(200, 1, "Document parsing timeout")
- def condition(_auth, _dataset_id, _document_num):
- res = list_documents(_auth, _dataset_id, {"page_size": _document_num})
- for doc in res["data"]["docs"]:
- if doc["run"] != "DONE":
- return False
- return True
-
- count = 100
- dataset_id = add_dataset_func
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, count, tmp_path)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- parse_documents,
- HttpApiAuth,
- dataset_id,
- {"document_ids": document_ids[i : i + 1]},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- condition(HttpApiAuth, dataset_id, count)
-
- validate_document_details(HttpApiAuth, dataset_id, document_ids)
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_stop_parse_documents.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_stop_parse_documents.py
deleted file mode 100644
index c176964289..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_stop_parse_documents.py
+++ /dev/null
@@ -1,203 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor
-from time import sleep
-
-import pytest
-from common import bulk_upload_documents, list_documents, parse_documents, stop_parse_documents
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import wait_for
-
-
-def validate_document_parse_done(auth, dataset_id, document_ids):
- for document_id in document_ids:
- res = list_documents(auth, dataset_id, params={"id": document_id})
- doc = res["data"]["docs"][0]
- assert doc["run"] == "DONE"
- assert len(doc["process_begin_at"]) > 0
- assert doc["process_duration"] > 0
- assert doc["progress"] > 0
- assert "Task done" in doc["progress_msg"]
-
-
-def validate_document_parse_cancel(auth, dataset_id, document_ids):
- for document_id in document_ids:
- res = list_documents(auth, dataset_id, params={"id": document_id})
- doc = res["data"]["docs"][0]
- assert doc["run"] == "CANCEL"
- assert len(doc["process_begin_at"]) > 0
- assert doc["progress"] == 0.0
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = stop_parse_documents(invalid_auth, "dataset_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestDocumentsParseStop:
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param(None, 102, """AttributeError("\'NoneType\' object has no attribute \'get\'")""", marks=pytest.mark.skip),
- pytest.param({"document_ids": []}, 102, "`document_ids` is required", marks=pytest.mark.p1),
- pytest.param({"document_ids": ["invalid_id"]}, 102, "you don't own the document invalid_id", marks=pytest.mark.p3),
- pytest.param({"document_ids": ["\n!?。;!?\"'"]}, 102, """you don\'t own the document \n!?。;!?"\'""", marks=pytest.mark.p3),
- pytest.param("not json", 102, "AttributeError(\"'str' object has no attribute 'get'\")", marks=pytest.mark.skip),
- pytest.param(lambda r: {"document_ids": r[:1]}, 0, "", marks=pytest.mark.p1),
- pytest.param(lambda r: {"document_ids": r}, 0, "", marks=pytest.mark.p1),
- ],
- )
- def test_basic_scenarios(self, HttpApiAuth, add_documents_func, payload, expected_code, expected_message):
- @wait_for(10, 1, "Document parsing timeout")
- def condition(_auth, _dataset_id, _document_ids):
- for _document_id in _document_ids:
- res = list_documents(_auth, _dataset_id, {"id": _document_id})
- if res["data"]["docs"][0]["run"] != "DONE":
- return False
- return True
-
- dataset_id, document_ids = add_documents_func
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
-
- if callable(payload):
- payload = payload(document_ids)
-
- res = stop_parse_documents(HttpApiAuth, dataset_id, payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert res["message"] == expected_message
- else:
- completed_document_ids = list(set(document_ids) - set(payload["document_ids"]))
- condition(HttpApiAuth, dataset_id, completed_document_ids)
- validate_document_parse_cancel(HttpApiAuth, dataset_id, payload["document_ids"])
- validate_document_parse_done(HttpApiAuth, dataset_id, completed_document_ids)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "invalid_dataset_id, expected_code, expected_message",
- [
- ("", 102, "You don't own the dataset ."),
- (
- "invalid_dataset_id",
- 102,
- "You don't own the dataset invalid_dataset_id.",
- ),
- ],
- )
- def test_invalid_dataset_id(
- self,
- HttpApiAuth,
- add_documents_func,
- invalid_dataset_id,
- expected_code,
- expected_message,
- ):
- dataset_id, document_ids = add_documents_func
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- res = stop_parse_documents(HttpApiAuth, invalid_dataset_id, {"document_ids": document_ids})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.skip
- @pytest.mark.parametrize(
- "payload",
- [
- lambda r: {"document_ids": ["invalid_id"] + r},
- lambda r: {"document_ids": r[:1] + ["invalid_id"] + r[1:3]},
- lambda r: {"document_ids": r + ["invalid_id"]},
- ],
- )
- def test_stop_parse_partial_invalid_document_id(self, HttpApiAuth, add_documents_func, payload):
- dataset_id, document_ids = add_documents_func
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
-
- if callable(payload):
- payload = payload(document_ids)
- res = stop_parse_documents(HttpApiAuth, dataset_id, payload)
- assert res["code"] == 102
- assert res["message"] == "you don't own the document invalid_id"
-
- validate_document_parse_cancel(HttpApiAuth, dataset_id, document_ids)
-
- @pytest.mark.p3
- def test_repeated_stop_parse(self, HttpApiAuth, add_documents_func):
- dataset_id, document_ids = add_documents_func
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- res = stop_parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0
-
- res = stop_parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 102
- assert res["message"] == "Can't stop parsing document that has not started or already completed"
-
- @pytest.mark.p3
- def test_duplicate_stop_parse(self, HttpApiAuth, add_documents_func):
- dataset_id, document_ids = add_documents_func
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- res = stop_parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids + document_ids})
- assert res["code"] == 0
- assert res["data"]["success_count"] == 3
- assert f"Duplicate document ids: {document_ids[0]}" in res["data"]["errors"]
-
-
[email protected](reason="unstable")
-def test_stop_parse_100_files(HttpApiAuth, add_dataset_func, tmp_path):
- document_num = 100
- dataset_id = add_dataset_func
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, document_num, tmp_path)
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- sleep(1)
- res = stop_parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0
- validate_document_parse_cancel(HttpApiAuth, dataset_id, document_ids)
-
-
[email protected](reason="unstable")
-def test_concurrent_parse(HttpApiAuth, add_dataset_func, tmp_path):
- document_num = 50
- dataset_id = add_dataset_func
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, document_num, tmp_path)
- parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- stop_parse_documents,
- HttpApiAuth,
- dataset_id,
- {"document_ids": document_ids[i : i + 1]},
- )
- for i in range(document_num)
- ]
- responses = [f.result() for f in futures]
- assert all(r["code"] == 0 for r in responses)
- validate_document_parse_cancel(HttpApiAuth, dataset_id, document_ids)
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_update_document.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_update_document.py
deleted file mode 100644
index 453367cc81..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_update_document.py
+++ /dev/null
@@ -1,614 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import pytest
-from common import list_documents, update_document
-from configs import DOCUMENT_NAME_LIMIT, INVALID_API_TOKEN, INVALID_ID_32
-from libs.auth import RAGFlowHttpApiAuth
-from configs import DEFAULT_PARSER_CONFIG
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = update_document(invalid_auth, "dataset_id", "document_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestDocumentsUpdated:
- # GET /api/v1/datasets/<dataset_id>/documents no longer support find by id/name
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "name, expected_code, expected_message",
- [
- ("new_name.txt", 0, ""),
- (
- f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt",
- 0,
- "",
- ),
- (
- 0,
- 102,
- "Field: <name> - Message: <Input should be a valid string> - Value: <0>",
- ),
- (
- None,
- 100,
- "AttributeError('NoneType' object has no attribute 'encode')",
- ),
- (
- "",
- 101,
- "The extension of file can't be changed",
- ),
- (
- "ragflow_test_upload_0",
- 101,
- "The extension of file can't be changed",
- ),
- (
- "ragflow_test_upload_1.txt",
- 102,
- "Duplicated document name in the same dataset.",
- ),
- (
- "RAGFLOW_TEST_UPLOAD_1.TXT",
- 0,
- "",
- ),
- ],
- )
- def test_name(self, HttpApiAuth, add_documents, name, expected_code, expected_message):
- dataset_id, document_ids = add_documents
- res = update_document(HttpApiAuth, dataset_id, document_ids[0], {"name": name})
- assert res["code"] == expected_code
- if expected_code == 0:
- res = list_documents(HttpApiAuth, dataset_id, {"id": document_ids[0]})
- assert res["data"]["docs"][0]["name"] == name
- else:
- assert res["message"] == expected_message
-
- # GET /api/v1/datasets/<dataset_id>/documents no longer support find by id/name
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- "the dataset doesn't own the document",
- ),
- ],
- )
- def test_invalid_document_id(self, HttpApiAuth, add_documents, document_id, expected_code, expected_message):
- dataset_id, _ = add_documents
- res = update_document(HttpApiAuth, dataset_id, document_id, {"name": "new_name.txt"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "dataset_id, expected_code, expected_message",
- [
- (
- INVALID_ID_32,
- 102,
- "you don't own the dataset",
- ),
- ],
- )
- def test_invalid_dataset_id(self, HttpApiAuth, add_documents, dataset_id, expected_code, expected_message):
- _, document_ids = add_documents
- res = update_document(HttpApiAuth, dataset_id, document_ids[0], {"name": "new_name.txt"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "meta_fields, expected_code, expected_message",
- [
- # Valid meta_fields
- ({"test": "test"}, 0, ""),
- # Valid meta_fields with various types
- ({"author": "alice", "year": 2024}, 0, ""),
- ({"tags": ["tag1", "tag2"]}, 0, ""),
- ({"count": 42, "price": 19.99}, 0, ""),
- # Invalid type - string instead of dict
- ("test", 102, "Field: <meta_fields> - Message: <Input should be a valid dictionary> - Value: <test>"),
- # Invalid type - list instead of dict
- ([], 102, "Field: <meta_fields> - Message: <Input should be a valid dictionary> - Value: <[]>"),
- # Invalid - list containing objects (unsupported type in list)
- ({"tags": [{"x": {"a": "b"}}]}, 102, "Field: <meta_fields> - Message: <The type is not supported in list: [{'x': {'a': 'b'}}]> - Value: <{'tags': [{'x': {'a': 'b'}}]}>"),
- ({"tags": [{"x": 1}]}, 102, "Field: <meta_fields> - Message: <The type is not supported in list: [{'x': 1}]> - Value: <{'tags': [{'x': 1}]}>"),
- # Invalid - nested object with unsupported type
- ({"obj": {"x": 1}}, 102, "Field: <meta_fields> - Message: <The type is not supported: {'x': 1}> - Value: <{'obj': {'x': 1}}>"),
- # Valid types of list
- ({"tags": [2, 1]}, 0, ""),
- ],
- )
- def test_meta_fields(self, HttpApiAuth, add_documents, meta_fields, expected_code, expected_message):
- dataset_id, document_ids = add_documents
- res = update_document(HttpApiAuth, dataset_id, document_ids[0], {"meta_fields": meta_fields})
- if expected_code == 0:
- res = list_documents(HttpApiAuth, dataset_id, {"id": document_ids[0]})
- assert res["data"]["docs"][0]["meta_fields"] == meta_fields
- else:
- assert expected_message in res["message"] or res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "meta_fields, expected_code, expected_message",
- [
- # Test with invalid document ID (not owned by dataset)
- ({"author": "alice"}, 102, "the dataset doesn't own the document"),
- ],
- )
- def test_meta_fields_invalid_document(self, HttpApiAuth, add_documents, meta_fields, expected_code, expected_message):
- """Test meta_fields update with invalid document ID"""
- dataset_id, _ = add_documents
- res = update_document(HttpApiAuth, dataset_id, "invalid_doc_id_12345678901234567890", {"meta_fields": meta_fields})
- assert res["code"] == expected_code
- assert expected_message in res["message"]
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "chunk_method, expected_code, expected_message",
- [
- ("naive", 0, ""),
- ("manual", 0, ""),
- ("qa", 0, ""),
- ("table", 0, ""),
- ("paper", 0, ""),
- ("book", 0, ""),
- ("laws", 0, ""),
- ("presentation", 0, ""),
- ("picture", 0, ""),
- ("one", 0, ""),
- ("knowledge_graph", 0, ""),
- ("email", 0, ""),
- ("tag", 0, ""),
- ("", 102, "`chunk_method` (empty string) is not valid"),
- (
- "other_chunk_method",
- 102,
- "Field: <chunk_method> - Message: <`chunk_method` other_chunk_method doesn't exist> - Value: <other_chunk_method>",
- ),
- ],
- )
- def test_chunk_method(self, HttpApiAuth, add_documents, chunk_method, expected_code, expected_message):
- dataset_id, document_ids = add_documents
- res = update_document(HttpApiAuth, dataset_id, document_ids[0], {"chunk_method": chunk_method})
- assert res["code"] == expected_code
- if expected_code == 0:
- res = list_documents(HttpApiAuth, dataset_id, {"id": document_ids[0]})
- doc_of_id = res["data"]["docs"][0]
- if chunk_method == "":
- assert doc_of_id["chunk_method"] == "naive"
- else:
- print(f"doc:{doc_of_id}")
- assert doc_of_id["chunk_method"] == chunk_method
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"chunk_count": 1}, 102, "can't change `chunk_count`"),
- pytest.param(
- {"create_date": "Fri, 14 Mar 2025 16:53:42 GMT"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"create_time": 1},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"created_by": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"dataset_id": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"id": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"location": "ragflow_test.txt"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"process_begin_at": 1},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"process_duration": 1.0},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param({"progress": 1.0}, 102, "can't change `progress`"),
- pytest.param(
- {"progress_msg": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"run": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"size": 1},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"source_type": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"thumbnail": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ({"token_count": 1}, 102, "can't change `token_count`"),
- pytest.param(
- {"type": "ragflow_test"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"update_date": "Fri, 14 Mar 2025 16:33:17 GMT"},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"update_time": 1},
- 102,
- "The input parameters are invalid.",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ],
- )
- def test_invalid_field(
- self,
- HttpApiAuth,
- add_documents,
- payload,
- expected_code,
- expected_message,
- ):
- dataset_id, document_ids = add_documents
- res = update_document(HttpApiAuth, dataset_id, document_ids[0], payload)
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"chunk_count": 100}, 102, "can't change `chunk_count`"),
- ({"token_count": 100}, 102, "can't change `token_count`"),
- ({"progress": 2.0}, 102, "Field: <progress> - Message: <Input should be less than or equal to 1> - Value: <2.0>"),
- ({"progress": 1.0}, 102, "can't change `progress`"),
- ({"meta_fields": []}, 102, "Field: <meta_fields> - Message: <Input should be a valid dictionary> - Value: <[]>"),
- ],
- )
- def test_update_doc_guards_and_error_paths(self, HttpApiAuth, add_documents, payload, expected_code, expected_message):
- """
- Test various guard conditions and error paths for document update functionality.
- This includes testing for invalid dataset ownership, document ownership,
- immutable fields, and validation errors.
- """
- dataset_id, document_ids = add_documents
- document_id = document_ids[0]
-
- res = update_document(HttpApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code
- if expected_message:
- assert expected_message in res["message"] or res["message"] == expected_message
-
-
-DEFAULT_PARSER_CONFIG_FOR_TEST = {
- "layout_recognize": "DeepDOC",
- "chunk_token_num": 512,
- "delimiter": "\n",
- "auto_keywords": 0,
- "auto_questions": 0,
- "html4excel": False,
- "topn_tags": 3,
- "raptor": {
- "use_raptor": True,
- "prompt": "Summarize the paragraphs below without inventing facts or changing numbers.\nOutput exactly two parts in the same language as the source:\n1. First line: a concise title only.\n2. Following lines: a concise summary of the content.\nDo not output labels, Markdown headings, bullet points, or any other commentary.\n\nParagraphs:\n{cluster_content}",
- "max_token": 512,
- "clustering_threshold": 0.3,
- "clustering_ratio": 0.5,
- "max_cluster": 64,
- "random_seed": 0,
- },
- "graphrag": {
- "use_graphrag": True,
- "entity_types": [
- "organization",
- "person",
- "geo",
- "event",
- "category",
- ],
- "method": "light",
- "batch_chunk_token_size": 4096,
- },
-}
-
-
-class TestUpdateDocumentParserConfig:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "chunk_method, parser_config, expected_code, expected_message",
- [
- ("naive", {}, 0, ""),
- (
- "naive",
- DEFAULT_PARSER_CONFIG_FOR_TEST,
- 0,
- "",
- ),
- pytest.param(
- "naive",
- {"chunk_token_num": -1},
- 102,
- "Field: <parser_config.chunk_token_num> - Message: <Input should be greater than or equal to 1> - Value: <-1>",
- ),
- pytest.param(
- "naive",
- {"chunk_token_num": 0},
- 102,
- "Field: <parser_config.chunk_token_num> - Message: <Input should be greater than or equal to 1> - Value: <0>",
- ),
- pytest.param(
- "naive",
- {"chunk_token_num": 100000000},
- 102,
- "Field: <parser_config.chunk_token_num> - Message: <Input should be less than or equal to 2048> - Value: <100000000>",
- ),
- pytest.param(
- "naive",
- {"chunk_token_num": 3.14},
- 102,
- "Field: <parser_config.chunk_token_num> - Message: <Input should be a valid integer> - Value: <3.14>",
- ),
- pytest.param(
- "naive",
- {"chunk_token_num": "1024"},
- 102,
- "Field: <parser_config.chunk_token_num> - Message: <Input should be a valid integer> - Value: <1024>",
- ),
- (
- "naive",
- {"layout_recognize": "DeepDOC"},
- 0,
- "",
- ),
- (
- "naive",
- {"layout_recognize": "Naive"},
- 0,
- "",
- ),
- ("naive", {"html4excel": True}, 0, ""),
- ("naive", {"html4excel": False}, 0, ""),
- pytest.param(
- "naive",
- {"html4excel": 1},
- 102,
- "Field: <parser_config.html4excel> - Message: <Input should be a valid boolean> - Value: <1>",
- ),
- ("naive", {"delimiter": ""}, 102, "Field: <parser_config.delimiter> - Message: <String should have at least 1 character> - Value: <>"),
- ("naive", {"delimiter": "`##`"}, 0, ""),
- pytest.param(
- "naive",
- {"delimiter": 1},
- 102,
- "Field: <parser_config.delimiter> - Message: <Input should be a valid string> - Value: <1>",
- ),
- pytest.param(
- "naive",
- {"task_page_size": -1},
- 102,
- "Field: <parser_config.task_page_size> - Message: <Input should be greater than or equal to 1> - Value: <-1>",
- ),
- pytest.param(
- "naive",
- {"task_page_size": 0},
- 102,
- "Field: <parser_config.task_page_size> - Message: <Input should be greater than or equal to 1> - Value: <0>",
- ),
- pytest.param(
- "naive",
- {"task_page_size": 100000000},
- 0,
- "",
- ),
- pytest.param(
- "naive",
- {"task_page_size": 3.14},
- 102,
- "Field: <parser_config.task_page_size> - Message: <Input should be a valid integer> - Value: <3.14>",
- ),
- pytest.param(
- "naive",
- {"task_page_size": "1024"},
- 102,
- "Field: <parser_config.task_page_size> - Message: <Input should be a valid integer> - Value: <1024>",
- ),
- (
- "naive",
- {
- "raptor": {
- "use_raptor": {"a": "b"},
- }
- },
- 102,
- "Field: <parser_config.raptor.use_raptor> - Message: <Input should be a valid boolean> - Value: <{'a': 'b'}>",
- ),
- ("naive", {"raptor": {"use_raptor": False}}, 0, ""),
- pytest.param(
- "naive",
- {"invalid_key": "invalid_value"},
- 0,
- "",
- ),
- pytest.param(
- "naive",
- {"auto_keywords": -1},
- 102,
- "Field: <parser_config.auto_keywords> - Message: <Input should be greater than or equal to 0> - Value: <-1>",
- ),
- pytest.param(
- "naive",
- {"auto_keywords": 32},
- 0,
- "",
- ),
- pytest.param(
- "naive",
- {"auto_keywords": "1024"},
- 102,
- "Field: <parser_config.auto_keywords> - Message: <Input should be a valid integer> - Value: <1024>",
- ),
- pytest.param(
- "naive",
- {"auto_keywords": 3.14},
- 102,
- "Field: <parser_config.auto_keywords> - Message: <Input should be a valid integer> - Value: <3.14>",
- ),
- pytest.param(
- "naive",
- {"auto_questions": -1},
- 102,
- "Field: <parser_config.auto_questions> - Message: <Input should be greater than or equal to 0> - Value: <-1>",
- ),
- pytest.param(
- "naive",
- {"auto_questions": 10},
- 0,
- "",
- ),
- pytest.param(
- "naive",
- {"auto_questions": 3.14},
- 102,
- "Field: <parser_config.auto_questions> - Message: <Input should be a valid integer> - Value: <3.14>",
- ),
- pytest.param(
- "naive",
- {"auto_questions": "1024"},
- 102,
- "Field: <parser_config.auto_questions> - Message: <Input should be a valid integer> - Value: <1024>",
- ),
- pytest.param(
- "naive",
- {"topn_tags": -1},
- 102,
- "Field: <parser_config.topn_tags> - Message: <Input should be greater than or equal to 1> - Value: <-1>",
- ),
- pytest.param(
- "naive",
- {"topn_tags": 10},
- 0,
- "",
- ),
- pytest.param(
- "naive",
- {"topn_tags": 3.14},
- 102,
- "Field: <parser_config.topn_tags> - Message: <Input should be a valid integer> - Value: <3.14>",
- ),
- pytest.param(
- "naive",
- {"topn_tags": "1024"},
- 102,
- "Field: <parser_config.topn_tags> - Message: <Input should be a valid integer> - Value: <1024>",
- ),
- ],
- )
- def test_parser_config(
- self,
- HttpApiAuth,
- add_documents,
- chunk_method,
- parser_config,
- expected_code,
- expected_message,
- ):
- dataset_id, document_ids = add_documents
- res = update_document(
- HttpApiAuth,
- dataset_id,
- document_ids[0],
- {"chunk_method": chunk_method, "parser_config": parser_config},
- )
- assert res["code"] == expected_code
- if expected_code == 0:
- res = list_documents(HttpApiAuth, dataset_id, {"id": document_ids[0]})
-
- doc_of_id = res["data"]["docs"][0]
- if parser_config == {}:
- assert doc_of_id["parser_config"] == DEFAULT_PARSER_CONFIG
- else:
- for k, v in parser_config.items():
- assert doc_of_id["parser_config"][k] == v
- if expected_code != 0 or expected_message:
- assert res["message"] == expected_message
diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_upload_documents.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_upload_documents.py
deleted file mode 100644
index 0b21dd6db0..0000000000
--- a/test/testcases/test_http_api/test_file_management_within_dataset/test_upload_documents.py
+++ /dev/null
@@ -1,219 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import string
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-import requests
-from common import FILE_API_URL, list_datasets, upload_documents
-from configs import DOCUMENT_NAME_LIMIT, HOST_ADDRESS, INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from requests_toolbelt import MultipartEncoder
-from utils.file_utils import create_txt_file
-
-
[email protected]("clear_datasets")
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (
- RAGFlowHttpApiAuth(INVALID_API_TOKEN),
- 401,
- "<Unauthorized '401: Unauthorized'>",
- ),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = upload_documents(invalid_auth, "dataset_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestDocumentsUpload:
- @pytest.mark.p1
- def test_valid_single_upload(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- assert res["code"] == 0
- assert res["data"][0]["dataset_id"] == dataset_id
- assert res["data"][0]["name"] == fp.name
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "generate_test_files",
- [
- "docx",
- "excel",
- "ppt",
- "image",
- "pdf",
- "txt",
- "md",
- "json",
- "eml",
- "html",
- ],
- indirect=True,
- )
- def test_file_type_validation(self, HttpApiAuth, add_dataset_func, generate_test_files, request):
- dataset_id = add_dataset_func
- fp = generate_test_files[request.node.callspec.params["generate_test_files"]]
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- assert res["code"] == 0
- assert res["data"][0]["dataset_id"] == dataset_id
- assert res["data"][0]["name"] == fp.name
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "file_type",
- ["exe", "unknown"],
- )
- def test_unsupported_file_type(self, HttpApiAuth, add_dataset_func, tmp_path, file_type):
- dataset_id = add_dataset_func
- fp = tmp_path / f"ragflow_test.{file_type}"
- fp.touch()
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- assert res["code"] == 500
- assert res["message"] == f"ragflow_test.{file_type}: This type of file has not been supported yet!"
-
- @pytest.mark.p2
- def test_missing_file(self, HttpApiAuth, add_dataset_func):
- dataset_id = add_dataset_func
- res = upload_documents(HttpApiAuth, dataset_id)
- assert res["code"] == 101
- assert res["message"] == "No file part!"
-
- @pytest.mark.p3
- def test_empty_file(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- fp = tmp_path / "empty.txt"
- fp.touch()
-
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- assert res["code"] == 0
- assert res["data"][0]["size"] == 0
-
- @pytest.mark.p3
- def test_filename_empty(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
- with fp.open("rb") as file_obj:
- fields = (("file", ("", file_obj)),)
- m = MultipartEncoder(fields=fields)
- res = requests.post(
- url=url,
- headers={"Content-Type": m.content_type},
- auth=HttpApiAuth,
- data=m,
- )
- assert res.json()["code"] == 101
- assert res.json()["message"] == "No file selected!"
-
- @pytest.mark.p2
- def test_filename_max_length(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt")
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- assert res["code"] == 0
- assert res["data"][0]["name"] == fp.name
-
- @pytest.mark.p2
- def test_invalid_dataset_id(self, HttpApiAuth, tmp_path):
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(HttpApiAuth, "invalid_dataset_id", [fp])
- assert res["code"] == 102
- assert res["message"] == "Can't find the dataset with ID invalid_dataset_id!"
-
- @pytest.mark.p2
- def test_duplicate_files(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(HttpApiAuth, dataset_id, [fp, fp])
- assert res["code"] == 0
- assert len(res["data"]) == 2
- for i in range(len(res["data"])):
- assert res["data"][i]["dataset_id"] == dataset_id
- expected_name = fp.name
- if i != 0:
- expected_name = f"{fp.stem}({i}){fp.suffix}"
- assert res["data"][i]["name"] == expected_name
-
- @pytest.mark.p2
- def test_same_file_repeat(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- for i in range(3):
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- assert res["code"] == 0
- assert len(res["data"]) == 1
- assert res["data"][0]["dataset_id"] == dataset_id
- expected_name = fp.name
- if i != 0:
- expected_name = f"{fp.stem}({i}){fp.suffix}"
- assert res["data"][0]["name"] == expected_name
-
- @pytest.mark.p3
- def test_filename_special_characters(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- illegal_chars = '<>:"/\\|?*'
- translation_table = str.maketrans({char: "_" for char in illegal_chars})
- safe_filename = string.punctuation.translate(translation_table)
- fp = tmp_path / f"{safe_filename}.txt"
- fp.write_text("Sample text content")
-
- res = upload_documents(HttpApiAuth, dataset_id, [fp])
- assert res["code"] == 0
- assert len(res["data"]) == 1
- assert res["data"][0]["dataset_id"] == dataset_id
- assert res["data"][0]["name"] == fp.name
-
- @pytest.mark.p1
- def test_multiple_files(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
- expected_document_count = 20
- fps = []
- for i in range(expected_document_count):
- fp = create_txt_file(tmp_path / f"ragflow_test_{i}.txt")
- fps.append(fp)
- res = upload_documents(HttpApiAuth, dataset_id, fps)
- assert res["code"] == 0
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["data"][0]["document_count"] == expected_document_count
-
- @pytest.mark.p3
- def test_concurrent_upload(self, HttpApiAuth, add_dataset_func, tmp_path):
- dataset_id = add_dataset_func
-
- count = 20
- fps = []
- for i in range(count):
- fp = create_txt_file(tmp_path / f"ragflow_test_{i}.txt")
- fps.append(fp)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(upload_documents, HttpApiAuth, dataset_id, [fp]) for fp in fps]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- res = list_datasets(HttpApiAuth, {"id": dataset_id})
- assert res["data"][0]["document_count"] == count
diff --git a/test/testcases/test_http_api/test_router_errors.py b/test/testcases/test_http_api/test_router_errors.py
deleted file mode 100644
index 98007d4e52..0000000000
--- a/test/testcases/test_http_api/test_router_errors.py
+++ /dev/null
@@ -1,29 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-import requests
-
-from configs import HOST_ADDRESS, VERSION
-
-
-def test_route_not_found_returns_json():
- url = f"{HOST_ADDRESS}/api/{VERSION}/__missing_route__"
- res = requests.get(url)
- assert res.status_code == 404
- payload = res.json()
- assert payload["error"] == "Not Found"
- assert f"/api/{VERSION}/__missing_route__" in payload["message"]
diff --git a/test/testcases/test_http_api/test_session_management/conftest.py b/test/testcases/test_http_api/test_session_management/conftest.py
deleted file mode 100644
index 3bae723954..0000000000
--- a/test/testcases/test_http_api/test_session_management/conftest.py
+++ /dev/null
@@ -1,41 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import batch_add_sessions_with_chat_assistant, delete_all_sessions_with_chat_assistant
-
-
[email protected](scope="class")
-def add_sessions_with_chat_assistant(request, HttpApiAuth, add_chat_assistants):
- def cleanup():
- for chat_assistant_id in chat_assistant_ids:
- delete_all_sessions_with_chat_assistant(HttpApiAuth, chat_assistant_id)
-
- request.addfinalizer(cleanup)
-
- _, _, chat_assistant_ids = add_chat_assistants
- return chat_assistant_ids[0], batch_add_sessions_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], 5)
-
-
[email protected](scope="function")
-def add_sessions_with_chat_assistant_func(request, HttpApiAuth, add_chat_assistants):
- def cleanup():
- for chat_assistant_id in chat_assistant_ids:
- delete_all_sessions_with_chat_assistant(HttpApiAuth, chat_assistant_id)
-
- request.addfinalizer(cleanup)
-
- _, _, chat_assistant_ids = add_chat_assistants
- return chat_assistant_ids[0], batch_add_sessions_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], 5)
diff --git a/test/testcases/test_http_api/test_session_management/test_agent_completions.py b/test/testcases/test_http_api/test_session_management/test_agent_completions.py
deleted file mode 100644
index 2006bfde66..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_agent_completions.py
+++ /dev/null
@@ -1,105 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import (
- agent_completions,
- create_agent,
- create_agent_session,
- delete_agent,
- delete_all_agent_sessions,
- list_agents,
-)
-
-AGENT_TITLE = "test_agent_http"
-MINIMAL_DSL = {
- "components": {
- "begin": {
- "obj": {"component_name": "Begin", "params": {}},
- "downstream": ["message"],
- "upstream": [],
- },
- "message": {
- "obj": {"component_name": "Message", "params": {"content": ["{sys.query}"]}},
- "downstream": [],
- "upstream": ["begin"],
- },
- },
- "history": [],
- "retrieval": [],
- "path": [],
- "globals": {
- "sys.query": "",
- "sys.user_id": "",
- "sys.conversation_turns": 0,
- "sys.files": [],
- },
- "variables": {},
-}
-
-
-def _agent_items(res):
- data = res.get("data", [])
- if isinstance(data, dict):
- return data.get("canvas", [])
- return data
-
-
[email protected](scope="function")
-def agent_id(HttpApiAuth, request):
- res = list_agents(HttpApiAuth, {"page_size": 100})
- assert res["code"] == 0, res
- for agent in _agent_items(res):
- if agent.get("title") == AGENT_TITLE:
- delete_agent(HttpApiAuth, agent["id"])
-
- res = create_agent(HttpApiAuth, {"title": AGENT_TITLE, "dsl": MINIMAL_DSL})
- assert res["code"] == 0, res
- res = list_agents(HttpApiAuth, {"title": AGENT_TITLE})
- assert res["code"] == 0, res
- agents = _agent_items(res)
- assert agents, res
- agent_id = agents[0]["id"]
-
- def cleanup():
- delete_all_agent_sessions(HttpApiAuth, agent_id)
- delete_agent(HttpApiAuth, agent_id)
-
- request.addfinalizer(cleanup)
- return agent_id
-
-
-class TestAgentCompletions:
- @pytest.mark.p2
- def test_agent_completion_stream_false(self, HttpApiAuth, agent_id):
- res = create_agent_session(HttpApiAuth, agent_id, payload={})
- assert res["code"] == 0, res
- session_id = res["data"]["id"]
-
- res = agent_completions(
- HttpApiAuth,
- agent_id,
- {"query": "hello", "stream": False, "session_id": session_id},
- )
- assert res["code"] == 0, res
- if isinstance(res["data"], dict):
- assert isinstance(res["data"].get("data"), dict), res
- content = res["data"]["data"].get("content", "")
- assert content, res
- assert "hello" in content, res
- assert res["data"].get("session_id") == session_id, res
- else:
- assert isinstance(res["data"], str), res
- assert res["data"].startswith("**ERROR**"), res
diff --git a/test/testcases/test_http_api/test_session_management/test_agent_sessions.py b/test/testcases/test_http_api/test_session_management/test_agent_sessions.py
deleted file mode 100644
index 4b8bf5e389..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_agent_sessions.py
+++ /dev/null
@@ -1,115 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-import requests
-from common import (
- create_agent,
- delete_agent,
- delete_all_agent_sessions,
- list_agents,
-)
-from configs import HOST_ADDRESS, VERSION
-
-AGENT_TITLE = "test_agent_http"
-MINIMAL_DSL = {
- "components": {
- "begin": {
- "obj": {"component_name": "Begin", "params": {}},
- "downstream": ["message"],
- "upstream": [],
- },
- "message": {
- "obj": {"component_name": "Message", "params": {"content": ["{sys.query}"]}},
- "downstream": [],
- "upstream": ["begin"],
- },
- },
- "history": [],
- "retrieval": [],
- "path": [],
- "globals": {
- "sys.query": "",
- "sys.user_id": "",
- "sys.conversation_turns": 0,
- "sys.files": [],
- },
- "variables": {},
-}
-
-
-def _agent_items(res):
- data = res.get("data", [])
- if isinstance(data, dict):
- return data.get("canvas", [])
- return data
-
-
[email protected](scope="function")
-def agent_id(HttpApiAuth, request):
- res = list_agents(HttpApiAuth, {"page_size": 100})
- assert res["code"] == 0, res
- for agent in _agent_items(res):
- if agent.get("title") == AGENT_TITLE:
- delete_agent(HttpApiAuth, agent["id"])
-
- res = create_agent(HttpApiAuth, {"title": AGENT_TITLE, "dsl": MINIMAL_DSL})
- assert res["code"] == 0, res
- res = list_agents(HttpApiAuth, {"title": AGENT_TITLE})
- assert res["code"] == 0, res
- agents = _agent_items(res)
- assert agents, res
- agent_id = agents[0]["id"]
-
- def cleanup():
- delete_all_agent_sessions(HttpApiAuth, agent_id)
- delete_agent(HttpApiAuth, agent_id)
-
- request.addfinalizer(cleanup)
- return agent_id
-
-
-class TestAgentSessions:
- @pytest.mark.p2
- def test_agent_crud_validation_contract(self, HttpApiAuth, agent_id):
- res = list_agents(HttpApiAuth, {"id": "missing-agent-id", "title": "missing-agent-title"})
- assert res["code"] == 0, res
- assert isinstance(res.get("data"), dict), res
- assert "canvas" in res["data"], res
- assert "total" in res["data"], res
-
- res = list_agents(HttpApiAuth, {"title": AGENT_TITLE, "desc": "true", "page_size": 1})
- assert res["code"] == 0, res
-
- res = create_agent(HttpApiAuth, {"title": "missing-dsl-agent"})
- assert res["code"] == 101, res
- assert "No DSL data in request" in res["message"], res
-
- res = create_agent(HttpApiAuth, {"dsl": MINIMAL_DSL})
- assert res["code"] == 101, res
- assert "No title in request" in res["message"], res
-
- res = create_agent(HttpApiAuth, {"title": AGENT_TITLE, "dsl": MINIMAL_DSL})
- assert res["code"] == 102, res
- assert "already exists" in res["message"], res
-
- update_url = f"{HOST_ADDRESS}/api/{VERSION}/agents/invalid-agent-id"
- res = requests.put(update_url, auth=HttpApiAuth, json={"title": "updated", "dsl": MINIMAL_DSL}).json()
- assert res["code"] == 103, res
- assert "Make sure you have permission to access the agent." in res["message"], res
-
- res = delete_agent(HttpApiAuth, "invalid-agent-id")
- assert res["code"] == 103, res
- assert "Only the owner of the agent is authorized for this operation." in res["message"], res
diff --git a/test/testcases/test_http_api/test_session_management/test_chat_completions.py b/test/testcases/test_http_api/test_session_management/test_chat_completions.py
deleted file mode 100644
index 2582f503dc..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_chat_completions.py
+++ /dev/null
@@ -1,142 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import (
- bulk_upload_documents,
- chat_completions,
- create_chat_assistant,
- create_session_with_chat_assistant,
- delete_all_chat_assistants,
- delete_all_sessions_with_chat_assistant,
- list_documents,
- parse_documents,
-)
-from utils import wait_for
-
-
-@wait_for(200, 1, "Document parsing timeout")
-def _parse_done(auth, dataset_id, document_ids=None):
- res = list_documents(auth, dataset_id)
- target_docs = res["data"]["docs"]
- if document_ids is None:
- return all(doc.get("run") == "DONE" for doc in target_docs)
- target_ids = set(document_ids)
- for doc in target_docs:
- if doc.get("id") in target_ids and doc.get("run") != "DONE":
- return False
- return True
-
-
-class TestChatCompletions:
- @pytest.mark.p3
- def test_chat_completion_stream_false_with_session(self, HttpApiAuth, add_dataset_func, tmp_path, request):
- dataset_id = add_dataset_func
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0, res
- _parse_done(HttpApiAuth, dataset_id, document_ids)
-
- res = create_chat_assistant(HttpApiAuth, {"name": "chat_completion_test", "dataset_ids": [dataset_id]})
- assert res["code"] == 0, res
- chat_id = res["data"]["id"]
- request.addfinalizer(lambda: delete_all_chat_assistants(HttpApiAuth))
- request.addfinalizer(lambda: delete_all_sessions_with_chat_assistant(HttpApiAuth, chat_id))
-
- res = create_session_with_chat_assistant(HttpApiAuth, chat_id, {"name": "session_for_completion"})
- assert res["code"] == 0, res
- session_id = res["data"]["id"]
-
- res = chat_completions(
- HttpApiAuth,
- chat_id,
- {
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- "session_id": session_id,
- },
- )
- assert res["code"] == 0, res
- assert isinstance(res["data"], dict), res
- for key in ["answer", "reference", "audio_binary", "id", "session_id"]:
- assert key in res["data"], res
- assert res["data"]["session_id"] == session_id, res
-
- @pytest.mark.p2
- def test_chat_completion_invalid_chat(self, HttpApiAuth):
- res = chat_completions(
- HttpApiAuth,
- "invalid_chat_id",
- {
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- "session_id": "invalid_session",
- },
- )
- assert res["code"] == 109, res
- assert "no authorization" in res.get("message", ""), res
-
- @pytest.mark.p2
- def test_chat_completion_invalid_session(self, HttpApiAuth, request):
- res = create_chat_assistant(HttpApiAuth, {"name": "chat_completion_invalid_session", "dataset_ids": []})
- assert res["code"] == 0, res
- chat_id = res["data"]["id"]
- request.addfinalizer(lambda: delete_all_chat_assistants(HttpApiAuth))
- request.addfinalizer(lambda: delete_all_sessions_with_chat_assistant(HttpApiAuth, chat_id))
-
- res = chat_completions(
- HttpApiAuth,
- chat_id,
- {
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- "session_id": "invalid_session",
- },
- )
- assert res["code"] == 102, res
- assert "Session not found!" in res.get("message", ""), res
-
- @pytest.mark.p2
- def test_chat_completion_stream_false_with_chat_without_session(self, HttpApiAuth, request):
- res = create_chat_assistant(HttpApiAuth, {"name": "chat_completion_invalid_meta", "dataset_ids": []})
- assert res["code"] == 0, res
- chat_id = res["data"]["id"]
- request.addfinalizer(lambda: delete_all_chat_assistants(HttpApiAuth))
- request.addfinalizer(lambda: delete_all_sessions_with_chat_assistant(HttpApiAuth, chat_id))
-
- res = chat_completions(
- HttpApiAuth,
- chat_id,
- {
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- },
- )
- assert res["code"] == 0, res
- assert res["data"]["session_id"], res
-
- @pytest.mark.p2
- def test_chat_completion_stream_false_without_chat(self, HttpApiAuth):
- res = chat_completions(
- HttpApiAuth,
- None,
- {
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- },
- )
- assert res["code"] == 0, res
- assert isinstance(res["data"], dict), res
- assert "answer" in res["data"], res
diff --git a/test/testcases/test_http_api/test_session_management/test_chat_completions_openai.py b/test/testcases/test_http_api/test_session_management/test_chat_completions_openai.py
deleted file mode 100644
index b45ae2562d..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_chat_completions_openai.py
+++ /dev/null
@@ -1,208 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import (
- bulk_upload_documents,
- chat_completions_openai,
- create_chat_assistant,
- delete_all_chat_assistants,
- list_documents,
- parse_documents,
-)
-from utils import wait_for
-
-
-@wait_for(200, 1, "Document parsing timeout")
-def _parse_done(auth, dataset_id, document_ids=None):
- res = list_documents(auth, dataset_id)
- target_docs = res["data"]["docs"]
- if document_ids is None:
- return all(doc.get("run") == "DONE" for doc in target_docs)
- target_ids = set(document_ids)
- for doc in target_docs:
- if doc.get("id") in target_ids and doc.get("run") != "DONE":
- return False
- return True
-
-
-class TestChatCompletionsOpenAI:
- """Test cases for the OpenAI-compatible chat completions endpoint"""
-
- @pytest.mark.p2
- def test_openai_chat_completion_non_stream(self, HttpApiAuth, add_dataset_func, tmp_path, request):
- """Test OpenAI-compatible endpoint returns proper response with token usage"""
- dataset_id = add_dataset_func
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0, res
- _parse_done(HttpApiAuth, dataset_id, document_ids)
-
- res = create_chat_assistant(HttpApiAuth, {"name": "openai_endpoint_test", "dataset_ids": [dataset_id]})
- assert res["code"] == 0, res
- chat_id = res["data"]["id"]
- request.addfinalizer(lambda: delete_all_chat_assistants(HttpApiAuth))
-
- res = chat_completions_openai(
- HttpApiAuth,
- chat_id,
- {
- "model": "model", # Legacy placeholder keeps using the chat assistant's configured model
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- },
- )
-
- # Verify OpenAI-compatible response structure
- assert "choices" in res, f"Response should contain 'choices': {res}"
- assert len(res["choices"]) > 0, f"'choices' should not be empty: {res}"
- assert "message" in res["choices"][0], f"Choice should contain 'message': {res}"
- assert "content" in res["choices"][0]["message"], f"Message should contain 'content': {res}"
-
- # Verify token usage is present and uses actual token counts (not character counts)
- assert "usage" in res, f"Response should contain 'usage': {res}"
- usage = res["usage"]
- assert "prompt_tokens" in usage, f"'usage' should contain 'prompt_tokens': {usage}"
- assert "completion_tokens" in usage, f"'usage' should contain 'completion_tokens': {usage}"
- assert "total_tokens" in usage, f"'usage' should contain 'total_tokens': {usage}"
- assert usage["total_tokens"] == usage["prompt_tokens"] + usage["completion_tokens"], f"total_tokens should equal prompt_tokens + completion_tokens: {usage}"
-
- @pytest.mark.p2
- def test_openai_chat_completion_token_count_reasonable(self, HttpApiAuth, add_dataset_func, tmp_path, request):
- """Test that token counts are reasonable (using tiktoken, not character counts)"""
- dataset_id = add_dataset_func
- document_ids = bulk_upload_documents(HttpApiAuth, dataset_id, 1, tmp_path)
- res = parse_documents(HttpApiAuth, dataset_id, {"document_ids": document_ids})
- assert res["code"] == 0, res
- _parse_done(HttpApiAuth, dataset_id, document_ids)
-
- res = create_chat_assistant(HttpApiAuth, {"name": "openai_token_count_test", "dataset_ids": [dataset_id]})
- assert res["code"] == 0, res
- chat_id = res["data"]["id"]
- request.addfinalizer(lambda: delete_all_chat_assistants(HttpApiAuth))
-
- # Use a message with known token count
- # "hello" is 1 token in cl100k_base encoding
- res = chat_completions_openai(
- HttpApiAuth,
- chat_id,
- {
- "model": "model", # Legacy placeholder keeps using the chat assistant's configured model
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- },
- )
-
- assert "usage" in res, f"Response should contain 'usage': {res}"
- usage = res["usage"]
-
- # The prompt tokens should be reasonable for the message "hello" plus any system context
- # If using len() instead of tiktoken, a short response could have equal or fewer tokens
- # than characters, which would be incorrect
- # With tiktoken, "hello" = 1 token, so prompt_tokens should include that plus context
- assert usage["prompt_tokens"] > 0, f"prompt_tokens should be greater than 0: {usage}"
- assert usage["completion_tokens"] > 0, f"completion_tokens should be greater than 0: {usage}"
-
- @pytest.mark.p2
- def test_openai_chat_completion_invalid_chat(self, HttpApiAuth):
- """Test OpenAI endpoint returns error for invalid chat ID"""
- res = chat_completions_openai(
- HttpApiAuth,
- "invalid_chat_id",
- {
- "model": "model", # Legacy placeholder keeps using the chat assistant's configured model
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- },
- )
- # Should return an error (format may vary based on implementation)
- assert "error" in res or res.get("code") != 0, f"Should return error for invalid chat: {res}"
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, requires_valid_chat, expected_message",
- [
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": "invalid_extra_body",
- },
- False,
- "extra_body must be an object.",
- ),
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": {"reference_metadata": "invalid_reference_metadata"},
- },
- False,
- "reference_metadata must be an object.",
- ),
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": {"reference_metadata": {"fields": "author"}},
- },
- False,
- "reference_metadata.fields must be an array.",
- ),
- (
- {
- "model": "model",
- "messages": [],
- },
- False,
- "You have to provide messages.",
- ),
- (
- {
- "model": "model",
- "messages": [{"role": "assistant", "content": "hello"}],
- },
- False,
- "The last content of this conversation is not from user.",
- ),
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": {"metadata_condition": "invalid"},
- },
- True,
- "metadata_condition must be an object.",
- ),
- ],
- )
- def test_openai_chat_completion_request_validation(
- self,
- HttpApiAuth,
- request,
- payload,
- requires_valid_chat,
- expected_message,
- ):
- chat_id = "invalid_chat_id"
- if requires_valid_chat:
- res = create_chat_assistant(HttpApiAuth, {"name": "openai_validation_case", "dataset_ids": []})
- assert res["code"] == 0, res
- chat_id = res["data"]["id"]
- request.addfinalizer(lambda: delete_all_chat_assistants(HttpApiAuth))
-
- res = chat_completions_openai(HttpApiAuth, chat_id, payload)
- assert res.get("code") != 0, res
- assert expected_message in res.get("message", ""), res
diff --git a/test/testcases/test_http_api/test_session_management/test_create_session_with_chat_assistant.py b/test/testcases/test_http_api/test_session_management/test_create_session_with_chat_assistant.py
deleted file mode 100644
index e80566bfbe..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_create_session_with_chat_assistant.py
+++ /dev/null
@@ -1,115 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import create_session_with_chat_assistant, delete_chat_assistants, list_session_with_chat_assistants
-from configs import INVALID_API_TOKEN, SESSION_WITH_CHAT_NAME_LIMIT
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = create_session_with_chat_assistant(invalid_auth, "chat_assistant_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
[email protected]("clear_session_with_chat_assistants")
-class TestSessionWithChatAssistantCreate:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"name": "valid_name"}, 0, ""),
- pytest.param({"name": "a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1)}, 102, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": 1}, 100, "", marks=pytest.mark.skip(reason="issues/")),
- ({"name": ""}, 102, "`name` can not be empty"),
- ({"name": "duplicated_name"}, 0, ""),
- ({"name": "case insensitive"}, 0, ""),
- ],
- )
- def test_name(self, HttpApiAuth, add_chat_assistants, payload, expected_code, expected_message):
- _, _, chat_assistant_ids = add_chat_assistants
- if payload["name"] == "duplicated_name":
- create_session_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
- elif payload["name"] == "case insensitive":
- create_session_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], {"name": payload["name"].upper()})
-
- res = create_session_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- assert res["data"]["name"] == payload["name"]
- assert res["data"]["chat_id"] == chat_assistant_ids[0]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "chat_assistant_id, expected_code, expected_message",
- [
- ("", 100, "<MethodNotAllowed '405: Method Not Allowed'>"),
- ("invalid_chat_assistant_id", 109, "no authorization"),
- ],
- )
- def test_invalid_chat_assistant_id(self, HttpApiAuth, chat_assistant_id, expected_code, expected_message):
- res = create_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, {"name": "valid_name"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_concurrent_create_session(self, HttpApiAuth, add_chat_assistants):
- count = 1000
- _, _, chat_assistant_ids = add_chat_assistants
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_ids[0])
- if res["code"] != 0:
- assert False, res
- sessions_count = len(res["data"])
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- create_session_with_chat_assistant,
- HttpApiAuth,
- chat_assistant_ids[0],
- {"name": f"session with chat assistant test {i}"},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_ids[0], {"page_size": count * 2})
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]) == sessions_count + count
-
- @pytest.mark.p3
- def test_add_session_to_deleted_chat_assistant(self, HttpApiAuth, add_chat_assistants):
- _, _, chat_assistant_ids = add_chat_assistants
- res = delete_chat_assistants(HttpApiAuth, {"ids": [chat_assistant_ids[0]]})
- assert res["code"] == 0
- res = create_session_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], {"name": "valid_name"})
- assert res["code"] == 109
- assert res["message"] == "no authorization"
diff --git a/test/testcases/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py b/test/testcases/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py
deleted file mode 100644
index d8044c55df..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_delete_sessions_with_chat_assistant.py
+++ /dev/null
@@ -1,168 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_add_sessions_with_chat_assistant, delete_session_with_chat_assistants, list_session_with_chat_assistants
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = delete_session_with_chat_assistants(invalid_auth, "chat_assistant_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestSessionWithChatAssistantDelete:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "chat_assistant_id, expected_code, expected_message",
- [
- (
- "invalid_chat_assistant_id",
- 109,
- "no authorization",
- ),
- ],
- )
- def test_invalid_chat_assistant_id(self, HttpApiAuth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message):
- _, session_ids = add_sessions_with_chat_assistant_func
- res = delete_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, {"ids": session_ids})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, marks=pytest.mark.p1),
- pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_delete_partial_invalid_id(self, HttpApiAuth, add_sessions_with_chat_assistant_func, payload):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- if callable(payload):
- payload = payload(session_ids)
- res = delete_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, payload)
- assert res["code"] == 0
- assert res["data"]["errors"][0] == "The chat doesn't own the session invalid_id"
-
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id)
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]) == 0
-
- @pytest.mark.p3
- def test_repeated_deletion(self, HttpApiAuth, add_sessions_with_chat_assistant_func):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- payload = {"ids": session_ids}
- res = delete_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, payload)
- assert res["code"] == 0
-
- res = delete_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, payload)
- assert res["code"] == 102
- assert "The chat doesn't own the session" in res["message"]
-
- @pytest.mark.p3
- def test_duplicate_deletion(self, HttpApiAuth, add_sessions_with_chat_assistant_func):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- res = delete_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, {"ids": session_ids * 2})
- assert res["code"] == 0
- assert "Duplicate session ids" in res["data"]["errors"][0]
- assert res["data"]["success_count"] == 5
-
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id)
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]) == 0
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, HttpApiAuth, add_chat_assistants):
- count = 100
- _, _, chat_assistant_ids = add_chat_assistants
- session_ids = batch_add_sessions_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], count)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- delete_session_with_chat_assistants,
- HttpApiAuth,
- chat_assistant_ids[0],
- {"ids": session_ids[i : i + 1]},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_delete_1k(self, HttpApiAuth, add_chat_assistants):
- sessions_num = 1_000
- _, _, chat_assistant_ids = add_chat_assistants
- session_ids = batch_add_sessions_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], sessions_num)
-
- res = delete_session_with_chat_assistants(HttpApiAuth, chat_assistant_ids[0], {"ids": session_ids})
- assert res["code"] == 0
-
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_ids[0])
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]) == 0
-
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message, remaining",
- [
- pytest.param(None, 0, "", 5, marks=pytest.mark.p3),
- pytest.param({"ids": ["invalid_id"]}, 102, "The chat doesn't own the session invalid_id", 5, marks=pytest.mark.p3),
- pytest.param("not json", 100, """AttributeError("\'str\' object has no attribute \'get\'")""", 5, marks=pytest.mark.skip),
- pytest.param(lambda r: {"ids": r[:1]}, 0, "", 4, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r}, 0, "", 0, marks=pytest.mark.p1),
- pytest.param({"delete_all": True}, 0, "", 0, marks=pytest.mark.p1),
- pytest.param({"ids": []}, 0, "", 5, marks=pytest.mark.p3),
- ],
- )
- def test_basic_scenarios(
- self,
- HttpApiAuth,
- add_sessions_with_chat_assistant_func,
- payload,
- expected_code,
- expected_message,
- remaining,
- ):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- if callable(payload):
- payload = payload(session_ids)
- res = delete_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, payload)
- assert res["code"] == expected_code
- if res["code"] != 0:
- assert res["message"] == expected_message
-
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id)
- if res["code"] != 0:
- assert False, res
- assert len(res["data"]) == remaining
diff --git a/test/testcases/test_http_api/test_session_management/test_list_sessions_with_chat_assistant.py b/test/testcases/test_http_api/test_session_management/test_list_sessions_with_chat_assistant.py
deleted file mode 100644
index 0ea46706f5..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_list_sessions_with_chat_assistant.py
+++ /dev/null
@@ -1,246 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import delete_chat_assistants, list_session_with_chat_assistants
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-from utils import is_sorted
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = list_session_with_chat_assistants(invalid_auth, "chat_assistant_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestSessionsWithChatAssistantList:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 0, 2, ""),
- pytest.param({"page": 0, "page_size": 2}, 100, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- ({"page": 2, "page_size": 2}, 0, 2, ""),
- ({"page": 3, "page_size": 2}, 0, 1, ""),
- ({"page": "3", "page_size": 2}, 0, 1, ""),
- pytest.param({"page": -1, "page_size": 2}, 100, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- pytest.param({"page": "a", "page_size": 2}, 100, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page(self, HttpApiAuth, add_sessions_with_chat_assistant, params, expected_code, expected_page_size, expected_message):
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page_size": None}, 0, 5, ""),
- ({"page_size": 0}, 0, 0, ""),
- ({"page_size": 1}, 0, 1, ""),
- ({"page_size": 6}, 0, 5, ""),
- ({"page_size": "1"}, 0, 1, ""),
- pytest.param({"page_size": -1}, 0, 5, "", marks=pytest.mark.skip),
- pytest.param({"page_size": "a"}, 100, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page_size(self, HttpApiAuth, add_sessions_with_chat_assistant, params, expected_code, expected_page_size, expected_message):
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]) == expected_page_size
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"orderby": None}, 0, lambda r: is_sorted(r["data"], "create_time", True), ""),
- ({"orderby": "create_time"}, 0, lambda r: is_sorted(r["data"], "create_time", True), ""),
- ({"orderby": "update_time"}, 0, lambda r: is_sorted(r["data"], "update_time", True), ""),
- ({"orderby": "name", "desc": "False"}, 0, lambda r: is_sorted(r["data"], "name", False), ""),
- pytest.param({"orderby": "unknown"}, 102, 0, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/")),
- ],
- )
- def test_orderby(
- self,
- HttpApiAuth,
- add_sessions_with_chat_assistant,
- params,
- expected_code,
- assertions,
- expected_message,
- ):
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"desc": None}, 0, lambda r: is_sorted(r["data"], "create_time", True), ""),
- ({"desc": "true"}, 0, lambda r: is_sorted(r["data"], "create_time", True), ""),
- ({"desc": "True"}, 0, lambda r: is_sorted(r["data"], "create_time", True), ""),
- ({"desc": True}, 0, lambda r: is_sorted(r["data"], "create_time", True), ""),
- ({"desc": "false"}, 0, lambda r: is_sorted(r["data"], "create_time", False), ""),
- ({"desc": "False"}, 0, lambda r: is_sorted(r["data"], "create_time", False), ""),
- ({"desc": False}, 0, lambda r: is_sorted(r["data"], "create_time", False), ""),
- ({"desc": "False", "orderby": "update_time"}, 0, lambda r: is_sorted(r["data"], "update_time", False), ""),
- pytest.param({"desc": "unknown"}, 102, 0, "desc should be true or false", marks=pytest.mark.skip(reason="issues/")),
- ],
- )
- def test_desc(
- self,
- HttpApiAuth,
- add_sessions_with_chat_assistant,
- params,
- expected_code,
- assertions,
- expected_message,
- ):
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_num, expected_message",
- [
- ({"name": None}, 0, 5, ""),
- ({"name": ""}, 0, 5, ""),
- ({"name": "session_with_chat_assistant_1"}, 0, 1, ""),
- ({"name": "unknown"}, 0, 0, ""),
- ],
- )
- def test_name(self, HttpApiAuth, add_sessions_with_chat_assistant, params, expected_code, expected_num, expected_message):
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if params["name"] == "session_with_chat_assistant_1":
- assert res["data"][0]["name"] == params["name"]
- else:
- assert len(res["data"]) == expected_num
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "session_id, expected_code, expected_num, expected_message",
- [
- (None, 0, 5, ""),
- ("", 0, 5, ""),
- (lambda r: r[0], 0, 1, ""),
- ("unknown", 0, 0, "The chat doesn't exist"),
- ],
- )
- def test_id(self, HttpApiAuth, add_sessions_with_chat_assistant, session_id, expected_code, expected_num, expected_message):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant
- if callable(session_id):
- params = {"id": session_id(session_ids)}
- else:
- params = {"id": session_id}
-
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- if params["id"] == session_ids[0]:
- assert res["data"][0]["id"] == params["id"]
- else:
- assert len(res["data"]) == expected_num
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "session_id, name, expected_code, expected_num, expected_message",
- [
- (lambda r: r[0], "session_with_chat_assistant_0", 0, 1, ""),
- (lambda r: r[0], "session_with_chat_assistant_100", 0, 0, ""),
- (lambda r: r[0], "unknown", 0, 0, ""),
- ("id", "session_with_chat_assistant_0", 0, 0, ""),
- ],
- )
- def test_name_and_id(self, HttpApiAuth, add_sessions_with_chat_assistant, session_id, name, expected_code, expected_num, expected_message):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant
- if callable(session_id):
- params = {"id": session_id(session_ids), "name": name}
- else:
- params = {"id": session_id, "name": name}
-
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == expected_code
- if expected_code == 0:
- assert len(res["data"]) == expected_num
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_concurrent_list(self, HttpApiAuth, add_sessions_with_chat_assistant):
- count = 100
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_session_with_chat_assistants, HttpApiAuth, chat_assistant_id) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_invalid_params(self, HttpApiAuth, add_sessions_with_chat_assistant):
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- params = {"a": "b"}
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, params=params)
- assert res["code"] == 0
- assert len(res["data"]) == 5
-
- @pytest.mark.p3
- def test_list_chats_after_deleting_associated_chat_assistant(self, HttpApiAuth, add_sessions_with_chat_assistant):
- chat_assistant_id, _ = add_sessions_with_chat_assistant
- res = delete_chat_assistants(HttpApiAuth, {"ids": [chat_assistant_id]})
- assert res["code"] == 0
-
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id)
- assert res["code"] == 109
- assert res["message"] == "no authorization"
diff --git a/test/testcases/test_http_api/test_session_management/test_related_questions.py b/test/testcases/test_http_api/test_session_management/test_related_questions.py
deleted file mode 100644
index c70322ddf4..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_related_questions.py
+++ /dev/null
@@ -1,39 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import related_questions
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestRelatedQuestions:
- @pytest.mark.p3
- def test_related_questions_success(self, HttpApiAuth):
- res = related_questions(HttpApiAuth, {"question": "ragflow", "industry": "search"})
- assert res["code"] == 0, res
- assert isinstance(res.get("data"), list), res
-
- @pytest.mark.p2
- def test_related_questions_missing_question(self, HttpApiAuth):
- res = related_questions(HttpApiAuth, {"industry": "search"})
- assert res["code"] == 101, res
- assert "question" in res.get("message", ""), res
-
- @pytest.mark.p2
- def test_related_questions_invalid_auth(self):
- res = related_questions(RAGFlowHttpApiAuth(INVALID_API_TOKEN), {"question": "ragflow", "industry": "search"})
- assert res["code"] == 102, res
- assert "API key is invalid" in res.get("message", ""), res
diff --git a/test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py b/test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
deleted file mode 100644
index 484fa63789..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
+++ /dev/null
@@ -1,2918 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import inspect
-import json
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _Args(dict):
- def get(self, key, default=None, type=None):
- value = super().get(key, default)
- if value is None or type is None:
- return value
- try:
- return type(value)
- except (TypeError, ValueError):
- return default
-
-
-class _StubHeaders:
- def __init__(self):
- self._items = []
-
- def add_header(self, key, value):
- self._items.append((key, value))
-
- def get(self, key, default=None):
- for existing_key, value in reversed(self._items):
- if existing_key == key:
- return value
- return default
-
-
-class _StubResponse:
- def __init__(self, body, mimetype=None, content_type=None):
- self.body = body
- self.mimetype = mimetype
- self.content_type = content_type
- self.headers = _StubHeaders()
-
-
-class _DummyUploadFile:
- def __init__(self, filename):
- self.filename = filename
- self.saved_path = None
-
- async def save(self, path):
- self.saved_path = path
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-async def _collect_stream(body):
- items = []
- if hasattr(body, "__aiter__"):
- async for item in body:
- if isinstance(item, bytes):
- item = item.decode("utf-8")
- items.append(item)
- else:
- for item in body:
- if isinstance(item, bytes):
- item = item.decode("utf-8")
- items.append(item)
- return items
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_session_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- # Mock common.constants module
- from enum import Enum
- from enum import StrEnum
-
- class _StubLLMType(StrEnum):
- CHAT = "chat"
- EMBEDDING = "embedding"
- ASR = "asr"
- VISION = "vision"
- RERANK = "rerank"
- TTS = "tts"
- OCR = "ocr"
-
- class _StubParserType(StrEnum):
- PRESENTATION = "presentation"
- LAWS = "laws"
- MANUAL = "manual"
- PAPER = "paper"
- RESUME = "resume"
- BOOK = "book"
- QA = "qa"
- TABLE = "table"
- NAIVE = "naive"
- PICTURE = "picture"
- ONE = "one"
- AUDIO = "audio"
- EMAIL = "email"
- KG = "knowledge_graph"
- TAG = "tag"
-
- class _StubRetCode(int, Enum):
- SUCCESS = 0
- NOT_EFFECTIVE = 10
- EXCEPTION_ERROR = 100
- ARGUMENT_ERROR = 101
- DATA_ERROR = 102
- OPERATING_ERROR = 103
- CONNECTION_ERROR = 105
- RUNNING = 106
- PERMISSION_ERROR = 108
- AUTHENTICATION_ERROR = 109
- BAD_REQUEST = 400
- UNAUTHORIZED = 401
- SERVER_ERROR = 500
- FORBIDDEN = 403
- NOT_FOUND = 404
- CONFLICT = 409
-
- class _StubStatusEnum(str, Enum):
- VALID = "1"
- INVALID = "0"
-
- class _StubActiveEnum(Enum):
- ACTIVE = "1"
- INACTIVE = "0"
-
- class _StubStorage(Enum):
- MINIO = 1
- AZURE_SPN = 2
- AZURE_SAS = 3
- AWS_S3 = 4
- OSS = 5
- OPENDAL = 6
- GCS = 7
-
- class _StubMCPServerType(StrEnum):
- SSE = "sse"
- STREAMABLE_HTTP = "streamable-http"
-
- class _StubTaskStatus(StrEnum):
- UNSTART = "0"
- RUNNING = "1"
- CANCEL = "2"
- DONE = "3"
- FAIL = "4"
- SCHEDULE = "5"
-
- class _StubFileSource(StrEnum):
- LOCAL = ""
- KNOWLEDGEBASE = "knowledgebase"
- S3 = "s3"
- NOTION = "notion"
- DISCORD = "discord"
- CONFLUENCE = "confluence"
- GMAIL = "gmail"
- GOOGLE_DRIVE = "google_drive"
- JIRA = "jira"
- SHAREPOINT = "sharepoint"
- SLACK = "slack"
- TEAMS = "teams"
- WEBDAV = "webdav"
- MOODLE = "moodle"
- DROPBOX = "dropbox"
- BOX = "box"
- R2 = "r2"
- OCI_STORAGE = "oci_storage"
- GOOGLE_CLOUD_STORAGE = "google_cloud_storage"
- AIRTABLE = "airtable"
- ASANA = "asana"
- GITHUB = "github"
- GITLAB = "gitlab"
- IMAP = "imap"
- BITBUCKET = "bitbucket"
- ZENDESK = "zendesk"
- SEAFILE = "seafile"
- MYSQL = "mysql"
- POSTGRESQL = "postgresql"
-
- common_constants_mod = ModuleType("common.constants")
- common_constants_mod.LLMType = _StubLLMType
- common_constants_mod.ParserType = _StubParserType
- common_constants_mod.RetCode = _StubRetCode
- common_constants_mod.StatusEnum = _StubStatusEnum
- common_constants_mod.ActiveEnum = _StubActiveEnum
- common_constants_mod.Storage = _StubStorage
- common_constants_mod.MCPServerType = _StubMCPServerType
- common_constants_mod.TaskStatus = _StubTaskStatus
- common_constants_mod.FileSource = _StubFileSource
- common_constants_mod.SERVICE_CONF = "service_conf.yaml"
- common_constants_mod.RAG_FLOW_SERVICE_NAME = "ragflow"
- common_constants_mod.SVR_QUEUE_NAME = "rag_flow_svr_queue"
- common_constants_mod.SVR_CONSUMER_GROUP_NAME = "rag_flow_svr_task_broker"
- common_constants_mod.PAGERANK_FLD = "pagerank_fea"
- common_constants_mod.TAG_FLD = "tag_feas"
- # Import pure-Python constants from the real module (no heavy deps)
- from common.constants import MAXIMUM_PAGE_NUMBER as _MPN, MAXIMUM_TASK_PAGE_NUMBER as _MTPN
-
- common_constants_mod.MAXIMUM_PAGE_NUMBER = _MPN
- common_constants_mod.MAXIMUM_TASK_PAGE_NUMBER = _MTPN
- monkeypatch.setitem(sys.modules, "common.constants", common_constants_mod)
-
- common_metadata_utils_mod = ModuleType("common.metadata_utils")
- common_metadata_utils_mod.apply_meta_data_filter = lambda *_args, **_kwargs: []
- common_metadata_utils_mod.convert_conditions = lambda conditions: conditions
- common_metadata_utils_mod.meta_filter = lambda *_args, **_kwargs: True
- monkeypatch.setitem(sys.modules, "common.metadata_utils", common_metadata_utils_mod)
-
- common_settings_mod = ModuleType("common.settings")
- common_settings_mod.retriever = SimpleNamespace()
- common_settings_mod.kg_retriever = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "common.settings", common_settings_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.add_tenant_id_to_kwargs = lambda func: func
- api_utils_mod.check_duplicate_ids = lambda ids, _kind="item": (ids, [])
- api_utils_mod.get_data_error_result = lambda message="Sorry! Data missing!", code=_StubRetCode.DATA_ERROR: {"code": code, "message": message}
- api_utils_mod.get_error_data_result = lambda message="Sorry! Data missing!", code=_StubRetCode.DATA_ERROR: {"code": code, "message": message}
- api_utils_mod.get_json_result = lambda code=_StubRetCode.SUCCESS, message="success", data=None: {"code": code, "message": message, "data": data}
- api_utils_mod.get_result = lambda code=_StubRetCode.SUCCESS, message="", data=None, total=None: {
- key: value for key, value in {"code": code, "message": message, "data": data, "total": total}.items() if value is not None
- }
- api_utils_mod.get_request_json = lambda: _AwaitableValue({})
- api_utils_mod.server_error_response = lambda e: {"code": _StubRetCode.SERVER_ERROR, "message": str(e)}
- api_utils_mod.validate_request = lambda *_args, **_kwargs: lambda func: func
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- rag_app_tag_mod = ModuleType("rag.app.tag")
- rag_app_tag_mod.label_question = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "rag.app.tag", rag_app_tag_mod)
-
- rag_prompts_generator_mod = ModuleType("rag.prompts.generator")
- rag_prompts_generator_mod.cross_languages = lambda *_args, **_kwargs: ""
- rag_prompts_generator_mod.keyword_extraction = lambda *_args, **_kwargs: ""
- rag_prompts_generator_mod.chunks_format = lambda chunks: chunks
- monkeypatch.setitem(sys.modules, "rag.prompts.generator", rag_prompts_generator_mod)
-
- rag_prompts_template_mod = ModuleType("rag.prompts.template")
- rag_prompts_template_mod.load_prompt = lambda *_args, **_kwargs: ""
- monkeypatch.setitem(sys.modules, "rag.prompts.template", rag_prompts_template_mod)
-
- rag_nlp_mod = ModuleType("rag.nlp")
- rag_nlp_mod.search = SimpleNamespace(index_name=lambda tenant_id: f"idx_{tenant_id}")
- monkeypatch.setitem(sys.modules, "rag.nlp", rag_nlp_mod)
- monkeypatch.setitem(sys.modules, "rag.nlp.search", rag_nlp_mod.search)
-
- deepdoc_pkg = ModuleType("deepdoc")
- deepdoc_parser_pkg = ModuleType("deepdoc.parser")
- deepdoc_parser_pkg.__path__ = []
-
- class _StubPdfParser:
- pass
-
- class _StubExcelParser:
- pass
-
- class _StubDocxParser:
- pass
-
- deepdoc_parser_pkg.PdfParser = _StubPdfParser
- deepdoc_parser_pkg.ExcelParser = _StubExcelParser
- deepdoc_parser_pkg.DocxParser = _StubDocxParser
- deepdoc_pkg.parser = deepdoc_parser_pkg
- monkeypatch.setitem(sys.modules, "deepdoc", deepdoc_pkg)
- monkeypatch.setitem(sys.modules, "deepdoc.parser", deepdoc_parser_pkg)
-
- deepdoc_excel_module = ModuleType("deepdoc.parser.excel_parser")
- deepdoc_excel_module.RAGFlowExcelParser = _StubExcelParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.excel_parser", deepdoc_excel_module)
-
- deepdoc_mineru_module = ModuleType("deepdoc.parser.mineru_parser")
-
- class _StubMinerUParser:
- pass
-
- deepdoc_mineru_module.MinerUParser = _StubMinerUParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.mineru_parser", deepdoc_mineru_module)
-
- deepdoc_paddle_module = ModuleType("deepdoc.parser.paddleocr_parser")
-
- class _StubPaddleOCRParser:
- pass
-
- deepdoc_paddle_module.PaddleOCRParser = _StubPaddleOCRParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.paddleocr_parser", deepdoc_paddle_module)
-
- deepdoc_parser_utils = ModuleType("deepdoc.parser.utils")
- deepdoc_parser_utils.get_text = lambda *_args, **_kwargs: ""
- monkeypatch.setitem(sys.modules, "deepdoc.parser.utils", deepdoc_parser_utils)
- monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))
-
- # Mock tenant_llm_service for TenantLLMService and TenantService
- tenant_llm_service_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _MockModelConfig:
- def __init__(self, tenant_id, model_name):
- self.tenant_id = tenant_id
- self.llm_name = model_name
- self.llm_factory = "Builtin"
- self.api_key = "fake-api-key"
- self.api_base = "https://api.example.com"
- self.model_type = "chat"
- self.max_tokens = 8192
- self.used_tokens = 0
- self.status = 1
- self.id = 1
-
- def to_dict(self):
- return {
- "tenant_id": self.tenant_id,
- "llm_name": self.llm_name,
- "llm_factory": self.llm_factory,
- "api_key": self.api_key,
- "api_base": self.api_base,
- "model_type": self.model_type,
- "max_tokens": self.max_tokens,
- "used_tokens": self.used_tokens,
- "status": self.status,
- "id": self.id,
- }
-
- class _StubTenantService:
- @staticmethod
- def get_by_id(tenant_id):
- # Return a mock tenant with default model configurations
- return True, SimpleNamespace(id=tenant_id, llm_id="chat-model", embd_id="embd-model", asr_id="asr-model", img2txt_id="img2txt-model", rerank_id="rerank-model", tts_id="tts-model")
-
- class _StubTenantLLMService:
- @staticmethod
- def get_api_key(tenant_id, model_name):
- return _MockModelConfig(tenant_id, model_name)
-
- @staticmethod
- def split_model_name_and_factory(model_name):
- if "@" in model_name:
- parts = model_name.split("@")
- return parts[0], parts[1]
- return model_name, None
-
- class _StubLLMFactoriesService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- tenant_llm_service_mod.TenantService = _StubTenantService
- tenant_llm_service_mod.TenantLLMService = _StubTenantLLMService
- tenant_llm_service_mod.LLMFactoriesService = _StubLLMFactoriesService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_service_mod)
-
- # Mock LLMService
- llm_service_mod = ModuleType("api.db.services.llm_service")
-
- class _StubLLM:
- def __init__(self, llm_name):
- self.llm_name = llm_name
- self.is_tools = False
-
- llm_service_mod.LLMService = SimpleNamespace(query=lambda llm_name: [_StubLLM(llm_name)] if llm_name else [])
-
- class _StubLLMBundle:
- def __init__(self, tenant_id: str, model_config: dict, lang="Chinese", **kwargs):
- self.tenant_id = tenant_id
- self.model_config = model_config
- self.lang = lang
-
- async def async_chat(self, prompt, messages, options):
- return "mock response"
-
- def transcription(self, audio_path):
- return "mock transcription"
-
- llm_service_mod.LLMBundle = _StubLLMBundle
- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)
-
- # Mock tenant_model_service to ensure it uses mocked services
- tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
-
- class _MockModelConfig2:
- def __init__(self, tenant_id, model_name, model_type="chat"):
- self.tenant_id = tenant_id
- self.llm_name = model_name
- self.llm_factory = "Builtin"
- self.api_key = "fake-api-key"
- self.api_base = "https://api.example.com"
- self.model_type = model_type
- self.max_tokens = 8192
- self.used_tokens = 0
- self.status = 1
- self.id = 1
-
- def to_dict(self):
- return {
- "tenant_id": self.tenant_id,
- "llm_name": self.llm_name,
- "llm_factory": self.llm_factory,
- "api_key": self.api_key,
- "api_base": self.api_base,
- "model_type": self.model_type,
- "max_tokens": self.max_tokens,
- "used_tokens": self.used_tokens,
- "status": self.status,
- "id": self.id,
- }
-
- def _get_model_config_by_id(
- tenant_model_id: str,
- allowed_tenant_ids=None,
- requester_tenant_id=None,
- ) -> dict:
- mock_tenant_id = "tenant-1"
- if allowed_tenant_ids is not None:
- if isinstance(allowed_tenant_ids, str):
- allowed_tenant_ids = {allowed_tenant_ids}
- else:
- allowed_tenant_ids = {str(tenant_id) for tenant_id in allowed_tenant_ids if tenant_id}
- if mock_tenant_id not in allowed_tenant_ids and str(requester_tenant_id) != mock_tenant_id:
- raise LookupError(f"Tenant Model with id {tenant_model_id} not authorized")
- return _MockModelConfig2(mock_tenant_id, "model-1").to_dict()
-
- def _get_model_config_from_provider_instance(tenant_id: str, model_type: str, model_name: str):
- if not model_name:
- raise Exception("Model Name is required")
- return _MockModelConfig2(tenant_id, model_name, model_type).to_dict()
-
- def _get_api_key(tenant_id: str, model_name: str):
- if not tenant_id or not model_name:
- return None
- return "fake-api-key"
-
- def _split_model_name(model_name: str):
- parts = model_name.split("@")
- if len(parts) == 1:
- return parts[0], "", ""
- if len(parts) == 2:
- return parts[0], "default", parts[1]
- return parts[0], parts[1], parts[2]
-
- def _get_tenant_default_model_by_type(tenant_id: str, model_type):
- # Check if tenant exists
- from api.db.services.tenant_llm_service import TenantService
-
- exist, tenant = TenantService.get_by_id(tenant_id)
- if not exist:
- raise LookupError("Tenant not found!")
- # Return mock tenant with default model configurations
- model_type_val = model_type if isinstance(model_type, str) else model_type.value
- model_name = ""
- if model_type_val == "embedding":
- model_name = tenant.embd_id
- elif model_type_val == "asr":
- model_name = tenant.asr_id
- elif model_type_val == "vision":
- model_name = tenant.img2txt_id
- elif model_type_val == "chat":
- model_name = tenant.llm_id
- elif model_type_val == "rerank":
- model_name = tenant.rerank_id
- elif model_type_val == "tts":
- model_name = tenant.tts_id
- elif model_type_val == "ocr":
- raise Exception("OCR model name is required")
- if not model_name:
- # Use friendly model type names
- friendly_names = {"embedding": "Embedding", "asr": "ASR", "vision": "Vision", "chat": "Chat", "rerank": "Rerank", "tts": "TTS", "ocr": "OCR"}
- friendly_name = friendly_names.get(model_type_val, model_type_val)
- raise Exception(f"No default {friendly_name} model is set")
- return _MockModelConfig2(tenant_id, model_name, model_type_val).to_dict()
-
- tenant_model_service_mod.get_model_config_by_id = _get_model_config_by_id
- tenant_model_service_mod.get_model_config_from_provider_instance = _get_model_config_from_provider_instance
- tenant_model_service_mod.resolve_model_config = _get_model_config_from_provider_instance
- tenant_model_service_mod.get_tenant_default_model_by_type = _get_tenant_default_model_by_type
- tenant_model_service_mod.get_api_key = _get_api_key
- tenant_model_service_mod.split_model_name = _split_model_name
- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = []
- agent_canvas_mod = ModuleType("agent.canvas")
- agent_dsl_migration_mod = ModuleType("agent.dsl_migration")
-
- class _StubCanvas:
- def __init__(self, *_args, **_kwargs):
- self._dsl = "{}"
-
- def reset(self):
- return None
-
- def get_prologue(self):
- return "stub prologue"
-
- def get_component_input_form(self, _name):
- return {}
-
- def get_mode(self):
- return "chat"
-
- def __str__(self):
- return self._dsl
-
- agent_dsl_migration_mod.normalize_chunker_dsl = lambda dsl: dsl
- agent_canvas_mod.Canvas = _StubCanvas
- agent_pkg.canvas = agent_canvas_mod
- agent_pkg.dsl_migration = agent_dsl_migration_mod
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
- monkeypatch.setitem(sys.modules, "agent.canvas", agent_canvas_mod)
- monkeypatch.setitem(sys.modules, "agent.dsl_migration", agent_dsl_migration_mod)
-
- quart_mod = ModuleType("quart")
- quart_mod.request = SimpleNamespace(args=_Args(), headers={}, files=_AwaitableValue({}), method="POST")
- quart_mod.Response = _StubResponse
- quart_mod.jsonify = lambda payload: payload
- quart_mod.current_app = SimpleNamespace()
- quart_mod.has_app_context = lambda: False
- quart_mod.has_request_context = lambda: False
- quart_mod.has_websocket_context = lambda: False
- quart_mod.websocket = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- api_apps_mod = ModuleType("api.apps")
- api_apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- api_apps_mod.AUTH_BETA = False
- api_apps_mod.current_user = SimpleNamespace(id="tenant-1")
- api_apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", api_apps_mod)
-
- quart_auth_mod = ModuleType("quart_auth")
-
- class _StubAuthUser:
- pass
-
- quart_auth_mod.AuthUser = _StubAuthUser
- monkeypatch.setitem(sys.modules, "quart_auth", quart_auth_mod)
-
- class _FakeExpr:
- def __or__(self, other):
- return self
-
- def __and__(self, other):
- return self
-
- class _FakeField:
- def __eq__(self, other):
- return _FakeExpr()
-
- def __ne__(self, other):
- return _FakeExpr()
-
- def is_null(self, value=True):
- return _FakeExpr()
-
- class _StubTaskModel:
- id = _FakeField()
- doc_id = _FakeField()
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.APIToken = SimpleNamespace(query=lambda **_kwargs: [])
- db_models_mod.Task = _StubTaskModel
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = [str(repo_root / "api" / "db" / "services")]
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- api_service_mod = ModuleType("api.db.services.api_service")
- api_service_mod.API4ConversationService = SimpleNamespace(
- get_names=lambda *_args, **_kwargs: [],
- get_list=lambda *_args, **_kwargs: (0, []),
- save=lambda **_kwargs: True,
- get_by_id=lambda _session_id: (True, SimpleNamespace(to_dict=lambda: {"id": _session_id})),
- delete_by_id=lambda *_args, **_kwargs: True,
- query=lambda **_kwargs: [],
- )
- monkeypatch.setitem(sys.modules, "api.db.services.api_service", api_service_mod)
-
- canvas_service_mod = ModuleType("api.db.services.canvas_service")
- canvas_service_mod.CanvasTemplateService = SimpleNamespace(get_all=lambda *_args, **_kwargs: [])
- canvas_service_mod.UserCanvasService = SimpleNamespace(
- query=lambda **_kwargs: [],
- get_by_id=lambda *_args, **_kwargs: (False, None),
- accessible=lambda *_args, **_kwargs: False,
- get_agent_dsl_with_release=lambda *_args, **_kwargs: (SimpleNamespace(id="agent-1"), "{}"),
- )
-
- async def _empty_agent_completion(*_args, **_kwargs):
- if False:
- yield None
-
- canvas_service_mod.completion = _empty_agent_completion
- canvas_service_mod.completion_openai = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.canvas_service", canvas_service_mod)
-
- conversation_service_mod = ModuleType("api.db.services.conversation_service")
- conversation_service_mod.ConversationService = SimpleNamespace(query=lambda **_kwargs: [])
- conversation_service_mod.async_iframe_completion = lambda *_args, **_kwargs: None
- conversation_service_mod.async_completion = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.db.services.conversation_service", conversation_service_mod)
-
- dialog_service_mod = ModuleType("api.db.services.dialog_service")
- dialog_service_mod.DialogService = SimpleNamespace(
- model=SimpleNamespace(_meta=SimpleNamespace(fields=[])),
- query=lambda **_kwargs: [],
- get_by_id=lambda *_args, **_kwargs: (False, None),
- )
- dialog_service_mod.async_ask = lambda *_args, **_kwargs: None
- dialog_service_mod.async_chat = lambda *_args, **_kwargs: None
- dialog_service_mod.gen_mindmap = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.db.services.dialog_service", dialog_service_mod)
-
- doc_metadata_service_mod = ModuleType("api.db.services.doc_metadata_service")
- doc_metadata_service_mod.DocMetadataService = SimpleNamespace(
- get_flatted_meta_by_kbs=lambda *_args, **_kwargs: [],
- get_metadata_for_documents=lambda *_args, **_kwargs: {},
- )
- monkeypatch.setitem(sys.modules, "api.db.services.doc_metadata_service", doc_metadata_service_mod)
-
- knowledgebase_service_mod = ModuleType("api.db.services.knowledgebase_service")
- knowledgebase_service_mod.KnowledgebaseService = SimpleNamespace(
- query=lambda **_kwargs: [],
- get_by_id=lambda *_args, **_kwargs: (False, None),
- )
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", knowledgebase_service_mod)
-
- search_service_mod = ModuleType("api.db.services.search_service")
- search_service_mod.SearchService = SimpleNamespace(
- query=lambda **_kwargs: [],
- get_detail=lambda *_args, **_kwargs: None,
- )
- monkeypatch.setitem(sys.modules, "api.db.services.search_service", search_service_mod)
-
- user_service_mod = ModuleType("api.db.services.user_service")
- user_service_mod.UserTenantService = SimpleNamespace(query=lambda **_kwargs: [])
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- user_canvas_version_mod = ModuleType("api.db.services.user_canvas_version")
- user_canvas_version_mod.UserCanvasVersionService = SimpleNamespace(
- list_by_canvas_id=lambda *_args, **_kwargs: [],
- get_by_id=lambda *_args, **_kwargs: (False, None),
- get_latest_version_title=lambda *_args, **_kwargs: "",
- save_or_replace_latest=lambda **_kwargs: True,
- build_version_title=lambda *_args, **_kwargs: "v1",
- )
- monkeypatch.setitem(sys.modules, "api.db.services.user_canvas_version", user_canvas_version_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "bot_api.py"
- spec = importlib.util.spec_from_file_location("test_session_sdk_routes_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, "test_session_sdk_routes_unit_module", module)
- spec.loader.exec_module(module)
-
- # Add TenantService to module for test compatibility
- class _StubTenantServiceForTest:
- @staticmethod
- def get_info_by(tenant_id):
- # Return mock tenant info for tests
- return []
-
- @staticmethod
- def get_by_id(tenant_id):
- # Return mock tenant by id
- return True, SimpleNamespace(id=tenant_id, llm_id="chat-model", embd_id="embd-model", asr_id="asr-model", img2txt_id="img2txt-model", rerank_id="rerank-model", tts_id="tts-model")
-
- module.TenantService = _StubTenantServiceForTest
-
- return module
-
-
-def _load_agent_api_module(monkeypatch):
- _load_session_module(monkeypatch)
- repo_root = Path(__file__).resolve().parents[4]
-
- agent_component_mod = ModuleType("agent.component")
-
- class _StubAgentLLM:
- pass
-
- agent_component_mod.LLM = _StubAgentLLM
- monkeypatch.setitem(sys.modules, "agent.component", agent_component_mod)
-
- api_apps_mod = ModuleType("api.apps")
- api_apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- api_apps_mod.current_user = SimpleNamespace(id="tenant-1")
- api_apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", api_apps_mod)
-
- api_apps_services_mod = ModuleType("api.apps.services")
- api_apps_services_mod.__path__ = [str(repo_root / "api" / "apps" / "services")]
- monkeypatch.setitem(sys.modules, "api.apps.services", api_apps_services_mod)
-
- canvas_replica_mod = ModuleType("api.apps.services.canvas_replica_service")
-
- class _StubCanvasReplicaService:
- @staticmethod
- def normalize_dsl(dsl):
- return dsl
-
- @staticmethod
- def replace_for_set(**_kwargs):
- return True
-
- @staticmethod
- def bootstrap(**_kwargs):
- return True
-
- @staticmethod
- def load_for_run(**_kwargs):
- return {"dsl": {}, "title": "agent", "canvas_category": "agent"}
-
- @staticmethod
- def commit_after_run(**_kwargs):
- return True
-
- canvas_replica_mod.CanvasReplicaService = _StubCanvasReplicaService
- monkeypatch.setitem(sys.modules, "api.apps.services.canvas_replica_service", canvas_replica_mod)
-
- file_service_mod = ModuleType("api.db.services.file_service")
- file_service_mod.FileService = SimpleNamespace(
- upload_info=lambda *_args, **_kwargs: {},
- get_blob=lambda *_args, **_kwargs: b"",
- )
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
-
- api_service_mod = ModuleType("api.db.services.api_service")
- api_service_mod.API4ConversationService = SimpleNamespace(
- get_names=lambda *_args, **_kwargs: [],
- get_list=lambda *_args, **_kwargs: (0, []),
- save=lambda **_kwargs: True,
- get_by_id=lambda _session_id: (True, SimpleNamespace(to_dict=lambda: {"id": _session_id})),
- delete_by_id=lambda *_args, **_kwargs: True,
- )
- monkeypatch.setitem(sys.modules, "api.db.services.api_service", api_service_mod)
-
- document_service_mod = ModuleType("api.db.services.document_service")
- document_service_mod.DocumentService = SimpleNamespace(
- clear_chunk_num_when_rerun=lambda *_args, **_kwargs: True,
- update_by_id=lambda *_args, **_kwargs: True,
- )
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", document_service_mod)
-
- knowledgebase_service_mod = ModuleType("api.db.services.knowledgebase_service")
- knowledgebase_service_mod.KnowledgebaseService = SimpleNamespace(
- query=lambda **_kwargs: [],
- get_by_id=lambda *_args, **_kwargs: (False, None),
- )
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", knowledgebase_service_mod)
-
- task_service_mod = ModuleType("api.db.services.task_service")
- task_service_mod.CANVAS_DEBUG_DOC_ID = "debug-doc"
- task_service_mod.GRAPH_RAPTOR_FAKE_DOC_ID = "graph-raptor-fake-doc"
- task_service_mod.TaskService = SimpleNamespace(filter_delete=lambda *_args, **_kwargs: True)
- task_service_mod.queue_dataflow = lambda *_args, **_kwargs: (True, "")
- monkeypatch.setitem(sys.modules, "api.db.services.task_service", task_service_mod)
-
- pipeline_operation_log_service_mod = ModuleType("api.db.services.pipeline_operation_log_service")
- pipeline_operation_log_service_mod.PipelineOperationLogService = SimpleNamespace(
- get_documents_info=lambda *_args, **_kwargs: [],
- update_by_id=lambda *_args, **_kwargs: True,
- )
- monkeypatch.setitem(
- sys.modules,
- "api.db.services.pipeline_operation_log_service",
- pipeline_operation_log_service_mod,
- )
-
- user_service_mod = ModuleType("api.db.services.user_service")
- user_service_mod.TenantService = SimpleNamespace(get_joined_tenants_by_user_id=lambda *_args, **_kwargs: [])
- user_service_mod.UserService = SimpleNamespace(get_by_id=lambda *_args, **_kwargs: (False, None))
- user_service_mod.UserTenantService = SimpleNamespace(query=lambda **_kwargs: [])
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- user_canvas_version_mod = ModuleType("api.db.services.user_canvas_version")
- user_canvas_version_mod.UserCanvasVersionService = SimpleNamespace(
- list_by_canvas_id=lambda *_args, **_kwargs: [],
- get_by_id=lambda *_args, **_kwargs: (False, None),
- get_latest_version_title=lambda *_args, **_kwargs: "",
- save_or_replace_latest=lambda **_kwargs: True,
- build_version_title=lambda *_args, **_kwargs: "v1",
- )
- monkeypatch.setitem(sys.modules, "api.db.services.user_canvas_version", user_canvas_version_mod)
-
- rag_flow_pipeline_mod = ModuleType("rag.flow.pipeline")
-
- class _StubPipeline:
- def __init__(self, *_args, **_kwargs):
- pass
-
- rag_flow_pipeline_mod.Pipeline = _StubPipeline
- monkeypatch.setitem(sys.modules, "rag.flow.pipeline", rag_flow_pipeline_mod)
-
- rag_redis_mod = ModuleType("rag.utils.redis_conn")
- rag_redis_mod.REDIS_CONN = SimpleNamespace(get=lambda *_args, **_kwargs: None)
- monkeypatch.setitem(sys.modules, "rag.utils.redis_conn", rag_redis_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "agent_api.py"
- spec = importlib.util.spec_from_file_location("test_agent_api_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, "test_agent_api_unit_module", module)
- spec.loader.exec_module(module)
- return module
-
-
-def _load_openai_api_module(monkeypatch):
- _load_session_module(monkeypatch)
- repo_root = Path(__file__).resolve().parents[4]
-
- api_apps_mod = ModuleType("api.apps")
- api_apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- api_apps_mod.login_required = lambda func: func
- api_apps_mod.current_user = SimpleNamespace(id="tenant-1")
- monkeypatch.setitem(sys.modules, "api.apps", api_apps_mod)
-
- api_apps_restful_mod = ModuleType("api.apps.restful_apis")
- api_apps_restful_mod.__path__ = [str(repo_root / "api" / "apps" / "restful_apis")]
- monkeypatch.setitem(sys.modules, "api.apps.restful_apis", api_apps_restful_mod)
-
- quart_mod = ModuleType("quart")
- quart_mod.Response = _StubResponse
- quart_mod.jsonify = lambda payload: payload
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "openai_api.py"
- spec = importlib.util.spec_from_file_location("test_openai_api_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, "test_openai_api_unit_module", module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_create_and_update_guard_matrix(monkeypatch):
- module = _load_session_module(monkeypatch)
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args()))
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [SimpleNamespace(id="agent-1")])
-
- def _raise_lookup(*_args, **_kwargs):
- raise LookupError("Agent not found.")
-
- monkeypatch.setattr(module.UserCanvasService, "get_agent_dsl_with_release", _raise_lookup)
- res = _run(inspect.unwrap(module.create_agent_session)("tenant-1", "agent-1"))
- assert res["message"] == "Agent not found."
-
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [])
- res = _run(inspect.unwrap(module.create_agent_session)("tenant-1", "agent-1"))
- assert res["message"] == "You cannot access the agent."
-
-
-def test_openai_chat_validation_matrix_unit(monkeypatch):
- module = _load_openai_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "num_tokens_from_string", lambda _text: 1)
- monkeypatch.setattr(
- module.DialogService,
- "query",
- lambda **_kwargs: [SimpleNamespace(kb_ids=["kb-1"], llm_id="chat-model", tenant_id="tenant-1")],
- )
-
- cases = [
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": "bad",
- },
- "extra_body must be an object.",
- ),
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": {"reference_metadata": "bad"},
- },
- "reference_metadata must be an object.",
- ),
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": {"reference_metadata": {"fields": "bad"}},
- },
- "reference_metadata.fields must be an array.",
- ),
- ({"model": "model", "messages": []}, "You have to provide messages."),
- (
- {"model": "model", "messages": [{"role": "assistant", "content": "hello"}]},
- "The last content of this conversation is not from user.",
- ),
- (
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "extra_body": {"metadata_condition": "bad"},
- },
- "metadata_condition must be an object.",
- ),
- ]
-
- for payload, expected in cases:
- monkeypatch.setattr(module, "get_request_json", lambda p=payload: _AwaitableValue(p))
- res = _run(inspect.unwrap(module.openai_chat_completions)("chat-1"))
- assert expected in res["message"]
-
-
-def test_openai_stream_generator_branches_unit(monkeypatch):
- module = _load_openai_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "num_tokens_from_string", lambda text: len(text or ""))
- monkeypatch.setattr(module, "convert_conditions", lambda cond: cond.get("conditions", []))
- monkeypatch.setattr(module, "meta_filter", lambda *_args, **_kwargs: [])
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kb_ids: [{"id": "doc-1"}])
- monkeypatch.setattr(
- module.DialogService,
- "query",
- lambda **_kwargs: [SimpleNamespace(kb_ids=["kb-1"], llm_id="chat-model", tenant_id="tenant-1")],
- )
- monkeypatch.setattr(module, "_build_reference_chunks", lambda *_args, **_kwargs: [{"id": "ref-1"}])
-
- async def fake_async_chat(_dia, _msg, _stream, **_kwargs):
- yield {"start_to_think": True}
- yield {"answer": "R"}
- yield {"end_to_think": True}
- yield {"answer": ""}
- yield {"answer": "C"}
- yield {"final": True, "answer": "DONE", "reference": {"chunks": []}}
- raise RuntimeError("boom")
-
- monkeypatch.setattr(module, "async_chat", fake_async_chat)
-
- payload = {
- "model": "model",
- "stream": True,
- "messages": [
- {"role": "system", "content": "sys"},
- {"role": "assistant", "content": "preface"},
- {"role": "user", "content": "hello"},
- ],
- "extra_body": {
- "reference": True,
- "reference_metadata": {"include": True, "fields": ["author"]},
- "metadata_condition": {"logic": "and", "conditions": [{"name": "author", "value": "bob"}]},
- },
- }
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(payload))
-
- resp = _run(inspect.unwrap(module.openai_chat_completions)("chat-1"))
- assert isinstance(resp, _StubResponse)
- assert resp.headers.get("Content-Type") == "text/event-stream; charset=utf-8"
-
- chunks = _run(_collect_stream(resp.body))
- assert any("reasoning_content" in chunk for chunk in chunks)
- assert any("**ERROR**: boom" in chunk for chunk in chunks)
- assert any('"usage"' in chunk for chunk in chunks)
- assert any('"reference"' in chunk for chunk in chunks)
- assert chunks[-1].strip() == "data:[DONE]"
-
-
-def test_openai_nonstream_branch_unit(monkeypatch):
- module = _load_openai_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "num_tokens_from_string", lambda text: len(text or ""))
- monkeypatch.setattr(
- module.DialogService,
- "query",
- lambda **_kwargs: [SimpleNamespace(kb_ids=[], llm_id="chat-model", tenant_id="tenant-1")],
- )
-
- async def fake_async_chat(_dia, _msg, _stream, **_kwargs):
- yield {"answer": "world", "reference": {}}
-
- monkeypatch.setattr(module, "async_chat", fake_async_chat)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.openai_chat_completions)("chat-1"))
- assert res["choices"][0]["message"]["content"] == "world"
-
-
-def test_openai_defaults_to_nonstream_when_stream_omitted_unit(monkeypatch):
- """Omitted stream must default to false (OpenAI API compat), not SSE."""
- module = _load_openai_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "num_tokens_from_string", lambda text: len(text or ""))
- monkeypatch.setattr(
- module.DialogService,
- "query",
- lambda **_kwargs: [SimpleNamespace(kb_ids=[], llm_id="chat-model", tenant_id="tenant-1")],
- )
-
- stream_flags = []
-
- async def fake_async_chat(_dia, _msg, stream, **_kwargs):
- stream_flags.append(stream)
- yield {"answer": "hello", "reference": {}}
-
- monkeypatch.setattr(module, "async_chat", fake_async_chat)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hi"}],
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.openai_chat_completions)("chat-1"))
- assert stream_flags == [False]
- assert isinstance(res, dict)
- assert res["object"] == "chat.completion"
- assert res["choices"][0]["message"]["content"] == "hello"
-
-
-def test_openai_array_text_content_normalized_unit(monkeypatch):
- """OpenAI-style array content with text parts must not crash async_chat."""
- module = _load_openai_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "num_tokens_from_string", lambda text: len(text or ""))
- monkeypatch.setattr(
- module.DialogService,
- "query",
- lambda **_kwargs: [SimpleNamespace(kb_ids=[], llm_id="chat-model", tenant_id="tenant-1")],
- )
-
- captured_msg = []
-
- async def fake_async_chat(_dia, msg, _stream, **_kwargs):
- captured_msg.append(msg)
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module, "async_chat", fake_async_chat)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "model": "model",
- "stream": False,
- "messages": [
- {
- "role": "user",
- "content": [
- {"type": "text", "text": "Hello"},
- {"type": "text", "text": "World"},
- ],
- }
- ],
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.openai_chat_completions)("chat-1"))
- assert captured_msg[0][0]["content"] == "Hello\nWorld"
- assert res["choices"][0]["message"]["content"] == "ok"
-
-
-def test_openai_invalid_message_content_type_unit(monkeypatch):
- module = _load_openai_api_module(monkeypatch)
-
- monkeypatch.setattr(
- module.DialogService,
- "query",
- lambda **_kwargs: [SimpleNamespace(kb_ids=[], llm_id="chat-model", tenant_id="tenant-1")],
- )
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "model": "model",
- "messages": [{"role": "user", "content": 12345}],
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.openai_chat_completions)("chat-1"))
- assert "messages[].content must be a string or an array of content parts." in res["message"]
-
-
-def test_openai_nonstream_forwards_generation_params_unit(monkeypatch):
- module = _load_openai_api_module(monkeypatch)
-
- base_llm_setting = {"temperature": 0.7, "model_type": "chat"}
- dia = SimpleNamespace(
- kb_ids=[],
- llm_id="chat-model",
- tenant_id="tenant-1",
- llm_setting=base_llm_setting,
- )
- captured = {}
-
- monkeypatch.setattr(module, "num_tokens_from_string", lambda text: len(text or ""))
- monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [dia])
-
- async def fake_async_chat(captured_dia, _msg, _stream, **_kwargs):
- captured["llm_setting"] = dict(captured_dia.llm_setting)
- yield {"answer": "world", "reference": {}}
-
- monkeypatch.setattr(module, "async_chat", fake_async_chat)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "model": "model",
- "messages": [{"role": "user", "content": "hello"}],
- "stream": False,
- "temperature": 0,
- "top_p": 0,
- "frequency_penalty": 0,
- "presence_penalty": 0,
- "max_tokens": 0,
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.openai_chat_completions)("chat-1"))
-
- assert res["choices"][0]["message"]["content"] == "world"
- assert captured["llm_setting"] == {
- "temperature": 0,
- "model_type": "chat",
- "top_p": 0,
- "frequency_penalty": 0,
- "presence_penalty": 0,
- "max_tokens": 0,
- }
- assert base_llm_setting == {"temperature": 0.7, "model_type": "chat"}
-
-
-def test_agents_openai_compatibility_unit(monkeypatch):
- module = _load_agent_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "Response", _StubResponse)
- monkeypatch.setattr(module, "jsonify", lambda payload: payload)
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"openai-compatible": True}))
- res = _run(inspect.unwrap(module.agent_chat_completion)("tenant-1"))
- assert "`agent_id` is required." in res["message"]
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"agent_id": "agent-1", "openai-compatible": True, "model": "model", "messages": []}),
- )
- res = _run(inspect.unwrap(module.agent_chat_completion)("tenant-1"))
- assert "at least one message" in res["message"]
-
- captured_calls = []
-
- async def _completion_openai_stream(*args, **kwargs):
- captured_calls.append((args, kwargs))
- yield "data:stream"
-
- monkeypatch.setattr(module, "completion_openai", _completion_openai_stream)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "agent_id": "agent-1",
- "openai-compatible": True,
- "model": "model",
- "messages": [
- {"role": "assistant", "content": "preface"},
- {"role": "user", "content": "latest question"},
- ],
- "stream": True,
- "metadata": {"id": "meta-session"},
- }
- ),
- )
- resp = _run(inspect.unwrap(module.agent_chat_completion)("tenant-1"))
- assert isinstance(resp, _StubResponse)
- assert resp.headers.get("Content-Type") == "text/event-stream; charset=utf-8"
- _run(_collect_stream(resp.body))
- assert captured_calls[-1][0][2] == "latest question"
-
- async def _completion_openai_nonstream(*args, **kwargs):
- captured_calls.append((args, kwargs))
- yield {"id": "non-stream"}
-
- monkeypatch.setattr(module, "completion_openai", _completion_openai_nonstream)
- monkeypatch.setattr(module.API4ConversationService, "get_by_id", lambda _session_id: (True, SimpleNamespace(dialog_id="agent-1")))
- monkeypatch.setattr(module.UserCanvasService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "agent_id": "agent-1",
- "openai-compatible": True,
- "model": "model",
- "messages": [
- {"role": "user", "content": "first"},
- {"role": "assistant", "content": "middle"},
- {"role": "user", "content": "final user"},
- ],
- "stream": False,
- "session_id": "session-1",
- "temperature": 0.5,
- }
- ),
- )
- res = _run(inspect.unwrap(module.agent_chat_completion)("tenant-1"))
- assert res["id"] == "non-stream"
- assert captured_calls[-1][0][2] == "final user"
- assert captured_calls[-1][1]["stream"] is False
- assert captured_calls[-1][1]["session_id"] == "session-1"
-
-
-def test_agent_completions_stream_and_nonstream_unit(monkeypatch):
- module = _load_agent_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "Response", _StubResponse)
- monkeypatch.setattr(module.API4ConversationService, "get_by_id", lambda _session_id: (True, SimpleNamespace(dialog_id="agent-1")))
- monkeypatch.setattr(module.UserCanvasService, "accessible", lambda *_args, **_kwargs: True)
-
- async def _agent_stream(*_args, **_kwargs):
- yield "data:not-json"
- yield "data:" + json.dumps(
- {
- "event": "node_finished",
- "data": {"component_id": "c1", "outputs": {"structured": {"alpha": 1}}},
- }
- )
- yield "data:" + json.dumps(
- {
- "event": "node_finished",
- "data": {"component_id": "c2", "outputs": {"structured": {}}},
- }
- )
- yield "data:" + json.dumps({"event": "other", "data": {}})
- yield "data:" + json.dumps({"event": "message", "data": {"content": "hello"}})
-
- monkeypatch.setattr(module, "agent_completion", _agent_stream)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "agent_id": "agent-1",
- "session_id": "session-1",
- "stream": True,
- "return_trace": True,
- }
- ),
- )
-
- resp = _run(inspect.unwrap(module.agent_chat_completion)("tenant-1"))
- chunks = _run(_collect_stream(resp.body))
- assert resp.headers.get("Content-Type") == "text/event-stream; charset=utf-8"
- assert any('"trace"' in chunk for chunk in chunks)
- assert any("hello" in chunk for chunk in chunks)
- assert chunks[-1].strip() == "data:[DONE]"
-
- async def _agent_nonstream(*_args, **_kwargs):
- yield "data:" + json.dumps({"event": "message", "data": {"content": "A", "reference": {"doc": "r"}}})
- yield "data:" + json.dumps(
- {
- "event": "node_finished",
- "data": {"component_id": "c2", "outputs": {"structured": {"foo": "bar"}}},
- }
- )
- yield "data:" + json.dumps(
- {
- "event": "node_finished",
- "data": {"component_id": "c3", "outputs": {"structured": {"baz": 1}}},
- }
- )
- yield "data:" + json.dumps(
- {
- "event": "node_finished",
- "data": {"component_id": "c4", "outputs": {"structured": {}}},
- }
- )
-
- monkeypatch.setattr(module, "agent_completion", _agent_nonstream)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "agent_id": "agent-1",
- "session_id": "session-1",
- "stream": False,
- "return_trace": True,
- }
- ),
- )
- res = _run(inspect.unwrap(module.agent_chat_completion)("tenant-1"))
- assert res["data"]["data"]["content"] == "A"
- assert res["data"]["data"]["reference"] == {"doc": "r"}
- assert res["data"]["data"]["structured"] == {
- "c2": {"foo": "bar"},
- "c3": {"baz": 1},
- "c4": {},
- }
- assert [item["component_id"] for item in res["data"]["data"]["trace"]] == ["c2", "c3", "c4"]
-
-
-class _FakeUploadFileField:
- def __init__(self, filename: str):
- self.filename = filename
-
-
-class _FakeRequestFiles:
- def __init__(self, filenames: list[str]):
- self._filenames = filenames
-
- def get(self, key, default=None):
- if key == "file" and self._filenames:
- return _FakeUploadFileField(self._filenames[0])
- return default
-
- def getlist(self, key):
- if key == "file":
- return [_FakeUploadFileField(n) for n in self._filenames]
- return []
-
-
-def test_agent_file_download_and_upload_unit(monkeypatch):
- module = _load_agent_api_module(monkeypatch)
- monkeypatch.setattr(module, "Response", _StubResponse)
-
- get_blob_calls = []
-
- def _get_blob(tenant_id, file_id):
- get_blob_calls.append((tenant_id, file_id))
- return b"file-bytes"
-
- monkeypatch.setattr(module.FileService, "get_blob", _get_blob)
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({"id": "doc-99"})))
-
- resp = _run(inspect.unwrap(module.download_agent_file)("tenant-1"))
- assert isinstance(resp, _StubResponse)
- assert resp.body == b"file-bytes"
- assert get_blob_calls == [("tenant-1", "doc-99")]
-
- upload_calls = []
-
- def _upload_info(tenant_id, file_obj, url=None):
- upload_calls.append((tenant_id, getattr(file_obj, "filename", None), url))
- return {"id": tenant_id, "file": getattr(file_obj, "filename", None), "url": url}
-
- monkeypatch.setattr(module.FileService, "upload_info", _upload_info)
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=_Args({"url": "https://example.com/a.png"}),
- files=_AwaitableValue(_FakeRequestFiles(["one.png"])),
- ),
- )
- res = _run(
- inspect.unwrap(module.upload_agent_file)(
- agent_id="agent-1",
- tenant_id="tenant-1",
- )
- )
- assert res["code"] == 0
- assert res["data"]["file"] == "one.png"
- assert upload_calls == [("tenant-1", "one.png", "https://example.com/a.png")]
-
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=_Args({}),
- files=_AwaitableValue(_FakeRequestFiles(["a.png", "b.png"])),
- ),
- )
- upload_calls.clear()
- res = _run(
- inspect.unwrap(module.upload_agent_file)(
- agent_id="agent-1",
- tenant_id="tenant-1",
- )
- )
- assert res["code"] == 0
- assert len(res["data"]) == 2
- assert set(upload_calls) == {
- ("tenant-1", "a.png", None),
- ("tenant-1", "b.png", None),
- }
-
- def _boom(*_a, **_k):
- raise ValueError("upload failed")
-
- monkeypatch.setattr(module.FileService, "upload_info", _boom)
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(
- args=_Args({}),
- files=_AwaitableValue(_FakeRequestFiles(["bad.png"])),
- ),
- )
- res = _run(
- inspect.unwrap(module.upload_agent_file)(
- agent_id="agent-1",
- tenant_id="tenant-1",
- )
- )
- assert res["code"] != 0
-
- monkeypatch.setattr(module.UserCanvasService, "accessible", lambda *_a, **_k: False)
- res = _run(module.upload_agent_file(agent_id="agent-1"))
- assert res["code"] == module.RetCode.OPERATING_ERROR
- assert "permission" in res["message"].lower()
-
-
-def test_delete_routes_partial_duplicate_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
-
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [SimpleNamespace(id="agent-1")])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
- res = _run(inspect.unwrap(module.delete_agent_session)("tenant-1", "agent-1"))
- assert res["code"] == 0
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"ids": ["session-1"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
-
- def _agent_query(**kwargs):
- if "id" not in kwargs:
- return [SimpleNamespace(id="session-1")]
- if kwargs["id"] == "session-1":
- return [SimpleNamespace(id="session-1")]
- return []
-
- monkeypatch.setattr(module.API4ConversationService, "query", _agent_query)
- monkeypatch.setattr(module.API4ConversationService, "delete_by_id", lambda *_args, **_kwargs: True)
- res = _run(inspect.unwrap(module.delete_agent_session)("tenant-1", "agent-1"))
- assert res["code"] == 0
-
-
-def test_delete_agent_session_error_matrix_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
-
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [SimpleNamespace(id="agent-1")])
- monkeypatch.setattr(module.API4ConversationService, "delete_by_id", lambda *_args, **_kwargs: True)
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"ids": ["ok", "missing"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (ids, []))
-
- def _query_partial(**kwargs):
- if "id" not in kwargs:
- return [SimpleNamespace(id="ok"), SimpleNamespace(id="missing")]
- if kwargs["id"] == "ok":
- return [SimpleNamespace(id="ok")]
- return []
-
- monkeypatch.setattr(module.API4ConversationService, "query", _query_partial)
- res = _run(inspect.unwrap(module.delete_agent_session)("tenant-1", "agent-1"))
- assert res["data"]["success_count"] == 1
- assert res["data"]["errors"] == ["The agent doesn't own the session missing"]
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"ids": ["missing"]}))
-
- def _query_all_failed(**kwargs):
- if "id" not in kwargs:
- return [SimpleNamespace(id="missing")]
- return []
-
- monkeypatch.setattr(module.API4ConversationService, "query", _query_all_failed)
- res = _run(inspect.unwrap(module.delete_agent_session)("tenant-1", "agent-1"))
- assert res["message"] == "The agent doesn't own the session missing"
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"ids": ["ok", "ok"]}))
- monkeypatch.setattr(module, "check_duplicate_ids", lambda ids, _kind: (["ok"], ["Duplicate session ids: ok"]))
-
- def _query_duplicate(**kwargs):
- if "id" not in kwargs:
- return [SimpleNamespace(id="ok")]
- if kwargs["id"] == "ok":
- return [SimpleNamespace(id="ok")]
- return []
-
- monkeypatch.setattr(module.API4ConversationService, "query", _query_duplicate)
- res = _run(inspect.unwrap(module.delete_agent_session)("tenant-1", "agent-1"))
- assert res["data"]["success_count"] == 1
- assert res["data"]["errors"] == ["Duplicate session ids: ok"]
-
-
- ("handler_name", "args"),
- [
- ("chatbot_completions", ("dialog-1",)),
- ("chatbots_inputs", ("dialog-1",)),
- ("agent_bot_completions", ("agent-1",)),
- ("begin_inputs", ("agent-1",)),
- ("ask_about_embedded", ()),
- ("retrieval_test_embedded", ()),
- ("related_questions_embedded", ()),
- ("detail_share_embedded", ()),
- ("mindmap", ()),
- ],
-)
-def test_sdk_session_routes_missing_authorization_unit(monkeypatch, handler_name, args):
- module = _load_session_module(monkeypatch)
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={}, args={"search_id": "search-1"}))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
-
- handler = inspect.unwrap(getattr(module, handler_name))
- res = _run(handler(*args))
-
- assert res["message"] == "Authorization is not valid!"
-
-
-def test_chatbot_routes_auth_stream_nonstream_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
- monkeypatch.setattr(module, "Response", _StubResponse)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
- res = _run(inspect.unwrap(module.chatbot_completions)("dialog-1"))
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer bad"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(inspect.unwrap(module.chatbot_completions)("dialog-1"))
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (False, None))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"stream": False}))
- res = _run(inspect.unwrap(module.chatbot_completions)("dialog-unauthorized"))
- assert res["message"] == "Authentication error: no access to this chatbot!"
-
- stream_calls = []
-
- async def _iframe_stream(dialog_id, **req):
- stream_calls.append((dialog_id, dict(req)))
- yield "data:stream-chunk"
-
- monkeypatch.setattr(module, "iframe_completion", _iframe_stream)
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(
- module.DialogService,
- "get_by_id",
- lambda _dialog_id: (True, SimpleNamespace(id="dialog-1", tenant_id="tenant-1", status="1")),
- )
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"stream": True}))
- resp = _run(inspect.unwrap(module.chatbot_completions)("dialog-1"))
- assert isinstance(resp, _StubResponse)
- assert resp.headers.get("Content-Type") == "text/event-stream; charset=utf-8"
- _run(_collect_stream(resp.body))
- assert stream_calls[-1][0] == "dialog-1"
- assert stream_calls[-1][1]["quote"] is False
- assert stream_calls[-1][1]["tenant_id"] == "tenant-1"
-
- async def _iframe_nonstream(_dialog_id, **_req):
- yield {"answer": "non-stream"}
-
- monkeypatch.setattr(module, "iframe_completion", _iframe_nonstream)
- monkeypatch.setattr(
- module.DialogService,
- "get_by_id",
- lambda _dialog_id: (True, SimpleNamespace(id="dialog-1", tenant_id="tenant-1", status="1")),
- )
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"stream": False, "quote": True}))
- res = _run(inspect.unwrap(module.chatbot_completions)("dialog-1"))
- assert res["data"]["answer"] == "non-stream"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-1"))
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer invalid"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-1"))
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (False, None))
-
- res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-404"))
- assert res["message"] == "Authentication error: no access to this chatbot!"
-
- # Happy path: valid token + owned dialog -> correct payload
- stub_dialog = SimpleNamespace(
- name="My Bot",
- icon="avatar.png",
- tenant_id="tenant-1",
- status="1",
- prompt_config={"prologue": "Hello!", "tavily_api_key": "key123"},
- )
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, stub_dialog))
- res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-404"))
- assert res["code"] == 0
- assert res["data"]["title"] == "My Bot"
- assert res["data"]["avatar"] == "avatar.png"
- assert res["data"]["prologue"] == "Hello!"
- assert res["data"]["has_tavily_key"] is True
- assert res["data"]["has_web_search_provider"] is True
-
- # Explicit Querit configuration also enables the provider-neutral flag.
- querit_dialog = SimpleNamespace(
- name="My Querit Bot",
- icon="avatar.png",
- tenant_id="tenant-1",
- status="1",
- llm_id="",
- prompt_config={
- "prologue": "Hello!",
- "web_search_provider": "querit",
- "querit_api_key": "querit-key123",
- },
- )
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, querit_dialog))
- res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-querit"))
- assert res["code"] == 0
- assert res["data"]["has_web_search_provider"] is True
-
- # Explicit Serply configuration also enables the provider-neutral flag.
- serply_dialog = SimpleNamespace(
- name="My Serply Bot",
- icon="avatar.png",
- tenant_id="tenant-1",
- status="1",
- llm_id="",
- prompt_config={
- "prologue": "Hello!",
- "web_search_provider": "serply",
- "serply_api_key": "serply-key123",
- },
- )
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, serply_dialog))
- res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-serply"))
- assert res["code"] == 0
- assert res["data"]["has_web_search_provider"] is True
-
- # You.com is keyless, so selecting it enables the flag with no key set.
- youcom_dialog = SimpleNamespace(
- name="My You.com Bot",
- icon="avatar.png",
- tenant_id="tenant-1",
- status="1",
- llm_id="",
- prompt_config={
- "prologue": "Hello!",
- "web_search_provider": "youcom",
- },
- )
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, youcom_dialog))
- res = _run(inspect.unwrap(module.chatbots_inputs)("dialog-youcom"))
- assert res["code"] == 0
- assert res["data"]["has_web_search_provider"] is True
-
-
-def test_agentbot_routes_auth_stream_nonstream_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
- monkeypatch.setattr(module, "Response", _StubResponse)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
- res = _run(inspect.unwrap(module.agent_bot_completions)("agent-1"))
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer bad"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(inspect.unwrap(module.agent_bot_completions)("agent-1"))
- assert "API key is invalid" in res["message"]
-
- async def _agent_stream(*_args, **_kwargs):
- yield "data:agent-stream"
-
- monkeypatch.setattr(module, "agent_completion", _agent_stream)
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"stream": True}))
- resp = _run(inspect.unwrap(module.agent_bot_completions)("agent-1"))
- assert isinstance(resp, _StubResponse)
- assert resp.headers.get("Content-Type") == "text/event-stream; charset=utf-8"
- _run(_collect_stream(resp.body))
-
- async def _agent_nonstream(*_args, **_kwargs):
- yield {"answer": "agent-non-stream"}
-
- monkeypatch.setattr(module, "agent_completion", _agent_nonstream)
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"stream": False}))
- res = _run(inspect.unwrap(module.agent_bot_completions)("agent-1"))
- assert res["data"]["answer"] == "agent-non-stream"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- res = _run(inspect.unwrap(module.begin_inputs)("agent-1"))
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer bad"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(inspect.unwrap(module.begin_inputs)("agent-1"))
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _agent_id: (False, None))
- res = _run(inspect.unwrap(module.begin_inputs)("agent-404"))
- assert res["message"] == "Can't find agent by ID: agent-404"
-
-
-def test_searchbots_ask_embedded_auth_and_stream_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
- monkeypatch.setattr(module, "Response", _StubResponse)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- res = _run(inspect.unwrap(module.ask_about_embedded)())
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer bad"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(inspect.unwrap(module.ask_about_embedded)())
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"question": "embedded q", "kb_ids": ["kb-1"], "search_id": "search-1"}),
- )
- monkeypatch.setattr(module.SearchService, "get_detail", lambda _search_id: {"search_config": {"mode": "test"}})
- captured = {}
-
- async def _embedded_async_ask(question, kb_ids, uid, search_config=None):
- captured["question"] = question
- captured["kb_ids"] = kb_ids
- captured["uid"] = uid
- captured["search_config"] = search_config
- yield {"answer": "embedded-answer"}
- raise RuntimeError("embedded stream boom")
-
- monkeypatch.setattr(module, "async_ask", _embedded_async_ask)
- resp = _run(inspect.unwrap(module.ask_about_embedded)())
- assert isinstance(resp, _StubResponse)
- assert resp.headers.get("Content-Type") == "text/event-stream; charset=utf-8"
- chunks = _run(_collect_stream(resp.body))
- assert any('"answer": "embedded-answer"' in chunk for chunk in chunks)
- assert any('"code": 500' in chunk and "**ERROR**: embedded stream boom" in chunk for chunk in chunks)
- assert '"data": true' in chunks[-1].lower()
- assert captured["search_config"] == {"mode": "test"}
-
-
-def test_searchbots_retrieval_test_embedded_matrix_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
- handler = inspect.unwrap(module.retrieval_test_embedded)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- res = _run(handler())
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer invalid"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(handler())
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"kb_id": [], "question": "q"}))
- res = _run(handler())
- assert res["message"] == "Please specify dataset firstly."
-
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="")])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"kb_id": "kb-1", "question": "q"}))
- res = _run(handler())
- assert res["message"] == "permission denined."
-
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"kb_id": ["kb-no-access"], "question": "q"}))
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-a")])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [])
- res = _run(handler())
- assert "Only owner of dataset authorized for this operation." in res["message"]
-
- llm_calls = []
-
- def _fake_llm_bundle(tenant_id, model_config, *args, **kwargs):
- # Extract llm_type from model_config for comparison
- llm_type = model_config.get("model_type") if isinstance(model_config, dict) else model_config
- llm_name = model_config.get("llm_name") if isinstance(model_config, dict) else None
- llm_calls.append((tenant_id, llm_type, llm_name, args, kwargs))
- return SimpleNamespace(tenant_id=tenant_id, llm_type=llm_type, llm_name=llm_name, args=args, kwargs=kwargs)
-
- monkeypatch.setattr(module, "LLMBundle", _fake_llm_bundle)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"kb_id": "kb-1", "question": "q", "meta_data_filter": {"method": "auto"}}),
- )
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kb_ids: [{"id": "doc-1"}])
-
- async def _apply_filter(_meta_filter, _metas, _question, _chat_mdl, _local_doc_ids, **kwargs):
- assert kwargs["kb_ids"] == ["kb-1"]
- assert callable(kwargs["metas_loader"])
- return ["doc-filtered"]
-
- monkeypatch.setattr(module, "apply_meta_data_filter", _apply_filter)
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-a")])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
- res = _run(handler())
- assert res["message"] == "Knowledgebase not found!"
- assert any(call[1] == module.LLMType.CHAT for call in llm_calls)
-
- llm_calls.clear()
- retrieval_capture = {}
-
- async def _fake_retrieval(
- question,
- embd_mdl,
- tenant_ids,
- kb_ids,
- page,
- size,
- similarity_threshold,
- vector_similarity_weight,
- top,
- local_doc_ids,
- rerank_mdl=None,
- highlight=None,
- rank_feature=None,
- ):
- retrieval_capture.update(
- {
- "question": question,
- "embd_mdl": embd_mdl,
- "tenant_ids": tenant_ids,
- "kb_ids": kb_ids,
- "page": page,
- "size": size,
- "similarity_threshold": similarity_threshold,
- "vector_similarity_weight": vector_similarity_weight,
- "top": top,
- "local_doc_ids": local_doc_ids,
- "rerank_mdl": rerank_mdl,
- "highlight": highlight,
- "rank_feature": rank_feature,
- }
- )
- return {"chunks": [{"id": "chunk-1", "doc_id": "doc-1", "kb_id": "kb-1", "vector": [0.1]}]}
-
- async def _translate(_tenant_id, _chat_id, question, _langs):
- return question + "-translated"
-
- monkeypatch.setattr(module, "cross_languages", _translate)
- monkeypatch.setattr(module, "label_question", lambda _question, _kbs: ["label-1"])
- monkeypatch.setattr(module.settings, "retriever", SimpleNamespace(retrieval=_fake_retrieval))
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "kb_id": "kb-1",
- "question": "translated-q",
- "doc_ids": ["doc-seed"],
- "cross_languages": ["es"],
- "search_id": "search-1",
- }
- ),
- )
- monkeypatch.setattr(
- module.SearchService,
- "get_detail",
- lambda _search_id: {
- "search_config": {
- "meta_data_filter": {"method": "auto"},
- "chat_id": "chat-for-filter",
- "similarity_threshold": 0.42,
- "vector_similarity_weight": 0.8,
- "top_k": 7,
- "rerank_id": "reranker-model",
- "reference_metadata": {"include": True, "fields": ["author"]},
- }
- },
- )
- monkeypatch.setattr(module.DocMetadataService, "get_flatted_meta_by_kbs", lambda _kb_ids: [{"id": "doc-2"}])
- monkeypatch.setattr(
- module.DocMetadataService,
- "get_metadata_for_documents",
- lambda _doc_ids, _kb_id: {"doc-1": {"author": "alice", "year": "2025"}},
- )
- monkeypatch.setattr(module, "apply_meta_data_filter", _apply_filter)
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-a")])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_by_id",
- lambda _kb_id: (True, SimpleNamespace(tenant_id="tenant-kb", embd_id="embd-model", tenant_embd_id=None)),
- )
- res = _run(handler())
- assert res["code"] == 0
- assert res["data"]["labels"] == ["label-1"]
- assert "vector" not in res["data"]["chunks"][0]
- assert retrieval_capture["kb_ids"] == ["kb-1"]
- assert retrieval_capture["tenant_ids"] == ["tenant-a"]
- assert retrieval_capture["question"] == "translated-q-translated"
- assert retrieval_capture["similarity_threshold"] == 0.42
- assert retrieval_capture["vector_similarity_weight"] == 0.8
- assert retrieval_capture["knn_top_k"] == 7
- assert retrieval_capture["local_doc_ids"] == ["doc-filtered"]
- assert retrieval_capture["rank_feature"] == ["label-1"]
- assert retrieval_capture["rerank_mdl"] is not None
- assert res["data"]["chunks"][0]["document_metadata"]["author"] == "alice"
- assert "year" not in res["data"]["chunks"][0]["document_metadata"]
- assert any(call[1] == module.LLMType.EMBEDDING.value and call[2] == "embd-model" for call in llm_calls)
-
- llm_calls.clear()
-
- async def _fake_keyword_extraction(_chat_mdl, question):
- return f"-{question}-keywords"
-
- async def _fake_kg_retrieval(question, tenant_ids, kb_ids, _embd_mdl, _chat_mdl):
- return {
- "id": "kg-chunk",
- "question": question,
- "tenant_ids": tenant_ids,
- "kb_ids": kb_ids,
- "content_with_weight": 1,
- "vector": [0.5],
- }
-
- monkeypatch.setattr(module, "keyword_extraction", _fake_keyword_extraction)
- monkeypatch.setattr(module.settings, "kg_retriever", SimpleNamespace(retrieval=_fake_kg_retrieval))
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "kb_id": "kb-1",
- "question": "keyword-q",
- "rerank_id": "manual-reranker",
- "keyword": True,
- "use_kg": True,
- }
- ),
- )
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_by_id",
- lambda _kb_id: (True, SimpleNamespace(tenant_id="tenant-kb", embd_id="embd-model", tenant_embd_id=None)),
- )
- res = _run(handler())
- assert res["code"] == 0
- assert res["data"]["chunks"][0]["id"] == "kg-chunk"
- assert all("vector" not in chunk for chunk in res["data"]["chunks"])
- assert any(call[1] == module.LLMType.RERANK.value for call in llm_calls)
-
- async def _raise_not_found(*_args, **_kwargs):
- raise RuntimeError("x not_found y")
-
- monkeypatch.setattr(module.settings, "retriever", SimpleNamespace(retrieval=_raise_not_found))
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"kb_id": "kb-1", "question": "q"}),
- )
- res = _run(handler())
- assert res["message"] == "No chunk found! Check the chunk status please!"
-
-
-def test_searchbots_related_questions_embedded_matrix_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
- handler = inspect.unwrap(module.related_questions_embedded)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- res = _run(handler())
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer bad"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(handler())
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="")])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"question": "q"}))
- res = _run(handler())
- assert res["message"] == "permission denined."
-
- captured = {}
-
- class _FakeChatBundle:
- async def async_chat(self, prompt, messages, options):
- captured["prompt"] = prompt
- captured["messages"] = messages
- captured["options"] = options
- return "1. Alpha\n2. Beta\nignored"
-
- def _fake_bundle(*args, **_kwargs):
- captured["bundle_args"] = args
- return _FakeChatBundle()
-
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"question": "solar", "search_id": "search-1"}),
- )
- monkeypatch.setattr(
- module.SearchService,
- "get_detail",
- lambda _search_id: {"search_config": {"chat_id": "chat-x", "llm_setting": {"temperature": 0.2}}},
- )
- monkeypatch.setattr(module, "LLMBundle", _fake_bundle)
- res = _run(handler())
- assert res["code"] == 0
- assert res["data"] == ["Alpha", "Beta"]
- # LLMBundle is called with (tenant_id, model_config)
- # model_config is a dict with model_type, llm_name, etc.
- assert captured["bundle_args"][0] == "tenant-1"
- assert captured["bundle_args"][1].get("model_type") == module.LLMType.CHAT
- assert captured["bundle_args"][1].get("llm_name") == "chat-x"
- assert captured["options"] == {"temperature": 0.2}
- assert "Keywords: solar" in captured["messages"][0]["content"]
-
-
-def test_searchbots_detail_share_embedded_matrix_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
- handler = inspect.unwrap(module.detail_share_embedded)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}, args={"search_id": "s-1"}))
- res = _run(handler())
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer bad"}, args={"search_id": "s-1"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(handler())
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}, args={"search_id": "s-1"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="")])
- res = _run(handler())
- assert res["message"] == "permission denined."
-
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-a")])
- monkeypatch.setattr(module.SearchService, "query", lambda **_kwargs: [])
- res = _run(handler())
- assert res["code"] == module.RetCode.OPERATING_ERROR
- assert "Has no permission for this operation." in res["message"]
-
- monkeypatch.setattr(module.SearchService, "query", lambda **_kwargs: [SimpleNamespace(id="s-1")])
- monkeypatch.setattr(module.SearchService, "get_detail", lambda _sid: None)
- res = _run(handler())
- assert res["message"] == "Can't find this Search App!"
-
- monkeypatch.setattr(module.SearchService, "get_detail", lambda _sid: {"id": "s-1", "name": "search-app"})
- res = _run(handler())
- assert res["code"] == 0
- assert res["data"]["id"] == "s-1"
-
-
-def test_searchbots_mindmap_embedded_matrix_unit(monkeypatch):
- module = _load_session_module(monkeypatch)
- handler = inspect.unwrap(module.mindmap)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer"}))
- res = _run(handler())
- assert res["message"] == "Authorization is not valid!"
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer bad"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [])
- res = _run(handler())
- assert "API key is invalid" in res["message"]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(headers={"Authorization": "Bearer ok"}))
- monkeypatch.setattr(module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"question": "q", "kb_ids": ["kb-1"]}))
-
- captured = {}
-
- async def _gen_ok(question, kb_ids, tenant_id, search_config):
- captured["params"] = (question, kb_ids, tenant_id, search_config)
- return {"nodes": [question]}
-
- monkeypatch.setattr(module, "gen_mindmap", _gen_ok)
- res = _run(handler())
- assert res["code"] == 0
- assert res["data"] == {"nodes": ["q"]}
- assert captured["params"] == ("q", ["kb-1"], "tenant-1", {})
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"question": "q2", "kb_ids": ["kb-1"], "search_id": "search-1"}),
- )
- monkeypatch.setattr(module.SearchService, "get_detail", lambda _sid: {"search_config": {"mode": "graph"}})
- res = _run(handler())
- assert res["code"] == 0
- assert captured["params"] == ("q2", ["kb-1"], "tenant-1", {"mode": "graph"})
-
- async def _gen_error(*_args, **_kwargs):
- return {"error": "mindmap boom"}
-
- monkeypatch.setattr(module, "gen_mindmap", _gen_error)
- res = _run(handler())
- assert "mindmap boom" in res["message"]
-
-
-def test_build_reference_chunks_metadata_matrix_unit(monkeypatch):
- module = _load_openai_api_module(monkeypatch)
-
- monkeypatch.setattr(module, "chunks_format", lambda _reference: [{"dataset_id": "kb-1", "document_id": "doc-1"}])
- res = module._build_reference_chunks([], include_metadata=False)
- assert res == [{"dataset_id": "kb-1", "document_id": "doc-1"}]
-
- monkeypatch.setattr(module, "chunks_format", lambda _reference: [{"dataset_id": "kb-1"}, {"document_id": "doc-2"}])
- res = module._build_reference_chunks([], include_metadata=True)
- assert all("document_metadata" not in chunk for chunk in res)
-
- monkeypatch.setattr(module, "chunks_format", lambda _reference: [{"dataset_id": "kb-1", "document_id": "doc-1"}])
- monkeypatch.setattr(module.DocMetadataService, "get_metadata_for_documents", lambda _doc_ids, _kb_id: {"doc-1": {"author": "alice"}})
- res = module._build_reference_chunks([], include_metadata=True, metadata_fields=None)
- assert res[0]["document_metadata"] == {"author": "alice"}
-
- res = module._build_reference_chunks([], include_metadata=True, metadata_fields=[])
- assert "document_metadata" not in res[0]
-
- res = module._build_reference_chunks([], include_metadata=True, metadata_fields=[1, None])
- assert "document_metadata" not in res[0]
-
- res = module._build_reference_chunks([], include_metadata=True, metadata_fields="author")
- assert "document_metadata" not in res[0]
-
- source_chunks = [
- {"dataset_id": "kb-1", "document_id": "doc-1"},
- {"dataset_id": "kb-2", "document_id": "doc-2"},
- {"dataset_id": "kb-1", "document_id": "doc-3"},
- {"dataset_id": "kb-1", "document_id": None},
- ]
- monkeypatch.setattr(module, "chunks_format", lambda _reference: [dict(chunk) for chunk in source_chunks])
-
- def _get_metadata(_doc_ids, kb_id):
- if kb_id == "kb-1":
- return {"doc-1": {"author": "alice", "year": 2024}}
- if kb_id == "kb-2":
- return {"doc-2": {"author": "bob", "tag": "rag"}}
- return {}
-
- monkeypatch.setattr(module.DocMetadataService, "get_metadata_for_documents", _get_metadata)
- res = module._build_reference_chunks([], include_metadata=True, metadata_fields=["author", "missing", 3])
- assert res[0]["document_metadata"] == {"author": "alice"}
- assert res[1]["document_metadata"] == {"author": "bob"}
- assert "document_metadata" not in res[2]
- assert "document_metadata" not in res[3]
-
-
-# ---------------------------------------------------------------------------
-# chat_api unit tests — session user-id spoofing fix
-# ---------------------------------------------------------------------------
-
-
-def _load_chat_api_module(monkeypatch):
- """Load api/apps/restful_apis/chat_api.py with all heavy dependencies mocked."""
- repo_root = Path(__file__).resolve().parents[4]
-
- from enum import Enum, StrEnum
-
- class _RetCode(int, Enum):
- SUCCESS = 0
- ARGUMENT_ERROR = 101
- DATA_ERROR = 102
- AUTHENTICATION_ERROR = 109
- SERVER_ERROR = 500
-
- class _StatusEnum(str, Enum):
- VALID = "1"
-
- class _LLMType(StrEnum):
- CHAT = "chat"
- TTS = "tts"
- ASR = "asr"
- RERANK = "rerank"
-
- quart_mod = ModuleType("quart")
- quart_mod.request = SimpleNamespace(args=_Args(), headers={}, files=_AwaitableValue({}), method="POST")
- quart_mod.Response = _StubResponse
- quart_mod.jsonify = lambda payload: payload
- quart_mod.current_app = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- api_apps_mod = ModuleType("api.apps")
- api_apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- api_apps_mod.current_user = SimpleNamespace(id="authenticated-user")
- api_apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", api_apps_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- common_constants_mod = ModuleType("common.constants")
- common_constants_mod.LLMType = _LLMType
- common_constants_mod.RetCode = _RetCode
- common_constants_mod.StatusEnum = _StatusEnum
- monkeypatch.setitem(sys.modules, "common.constants", common_constants_mod)
-
- common_settings_mod = ModuleType("common.settings")
- common_settings_mod.STORAGE_IMPL = SimpleNamespace(rm=lambda *_a, **_k: None)
- common_pkg.settings = common_settings_mod
- monkeypatch.setitem(sys.modules, "common.settings", common_settings_mod)
-
- async def _thread_pool_exec(func, *args, **kwargs):
- """Run func synchronously, standing in for the real async thread-pool executor."""
- return func(*args, **kwargs)
-
- common_misc_mod = ModuleType("common.misc_utils")
- common_misc_mod.get_uuid = lambda: "test-uuid"
- common_misc_mod.thread_pool_exec = _thread_pool_exec
- monkeypatch.setitem(sys.modules, "common.misc_utils", common_misc_mod)
-
- joint_pkg = ModuleType("api.db.joint_services")
- joint_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.joint_services", joint_pkg)
-
- tenant_model_svc = ModuleType("api.db.joint_services.tenant_model_service")
- tenant_model_svc.get_tenant_default_model_by_type = lambda *_a, **_k: {}
- tenant_model_svc.get_model_config_from_provider_instance = lambda **_k: {}
- tenant_model_svc.get_model_config_by_id = lambda **_k: {}
- tenant_model_svc.resolve_model_config = lambda **_k: {}
- tenant_model_svc.resolve_model_id = lambda *_a, **_k: "model-id"
- tenant_model_svc.get_composite_model_name_by_id = lambda *_a, **_k: "composite-model-name"
- tenant_model_svc.get_api_key = lambda **_k: "fake-api-key"
- tenant_model_svc.split_model_name = lambda model_name: (model_name, "", "")
- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_svc)
-
- chunk_feedback_mod = ModuleType("api.db.services.chunk_feedback_service")
- chunk_feedback_mod.ChunkFeedbackService = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "api.db.services.chunk_feedback_service", chunk_feedback_mod)
-
- def _make_conv(conv_id):
- """Return a minimal conversation stub for ConversationService.get_by_id mocks."""
- return SimpleNamespace(
- id=conv_id,
- dialog_id="chat-1",
- message=[],
- reference=[],
- user_id="authenticated-user",
- name="test",
- to_dict=lambda: {
- "id": conv_id,
- "dialog_id": "chat-1",
- "message": [],
- "reference": [],
- "user_id": "authenticated-user",
- "name": "test",
- },
- )
-
- conv_svc_mod = ModuleType("api.db.services.conversation_service")
- conv_svc_mod.ConversationService = SimpleNamespace(
- save=lambda **_k: True,
- get_by_id=lambda _id: (True, _make_conv(_id)),
- query=lambda **_k: [],
- get_list=lambda *_a, **_k: [],
- )
- conv_svc_mod.structure_answer = lambda *_a, **_k: {}
- monkeypatch.setitem(sys.modules, "api.db.services.conversation_service", conv_svc_mod)
-
- dialog_svc_mod = ModuleType("api.db.services.dialog_service")
- dialog_svc_mod.DialogService = SimpleNamespace(
- model=SimpleNamespace(_meta=SimpleNamespace(fields=[])),
- query=lambda **_k: [SimpleNamespace(id="chat-1", icon="")],
- get_by_id=lambda _id: (
- True,
- SimpleNamespace(
- prompt_config={"prologue": ""},
- tenant_id="tenant-1",
- llm_id="model",
- kb_ids=[],
- id=_id,
- ),
- ),
- )
- dialog_svc_mod.async_chat = lambda *_a, **_k: None
- dialog_svc_mod.gen_mindmap = lambda *_a, **_k: None
- dialog_svc_mod.rag_agent = lambda *_a, **_k: None
- monkeypatch.setitem(sys.modules, "api.db.services.dialog_service", dialog_svc_mod)
-
- kb_svc_mod = ModuleType("api.db.services.knowledgebase_service")
- kb_svc_mod.KnowledgebaseService = SimpleNamespace(query=lambda **_k: [], accessible=lambda **_k: True)
- kb_svc_mod.validate_dataset_embedding_models = lambda _kbs: None
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_svc_mod)
-
- class _FakeLLMBundle:
- def __init__(self, *_a, **_k):
- pass
-
- llm_svc_mod = ModuleType("api.db.services.llm_service")
- llm_svc_mod.LLMBundle = _FakeLLMBundle
- llm_svc_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_svc_mod)
-
- search_svc_mod = ModuleType("api.db.services.search_service")
- search_svc_mod.SearchService = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "api.db.services.search_service", search_svc_mod)
-
- tenant_llm_svc_mod = ModuleType("api.db.services.tenant_llm_service")
- tenant_llm_svc_mod.TenantLLMService = SimpleNamespace(get_api_key=lambda **_k: None)
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_svc_mod)
-
- user_svc_mod = ModuleType("api.db.services.user_service")
- user_svc_mod.TenantService = SimpleNamespace(
- get_by_id=lambda _id: (True, SimpleNamespace(id=_id, llm_id="chat-model")),
- get_joined_tenants_by_user_id=lambda _id: [],
- )
- user_svc_mod.UserTenantService = SimpleNamespace(query=lambda **_k: [])
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_svc_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.check_duplicate_ids = lambda ids, _kind: (ids, [])
- api_utils_mod.get_data_error_result = lambda message="Error", code=_RetCode.DATA_ERROR: {"code": code, "message": message}
- api_utils_mod.get_json_result = lambda code=_RetCode.SUCCESS, message="success", data=None: {"code": code, "message": message, "data": data}
- api_utils_mod.get_request_json = lambda: _AwaitableValue({})
- api_utils_mod.server_error_response = lambda e: {"code": _RetCode.SERVER_ERROR, "message": str(e)}
- api_utils_mod.validate_request = lambda *_a, **_k: lambda func: func
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- rag_gen_mod = ModuleType("rag.prompts.generator")
- rag_gen_mod.chunks_format = lambda chunks: chunks
- monkeypatch.setitem(sys.modules, "rag.prompts.generator", rag_gen_mod)
-
- rag_tmpl_mod = ModuleType("rag.prompts.template")
- rag_tmpl_mod.load_prompt = lambda *_a, **_k: ""
- monkeypatch.setitem(sys.modules, "rag.prompts.template", rag_tmpl_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "chat_api.py"
- spec = importlib.util.spec_from_file_location("test_chat_api_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, "test_chat_api_unit_module", module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_create_session_user_id_not_spoofable(monkeypatch):
- """create_session must attribute the new session to current_user, ignoring any user_id in the request body."""
- module = _load_chat_api_module(monkeypatch)
-
- saved_kwargs = {}
-
- def _capture_save(**kwargs):
- """Capture ConversationService.save arguments for later assertion."""
- saved_kwargs.update(kwargs)
- return True
-
- monkeypatch.setattr(module.ConversationService, "save", _capture_save)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"name": "my session", "user_id": "attacker-id"}),
- )
-
- res = _run(inspect.unwrap(module.create_session)("chat-1"))
-
- assert res["code"] == 0, res
- assert saved_kwargs["user_id"] == module.current_user.id
- assert saved_kwargs["user_id"] != "attacker-id"
-
-
-def test_session_completion_user_id_not_spoofable(monkeypatch):
- """session_completion must pass current_user.id to _create_session_for_completion, ignoring user_id in the request."""
- module = _load_chat_api_module(monkeypatch)
-
- captured_user_ids = []
-
- async def _spy_create_session(_chat_id, _dialog, user_id):
- """Record the user_id passed to _create_session_for_completion, then abort early."""
- captured_user_ids.append(user_id)
- raise RuntimeError("stop-sentinel")
-
- monkeypatch.setattr(module, "_create_session_for_completion", _spy_create_session)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "messages": [{"role": "user", "content": "hello"}],
- "chat_id": "chat-1",
- "user_id": "attacker-id",
- "stream": False,
- }
- ),
- )
-
- _run(inspect.unwrap(module.session_completion)())
-
- assert captured_user_ids == [module.current_user.id]
-
-
-def test_session_completion_uses_server_history_by_default(monkeypatch):
- """Session chat completions should append only the latest user message to stored history by default."""
- module = _load_chat_api_module(monkeypatch)
-
- captured_messages = []
- conv = SimpleNamespace(
- id="session-1",
- dialog_id="chat-1",
- message=[
- {"role": "assistant", "content": "prologue"},
- {"role": "user", "content": "server old question", "id": "old-user"},
- {"role": "assistant", "content": "server old answer", "id": "old-user"},
- ],
- reference=[],
- user_id="authenticated-user",
- name="test",
- )
- conv.to_dict = lambda: {
- "id": conv.id,
- "dialog_id": conv.dialog_id,
- "message": conv.message,
- "reference": conv.reference,
- "user_id": conv.user_id,
- "name": conv.name,
- }
-
- async def _fake_async_chat(_dia, messages, stream=True, **_kwargs):
- captured_messages.append([dict(message) for message in messages])
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
- monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
- monkeypatch.setattr(module, "async_chat", _fake_async_chat)
- monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "chat_id": "chat-1",
- "session_id": "session-1",
- "stream": False,
- "messages": [
- {"role": "user", "content": "client old question", "id": "client-old"},
- {"role": "assistant", "content": "client old answer", "id": "client-old"},
- {"role": "user", "content": "latest question", "id": "latest"},
- ],
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.session_completion)())
-
- assert res["code"] == 0, res
- assert [message["content"] for message in captured_messages[0]] == [
- "server old question",
- "server old answer",
- "latest question",
- ]
- assert [message["content"] for message in conv.message] == [
- "prologue",
- "server old question",
- "server old answer",
- "latest question",
- ]
-
-
-def test_session_completion_preserves_zero_generation_params(monkeypatch):
- module = _load_chat_api_module(monkeypatch)
-
- captured = {}
-
- async def _fake_async_chat(dia, _messages, stream=True, **_kwargs):
- captured["llm_setting"] = dict(dia.llm_setting)
- captured["kwargs"] = dict(_kwargs)
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module, "async_chat", _fake_async_chat)
- monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "stream": False,
- "messages": [{"role": "user", "content": "latest question"}],
- "temperature": 0,
- "top_p": 0,
- "frequency_penalty": 0,
- "presence_penalty": 0,
- "max_tokens": 0,
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.session_completion)())
-
- assert res["code"] == 0, res
- assert captured["llm_setting"] == {
- "temperature": 0,
- "top_p": 0,
- "frequency_penalty": 0,
- "presence_penalty": 0,
- "max_tokens": 0,
- }
- assert not {
- "temperature",
- "top_p",
- "frequency_penalty",
- "presence_penalty",
- "max_tokens",
- }.intersection(captured["kwargs"])
-
-
-def test_session_completion_merges_generation_params_for_existing_chat(monkeypatch):
- module = _load_chat_api_module(monkeypatch)
-
- base_llm_setting = {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
- dia = SimpleNamespace(
- id="chat-1",
- tenant_id="tenant-1",
- llm_id="model",
- llm_setting=base_llm_setting,
- prompt_config={"prologue": ""},
- kb_ids=[],
- )
- conv = SimpleNamespace(
- id="session-1",
- dialog_id="chat-1",
- message=[],
- reference=[],
- user_id="authenticated-user",
- name="test",
- )
- conv.to_dict = lambda: {
- "id": conv.id,
- "dialog_id": conv.dialog_id,
- "message": conv.message,
- "reference": conv.reference,
- "user_id": conv.user_id,
- "name": conv.name,
- }
- captured = {}
-
- async def _fake_async_chat(captured_dia, _messages, stream=True, **_kwargs):
- captured["llm_setting"] = dict(captured_dia.llm_setting)
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, dia))
- monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
- monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
- monkeypatch.setattr(module, "async_chat", _fake_async_chat)
- monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "chat_id": "chat-1",
- "session_id": "session-1",
- "stream": False,
- "messages": [{"role": "user", "content": "latest question"}],
- "temperature": 0,
- "presence_penalty": 0,
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.session_completion)())
-
- assert res["code"] == 0, res
- assert captured["llm_setting"] == {
- "temperature": 0,
- "top_p": 0.3,
- "custom": "keep",
- "presence_penalty": 0,
- }
- assert base_llm_setting == {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
-
-
-def test_session_completion_keeps_dialog_settings_without_overrides(monkeypatch):
- module = _load_chat_api_module(monkeypatch)
-
- base_llm_setting = {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
- dia = SimpleNamespace(
- id="chat-1",
- tenant_id="tenant-1",
- llm_id="model",
- llm_setting=base_llm_setting,
- prompt_config={"prologue": ""},
- kb_ids=[],
- )
- conv = SimpleNamespace(
- id="session-1",
- dialog_id="chat-1",
- message=[],
- reference=[],
- user_id="authenticated-user",
- name="test",
- )
- conv.to_dict = lambda: {
- "id": conv.id,
- "dialog_id": conv.dialog_id,
- "message": conv.message,
- "reference": conv.reference,
- "user_id": conv.user_id,
- "name": conv.name,
- }
- captured = {}
-
- async def _fake_async_chat(captured_dia, _messages, stream=True, **_kwargs):
- captured["llm_setting"] = dict(captured_dia.llm_setting)
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, dia))
- monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
- monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
- monkeypatch.setattr(module, "async_chat", _fake_async_chat)
- monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "chat_id": "chat-1",
- "session_id": "session-1",
- "stream": False,
- "messages": [{"role": "user", "content": "latest question"}],
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.session_completion)())
-
- assert res["code"] == 0, res
- # No per-request generation params were sent, so the merge is a no-op and the
- # dialog's stored settings must be passed through untouched.
- assert captured["llm_setting"] == {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
- assert base_llm_setting == {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
-
-
-def test_session_completion_assigns_default_model_then_merges_for_existing_chat(monkeypatch):
- module = _load_chat_api_module(monkeypatch)
-
- base_llm_setting = {"top_p": 0.3, "custom": "keep"}
- dia = SimpleNamespace(
- id="chat-1",
- tenant_id="tenant-1",
- llm_id="",
- llm_setting=base_llm_setting,
- prompt_config={"prologue": ""},
- kb_ids=[],
- )
- conv = SimpleNamespace(
- id="session-1",
- dialog_id="chat-1",
- message=[],
- reference=[],
- user_id="authenticated-user",
- name="test",
- )
- conv.to_dict = lambda: {
- "id": conv.id,
- "dialog_id": conv.dialog_id,
- "message": conv.message,
- "reference": conv.reference,
- "user_id": conv.user_id,
- "name": conv.name,
- }
- captured = {}
-
- async def _fake_async_chat(captured_dia, _messages, stream=True, **_kwargs):
- captured["llm_id"] = captured_dia.llm_id
- captured["llm_setting"] = dict(captured_dia.llm_setting)
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, dia))
- monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
- monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
- monkeypatch.setattr(
- module.TenantService,
- "get_by_id",
- lambda _tenant_id: (True, SimpleNamespace(llm_id="tenant-default")),
- )
- monkeypatch.setattr(module, "async_chat", _fake_async_chat)
- monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "chat_id": "chat-1",
- "session_id": "session-1",
- "stream": False,
- "messages": [{"role": "user", "content": "latest question"}],
- "temperature": 0,
- "presence_penalty": 0,
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.session_completion)())
-
- assert res["code"] == 0, res
- # Dialog had no model yet: fall back to the tenant default and still merge the
- # caller's per-request overrides onto the stored settings.
- assert captured["llm_id"] == "tenant-default"
- assert captured["llm_setting"] == {
- "top_p": 0.3,
- "custom": "keep",
- "temperature": 0,
- "presence_penalty": 0,
- }
-
-
-def test_session_completion_can_use_submitted_full_history(monkeypatch):
- """The UI opt-in flag should preserve the previous full-history request behavior."""
- module = _load_chat_api_module(monkeypatch)
-
- captured_messages = []
- conv = SimpleNamespace(
- id="session-1",
- dialog_id="chat-1",
- message=[
- {"role": "assistant", "content": "prologue"},
- {"role": "user", "content": "server old question", "id": "old-user"},
- ],
- reference=[],
- user_id="authenticated-user",
- name="test",
- )
- conv.to_dict = lambda: {
- "id": conv.id,
- "dialog_id": conv.dialog_id,
- "message": conv.message,
- "reference": conv.reference,
- "user_id": conv.user_id,
- "name": conv.name,
- }
-
- async def _fake_async_chat(_dia, messages, stream=True, **_kwargs):
- captured_messages.append([dict(message) for message in messages])
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
- monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
- monkeypatch.setattr(module, "async_chat", _fake_async_chat)
- monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "chat_id": "chat-1",
- "session_id": "session-1",
- "stream": False,
- "pass_all_history_messages": True,
- "messages": [
- {"role": "user", "content": "client old question", "id": "client-old"},
- {"role": "assistant", "content": "client old answer", "id": "client-old"},
- {"role": "user", "content": "latest question", "id": "latest"},
- ],
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.session_completion)())
-
- assert res["code"] == 0, res
- assert [message["content"] for message in captured_messages[0]] == [
- "client old question",
- "client old answer",
- "latest question",
- ]
- assert [message["content"] for message in conv.message] == [
- "client old question",
- "client old answer",
- "latest question",
- ]
-
-
-def test_session_completion_accepts_question_payload(monkeypatch):
- """Compatibility calls from /chats/{chat_id}/completions may send only `question`."""
- module = _load_chat_api_module(monkeypatch)
-
- captured_messages = []
- conv = SimpleNamespace(
- id="session-1",
- dialog_id="chat-1",
- message=[{"role": "assistant", "content": "prologue"}],
- reference=[],
- user_id="authenticated-user",
- name="test",
- )
- conv.to_dict = lambda: {
- "id": conv.id,
- "dialog_id": conv.dialog_id,
- "message": conv.message,
- "reference": conv.reference,
- "user_id": conv.user_id,
- "name": conv.name,
- }
-
- async def _fake_async_chat(_dia, messages, stream=True, **_kwargs):
- captured_messages.append([dict(message) for message in messages])
- yield {"answer": "ok", "reference": {}}
-
- monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
- monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
- monkeypatch.setattr(module, "async_chat", _fake_async_chat)
- monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "chat_id": "chat-1",
- "session_id": "session-1",
- "stream": False,
- "question": "latest question",
- }
- ),
- )
-
- res = _run(inspect.unwrap(module.session_completion)())
-
- assert res["code"] == 0, res
- assert [message["content"] for message in captured_messages[0]] == ["latest question"]
- assert conv.message[-1]["content"] == "latest question"
diff --git a/test/testcases/test_http_api/test_session_management/test_update_session_with_chat_assistant.py b/test/testcases/test_http_api/test_session_management/test_update_session_with_chat_assistant.py
deleted file mode 100644
index dc294fc0d3..0000000000
--- a/test/testcases/test_http_api/test_session_management/test_update_session_with_chat_assistant.py
+++ /dev/null
@@ -1,145 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from random import randint
-
-import pytest
-from common import delete_chat_assistants, list_session_with_chat_assistants, update_session_with_chat_assistant
-from configs import INVALID_API_TOKEN, INVALID_ID_32, SESSION_WITH_CHAT_NAME_LIMIT
-from libs.auth import RAGFlowHttpApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = update_session_with_chat_assistant(invalid_auth, "chat_assistant_id", "session_id")
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestSessionWithChatAssistantUpdate:
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param({"name": "valid_name"}, 0, "", marks=pytest.mark.p1),
- pytest.param({"name": "a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1)}, 102, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": 1}, 100, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": ""}, 102, "`name` can not be empty", marks=pytest.mark.p3),
- pytest.param({"name": "duplicated_name"}, 0, "", marks=pytest.mark.p3),
- pytest.param({"name": "case insensitive"}, 0, "", marks=pytest.mark.p3),
- ],
- )
- def test_name(self, HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- if payload["name"] == "duplicated_name":
- update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], payload)
- elif payload["name"] == "case insensitive":
- update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], {"name": payload["name"].upper()})
-
- res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- res = list_session_with_chat_assistants(HttpApiAuth, chat_assistant_id, {"id": session_ids[0]})
- assert res["data"][0]["name"] == payload["name"]
- else:
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "chat_assistant_id, expected_code, expected_message",
- [
- (INVALID_ID_32, 109, "no authorization"),
- ],
- )
- def test_invalid_chat_assistant_id(self, HttpApiAuth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message):
- _, session_ids = add_sessions_with_chat_assistant_func
- res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], {"name": "valid_name"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "session_id, expected_code, expected_message",
- [
- ("", 100, "<MethodNotAllowed '405: Method Not Allowed'>"),
- ("invalid_session_id", 102, "Session not found!"),
- ],
- )
- def test_invalid_session_id(self, HttpApiAuth, add_sessions_with_chat_assistant_func, session_id, expected_code, expected_message):
- chat_assistant_id, _ = add_sessions_with_chat_assistant_func
- res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_id, {"name": "valid_name"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
- @pytest.mark.p3
- def test_repeated_update_session(self, HttpApiAuth, add_sessions_with_chat_assistant_func):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], {"name": "valid_name_1"})
- assert res["code"] == 0
-
- res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], {"name": "valid_name_2"})
- assert res["code"] == 0
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param({"unknown_key": "unknown_value"}, 100, "ValueError", marks=pytest.mark.skip),
- ({}, 0, ""),
- pytest.param(None, 100, "TypeError", marks=pytest.mark.skip),
- ],
- )
- def test_invalid_params(self, HttpApiAuth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], payload)
- assert res["code"] == expected_code
- if expected_code != 0:
- assert expected_message in res["message"]
-
- @pytest.mark.p3
- def test_concurrent_update_session(self, HttpApiAuth, add_sessions_with_chat_assistant_func):
- count = 50
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- update_session_with_chat_assistant,
- HttpApiAuth,
- chat_assistant_id,
- session_ids[randint(0, 4)],
- {"name": f"update session test {i}"},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_update_session_to_deleted_chat_assistant(self, HttpApiAuth, add_sessions_with_chat_assistant_func):
- chat_assistant_id, session_ids = add_sessions_with_chat_assistant_func
- delete_chat_assistants(HttpApiAuth, {"ids": [chat_assistant_id]})
- res = update_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, session_ids[0], {"name": "valid_name"})
- assert res["code"] == 109
- assert res["message"] == "no authorization"
diff --git a/test/testcases/test_sdk_api/common.py b/test/testcases/test_sdk_api/common.py
deleted file mode 100644
index 5b48ac2277..0000000000
--- a/test/testcases/test_sdk_api/common.py
+++ /dev/null
@@ -1,125 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-from pathlib import Path
-
-import pytest
-from configs import IS_GO_PROXY
-from ragflow_sdk import Chat, Chunk, DataSet, Document, RAGFlow, Session
-from utils.file_utils import create_txt_file
-
-
-REST_API_MAX_PAGE_SIZE = 100
-
-
-def _skip_missing_go_document_metadata(error: Exception) -> None:
- if IS_GO_PROXY and "Unknown column 'meta_fields'" in str(error):
- pytest.skip("Go deployment database schema is missing document.meta_fields")
-
-
-def list_all_documents(dataset: DataSet, *, limit: int | None = None, page_size: int = REST_API_MAX_PAGE_SIZE) -> list[Document]:
- page_size = min(page_size, REST_API_MAX_PAGE_SIZE)
- documents: list[Document] = []
- page = 1
- while True:
- batch = dataset.list_documents(page=page, page_size=page_size)
- documents.extend(batch)
- if limit is not None and len(documents) >= limit:
- return documents[:limit]
- if len(batch) < page_size:
- return documents
- page += 1
-
-
-def list_all_sessions(chat_assistant: Chat, *, limit: int | None = None, page_size: int = REST_API_MAX_PAGE_SIZE) -> list[Session]:
- page_size = min(page_size, REST_API_MAX_PAGE_SIZE)
- sessions: list[Session] = []
- page = 1
- while True:
- batch = chat_assistant.list_sessions(page=page, page_size=page_size)
- sessions.extend(batch)
- if limit is not None and len(sessions) >= limit:
- return sessions[:limit]
- if len(batch) < page_size:
- return sessions
- page += 1
-
-
-def valid_chat_llm_id(client: RAGFlow) -> str:
- # SDK tests use the tenant's configured chat model; this helper discovers test fixture state, not SDK behavior.
- res = client.get("/users/me/models")
- data = res.json()
- if data.get("code") == 0:
- llm_id = (data.get("data") or {}).get("llm_id")
- if llm_id:
- return llm_id
- raise Exception("No valid chat llm_id is configured for the current tenant")
-
-
-# DATASET MANAGEMENT
-def batch_create_datasets(client: RAGFlow, num: int) -> list[DataSet]:
- return [client.create_dataset(name=f"dataset_{i}") for i in range(num)]
-
-
-def delete_all_datasets(client: RAGFlow, *, page_size: int = 100) -> None:
- client.delete_datasets(delete_all=True)
-
-
-def delete_all_chats(client: RAGFlow, *, page_size: int = 100) -> None:
- client.delete_chats(delete_all=True)
-
-
-# FILE MANAGEMENT WITHIN DATASET
-def bulk_upload_documents(dataset: DataSet, num: int, tmp_path: Path) -> list[Document]:
- document_infos = []
- for i in range(num):
- fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
- with fp.open("rb") as f:
- blob = f.read()
- document_infos.append({"display_name": fp.name, "blob": blob})
-
- try:
- return dataset.upload_documents(document_infos)
- except Exception as error:
- _skip_missing_go_document_metadata(error)
- raise
-
-
-def delete_all_documents(dataset: DataSet, *, page_size: int = 100) -> None:
- dataset.delete_documents(delete_all=True)
-
-
-def delete_all_sessions(chat_assistant: Chat, *, page_size: int = 100) -> None:
- chat_assistant.delete_sessions(delete_all=True)
-
-
-def delete_all_chunks(document: Document, *, page_size: int = 100) -> None:
- document.delete_chunks(delete_all=True)
-
-
-# CHUNK MANAGEMENT WITHIN DATASET
-def batch_add_chunks(document: Document, num: int) -> list[Chunk]:
- return [document.add_chunk(content=f"chunk test {i}") for i in range(num)]
-
-
-# CHAT ASSISTANT MANAGEMENT
-def batch_create_chat_assistants(client: RAGFlow, num: int) -> list[Chat]:
- return [client.create_chat(name=f"test_chat_assistant_{i}") for i in range(num)]
-
-
-# SESSION MANAGEMENT
-def batch_add_sessions_with_chat_assistant(chat_assistant: Chat, num) -> list[Session]:
- return [chat_assistant.create_session(name=f"session_with_chat_assistant_{i}") for i in range(num)]
diff --git a/test/testcases/test_sdk_api/conftest.py b/test/testcases/test_sdk_api/conftest.py
deleted file mode 100644
index 2c185e0356..0000000000
--- a/test/testcases/test_sdk_api/conftest.py
+++ /dev/null
@@ -1,221 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-from pathlib import Path
-from time import sleep
-
-import pytest
-from common import (
- batch_add_chunks,
- batch_create_chat_assistants,
- batch_create_datasets,
- bulk_upload_documents,
- delete_all_chats,
- delete_all_chunks,
- delete_all_datasets,
- delete_all_sessions,
-)
-from configs import HOST_ADDRESS, IS_GO_PROXY, VERSION
-from pytest import FixtureRequest
-from ragflow_sdk import Chat, Chunk, DataSet, Document, RAGFlow
-from utils import wait_for
-from utils.file_utils import (
- create_docx_file,
- create_eml_file,
- create_excel_file,
- create_html_file,
- create_image_file,
- create_json_file,
- create_md_file,
- create_pdf_file,
- create_ppt_file,
- create_txt_file,
-)
-
-
-GO_ONLY_PATH_SKIPS = {
- "test_file_management_within_dataset/test_download_document.py::test_file_type_validation": "Go deployment database schema is missing document.meta_fields",
- "test_file_management_within_dataset/test_upload_documents.py": "Go deployment database schema is missing document.meta_fields",
- "test_dataset_mangement/test_create_dataset.py::TestDatasetCreate::test_parser_config_invalid": "Go dataset parser_config only validates serialized size, not individual fields",
- "test_dataset_mangement/test_create_dataset.py::TestDatasetCreate::test_parser_config_empty": "Go dataset creation does not preserve an explicit empty parser_config",
- "test_dataset_mangement/test_create_dataset.py::TestDatasetCreate::test_parser_config_unset": "Go dataset creation does not return the SDK parser_config contract",
- "test_dataset_mangement/test_create_dataset.py::TestParserConfigBugFix": "Go dataset parser_config defaults do not match the SDK contract",
- "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_parser_config": "Go dataset updates do not return the SDK parser_config contract",
- "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_parser_config_invalid": "Go dataset parser_config only validates serialized size, not individual fields",
- "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_parser_config_empty": "Go dataset update ignores an explicit empty parser_config",
- "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_field_unsupported": "Go dataset update ignores unsupported fields",
- "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_pagerank": "Go dataset update does not persist pagerank",
- "test_dataset_mangement/test_update_dataset.py::TestDatasetUpdate::test_pagerank_set_to_0": "Go dataset update does not persist pagerank",
- "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_page_invalid": "Go dataset list normalizes invalid page values instead of rejecting them",
- "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_page_size_invalid": "Go dataset list normalizes invalid page_size values instead of rejecting them",
- "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_name": "Go dataset list does not apply the name filter",
- "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_id": "Go dataset list does not apply the id filter",
- "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_id_empty": "Go dataset list accepts an empty id instead of rejecting it",
- "test_dataset_mangement/test_list_datasets.py::TestDatasetsList::test_name_and_id": "Go dataset list does not combine name and id filters",
- "test_memory_management/test_list_memory.py::TestMemoryList::test_get_config_invalid_memory_id_raises": "Go memory config lookup does not reject an unknown memory id",
- "test_message_management/test_add_message.py::TestAddRawMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_add_message.py::TestAddMultipleTypeMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_add_message.py::TestAddToMultipleMemory": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_forget_message.py::TestForgetMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_get_message_content.py::TestGetMessageContent": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_get_recent_message.py::TestGetRecentMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_list_message.py::TestMessageList": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_search_message.py::TestSearchMessage": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
- "test_message_management/test_update_message_status.py::TestUpdateMessageStatus": "Go built-in embedding uses an unavailable localhost:6380 endpoint",
-}
-
-
-def pytest_collection_modifyitems(items):
- if not IS_GO_PROXY:
- return
- for item in items:
- for test_path, reason in GO_ONLY_PATH_SKIPS.items():
- matched_at = item.nodeid.find(test_path)
- matched_suffix = item.nodeid[matched_at + len(test_path) :] if matched_at >= 0 else ""
- if matched_at >= 0 and (not matched_suffix or matched_suffix.startswith("[") or matched_suffix.startswith("::")):
- item.add_marker(pytest.mark.skip(reason=reason))
- break
-
-
-@wait_for(200, 1, "Document parsing timeout")
-def condition(_dataset: DataSet):
- documents = _dataset.list_documents(page_size=100)
- for document in documents:
- if document.run != "DONE":
- return False
- return True
-
-
-def generate_test_files(request: FixtureRequest, tmp_path: Path):
- file_creators = {
- "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
- "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
- "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
- "image": (tmp_path / "ragflow_test.png", create_image_file),
- "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
- "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
- "md": (tmp_path / "ragflow_test.md", create_md_file),
- "json": (tmp_path / "ragflow_test.json", create_json_file),
- "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
- "html": (tmp_path / "ragflow_test.html", create_html_file),
- }
-
- files = {}
- for file_type, (file_path, creator_func) in file_creators.items():
- if request.param in ["", file_type]:
- creator_func(file_path)
- files[file_type] = file_path
- return files
-
-
[email protected](scope="class")
-def ragflow_tmp_dir(request: FixtureRequest, tmp_path_factory: Path) -> Path:
- class_name = request.cls.__name__
- return tmp_path_factory.mktemp(class_name)
-
-
[email protected](scope="session")
-def client(token: str) -> RAGFlow:
- return RAGFlow(api_key=token, base_url=HOST_ADDRESS, version=VERSION)
-
-
[email protected](scope="function")
-def clear_datasets(request: FixtureRequest, client: RAGFlow):
- def cleanup():
- delete_all_datasets(client)
-
- request.addfinalizer(cleanup)
-
-
[email protected](scope="function")
-def clear_chat_assistants(request: FixtureRequest, client: RAGFlow):
- def cleanup():
- delete_all_chats(client)
-
- request.addfinalizer(cleanup)
-
-
[email protected](scope="function")
-def clear_session_with_chat_assistants(request, add_chat_assistants):
- def cleanup():
- for chat_assistant in chat_assistants:
- try:
- delete_all_sessions(chat_assistant)
- except Exception:
- pass
-
- request.addfinalizer(cleanup)
-
- _, _, chat_assistants = add_chat_assistants
-
-
[email protected](scope="class")
-def add_dataset(request: FixtureRequest, client: RAGFlow) -> DataSet:
- def cleanup():
- delete_all_datasets(client)
-
- request.addfinalizer(cleanup)
- return batch_create_datasets(client, 1)[0]
-
-
[email protected](scope="function")
-def add_dataset_func(request: FixtureRequest, client: RAGFlow) -> DataSet:
- def cleanup():
- delete_all_datasets(client)
-
- request.addfinalizer(cleanup)
- return batch_create_datasets(client, 1)[0]
-
-
[email protected](scope="class")
-def add_document(add_dataset: DataSet, ragflow_tmp_dir: Path) -> tuple[DataSet, Document]:
- return add_dataset, bulk_upload_documents(add_dataset, 1, ragflow_tmp_dir)[0]
-
-
[email protected](scope="class")
-def add_chunks(request: FixtureRequest, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chunk]]:
- def cleanup():
- try:
- delete_all_chunks(document)
- except Exception:
- pass
-
- request.addfinalizer(cleanup)
-
- dataset, document = add_document
- dataset.async_parse_documents([document.id])
- condition(dataset)
- chunks = batch_add_chunks(document, 4)
- # issues/6487
- sleep(1)
- return dataset, document, chunks
-
-
[email protected](scope="class")
-def add_chat_assistants(request, client, add_document) -> tuple[DataSet, Document, list[Chat]]:
- def cleanup():
- try:
- delete_all_chats(client)
- except Exception:
- pass
-
- request.addfinalizer(cleanup)
-
- dataset, document = add_document
- dataset.async_parse_documents([document.id])
- condition(dataset)
- return dataset, document, batch_create_chat_assistants(client, 5)
diff --git a/test/testcases/test_sdk_api/test_agent_management/test_agent_crud_unit.py b/test/testcases/test_sdk_api/test_agent_management/test_agent_crud_unit.py
deleted file mode 100644
index c54095d7f1..0000000000
--- a/test/testcases/test_sdk_api/test_agent_management/test_agent_crud_unit.py
+++ /dev/null
@@ -1,221 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import pytest
-from configs import HOST_ADDRESS
-from ragflow_sdk import RAGFlow
-from ragflow_sdk.modules.agent import Agent
-from ragflow_sdk.modules.session import Session
-
-
-class _DummyResponse:
- def __init__(self, payload):
- self._payload = payload
-
- def json(self):
- return self._payload
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def test_list_agents_success_and_error(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- captured = {}
-
- def _ok_get(path, params=None, json=None):
- captured["path"] = path
- captured["params"] = params
- captured["json"] = json
- return _DummyResponse({"code": 0, "data": {"canvas": [{"id": "agent-1", "title": "Agent One"}], "total": 1}})
-
- monkeypatch.setattr(client, "get", _ok_get)
- agents = client.list_agents()
- assert captured["path"] == "/agents"
- assert captured["params"] == {"page": 1, "page_size": 30, "orderby": "update_time", "desc": True}
- assert isinstance(agents[0], Agent), str(agents)
- assert agents[0].id == "agent-1", str(agents[0])
- assert agents[0].title == "Agent One", str(agents[0])
-
- monkeypatch.setattr(client, "get", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "list boom"}))
- with pytest.raises(Exception) as exception_info:
- client.list_agents()
- assert "list boom" in str(exception_info.value), str(exception_info.value)
-
-
-def test_create_agent_payload_and_error(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- calls = []
-
- def _ok_post(path, json=None, stream=False, files=None):
- calls.append((path, json, stream, files))
- return _DummyResponse({"code": 0, "message": "ok"})
-
- monkeypatch.setattr(client, "post", _ok_post)
- client.create_agent("agent-title", {"graph": {}}, description=None)
- assert calls[-1][0] == "/agents"
- assert calls[-1][1] == {"title": "agent-title", "dsl": {"graph": {}}}
-
- client.create_agent("agent-title", {"graph": {}}, description="desc")
- assert calls[-1][1] == {"title": "agent-title", "dsl": {"graph": {}}, "description": "desc"}
-
- client.create_agent("agent-title", {"graph": {}}, canvas_type="Marketing")
- assert calls[-1][1] == {"title": "agent-title", "dsl": {"graph": {}}, "canvas_type": "Marketing"}
-
- monkeypatch.setattr(client, "post", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "create boom"}))
- with pytest.raises(Exception) as exception_info:
- client.create_agent("agent-title", {"graph": {}})
- assert "create boom" in str(exception_info.value), str(exception_info.value)
-
-
-def test_update_agent_payload_matrix_and_error(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- calls = []
-
- def _ok_put(path, json):
- calls.append((path, json))
- return _DummyResponse({"code": 0, "message": "ok"})
-
- monkeypatch.setattr(client, "put", _ok_put)
- cases = [
- ({"title": "new-title"}, {"title": "new-title"}),
- ({"description": "new-description"}, {"description": "new-description"}),
- ({"dsl": {"nodes": []}}, {"dsl": {"nodes": []}}),
- (
- {"title": "new-title", "description": "new-description", "dsl": {"nodes": []}},
- {"title": "new-title", "description": "new-description", "dsl": {"nodes": []}},
- ),
- ({"canvas_type": "Agent"}, {"canvas_type": "Agent"}),
- ]
- for kwargs, expected_payload in cases:
- client.update_agent("agent-1", **kwargs)
- assert calls[-1][0] == "/agents/agent-1"
- assert calls[-1][1] == expected_payload
-
- monkeypatch.setattr(client, "put", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "update boom"}))
- with pytest.raises(Exception) as exception_info:
- client.update_agent("agent-1", title="bad")
- assert "update boom" in str(exception_info.value), str(exception_info.value)
-
-
-def test_delete_agent_success_and_error(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- calls = []
-
- def _ok_delete(path, json):
- calls.append((path, json))
- return _DummyResponse({"code": 0, "message": "ok"})
-
- monkeypatch.setattr(client, "delete", _ok_delete)
- client.delete_agent("agent-1")
- assert calls[-1] == ("/agents/agent-1", {})
-
- monkeypatch.setattr(client, "delete", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "delete boom"}))
- with pytest.raises(Exception) as exception_info:
- client.delete_agent("agent-1")
- assert "delete boom" in str(exception_info.value), str(exception_info.value)
-
-
-def test_agent_and_dsl_default_initialization():
- client = RAGFlow("token", HOST_ADDRESS)
-
- agent = Agent(client, {"id": "agent-1", "title": "Agent One"})
- assert agent.id == "agent-1"
- assert agent.avatar is None
- assert agent.canvas_type is None
- assert agent.description is None
- assert agent.dsl is None
-
- dsl = Agent.Dsl(client, {})
- assert dsl.answer == []
- assert "begin" in dsl.components
- assert dsl.components["begin"]["obj"]["component_name"] == "Begin"
- assert dsl.graph["nodes"][0]["id"] == "begin"
- assert dsl.history == []
- assert dsl.messages == []
- assert dsl.path == []
- assert dsl.reference == []
-
-
-def test_agent_session_methods_success_and_error_paths(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- agent = Agent(client, {"id": "agent-1"})
- calls = {"post": [], "get": [], "rm": []}
-
- def _ok_post(path, json=None, stream=False, files=None):
- calls["post"].append((path, json, stream, files))
- return _DummyResponse({"code": 0, "data": {"id": "session-1", "agent_id": "agent-1", "name": "one"}})
-
- def _ok_get(path, params=None):
- calls["get"].append((path, params))
- return _DummyResponse(
- {
- "code": 0,
- "data": [
- {"id": "session-1", "agent_id": "agent-1", "name": "one"},
- {"id": "session-2", "agent_id": "agent-1", "name": "two"},
- ],
- }
- )
-
- def _ok_rm(path, payload):
- calls["rm"].append((path, payload))
- return _DummyResponse({"code": 0, "message": "ok"})
-
- monkeypatch.setattr(agent, "post", _ok_post)
- monkeypatch.setattr(agent, "get", _ok_get)
- monkeypatch.setattr(agent, "rm", _ok_rm)
-
- session = agent.create_session(name="session-name")
- assert isinstance(session, Session), str(session)
- assert session.id == "session-1", str(session)
- assert calls["post"][-1][0] == "/agents/agent-1/sessions"
- assert calls["post"][-1][1] == {"name": "session-name"}
-
- sessions = agent.list_sessions(page=2, page_size=5, orderby="create_time", desc=False, id="session-1")
- assert len(sessions) == 2, str(sessions)
- assert all(isinstance(item, Session) for item in sessions), str(sessions)
- assert calls["get"][-1][0] == "/agents/agent-1/sessions"
- assert calls["get"][-1][1]["page"] == 2
- assert calls["get"][-1][1]["id"] == "session-1"
-
- agent.delete_sessions(ids=["session-1", "session-2"])
- assert calls["rm"][-1] == ("/agents/agent-1/sessions", {"ids": ["session-1", "session-2"]})
-
- monkeypatch.setattr(agent, "post", lambda *_args, **_kwargs: _DummyResponse({"code": 1, "message": "create failed"}))
- with pytest.raises(Exception, match="create failed"):
- agent.create_session(name="bad")
-
- monkeypatch.setattr(agent, "get", lambda *_args, **_kwargs: _DummyResponse({"code": 2, "message": "list failed"}))
- with pytest.raises(Exception, match="list failed"):
- agent.list_sessions()
-
- monkeypatch.setattr(agent, "rm", lambda *_args, **_kwargs: _DummyResponse({"code": 3, "message": "delete failed"}))
- with pytest.raises(Exception, match="delete failed"):
- agent.delete_sessions(ids=["session-1"])
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/conftest.py b/test/testcases/test_sdk_api/test_chat_assistant_management/conftest.py
deleted file mode 100644
index 19502f977a..0000000000
--- a/test/testcases/test_sdk_api/test_chat_assistant_management/conftest.py
+++ /dev/null
@@ -1,52 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import batch_create_chat_assistants, delete_all_chats
-from pytest import FixtureRequest
-from ragflow_sdk import Chat, DataSet, Document, RAGFlow
-from utils import wait_for
-
-
-@wait_for(200, 1, "Document parsing timeout")
-def condition(_dataset: DataSet):
- documents = _dataset.list_documents(page_size=100)
- for document in documents:
- if document.run != "DONE":
- return False
- return True
-
-
-def _ensure_parsed(dataset: DataSet, document: Document):
- """Trigger parsing only if the document is not already done or in progress."""
- if document.run == "DONE":
- return
- try:
- dataset.async_parse_documents([document.id])
- except Exception:
- pass # Already being processed
- condition(dataset)
-
-
[email protected](scope="function")
-def add_chat_assistants_func(request: FixtureRequest, client: RAGFlow, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chat]]:
- def cleanup():
- delete_all_chats(client)
-
- request.addfinalizer(cleanup)
-
- dataset, document = add_document
- _ensure_parsed(dataset, document)
- return dataset, document, batch_create_chat_assistants(client, 5)
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_chat_crud_unit.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_chat_crud_unit.py
deleted file mode 100644
index fe52e1d0da..0000000000
--- a/test/testcases/test_sdk_api/test_chat_assistant_management/test_chat_crud_unit.py
+++ /dev/null
@@ -1,88 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import pytest
-from configs import HOST_ADDRESS
-from ragflow_sdk import RAGFlow
-from ragflow_sdk.modules.chat import Chat
-from ragflow_sdk.modules.session import Session
-
-
-class _DummyResponse:
- def __init__(self, payload):
- self._payload = payload
-
- def json(self):
- return self._payload
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def test_chat_create_session_raises_server_error_message(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- chat = Chat(client, {"id": "chat-1"})
-
- monkeypatch.setattr(
- chat,
- "post",
- lambda *_args, **_kwargs: _DummyResponse({"code": 102, "message": "`name` can not be empty"}),
- )
-
- with pytest.raises(Exception) as exception_info:
- chat.create_session(name="")
- assert "`name` can not be empty" in str(exception_info.value), str(exception_info.value)
-
-
-def test_chat_list_sessions_forwards_restful_query_params(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- chat = Chat(client, {"id": "chat-1"})
- calls = []
-
- def _ok_get(path, params=None):
- calls.append((path, params))
- return _DummyResponse(
- {
- "code": 0,
- "data": [
- {"id": "session-1", "chat_id": "chat-1", "name": "one"},
- {"id": "session-2", "chat_id": "chat-1", "name": "two"},
- ],
- }
- )
-
- monkeypatch.setattr(chat, "get", _ok_get)
-
- sessions = chat.list_sessions(page=2, page_size=2, orderby="create_time", desc=False, id="session-1", name="one", user_id="user-1")
- assert len(sessions) == 2, str(sessions)
- assert all(isinstance(item, Session) for item in sessions), str(sessions)
- assert calls[-1][0] == "/chats/chat-1/sessions"
- assert calls[-1][1]["page_size"] == 2
- assert calls[-1][1]["name"] == "one"
- assert calls[-1][1]["user_id"] == "user-1"
-
- all_sessions = chat.list_sessions(page_size=0)
- assert len(all_sessions) == 2, str(all_sessions)
- assert calls[-1][1]["page_size"] == 0
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_create_chat_assistant.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_create_chat_assistant.py
deleted file mode 100644
index 8d226acdcf..0000000000
--- a/test/testcases/test_sdk_api/test_chat_assistant_management/test_create_chat_assistant.py
+++ /dev/null
@@ -1,197 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import pytest
-from common import valid_chat_llm_id
-from configs import CHAT_ASSISTANT_NAME_LIMIT
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-
-
[email protected]("clear_chat_assistants")
-class TestChatAssistantCreate:
- @pytest.mark.p3
- @pytest.mark.usefixtures("add_chunks")
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("valid_name", ""),
- pytest.param("a" * (CHAT_ASSISTANT_NAME_LIMIT + 1), "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param(1, "", marks=pytest.mark.skip(reason="issues/")),
- ("", "`name` is required"),
- ("duplicated_name", "duplicated chat name in creating chat"),
- ("case insensitive", "duplicated chat name in creating chat"),
- ],
- )
- def test_name(self, client, name, expected_message):
- if name == "duplicated_name":
- client.create_chat(name=name)
- elif name == "case insensitive":
- client.create_chat(name=name.upper())
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.create_chat(name=name)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant = client.create_chat(name=name)
- assert chat_assistant.name == name
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_ids, expected_message",
- [
- ([], ""),
- (lambda r: [r], ""),
- (["invalid_dataset_id"], "You don't own the dataset invalid_dataset_id"),
- ("invalid_dataset_id", "violates type hint list[str] | None"),
- ],
- )
- def test_dataset_ids(self, client, add_chunks, dataset_ids, expected_message):
- dataset, _, _ = add_chunks
- if callable(dataset_ids):
- dataset_ids = dataset_ids(dataset.id)
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.create_chat(name="ragflow test", dataset_ids=dataset_ids)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant = client.create_chat(name="ragflow test", dataset_ids=dataset_ids)
- assert chat_assistant.name == "ragflow test"
-
- @pytest.mark.p3
- def test_icon(self, client, tmp_path):
- fn = create_image_file(tmp_path / "ragflow_test.png")
- chat_assistant = client.create_chat(name="icon_test", icon=encode_avatar(fn), dataset_ids=[])
- assert chat_assistant.name == "icon_test"
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "llm_setting, expected_message",
- [
- ({}, ""),
- ({"temperature": 0}, ""),
- ({"temperature": 1}, ""),
- pytest.param({"temperature": -1}, "", marks=pytest.mark.skip),
- pytest.param({"temperature": 10}, "", marks=pytest.mark.skip),
- pytest.param({"temperature": "a"}, "", marks=pytest.mark.skip),
- ({"top_p": 0}, ""),
- ({"top_p": 1}, ""),
- pytest.param({"top_p": -1}, "", marks=pytest.mark.skip),
- pytest.param({"top_p": 10}, "", marks=pytest.mark.skip),
- pytest.param({"top_p": "a"}, "", marks=pytest.mark.skip),
- ({"presence_penalty": 0}, ""),
- ({"presence_penalty": 1}, ""),
- pytest.param({"presence_penalty": -1}, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": 10}, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": "a"}, "", marks=pytest.mark.skip),
- ({"frequency_penalty": 0}, ""),
- ({"frequency_penalty": 1}, ""),
- pytest.param({"frequency_penalty": -1}, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": 10}, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": "a"}, "", marks=pytest.mark.skip),
- ({"max_token": 0}, ""),
- ({"max_token": 1024}, ""),
- pytest.param({"max_token": -1}, "", marks=pytest.mark.skip),
- pytest.param({"max_token": 10}, "", marks=pytest.mark.skip),
- pytest.param({"max_token": "a"}, "", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
- ],
- )
- def test_llm_setting(self, client, add_chunks, llm_setting, expected_message):
- dataset, _, _ = add_chunks
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_setting=llm_setting or None)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant = client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_setting=llm_setting or None)
- for k, v in llm_setting.items():
- assert getattr(chat_assistant.llm_setting, k) == v
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "llm_id, expected_message",
- [
- (valid_chat_llm_id, ""),
- ("unknown", "`llm_id` unknown doesn't exist"),
- ],
- )
- def test_llm_id(self, client, add_chunks, llm_id, expected_message):
- dataset, _, _ = add_chunks
- if callable(llm_id):
- llm_id = llm_id(client)
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_id=llm_id)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant = client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_id=llm_id)
- assert chat_assistant.llm_id == llm_id
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "prompt_config, expected_message",
- [
- ({"empty_response": "Hello World"}, ""),
- ({"empty_response": ""}, ""),
- ({"empty_response": "!@#$%^&*()"}, ""),
- ({"empty_response": "中文测试"}, ""),
- pytest.param({"empty_response": 123}, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": True}, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": " "}, "", marks=pytest.mark.skip),
- ({"prologue": "Hello World"}, ""),
- ({"prologue": ""}, ""),
- ({"prologue": "!@#$%^&*()"}, ""),
- ({"prologue": "中文测试"}, ""),
- pytest.param({"prologue": 123}, "", marks=pytest.mark.skip),
- pytest.param({"prologue": True}, "", marks=pytest.mark.skip),
- pytest.param({"prologue": " "}, "", marks=pytest.mark.skip),
- ({"quote": True}, ""),
- ({"quote": False}, ""),
- ({"system": "Hello World {knowledge}"}, ""),
- ({"system": "{knowledge}"}, ""),
- ({"system": "!@#$%^&*() {knowledge}"}, ""),
- ({"system": "中文测试 {knowledge}"}, ""),
- ({"system": "Hello World"}, ""),
- ({"system": "Hello World", "parameters": []}, ""),
- pytest.param({"system": 123}, "", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
- ],
- )
- def test_prompt_config(self, client, add_chunks, prompt_config, expected_message):
- dataset, _, _ = add_chunks
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt_config=prompt_config)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant = client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt_config=prompt_config)
- for k, v in prompt_config.items():
- assert getattr(chat_assistant.prompt_config, k) == v
-
-
-class TestChatAssistantCreate2:
- @pytest.mark.p3
- def test_unparsed_document(self, client, add_document):
- dataset, _ = add_document
- with pytest.raises(Exception) as exception_info:
- client.create_chat(name="prompt_test", dataset_ids=[dataset.id])
- assert "doesn't own parsed file" in str(exception_info.value)
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_delete_chat_assistants.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_delete_chat_assistants.py
deleted file mode 100644
index 487c63598b..0000000000
--- a/test/testcases/test_sdk_api/test_chat_assistant_management/test_delete_chat_assistants.py
+++ /dev/null
@@ -1,116 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_create_chat_assistants
-
-
-class TestChatAssistantsDelete:
- @pytest.mark.parametrize(
- "payload, expected_message, remaining",
- [
- pytest.param(None, "", 5, marks=pytest.mark.p3),
- pytest.param({"ids": []}, "", 5, marks=pytest.mark.p3),
- pytest.param({"ids": ["invalid_id"]}, "Chat(invalid_id) not found.", 5, marks=pytest.mark.p3),
- pytest.param({"ids": ["\n!?。;!?\"'"]}, """Chat(\n!?。;!?"\') not found.""", 5, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r[:1]}, "", 4, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r}, "", 0, marks=pytest.mark.p3),
- ],
- )
- def test_basic_scenarios(self, client, add_chat_assistants_func, payload, expected_message, remaining):
- _, _, chat_assistants = add_chat_assistants_func
- if callable(payload):
- payload = payload([chat_assistant.id for chat_assistant in chat_assistants])
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.delete_chats(**payload)
- assert expected_message in str(exception_info.value)
- else:
- if payload is None:
- client.delete_chats(payload)
- else:
- client.delete_chats(**payload)
-
- assistants = client.list_chats()
- assert len(assistants) == remaining
-
- @pytest.mark.p3
- def test_delete_chats_nonzero_response_raises(self, client, monkeypatch):
- class _DummyResponse:
- def json(self):
- return {"code": 1, "message": "boom"}
-
- monkeypatch.setattr(client, "delete", lambda *_args, **_kwargs: _DummyResponse())
- with pytest.raises(Exception) as exception_info:
- client.delete_chats(ids=["chat-1"])
- assert "boom" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_delete_partial_invalid_id(self, client, add_chat_assistants_func, payload):
- _, _, chat_assistants = add_chat_assistants_func
- payload = payload([chat_assistant.id for chat_assistant in chat_assistants])
- client.delete_chats(**payload)
-
- assistants = client.list_chats()
- assert len(assistants) == 0
-
- @pytest.mark.p3
- def test_repeated_deletion(self, client, add_chat_assistants_func):
- _, _, chat_assistants = add_chat_assistants_func
- chat_ids = [chat.id for chat in chat_assistants]
- client.delete_chats(ids=chat_ids)
-
- with pytest.raises(Exception) as exception_info:
- client.delete_chats(ids=chat_ids)
- assert "not found" in str(exception_info.value)
-
- @pytest.mark.p3
- def test_duplicate_deletion(self, client, add_chat_assistants_func):
- _, _, chat_assistants = add_chat_assistants_func
- chat_ids = [chat.id for chat in chat_assistants]
- client.delete_chats(ids=chat_ids + chat_ids)
-
- assistants = client.list_chats()
- assert len(assistants) == 0
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, client):
- count = 100
- chat_ids = [client.create_chat(name=f"test_{i}").id for i in range(count)]
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(client.delete_chats, ids=[chat_ids[i]]) for i in range(count)]
- responses = list(as_completed(futures))
-
- assert len(responses) == count
- assert all(future.exception() is None for future in futures)
-
- @pytest.mark.p3
- def test_delete_1k(self, client):
- chat_assistants = batch_create_chat_assistants(client, 1_000)
- client.delete_chats(ids=[chat_assistants.id for chat_assistants in chat_assistants])
-
- assistants = client.list_chats()
- assert len(assistants) == 0
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_list_chat_assistants.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_list_chat_assistants.py
deleted file mode 100644
index 93085a0d84..0000000000
--- a/test/testcases/test_sdk_api/test_chat_assistant_management/test_list_chat_assistants.py
+++ /dev/null
@@ -1,232 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-
-
[email protected]("add_chat_assistants")
-class TestChatAssistantsList:
- @pytest.mark.p3
- def test_default(self, client):
- assistants = client.list_chats()
- assert len(assistants) == 5
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page": 0, "page_size": 2}, 2, ""),
- ({"page": 2, "page_size": 2}, 2, ""),
- ({"page": 3, "page_size": 2}, 1, ""),
- ({"page": "3", "page_size": 2}, 0, "not instance of"),
- pytest.param(
- {"page": -1, "page_size": 2},
- 0,
- "1064",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page": "a", "page_size": 2},
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page(self, client, params, expected_page_size, expected_message):
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.list_chats(**params)
- assert expected_message in str(exception_info.value)
- else:
- assistants = client.list_chats(**params)
- assert len(assistants) == expected_page_size
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page_size": 0}, 5, ""),
- ({"page_size": 1}, 1, ""),
- ({"page_size": 6}, 5, ""),
- ({"page_size": "1"}, 0, "not instance of"),
- pytest.param(
- {"page_size": -1},
- 0,
- "1064",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page_size": "a"},
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page_size(self, client, params, expected_page_size, expected_message):
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.list_chats(**params)
- assert expected_message in str(exception_info.value)
- else:
- assistants = client.list_chats(**params)
- assert len(assistants) == expected_page_size
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"orderby": "create_time"}, ""),
- ({"orderby": "update_time"}, ""),
- pytest.param({"orderby": "name", "desc": "False"}, "", marks=pytest.mark.skip(reason="issues/5851")),
- pytest.param({"orderby": "unknown"}, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_orderby(self, client, params, expected_message):
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.list_chats(**params)
- assert expected_message in str(exception_info.value)
- else:
- client.list_chats(**params)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"desc": None}, "not instance of"),
- ({"desc": "true"}, "not instance of"),
- ({"desc": "True"}, "not instance of"),
- ({"desc": True}, ""),
- ({"desc": "false"}, "not instance of"),
- ({"desc": "False"}, "not instance of"),
- ({"desc": False}, ""),
- ({"desc": "False", "orderby": "update_time"}, "not instance of"),
- pytest.param(
- {"desc": "unknown"},
- "desc should be true or false",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_desc(self, client, params, expected_message):
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.list_chats(**params)
- assert expected_message in str(exception_info.value)
- else:
- client.list_chats(**params)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_num, expected_message",
- [
- ({"keywords": None}, 5, ""),
- ({"keywords": ""}, 5, ""),
- ({"keywords": "test_chat_assistant_1"}, 1, ""),
- ({"keywords": "unknown"}, 0, ""),
- ],
- )
- def test_keywords(self, client, params, expected_num, expected_message):
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.list_chats(**params)
- assert expected_message in str(exception_info.value)
- else:
- assistants = client.list_chats(**params)
- if params["keywords"] in [None, ""]:
- assert len(assistants) == expected_num
- else:
- assert len(assistants) == expected_num
- if expected_num:
- assert assistants[0].name == params["keywords"]
-
- @pytest.mark.p1
- def test_exact_id_and_name_filters(self, client, add_chat_assistants):
- _, _, chat_assistants = add_chat_assistants
- target = chat_assistants[1]
-
- assistants = client.list_chats(id=target.id)
- assert len(assistants) == 1
- assert assistants[0].id == target.id
-
- assistants = client.list_chats(name=target.name)
- assert len(assistants) == 1
- assert assistants[0].name == target.name
-
- assistants = client.list_chats(name=target.name, keywords="unknown")
- assert len(assistants) == 1
- assert assistants[0].name == target.name
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "chat_assistant_id, expected_message",
- [
- (lambda r: r[0], ""),
- ("unknown", "no authorization"),
- ],
- )
- def test_get_chat(self, client, add_chat_assistants, chat_assistant_id, expected_message):
- _, _, chat_assistants = add_chat_assistants
- chat_id = chat_assistant_id([chat.id for chat in chat_assistants]) if callable(chat_assistant_id) else chat_assistant_id
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.get_chat(chat_id)
- assert expected_message in str(exception_info.value)
- else:
- assistant = client.get_chat(chat_id)
- assert assistant.id == chat_id
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "chat_assistant_id, keywords, expected_num, expected_message",
- [
- (lambda r: r[0], "test_chat_assistant_0", 1, ""),
- (lambda r: r[0], "test_chat_assistant_1", 1, ""),
- (lambda r: r[0], "unknown", 0, ""),
- ],
- )
- def test_get_and_keywords_are_separate_lookups(self, client, add_chat_assistants, chat_assistant_id, keywords, expected_num, expected_message):
- _, _, chat_assistants = add_chat_assistants
- chat_id = chat_assistant_id([chat.id for chat in chat_assistants]) if callable(chat_assistant_id) else chat_assistant_id
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.get_chat(chat_id)
- assert expected_message in str(exception_info.value)
- else:
- client.get_chat(chat_id)
- assistants = client.list_chats(keywords=keywords)
- assert len(assistants) == expected_num
-
- @pytest.mark.p3
- def test_concurrent_list(self, client):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(client.list_chats) for _ in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- @pytest.mark.p3
- def test_list_chats_after_deleting_associated_dataset(self, client, add_chat_assistants):
- dataset, _, _ = add_chat_assistants
- client.delete_datasets(ids=[dataset.id])
-
- assistants = client.list_chats()
- assert len(assistants) == 5
diff --git a/test/testcases/test_sdk_api/test_chat_assistant_management/test_update_chat_assistant.py b/test/testcases/test_sdk_api/test_chat_assistant_management/test_update_chat_assistant.py
deleted file mode 100644
index 719e17a43c..0000000000
--- a/test/testcases/test_sdk_api/test_chat_assistant_management/test_update_chat_assistant.py
+++ /dev/null
@@ -1,209 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import pytest
-from common import valid_chat_llm_id
-from configs import CHAT_ASSISTANT_NAME_LIMIT
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-
-
-class TestChatAssistantUpdate:
- @pytest.mark.p3
- def test_update_rejects_non_dict(self, add_chat_assistants_func):
- _, _, chat_assistants = add_chat_assistants_func
- chat_assistant = chat_assistants[0]
-
- with pytest.raises(Exception) as exception_info:
- chat_assistant.update.__wrapped__(chat_assistant, "bad")
- assert "`update_message` must be a dict" in str(exception_info.value)
-
- @pytest.mark.p3
- def test_update_raises_on_nonzero_response(self, add_chat_assistants_func, monkeypatch):
- _, _, chat_assistants = add_chat_assistants_func
- chat_assistant = chat_assistants[0]
-
- class _DummyResponse:
- def json(self):
- return {"code": 1, "message": "boom"}
-
- monkeypatch.setattr(chat_assistant, "patch", lambda *_args, **_kwargs: _DummyResponse())
-
- with pytest.raises(Exception) as exception_info:
- chat_assistant.update({"name": "error-case"})
- assert "boom" in str(exception_info.value)
-
- @pytest.mark.p3
- def test_update_uses_patch_for_partial_payload(self, add_chat_assistants_func, monkeypatch):
- _, _, chat_assistants = add_chat_assistants_func
- chat_assistant = chat_assistants[0]
- captured = {}
-
- class _DummyResponse:
- def json(self):
- return {"code": 0, "message": "ok"}
-
- def _patch(path, payload):
- captured["path"] = path
- captured["payload"] = payload
- return _DummyResponse()
-
- monkeypatch.setattr(chat_assistant, "patch", _patch)
- monkeypatch.setattr(chat_assistant, "put", lambda *_args, **_kwargs: pytest.fail("update() should not use PUT"))
-
- chat_assistant.update({"name": "renamed"})
-
- assert captured["path"] == f"/chats/{chat_assistant.id}"
- assert captured["payload"] == {"name": "renamed"}
-
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- pytest.param({"name": "valid_name"}, "", marks=pytest.mark.p3),
- pytest.param({"name": "a" * (CHAT_ASSISTANT_NAME_LIMIT + 1)}, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": 1}, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": ""}, "`name` cannot be empty", marks=pytest.mark.p3),
- pytest.param({"name": "test_chat_assistant_1"}, "duplicated chat name", marks=pytest.mark.p3),
- pytest.param({"name": "TEST_CHAT_ASSISTANT_1"}, "duplicated chat name", marks=pytest.mark.p3),
- ],
- )
- def test_name(self, client, add_chat_assistants_func, payload, expected_message):
- _, _, chat_assistants = add_chat_assistants_func
- chat_assistant = chat_assistants[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.update(payload)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant.update(payload)
- updated_chat = client.get_chat(chat_assistant.id)
- assert updated_chat.name == payload["name"], str(updated_chat)
-
- @pytest.mark.p3
- def test_icon(self, client, add_chat_assistants_func, tmp_path):
- dataset, _, chat_assistants = add_chat_assistants_func
- chat_assistant = chat_assistants[0]
-
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {"name": "icon_test", "icon": encode_avatar(fn), "dataset_ids": [dataset.id]}
-
- chat_assistant.update(payload)
- updated_chat = client.get_chat(chat_assistant.id)
- assert updated_chat.name == payload["name"], str(updated_chat)
- assert updated_chat.icon is not None, str(updated_chat)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "llm_setting, expected_message",
- [
- ({"model_name": valid_chat_llm_id}, ""),
- ({"model_name": "unknown"}, "`llm_id` unknown doesn't exist"),
- ({"temperature": 0}, ""),
- ({"temperature": 1}, ""),
- pytest.param({"temperature": -1}, "", marks=pytest.mark.skip),
- pytest.param({"temperature": 10}, "", marks=pytest.mark.skip),
- pytest.param({"temperature": "a"}, "", marks=pytest.mark.skip),
- ({"top_p": 0}, ""),
- ({"top_p": 1}, ""),
- pytest.param({"top_p": -1}, "", marks=pytest.mark.skip),
- pytest.param({"top_p": 10}, "", marks=pytest.mark.skip),
- pytest.param({"top_p": "a"}, "", marks=pytest.mark.skip),
- ({"presence_penalty": 0}, ""),
- ({"presence_penalty": 1}, ""),
- pytest.param({"presence_penalty": -1}, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": 10}, "", marks=pytest.mark.skip),
- pytest.param({"presence_penalty": "a"}, "", marks=pytest.mark.skip),
- ({"frequency_penalty": 0}, ""),
- ({"frequency_penalty": 1}, ""),
- pytest.param({"frequency_penalty": -1}, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": 10}, "", marks=pytest.mark.skip),
- pytest.param({"frequency_penalty": "a"}, "", marks=pytest.mark.skip),
- ({"max_token": 0}, ""),
- ({"max_token": 1024}, ""),
- pytest.param({"max_token": -1}, "", marks=pytest.mark.skip),
- pytest.param({"max_token": 10}, "", marks=pytest.mark.skip),
- pytest.param({"max_token": "a"}, "", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
- ],
- )
- def test_llm_setting(self, client, add_chat_assistants_func, llm_setting, expected_message):
- dataset, _, chat_assistants = add_chat_assistants_func
- chat_assistant = chat_assistants[0]
- llm_setting = dict(llm_setting)
- llm_id = llm_setting.pop("model_name", None)
- if callable(llm_id):
- llm_id = llm_id(client)
- payload = {"name": "llm_test", "dataset_ids": [dataset.id], "llm_setting": llm_setting}
- if llm_id is not None:
- payload["llm_id"] = llm_id
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.update(payload)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant.update(payload)
- updated_chat = client.get_chat(chat_assistant.id)
- if llm_id:
- assert updated_chat.llm_id == llm_id, str(updated_chat)
- for k, v in llm_setting.items():
- assert getattr(updated_chat.llm_setting, k) == v, str(updated_chat)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "prompt_config, expected_message",
- [
- ({"empty_response": "Hello World"}, ""),
- ({"empty_response": ""}, ""),
- ({"empty_response": "!@#$%^&*()"}, ""),
- ({"empty_response": "中文测试"}, ""),
- pytest.param({"empty_response": 123}, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": True}, "", marks=pytest.mark.skip),
- pytest.param({"empty_response": " "}, "", marks=pytest.mark.skip),
- ({"prologue": "Hello World"}, ""),
- ({"prologue": ""}, ""),
- ({"prologue": "!@#$%^&*()"}, ""),
- ({"prologue": "中文测试"}, ""),
- pytest.param({"prologue": 123}, "", marks=pytest.mark.skip),
- pytest.param({"prologue": True}, "", marks=pytest.mark.skip),
- pytest.param({"prologue": " "}, "", marks=pytest.mark.skip),
- ({"quote": True}, ""),
- ({"quote": False}, ""),
- ({"system": "Hello World {knowledge}"}, ""),
- ({"system": "{knowledge}"}, ""),
- ({"system": "!@#$%^&*() {knowledge}"}, ""),
- ({"system": "中文测试 {knowledge}"}, ""),
- ({"system": "Hello World"}, ""),
- ({"system": "Hello World", "parameters": []}, ""),
- pytest.param({"system": 123}, "", marks=pytest.mark.skip),
- pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
- ],
- )
- def test_prompt_config(self, client, add_chat_assistants_func, prompt_config, expected_message):
- dataset, _, chat_assistants = add_chat_assistants_func
- chat_assistant = chat_assistants[0]
- payload = {"name": "prompt_test", "prompt_config": prompt_config, "dataset_ids": [dataset.id]}
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.update(payload)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant.update(payload)
- updated_chat = client.get_chat(chat_assistant.id)
- for k, v in prompt_config.items():
- assert getattr(updated_chat.prompt_config, k) == v, str(updated_chat)
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/conftest.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/conftest.py
deleted file mode 100644
index 4727c85a48..0000000000
--- a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/conftest.py
+++ /dev/null
@@ -1,55 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import pytest
-from common import batch_add_chunks, delete_all_chunks
-from pytest import FixtureRequest
-from ragflow_sdk import Chunk, DataSet, Document
-from utils import wait_for
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_dataset: DataSet):
- documents = _dataset.list_documents(page_size=100)
- for document in documents:
- if document.run != "DONE":
- return False
- return True
-
-
-@wait_for(30, 1, "Chunk indexing timeout")
-def chunks_visible(_document: Document, _chunk_ids: list[str]):
- visible_ids = {chunk.id for chunk in _document.list_chunks(page_size=100)}
- return set(_chunk_ids).issubset(visible_ids)
-
-
[email protected](scope="function")
-def add_chunks_func(request: FixtureRequest, add_document: tuple[DataSet, Document]) -> tuple[DataSet, Document, list[Chunk]]:
- def cleanup():
- try:
- delete_all_chunks(document)
- except Exception:
- pass
-
- request.addfinalizer(cleanup)
-
- dataset, document = add_document
- dataset.async_parse_documents([document.id])
- condition(dataset)
- chunks = batch_add_chunks(document, 4)
- chunks_visible(document, [chunk.id for chunk in chunks])
- return dataset, document, chunks
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_add_chunk.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_add_chunk.py
deleted file mode 100644
index 2db1022752..0000000000
--- a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_add_chunk.py
+++ /dev/null
@@ -1,190 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from time import sleep
-
-import pytest
-from ragflow_sdk import Chunk
-
-
-def validate_chunk_details(dataset_id: str, document_id: str, payload: dict, chunk: Chunk):
- assert chunk.dataset_id == dataset_id
- assert chunk.document_id == document_id
- assert chunk.content == payload["content"]
- if "important_keywords" in payload:
- assert chunk.important_keywords == payload["important_keywords"]
- if "questions" in payload:
- assert chunk.questions == [str(q).strip() for q in payload.get("questions", []) if str(q).strip()]
- if "tag_kwd" in payload:
- assert chunk.tag_kwd == payload["tag_kwd"]
-
-
-class TestAddChunk:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"content": None}, "not instance of"),
- ({"content": ""}, "`content` is required"),
- ({"content": 1}, "not instance of"),
- ({"content": "a"}, ""),
- ({"content": " "}, "`content` is required"),
- ({"content": "\n!?。;!?\"'"}, ""),
- ],
- )
- def test_content(self, add_document, payload, expected_message):
- dataset, document = add_document
- chunks_count = len(document.list_chunks())
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.add_chunk(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk = document.add_chunk(**payload)
- validate_chunk_details(dataset.id, document.id, payload, chunk)
-
- sleep(1)
- chunks = document.list_chunks()
- assert len(chunks) == chunks_count + 1, str(chunks)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"content": "chunk test important_keywords 1", "important_keywords": ["a", "b", "c"]}, ""),
- ({"content": "chunk test important_keywords 2", "important_keywords": [""]}, ""),
- ({"content": "chunk test important_keywords 3", "important_keywords": [1]}, "not instance of"),
- ({"content": "chunk test important_keywords 4", "important_keywords": ["a", "a"]}, ""),
- ({"content": "chunk test important_keywords 5", "important_keywords": "abc"}, "not instance of"),
- ({"content": "chunk test important_keywords 6", "important_keywords": 123}, "not instance of"),
- ],
- )
- def test_important_keywords(self, add_document, payload, expected_message):
- dataset, document = add_document
- chunks_count = len(document.list_chunks())
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.add_chunk(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk = document.add_chunk(**payload)
- validate_chunk_details(dataset.id, document.id, payload, chunk)
-
- sleep(1)
- chunks = document.list_chunks()
- assert len(chunks) == chunks_count + 1, str(chunks)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"content": "chunk test test_questions 1", "questions": ["a", "b", "c"]}, ""),
- ({"content": "chunk test test_questions 2", "questions": [""]}, ""),
- ({"content": "chunk test test_questions 3", "questions": [1]}, "not instance of"),
- ({"content": "chunk test test_questions 4", "questions": ["a", "a"]}, ""),
- ({"content": "chunk test test_questions 5", "questions": "abc"}, "not instance of"),
- ({"content": "chunk test test_questions 6", "questions": 123}, "not instance of"),
- ],
- )
- def test_questions(self, add_document, payload, expected_message):
- dataset, document = add_document
- chunks_count = len(document.list_chunks())
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.add_chunk(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk = document.add_chunk(**payload)
- validate_chunk_details(dataset.id, document.id, payload, chunk)
-
- sleep(1)
- chunks = document.list_chunks()
- assert len(chunks) == chunks_count + 1, str(chunks)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"content": "chunk test test_tag_kwd 1", "tag_kwd": ["tag1", "tag2"]}, ""),
- ({"content": "chunk test test_tag_kwd 2", "tag_kwd": [""]}, ""),
- ({"content": "chunk test test_tag_kwd 3", "tag_kwd": [1]}, "not instance of"),
- ({"content": "chunk test test_tag_kwd 4", "tag_kwd": ["tag", "tag"]}, ""),
- ({"content": "chunk test test_tag_kwd 5", "tag_kwd": "abc"}, "not instance of"),
- ({"content": "chunk test test_tag_kwd 6", "tag_kwd": 123}, "not instance of"),
- ],
- )
- def test_tag_kwd(self, add_document, payload, expected_message):
- dataset, document = add_document
- chunks_count = len(document.list_chunks())
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.add_chunk(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk = document.add_chunk(**payload)
- validate_chunk_details(dataset.id, document.id, payload, chunk)
-
- sleep(1)
- chunks = document.list_chunks()
- assert len(chunks) == chunks_count + 1, str(chunks)
-
- @pytest.mark.p3
- def test_repeated_add_chunk(self, add_document):
- payload = {"content": "chunk test repeated_add_chunk"}
- dataset, document = add_document
- chunks_count = len(document.list_chunks())
-
- chunk1 = document.add_chunk(**payload)
- validate_chunk_details(dataset.id, document.id, payload, chunk1)
- sleep(1)
- chunks = document.list_chunks()
- assert len(chunks) == chunks_count + 1, str(chunks)
-
- chunk2 = document.add_chunk(**payload)
- validate_chunk_details(dataset.id, document.id, payload, chunk2)
- sleep(1)
- chunks = document.list_chunks()
- assert len(chunks) == chunks_count + 1, str(chunks)
-
- @pytest.mark.p2
- def test_add_chunk_to_deleted_document(self, add_document):
- dataset, document = add_document
- dataset.delete_documents(ids=[document.id])
-
- with pytest.raises(Exception) as exception_info:
- document.add_chunk(content="chunk test")
- assert f"you don't own the document {document.id}" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.skip(reason="issues/6411")
- @pytest.mark.p3
- def test_concurrent_add_chunk(self, add_document):
- count = 50
- _, document = add_document
- initial_chunk_count = len(document.list_chunks())
-
- def add_chunk_task(i):
- return document.add_chunk(content=f"chunk test concurrent {i}")
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(add_chunk_task, i) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- sleep(5)
- assert len(document.list_chunks(page_size=100)) == initial_chunk_count + count
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_delete_chunks.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_delete_chunks.py
deleted file mode 100644
index 20d5dc2aab..0000000000
--- a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_delete_chunks.py
+++ /dev/null
@@ -1,116 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_add_chunks
-
-
-class TestChunksDeletion:
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:4]}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_delete_partial_invalid_id(self, add_chunks_func, payload):
- _, document, chunks = add_chunks_func
- chunk_ids = [chunk.id for chunk in chunks]
- payload = payload(chunk_ids)
-
- with pytest.raises(Exception) as exception_info:
- document.delete_chunks(**payload)
- assert "rm_chunk deleted chunks" in str(exception_info.value), str(exception_info.value)
-
- remaining_chunks = document.list_chunks()
- assert len(remaining_chunks) == 1, str(remaining_chunks)
-
- @pytest.mark.p3
- def test_repeated_deletion(self, add_chunks_func):
- _, document, chunks = add_chunks_func
- chunk_ids = [chunk.id for chunk in chunks]
- document.delete_chunks(ids=chunk_ids)
-
- with pytest.raises(Exception) as exception_info:
- document.delete_chunks(ids=chunk_ids)
- assert "rm_chunk deleted chunks 0, expect" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_duplicate_deletion(self, add_chunks_func):
- _, document, chunks = add_chunks_func
- chunk_ids = [chunk.id for chunk in chunks]
- document.delete_chunks(ids=chunk_ids * 2)
- remaining_chunks = document.list_chunks()
- assert len(remaining_chunks) == 1, str(remaining_chunks)
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, add_document):
- count = 100
- _, document = add_document
- chunks = batch_add_chunks(document, count)
- chunk_ids = [chunk.id for chunk in chunks]
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(document.delete_chunks, ids=[chunk_id]) for chunk_id in chunk_ids]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- @pytest.mark.p3
- def test_delete_1k(self, add_document):
- count = 1_000
- _, document = add_document
- chunks = batch_add_chunks(document, count)
- chunk_ids = [chunk.id for chunk in chunks]
-
- from time import sleep
-
- sleep(1)
-
- document.delete_chunks(ids=chunk_ids)
- remaining_chunks = document.list_chunks()
- assert len(remaining_chunks) == 0, str(remaining_chunks)
-
- @pytest.mark.parametrize(
- "payload, expected_message, remaining",
- [
- pytest.param(None, "", 5, marks=pytest.mark.p3),
- pytest.param({"ids": ["invalid_id"]}, "rm_chunk deleted chunks 0, expect 1", 5, marks=pytest.mark.p3),
- pytest.param("not json", "UnboundLocalError", 5, marks=pytest.mark.skip(reason="pull/6376")),
- pytest.param(lambda r: {"ids": r[:1]}, "", 4, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r}, "", 1, marks=pytest.mark.p3),
- pytest.param({"ids": []}, "", 5, marks=pytest.mark.p3),
- ],
- )
- def test_basic_scenarios(self, add_chunks_func, payload, expected_message, remaining):
- _, document, chunks = add_chunks_func
- chunk_ids = [chunk.id for chunk in chunks]
- if callable(payload):
- payload = payload(chunk_ids)
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.delete_chunks(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- if payload is None:
- document.delete_chunks()
- else:
- document.delete_chunks(**payload)
-
- remaining_chunks = document.list_chunks()
- assert len(remaining_chunks) == remaining, str(remaining_chunks)
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_list_chunks.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_list_chunks.py
deleted file mode 100644
index e4f11dfcb2..0000000000
--- a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_list_chunks.py
+++ /dev/null
@@ -1,160 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_add_chunks
-from utils.engine_utils import get_doc_engine
-
-
-class TestChunksList:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 2, ""),
- pytest.param({"page": 0, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- ({"page": 2, "page_size": 2}, 2, ""),
- ({"page": 3, "page_size": 2}, 1, ""),
- ({"page": "3", "page_size": 2}, 1, ""),
- pytest.param({"page": -1, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- pytest.param({"page": "a", "page_size": 2}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page(self, add_chunks, params, expected_page_size, expected_message):
- _, document, _ = add_chunks
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.list_chunks(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = document.list_chunks(**params)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page_size": None}, 5, ""),
- pytest.param({"page_size": 0}, 5, ""),
- ({"page_size": 1}, 1, ""),
- ({"page_size": 6}, 5, ""),
- ({"page_size": "1"}, 1, ""),
- pytest.param({"page_size": -1}, 5, "", marks=pytest.mark.skip),
- pytest.param({"page_size": "a"}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page_size(self, add_chunks, params, expected_page_size, expected_message):
- _, document, _ = add_chunks
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.list_chunks(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = document.list_chunks(**params)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"keywords": None}, 5),
- ({"keywords": ""}, 5),
- ({"keywords": "1"}, 1),
- ({"keywords": "chunk"}, 4),
- pytest.param({"keywords": "ragflow"}, 1, marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6509")),
- pytest.param({"keywords": "ragflow"}, 5, marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") != "infinity", reason="issues/6509")),
- ({"keywords": "unknown"}, 0),
- ],
- )
- def test_keywords(self, add_chunks, params, expected_page_size):
- _, document, _ = add_chunks
- if params.get("keywords") == "ragflow":
- doc_engine = get_doc_engine(document.rag)
- if doc_engine == "infinity" and expected_page_size == 1:
- pytest.skip("issues/6509")
- if doc_engine != "infinity" and expected_page_size == 5:
- pytest.skip("issues/6509")
- chunks = document.list_chunks(**params)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "chunk_id, expected_page_size, expected_message",
- [
- (None, 5, ""),
- ("", 5, ""),
- pytest.param(lambda r: r[0], 1, "", marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6499")),
- pytest.param("unknown", 0, """AttributeError("\'NoneType\' object has no attribute \'keys\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_id(self, add_chunks, chunk_id, expected_page_size, expected_message):
- _, document, chunks = add_chunks
- if callable(chunk_id) and get_doc_engine(document.rag) == "infinity":
- pytest.skip("issues/6499")
- chunk_ids = [chunk.id for chunk in chunks]
- if callable(chunk_id):
- params = {"id": chunk_id(chunk_ids)}
- else:
- params = {"id": chunk_id}
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.list_chunks(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = document.list_chunks(**params)
- if params["id"] in [None, ""]:
- assert len(chunks) == expected_page_size, str(chunks)
- else:
- assert chunks[0].id == params["id"], str(chunks)
-
- @pytest.mark.p3
- def test_concurrent_list(self, add_chunks):
- _, document, _ = add_chunks
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(document.list_chunks) for _ in range(count)]
-
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(len(future.result()) == 5 for future in futures)
-
- @pytest.mark.p3
- def test_default(self, add_document):
- _, document = add_document
- batch_add_chunks(document, 31)
-
- from time import sleep
-
- sleep(3)
-
- chunks = document.list_chunks()
- assert len(chunks) == 30, str(chunks)
-
- @pytest.mark.p3
- def test_list_chunks_invalid_document_id_raises(self, add_chunks):
- _, document, _ = add_chunks
- invalid_document = document.__class__(
- document.rag,
- {"id": "missing-document-id-for-chunks", "dataset_id": document.dataset_id},
- )
- with pytest.raises(Exception) as exception_info:
- invalid_document.list_chunks()
- assert str(exception_info.value), exception_info
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_retrieval_chunks.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_retrieval_chunks.py
deleted file mode 100644
index 112765b730..0000000000
--- a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_retrieval_chunks.py
+++ /dev/null
@@ -1,245 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-
-DOC_ENGINE = (os.getenv("DOC_ENGINE") or "").lower()
-
-
-class TestChunksRetrieval:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_page_size, expected_message",
- [
- ({"question": "chunk", "dataset_ids": None}, 4, ""),
- ({"question": "chunk", "document_ids": None}, 0, "missing 1 required positional argument"),
- ({"question": "chunk", "dataset_ids": None, "document_ids": None}, 4, ""),
- ({"question": "chunk"}, 0, "missing 1 required positional argument"),
- ],
- )
- def test_basic_scenarios(self, client, add_chunks, payload, expected_page_size, expected_message):
- dataset, document, _ = add_chunks
- if "dataset_ids" in payload:
- payload["dataset_ids"] = [dataset.id]
- if "document_ids" in payload:
- payload["document_ids"] = [document.id]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.retrieve(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = client.retrieve(**payload)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_page_size, expected_message",
- [
- pytest.param(
- {"page": None, "page_size": 2},
- 2,
- """TypeError("int() argument must be a string, a bytes-like object or a real number, not \'NoneType\'")""",
- marks=pytest.mark.skip,
- ),
- pytest.param(
- {"page": 0, "page_size": 2},
- 0,
- "ValueError('Search does not support negative slicing.')",
- marks=pytest.mark.skip,
- ),
- ({"page": 2, "page_size": 2}, 2, ""),
- ({"page": 3, "page_size": 2}, 0, ""),
- ({"page": "3", "page_size": 2}, 0, ""),
- pytest.param(
- {"page": -1, "page_size": 2},
- 0,
- "ValueError('Search does not support negative slicing.')",
- marks=pytest.mark.skip,
- ),
- pytest.param(
- {"page": "a", "page_size": 2},
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_page(self, client, add_chunks, payload, expected_page_size, expected_message):
- dataset, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.retrieve(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = client.retrieve(**payload)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_page_size, expected_message",
- [
- pytest.param(
- {"page_size": None},
- 0,
- """TypeError("int() argument must be a string, a bytes-like object or a real number, not \'NoneType\'")""",
- marks=pytest.mark.skip,
- ),
- pytest.param({"page_size": 1}, 1, "", marks=pytest.mark.skip(reason="issues/10692")),
- ({"page_size": 5}, 4, ""),
- pytest.param({"page_size": "1"}, 1, "", marks=pytest.mark.skip(reason="issues/10692")),
- pytest.param(
- {"page_size": "a"},
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_page_size(self, client, add_chunks, payload, expected_page_size, expected_message):
- dataset, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.retrieve(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = client.retrieve(**payload)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_page_size, expected_message",
- [
- ({"vector_similarity_weight": 0}, 4, ""),
- ({"vector_similarity_weight": 0.5}, 4, ""),
- ({"vector_similarity_weight": 10}, 4, ""),
- pytest.param(
- {"vector_similarity_weight": "a"},
- 0,
- """ValueError("could not convert string to float: 'a'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_vector_similarity_weight(self, client, add_chunks, payload, expected_page_size, expected_message):
- dataset, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.retrieve(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = client.retrieve(**payload)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_page_size, expected_message",
- [
- ({"top_k": 10}, 4, ""),
- pytest.param(
- {"top_k": 1},
- 4,
- "",
- marks=pytest.mark.skipif(DOC_ENGINE in ["infinity", "opensearch"], reason="Infinity"),
- ),
- pytest.param(
- {"top_k": 1},
- 1,
- "",
- marks=pytest.mark.skipif(DOC_ENGINE in ["", "opensearch", "elasticsearch"], reason="elasticsearch"),
- ),
- ({"top_k": -1}, 4, "must be greater than 0"),
- pytest.param(
- {"top_k": "a"},
- 0,
- """ValueError("invalid literal for int() with base 10: \'a\'")""",
- marks=pytest.mark.skip,
- ),
- ],
- )
- def test_top_k(self, client, add_chunks, payload, expected_page_size, expected_message):
- dataset, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.retrieve(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = client.retrieve(**payload)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.skip
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"rerank_id": "BAAI/bge-reranker-v2-m3"}, ""),
- pytest.param({"rerank_id": "unknown"}, "LookupError('Model(unknown) not authorized')", marks=pytest.mark.skip),
- ],
- )
- def test_rerank_id(self, client, add_chunks, payload, expected_message):
- dataset, _, _ = add_chunks
- payload.update({"question": "chunk", "dataset_ids": [dataset.id]})
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.retrieve(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = client.retrieve(**payload)
- assert len(chunks) > 0, str(chunks)
-
- @pytest.mark.skip
- @pytest.mark.parametrize(
- "payload, expected_page_size, expected_message",
- [
- ({"keyword": True}, 5, ""),
- ({"keyword": "True"}, 5, ""),
- ({"keyword": False}, 5, ""),
- ({"keyword": "False"}, 5, ""),
- ({"keyword": None}, 5, ""),
- ],
- )
- def test_keyword(self, client, add_chunks, payload, expected_page_size, expected_message):
- dataset, _, _ = add_chunks
- payload.update({"question": "chunk test", "dataset_ids": [dataset.id]})
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- client.retrieve(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunks = client.retrieve(**payload)
- assert len(chunks) == expected_page_size, str(chunks)
-
- @pytest.mark.p3
- def test_concurrent_retrieval(self, client, add_chunks):
- dataset, _, _ = add_chunks
- count = 100
- payload = {"question": "chunk", "dataset_ids": [dataset.id]}
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(client.retrieve, **payload) for _ in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
diff --git a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_update_chunk.py b/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_update_chunk.py
deleted file mode 100644
index 460292da3f..0000000000
--- a/test/testcases/test_sdk_api/test_chunk_management_within_dataset/test_update_chunk.py
+++ /dev/null
@@ -1,171 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from random import randint
-
-import pytest
-
-
-class TestUpdatedChunk:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"content": None}, ""),
- ({"content": ""}, "`content` is required"),
- pytest.param(
- {"content": 1},
- "TypeError('expected string or bytes-like object')",
- marks=pytest.mark.skip,
- ),
- ({"content": "update chunk"}, ""),
- ({"content": " "}, "`content` is required"),
- ({"content": "\n!?。;!?\"'"}, ""),
- ],
- )
- def test_content(self, add_chunks, payload, expected_message):
- _, _, chunks = add_chunks
- chunk = chunks[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chunk.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk.update(payload)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"important_keywords": ["a", "b", "c"]}, ""),
- ({"important_keywords": [""]}, ""),
- ({"important_keywords": [1]}, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"important_keywords": ["a", "a"]}, ""),
- ({"important_keywords": "abc"}, "`important_keywords` should be a list"),
- ({"important_keywords": 123}, "`important_keywords` should be a list"),
- ],
- )
- def test_important_keywords(self, add_chunks, payload, expected_message):
- _, _, chunks = add_chunks
- chunk = chunks[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chunk.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk.update(payload)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"questions": ["a", "b", "c"]}, ""),
- ({"questions": [""]}, ""),
- ({"questions": [1]}, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"questions": ["a", "a"]}, ""),
- ({"questions": "abc"}, "`questions` should be a list"),
- ({"questions": 123}, "`questions` should be a list"),
- ],
- )
- def test_questions(self, add_chunks, payload, expected_message):
- _, _, chunks = add_chunks
- chunk = chunks[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chunk.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk.update(payload)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"tag_kwd": ["tag1", "tag2"]}, ""),
- ({"tag_kwd": [""]}, ""),
- ({"tag_kwd": [1]}, "`tag_kwd` must be a list of strings"),
- ({"tag_kwd": ["tag", "tag"]}, ""),
- ({"tag_kwd": "tag"}, "`tag_kwd` should be a list"),
- ({"tag_kwd": 123}, "`tag_kwd` should be a list"),
- ],
- )
- def test_tag_kwd(self, add_chunks, payload, expected_message):
- _, _, chunks = add_chunks
- chunk = chunks[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chunk.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk.update(payload)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"available": True}, ""),
- pytest.param({"available": "True"}, """ValueError("invalid literal for int() with base 10: \'True\'")""", marks=pytest.mark.skip),
- ({"available": 1}, ""),
- ({"available": False}, ""),
- pytest.param({"available": "False"}, """ValueError("invalid literal for int() with base 10: \'False\'")""", marks=pytest.mark.skip),
- ({"available": 0}, ""),
- ],
- )
- def test_available(self, add_chunks, payload, expected_message):
- _, _, chunks = add_chunks
- chunk = chunks[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chunk.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- chunk.update(payload)
-
- @pytest.mark.p3
- def test_repeated_update_chunk(self, add_chunks):
- _, _, chunks = add_chunks
- chunk = chunks[0]
-
- chunk.update({"content": "chunk test 1"})
- chunk.update({"content": "chunk test 2"})
-
- @pytest.mark.p3
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6554")
- def test_concurrent_update_chunk(self, add_chunks):
- count = 50
- _, _, chunks = add_chunks
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(chunks[randint(0, 3)].update, {"content": f"update chunk test {i}"}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- @pytest.mark.p3
- def test_update_chunk_to_deleted_document(self, add_chunks):
- dataset, document, chunks = add_chunks
- dataset.delete_documents(ids=[document.id])
-
- with pytest.raises(Exception) as exception_info:
- chunks[0].update({})
- message = str(exception_info.value)
- ownership_message = f"you don't own the document {chunks[0].document_id}"
- assert message.rstrip(".") == ownership_message or message == f"Can't find this chunk {chunks[0].id}", message
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/conftest.py b/test/testcases/test_sdk_api/test_dataset_mangement/conftest.py
deleted file mode 100644
index 998af94995..0000000000
--- a/test/testcases/test_sdk_api/test_dataset_mangement/conftest.py
+++ /dev/null
@@ -1,39 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import pytest
-from common import batch_create_datasets, delete_all_datasets
-
-
[email protected](scope="class")
-def add_datasets(client, request):
- def cleanup():
- delete_all_datasets(client)
-
- request.addfinalizer(cleanup)
-
- return batch_create_datasets(client, 5)
-
-
[email protected](scope="function")
-def add_datasets_func(client, request):
- def cleanup():
- delete_all_datasets(client)
-
- request.addfinalizer(cleanup)
-
- return batch_create_datasets(client, 3)
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_create_dataset.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_create_dataset.py
deleted file mode 100644
index 39de107137..0000000000
--- a/test/testcases/test_sdk_api/test_dataset_mangement/test_create_dataset.py
+++ /dev/null
@@ -1,702 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from operator import attrgetter
-
-import pytest
-from configs import DATASET_NAME_LIMIT, DEFAULT_PARSER_CONFIG, HOST_ADDRESS, INVALID_API_TOKEN, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
-from hypothesis import example, given, settings
-from ragflow_sdk import DataSet, RAGFlow
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-from utils.hypothesis_utils import valid_names
-
-
[email protected]("clear_datasets")
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- ids=["empty_auth", "invalid_api_token"],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**{"name": "auth_test"})
- assert str(exception_info.value) == expected_message
-
-
[email protected]("clear_datasets")
-class TestCapability:
- @pytest.mark.p3
- def test_create_dataset_1k(self, client):
- count = 1_000
- for i in range(count):
- payload = {"name": f"dataset_{i}"}
- client.create_dataset(**payload)
- datasets = []
- for page in range(1, (count // 100) + 1):
- datasets.extend(client.list_datasets(page=page, page_size=100))
- assert len(datasets) == count
-
- @pytest.mark.p3
- def test_create_dataset_concurrent(self, client):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(client.create_dataset, **{"name": f"dataset_{i}"}) for i in range(100)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
-
[email protected]("clear_datasets")
-class TestDatasetCreate:
- @pytest.mark.p1
- @given(name=valid_names())
- @example("a" * 128)
- @settings(max_examples=20)
- def test_name(self, client, name):
- dataset = client.create_dataset(**{"name": name})
- assert dataset.name == name, str(dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "String should have at least 1 character"),
- (" ", "String should have at least 1 character"),
- ("a" * (DATASET_NAME_LIMIT + 1), "String should have at most 128 characters"),
- (0, "not instance of"),
- (None, "not instance of"),
- ],
- ids=["empty_name", "space_name", "too_long_name", "invalid_name", "None_name"],
- )
- def test_name_invalid(self, client, name, expected_message):
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**{"name": name})
- if IS_GO_PROXY:
- if name == "":
- expected_message = "failed on the 'required' tag"
- elif isinstance(name, str) and not name.strip():
- expected_message = "dataset name can't be empty"
- elif isinstance(name, str):
- expected_message = f"Dataset name length is {len(name)} which is large than {DATASET_NAME_LIMIT}"
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_name_duplicated(self, client):
- name = "duplicated_name"
- payload = {"name": name}
- client.create_dataset(**payload)
-
- dataset = client.create_dataset(**payload)
- assert dataset.name == name + "(1)", str(dataset)
-
- @pytest.mark.p3
- def test_name_case_insensitive(self, client):
- name = "CaseInsensitive"
- payload = {"name": name.upper()}
- client.create_dataset(**payload)
-
- payload = {"name": name.lower()}
- dataset = client.create_dataset(**payload)
- assert dataset.name == name.lower() + "(1)", str(dataset)
-
- @pytest.mark.p2
- def test_avatar(self, client, tmp_path):
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {
- "name": "avatar",
- "avatar": f"data:image/png;base64,{encode_avatar(fn)}",
- }
- client.create_dataset(**payload)
-
- @pytest.mark.p3
- def test_avatar_exceeds_limit_length(self, client):
- payload = {"name": "avatar_exceeds_limit_length", "avatar": "a" * 65536}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "name, prefix, expected_message",
- [
- ("empty_prefix", "", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("missing_comma", "data:image/png;base64", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("unsupported_mine_type", "invalid_mine_prefix:image/png;base64,", "invalid MIME prefix format. Must start with 'data:'"),
- ("invalid_mine_type", "data:unsupported_mine_type;base64,", "Unsupported MIME type. Allowed: ['image/jpeg', 'image/png']"),
- ],
- ids=["empty_prefix", "missing_comma", "unsupported_mine_type", "invalid_mine_type"],
- )
- def test_avatar_invalid_prefix(self, client, tmp_path, name, prefix, expected_message):
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {
- "name": name,
- "avatar": f"{prefix}{encode_avatar(fn)}",
- }
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_avatar_unset(self, client):
- payload = {"name": "avatar_unset"}
- dataset = client.create_dataset(**payload)
- assert dataset.avatar is None, str(dataset)
-
- @pytest.mark.p2
- def test_description(self, client):
- payload = {"name": "description", "description": "description"}
- dataset = client.create_dataset(**payload)
- assert dataset.description == "description", str(dataset)
-
- @pytest.mark.p3
- def test_description_exceeds_limit_length(self, client):
- payload = {"name": "description_exceeds_limit_length", "description": "a" * 65536}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_description_unset(self, client):
- payload = {"name": "description_unset"}
- dataset = client.create_dataset(**payload)
- assert dataset.description is None, str(dataset)
-
- @pytest.mark.p3
- def test_description_none(self, client):
- payload = {"name": "description_none", "description": None}
- dataset = client.create_dataset(**payload)
- assert dataset.description is None, str(dataset)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("BAAI/bge-small-en-v1.5@Builtin", "BAAI/bge-small-en-v1.5@Builtin"),
- ("embedding-3@ZHIPU-AI", "embedding-3@CI@ZHIPU-AI"),
- ],
- ids=["builtin_baai", "tenant_zhipu"],
- )
- def test_embedding_model(self, client, name, embedding_model):
- payload = {"name": name, "embedding_model": embedding_model}
- dataset = client.create_dataset(**payload)
- assert dataset.embedding_model == embedding_model, str(dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("unknown_llm_name", "unknown@CI@ZHIPU-AI"),
- ("unknown_llm_factory", "embedding-3@unknown"),
- ("tenant_no_auth_default_tenant_llm", "text-embedding-v3@Tongyi-Qianwen"),
- ("tenant_no_auth", "text-embedding-3-small@OpenAI"),
- ],
- ids=["unknown_llm_name", "unknown_llm_factory", "tenant_no_auth_default_tenant_llm", "tenant_no_auth"],
- )
- def test_embedding_model_invalid(self, client, name, embedding_model):
- payload = {"name": name, "embedding_model": embedding_model}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert "not found" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("empty", ""),
- ("space", " "),
- ("missing_at", "BAAI/bge-small-en-v1.5Builtin"),
- ("missing_model_name", "@Builtin"),
- ("missing_provider", "BAAI/bge-small-en-v1.5@"),
- ("whitespace_only_model_name", " @Builtin"),
- ("whitespace_only_provider", "BAAI/bge-small-en-v1.5@ "),
- ],
- ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],
- )
- def test_embedding_model_format(self, client, name, embedding_model):
- payload = {"name": name, "embedding_model": embedding_model}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- if name in ["empty", "space", "missing_at"]:
- assert "embedding model identifier must follow <model_name>@<provider> format" in str(exception_info.value), str(exception_info.value)
- else:
- assert "both model_name and provider must be non-empty strings" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_embedding_model_unset(self, client):
- payload = {"name": "embedding_model_unset"}
- dataset = client.create_dataset(**payload)
- assert dataset.embedding_model.startswith("BAAI/bge-small-en-v1.5@Local@Builtin") if IS_GO_PROXY else dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(dataset)
-
- @pytest.mark.p2
- def test_embedding_model_none(self, client):
- payload = {"name": "embedding_model_none", "embedding_model": None}
- dataset = client.create_dataset(**payload)
- assert dataset.embedding_model.startswith("BAAI/bge-small-en-v1.5@Local@Builtin") if IS_GO_PROXY else dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, permission",
- [
- ("me", "me"),
- ("team", "team"),
- ],
- ids=["me", "team"],
- )
- def test_permission(self, client, name, permission):
- payload = {"name": name, "permission": permission}
- dataset = client.create_dataset(**payload)
- assert dataset.permission == permission.lower().strip(), str(dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, permission",
- [
- ("empty", ""),
- ("unknown", "unknown"),
- ("me_upercase", "ME"),
- ("team_upercase", "TEAM"),
- ("whitespace", " ME "),
- ],
- ids=["empty", "unknown", "me_upercase", "team_upercase", "whitespace"],
- )
- def test_permission_invalid(self, client, name, permission):
- payload = {"name": name, "permission": permission}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert "Input should be 'me' or 'team'" in str(exception_info.value)
-
- @pytest.mark.p2
- def test_permission_unset(self, client):
- payload = {"name": "permission_unset"}
- dataset = client.create_dataset(**payload)
- assert dataset.permission == "me", str(dataset)
-
- @pytest.mark.p3
- def test_permission_none(self, client):
- payload = {"name": "permission_none", "permission": None}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert "not instance of" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "name, chunk_method",
- [
- ("naive", "naive"),
- ("book", "book"),
- ("email", "email"),
- ("laws", "laws"),
- ("manual", "manual"),
- ("one", "one"),
- ("paper", "paper"),
- ("picture", "picture"),
- ("presentation", "presentation"),
- ("qa", "qa"),
- ("table", "table"),
- ("tag", "tag"),
- ("resume", "resume"),
- ],
- ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table", "tag", "resume"],
- )
- def test_chunk_method(self, client, name, chunk_method):
- payload = {"name": name, "chunk_method": chunk_method}
- dataset = client.create_dataset(**payload)
- assert dataset.chunk_method == chunk_method, str(dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, chunk_method",
- [
- ("empty", ""),
- ("unknown", "unknown"),
- ],
- ids=["empty", "unknown"],
- )
- def test_chunk_method_invalid(self, client, name, chunk_method):
- payload = {"name": name, "chunk_method": chunk_method}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- error_message = str(exception_info.value)
- if IS_GO_PROXY:
- assert error_message.startswith("Input should be 'naive', 'book'") and error_message.endswith("or 'tag'"), error_message
- else:
- assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in error_message, error_message
-
- @pytest.mark.p2
- def test_chunk_method_unset(self, client):
- payload = {"name": "chunk_method_unset"}
- dataset = client.create_dataset(**payload)
- assert dataset.chunk_method == "naive", str(dataset)
-
- @pytest.mark.p3
- def test_chunk_method_none(self, client):
- payload = {"name": "chunk_method_none", "chunk_method": None}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert "not instance of" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "name, parser_config",
- [
- ("auto_keywords_min", {"auto_keywords": 0}),
- ("auto_keywords_mid", {"auto_keywords": 16}),
- ("auto_keywords_max", {"auto_keywords": 32}),
- ("auto_questions_min", {"auto_questions": 0}),
- ("auto_questions_mid", {"auto_questions": 5}),
- ("auto_questions_max", {"auto_questions": 10}),
- ("chunk_token_num_min", {"chunk_token_num": 1}),
- ("chunk_token_num_mid", {"chunk_token_num": 1024}),
- ("chunk_token_num_max", {"chunk_token_num": 2048}),
- ("delimiter", {"delimiter": "\n"}),
- ("delimiter_space", {"delimiter": " "}),
- ("html4excel_true", {"html4excel": True}),
- ("html4excel_false", {"html4excel": False}),
- ("layout_recognize_DeepDOC", {"layout_recognize": "DeepDOC"}),
- ("layout_recognize_navie", {"layout_recognize": "Plain Text"}),
- ("tag_kb_ids", {"tag_kb_ids": ["1", "2"]}),
- ("topn_tags_min", {"topn_tags": 1}),
- ("topn_tags_mid", {"topn_tags": 5}),
- ("topn_tags_max", {"topn_tags": 10}),
- ("filename_embd_weight_min", {"filename_embd_weight": 0.1}),
- ("filename_embd_weight_mid", {"filename_embd_weight": 0.5}),
- ("filename_embd_weight_max", {"filename_embd_weight": 1.0}),
- ("task_page_size_min", {"task_page_size": 1}),
- ("task_page_size_None", {"task_page_size": None}),
- ("pages", {"pages": [[1, 100]]}),
- ("pages_none", {"pages": None}),
- ("graphrag_true", {"graphrag": {"use_graphrag": True}}),
- ("graphrag_false", {"graphrag": {"use_graphrag": False}}),
- ("graphrag_entity_types", {"graphrag": {"entity_types": ["age", "sex", "height", "weight"]}}),
- ("graphrag_method_general", {"graphrag": {"method": "general"}}),
- ("graphrag_method_light", {"graphrag": {"method": "light"}}),
- ("graphrag_community_true", {"graphrag": {"community": True}}),
- ("graphrag_community_false", {"graphrag": {"community": False}}),
- ("graphrag_resolution_true", {"graphrag": {"resolution": True}}),
- ("graphrag_resolution_false", {"graphrag": {"resolution": False}}),
- ("raptor_true", {"raptor": {"use_raptor": True}}),
- ("raptor_false", {"raptor": {"use_raptor": False}}),
- ("raptor_prompt", {"raptor": {"prompt": "Who are you?"}}),
- ("raptor_max_token_min", {"raptor": {"max_token": 512}}),
- ("raptor_max_token_mid", {"raptor": {"max_token": 1024}}),
- ("raptor_max_token_max", {"raptor": {"max_token": 2048}}),
- ("raptor_clustering_threshold_min", {"raptor": {"clustering_threshold": 0.0}}),
- ("raptor_clustering_threshold_mid", {"raptor": {"clustering_threshold": 0.5}}),
- ("raptor_clustering_threshold_max", {"raptor": {"clustering_threshold": 1.0}}),
- ("raptor_max_cluster_min", {"raptor": {"max_cluster": 1}}),
- ("raptor_max_cluster_mid", {"raptor": {"max_cluster": 512}}),
- ("raptor_max_cluster_max", {"raptor": {"max_cluster": 1024}}),
- ("raptor_random_seed_min", {"raptor": {"random_seed": 0}}),
- ],
- ids=[
- "auto_keywords_min",
- "auto_keywords_mid",
- "auto_keywords_max",
- "auto_questions_min",
- "auto_questions_mid",
- "auto_questions_max",
- "chunk_token_num_min",
- "chunk_token_num_mid",
- "chunk_token_num_max",
- "delimiter",
- "delimiter_space",
- "html4excel_true",
- "html4excel_false",
- "layout_recognize_DeepDOC",
- "layout_recognize_navie",
- "tag_kb_ids",
- "topn_tags_min",
- "topn_tags_mid",
- "topn_tags_max",
- "filename_embd_weight_min",
- "filename_embd_weight_mid",
- "filename_embd_weight_max",
- "task_page_size_min",
- "task_page_size_None",
- "pages",
- "pages_none",
- "graphrag_true",
- "graphrag_false",
- "graphrag_entity_types",
- "graphrag_method_general",
- "graphrag_method_light",
- "graphrag_community_true",
- "graphrag_community_false",
- "graphrag_resolution_true",
- "graphrag_resolution_false",
- "raptor_true",
- "raptor_false",
- "raptor_prompt",
- "raptor_max_token_min",
- "raptor_max_token_mid",
- "raptor_max_token_max",
- "raptor_clustering_threshold_min",
- "raptor_clustering_threshold_mid",
- "raptor_clustering_threshold_max",
- "raptor_max_cluster_min",
- "raptor_max_cluster_mid",
- "raptor_max_cluster_max",
- "raptor_random_seed_min",
- ],
- )
- def test_parser_config(self, client, name, parser_config):
- parser_config_o = DataSet.ParserConfig(client, parser_config)
- payload = {"name": name, "parser_config": parser_config_o}
- dataset = client.create_dataset(**payload)
- for k, v in parser_config.items():
- if k in {"graphrag", "raptor"}:
- assert not hasattr(dataset.parser_config, k), str(dataset)
- continue
- if isinstance(v, dict):
- for kk, vv in v.items():
- assert attrgetter(f"{k}.{kk}")(dataset.parser_config) == vv, str(dataset)
- else:
- assert attrgetter(k)(dataset.parser_config) == v, str(dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, parser_config, expected_message",
- [
- ("auto_keywords_min_limit", {"auto_keywords": -1}, "Input should be greater than or equal to 0"),
- ("auto_keywords_max_limit", {"auto_keywords": 33}, "Input should be less than or equal to 32"),
- ("auto_keywords_float_not_allowed", {"auto_keywords": 3.14}, "Input should be a valid integer"),
- ("auto_keywords_type_invalid", {"auto_keywords": "string"}, "Input should be a valid integer"),
- ("auto_questions_min_limit", {"auto_questions": -1}, "Input should be greater than or equal to 0"),
- ("auto_questions_max_limit", {"auto_questions": 11}, "Input should be less than or equal to 10"),
- ("auto_questions_float_not_allowed", {"auto_questions": 3.14}, "Input should be a valid integer"),
- ("auto_questions_type_invalid", {"auto_questions": "string"}, "Input should be a valid integer"),
- ("chunk_token_num_min_limit", {"chunk_token_num": 0}, "Input should be greater than or equal to 1"),
- ("chunk_token_num_max_limit", {"chunk_token_num": 2049}, "Input should be less than or equal to 2048"),
- ("chunk_token_num_float_not_allowed", {"chunk_token_num": 3.14}, "Input should be a valid integer"),
- ("chunk_token_num_type_invalid", {"chunk_token_num": "string"}, "Input should be a valid integer"),
- ("delimiter_empty", {"delimiter": ""}, "String should have at least 1 character"),
- ("html4excel_type_invalid", {"html4excel": "string"}, "Input should be a valid boolean"),
- ("tag_kb_ids_not_list", {"tag_kb_ids": "1,2"}, "Input should be a valid list"),
- ("tag_kb_ids_int_in_list", {"tag_kb_ids": [1, 2]}, "Input should be a valid string"),
- ("topn_tags_min_limit", {"topn_tags": 0}, "Input should be greater than or equal to 1"),
- ("topn_tags_max_limit", {"topn_tags": 11}, "Input should be less than or equal to 10"),
- ("topn_tags_float_not_allowed", {"topn_tags": 3.14}, "Input should be a valid integer"),
- ("topn_tags_type_invalid", {"topn_tags": "string"}, "Input should be a valid integer"),
- ("filename_embd_weight_min_limit", {"filename_embd_weight": -1}, "Input should be greater than or equal to 0"),
- ("filename_embd_weight_max_limit", {"filename_embd_weight": 1.1}, "Input should be less than or equal to 1"),
- ("filename_embd_weight_type_invalid", {"filename_embd_weight": "string"}, "Input should be a valid number"),
- ("task_page_size_min_limit", {"task_page_size": 0}, "Input should be greater than or equal to 1"),
- ("task_page_size_float_not_allowed", {"task_page_size": 3.14}, "Input should be a valid integer"),
- ("task_page_size_type_invalid", {"task_page_size": "string"}, "Input should be a valid integer"),
- ("pages_not_list", {"pages": "1,2"}, "Input should be a valid list"),
- ("pages_not_list_in_list", {"pages": ["1,2"]}, "Input should be a valid list"),
- ("pages_not_int_list", {"pages": [["string1", "string2"]]}, "Input should be a valid integer"),
- ("graphrag_type_invalid", {"graphrag": {"use_graphrag": "string"}}, "Input should be a valid boolean"),
- ("graphrag_entity_types_not_list", {"graphrag": {"entity_types": "1,2"}}, "Input should be a valid list"),
- ("graphrag_entity_types_not_str_in_list", {"graphrag": {"entity_types": [1, 2]}}, "nput should be a valid string"),
- ("graphrag_method_unknown", {"graphrag": {"method": "unknown"}}, "Input should be 'light', 'general' or 'ner'"),
- ("graphrag_method_none", {"graphrag": {"method": None}}, "Input should be 'light', 'general' or 'ner'"),
- ("graphrag_community_type_invalid", {"graphrag": {"community": "string"}}, "Input should be a valid boolean"),
- ("graphrag_resolution_type_invalid", {"graphrag": {"resolution": "string"}}, "Input should be a valid boolean"),
- ("raptor_type_invalid", {"raptor": {"use_raptor": "string"}}, "Input should be a valid boolean"),
- ("raptor_prompt_empty", {"raptor": {"prompt": ""}}, "String should have at least 1 character"),
- ("raptor_prompt_space", {"raptor": {"prompt": " "}}, "String should have at least 1 character"),
- ("raptor_max_token_max_limit", {"raptor": {"max_token": 2049}}, "Input should be less than or equal to 2048"),
- ("raptor_max_token_type_invalid", {"raptor": {"max_token": "string"}}, "Input should be a valid integer"),
- ("raptor_clustering_threshold_min_limit", {"raptor": {"clustering_threshold": -0.1}}, "Input should be greater than or equal to 0"),
- ("raptor_clustering_threshold_max_limit", {"raptor": {"clustering_threshold": 1.1}}, "Input should be less than or equal to 1"),
- ("raptor_clustering_threshold_type_invalid", {"raptor": {"clustering_threshold": "string"}}, "Input should be a valid number"),
- ("raptor_max_cluster_min_limit", {"raptor": {"max_cluster": 0}}, "Input should be greater than or equal to 1"),
- ("raptor_max_cluster_max_limit", {"raptor": {"max_cluster": 1025}}, "Input should be less than or equal to 1024"),
- ("raptor_max_cluster_float_not_allowed", {"raptor": {"max_cluster": 3.14}}, "Input should be a valid integer"),
- ("raptor_max_cluster_type_invalid", {"raptor": {"max_cluster": "string"}}, "Input should be a valid integer"),
- ("raptor_random_seed_min_limit", {"raptor": {"random_seed": -1}}, "Input should be greater than or equal to 0"),
- ("raptor_random_seed_float_not_allowed", {"raptor": {"random_seed": 3.14}}, "Input should be a valid integer"),
- ("raptor_random_seed_type_invalid", {"raptor": {"random_seed": "string"}}, "Input should be a valid integer"),
- ("parser_config_type_invalid", {"delimiter": "a" * 65536}, "Parser config exceeds size limit (max 65,535 characters)"),
- ],
- ids=[
- "auto_keywords_min_limit",
- "auto_keywords_max_limit",
- "auto_keywords_float_not_allowed",
- "auto_keywords_type_invalid",
- "auto_questions_min_limit",
- "auto_questions_max_limit",
- "auto_questions_float_not_allowed",
- "auto_questions_type_invalid",
- "chunk_token_num_min_limit",
- "chunk_token_num_max_limit",
- "chunk_token_num_float_not_allowed",
- "chunk_token_num_type_invalid",
- "delimiter_empty",
- "html4excel_type_invalid",
- "tag_kb_ids_not_list",
- "tag_kb_ids_int_in_list",
- "topn_tags_min_limit",
- "topn_tags_max_limit",
- "topn_tags_float_not_allowed",
- "topn_tags_type_invalid",
- "filename_embd_weight_min_limit",
- "filename_embd_weight_max_limit",
- "filename_embd_weight_type_invalid",
- "task_page_size_min_limit",
- "task_page_size_float_not_allowed",
- "task_page_size_type_invalid",
- "pages_not_list",
- "pages_not_list_in_list",
- "pages_not_int_list",
- "graphrag_type_invalid",
- "graphrag_entity_types_not_list",
- "graphrag_entity_types_not_str_in_list",
- "graphrag_method_unknown",
- "graphrag_method_none",
- "graphrag_community_type_invalid",
- "graphrag_resolution_type_invalid",
- "raptor_type_invalid",
- "raptor_prompt_empty",
- "raptor_prompt_space",
- "raptor_max_token_max_limit",
- "raptor_max_token_type_invalid",
- "raptor_clustering_threshold_min_limit",
- "raptor_clustering_threshold_max_limit",
- "raptor_clustering_threshold_type_invalid",
- "raptor_max_cluster_min_limit",
- "raptor_max_cluster_max_limit",
- "raptor_max_cluster_float_not_allowed",
- "raptor_max_cluster_type_invalid",
- "raptor_random_seed_min_limit",
- "raptor_random_seed_float_not_allowed",
- "raptor_random_seed_type_invalid",
- "parser_config_type_invalid",
- ],
- )
- def test_parser_config_invalid(self, client, name, parser_config, expected_message):
- parser_config_o = DataSet.ParserConfig(client, parser_config)
- payload = {"name": name, "parser_config": parser_config_o}
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_parser_config_empty(self, client):
- excepted_value = DataSet.ParserConfig(
- client,
- DEFAULT_PARSER_CONFIG,
- )
- parser_config_o = DataSet.ParserConfig(client, {})
- payload = {"name": "parser_config_empty", "parser_config": parser_config_o}
- dataset = client.create_dataset(**payload)
- assert str(dataset.parser_config) == str(excepted_value), str(dataset)
-
- @pytest.mark.p2
- def test_parser_config_unset(self, client):
- excepted_value = DataSet.ParserConfig(
- client,
- DEFAULT_PARSER_CONFIG,
- )
- payload = {"name": "parser_config_unset"}
- dataset = client.create_dataset(**payload)
- assert str(dataset.parser_config) == str(excepted_value), str(dataset)
-
- @pytest.mark.p3
- def test_parser_config_none(self, client):
- excepted_value = DataSet.ParserConfig(
- client,
- DEFAULT_PARSER_CONFIG,
- )
- payload = {"name": "parser_config_empty", "parser_config": None}
- dataset = client.create_dataset(**payload)
- assert str(dataset.parser_config) == str(excepted_value), str(dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload",
- [
- {"name": "id", "id": "id"},
- {"name": "tenant_id", "tenant_id": "e57c1966f99211efb41e9e45646e0111"},
- {"name": "created_by", "created_by": "created_by"},
- {"name": "create_date", "create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"name": "create_time", "create_time": 1741671443322},
- {"name": "update_date", "update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"name": "update_time", "update_time": 1741671443339},
- {"name": "document_count", "document_count": 1},
- {"name": "chunk_count", "chunk_count": 1},
- {"name": "token_num", "token_num": 1},
- {"name": "status", "status": "1"},
- {"name": "pagerank", "pagerank": 50},
- {"name": "unknown_field", "unknown_field": "unknown_field"},
- ],
- )
- def test_unsupported_field(self, client, payload):
- with pytest.raises(Exception) as exception_info:
- client.create_dataset(**payload)
- assert "got an unexpected keyword argument" in str(exception_info.value), str(exception_info.value)
-
-
[email protected]("clear_datasets")
-class TestParserConfigBugFix:
- @pytest.mark.p1
- def test_parser_config_missing_raptor_and_graphrag(self, client):
- parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024})
- payload = {"name": "test_parser_config_missing_fields_sdk", "parser_config": parser_config}
- dataset = client.create_dataset(**payload)
-
- config = dataset.parser_config
- assert not hasattr(config, "raptor"), "raptor field should not be exposed"
- assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
- assert config.chunk_token_num == 1024, "User-provided chunk_token_num should be preserved"
-
- @pytest.mark.p1
- def test_parser_config_with_only_raptor(self, client):
- parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024, "raptor": {"use_raptor": True}})
- payload = {"name": "test_parser_config_only_raptor_sdk", "parser_config": parser_config}
- dataset = client.create_dataset(**payload)
-
- config = dataset.parser_config
- assert not hasattr(config, "raptor"), "raptor field should not be exposed"
- assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
-
- @pytest.mark.p1
- def test_parser_config_with_only_graphrag(self, client):
- parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024, "graphrag": {"use_graphrag": True}})
- payload = {"name": "test_parser_config_only_graphrag_sdk", "parser_config": parser_config}
- dataset = client.create_dataset(**payload)
-
- config = dataset.parser_config
- assert not hasattr(config, "raptor"), "raptor field should not be exposed"
- assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
-
- @pytest.mark.p1
- def test_parser_config_with_both_fields(self, client):
- parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 1024, "raptor": {"use_raptor": True}, "graphrag": {"use_graphrag": True}})
- payload = {"name": "test_parser_config_both_fields_sdk", "parser_config": parser_config}
- dataset = client.create_dataset(**payload)
-
- config = dataset.parser_config
- assert not hasattr(config, "raptor"), "raptor field should not be exposed"
- assert not hasattr(config, "graphrag"), "graphrag field should not be exposed"
-
- @pytest.mark.p2
- @pytest.mark.parametrize("chunk_method", ["qa", "manual", "paper", "book", "laws", "presentation"])
- def test_parser_config_different_chunk_methods(self, client, chunk_method):
- parser_config = DataSet.ParserConfig(client, {"chunk_token_num": 512})
- payload = {"name": f"test_parser_config_{chunk_method}_sdk", "chunk_method": chunk_method, "parser_config": parser_config}
- dataset = client.create_dataset(**payload)
-
- config = dataset.parser_config
- assert not hasattr(config, "raptor"), f"raptor field should not be exposed for {chunk_method}"
- assert not hasattr(config, "graphrag"), f"graphrag field should not be exposed for {chunk_method}"
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_delete_datasets.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_delete_datasets.py
deleted file mode 100644
index cf7488fa45..0000000000
--- a/test/testcases/test_sdk_api/test_dataset_mangement/test_delete_datasets.py
+++ /dev/null
@@ -1,179 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import uuid
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_create_datasets
-from configs import HOST_ADDRESS, INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE
-from ragflow_sdk import RAGFlow
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets()
- assert str(exception_info.value) == expected_message
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_delete_dataset_1k(self, client):
- datasets = batch_create_datasets(client, 1_000)
- client.delete_datasets(**{"ids": [dataset.id for dataset in datasets]})
-
- datasets = client.list_datasets()
- assert len(datasets) == 0, datasets
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, client):
- count = 1_000
- datasets = batch_create_datasets(client, count)
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(client.delete_datasets, **{"ids": [dataset.id for dataset in datasets][i : i + 1]}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- datasets = client.list_datasets()
- assert len(datasets) == 0, datasets
-
-
-class TestDatasetsDelete:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "func, remaining",
- [
- (lambda r: {"ids": r[:1]}, 2),
- (lambda r: {"ids": r}, 0),
- ],
- ids=["single_dataset", "multiple_datasets"],
- )
- def test_ids(self, client, add_datasets_func, func, remaining):
- payload = None
- if callable(func):
- payload = func([dataset.id for dataset in add_datasets_func])
- client.delete_datasets(**payload)
-
- datasets = client.list_datasets()
- assert len(datasets) == remaining, str(datasets)
-
- @pytest.mark.p2
- @pytest.mark.usefixtures("add_dataset_func")
- def test_ids_empty(self, client):
- payload = {"ids": []}
- client.delete_datasets(**payload)
-
- datasets = client.list_datasets()
- assert len(datasets) == 1, str(datasets)
-
- @pytest.mark.p3
- @pytest.mark.usefixtures("add_datasets_func")
- def test_ids_none(self, client):
- payload = {"ids": None}
- client.delete_datasets(**payload)
-
- datasets = client.list_datasets()
- assert len(datasets) == 3, str(datasets)
-
- @pytest.mark.p2
- @pytest.mark.usefixtures("add_dataset_func")
- def test_id_not_uuid(self, client):
- payload = {"ids": ["not_uuid"]}
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets(**payload)
- assert "Invalid UUID format" in str(exception_info.value), str(exception_info.value)
-
- datasets = client.list_datasets()
- assert len(datasets) == 1, str(datasets)
-
- @pytest.mark.p3
- @pytest.mark.usefixtures("add_dataset_func")
- def test_id_not_uuid1(self, client):
- payload = {"ids": [uuid.uuid4().hex]}
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets(**payload)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- @pytest.mark.usefixtures("add_dataset_func")
- def test_id_wrong_uuid(self, client):
- payload = {"ids": ["d94a8dc02c9711f0930f7fbc369eab6d"]}
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets(**payload)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- datasets = client.list_datasets()
- assert len(datasets) == 1, str(datasets)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "func",
- [
- lambda r: {"ids": ["d94a8dc02c9711f0930f7fbc369eab6d"] + r},
- lambda r: {"ids": r[:1] + ["d94a8dc02c9711f0930f7fbc369eab6d"] + r[1:3]},
- lambda r: {"ids": r + ["d94a8dc02c9711f0930f7fbc369eab6d"]},
- ],
- )
- def test_ids_partial_invalid(self, client, add_datasets_func, func):
- if callable(func):
- payload = func([dataset.id for dataset in add_datasets_func])
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets(**payload)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- datasets = client.list_datasets()
- assert len(datasets) == 3, str(datasets)
-
- @pytest.mark.p2
- def test_ids_duplicate(self, client, add_datasets_func):
- dataset_ids = [dataset.id for dataset in add_datasets_func]
- payload = {"ids": dataset_ids + dataset_ids}
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets(**payload)
- assert "Duplicate ids:" in str(exception_info.value), str(exception_info.value)
-
- datasets = client.list_datasets()
- assert len(datasets) == 3, str(datasets)
-
- @pytest.mark.p2
- def test_repeated_delete(self, client, add_datasets_func):
- dataset_ids = [dataset.id for dataset in add_datasets_func]
- payload = {"ids": dataset_ids}
- client.delete_datasets(**payload)
-
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets(**payload)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- @pytest.mark.usefixtures("add_dataset_func")
- def test_field_unsupported(self, client):
- payload = {"unknown_field": "unknown_field"}
- with pytest.raises(Exception) as exception_info:
- client.delete_datasets(**payload)
- assert "got an unexpected keyword argument 'unknown_field'" in str(exception_info.value), str(exception_info.value)
-
- datasets = client.list_datasets()
- assert len(datasets) == 1, str(datasets)
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_list_datasets.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_list_datasets.py
deleted file mode 100644
index 60e7e405d3..0000000000
--- a/test/testcases/test_sdk_api/test_dataset_mangement/test_list_datasets.py
+++ /dev/null
@@ -1,370 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import uuid
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from configs import HOST_ADDRESS, INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE
-from ragflow_sdk import RAGFlow
-
-
-class _ListDatasetsResponse:
- def json(self):
- return {"code": 0, "data": []}
-
-
-class TestListDatasetsArgumentValidation:
- @pytest.fixture(autouse=True)
- def set_tenant_info(self):
- return None
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params",
- [{"id": "dataset-id", "ids": ["dataset-id"]}, {"id": "dataset-id", "ids": [""]}],
- )
- def test_rejects_conflicting_id_and_ids_before_get(self, monkeypatch, params):
- client = RAGFlow("token", HOST_ADDRESS)
- get_calls = []
- monkeypatch.setattr(client, "get", lambda *_args, **_kwargs: get_calls.append((_args, _kwargs)))
-
- with pytest.raises(ValueError, match="^Cannot use both 'id' and 'ids' parameters at the same time\\.$"):
- client.list_datasets(**params)
-
- assert get_calls == []
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params",
- [{"id": "dataset-id"}, {"ids": ["dataset-id"]}, {"id": None, "ids": None}, {"id": "dataset-id", "ids": []}, {"id": "", "ids": ["dataset-id"]}],
- )
- def test_preserves_nonconflicting_id_and_ids(self, monkeypatch, params):
- client = RAGFlow("token", HOST_ADDRESS)
- get_calls = []
-
- def _get(path, params=None, json=None):
- get_calls.append((path, params, json))
- return _ListDatasetsResponse()
-
- monkeypatch.setattr(client, "get", _get)
- assert client.list_datasets(**params) == []
- assert get_calls == [
- (
- "/datasets",
- {"page": 1, "page_size": 30, "orderby": "create_time", "desc": True, "id": params.get("id"), "ids": params.get("ids"), "name": None},
- None,
- )
- ]
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.list_datasets()
- assert expected_message in str(exception_info.value)
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_concurrent_list(self, client):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- client.list_datasets,
- )
- for _ in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
-
[email protected]("add_datasets")
-class TestDatasetsList:
- @pytest.mark.p2
- def test_params_unset(self, client):
- datasets = client.list_datasets()
- assert len(datasets) == 5, str(datasets)
-
- @pytest.mark.p2
- def test_params_empty(self, client):
- datasets = client.list_datasets(**{})
- assert len(datasets) == 5, str(datasets)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page": 2, "page_size": 2}, 2),
- ({"page": 3, "page_size": 2}, 1),
- ({"page": 4, "page_size": 2}, 0),
- ({"page": 1, "page_size": 10}, 5),
- ],
- ids=["normal_middle_page", "normal_last_partial_page", "beyond_max_page", "full_data_single_page"],
- )
- def test_page(self, client, params, expected_page_size):
- datasets = client.list_datasets(**params)
- assert len(datasets) == expected_page_size, str(datasets)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"page": 0}, "Input should be greater than or equal to 1"),
- ({"page": "a"}, "not instance of"),
- ],
- ids=["page_0", "page_a"],
- )
- def test_page_invalid(self, client, params, expected_message):
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_page_none(self, client):
- params = {"page": None}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "not instance of" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page_size": 1}, 1),
- ({"page_size": 3}, 3),
- ({"page_size": 5}, 5),
- ({"page_size": 6}, 5),
- ],
- ids=["min_valid_page_size", "medium_page_size", "page_size_equals_total", "page_size_exceeds_total"],
- )
- def test_page_size(self, client, params, expected_page_size):
- datasets = client.list_datasets(**params)
- assert len(datasets) == expected_page_size, str(datasets)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"page_size": 0}, "Input should be greater than or equal to 1"),
- ({"page_size": "a"}, "not instance of"),
- ],
- )
- def test_page_size_invalid(self, client, params, expected_message):
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_page_size_none(self, client):
- params = {"page_size": None}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "not instance of" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params",
- [
- {"orderby": "create_time"},
- {"orderby": "update_time"},
- ],
- ids=["orderby_create_time", "orderby_update_time"],
- )
- def test_orderby(self, client, params):
- client.list_datasets(**params)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params",
- [
- {"orderby": ""},
- {"orderby": "unknown"},
- {"orderby": "CREATE_TIME"},
- {"orderby": "UPDATE_TIME"},
- {"orderby": " create_time "},
- ],
- ids=["empty", "unknown", "orderby_create_time_upper", "orderby_update_time_upper", "whitespace"],
- )
- def test_orderby_invalid(self, client, params):
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "Input should be 'create_time' or 'update_time'" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_orderby_none(self, client):
- params = {"orderby": None}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "not instance of" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params",
- [
- {"desc": True},
- {"desc": False},
- ],
- ids=["desc=True", "desc=False"],
- )
- def test_desc(self, client, params):
- client.list_datasets(**params)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params",
- [
- {"desc": 3.14},
- {"desc": "unknown"},
- ],
- ids=["float_value", "invalid_string"],
- )
- def test_desc_invalid(self, client, params):
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "not instance of" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_desc_none(self, client):
- params = {"desc": None}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "not instance of" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p1
- def test_name(self, client):
- params = {"name": "dataset_1"}
- datasets = client.list_datasets(**params)
- assert len(datasets) == 1, str(datasets)
- assert datasets[0].name == "dataset_1", str(datasets)
-
- @pytest.mark.p2
- def test_name_wrong(self, client):
- params = {"name": "wrong name"}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_get_dataset_not_found_raises(self, client, monkeypatch):
- monkeypatch.setattr(client, "list_datasets", lambda **_: [])
- with pytest.raises(Exception) as exception_info:
- client.get_dataset(name="missing-name-for-coverage")
- assert "Dataset missing-name-for-coverage not found" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_name_empty(self, client):
- params = {"name": ""}
- datasets = client.list_datasets(**params)
- assert len(datasets) == 5, str(datasets)
-
- @pytest.mark.p2
- def test_name_none(self, client):
- params = {"name": None}
- datasets = client.list_datasets(**params)
- assert len(datasets) == 5, str(datasets)
-
- @pytest.mark.p1
- def test_id(self, client, add_datasets):
- dataset_ids = [dataset.id for dataset in add_datasets]
- params = {"id": dataset_ids[0]}
- datasets = client.list_datasets(**params)
- assert len(datasets) == 1, str(datasets)
- assert datasets[0].id == dataset_ids[0], str(datasets)
-
- @pytest.mark.p2
- def test_id_not_uuid(self, client):
- params = {"id": "not_uuid"}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "Invalid UUID format" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_id_not_uuid1(self, client):
- params = {"id": uuid.uuid4().hex}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_id_wrong_uuid(self, client):
- params = {"id": "d94a8dc02c9711f0930f7fbc369eab6d"}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_id_empty(self, client):
- params = {"id": ""}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "Invalid UUID format" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_id_none(self, client):
- params = {"id": None}
- datasets = client.list_datasets(**params)
- assert len(datasets) == 5, str(datasets)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "func, name, expected_num",
- [
- (lambda r: r[0].id, "dataset_0", 1),
- (lambda r: r[0].id, "dataset_1", 0),
- ],
- ids=["name_and_id_match", "name_and_id_mismatch"],
- )
- def test_name_and_id(self, client, add_datasets, func, name, expected_num):
- params = None
- if callable(func):
- params = {"id": func(add_datasets), "name": name}
- datasets = client.list_datasets(**params)
- assert len(datasets) == expected_num, str(datasets)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "dataset_id, name",
- [
- (lambda r: r[0].id, "wrong_name"),
- (uuid.uuid1().hex, "dataset_0"),
- ],
- ids=["name", "id"],
- )
- def test_name_and_id_wrong(self, client, add_datasets, dataset_id, name):
- if callable(dataset_id):
- params = {"id": dataset_id(add_datasets), "name": name}
- else:
- params = {"id": dataset_id, "name": name}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "lacks permission for dataset" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_field_unsupported(self, client):
- params = {"unknown_field": "unknown_field"}
- with pytest.raises(Exception) as exception_info:
- client.list_datasets(**params)
- assert "got an unexpected keyword argument" in str(exception_info.value), str(exception_info.value)
diff --git a/test/testcases/test_sdk_api/test_dataset_mangement/test_update_dataset.py b/test/testcases/test_sdk_api/test_dataset_mangement/test_update_dataset.py
deleted file mode 100644
index 543245e0dd..0000000000
--- a/test/testcases/test_sdk_api/test_dataset_mangement/test_update_dataset.py
+++ /dev/null
@@ -1,777 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from operator import attrgetter
-
-import pytest
-from configs import DATASET_NAME_LIMIT, IS_GO_PROXY
-from hypothesis import HealthCheck, example, given, settings
-from ragflow_sdk import DataSet
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-from utils.hypothesis_utils import valid_names
-from configs import DEFAULT_PARSER_CONFIG
-from utils.engine_utils import get_doc_engine
-
-
-class TestRquest:
- @pytest.mark.p2
- def test_payload_empty(self, add_dataset_func):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({})
- assert "no properties were modified" in str(exception_info.value), str(exception_info.value)
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_update_dateset_concurrent(self, add_dataset_func):
- dataset = add_dataset_func
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(dataset.update, {"name": f"dataset_{i}"}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
-
-class TestDatasetUpdate:
- @pytest.mark.p1
- @given(name=valid_names())
- @example("a" * 128)
- @settings(max_examples=20, suppress_health_check=[HealthCheck.function_scoped_fixture])
- def test_name(self, client, add_dataset_func, name):
- dataset = add_dataset_func
- payload = {"name": name}
- dataset.update(payload)
- assert dataset.name == name, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.name == name, str(retrieved_dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "String should have at least 1 character"),
- (" ", "String should have at least 1 character"),
- ("a" * (DATASET_NAME_LIMIT + 1), "String should have at most 128 characters"),
- (0, "Input should be a valid string"),
- (None, "Input should be a valid string"),
- ],
- ids=["empty_name", "space_name", "too_long_name", "invalid_name", "None_name"],
- )
- def test_name_invalid(self, add_dataset_func, name, expected_message):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"name": name})
- error_message = str(exception_info.value)
- if IS_GO_PROXY:
- if name is None:
- pytest.skip("Go dataset update ignores an explicit null name")
- if not isinstance(name, str):
- assert "cannot unmarshal" in error_message and ".name" in error_message, error_message
- return
- assert expected_message in error_message, error_message
-
- @pytest.mark.p3
- def test_name_duplicated(self, add_datasets_func):
- datasets = add_datasets_func
- name = "dataset_1"
- with pytest.raises(Exception) as exception_info:
- datasets[0].update({"name": name})
- assert f"Dataset name '{name}' already exists" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_name_case_insensitive(self, add_datasets_func):
- dataset = add_datasets_func[0]
- name = "DATASET_1"
- with pytest.raises(Exception) as exception_info:
- dataset.update({"name": name})
- assert f"Dataset name '{name}' already exists" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_avatar(self, client, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- fn = create_image_file(tmp_path / "ragflow_test.png")
- avatar_data = f"data:image/png;base64,{encode_avatar(fn)}"
- dataset.update({"avatar": avatar_data})
- assert dataset.avatar == avatar_data, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.avatar == avatar_data, str(retrieved_dataset)
-
- @pytest.mark.p3
- def test_avatar_exceeds_limit_length(self, add_dataset_func):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"avatar": "a" * 65536})
- assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "avatar_prefix, expected_message",
- [
- ("", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("data:image/png;base64", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),
- ("invalid_mine_prefix:image/png;base64,", "invalid MIME prefix format. Must start with 'data:'"),
- ("data:unsupported_mine_type;base64,", "Unsupported MIME type. Allowed: ['image/jpeg', 'image/png']"),
- ],
- ids=["empty_prefix", "missing_comma", "unsupported_mine_type", "invalid_mine_type"],
- )
- def test_avatar_invalid_prefix(self, add_dataset_func, tmp_path, avatar_prefix, expected_message):
- dataset = add_dataset_func
- fn = create_image_file(tmp_path / "ragflow_test.png")
- with pytest.raises(Exception) as exception_info:
- dataset.update({"avatar": f"{avatar_prefix}{encode_avatar(fn)}"})
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_avatar_none(self, client, add_dataset_func):
- dataset = add_dataset_func
- dataset.update({"avatar": None})
- assert dataset.avatar is None, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.avatar is None, str(retrieved_dataset)
-
- @pytest.mark.p2
- def test_description(self, client, add_dataset_func):
- dataset = add_dataset_func
- dataset.update({"description": "description"})
- assert dataset.description == "description", str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.description == "description", str(retrieved_dataset)
-
- @pytest.mark.p3
- def test_description_exceeds_limit_length(self, add_dataset_func):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"description": "a" * 65536})
- assert "String should have at most 65535 characters" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- def test_description_none(self, client, add_dataset_func):
- dataset = add_dataset_func
- dataset.update({"description": None})
- assert dataset.description is None, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.description is None, str(retrieved_dataset)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "embedding_model",
- [
- "BAAI/bge-small-en-v1.5@Builtin",
- "embedding-3@CI@ZHIPU-AI",
- ],
- ids=["builtin_baai", "tenant_zhipu"],
- )
- def test_embedding_model(self, client, add_dataset_func, embedding_model):
- dataset = add_dataset_func
- dataset.update({"embedding_model": embedding_model})
- assert dataset.embedding_model == embedding_model, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.embedding_model == embedding_model, str(retrieved_dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("unknown_llm_name", "unknown@ZHIPU-AI"),
- ("unknown_llm_factory", "embedding-3@unknown"),
- ("tenant_no_auth_default_tenant_llm", "text-embedding-v3@Tongyi-Qianwen"),
- ("tenant_no_auth", "text-embedding-3-small@OpenAI"),
- ],
- ids=["unknown_llm_name", "unknown_llm_factory", "tenant_no_auth_default_tenant_llm", "tenant_no_auth"],
- )
- def test_embedding_model_invalid(self, add_dataset_func, name, embedding_model):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"name": name, "embedding_model": embedding_model})
- error_msg = str(exception_info.value)
- assert "not found" in error_msg, error_msg
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, embedding_model",
- [
- ("empty", ""),
- ("space", " "),
- ("missing_at", "BAAI/bge-small-en-v1.5Builtin"),
- ("missing_model_name", "@Builtin"),
- ("missing_provider", "BAAI/bge-small-en-v1.5@"),
- ("whitespace_only_model_name", " @Builtin"),
- ("whitespace_only_provider", "BAAI/bge-small-en-v1.5@ "),
- ],
- ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],
- )
- def test_embedding_model_format(self, add_dataset_func, name, embedding_model):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"name": name, "embedding_model": embedding_model})
- error_msg = str(exception_info.value)
- if IS_GO_PROXY and name in ["empty", "space"]:
- assert "lookup failed: record not found" in error_msg, error_msg
- elif name in ["empty", "space", "missing_at"]:
- assert "embedding model identifier must follow <model_name>@<provider> format" in error_msg, error_msg
- else:
- assert "both model_name and provider must be non-empty strings" in error_msg, error_msg
-
- @pytest.mark.p2
- def test_embedding_model_none(self, client, add_dataset_func):
- dataset = add_dataset_func
- if IS_GO_PROXY:
- pytest.skip("Go dataset update ignores an explicit null embedding_model")
- dataset.update({"embedding_model": None})
- assert dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(retrieved_dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "permission",
- [
- "me",
- "team",
- ],
- ids=["me", "team"],
- )
- def test_permission(self, client, add_dataset_func, permission):
- dataset = add_dataset_func
- dataset.update({"permission": permission})
- assert dataset.permission == permission.lower().strip(), str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.permission == permission.lower().strip(), str(retrieved_dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "permission",
- [
- "",
- "unknown",
- list(),
- "ME",
- "TEAM",
- " ME ",
- ],
- ids=["empty", "unknown", "type_error", "me_upercase", "team_upercase", "whitespace"],
- )
- def test_permission_invalid(self, add_dataset_func, permission):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"permission": permission})
- error_message = str(exception_info.value)
- if IS_GO_PROXY and not isinstance(permission, str):
- assert "cannot unmarshal" in error_message and ".permission" in error_message, error_message
- else:
- assert "Input should be 'me' or 'team'" in error_message, error_message
-
- @pytest.mark.p3
- def test_permission_none(self, add_dataset_func):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"permission": None})
- assert "Input should be 'me' or 'team'" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "chunk_method",
- [
- "naive",
- "book",
- "email",
- "laws",
- "manual",
- "one",
- "paper",
- "picture",
- "presentation",
- "qa",
- "table",
- "tag",
- ],
- ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table", "tag"],
- )
- def test_chunk_method(self, client, add_dataset_func, chunk_method):
- dataset = add_dataset_func
- dataset.update({"chunk_method": chunk_method})
- assert dataset.chunk_method == chunk_method, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.chunk_method == chunk_method, str(retrieved_dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "chunk_method",
- [
- "",
- "unknown",
- list(),
- ],
- ids=["empty", "unknown", "type_error"],
- )
- def test_chunk_method_invalid(self, add_dataset_func, chunk_method):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"chunk_method": chunk_method})
- error_message = str(exception_info.value)
- if IS_GO_PROXY and not isinstance(chunk_method, str):
- assert "cannot unmarshal" in error_message and ".chunk_method" in error_message, error_message
- elif IS_GO_PROXY:
- assert error_message.startswith("Input should be 'naive', 'book'") and error_message.endswith("or 'tag'"), error_message
- else:
- assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in error_message, error_message
-
- @pytest.mark.p3
- def test_chunk_method_none(self, add_dataset_func):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"chunk_method": None})
- assert "Input should be 'naive', 'book', 'email', 'laws', 'manual', 'one', 'paper', 'picture', 'presentation', 'qa', 'table', 'tag' or 'resume'" in str(exception_info.value), str(
- exception_info.value
- )
-
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="#8208")
- @pytest.mark.p2
- @pytest.mark.parametrize("pagerank", [0, 50, 100], ids=["min", "mid", "max"])
- def test_pagerank(self, client, add_dataset_func, pagerank):
- if get_doc_engine(client) == "infinity":
- pytest.skip("#8208")
- dataset = add_dataset_func
- dataset.update({"pagerank": pagerank})
- assert dataset.pagerank == pagerank, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.pagerank == pagerank, str(retrieved_dataset)
-
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="#8208")
- @pytest.mark.p2
- def test_pagerank_set_to_0(self, client, add_dataset_func):
- if get_doc_engine(client) == "infinity":
- pytest.skip("#8208")
- dataset = add_dataset_func
- dataset.update({"pagerank": 50})
- assert dataset.pagerank == 50, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.pagerank == 50, str(retrieved_dataset)
-
- dataset.update({"pagerank": 0})
- assert dataset.pagerank == 0, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.pagerank == 0, str(retrieved_dataset)
-
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") != "infinity", reason="#8208")
- @pytest.mark.p2
- def test_pagerank_infinity(self, client, add_dataset_func):
- if get_doc_engine(client) != "infinity":
- pytest.skip("#8208")
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"pagerank": 50})
- assert "'pagerank' can only be set when doc_engine is elasticsearch" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "pagerank, expected_message",
- [
- (-1, "Input should be greater than or equal to 0"),
- (101, "Input should be less than or equal to 100"),
- ],
- ids=["min_limit", "max_limit"],
- )
- def test_pagerank_invalid(self, add_dataset_func, pagerank, expected_message):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"pagerank": pagerank})
- error_message = str(exception_info.value)
- if IS_GO_PROXY and pagerank == -1 and "less than or equal to 100" in error_message:
- pytest.skip("Go dataset update applies the wrong pagerank bound error for negative values")
- assert expected_message in error_message, error_message
-
- @pytest.mark.p3
- def test_pagerank_none(self, add_dataset_func):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"pagerank": None})
- assert "Input should be a valid integer" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "parser_config",
- [
- {"auto_keywords": 0},
- {"auto_keywords": 16},
- {"auto_keywords": 32},
- {"auto_questions": 0},
- {"auto_questions": 5},
- {"auto_questions": 10},
- {"chunk_token_num": 1},
- {"chunk_token_num": 1024},
- {"chunk_token_num": 2048},
- {"delimiter": "\n"},
- {"delimiter": " "},
- {"html4excel": True},
- {"html4excel": False},
- {"layout_recognize": "DeepDOC"},
- {"layout_recognize": "Plain Text"},
- {"tag_kb_ids": ["1", "2"]},
- {"topn_tags": 1},
- {"topn_tags": 5},
- {"topn_tags": 10},
- {"filename_embd_weight": 0.1},
- {"filename_embd_weight": 0.5},
- {"filename_embd_weight": 1.0},
- {"task_page_size": 1},
- {"task_page_size": None},
- {"pages": [[1, 100]]},
- {"pages": None},
- {"graphrag": {"use_graphrag": True}},
- {"graphrag": {"use_graphrag": False}},
- {"graphrag": {"entity_types": ["age", "sex", "height", "weight"]}},
- {"graphrag": {"method": "general"}},
- {"graphrag": {"method": "light"}},
- {"graphrag": {"community": True}},
- {"graphrag": {"community": False}},
- {"graphrag": {"resolution": True}},
- {"graphrag": {"resolution": False}},
- {"raptor": {"use_raptor": True}},
- {"raptor": {"use_raptor": False}},
- {"raptor": {"prompt": "Who are you?"}},
- {"raptor": {"max_token": 512}},
- {"raptor": {"max_token": 1024}},
- {"raptor": {"max_token": 2048}},
- {"raptor": {"clustering_threshold": 0.0}},
- {"raptor": {"clustering_threshold": 0.5}},
- {"raptor": {"clustering_threshold": 1.0}},
- {"raptor": {"max_cluster": 1}},
- {"raptor": {"max_cluster": 512}},
- {"raptor": {"max_cluster": 1024}},
- {"raptor": {"random_seed": 0}},
- ],
- ids=[
- "auto_keywords_min",
- "auto_keywords_mid",
- "auto_keywords_max",
- "auto_questions_min",
- "auto_questions_mid",
- "auto_questions_max",
- "chunk_token_num_min",
- "chunk_token_num_mid",
- "chunk_token_num_max",
- "delimiter",
- "delimiter_space",
- "html4excel_true",
- "html4excel_false",
- "layout_recognize_DeepDOC",
- "layout_recognize_navie",
- "tag_kb_ids",
- "topn_tags_min",
- "topn_tags_mid",
- "topn_tags_max",
- "filename_embd_weight_min",
- "filename_embd_weight_mid",
- "filename_embd_weight_max",
- "task_page_size_min",
- "task_page_size_None",
- "pages",
- "pages_none",
- "graphrag_true",
- "graphrag_false",
- "graphrag_entity_types",
- "graphrag_method_general",
- "graphrag_method_light",
- "graphrag_community_true",
- "graphrag_community_false",
- "graphrag_resolution_true",
- "graphrag_resolution_false",
- "raptor_true",
- "raptor_false",
- "raptor_prompt",
- "raptor_max_token_min",
- "raptor_max_token_mid",
- "raptor_max_token_max",
- "raptor_clustering_threshold_min",
- "raptor_clustering_threshold_mid",
- "raptor_clustering_threshold_max",
- "raptor_max_cluster_min",
- "raptor_max_cluster_mid",
- "raptor_max_cluster_max",
- "raptor_random_seed_min",
- ],
- )
- def test_parser_config(self, client, add_dataset_func, parser_config):
- dataset = add_dataset_func
- dataset.update({"parser_config": parser_config})
- for k, v in parser_config.items():
- if k in {"graphrag", "raptor"}:
- assert not hasattr(dataset.parser_config, k), str(dataset)
- continue
- if isinstance(v, dict):
- for kk, vv in v.items():
- assert attrgetter(f"{k}.{kk}")(dataset.parser_config) == vv, str(dataset)
- else:
- assert attrgetter(k)(dataset.parser_config) == v, str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- for k, v in parser_config.items():
- if k in {"graphrag", "raptor"}:
- assert not hasattr(retrieved_dataset.parser_config, k), str(retrieved_dataset)
- continue
- if isinstance(v, dict):
- for kk, vv in v.items():
- assert attrgetter(f"{k}.{kk}")(retrieved_dataset.parser_config) == vv, str(retrieved_dataset)
- else:
- assert attrgetter(k)(retrieved_dataset.parser_config) == v, str(retrieved_dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "parser_config, expected_message",
- [
- ({"auto_keywords": -1}, "Input should be greater than or equal to 0"),
- ({"auto_keywords": 33}, "Input should be less than or equal to 32"),
- ({"auto_keywords": 3.14}, "Input should be a valid integer"),
- ({"auto_keywords": "string"}, "Input should be a valid integer"),
- ({"auto_questions": -1}, "Input should be greater than or equal to 0"),
- ({"auto_questions": 11}, "Input should be less than or equal to 10"),
- ({"auto_questions": 3.14}, "Input should be a valid integer"),
- ({"auto_questions": "string"}, "Input should be a valid integer"),
- ({"chunk_token_num": 0}, "Input should be greater than or equal to 1"),
- ({"chunk_token_num": 2049}, "Input should be less than or equal to 2048"),
- ({"chunk_token_num": 3.14}, "Input should be a valid integer"),
- ({"chunk_token_num": "string"}, "Input should be a valid integer"),
- ({"delimiter": ""}, "String should have at least 1 character"),
- ({"html4excel": "string"}, "Input should be a valid boolean"),
- ({"tag_kb_ids": "1,2"}, "Input should be a valid list"),
- ({"tag_kb_ids": [1, 2]}, "Input should be a valid string"),
- ({"topn_tags": 0}, "Input should be greater than or equal to 1"),
- ({"topn_tags": 11}, "Input should be less than or equal to 10"),
- ({"topn_tags": 3.14}, "Input should be a valid integer"),
- ({"topn_tags": "string"}, "Input should be a valid integer"),
- ({"filename_embd_weight": -1}, "Input should be greater than or equal to 0"),
- ({"filename_embd_weight": 1.1}, "Input should be less than or equal to 1"),
- ({"filename_embd_weight": "string"}, "Input should be a valid number"),
- ({"task_page_size": 0}, "Input should be greater than or equal to 1"),
- ({"task_page_size": 3.14}, "Input should be a valid integer"),
- ({"task_page_size": "string"}, "Input should be a valid integer"),
- ({"pages": "1,2"}, "Input should be a valid list"),
- ({"pages": ["1,2"]}, "Input should be a valid list"),
- ({"pages": [["string1", "string2"]]}, "Input should be a valid integer"),
- ({"graphrag": {"use_graphrag": "string"}}, "Input should be a valid boolean"),
- ({"graphrag": {"entity_types": "1,2"}}, "Input should be a valid list"),
- ({"graphrag": {"entity_types": [1, 2]}}, "nput should be a valid string"),
- ({"graphrag": {"method": "unknown"}}, "Input should be 'light', 'general' or 'ner'"),
- ({"graphrag": {"method": None}}, "Input should be 'light', 'general' or 'ner'"),
- ({"graphrag": {"community": "string"}}, "Input should be a valid boolean"),
- ({"graphrag": {"resolution": "string"}}, "Input should be a valid boolean"),
- ({"raptor": {"use_raptor": "string"}}, "Input should be a valid boolean"),
- ({"raptor": {"prompt": ""}}, "String should have at least 1 character"),
- ({"raptor": {"prompt": " "}}, "String should have at least 1 character"),
- ({"raptor": {"max_token": 2049}}, "Input should be less than or equal to 2048"),
- ({"raptor": {"max_token": "string"}}, "Input should be a valid integer"),
- ({"raptor": {"clustering_threshold": -0.1}}, "Input should be greater than or equal to 0"),
- ({"raptor": {"clustering_threshold": 1.1}}, "Input should be less than or equal to 1"),
- ({"raptor": {"clustering_threshold": "string"}}, "Input should be a valid number"),
- ({"raptor": {"max_cluster": 0}}, "Input should be greater than or equal to 1"),
- ({"raptor": {"max_cluster": 1025}}, "Input should be less than or equal to 1024"),
- ({"raptor": {"max_cluster": 3.14}}, "Input should be a valid integer"),
- ({"raptor": {"max_cluster": "string"}}, "Input should be a valid integer"),
- ({"raptor": {"random_seed": -1}}, "Input should be greater than or equal to 0"),
- ({"raptor": {"random_seed": 3.14}}, "Input should be a valid integer"),
- ({"raptor": {"random_seed": "string"}}, "Input should be a valid integer"),
- ({"delimiter": "a" * 65536}, "Parser config exceeds size limit (max 65,535 characters)"),
- ],
- ids=[
- "auto_keywords_min_limit",
- "auto_keywords_max_limit",
- "auto_keywords_float_not_allowed",
- "auto_keywords_type_invalid",
- "auto_questions_min_limit",
- "auto_questions_max_limit",
- "auto_questions_float_not_allowed",
- "auto_questions_type_invalid",
- "chunk_token_num_min_limit",
- "chunk_token_num_max_limit",
- "chunk_token_num_float_not_allowed",
- "chunk_token_num_type_invalid",
- "delimiter_empty",
- "html4excel_type_invalid",
- "tag_kb_ids_not_list",
- "tag_kb_ids_int_in_list",
- "topn_tags_min_limit",
- "topn_tags_max_limit",
- "topn_tags_float_not_allowed",
- "topn_tags_type_invalid",
- "filename_embd_weight_min_limit",
- "filename_embd_weight_max_limit",
- "filename_embd_weight_type_invalid",
- "task_page_size_min_limit",
- "task_page_size_float_not_allowed",
- "task_page_size_type_invalid",
- "pages_not_list",
- "pages_not_list_in_list",
- "pages_not_int_list",
- "graphrag_type_invalid",
- "graphrag_entity_types_not_list",
- "graphrag_entity_types_not_str_in_list",
- "graphrag_method_unknown",
- "graphrag_method_none",
- "graphrag_community_type_invalid",
- "graphrag_resolution_type_invalid",
- "raptor_type_invalid",
- "raptor_prompt_empty",
- "raptor_prompt_space",
- "raptor_max_token_max_limit",
- "raptor_max_token_type_invalid",
- "raptor_clustering_threshold_min_limit",
- "raptor_clustering_threshold_max_limit",
- "raptor_clustering_threshold_type_invalid",
- "raptor_max_cluster_min_limit",
- "raptor_max_cluster_max_limit",
- "raptor_max_cluster_float_not_allowed",
- "raptor_max_cluster_type_invalid",
- "raptor_random_seed_min_limit",
- "raptor_random_seed_float_not_allowed",
- "raptor_random_seed_type_invalid",
- "parser_config_type_invalid",
- ],
- )
- def test_parser_config_invalid(self, add_dataset_func, parser_config, expected_message):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update({"parser_config": parser_config})
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_parser_config_empty(self, client, add_dataset_func):
- dataset = add_dataset_func
- expected_config = DataSet.ParserConfig(
- client,
- DEFAULT_PARSER_CONFIG,
- )
- dataset.update({"parser_config": {}})
- assert str(dataset.parser_config) == str(expected_config), str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
-
- @pytest.mark.p3
- def test_parser_config_none(self, client, add_dataset_func):
- dataset = add_dataset_func
- expected_config = DataSet.ParserConfig(
- client,
- DEFAULT_PARSER_CONFIG,
- )
- dataset.update({"parser_config": None})
- assert str(dataset.parser_config) == str(expected_config), str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
-
- @pytest.mark.p3
- def test_parser_config_empty_with_chunk_method_change(self, client, add_dataset_func):
- dataset = add_dataset_func
- expected_config = DataSet.ParserConfig(
- client,
- {
- "image_context_size": 0,
- "table_context_size": 0,
- },
- )
- dataset.update({"chunk_method": "qa", "parser_config": {}})
- assert str(dataset.parser_config) == str(expected_config), str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
-
- @pytest.mark.p3
- def test_parser_config_unset_with_chunk_method_change(self, client, add_dataset_func):
- dataset = add_dataset_func
- expected_config = DataSet.ParserConfig(
- client,
- {
- "image_context_size": 0,
- "table_context_size": 0,
- },
- )
- dataset.update({"chunk_method": "qa"})
- assert str(dataset.parser_config) == str(expected_config), str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
-
- @pytest.mark.p3
- def test_parser_config_none_with_chunk_method_change(self, client, add_dataset_func):
- dataset = add_dataset_func
- expected_config = DataSet.ParserConfig(
- client,
- {
- "image_context_size": 0,
- "table_context_size": 0,
- },
- )
- dataset.update({"chunk_method": "qa", "parser_config": None})
- assert str(dataset.parser_config) == str(expected_config), str(dataset)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert str(retrieved_dataset.parser_config) == str(expected_config), str(retrieved_dataset)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload",
- [
- {"id": "id"},
- {"tenant_id": "e57c1966f99211efb41e9e45646e0111"},
- {"created_by": "created_by"},
- {"create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"create_time": 1741671443322},
- {"update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},
- {"update_time": 1741671443339},
- {"document_count": 1},
- {"chunk_count": 1},
- {"token_num": 1},
- {"status": "1"},
- {"unknown_field": "unknown_field"},
- ],
- )
- def test_field_unsupported(self, add_dataset_func, payload):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.update(payload)
- assert "Extra inputs are not permitted" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_field_unset(self, client, add_dataset_func):
- dataset = add_dataset_func
- original_dataset = client.get_dataset(name=dataset.name)
-
- dataset.update({"name": "default_unset"})
-
- updated_dataset = client.get_dataset(name="default_unset")
- assert updated_dataset.avatar == original_dataset.avatar, str(updated_dataset)
- assert updated_dataset.description == original_dataset.description, str(updated_dataset)
- assert updated_dataset.embedding_model == original_dataset.embedding_model, str(updated_dataset)
- assert updated_dataset.permission == original_dataset.permission, str(updated_dataset)
- assert updated_dataset.chunk_method == original_dataset.chunk_method, str(updated_dataset)
- assert updated_dataset.pagerank == original_dataset.pagerank, str(updated_dataset)
- assert str(updated_dataset.parser_config) == str(original_dataset.parser_config), str(updated_dataset)
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py
deleted file mode 100644
index b60f5f2886..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/conftest.py
+++ /dev/null
@@ -1,57 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import pytest
-from common import bulk_upload_documents, delete_all_documents
-from pytest import FixtureRequest
-from ragflow_sdk import DataSet, Document
-
-
[email protected](scope="function")
-def add_document_func(request: FixtureRequest, add_dataset: DataSet, ragflow_tmp_dir) -> tuple[DataSet, Document]:
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, 1, ragflow_tmp_dir)
-
- def cleanup():
- delete_all_documents(dataset)
-
- request.addfinalizer(cleanup)
- return dataset, documents[0]
-
-
[email protected](scope="class")
-def add_documents(request: FixtureRequest, add_dataset: DataSet, ragflow_tmp_dir) -> tuple[DataSet, list[Document]]:
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, 5, ragflow_tmp_dir)
-
- def cleanup():
- delete_all_documents(dataset)
-
- request.addfinalizer(cleanup)
- return dataset, documents
-
-
[email protected](scope="function")
-def add_documents_func(request: FixtureRequest, add_dataset_func: DataSet, ragflow_tmp_dir) -> tuple[DataSet, list[Document]]:
- dataset = add_dataset_func
- documents = bulk_upload_documents(dataset, 3, ragflow_tmp_dir)
-
- def cleanup():
- delete_all_documents(dataset)
-
- request.addfinalizer(cleanup)
- return dataset, documents
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py
deleted file mode 100644
index 8b059aa7d9..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_delete_documents.py
+++ /dev/null
@@ -1,120 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import bulk_upload_documents, list_all_documents
-
-
-class TestDocumentsDeletion:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message, remaining",
- [
- ({"ids": None}, "should either provide doc ids or set delete_all(true), dataset:", 3),
- ({"ids": []}, "should either provide doc ids or set delete_all(true), dataset:", 3),
- ({"ids": ["invalid_id"]}, "These documents do not belong to dataset", 3),
- ({"ids": ["\n!?。;!?\"'"]}, "These documents do not belong to dataset", 3),
- ("not json", "must be a mapping", 3),
- (lambda r: {"ids": r[:1]}, "", 2),
- (lambda r: {"ids": r}, "", 0),
- ],
- )
- def test_basic_scenarios(
- self,
- add_documents_func,
- payload,
- expected_message,
- remaining,
- ):
- dataset, documents = add_documents_func
- if callable(payload):
- payload = payload([document.id for document in documents])
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.delete_documents(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- dataset.delete_documents(**payload)
-
- documents = dataset.list_documents()
- assert len(documents) == remaining, str(documents)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload",
- [
- lambda r: {"ids": ["invalid_id"] + r},
- lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:3]},
- lambda r: {"ids": r + ["invalid_id"]},
- ],
- )
- def test_delete_partial_invalid_id(self, add_documents_func, payload):
- dataset, documents = add_documents_func
- payload = payload([document.id for document in documents])
-
- with pytest.raises(Exception) as exception_info:
- dataset.delete_documents(**payload)
- assert "These documents do not belong to dataset" in str(exception_info.value), str(exception_info.value)
-
- documents = dataset.list_documents()
- assert len(documents) == 3, str(documents)
-
- @pytest.mark.p3
- def test_repeated_deletion(self, add_documents_func):
- dataset, documents = add_documents_func
- document_ids = [document.id for document in documents]
- dataset.delete_documents(ids=document_ids)
- with pytest.raises(Exception) as exception_info:
- dataset.delete_documents(ids=document_ids)
- assert "Document not found" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_duplicate_deletion(self, add_documents_func):
- dataset, documents = add_documents_func
- document_ids = [document.id for document in documents]
- with pytest.raises(Exception) as exception_info:
- dataset.delete_documents(ids=document_ids + document_ids)
- assert "Field: <ids> - Message: <Duplicate ids:" in str(exception_info.value), str(exception_info.value)
- assert len(dataset.list_documents()) == 3, str(dataset.list_documents())
-
-
-def test_concurrent_deletion(add_dataset, tmp_path):
- count = 100
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, count, tmp_path)
-
- def delete_doc(doc_id):
- dataset.delete_documents(ids=[doc_id])
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(delete_doc, doc.id) for doc in documents]
-
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
-
-def test_delete_1k(add_dataset, tmp_path):
- count = 1_000
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, count, tmp_path)
- assert len(list_all_documents(dataset, limit=count + 1)) == count
-
- dataset.delete_documents(ids=[doc.id for doc in documents])
- assert len(dataset.list_documents()) == 0
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py
deleted file mode 100644
index 821f35f958..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_download_document.py
+++ /dev/null
@@ -1,185 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import bulk_upload_documents
-from ragflow_sdk.modules.document import Document
-from utils import compare_by_hash
-
-
-class _DownloadResponse:
- def __init__(self, content, payload, headers=None):
- self.content = content
- self._payload = payload
- self.headers = headers or {}
-
- def json(self):
- if isinstance(self._payload, Exception):
- raise self._payload
- return self._payload
-
-
- "content,payload",
- [
- (b"[]", []),
- (b"[1, 2]", [1, 2]),
- (b"null", None),
- (b'"hello"', "hello"),
- (b"42", 42),
- (
- b'{"code": 0, "message": "file content"}',
- {"code": 0, "message": "file content"},
- ),
- ],
-)
-def test_download_preserves_attached_json_bytes(monkeypatch, content, payload):
- response = _DownloadResponse(
- content,
- payload,
- {"Content-Disposition": 'attachment; filename="sample.json"'},
- )
- document = Document(None, {"id": "doc", "dataset_id": "dataset"})
- monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
-
- assert document.download() == content
-
-
-def test_download_still_raises_api_error_without_attachment(monkeypatch):
- response = _DownloadResponse(
- b'{"code": 102, "message": "document not found"}',
- {"code": 102, "message": "document not found"},
- {"Content-Type": "application/json"},
- )
- document = Document(None, {"id": "doc", "dataset_id": "dataset"})
- monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
-
- with pytest.raises(Exception, match="document not found"):
- document.download()
-
-
-def test_download_raises_api_error_with_extra_response_fields(monkeypatch):
- response = _DownloadResponse(
- b'{"code": 102, "message": "document not found", "data": null}',
- {"code": 102, "message": "document not found", "data": None},
- {"Content-Type": "application/json"},
- )
- document = Document(None, {"id": "doc", "dataset_id": "dataset"})
- monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
-
- with pytest.raises(Exception, match="document not found"):
- document.download()
-
-
- "content,payload",
- [
- (b'{"code": 0, "message": "success"}', {"code": 0, "message": "success"}),
- (b"plain text", ValueError("not JSON")),
- ],
-)
-def test_download_preserves_non_error_content_without_attachment(monkeypatch, content, payload):
- response = _DownloadResponse(content, payload, {"Content-Type": "application/json"})
- document = Document(None, {"id": "doc", "dataset_id": "dataset"})
- monkeypatch.setattr(document, "get", lambda *_args, **_kwargs: response)
-
- assert document.download() == content
-
-
- "generate_test_files",
- [
- "docx",
- "excel",
- "ppt",
- "image",
- "pdf",
- "txt",
- "md",
- "json",
- "eml",
- "html",
- ],
- indirect=True,
-)
-def test_file_type_validation(add_dataset, generate_test_files, request):
- dataset = add_dataset
- fp = generate_test_files[request.node.callspec.params["generate_test_files"]]
- with fp.open("rb") as f:
- blob = f.read()
-
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
-
- for document in documents:
- with fp.with_stem("ragflow_test_download").open("wb") as f:
- f.write(document.download())
-
- assert compare_by_hash(fp, fp.with_stem("ragflow_test_download"))
-
-
-class TestDocumentDownload:
- @pytest.mark.p3
- def test_same_file_repeat(self, add_documents, tmp_path, ragflow_tmp_dir):
- num = 5
- _, documents = add_documents
-
- for i in range(num):
- download_path = tmp_path / f"ragflow_test_download_{i}.txt"
- with download_path.open("wb") as f:
- f.write(documents[0].download())
- assert compare_by_hash(ragflow_tmp_dir / "ragflow_test_upload_0.txt", download_path), f"Downloaded file {i} does not match original"
-
- @pytest.mark.p3
- def test_download_error_json_raises(self, add_documents):
- dataset, documents = add_documents
- document = documents[0]
- invalid_document = document.__class__(
- document.rag,
- {"id": "missing-document-id-for-download", "dataset_id": dataset.id},
- )
- with pytest.raises(Exception) as exception_info:
- invalid_document.download()
- assert str(exception_info.value), exception_info
-
-
-def test_concurrent_download(add_dataset, tmp_path):
- count = 20
- dataset = add_dataset
- documents = bulk_upload_documents(dataset, count, tmp_path)
-
- def download_doc(document, i):
- download_path = tmp_path / f"ragflow_test_download_{i}.txt"
- with download_path.open("wb") as f:
- f.write(document.download())
- # assert compare_by_hash(tmp_path / f"ragflow_test_upload_{i}.txt", download_path), str(download_path)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(download_doc, documents[i], i) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- for i in range(count):
- assert compare_by_hash(
- tmp_path / f"ragflow_test_upload_{i}.txt",
- tmp_path / f"ragflow_test_download_{i}.txt",
- )
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py
deleted file mode 100644
index 7547228397..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_list_documents.py
+++ /dev/null
@@ -1,276 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-
-
-class TestDocumentsList:
- @pytest.mark.p1
- def test_default(self, add_documents):
- dataset, _ = add_documents
- documents = dataset.list_documents()
- assert len(documents) == 5, str(documents)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 2, "not instance of"),
- ({"page": 1, "page_size": 2}, 2, ""),
- ({"page": 2, "page_size": 2}, 2, ""),
- ({"page": 3, "page_size": 2}, 1, ""),
- ({"page": "3", "page_size": 2}, 1, "not instance of"),
- pytest.param(
- {"page": -1, "page_size": 2},
- 0,
- "Invalid page number",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page": "a", "page_size": 2},
- 0,
- "Invalid page value",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page(self, add_documents, params, expected_page_size, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_page_size, str(documents)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page_size": None}, 5, "not instance of"),
- ({"page_size": 2}, 2, ""),
- ({"page_size": 1}, 1, ""),
- ({"page_size": 6}, 5, ""),
- ({"page_size": "1"}, 1, "not instance of"),
- pytest.param(
- {"page_size": -1},
- 0,
- "Invalid page size",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- pytest.param(
- {"page_size": "a"},
- 0,
- "Invalid page size value",
- marks=pytest.mark.skip(reason="issues/5851"),
- ),
- ],
- )
- def test_page_size(self, add_documents, params, expected_page_size, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_page_size, str(documents)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"orderby": None}, "not instance of"),
- ({"orderby": "create_time"}, ""),
- ({"orderby": "update_time"}, ""),
- pytest.param({"orderby": "name", "desc": "False"}, "", marks=pytest.mark.skip(reason="issues/5851")),
- pytest.param({"orderby": "unknown"}, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_orderby(self, add_documents, params, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- dataset.list_documents(**params)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"desc": None}, "not instance of"),
- ({"desc": "true"}, "not instance of"),
- ({"desc": "True"}, "not instance of"),
- ({"desc": True}, ""),
- pytest.param({"desc": "false"}, "", marks=pytest.mark.skip(reason="issues/5851")),
- ({"desc": "False"}, "not instance of"),
- ({"desc": False}, ""),
- ({"desc": "False", "orderby": "update_time"}, "not instance of"),
- pytest.param({"desc": "unknown"}, "desc should be true or false", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_desc(self, add_documents, params, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- dataset.list_documents(**params)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_num",
- [
- ({"keywords": None}, 5),
- ({"keywords": ""}, 5),
- ({"keywords": "0"}, 1),
- ({"keywords": "ragflow_test_upload"}, 5),
- ({"keywords": "unknown"}, 0),
- ],
- )
- def test_keywords(self, add_documents, params, expected_num):
- dataset, _ = add_documents
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_num, str(documents)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_num, expected_message",
- [
- ({"name": None}, 5, ""),
- ({"name": ""}, 5, ""),
- ({"name": "ragflow_test_upload_0.txt"}, 1, ""),
- ({"name": "unknown.txt"}, 0, "you don't own the document unknown.txt"),
- ],
- )
- def test_name(self, add_documents, params, expected_num, expected_message):
- dataset, _ = add_documents
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_num, str(documents)
- if params["name"] not in [None, ""]:
- assert documents[0].name == params["name"], str(documents)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "document_id, expected_num, expected_message",
- [
- (None, 5, ""),
- ("", 5, ""),
- (lambda docs: docs[0].id, 1, ""),
- ("unknown.txt", 0, "you don't own the document unknown.txt"),
- ],
- )
- def test_id(self, add_documents, document_id, expected_num, expected_message):
- dataset, documents = add_documents
- if callable(document_id):
- params = {"id": document_id(documents)}
- else:
- params = {"id": document_id}
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_num, str(documents)
- if params["id"] not in [None, ""]:
- assert documents[0].id == params["id"], str(documents)
-
- @pytest.mark.p1
- def test_ids(self, add_documents):
- dataset, fixture_documents = add_documents
- expected_ids = {document.id for document in fixture_documents[:2]}
-
- documents = dataset.list_documents(ids=list(expected_ids))
-
- assert len(documents) == 2, str(documents)
- assert {document.id for document in documents} == expected_ids, str(documents)
-
- @pytest.mark.p1
- def test_empty_ids_is_unfiltered(self, add_documents):
- dataset, fixture_documents = add_documents
- expected_ids = {document.id for document in fixture_documents}
-
- documents = dataset.list_documents(ids=[])
-
- assert len(documents) == 5, str(documents)
- assert {document.id for document in documents} == expected_ids, str(documents)
-
- @pytest.mark.p3
- def test_id_and_ids_raise_value_error(self, add_documents):
- dataset, documents = add_documents
-
- with pytest.raises(ValueError) as exception_info:
- dataset.list_documents(id=documents[0].id, ids=[documents[1].id])
-
- assert str(exception_info.value) == "Cannot use both 'id' and 'ids' parameters at the same time."
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, name, expected_num, expected_message",
- [
- (lambda docs: docs[0].id, "ragflow_test_upload_0.txt", 1, ""),
- (lambda docs: docs[0].id, "ragflow_test_upload_1.txt", 0, ""),
- (lambda docs: docs[0].id, "unknown", 0, "you don't own the document unknown"),
- ("invalid_id", "ragflow_test_upload_0.txt", 0, "you don't own the document invalid_id"),
- ],
- )
- def test_name_and_id(self, add_documents, document_id, name, expected_num, expected_message):
- dataset, documents = add_documents
- params = {"id": document_id(documents) if callable(document_id) else document_id, "name": name}
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.list_documents(**params)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- documents = dataset.list_documents(**params)
- assert len(documents) == expected_num, str(documents)
-
- @pytest.mark.p3
- def test_concurrent_list(self, add_documents):
- dataset, _ = add_documents
- count = 100
-
- def list_docs():
- return dataset.list_documents()
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_docs) for _ in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- for future in futures:
- docs = future.result()
- assert len(docs) == 5, str(docs)
-
- @pytest.mark.p3
- def test_invalid_params(self, add_documents):
- dataset, _ = add_documents
- params = {"a": "b"}
- with pytest.raises(TypeError) as exception_info:
- dataset.list_documents(**params)
- assert "got an unexpected keyword argument" in str(exception_info.value), str(exception_info.value)
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py
deleted file mode 100644
index c739b0b38f..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_parse_documents.py
+++ /dev/null
@@ -1,293 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-import pytest
-from common import bulk_upload_documents, list_all_documents
-from ragflow_sdk import DataSet
-from ragflow_sdk.modules.document import Document
-from utils import wait_for
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_dataset: DataSet, _document_ids: list[str] = None):
- documents = _dataset.list_documents(page_size=100)
-
- if _document_ids is None:
- for document in documents:
- if document.run != "DONE":
- return False
- return True
-
- target_ids = set(_document_ids)
- for document in documents:
- if document.id in target_ids:
- if document.run != "DONE":
- return False
- return True
-
-
-def validate_document_details(dataset, document_ids):
- target_ids = set(document_ids)
- found_ids = set()
- for document in list_all_documents(dataset):
- if document.id in target_ids:
- found_ids.add(document.id)
- assert document.run == "DONE"
- assert len(document.process_begin_at) > 0
- assert document.process_duration > 0
- assert document.progress > 0
- assert "Task done" in document.progress_msg
- assert found_ids == target_ids
-
-
-class TestDocumentsParse:
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- pytest.param(None, "AttributeError", marks=pytest.mark.skip),
- pytest.param({"document_ids": []}, "`document_ids` is required", marks=pytest.mark.p3),
- pytest.param({"document_ids": ["invalid_id"]}, "Documents not found: ['invalid_id']", marks=pytest.mark.p3),
- pytest.param({"document_ids": ["\n!?。;!?\"'"]}, "Documents not found: ['\\n!?。;!?\"\\'']", marks=pytest.mark.p3),
- pytest.param("not json", "AttributeError", marks=pytest.mark.skip),
- pytest.param(lambda r: {"document_ids": r[:1]}, "", marks=pytest.mark.p3),
- pytest.param(lambda r: {"document_ids": r}, "", marks=pytest.mark.p3),
- ],
- )
- def test_basic_scenarios(self, add_documents_func, payload, expected_message):
- dataset, documents = add_documents_func
- if callable(payload):
- payload = payload([doc.id for doc in documents])
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- dataset.async_parse_documents(**payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- dataset.async_parse_documents(**payload)
- condition(dataset, payload["document_ids"])
- validate_document_details(dataset, payload["document_ids"])
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"document_ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"document_ids": r[:1] + ["invalid_id"] + r[1:3]}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"document_ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_parse_partial_invalid_document_id(self, add_documents_func, payload):
- dataset, documents = add_documents_func
- document_ids = [doc.id for doc in documents]
- payload = payload(document_ids)
-
- with pytest.raises(Exception) as exception_info:
- dataset.async_parse_documents(**payload)
- assert "Documents not found: ['invalid_id']" in str(exception_info.value), str(exception_info.value)
-
- condition(dataset, document_ids)
- validate_document_details(dataset, document_ids)
-
- @pytest.mark.p3
- def test_repeated_parse(self, add_documents_func):
- dataset, documents = add_documents_func
- document_ids = [doc.id for doc in documents]
- dataset.async_parse_documents(document_ids=document_ids)
- condition(dataset, document_ids)
- dataset.async_parse_documents(document_ids=document_ids)
-
- @pytest.mark.p3
- def test_duplicate_parse(self, add_documents_func):
- dataset, documents = add_documents_func
- document_ids = [doc.id for doc in documents]
- dataset.async_parse_documents(document_ids=document_ids + document_ids)
- condition(dataset, document_ids)
- validate_document_details(dataset, document_ids)
-
-
-def test_get_documents_status_handles_terminal_and_progress_paths(add_dataset_func, monkeypatch):
- """Collect terminal states and completed progress without another lookup."""
- dataset = add_dataset_func
- states = {"doc-done": ("DONE", 0.0), "doc-fail": ("FAIL", -1.0), "doc-cancel": ("CANCEL", 0.3), "doc-progress": ("RUNNING", 1.0)}
- calls = []
-
- def _list_documents(id=None, **_kwargs):
- calls.append(id)
- run, progress = states[id]
- return [Document(dataset.rag, {"id": id, "run": run, "progress": progress, "chunk_count": 2, "token_count": 4})]
-
- monkeypatch.setattr(dataset, "list_documents", _list_documents)
- monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("terminal documents must not be polled again"))
-
- finished = dataset._get_documents_status(list(states))
- assert sorted(finished) == sorted((doc_id, "DONE" if run == "RUNNING" else run, 2, 4) for doc_id, (run, _) in states.items())
- assert sorted(calls) == sorted(states)
-
-
-def test_get_documents_status_propagates_lookup_error(add_dataset_func, monkeypatch):
- """Surface the original lookup error instead of silently retrying."""
- dataset = add_dataset_func
- error = RuntimeError("temporary list failure")
- calls = []
-
- def _list_documents(id=None, **_kwargs):
- calls.append(id)
- raise error
-
- monkeypatch.setattr(dataset, "list_documents", _list_documents)
- monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("lookup failures must not be retried"))
-
- with pytest.raises(RuntimeError, match="temporary list failure") as exc_info:
- dataset._get_documents_status(["doc-1"])
- assert exc_info.value is error
- assert calls == ["doc-1"]
-
-
-def test_get_documents_status_raises_for_missing_document(add_dataset_func, monkeypatch):
- """Stop waiting when the requested document is no longer returned."""
- dataset = add_dataset_func
- calls = []
-
- def _list_documents(id=None, **_kwargs):
- calls.append(id)
- return []
-
- monkeypatch.setattr(dataset, "list_documents", _list_documents)
- monkeypatch.setattr("time.sleep", lambda *_args: pytest.fail("missing documents must not be retried"))
-
- with pytest.raises(RuntimeError, match="Document doc-1 not found"):
- dataset._get_documents_status(["doc-1"])
- assert calls == ["doc-1"]
-
-
-def test_parse_documents_keyboard_interrupt_triggers_cancel_then_returns_status(add_dataset_func, monkeypatch):
- dataset = add_dataset_func
- state = {"cancel_calls": 0, "status_calls": 0}
- expected_status = [("doc-1", "DONE", 1, 2)]
-
- def _raise_keyboard_interrupt(_document_ids):
- raise KeyboardInterrupt
-
- def _cancel(document_ids):
- state["cancel_calls"] += 1
- assert document_ids == ["doc-1"]
-
- def _status(document_ids):
- state["status_calls"] += 1
- assert document_ids == ["doc-1"]
- return expected_status
-
- monkeypatch.setattr(dataset, "async_parse_documents", _raise_keyboard_interrupt)
- monkeypatch.setattr(dataset, "async_cancel_parse_documents", _cancel)
- monkeypatch.setattr(dataset, "_get_documents_status", _status)
-
- status = dataset.parse_documents(["doc-1"])
- assert status == expected_status
- assert state["cancel_calls"] == 1
- assert state["status_calls"] == 1
-
-
-def test_parse_documents_returns_first_completed_status(add_dataset_func, monkeypatch):
- """Return the first wait result without polling the documents twice."""
- dataset = add_dataset_func
- state = {"status_calls": 0}
-
- def _noop_parse(_document_ids):
- return None
-
- def _status(document_ids):
- state["status_calls"] += 1
- assert document_ids == ["doc-1"]
- return [("doc-1", f"DONE-{state['status_calls']}", 1, 2)]
-
- monkeypatch.setattr(dataset, "async_parse_documents", _noop_parse)
- monkeypatch.setattr(dataset, "_get_documents_status", _status)
-
- status = dataset.parse_documents(["doc-1"])
- assert state["status_calls"] == 1
- assert status == [("doc-1", "DONE-1", 1, 2)]
-
-
-def test_async_cancel_parse_documents_raises_on_nonzero_code(add_dataset_func, monkeypatch):
- dataset = add_dataset_func
-
- class _Resp:
- @staticmethod
- def json():
- return {"code": 102, "message": "cancel failed"}
-
- monkeypatch.setattr(dataset, "rm", lambda *_args, **_kwargs: _Resp())
-
- with pytest.raises(Exception) as exc_info:
- dataset.async_cancel_parse_documents(["doc-1"])
- assert "cancel failed" in str(exc_info.value), str(exc_info.value)
-
-
-def test_parse_100_files(add_dataset_func, tmp_path):
- @wait_for(200, 1, "Document parsing timeout")
- def condition_inner(_dataset: DataSet, _count: int):
- docs = list_all_documents(_dataset, limit=_count)
- if len(docs) < _count:
- return False
- for document in docs:
- if document.run != "DONE":
- return False
- return True
-
- count = 100
- dataset = add_dataset_func
- documents = bulk_upload_documents(dataset, count, tmp_path)
- document_ids = [doc.id for doc in documents]
-
- dataset.async_parse_documents(document_ids=document_ids)
- condition_inner(dataset, count)
- validate_document_details(dataset, document_ids)
-
-
-def test_concurrent_parse(add_dataset_func, tmp_path):
- @wait_for(200, 1, "Document parsing timeout")
- def condition_inner(_dataset: DataSet, _count: int):
- docs = list_all_documents(_dataset, limit=_count)
- if len(docs) < _count:
- return False
- for document in docs:
- if document.run != "DONE":
- return False
- return True
-
- count = 100
- dataset = add_dataset_func
- documents = bulk_upload_documents(dataset, count, tmp_path)
- document_ids = [doc.id for doc in documents]
-
- def parse_doc(doc_id):
- dataset.async_parse_documents(document_ids=[doc_id])
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(parse_doc, doc.id) for doc in documents]
-
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- condition_inner(dataset, count)
- validate_document_details(dataset, document_ids)
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py
deleted file mode 100644
index 3cc4051fb3..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_stop_parse_documents.py
+++ /dev/null
@@ -1,41 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import pytest
-
-
-def validate_document_parse_done(dataset, document_ids):
- documents = dataset.list_documents(page_size=100)
- for document in documents:
- if document.id in document_ids:
- assert document.run == "DONE"
- assert len(document.process_begin_at) > 0
- assert document.process_duration > 0
- assert document.progress > 0
- assert "Task done" in document.progress_msg
-
-
-def validate_document_parse_cancel(dataset, document_ids):
- documents = dataset.list_documents(page_size=100)
- for document in documents:
- assert document.run == "CANCEL"
- assert len(document.process_begin_at) > 0
- assert document.progress == 0.0
-
-
-class TestDocumentsParseStop:
- pass
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py
deleted file mode 100644
index 78d5fbb217..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_update_document.py
+++ /dev/null
@@ -1,514 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import pytest
-from configs import DOCUMENT_NAME_LIMIT
-from ragflow_sdk import DataSet
-from configs import DEFAULT_PARSER_CONFIG
-
-
-class TestDocumentsUpdated:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("new_name.txt", ""),
- (f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt", ""),
- (0, "AttributeError"),
- (None, "AttributeError"),
- ("", "The extension of file can't be changed"),
- ("ragflow_test_upload_0", "The extension of file can't be changed"),
- ("ragflow_test_upload_1.txt", "Duplicated document name in the same dataset"),
- ("RAGFLOW_TEST_UPLOAD_1.TXT", ""),
- ],
- )
- def test_name(self, add_documents, name, expected_message):
- dataset, documents = add_documents
- document = documents[0]
-
- if expected_message:
- if name is None or (isinstance(name, int) and name == 0):
- # Skip tests that don't raise exceptions as expected
- pytest.skip("This test case doesn't consistently raise an exception as expected")
- elif name == "":
- # Check if empty string raises an exception or not
- try:
- document.update({"name": name})
- # If no exception is raised, the test expectation might be wrong
- pytest.skip("Empty string name doesn't raise an exception as expected")
- except Exception as e:
- assert expected_message in str(e), str(e)
- elif name == "ragflow_test_upload_0":
- # Check if this case raises an exception or not
- try:
- document.update({"name": name})
- # If no exception is raised, the test expectation might be wrong
- pytest.skip("Name without extension doesn't raise an exception as expected")
- except Exception as e:
- assert expected_message in str(e), str(e)
- else:
- with pytest.raises(Exception) as exception_info:
- document.update({"name": name})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- document.update({"name": name})
- docs = dataset.list_documents(id=document.id)
- updated_doc = [doc for doc in docs if doc.id == document.id][0]
- assert updated_doc.name == name, str(updated_doc)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "meta_fields, expected_message",
- [
- ({"test": "test"}, ""),
- ("test", "meta_fields must be a dictionary"),
- ],
- )
- def test_meta_fields(self, add_documents, meta_fields, expected_message):
- _, documents = add_documents
- document = documents[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.update({"meta_fields": meta_fields})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- document.update({"meta_fields": meta_fields})
-
- @pytest.mark.p2
- def test_meta_fields_invalid_type_guard_p2(self, add_documents):
- _, documents = add_documents
- document = documents[0]
- with pytest.raises(Exception) as exception_info:
- document.update({"meta_fields": "not-a-dict"})
- assert "meta_fields must be a dictionary" in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "chunk_method, expected_message",
- [
- ("naive", ""),
- ("manual", ""),
- ("qa", ""),
- ("table", ""),
- ("paper", ""),
- ("book", ""),
- ("laws", ""),
- ("presentation", ""),
- ("picture", ""),
- ("one", ""),
- ("knowledge_graph", ""),
- ("email", ""),
- ("tag", ""),
- ("", "`chunk_method` (empty string) is not valid"),
- ("other_chunk_method", "`chunk_method` other_chunk_method doesn't exist"),
- ],
- )
- def test_chunk_method(self, add_documents, chunk_method, expected_message):
- dataset, documents = add_documents
- document = documents[0]
-
- if expected_message:
- if chunk_method == "":
- # Check if empty string raises an exception or not
- try:
- document.update({"chunk_method": chunk_method})
- # If no exception is raised, skip this test
- pytest.skip("Empty chunk_method doesn't raise an exception as expected")
- except Exception as e:
- assert expected_message in str(e), str(e)
- elif chunk_method == "other_chunk_method":
- with pytest.raises(Exception) as exception_info:
- document.update({"chunk_method": chunk_method})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- with pytest.raises(Exception) as exception_info:
- document.update({"chunk_method": chunk_method})
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- document.update({"chunk_method": chunk_method})
- docs = dataset.list_documents()
- updated_doc = [doc for doc in docs if doc.id == document.id][0]
- assert updated_doc.chunk_method == chunk_method, str(updated_doc)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"chunk_count": 1}, "can't change `chunk_count`"),
- pytest.param(
- {"create_date": "Fri, 14 Mar 2025 16:53:42 GMT"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"create_time": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"created_by": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"dataset_id": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"id": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"location": "ragflow_test.txt"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"process_begin_at": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"process_duration": 1.0},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ({"progress": 1.0}, "can't change `progress`"),
- pytest.param(
- {"progress_msg": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"run": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"size": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"source_type": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"thumbnail": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ({"token_count": 1}, "can't change `token_count`"),
- pytest.param(
- {"type": "ragflow_test"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"update_date": "Fri, 14 Mar 2025 16:33:17 GMT"},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- pytest.param(
- {"update_time": 1},
- "The input parameters are invalid",
- marks=pytest.mark.skip(reason="issues/6104"),
- ),
- ],
- )
- def test_invalid_field(self, add_documents, payload, expected_message):
- _, documents = add_documents
- document = documents[0]
-
- with pytest.raises(Exception) as exception_info:
- document.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"chunk_count": 1}, "can't change `chunk_count`"),
- ],
- )
- def test_immutable_fields_chunk_count(self, add_documents, payload, expected_message):
- _, documents = add_documents
- document = documents[0]
-
- with pytest.raises(Exception) as exception_info:
- document.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"token_count": 9999}, "can't change `token_count`"), # Attempt to change immutable field
- ],
- )
- def test_immutable_fields_token_count(self, add_documents, payload, expected_message):
- _, documents = add_documents
- document = documents[0]
-
- with pytest.raises(Exception) as exception_info:
- document.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- ({"progress": 0.5}, "can't change `progress`"), # Attempt to change immutable field
- ({"progress": 1.5}, "Field: <progress> - Message: <Input should be less than or equal to 1> - Value: <1.5>"), # Attempt to change immutable field
- ],
- )
- def test_immutable_fields_progress(self, add_documents, payload, expected_message):
- _, documents = add_documents
- document = documents[0]
-
- with pytest.raises(Exception) as exception_info:
- document.update(payload)
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
-
-DEFAULT_PARSER_CONFIG_FOR_TEST = {
- "layout_recognize": "DeepDOC",
- "chunk_token_num": 512,
- "delimiter": "\n",
- "auto_keywords": 0,
- "auto_questions": 0,
- "html4excel": False,
- "topn_tags": 3,
- "raptor": {
- "use_raptor": True,
- "prompt": "Summarize the paragraphs below without inventing facts or changing numbers.\nOutput exactly two parts in the same language as the source:\n1. First line: a concise title only.\n2. Following lines: a concise summary of the content.\nDo not output labels, Markdown headings, bullet points, or any other commentary.\n\nParagraphs:\n{cluster_content}",
- "max_token": 512,
- "clustering_threshold": 0.3,
- "clustering_ratio": 0.5,
- "max_cluster": 64,
- "random_seed": 0,
- },
- "graphrag": {
- "use_graphrag": True,
- "entity_types": [
- "organization",
- "person",
- "geo",
- "event",
- "category",
- ],
- "method": "light",
- "batch_chunk_token_size": 4096,
- },
-}
-
-
-class TestUpdateDocumentParserConfig:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "chunk_method, parser_config, expected_message",
- [
- ("naive", {}, ""),
- pytest.param(
- "naive",
- DEFAULT_PARSER_CONFIG_FOR_TEST,
- "",
- marks=pytest.mark.skip(reason="DEFAULT_PARSER_CONFIG contains fields not allowed in document update API"),
- ),
- pytest.param(
- "naive",
- {"chunk_token_num": -1},
- "Field: <parser_config.chunk_token_num> - Message: <Input should be greater than or equal to 1> - Value: <-1>",
- ),
- (
- "naive",
- {"chunk_token_num": 0},
- "Input should be greater than or equal to 1",
- ),
- (
- "naive",
- {"chunk_token_num": 100000000},
- "Input should be less than or equal to 2048",
- ),
- (
- "naive",
- {"chunk_token_num": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"chunk_token_num": "1024"},
- "Input should be a valid integer",
- ),
- ("naive", {"layout_recognize": "DeepDOC"}, ""),
- ("naive", {"layout_recognize": "Naive"}, ""),
- ("naive", {"html4excel": True}, ""),
- ("naive", {"html4excel": False}, ""),
- (
- "naive",
- {"html4excel": 1},
- "Input should be a valid boolean",
- ),
- ("naive", {"delimiter": ""}, "String should have at least 1 character"),
- ("naive", {"delimiter": "`##`"}, ""),
- (
- "naive",
- {"delimiter": 1},
- "Input should be a valid string",
- ),
- (
- "naive",
- {"task_page_size": -1},
- "Input should be greater than or equal to 1",
- ),
- (
- "naive",
- {"task_page_size": 0},
- "Input should be greater than or equal to 1",
- ),
- pytest.param(
- "naive",
- {"task_page_size": 100000000},
- "",
- ),
- (
- "naive",
- {"task_page_size": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"task_page_size": "1024"},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {
- "raptor": {
- "use_raptor": True,
- "prompt": "Summarize the paragraphs below without inventing facts or changing numbers.\nOutput exactly two parts in the same language as the source:\n1. First line: a concise title only.\n2. Following lines: a concise summary of the content.\nDo not output labels, Markdown headings, bullet points, or any other commentary.\n\nParagraphs:\n{cluster_content}",
- "max_token": 512,
- "clustering_threshold": 0.3,
- "clustering_ratio": 0.5,
- "max_cluster": 64,
- "random_seed": 0,
- }
- },
- "",
- ),
- ("naive", {"raptor": {"use_raptor": False}}, ""),
- (
- "naive",
- {"invalid_key": "invalid_value"},
- "",
- ),
- (
- "naive",
- {"auto_keywords": -1},
- "Input should be greater than or equal to 0",
- ),
- pytest.param(
- "naive",
- {"auto_keywords": 32},
- "",
- ),
- (
- "naive",
- {"auto_keywords": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"auto_keywords": "1024"},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"auto_questions": -1},
- "Input should be greater than or equal to 0",
- ),
- pytest.param(
- "naive",
- {"auto_questions": 10},
- "",
- ),
- (
- "naive",
- {"auto_questions": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"auto_questions": "1024"},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"topn_tags": -1},
- "Input should be greater than or equal to 1",
- ),
- pytest.param(
- "naive",
- {"topn_tags": 10},
- "",
- ),
- (
- "naive",
- {"topn_tags": 3.14},
- "Input should be a valid integer",
- ),
- (
- "naive",
- {"topn_tags": "1024"},
- "Input should be a valid integer",
- ),
- ],
- )
- def test_parser_config(self, client, add_documents, chunk_method, parser_config, expected_message):
- dataset, documents = add_documents
- document = documents[0]
- from operator import attrgetter
-
- update_data = {"chunk_method": chunk_method, "parser_config": parser_config}
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- document.update(update_data)
- assert expected_message in str(exception_info.value), str(exception_info.value)
- else:
- document.update(update_data)
- docs = dataset.list_documents(id=document.id)
- updated_doc = [doc for doc in docs if doc.id == document.id][0]
- if parser_config:
- for k, v in parser_config.items():
- if k in {"graphrag", "raptor"}:
- assert not hasattr(updated_doc.parser_config, k), str(updated_doc)
- continue
- if isinstance(v, dict):
- for kk, vv in v.items():
- assert attrgetter(f"{k}.{kk}")(updated_doc.parser_config) == vv, str(updated_doc)
- else:
- assert attrgetter(k)(updated_doc.parser_config) == v, str(updated_doc)
- else:
- expected_config = DataSet.ParserConfig(
- client,
- DEFAULT_PARSER_CONFIG,
- )
- assert str(updated_doc.parser_config) == str(expected_config), str(updated_doc)
diff --git a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py b/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py
deleted file mode 100644
index 4ceb31624c..0000000000
--- a/test/testcases/test_sdk_api/test_file_management_within_dataset/test_upload_documents.py
+++ /dev/null
@@ -1,211 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import string
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from configs import DOCUMENT_NAME_LIMIT
-from utils.file_utils import create_txt_file
-
-
-class TestDocumentsUpload:
- @pytest.mark.p1
- def test_valid_single_upload(self, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- with fp.open("rb") as f:
- blob = f.read()
-
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.dataset_id == dataset.id, str(document)
- assert document.name == fp.name, str(document)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "generate_test_files",
- [
- "docx",
- "excel",
- "ppt",
- "image",
- "pdf",
- "txt",
- "md",
- "json",
- "eml",
- "html",
- ],
- indirect=True,
- )
- def test_file_type_validation(self, add_dataset_func, generate_test_files, request):
- dataset = add_dataset_func
- fp = generate_test_files[request.node.callspec.params["generate_test_files"]]
-
- with fp.open("rb") as f:
- blob = f.read()
-
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.dataset_id == dataset.id, str(document)
- assert document.name == fp.name, str(document)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "file_type",
- ["exe", "unknown"],
- )
- def test_unsupported_file_type(self, add_dataset_func, tmp_path, file_type):
- dataset = add_dataset_func
- fp = tmp_path / f"ragflow_test.{file_type}"
- fp.touch()
-
- with fp.open("rb") as f:
- blob = f.read()
-
- with pytest.raises(Exception) as exception_info:
- dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- assert str(exception_info.value) == f"ragflow_test.{file_type}: This type of file has not been supported yet!", str(exception_info.value)
-
- @pytest.mark.p2
- def test_missing_file(self, add_dataset_func):
- dataset = add_dataset_func
- with pytest.raises(Exception) as exception_info:
- dataset.upload_documents([])
- assert str(exception_info.value) == "No file part!", str(exception_info.value)
-
- @pytest.mark.p3
- def test_empty_file(self, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- fp = tmp_path / "empty.txt"
- fp.touch()
-
- with fp.open("rb") as f:
- blob = f.read()
-
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.size == 0, str(document)
-
- @pytest.mark.p3
- def test_filename_empty(self, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
-
- with fp.open("rb") as f:
- blob = f.read()
-
- with pytest.raises(Exception) as exception_info:
- dataset.upload_documents([{"display_name": "", "blob": blob}])
- assert str(exception_info.value) == "No file selected!", str(exception_info.value)
-
- @pytest.mark.p2
- def test_filename_max_length(self, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt")
-
- with fp.open("rb") as f:
- blob = f.read()
-
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- for document in documents:
- assert document.dataset_id == dataset.id, str(document)
- assert document.name == fp.name, str(document)
-
- @pytest.mark.p2
- def test_duplicate_files(self, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
-
- with fp.open("rb") as f:
- blob = f.read()
-
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}, {"display_name": fp.name, "blob": blob}])
-
- assert len(documents) == 2, str(documents)
- for i, document in enumerate(documents):
- assert document.dataset_id == dataset.id, str(document)
- expected_name = fp.name if i == 0 else f"{fp.stem}({i}){fp.suffix}"
- assert document.name == expected_name, str(document)
-
- @pytest.mark.p3
- def test_same_file_repeat(self, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
-
- with fp.open("rb") as f:
- blob = f.read()
-
- for i in range(3):
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- assert len(documents) == 1, str(documents)
- document = documents[0]
- assert document.dataset_id == dataset.id, str(document)
- expected_name = fp.name if i == 0 else f"{fp.stem}({i}){fp.suffix}"
- assert document.name == expected_name, str(document)
-
- @pytest.mark.p3
- def test_filename_special_characters(self, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- illegal_chars = '<>:"/\\|?*'
- translation_table = str.maketrans({char: "_" for char in illegal_chars})
- safe_filename = string.punctuation.translate(translation_table)
- fp = tmp_path / f"{safe_filename}.txt"
- fp.write_text("Sample text content")
-
- with fp.open("rb") as f:
- blob = f.read()
-
- documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
- assert len(documents) == 1, str(documents)
- document = documents[0]
- assert document.dataset_id == dataset.id, str(document)
- assert document.name == fp.name, str(document)
-
- @pytest.mark.p3
- def test_multiple_files(self, client, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- expected_document_count = 20
- document_infos = []
- for i in range(expected_document_count):
- fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
- with fp.open("rb") as f:
- blob = f.read()
- document_infos.append({"display_name": fp.name, "blob": blob})
- documents = dataset.upload_documents(document_infos)
- assert len(documents) == expected_document_count, str(documents)
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.document_count == expected_document_count, str(retrieved_dataset)
-
- @pytest.mark.p3
- def test_concurrent_upload(self, client, add_dataset_func, tmp_path):
- dataset = add_dataset_func
- count = 20
- fps = [create_txt_file(tmp_path / f"ragflow_test_{i}.txt") for i in range(count)]
-
- def upload_file(fp):
- with fp.open("rb") as f:
- blob = f.read()
- return dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(upload_file, fp) for fp in fps]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- retrieved_dataset = client.get_dataset(name=dataset.name)
- assert retrieved_dataset.document_count == count, str(retrieved_dataset)
diff --git a/test/testcases/test_sdk_api/test_memory_management/conftest.py b/test/testcases/test_sdk_api/test_memory_management/conftest.py
deleted file mode 100644
index 992ef74d10..0000000000
--- a/test/testcases/test_sdk_api/test_memory_management/conftest.py
+++ /dev/null
@@ -1,53 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-import random
-
-
[email protected](scope="class")
-def add_memory_func(client, request):
- def cleanup():
- memory_list_res = client.list_memory()
- exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
- for memory_id in exist_memory_ids:
- client.delete_memory(memory_id)
-
- request.addfinalizer(cleanup)
-
- memory_ids = []
- for i in range(3):
- payload = {
- "name": f"test_memory_{i}",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = client.create_memory(**payload)
- memory_ids.append(res.id)
- request.cls.memory_ids = memory_ids
- return memory_ids
-
-
[email protected](scope="class")
-def delete_test_memory(client, request):
- def cleanup():
- memory_list_res = client.list_memory()
- exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
- for memory_id in exist_memory_ids:
- client.delete_memory(memory_id)
-
- request.addfinalizer(cleanup)
- return
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_create_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_create_memory.py
deleted file mode 100644
index 3a101c33f0..0000000000
--- a/test/testcases/test_sdk_api/test_memory_management/test_create_memory.py
+++ /dev/null
@@ -1,112 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-import re
-
-import pytest
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
-from ragflow_sdk import RAGFlow
-from hypothesis import example, given, settings
-from utils.hypothesis_utils import valid_names
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- ids=["empty_auth", "invalid_api_token"],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.create_memory(**{"name": "test_memory", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"})
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("delete_test_memory")
-class TestMemoryCreate:
- @pytest.mark.p1
- @given(name=valid_names())
- @example("e" * 128)
- @settings(max_examples=20)
- def test_name(self, client, name):
- payload = {
- "name": name,
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- memory = client.create_memory(**payload)
- pattern = rf"^{name}|{name}(?:\((\d+)\))?$"
- escaped_name = re.escape(memory.name)
- assert re.match(pattern, escaped_name), str(memory)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "Memory name cannot be empty or whitespace."),
- (" ", "Memory name cannot be empty or whitespace."),
- ("a" * 129, f"Memory name '{'a' * 129}' exceeds limit of 128."),
- ],
- ids=["empty_name", "space_name", "too_long_name"],
- )
- def test_name_invalid(self, client, name, expected_message):
- payload = {
- "name": name,
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- with pytest.raises(Exception) as exception_info:
- client.create_memory(**payload)
- if IS_GO_PROXY:
- if name == "":
- expected_message = "failed on the 'required' tag"
- elif not name.strip():
- expected_message = "name can't be empty"
- else:
- expected_message = "could not create new memory"
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- @given(name=valid_names())
- @settings(deadline=None)
- def test_type_invalid(self, client, name):
- payload = {"name": name, "memory_type": ["something"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
- with pytest.raises(Exception) as exception_info:
- client.create_memory(**payload)
- expected_message = "memory type 'something' is not supported" if IS_GO_PROXY else f"Memory type '{ {'something'} }' is not supported."
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
- @pytest.mark.p3
- def test_name_duplicated(self, client):
- name = "duplicated_name_test"
- payload = {
- "name": name,
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res1 = client.create_memory(**payload)
- assert res1.name == name, str(res1)
-
- res2 = client.create_memory(**payload)
- assert res2.name == f"{name}(1)", str(res2)
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_list_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_list_memory.py
deleted file mode 100644
index 54b95505ee..0000000000
--- a/test/testcases/test_sdk_api/test_memory_management/test_list_memory.py
+++ /dev/null
@@ -1,141 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from ragflow_sdk import RAGFlow
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.list_memory()
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_capability(self, client):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(client.list_memory) for _ in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
[email protected]("add_memory_func")
-class TestMemoryList:
- @pytest.mark.p2
- def test_params_unset(self, client):
- res = client.list_memory()
- assert len(res["memory_list"]) == 3, str(res)
- assert res["total_count"] == 3, str(res)
-
- @pytest.mark.p2
- def test_params_empty(self, client):
- res = client.list_memory(**{})
- assert len(res["memory_list"]) == 3, str(res)
- assert res["total_count"] == 3, str(res)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page": 1, "page_size": 10}, 3),
- ({"page": 2, "page_size": 10}, 0),
- ({"page": 1, "page_size": 2}, 2),
- ({"page": 2, "page_size": 2}, 1),
- ({"page": 5, "page_size": 10}, 0),
- ],
- ids=["normal_first_page", "beyond_max_page", "normal_last_partial_page", "normal_middle_page", "full_data_single_page"],
- )
- def test_page(self, client, params, expected_page_size):
- # have added 3 memories in fixture
- res = client.list_memory(**params)
- assert len(res["memory_list"]) == expected_page_size, str(res)
- assert res["total_count"] == 3, str(res)
-
- @pytest.mark.p2
- def test_filter_memory_type(self, client):
- res = client.list_memory(**{"memory_type": ["semantic"]})
- for memory in res["memory_list"]:
- assert "semantic" in memory.memory_type, str(memory)
-
- @pytest.mark.p2
- def test_filter_multi_memory_type(self, client):
- res = client.list_memory(**{"memory_type": ["episodic", "procedural"]})
- for memory in res["memory_list"]:
- assert "episodic" in memory.memory_type or "procedural" in memory.memory_type, str(memory)
-
- @pytest.mark.p2
- def test_filter_storage_type(self, client):
- res = client.list_memory(**{"storage_type": "table"})
- for memory in res["memory_list"]:
- assert memory.storage_type == "table", str(memory)
-
- @pytest.mark.p2
- def test_match_keyword(self, client):
- res = client.list_memory(**{"keywords": "s"})
- for memory in res["memory_list"]:
- assert "s" in memory.name, str(memory)
-
- @pytest.mark.p1
- def test_get_config(self, client):
- memory_list = client.list_memory()
- assert len(memory_list["memory_list"]) > 0, str(memory_list)
- memory = memory_list["memory_list"][0]
- memory_id = memory.id
- memory_config = memory.get_config()
- assert memory_config.id == memory_id, memory_config
- for field in [
- "name",
- "avatar",
- "tenant_id",
- "owner_name",
- "memory_type",
- "storage_type",
- "embd_id",
- "llm_id",
- "permissions",
- "description",
- "memory_size",
- "forgetting_policy",
- "temperature",
- "system_prompt",
- "user_prompt",
- ]:
- assert hasattr(memory, field), memory_config
-
- @pytest.mark.p2
- def test_get_config_invalid_memory_id_raises(self, client):
- memory_list = client.list_memory()
- assert len(memory_list["memory_list"]) > 0, str(memory_list)
- memory = memory_list["memory_list"][0]
- memory.id = "missing-memory-id-for-config"
- with pytest.raises(Exception) as exception_info:
- memory.get_config()
- assert str(exception_info.value), exception_info
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_rm_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_rm_memory.py
deleted file mode 100644
index a2099e4c57..0000000000
--- a/test/testcases/test_sdk_api/test_memory_management/test_rm_memory.py
+++ /dev/null
@@ -1,53 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from ragflow_sdk import RAGFlow
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.delete_memory("some_memory_id")
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_func")
-class TestMemoryDelete:
- @pytest.mark.p1
- def test_memory_id(self, client):
- memory_ids = self.memory_ids
- client.delete_memory(memory_ids[0])
- res = client.list_memory()
- assert res["total_count"] == 2, res
-
- @pytest.mark.p2
- def test_id_wrong_uuid(self, client):
- with pytest.raises(Exception) as exception_info:
- client.delete_memory("d94a8dc02c9711f0930f7fbc369eab6d")
- assert exception_info.value, str(exception_info.value)
-
- res = client.list_memory()
- assert len(res["memory_list"]) == 2, res
diff --git a/test/testcases/test_sdk_api/test_memory_management/test_update_memory.py b/test/testcases/test_sdk_api/test_memory_management/test_update_memory.py
deleted file mode 100644
index 2cb3bfc92d..0000000000
--- a/test/testcases/test_sdk_api/test_memory_management/test_update_memory.py
+++ /dev/null
@@ -1,162 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-import pytest
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
-from ragflow_sdk import RAGFlow, Memory
-from hypothesis import HealthCheck, example, given, settings
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-from utils.hypothesis_utils import valid_names
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- ids=["empty_auth", "invalid_api_token"],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
-
- with pytest.raises(Exception) as exception_info:
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- memory = Memory(client, {"id": "memory_id"})
- memory.update({"name": "New_Name"})
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_func")
-class TestMemoryUpdate:
- @pytest.mark.p1
- @given(name=valid_names())
- @example("f" * 128)
- @settings(max_examples=20, suppress_health_check=[HealthCheck.function_scoped_fixture])
- def test_name(self, client, name):
- memory_ids = self.memory_ids
- update_dict = {"name": name}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.name == name, str(res)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "Memory name cannot be empty or whitespace."),
- (" ", "Memory name cannot be empty or whitespace."),
- ("a" * 129, f"Memory name '{'a' * 129}' exceeds limit of 128."),
- ],
- )
- def test_name_invalid(self, client, name, expected_message):
- memory_ids = self.memory_ids
- update_dict = {"name": name}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- with pytest.raises(Exception) as exception_info:
- memory.update(update_dict)
- if IS_GO_PROXY:
- if len(name) > 128:
- expected_message = "failed to update memory"
- else:
- expected_message = "name can't be empty"
- assert expected_message in str(exception_info.value), str(exception_info.value)
-
- @pytest.mark.p2
- def test_duplicate_name(self, client):
- memory_ids = self.memory_ids
- update_dict = {"name": "Test_Memory"}
- memory_0 = Memory(client, {"id": memory_ids[0]})
- res_0 = memory_0.update(update_dict)
- assert res_0.name == "Test_Memory", str(res_0)
-
- memory_1 = Memory(client, {"id": memory_ids[1]})
- res_1 = memory_1.update(update_dict)
- assert res_1.name == "Test_Memory(1)", str(res_1)
-
- @pytest.mark.p2
- def test_avatar(self, client, tmp_path):
- memory_ids = self.memory_ids
- fn = create_image_file(tmp_path / "ragflow_test.png")
- update_dict = {"avatar": f"data:image/png;base64,{encode_avatar(fn)}"}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.avatar == f"data:image/png;base64,{encode_avatar(fn)}", str(res)
-
- @pytest.mark.p2
- def test_description(self, client):
- memory_ids = self.memory_ids
- description = "This is a test description."
- update_dict = {"description": description}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.description == description, str(res)
-
- @pytest.mark.p1
- def test_llm(self, client):
- memory_ids = self.memory_ids
- llm_id = "glm-4@ZHIPU-AI"
- update_dict = {"llm_id": llm_id}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.llm_id == llm_id, str(res)
-
- @pytest.mark.p2
- @pytest.mark.parametrize("permission", ["me", "team"], ids=["me", "team"])
- def test_permission(self, client, permission):
- memory_ids = self.memory_ids
- update_dict = {"permissions": permission}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.permissions == permission.lower().strip(), str(res)
-
- @pytest.mark.p1
- def test_memory_size(self, client):
- memory_ids = self.memory_ids
- memory_size = 1048576 # 1 MB
- update_dict = {"memory_size": memory_size}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.memory_size == memory_size, str(res)
-
- @pytest.mark.p1
- def test_temperature(self, client):
- memory_ids = self.memory_ids
- temperature = 0.7
- update_dict = {"temperature": temperature}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.temperature == temperature, str(res)
-
- @pytest.mark.p1
- def test_system_prompt(self, client):
- memory_ids = self.memory_ids
- system_prompt = "This is a system prompt."
- update_dict = {"system_prompt": system_prompt}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.system_prompt == system_prompt, str(res)
-
- @pytest.mark.p1
- def test_user_prompt(self, client):
- memory_ids = self.memory_ids
- user_prompt = "This is a user prompt."
- update_dict = {"user_prompt": user_prompt}
- memory = Memory(client, {"id": random.choice(memory_ids)})
- res = memory.update(update_dict)
- assert res.user_prompt == user_prompt, res
diff --git a/test/testcases/test_sdk_api/test_message_management/conftest.py b/test/testcases/test_sdk_api/test_message_management/conftest.py
deleted file mode 100644
index 8e374385cb..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/conftest.py
+++ /dev/null
@@ -1,162 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import time
-import uuid
-
-import pytest
-import random
-from configs import IS_GO_PROXY
-
-
-MESSAGE_USER_ID = "sdk-api-test-user" if IS_GO_PROXY else ""
-
-
[email protected](scope="class")
-def add_empty_raw_type_memory(client, request):
- def cleanup():
- memory_list_res = client.list_memory()
- exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
- for _memory_id in exist_memory_ids:
- client.delete_memory(_memory_id)
-
- request.addfinalizer(cleanup)
- payload = {"name": "test_memory_0", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
- res = client.create_memory(**payload)
- memory_id = res.id
- request.cls.memory_id = memory_id
- request.cls.memory_type = payload["memory_type"]
- return memory_id
-
-
[email protected](scope="class")
-def add_empty_multiple_type_memory(client, request):
- def cleanup():
- memory_list_res = client.list_memory()
- exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
- for _memory_id in exist_memory_ids:
- client.delete_memory(_memory_id)
-
- request.addfinalizer(cleanup)
- payload = {
- "name": "test_memory_0",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = client.create_memory(**payload)
- memory_id = res.id
- request.cls.memory_id = memory_id
- request.cls.memory_type = payload["memory_type"]
- return memory_id
-
-
[email protected](scope="class")
-def add_2_multiple_type_memory(client, request):
- def cleanup():
- memory_list_res = client.list_memory()
- exist_memory_ids = [memory.id for memory in memory_list_res["memory_list"]]
- for _memory_id in exist_memory_ids:
- client.delete_memory(_memory_id)
-
- request.addfinalizer(cleanup)
- memory_ids = []
- for i in range(2):
- payload = {
- "name": f"test_memory_{i}",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = client.create_memory(**payload)
- memory_ids.append(res.id)
- request.cls.memory_ids = memory_ids
- return memory_ids
-
-
[email protected](scope="class")
-def add_memory_with_multiple_type_message_func(client, request):
- def cleanup():
- memory_list_res = client.list_memory()
- exist_memory_ids = [mem.id for mem in memory_list_res["memory_list"]]
- for _memory_id in exist_memory_ids:
- client.delete_memory(_memory_id)
-
- request.addfinalizer(cleanup)
-
- payload = {
- "name": "test_memory_0",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- memory = client.create_memory(**payload)
- memory_id = memory.id
- agent_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_id,
- "session_id": uuid.uuid4().hex,
- "user_id": MESSAGE_USER_ID,
- "user_input": "what is coriander?",
- "agent_response": """
-Coriander is a versatile herb with two main edible parts, and its name can refer to both:
-1. Leaves and Stems (often called Cilantro or Fresh Coriander): These are the fresh, green, fragrant leaves and tender stems of the plant Coriandrum sativum. They have a bright, citrusy, and sometimes pungent flavor. Cilantro is widely used as a garnish or key ingredient in cuisines like Mexican, Indian, Thai, and Middle Eastern.
-2. Seeds (called Coriander Seeds): These are the dried, golden-brown seeds of the same plant. When ground, they become coriander powder. The seeds have a warm, nutty, floral, and slightly citrusy taste, completely different from the fresh leaves. They are a fundamental spice in curries, stews, pickles, and baking.
-Key Point of Confusion: The naming differs by region. In North America, "coriander" typically refers to the seeds, while "cilantro" refers to the fresh leaves. In the UK, Europe, and many other parts of the world, "coriander" refers to the fresh herb, and the seeds are called "coriander seeds."
-""",
- }
- client.add_message(**message_payload)
- request.cls.memory_id = memory_id
- request.cls.agent_id = agent_id
- time.sleep(2) # make sure refresh to index before search
- return memory_id
-
-
[email protected](scope="class")
-def add_memory_with_5_raw_message_func(client, request):
- def cleanup():
- memory_list_res = client.list_memory()
- exist_memory_ids = [mem.id for mem in memory_list_res["memory_list"]]
- for _memory_id in exist_memory_ids:
- client.delete_memory(_memory_id)
-
- request.addfinalizer(cleanup)
-
- payload = {"name": "test_memory_1", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
- memory = client.create_memory(**payload)
- memory_id = memory.id
- agent_ids = [uuid.uuid4().hex for _ in range(2)]
- session_ids = [uuid.uuid4().hex for _ in range(5)]
- for i in range(5):
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_ids[i % 2],
- "session_id": session_ids[i],
- "user_id": MESSAGE_USER_ID,
- "user_input": "what is coriander?",
- "agent_response": """
-Coriander is a versatile herb with two main edible parts, and its name can refer to both:
-1. Leaves and Stems (often called Cilantro or Fresh Coriander): These are the fresh, green, fragrant leaves and tender stems of the plant Coriandrum sativum. They have a bright, citrusy, and sometimes pungent flavor. Cilantro is widely used as a garnish or key ingredient in cuisines like Mexican, Indian, Thai, and Middle Eastern.
-2. Seeds (called Coriander Seeds): These are the dried, golden-brown seeds of the same plant. When ground, they become coriander powder. The seeds have a warm, nutty, floral, and slightly citrusy taste, completely different from the fresh leaves. They are a fundamental spice in curries, stews, pickles, and baking.
-Key Point of Confusion: The naming differs by region. In North America, "coriander" typically refers to the seeds, while "cilantro" refers to the fresh leaves. In the UK, Europe, and many other parts of the world, "coriander" refers to the fresh herb, and the seeds are called "coriander seeds."
-""",
- }
- client.add_message(**message_payload)
- request.cls.memory_id = memory_id
- request.cls.agent_ids = agent_ids
- request.cls.session_ids = session_ids
- time.sleep(2) # make sure refresh to index before search
- return memory_id
diff --git a/test/testcases/test_sdk_api/test_message_management/test_add_message.py b/test/testcases/test_sdk_api/test_message_management/test_add_message.py
deleted file mode 100644
index fc0dcd4435..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/test_add_message.py
+++ /dev/null
@@ -1,145 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import time
-import uuid
-import pytest
-from ragflow_sdk import RAGFlow, Memory
-from configs import HOST_ADDRESS, INVALID_API_TOKEN, IS_GO_PROXY, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-MESSAGE_USER_ID = "sdk-api-test-user" if IS_GO_PROXY else ""
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.add_message(**{"memory_id": [""], "agent_id": "", "session_id": "", "user_id": MESSAGE_USER_ID, "user_input": "what is pineapple?", "agent_response": ""})
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_empty_raw_type_memory")
-class TestAddRawMessage:
- @pytest.mark.p3
- def test_add_raw_message(self, client):
- memory_id = self.memory_id
- agent_id = uuid.uuid4().hex
- session_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_id,
- "session_id": session_id,
- "user_id": MESSAGE_USER_ID,
- "user_input": "what is pineapple?",
- "agent_response": """
-A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
-Scientific Name: Ananas comosus
-Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
-Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
-Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
-Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
-Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
-Are you asking about the fruit itself, or its use in a specific context?
-""",
- }
- add_res = client.add_message(**message_payload)
- assert add_res == "All add to task.", str(add_res)
- time.sleep(2) # make sure refresh to index before search
- memory = Memory(client, {"id": memory_id})
- message_res = memory.list_memory_messages(**{"agent_id": agent_id, "keywords": session_id})
- assert message_res["messages"]["total_count"] > 0
- for message in message_res["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
- assert message["session_id"] == session_id, message
-
-
[email protected]("add_empty_multiple_type_memory")
-class TestAddMultipleTypeMessage:
- @pytest.mark.p1
- def test_add_multiple_type_message(self, client):
- memory_id = self.memory_id
- agent_id = uuid.uuid4().hex
- session_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_id,
- "session_id": session_id,
- "user_id": MESSAGE_USER_ID,
- "user_input": "what is pineapple?",
- "agent_response": """
-A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
-Scientific Name: Ananas comosus
-Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
-Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
-Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
-Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
-Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
-Are you asking about the fruit itself, or its use in a specific context?
-""",
- }
- add_res = client.add_message(**message_payload)
- assert add_res == "All add to task.", str(add_res)
- time.sleep(2) # make sure refresh to index before search
- memory = Memory(client, {"id": memory_id})
- message_res = memory.list_memory_messages(**{"agent_id": agent_id, "keywords": session_id})
- assert message_res["messages"]["total_count"] > 0
- for message in message_res["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
- assert message["session_id"] == session_id, message
-
-
[email protected]("add_2_multiple_type_memory")
-class TestAddToMultipleMemory:
- @pytest.mark.p3
- def test_add_to_multiple_memory(self, client):
- memory_ids = self.memory_ids
- agent_id = uuid.uuid4().hex
- session_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": memory_ids,
- "agent_id": agent_id,
- "session_id": session_id,
- "user_id": MESSAGE_USER_ID,
- "user_input": "what is pineapple?",
- "agent_response": """
-A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
-Scientific Name: Ananas comosus
-Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
-Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
-Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
-Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
-Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
-Are you asking about the fruit itself, or its use in a specific context?
-""",
- }
- add_res = client.add_message(**message_payload)
- assert add_res == "All add to task.", str(add_res)
- time.sleep(2) # make sure refresh to index before search
- for memory_id in memory_ids:
- memory = Memory(client, {"id": memory_id})
- message_res = memory.list_memory_messages(**{"agent_id": agent_id, "keywords": session_id})
- assert message_res["messages"]["total_count"] > 0
- for message in message_res["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
- assert message["session_id"] == session_id, message
diff --git a/test/testcases/test_sdk_api/test_message_management/test_forget_message.py b/test/testcases/test_sdk_api/test_message_management/test_forget_message.py
deleted file mode 100644
index c3f51110ec..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/test_forget_message.py
+++ /dev/null
@@ -1,53 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-import pytest
-from ragflow_sdk import RAGFlow, Memory
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- memory = Memory(client, {"id": "empty_memory_id"})
- memory.forget_message(0)
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestForgetMessage:
- @pytest.mark.p3
- def test_forget_message(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- list_res = memory.list_memory_messages()
- assert len(list_res["messages"]["message_list"]) > 0
-
- message = random.choice(list_res["messages"]["message_list"])
- res = memory.forget_message(message["message_id"])
- assert res, str(res)
-
- forgot_message_res = memory.get_message_content(message["message_id"])
- assert forgot_message_res["forget_at"] not in ["-", ""], forgot_message_res
diff --git a/test/testcases/test_sdk_api/test_message_management/test_get_message_content.py b/test/testcases/test_sdk_api/test_message_management/test_get_message_content.py
deleted file mode 100644
index a223da5f10..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/test_get_message_content.py
+++ /dev/null
@@ -1,53 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-
-import pytest
-from ragflow_sdk import RAGFlow, Memory
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- memory = Memory(client, {"id": "empty_memory_id"})
- memory.get_message_content(0)
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_with_multiple_type_message_func")
-class TestGetMessageContent:
- @pytest.mark.p1
- def test_get_message_content(self, client):
- memory_id = self.memory_id
- recent_messages = client.get_recent_messages([memory_id])
- assert len(recent_messages) > 0, recent_messages
- message = random.choice(recent_messages)
- message_id = message["message_id"]
- memory = Memory(client, {"id": memory_id})
- content_res = memory.get_message_content(message_id)
- for field in ["content", "content_embed"]:
- assert field in content_res
- assert content_res[field] is not None, content_res
diff --git a/test/testcases/test_sdk_api/test_message_management/test_get_recent_message.py b/test/testcases/test_sdk_api/test_message_management/test_get_recent_message.py
deleted file mode 100644
index 786873c793..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/test_get_recent_message.py
+++ /dev/null
@@ -1,63 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-
-import pytest
-from ragflow_sdk import RAGFlow
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.get_recent_messages(["some_memory_id"])
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestGetRecentMessage:
- @pytest.mark.p3
- def test_get_recent_messages(self, client):
- memory_id = self.memory_id
- res = client.get_recent_messages([memory_id])
- assert len(res) == 5, res
-
- @pytest.mark.p3
- def test_filter_recent_messages_by_agent(self, client):
- memory_id = self.memory_id
- agent_ids = self.agent_ids
- agent_id = random.choice(agent_ids)
- res = client.get_recent_messages(**{"agent_id": agent_id, "memory_id": [memory_id]})
- for message in res:
- assert message["agent_id"] == agent_id, message
-
- @pytest.mark.p2
- def test_filter_recent_messages_by_session(self, client):
- memory_id = self.memory_id
- session_ids = self.session_ids
- session_id = random.choice(session_ids)
- res = client.get_recent_messages(**{"session_id": session_id, "memory_id": [memory_id]})
- for message in res:
- assert message["session_id"] == session_id, message
diff --git a/test/testcases/test_sdk_api/test_message_management/test_list_message.py b/test/testcases/test_sdk_api/test_message_management/test_list_message.py
deleted file mode 100644
index 0ae2ac35db..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/test_list_message.py
+++ /dev/null
@@ -1,102 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-import random
-
-import pytest
-from ragflow_sdk import RAGFlow, Memory
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-from utils.engine_utils import get_doc_engine
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- memory = Memory(client, {"id": "empty_memory_id"})
- memory.list_memory_messages()
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestMessageList:
- @pytest.mark.p3
- def test_params_unset(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- res = memory.list_memory_messages()
- assert len(res["messages"]["message_list"]) == 5, str(res)
-
- @pytest.mark.p3
- def test_params_empty(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- res = memory.list_memory_messages(**{})
- assert len(res["messages"]["message_list"]) == 5, str(res)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page": 1, "page_size": 10}, 5),
- ({"page": 2, "page_size": 10}, 0),
- ({"page": 1, "page_size": 2}, 2),
- ({"page": 3, "page_size": 2}, 1),
- ({"page": 5, "page_size": 10}, 0),
- ],
- ids=["normal_first_page", "beyond_max_page", "normal_last_partial_page", "normal_middle_page", "full_data_single_page"],
- )
- def test_page_size(self, client, params, expected_page_size):
- # have added 5 messages in fixture
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- res = memory.list_memory_messages(**params)
- assert len(res["messages"]["message_list"]) == expected_page_size, str(res)
-
- @pytest.mark.p2
- def test_filter_agent_id(self, client):
- memory_id = self.memory_id
- agent_ids = self.agent_ids
- agent_id = random.choice(agent_ids)
- memory = Memory(client, {"id": memory_id})
- res = memory.list_memory_messages(**{"agent_id": agent_id})
- for message in res["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
-
- @pytest.mark.p2
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="Not support.")
- def test_search_keyword(self, client):
- if get_doc_engine(client) == "infinity":
- pytest.skip("Not support.")
- memory_id = self.memory_id
- session_ids = self.session_ids
- session_id = random.choice(session_ids)
- slice_start = random.randint(0, len(session_id) - 2)
- slice_end = random.randint(slice_start + 1, len(session_id) - 1)
- keyword = session_id[slice_start:slice_end]
- memory = Memory(client, {"id": memory_id})
- res = memory.list_memory_messages(**{"keywords": keyword})
- assert len(res["messages"]["message_list"]) > 0, res
- for message in res["messages"]["message_list"]:
- assert keyword in message["session_id"], message
diff --git a/test/testcases/test_sdk_api/test_message_management/test_search_message.py b/test/testcases/test_sdk_api/test_message_management/test_search_message.py
deleted file mode 100644
index 2ce08e54d0..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/test_search_message.py
+++ /dev/null
@@ -1,75 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from ragflow_sdk import RAGFlow, Memory
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- client.search_message("", ["empty_memory_id"])
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_with_multiple_type_message_func")
-class TestSearchMessage:
- @pytest.mark.p1
- def test_query(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- list_res = memory.list_memory_messages()
- assert list_res["messages"]["total_count"] > 0
-
- query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
- res = client.search_message(**{"memory_id": [memory_id], "query": query})
- assert len(res) > 0
-
- @pytest.mark.p2
- def test_query_with_agent_filter(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- list_res = memory.list_memory_messages()
- assert list_res["messages"]["total_count"] > 0
-
- agent_id = self.agent_id
- query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
- res = client.search_message(**{"memory_id": [memory_id], "query": query, "agent_id": agent_id})
- assert len(res) > 0
- for message in res:
- assert message["agent_id"] == agent_id, message
-
- @pytest.mark.p2
- def test_query_with_not_default_params(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- list_res = memory.list_memory_messages()
- assert list_res["messages"]["total_count"] > 0
-
- query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
- params = {"similarity_threshold": 0.1, "keywords_similarity_weight": 0.6, "top_n": 4}
- res = client.search_message(**{"memory_id": [memory_id], "query": query, **params})
- assert len(res) > 0
- assert len(res) <= params["top_n"]
diff --git a/test/testcases/test_sdk_api/test_message_management/test_update_message_status.py b/test/testcases/test_sdk_api/test_message_management/test_update_message_status.py
deleted file mode 100644
index 6ce49bc45e..0000000000
--- a/test/testcases/test_sdk_api/test_message_management/test_update_message_status.py
+++ /dev/null
@@ -1,72 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-
-import pytest
-from ragflow_sdk import RAGFlow, Memory
-from configs import INVALID_API_TOKEN, HOST_ADDRESS, SDK_UNAUTHORIZED_ERROR_MESSAGE
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_message",
- [
- (None, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- (INVALID_API_TOKEN, SDK_UNAUTHORIZED_ERROR_MESSAGE),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_message):
- client = RAGFlow(invalid_auth, HOST_ADDRESS)
- with pytest.raises(Exception) as exception_info:
- memory = Memory(client, {"id": "empty_memory_id"})
- memory.update_message_status(0, False)
- assert str(exception_info.value) == expected_message, str(exception_info.value)
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestUpdateMessageStatus:
- @pytest.mark.p3
- def test_update_to_false(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- list_res = memory.list_memory_messages()
- assert len(list_res["messages"]["message_list"]) > 0, str(list_res)
-
- message = random.choice(list_res["messages"]["message_list"])
- res = memory.update_message_status(message["message_id"], False)
- assert res, str(res)
-
- updated_message_res = memory.get_message_content(message["message_id"])
- assert not updated_message_res["status"], str(updated_message_res)
-
- @pytest.mark.p3
- def test_update_to_true(self, client):
- memory_id = self.memory_id
- memory = Memory(client, {"id": memory_id})
- list_res = memory.list_memory_messages()
- assert len(list_res["messages"]["message_list"]) > 0, str(list_res)
- # set 1 random message to false first
- message = random.choice(list_res["messages"]["message_list"])
- set_to_false_res = memory.update_message_status(message["message_id"], False)
- assert set_to_false_res, str(set_to_false_res)
- updated_message_res = memory.get_message_content(message["message_id"])
- assert not updated_message_res["status"], updated_message_res
- # set to true
- set_to_true_res = memory.update_message_status(message["message_id"], True)
- assert set_to_true_res, str(set_to_true_res)
- res = memory.get_message_content(message["message_id"])
- assert res["status"], res
diff --git a/test/testcases/test_sdk_api/test_session_management/conftest.py b/test/testcases/test_sdk_api/test_session_management/conftest.py
deleted file mode 100644
index 351dfcf4ad..0000000000
--- a/test/testcases/test_sdk_api/test_session_management/conftest.py
+++ /dev/null
@@ -1,49 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from common import batch_add_sessions_with_chat_assistant, delete_all_sessions
-from pytest import FixtureRequest
-from ragflow_sdk import Chat, DataSet, Document, Session
-
-
[email protected](scope="class")
-def add_sessions_with_chat_assistant(request: FixtureRequest, add_chat_assistants: tuple[DataSet, Document, list[Chat]]) -> tuple[Chat, list[Session]]:
- def cleanup():
- for chat_assistant in chat_assistants:
- try:
- delete_all_sessions(chat_assistant)
- except Exception:
- pass
-
- request.addfinalizer(cleanup)
-
- _, _, chat_assistants = add_chat_assistants
- return chat_assistants[0], batch_add_sessions_with_chat_assistant(chat_assistants[0], 5)
-
-
[email protected](scope="function")
-def add_sessions_with_chat_assistant_func(request: FixtureRequest, add_chat_assistants: tuple[DataSet, Document, list[Chat]]) -> tuple[Chat, list[Session]]:
- def cleanup():
- for chat_assistant in chat_assistants:
- try:
- delete_all_sessions(chat_assistant)
- except Exception:
- pass
-
- request.addfinalizer(cleanup)
-
- _, _, chat_assistants = add_chat_assistants
- return chat_assistants[0], batch_add_sessions_with_chat_assistant(chat_assistants[0], 5)
diff --git a/test/testcases/test_sdk_api/test_session_management/test_create_session_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_create_session_with_chat_assistant.py
deleted file mode 100644
index 08fb241162..0000000000
--- a/test/testcases/test_sdk_api/test_session_management/test_create_session_with_chat_assistant.py
+++ /dev/null
@@ -1,217 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import list_all_sessions
-from configs import HOST_ADDRESS, SESSION_WITH_CHAT_NAME_LIMIT
-from ragflow_sdk import RAGFlow
-from ragflow_sdk.modules.session import Session
-
-
-class _DummyStreamResponse:
- def __init__(self, lines):
- self._lines = lines
-
- def iter_lines(self, decode_unicode=True):
- del decode_unicode
- for line in self._lines:
- yield line
-
-
-class _DummyJsonResponse:
- def __init__(self, payload):
- self._payload = payload
-
- def json(self):
- return self._payload
-
-
[email protected]("clear_session_with_chat_assistants")
-class TestSessionWithChatAssistantCreate:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("valid_name", ""),
- pytest.param("a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1), "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param(1, "", marks=pytest.mark.skip(reason="issues/")),
- ("", "`name` can not be empty"),
- ("duplicated_name", ""),
- ("case insensitive", ""),
- ],
- )
- def test_name(self, add_chat_assistants, name, expected_message):
- _, _, chat_assistants = add_chat_assistants
- chat_assistant = chat_assistants[0]
-
- if name == "duplicated_name":
- chat_assistant.create_session(name=name)
- elif name == "case insensitive":
- chat_assistant.create_session(name=name.upper())
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.create_session(name=name)
- assert expected_message in str(exception_info.value)
- else:
- session = chat_assistant.create_session(name=name)
- assert session.name == name, str(session)
- assert session.chat_id == chat_assistant.id, str(session)
-
- @pytest.mark.p3
- def test_concurrent_create_session(self, add_chat_assistants):
- count = 1000
- _, _, chat_assistants = add_chat_assistants
- chat_assistant = chat_assistants[0]
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(chat_assistant.create_session, name=f"session with chat assistant test {i}") for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- updated_sessions = list_all_sessions(chat_assistant, limit=count + 1)
- assert len(updated_sessions) == count
-
- @pytest.mark.p3
- def test_add_session_to_deleted_chat_assistant(self, client, add_chat_assistants):
- _, _, chat_assistants = add_chat_assistants
- chat_assistant = chat_assistants[0]
-
- client.delete_chats(ids=[chat_assistant.id])
- with pytest.raises(Exception) as exception_info:
- chat_assistant.create_session(name="valid_name")
- assert "no authorization" in str(exception_info.value)
-
-
-def test_session_module_streaming_and_helper_paths_unit(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
- chat_session = Session(client, {"id": "session-chat", "chat_id": "chat-1"})
- chat_done_session = Session(client, {"id": "session-chat-done", "chat_id": "chat-1"})
- agent_session = Session(client, {"id": "session-agent", "agent_id": "agent-1"})
- calls = []
-
- chat_stream = _DummyStreamResponse(
- [
- "",
- "data: {bad json}",
- 'data: {"event":"workflow_started","data":{"content":"skip"}}',
- '{"data":{"answer":"chat-answer","reference":{"chunks":[{"id":"chunk-1"}]}}}',
- 'data: {"data": true}',
- "data: [DONE]",
- ]
- )
- agent_stream = _DummyStreamResponse(
- [
- "data: {bad json}",
- 'data: {"event":"message","data":{"content":"agent-answer"}}',
- 'data: {"event":"message_end","data":{"content":"done"}}',
- ]
- )
-
- def _chat_post(path, json=None, stream=False, files=None):
- calls.append(("chat", path, json, stream, files))
- return chat_stream
-
- def _agent_post(path, json=None, stream=False, files=None):
- calls.append(("agent", path, json, stream, files))
- return agent_stream
-
- monkeypatch.setattr(chat_session, "post", _chat_post)
- monkeypatch.setattr(
- chat_done_session,
- "post",
- lambda *_args, **_kwargs: _DummyStreamResponse(['{"data":{"answer":"chat-done","reference":{"chunks":[]}}}', "data: [DONE]"]),
- )
- monkeypatch.setattr(agent_session, "post", _agent_post)
-
- chat_messages = list(chat_session.ask("hello chat", stream=True, temperature=0.2))
- assert len(chat_messages) == 1
- assert chat_messages[0].content == "chat-answer"
- assert chat_messages[0].reference == [{"id": "chunk-1"}]
-
- chat_done_messages = list(chat_done_session.ask("hello done", stream=True))
- assert len(chat_done_messages) == 1
- assert chat_done_messages[0].content == "chat-done"
-
- agent_messages = list(agent_session.ask("hello agent", stream=True, top_p=0.8))
- assert len(agent_messages) == 1
- assert agent_messages[0].content == "agent-answer"
-
- assert calls[0][1] == "/chats/chat-1/completions"
- assert calls[0][2]["question"] == "hello chat"
- assert calls[0][2]["session_id"] == "session-chat"
- assert calls[0][2]["temperature"] == 0.2
- assert calls[0][3] is True
- assert calls[1][1] == "/agents/chat/completions"
- assert calls[1][2]["agent_id"] == "agent-1"
- assert calls[1][2]["query"] == "hello agent"
- assert calls[1][2]["session_id"] == "session-agent"
- assert calls[1][2]["openai-compatible"] is False
- assert calls[1][2]["top_p"] == 0.8
- assert calls[1][3] is True
-
-
-def test_agent_session_preserves_non_stream_reference_chunks(monkeypatch, caplog):
- first_chunk = {"id": "chunk-1", "content": "first source"}
- second_chunk = {"id": "chunk-2", "content": "second source"}
- session = Session(None, {"id": "session-agent", "agent_id": "agent-1"})
- response = _DummyJsonResponse(
- {
- "data": {
- "event": "message_end",
- "data": {
- "content": "agent-answer",
- "reference": {"chunks": {"500": first_chunk, "100": second_chunk}},
- },
- }
- }
- )
- monkeypatch.setattr(session, "post", lambda *_args, **_kwargs: response)
-
- with caplog.at_level("DEBUG", logger="ragflow_sdk.modules.session"):
- messages = list(session.ask("hello agent", stream=False))
-
- assert len(messages) == 1
- assert messages[0].content == "agent-answer"
- assert messages[0].reference == [first_chunk, second_chunk]
- assert "session_id=session-agent event=message_end reference_count=2" in caplog.text
- assert "first source" not in caplog.text
- assert "second source" not in caplog.text
-
-
-def test_agent_session_streams_all_message_references_without_duplicate_content(monkeypatch):
- first_chunk = {"id": "chunk-1", "content": "first source"}
- second_chunk = {"id": "chunk-2", "content": "second source"}
- session = Session(None, {"id": "session-agent", "agent_id": "agent-1"})
- response = _DummyStreamResponse(
- [
- 'data: {"event":"message","data":{"content":"first answer"}}',
- 'data: {"event":"message_end","data":{"content":"first answer","reference":{"chunks":{"123":{"id":"chunk-1","content":"first source"}}}}}',
- 'data: {"event":"message","data":{"content":"second answer"}}',
- 'data: {"event":"message_end","data":{"content":"second answer","reference":{"chunks":{"456":{"id":"chunk-2","content":"second source"}}}}}',
- "data: [DONE]",
- ]
- )
- monkeypatch.setattr(session, "post", lambda *_args, **_kwargs: response)
-
- messages = list(session.ask("hello agent", stream=True))
-
- assert "".join(message.content for message in messages) == "first answersecond answer"
- assert [message.reference for message in messages if message.reference] == [[first_chunk], [second_chunk]]
diff --git a/test/testcases/test_sdk_api/test_session_management/test_delete_sessions_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_delete_sessions_with_chat_assistant.py
deleted file mode 100644
index ae996dc659..0000000000
--- a/test/testcases/test_sdk_api/test_session_management/test_delete_sessions_with_chat_assistant.py
+++ /dev/null
@@ -1,111 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from common import batch_add_sessions_with_chat_assistant
-
-
-class TestSessionWithChatAssistantDelete:
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_delete_partial_invalid_id(self, add_sessions_with_chat_assistant_func, payload):
- chat_assistant, sessions = add_sessions_with_chat_assistant_func
- if callable(payload):
- payload = payload([session.id for session in sessions])
-
- chat_assistant.delete_sessions(**payload)
-
- sessions = chat_assistant.list_sessions()
- assert len(sessions) == 0
-
- @pytest.mark.p3
- def test_repeated_deletion(self, add_sessions_with_chat_assistant_func):
- chat_assistant, sessions = add_sessions_with_chat_assistant_func
- session_ids = {"ids": [session.id for session in sessions]}
-
- chat_assistant.delete_sessions(**session_ids)
-
- with pytest.raises(Exception) as exception_info:
- chat_assistant.delete_sessions(**session_ids)
- assert "The chat doesn't own the session" in str(exception_info.value)
-
- @pytest.mark.p3
- def test_duplicate_deletion(self, add_sessions_with_chat_assistant_func):
- chat_assistant, sessions = add_sessions_with_chat_assistant_func
- session_ids = {"ids": [session.id for session in sessions] * 2}
- chat_assistant.delete_sessions(**session_ids)
- sessions = chat_assistant.list_sessions()
- assert len(sessions) == 0
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, add_chat_assistants):
- count = 100
- _, _, chat_assistants = add_chat_assistants
- chat_assistant = chat_assistants[0]
- sessions = batch_add_sessions_with_chat_assistant(chat_assistant, count)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(chat_assistant.delete_sessions, ids=[sessions[i].id]) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- @pytest.mark.p3
- def test_delete_1k(self, add_chat_assistants):
- count = 1_000
- _, _, chat_assistants = add_chat_assistants
- chat_assistant = chat_assistants[0]
- ssessions = batch_add_sessions_with_chat_assistant(chat_assistant, count)
- chat_assistant.delete_sessions(ids=[ssession.id for ssession in ssessions])
-
- sessions = chat_assistant.list_sessions()
- assert len(sessions) == 0
-
- @pytest.mark.parametrize(
- "payload, expected_message, remaining",
- [
- pytest.param(None, "", 5, marks=pytest.mark.p3),
- pytest.param({"ids": ["invalid_id"]}, "The chat doesn't own the session invalid_id", 5, marks=pytest.mark.p3),
- pytest.param("not json", """AttributeError("\'str\' object has no attribute \'get\'")""", 5, marks=pytest.mark.skip),
- pytest.param(lambda r: {"ids": r[:1]}, "", 4, marks=pytest.mark.p3),
- pytest.param(lambda r: {"ids": r}, "", 0, marks=pytest.mark.p3),
- pytest.param({"ids": []}, "", 5, marks=pytest.mark.p3),
- ],
- )
- def test_basic_scenarios(self, add_sessions_with_chat_assistant_func, payload, expected_message, remaining):
- chat_assistant, sessions = add_sessions_with_chat_assistant_func
- if callable(payload):
- payload = payload([session.id for session in sessions])
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.delete_sessions(**payload)
- assert expected_message in str(exception_info.value)
- else:
- if payload is None:
- chat_assistant.delete_sessions()
- else:
- chat_assistant.delete_sessions(**payload)
-
- sessions = chat_assistant.list_sessions()
- assert len(sessions) == remaining
diff --git a/test/testcases/test_sdk_api/test_session_management/test_list_sessions_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_list_sessions_with_chat_assistant.py
deleted file mode 100644
index 21b6fea9f9..0000000000
--- a/test/testcases/test_sdk_api/test_session_management/test_list_sessions_with_chat_assistant.py
+++ /dev/null
@@ -1,281 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from configs import HOST_ADDRESS
-from ragflow_sdk import RAGFlow
-from ragflow_sdk.modules.session import Message, Session
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-class TestSessionsWithChatAssistantList:
- @pytest.mark.p3
- def test_list_sessions_raises_on_nonzero_response(self, add_sessions_with_chat_assistant, monkeypatch):
- chat_assistant, _ = add_sessions_with_chat_assistant
-
- class _DummyResponse:
- def json(self):
- return {"code": 1, "message": "boom"}
-
- monkeypatch.setattr(chat_assistant, "get", lambda *_args, **_kwargs: _DummyResponse())
-
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions()
- assert "boom" in str(exception_info.value)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 0, "not instance of"),
- pytest.param({"page": 0, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- ({"page": 2, "page_size": 2}, 2, ""),
- ({"page": 3, "page_size": 2}, 1, ""),
- ({"page": "3", "page_size": 2}, 0, "not instance of"),
- pytest.param({"page": -1, "page_size": 2}, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- pytest.param({"page": "a", "page_size": 2}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page(self, add_sessions_with_chat_assistant, params, expected_page_size, expected_message):
- chat_assistant, _ = add_sessions_with_chat_assistant
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions(**params)
- assert expected_message in str(exception_info.value)
- else:
- sessions = chat_assistant.list_sessions(**params)
- assert len(sessions) == expected_page_size
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size, expected_message",
- [
- ({"page_size": None}, 0, "not instance of"),
- ({"page_size": 0}, 5, ""),
- ({"page_size": 1}, 1, ""),
- ({"page_size": 6}, 5, ""),
- ({"page_size": "1"}, 0, "not instance of"),
- pytest.param({"page_size": -1}, 5, "", marks=pytest.mark.skip),
- pytest.param({"page_size": "a"}, 0, """ValueError("invalid literal for int() with base 10: \'a\'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page_size(self, add_sessions_with_chat_assistant, params, expected_page_size, expected_message):
- chat_assistant, _ = add_sessions_with_chat_assistant
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions(**params)
- assert expected_message in str(exception_info.value)
- else:
- sessions = chat_assistant.list_sessions(**params)
- assert len(sessions) == expected_page_size
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"orderby": None}, "not instance of"),
- ({"orderby": "create_time"}, ""),
- ({"orderby": "update_time"}, ""),
- ({"orderby": "name", "desc": "False"}, "not instance of"),
- pytest.param({"orderby": "unknown"}, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/")),
- ],
- )
- def test_orderby(self, add_sessions_with_chat_assistant, params, expected_message):
- chat_assistant, _ = add_sessions_with_chat_assistant
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions(**params)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant.list_sessions(**params)
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_message",
- [
- ({"desc": None}, "not instance of"),
- ({"desc": "true"}, "not instance of"),
- ({"desc": "True"}, "not instance of"),
- ({"desc": True}, ""),
- ({"desc": "false"}, "not instance of"),
- ({"desc": "False"}, "not instance of"),
- ({"desc": False}, ""),
- ({"desc": "False", "orderby": "update_time"}, "not instance of"),
- pytest.param({"desc": "unknown"}, "desc should be true or false", marks=pytest.mark.skip(reason="issues/")),
- ],
- )
- def test_desc(self, add_sessions_with_chat_assistant, params, expected_message):
- chat_assistant, _ = add_sessions_with_chat_assistant
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions(**params)
- assert expected_message in str(exception_info.value)
- else:
- chat_assistant.list_sessions(**params)
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_num, expected_message",
- [
- ({"name": None}, 0, "not instance of"),
- ({"name": ""}, 5, ""),
- ({"name": "session_with_chat_assistant_1"}, 1, ""),
- ({"name": "unknown"}, 0, ""),
- ],
- )
- def test_name(self, add_sessions_with_chat_assistant, params, expected_num, expected_message):
- chat_assistant, _ = add_sessions_with_chat_assistant
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions(**params)
- assert expected_message in str(exception_info.value)
- else:
- sessions = chat_assistant.list_sessions(**params)
- if params["name"] == "session_with_chat_assistant_1":
- assert sessions[0].name == params["name"]
- else:
- assert len(sessions) == expected_num
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "session_id, expected_num, expected_message",
- [
- (None, 0, "not instance of"),
- ("", 5, ""),
- (lambda r: r[0], 1, ""),
- ("unknown", 0, ""),
- ],
- )
- def test_id(self, add_sessions_with_chat_assistant, session_id, expected_num, expected_message):
- chat_assistant, sessions = add_sessions_with_chat_assistant
- if callable(session_id):
- params = {"id": session_id([s.id for s in sessions])}
- else:
- params = {"id": session_id}
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions(**params)
- assert expected_message in str(exception_info.value)
- else:
- list_sessions = chat_assistant.list_sessions(**params)
- if "id" in params and params["id"] == sessions[0].id:
- assert list_sessions[0].id == params["id"]
- else:
- assert len(list_sessions) == expected_num
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "session_id, name, expected_num, expected_message",
- [
- (lambda r: r[0], "session_with_chat_assistant_0", 1, ""),
- (lambda r: r[0], "session_with_chat_assistant_100", 0, ""),
- (lambda r: r[0], "unknown", 0, ""),
- ("id", "session_with_chat_assistant_0", 0, ""),
- ],
- )
- def test_name_and_id(self, add_sessions_with_chat_assistant, session_id, name, expected_num, expected_message):
- chat_assistant, sessions = add_sessions_with_chat_assistant
- if callable(session_id):
- params = {"id": session_id([s.id for s in sessions]), "name": name}
- else:
- params = {"id": session_id, "name": name}
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions(**params)
- assert expected_message in str(exception_info.value)
- else:
- list_sessions = chat_assistant.list_sessions(**params)
- assert len(list_sessions) == expected_num
-
- @pytest.mark.p3
- def test_concurrent_list(self, add_sessions_with_chat_assistant):
- count = 100
- chat_assistant, _ = add_sessions_with_chat_assistant
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(chat_assistant.list_sessions) for _ in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- @pytest.mark.p3
- def test_list_chats_after_deleting_associated_chat_assistant(self, client, add_sessions_with_chat_assistant):
- chat_assistant, _ = add_sessions_with_chat_assistant
- client.delete_chats(ids=[chat_assistant.id])
-
- with pytest.raises(Exception) as exception_info:
- chat_assistant.list_sessions()
- assert "no authorization" in str(exception_info.value)
-
-
-def test_session_module_error_paths_unit(monkeypatch):
- client = RAGFlow("token", HOST_ADDRESS)
-
- unknown_session = Session(client, {"id": "session-unknown", "chat_id": "chat-1"})
- unknown_session._Session__session_type = "unknown" # noqa: SLF001
- with pytest.raises(Exception) as exception_info:
- list(unknown_session.ask("hello", stream=False))
- assert "Unknown session type" in str(exception_info.value)
-
- bad_json_session = Session(client, {"id": "session-bad-json", "chat_id": "chat-1"})
-
- class _BadJsonResponse:
- def json(self):
- raise ValueError("json decode failed")
-
- monkeypatch.setattr(bad_json_session, "post", lambda *_args, **_kwargs: _BadJsonResponse())
- with pytest.raises(Exception) as exception_info:
- list(bad_json_session.ask("hello", stream=False))
- assert "Invalid response" in str(exception_info.value)
-
- ok_json_session = Session(client, {"id": "session-ok-json", "chat_id": "chat-1"})
-
- class _OkJsonResponse:
- def json(self):
- return {"data": {"answer": "ok-answer", "reference": {"chunks": [{"id": "chunk-ok"}]}}}
-
- monkeypatch.setattr(ok_json_session, "post", lambda *_args, **_kwargs: _OkJsonResponse())
- ok_messages = list(ok_json_session.ask("hello", stream=False))
- assert len(ok_messages) == 1
- assert ok_messages[0].content == "ok-answer"
- assert ok_messages[0].reference == [{"id": "chunk-ok"}]
-
- transport_session = Session(client, {"id": "session-transport", "chat_id": "chat-1"})
- monkeypatch.setattr(
- transport_session,
- "post",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("transport boom")),
- )
- with pytest.raises(RuntimeError) as exception_info:
- list(transport_session.ask("hello", stream=False))
- assert "transport boom" in str(exception_info.value)
-
- message = Message(client, {})
- assert message.content == "Hi! I am your assistant, can I help you?"
- assert message.reference is None
- assert message.role == "assistant"
- assert message.prompt is None
- assert message.id is None
diff --git a/test/testcases/test_sdk_api/test_session_management/test_update_session_with_chat_assistant.py b/test/testcases/test_sdk_api/test_session_management/test_update_session_with_chat_assistant.py
deleted file mode 100644
index b92cc11eb9..0000000000
--- a/test/testcases/test_sdk_api/test_session_management/test_update_session_with_chat_assistant.py
+++ /dev/null
@@ -1,98 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from random import randint
-
-import pytest
-from configs import SESSION_WITH_CHAT_NAME_LIMIT
-
-
-class TestSessionWithChatAssistantUpdate:
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- pytest.param({"name": "valid_name"}, "", marks=pytest.mark.p3),
- pytest.param({"name": "a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1)}, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": 1}, "", marks=pytest.mark.skip(reason="issues/")),
- pytest.param({"name": ""}, "`name` can not be empty", marks=pytest.mark.p3),
- pytest.param({"name": "duplicated_name"}, "", marks=pytest.mark.p3),
- pytest.param({"name": "case insensitive"}, "", marks=pytest.mark.p3),
- ],
- )
- def test_name(self, add_sessions_with_chat_assistant_func, payload, expected_message):
- chat_assistant, sessions = add_sessions_with_chat_assistant_func
- session = sessions[0]
-
- if payload["name"] == "duplicated_name":
- session.update(payload)
- elif payload["name"] == "case insensitive":
- session.update({"name": payload["name"].upper()})
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- session.update(payload)
- assert expected_message in str(exception_info.value)
- else:
- session.update(payload)
- updated_session = chat_assistant.list_sessions(id=session.id)[0]
- assert updated_session.name == payload["name"]
-
- @pytest.mark.p3
- def test_repeated_update_session(self, add_sessions_with_chat_assistant_func):
- _, sessions = add_sessions_with_chat_assistant_func
- session = sessions[0]
-
- session.update({"name": "valid_name_1"})
- session.update({"name": "valid_name_2"})
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_message",
- [
- pytest.param({"unknown_key": "unknown_value"}, "ValueError", marks=pytest.mark.skip),
- ({}, ""),
- pytest.param(None, "TypeError", marks=pytest.mark.skip),
- ],
- )
- def test_invalid_params(self, add_sessions_with_chat_assistant_func, payload, expected_message):
- _, sessions = add_sessions_with_chat_assistant_func
- session = sessions[0]
-
- if expected_message:
- with pytest.raises(Exception) as exception_info:
- session.update(payload)
- assert expected_message in str(exception_info.value)
- else:
- session.update(payload)
-
- @pytest.mark.p3
- def test_concurrent_update_session(self, add_sessions_with_chat_assistant_func):
- count = 50
- _, sessions = add_sessions_with_chat_assistant_func
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(sessions[randint(0, 4)].update, {"name": f"update session test {i}"}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
-
- @pytest.mark.p3
- def test_update_session_to_deleted_chat_assistant(self, client, add_sessions_with_chat_assistant_func):
- chat_assistant, sessions = add_sessions_with_chat_assistant_func
- client.delete_chats(ids=[chat_assistant.id])
-
- with pytest.raises(Exception) as exception_info:
- sessions[0].update({"name": "valid_name"})
- assert "no authorization" in str(exception_info.value)
diff --git a/test/testcases/test_web_api/conftest.py b/test/testcases/test_web_api/conftest.py
deleted file mode 100644
index 4e20efc192..0000000000
--- a/test/testcases/test_web_api/conftest.py
+++ /dev/null
@@ -1,176 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from time import sleep
-from ragflow_sdk import RAGFlow
-from configs import HOST_ADDRESS, VERSION
-import pytest
-from test_common import (
- batch_add_chunks,
- batch_create_datasets,
- bulk_upload_documents,
- delete_chunks,
- delete_chats,
- list_chunks,
- list_documents,
- list_datasets,
- parse_documents,
- delete_datasets,
-)
-from libs.auth import RAGFlowWebApiAuth
-from pytest import FixtureRequest
-from utils import wait_for
-from utils.file_utils import (
- create_docx_file,
- create_eml_file,
- create_excel_file,
- create_html_file,
- create_image_file,
- create_json_file,
- create_md_file,
- create_pdf_file,
- create_ppt_file,
- create_txt_file,
-)
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_auth, _kb_id):
- res = list_documents(_auth, {"kb_id": _kb_id})
- for doc in res["data"]["docs"]:
- if doc["run"] != "DONE":
- return False
- return True
-
-
-def generate_test_files(request: FixtureRequest, tmp_path):
- file_creators = {
- "docx": (tmp_path / "ragflow_test.docx", create_docx_file),
- "excel": (tmp_path / "ragflow_test.xlsx", create_excel_file),
- "ppt": (tmp_path / "ragflow_test.pptx", create_ppt_file),
- "image": (tmp_path / "ragflow_test.png", create_image_file),
- "pdf": (tmp_path / "ragflow_test.pdf", create_pdf_file),
- "txt": (tmp_path / "ragflow_test.txt", create_txt_file),
- "md": (tmp_path / "ragflow_test.md", create_md_file),
- "json": (tmp_path / "ragflow_test.json", create_json_file),
- "eml": (tmp_path / "ragflow_test.eml", create_eml_file),
- "html": (tmp_path / "ragflow_test.html", create_html_file),
- }
-
- files = {}
- for file_type, (file_path, creator_func) in file_creators.items():
- if request.param in ["", file_type]:
- creator_func(file_path)
- files[file_type] = file_path
- return files
-
-
[email protected](scope="class")
-def ragflow_tmp_dir(request, tmp_path_factory):
- class_name = request.cls.__name__
- return tmp_path_factory.mktemp(class_name)
-
-
[email protected](scope="session")
-def client(token: str) -> RAGFlow:
- return RAGFlow(api_key=token, base_url=HOST_ADDRESS, version=VERSION)
-
-
[email protected](scope="session")
-def WebApiAuth(auth):
- return RAGFlowWebApiAuth(auth)
-
-
-def require_env_flag():
- def _require(flag, value="1"):
- if os.getenv(flag) != value:
- pytest.skip(f"Requires {flag}={value}")
-
- return _require
-
-
[email protected](scope="function")
-def clear_datasets(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth):
- def cleanup():
- res = list_datasets(WebApiAuth, params={"page_size": 100})
- kb_ids = [kb["id"] for kb in res["data"]]
- delete_datasets(WebApiAuth, {"ids": kb_ids})
-
- request.addfinalizer(cleanup)
-
-
[email protected](scope="function")
-def clear_chats(request, WebApiAuth):
- def cleanup():
- delete_chats(WebApiAuth)
-
- request.addfinalizer(cleanup)
-
-
[email protected](scope="class")
-def add_dataset(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth) -> str:
- def cleanup():
- res = list_datasets(WebApiAuth, params={"page_size": 100})
- kb_ids = [kb["id"] for kb in res["data"]]
- delete_datasets(WebApiAuth, {"ids": kb_ids})
-
- request.addfinalizer(cleanup)
- return batch_create_datasets(WebApiAuth, 1)[0]
-
-
[email protected](scope="function")
-def add_dataset_func(request: FixtureRequest, WebApiAuth: RAGFlowWebApiAuth) -> str:
- def cleanup():
- res = list_datasets(WebApiAuth, params={"page_size": 100})
- kb_ids = [kb["id"] for kb in res["data"]]
- delete_datasets(WebApiAuth, {"ids": kb_ids})
-
- request.addfinalizer(cleanup)
- return batch_create_datasets(WebApiAuth, 1)[0]
-
-
[email protected](scope="class")
-def add_document(request, WebApiAuth, add_dataset, ragflow_tmp_dir):
- # def cleanup():
- # res = list_documents(WebApiAuth, {"kb_id": dataset_id})
- # for doc in res["data"]["docs"]:
- # delete_document(WebApiAuth, {"doc_id": doc["id"]})
-
- # request.addfinalizer(cleanup)
-
- dataset_id = add_dataset
- return dataset_id, bulk_upload_documents(WebApiAuth, dataset_id, 1, ragflow_tmp_dir)[0]
-
-
[email protected](scope="class")
-def add_chunks(request, WebApiAuth, add_document):
- def cleanup():
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- if res["code"] == 0:
- chunk_ids = [chunk["id"] for chunk in res["data"]["chunks"]]
- delete_chunks(WebApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
-
- request.addfinalizer(cleanup)
-
- dataset_id, document_id = add_document
- parse_documents(WebApiAuth, {"doc_ids": [document_id], "run": "1"})
- condition(WebApiAuth, dataset_id)
- chunk_ids = batch_add_chunks(WebApiAuth, dataset_id, document_id, 4)
- # issues/6487
- sleep(1)
- return dataset_id, document_id, chunk_ids
diff --git a/test/testcases/test_web_api/test_agent_app/test_agents_webhook_unit.py b/test/testcases/test_web_api/test_agent_app/test_agents_webhook_unit.py
deleted file mode 100644
index b556b172a4..0000000000
--- a/test/testcases/test_web_api/test_agent_app/test_agents_webhook_unit.py
+++ /dev/null
@@ -1,1649 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-import base64
-import hashlib
-import hmac
-import importlib.util
-import json
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _Args(dict):
- def get(self, key, default=None, type=None):
- value = super().get(key, default)
- if value is None or type is None:
- return value
- try:
- return type(value)
- except (TypeError, ValueError):
- return default
-
-
-class _DummyRequest:
- def __init__(
- self,
- *,
- path="/api/v1/agents/agent-1/webhook",
- method="POST",
- headers=None,
- content_length=0,
- remote_addr="127.0.0.1",
- args=None,
- json_body=None,
- raw_body=b"",
- form=None,
- files=None,
- authorization=None,
- ):
- self.path = path
- self.method = method
- self.headers = headers or {}
- self.content_length = content_length
- self.remote_addr = remote_addr
- self.args = args or {}
- self.authorization = authorization
- self.form = _AwaitableValue(form or {})
- self.files = _AwaitableValue(files or {})
- self._json_body = json_body
- self._raw_body = raw_body
-
- async def get_json(self):
- return self._json_body
-
- async def get_data(self):
- return self._raw_body
-
-
-class _CanvasRecord:
- def __init__(self, *, canvas_category, dsl, user_id="tenant-1"):
- self.canvas_category = canvas_category
- self.dsl = dsl
- self.user_id = user_id
-
- def to_dict(self):
- return {"user_id": self.user_id, "dsl": self.dsl}
-
-
-class _StubCanvas:
- def __init__(self, dsl, user_id, agent_id, canvas_id=None):
- self.dsl = dsl
- self.user_id = user_id
- self.agent_id = agent_id
- self.canvas_id = canvas_id
-
- async def run(self, **_kwargs):
- if False:
- yield {}
-
- async def get_files_async(self, desc):
- return {"files": desc}
-
- @staticmethod
- def validate_component_parameters(_dsl):
- return {}
-
- def __str__(self):
- return "{}"
-
-
-class _StubRedisConn:
- def __init__(self):
- self.bucket_result = [1]
- self.bucket_exc = None
- self.REDIS = object()
-
- def lua_token_bucket(self, **_kwargs):
- if self.bucket_exc is not None:
- raise self.bucket_exc
- return self.bucket_result
-
- def get(self, _key):
- return None
-
- def set_obj(self, _key, _obj, _ttl):
- return None
-
-
-class _FakeRedisPipeline:
- def __init__(self, client):
- self.client = client
- self.pending = None
-
- def __enter__(self):
- return self
-
- def __exit__(self, *_args):
- return False
-
- def watch(self, _key):
- return None
-
- def get(self, key):
- return self.client.values.get(key)
-
- def multi(self):
- return None
-
- def set(self, key, value, *, ex):
- self.pending = (key, value, ex)
-
- def execute(self):
- self.client.execute_calls += 1
- if self.client.conflicts_remaining:
- if self.client.conflict_value is not None:
- key, conflict_value = self.client.conflict_value
- self.client.values[key] = conflict_value
- self.client.conflicts_remaining -= 1
- raise self.client.watch_error()
-
- key, value, ttl = self.pending
- self.client.values[key] = value
- self.client.ttls[key] = ttl
- return [True]
-
-
-class _FakeRedisClient:
- def __init__(self, *, conflict_value=None, conflicts_remaining=None, watch_error=RuntimeError):
- self.values = {}
- self.ttls = {}
- self.execute_calls = 0
- self.conflict_value = conflict_value
- self.conflicts_remaining = int(conflict_value is not None) if conflicts_remaining is None else conflicts_remaining
- self.watch_error = watch_error
-
- def pipeline(self):
- return _FakeRedisPipeline(self)
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _default_webhook_params(
- *,
- security=None,
- methods=None,
- content_types="application/json",
- schema=None,
- execution_mode="Immediately",
- response=None,
-):
- return {
- "mode": "Webhook",
- "methods": methods if methods is not None else ["POST"],
- "security": security if security is not None else {},
- "content_types": content_types,
- "schema": schema
- if schema is not None
- else {
- "query": {"properties": {}, "required": []},
- "headers": {"properties": {}, "required": []},
- "body": {"properties": {}, "required": []},
- },
- "execution_mode": execution_mode,
- "response": response if response is not None else {},
- }
-
-
-def _anonymous_security(**overrides):
- security = {"auth_type": "none", "allow_anonymous": True}
- security.update(overrides)
- return security
-
-
-def _make_webhook_cvs(module, *, params=None, dsl=None, canvas_category=None):
- if dsl is None:
- if params is None:
- params = _default_webhook_params()
- dsl = {
- "components": {
- "begin": {
- "obj": {"component_name": "Begin", "params": params},
- "downstream": [],
- "upstream": [],
- }
- }
- }
- if canvas_category is None:
- canvas_category = module.CanvasCategory.Agent
- return _CanvasRecord(canvas_category=canvas_category, dsl=dsl)
-
-
-def _patch_background_task(monkeypatch, module):
- def _fake_create_task(coro):
- coro.close()
- return None
-
- monkeypatch.setattr(module.asyncio, "create_task", _fake_create_task)
-
-
-def _load_agents_app(monkeypatch, *, target="rest"):
- repo_root = Path(__file__).resolve().parents[4]
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
- settings_mod = ModuleType("common.settings")
- settings_mod.DATABASE_TYPE = "mysql"
- settings_mod.docStoreConn = SimpleNamespace(
- index_exist=lambda *_args, **_kwargs: False,
- delete=lambda *_args, **_kwargs: None,
- )
- common_pkg.settings = settings_mod
- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = [str(repo_root / "agent")]
- canvas_mod = ModuleType("agent.canvas")
- canvas_mod.Canvas = _StubCanvas
- component_mod = ModuleType("agent.component")
- component_mod.LLM = type("_StubAgentLLM", (), {})
- dsl_migration_mod = ModuleType("agent.dsl_migration")
- dsl_migration_mod.normalize_chunker_dsl = lambda dsl: dsl
- agent_pkg.canvas = canvas_mod
- agent_pkg.component = component_mod
- agent_pkg.dsl_migration = dsl_migration_mod
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
- monkeypatch.setitem(sys.modules, "agent.canvas", canvas_mod)
- monkeypatch.setitem(sys.modules, "agent.component", component_mod)
- monkeypatch.setitem(sys.modules, "agent.dsl_migration", dsl_migration_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.Task = type("_StubTask", (), {"doc_id": "doc_id"})
- db_models_mod.APIToken = type(
- "_StubAPIToken",
- (),
- {"query": staticmethod(lambda **_kwargs: [])},
- )
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- canvas_service_mod = ModuleType("api.db.services.canvas_service")
-
- class _StubUserCanvasService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_list(*_args, **_kwargs):
- return []
-
- @staticmethod
- def get_by_tenant_ids(*_args, **_kwargs):
- return [], 0
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def update_by_id(*_args, **_kwargs):
- return True
-
- @staticmethod
- def delete_by_id(*_args, **_kwargs):
- return True
-
- @staticmethod
- def get_by_id(_id):
- return False, None
-
- @staticmethod
- def get_by_canvas_id(_id):
- return False, None
-
- @staticmethod
- def accessible(*_args, **_kwargs):
- return True
-
- canvas_service_mod.UserCanvasService = _StubUserCanvasService
- canvas_service_mod.CanvasTemplateService = type("_StubCanvasTemplateService", (), {})
- canvas_service_mod.completion = lambda *_args, **_kwargs: None
- canvas_service_mod.completion_openai = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.db.services.canvas_service", canvas_service_mod)
- services_pkg.canvas_service = canvas_service_mod
-
- compilation_template_group_service_mod = ModuleType("api.db.services.compilation_template_group_service")
- compilation_template_group_service_mod.CompilationTemplateGroupService = type(
- "_StubCompilationTemplateGroupService",
- (),
- {"list_saved": staticmethod(lambda *_args, **_kwargs: [])},
- )
- monkeypatch.setitem(sys.modules, "api.db.services.compilation_template_group_service", compilation_template_group_service_mod)
- services_pkg.compilation_template_group_service = compilation_template_group_service_mod
-
- api_service_mod = ModuleType("api.db.services.api_service")
-
- class _StubAPI4ConversationService:
- @staticmethod
- def get_names(*_args, **_kwargs):
- return []
-
- @staticmethod
- def get_list(*_args, **_kwargs):
- return 0, []
-
- api_service_mod.API4ConversationService = _StubAPI4ConversationService
- monkeypatch.setitem(sys.modules, "api.db.services.api_service", api_service_mod)
- services_pkg.api_service = api_service_mod
-
- document_service_mod = ModuleType("api.db.services.document_service")
- document_service_mod.DocumentService = type(
- "_StubDocumentService",
- (),
- {
- "clear_chunk_num_when_rerun": staticmethod(lambda *_args, **_kwargs: True),
- "update_by_id": staticmethod(lambda *_args, **_kwargs: True),
- },
- )
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", document_service_mod)
- services_pkg.document_service = document_service_mod
-
- file_service_mod = ModuleType("api.db.services.file_service")
-
- class _StubFileService:
- @staticmethod
- def upload_info(*_args, **_kwargs):
- return {"id": "uploaded"}
-
- file_service_mod.FileService = _StubFileService
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
- services_pkg.file_service = file_service_mod
-
- knowledgebase_service_mod = ModuleType("api.db.services.knowledgebase_service")
- knowledgebase_service_mod.KnowledgebaseService = type(
- "_StubKnowledgebaseService",
- (),
- {"query": staticmethod(lambda **_kwargs: [])},
- )
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", knowledgebase_service_mod)
- services_pkg.knowledgebase_service = knowledgebase_service_mod
-
- pipeline_log_service_mod = ModuleType("api.db.services.pipeline_operation_log_service")
- pipeline_log_service_mod.PipelineOperationLogService = type(
- "_StubPipelineOperationLogService",
- (),
- {
- "get_documents_info": staticmethod(lambda *_args, **_kwargs: []),
- "update_by_id": staticmethod(lambda *_args, **_kwargs: True),
- },
- )
- monkeypatch.setitem(sys.modules, "api.db.services.pipeline_operation_log_service", pipeline_log_service_mod)
- services_pkg.pipeline_operation_log_service = pipeline_log_service_mod
-
- task_service_mod = ModuleType("api.db.services.task_service")
- task_service_mod.CANVAS_DEBUG_DOC_ID = "debug-doc-id"
- task_service_mod.TaskService = type(
- "_StubTaskService",
- (),
- {"filter_delete": staticmethod(lambda *_args, **_kwargs: True)},
- )
- task_service_mod.queue_dataflow = lambda *_args, **_kwargs: (True, "")
- monkeypatch.setitem(sys.modules, "api.db.services.task_service", task_service_mod)
- services_pkg.task_service = task_service_mod
-
- canvas_version_mod = ModuleType("api.db.services.user_canvas_version")
-
- class _StubUserCanvasVersionService:
- @staticmethod
- def insert(**_kwargs):
- return True
-
- @staticmethod
- def delete_all_versions(*_args, **_kwargs):
- return True
-
- @staticmethod
- def save_or_replace_latest(*_args, **_kwargs):
- return True
-
- @staticmethod
- def build_version_title(*_args, **_kwargs):
- return "stub_version_title"
-
- canvas_version_mod.UserCanvasVersionService = _StubUserCanvasVersionService
- monkeypatch.setitem(sys.modules, "api.db.services.user_canvas_version", canvas_version_mod)
- services_pkg.user_canvas_version = canvas_version_mod
-
- tenant_llm_service_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _StubLLMFactoriesService:
- @staticmethod
- def get_api_key(*_args, **_kwargs):
- return None
-
- tenant_llm_service_mod.LLMFactoriesService = _StubLLMFactoriesService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_service_mod)
- services_pkg.tenant_llm_service = tenant_llm_service_mod
-
- user_service_mod = ModuleType("api.db.services.user_service")
-
- class _StubTenantService:
- @staticmethod
- def get_joined_tenants_by_user_id(_tenant_id):
- return []
-
- class _StubUserService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_by_id(_id):
- return False, None
-
- user_service_mod.TenantService = _StubTenantService
- user_service_mod.UserService = _StubUserService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
- services_pkg.user_service = user_service_mod
- services_pkg.TenantService = _StubTenantService
- services_pkg.UserService = _StubUserService
-
- # Stub api.apps package to prevent api/apps/__init__.py from executing
- # (it triggers heavy imports like quart, settings, DB connections).
- api_apps_pkg = ModuleType("api.apps")
- api_apps_pkg.__path__ = []
- api_apps_pkg.AUTH_API = "api"
- api_apps_pkg.AUTH_BETA = "beta"
- api_apps_pkg.AUTH_JWT = "jwt"
- api_apps_pkg.current_user = SimpleNamespace(id="tenant-1")
-
- def _identity_decorator(func=None, **_kwargs):
- if func is None:
- return lambda wrapped: wrapped
- return func
-
- api_apps_pkg.login_required = _identity_decorator
- monkeypatch.setitem(sys.modules, "api.apps", api_apps_pkg)
-
- api_apps_services_pkg = ModuleType("api.apps.services")
- api_apps_services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.apps.services", api_apps_services_pkg)
- api_apps_pkg.services = api_apps_services_pkg
-
- canvas_replica_mod = ModuleType("api.apps.services.canvas_replica_service")
-
- class _StubCanvasReplicaService:
- @classmethod
- def normalize_dsl(cls, dsl):
- import json
-
- if isinstance(dsl, str):
- return json.loads(dsl)
- return dsl
-
- @classmethod
- def bootstrap(cls, *_args, **_kwargs):
- return {}
-
- @classmethod
- def load_for_run(cls, *_args, **_kwargs):
- return None
-
- @classmethod
- def commit_after_run(cls, *_args, **_kwargs):
- return True
-
- @classmethod
- def replace_for_set(cls, *_args, **_kwargs):
- return True
-
- @classmethod
- def create_if_absent(cls, *_args, **_kwargs):
- return {}
-
- canvas_replica_mod.CanvasReplicaService = _StubCanvasReplicaService
- monkeypatch.setitem(sys.modules, "api.apps.services.canvas_replica_service", canvas_replica_mod)
- api_apps_services_pkg.canvas_replica_service = canvas_replica_mod
-
- redis_obj = _StubRedisConn()
- redis_mod = ModuleType("rag.utils.redis_conn")
- redis_mod.REDIS_CONN = redis_obj
- monkeypatch.setitem(sys.modules, "rag.utils.redis_conn", redis_mod)
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = []
- rag_flow_pkg = ModuleType("rag.flow")
- rag_flow_pkg.__path__ = []
- rag_flow_pipeline_mod = ModuleType("rag.flow.pipeline")
- rag_flow_pipeline_mod.Pipeline = type("_StubPipeline", (), {})
- rag_nlp_pkg = ModuleType("rag.nlp")
- rag_search_mod = ModuleType("rag.nlp.search")
- rag_search_mod.index_name = lambda tenant_id: f"idx-{tenant_id}"
- rag_nlp_pkg.search = rag_search_mod
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
- monkeypatch.setitem(sys.modules, "rag.flow", rag_flow_pkg)
- monkeypatch.setitem(sys.modules, "rag.flow.pipeline", rag_flow_pipeline_mod)
- monkeypatch.setitem(sys.modules, "rag.nlp", rag_nlp_pkg)
- monkeypatch.setitem(sys.modules, "rag.nlp.search", rag_search_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "agent_api.py"
- spec = importlib.util.spec_from_file_location("test_agents_webhook_unit", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- module.REDIS_CONN = redis_obj
- return module
-
-
-def _assert_bad_request(res, expected_substring):
- assert isinstance(res, tuple), res
- payload, code = res
- assert code == 400, res
- assert payload["code"] == 400, payload
- assert expected_substring in payload["message"], payload
-
-
-def test_agents_crud_unit_branches(monkeypatch):
- module = _load_agents_app(monkeypatch)
-
- monkeypatch.setattr(module.TenantService, "get_joined_tenants_by_user_id", lambda _tenant_id: [])
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(args={"owner_ids": "other-tenant", "desc": "false", "page": "1", "page_size": "10"}),
- )
- res = module.list_agents.__wrapped__("tenant-1")
- assert res["code"] == module.RetCode.OPERATING_ERROR
- assert "authorized owner_ids" in res["message"]
-
- captured = {}
-
- def fake_get_by_tenant_ids(owner_ids, tenant_id, page, page_size, orderby, desc, keywords, canvas_category_list, tags, canvas_type=None):
- captured["owner_ids"] = owner_ids
- captured["tenant_id"] = tenant_id
- captured["page"] = page
- captured["page_size"] = page_size
- captured["orderby"] = orderby
- captured["desc"] = desc
- captured["keywords"] = keywords
- captured["canvas_category_list"] = canvas_category_list
- captured["tags"] = tags
- return [{"id": "agent-1"}], 1
-
- monkeypatch.setattr(module.UserCanvasService, "get_by_tenant_ids", fake_get_by_tenant_ids)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"desc": "true"}))
- res = module.list_agents.__wrapped__("tenant-1")
- assert res["code"] == module.RetCode.SUCCESS
- assert captured["owner_ids"] == ["tenant-1"]
- assert captured["desc"] is True
-
- async def req_no_dsl():
- return {"title": "agent-a"}
-
- monkeypatch.setattr(module, "get_request_json", req_no_dsl)
- res = _run(module.create_agent.__wrapped__("tenant-1"))
- assert res["code"] == module.RetCode.ARGUMENT_ERROR
- assert "No DSL data in request" in res["message"]
-
- async def req_no_title():
- return {"dsl": {"components": {}}}
-
- monkeypatch.setattr(module, "get_request_json", req_no_title)
- res = _run(module.create_agent.__wrapped__("tenant-1"))
- assert res["code"] == module.RetCode.ARGUMENT_ERROR
- assert "No title in request" in res["message"]
-
- async def req_dup():
- return {"dsl": {"components": {}}, "title": "agent-dup"}
-
- monkeypatch.setattr(module, "get_request_json", req_dup)
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [object()])
- res = _run(module.create_agent.__wrapped__("tenant-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "already exists" in res["message"]
-
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module, "get_uuid", lambda: "agent-created")
- monkeypatch.setattr(module.UserCanvasService, "save", lambda **_kwargs: False)
- res = _run(module.create_agent.__wrapped__("tenant-1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "Fail to create agent" in res["message"]
-
- async def req_update():
- return {"dsl": {"nodes": []}, "title": " webhook-agent ", "unused": None}
-
- monkeypatch.setattr(module, "get_request_json", req_update)
- monkeypatch.setattr(module.UserCanvasService, "accessible", lambda *_a, **_kw: False)
-
- @module._require_canvas_access_async
- async def _dummy_update(agent_id, tenant_id):
- return module.get_json_result(data=True)
-
- res = _run(_dummy_update(agent_id="agent-1", tenant_id="tenant-1"))
- assert res["code"] == module.RetCode.OPERATING_ERROR
-
- calls = {"update": 0, "save_or_replace_latest": 0, "replace_for_set": 0}
- monkeypatch.setattr(module.UserCanvasService, "accessible", lambda *_a, **_kw: True)
- monkeypatch.setattr(
- module.UserCanvasService,
- "get_by_id",
- lambda _id: (True, SimpleNamespace(title="agent-1", canvas_category=module.CanvasCategory.Agent, update_time=1234567890)),
- )
- monkeypatch.setattr(
- module.UserCanvasService,
- "update_by_id",
- lambda *_args, **_kwargs: calls.__setitem__("update", calls["update"] + 1),
- )
- monkeypatch.setattr(
- module.UserCanvasVersionService,
- "save_or_replace_latest",
- lambda *_args, **_kwargs: calls.__setitem__("save_or_replace_latest", calls["save_or_replace_latest"] + 1),
- )
- monkeypatch.setattr(
- module.CanvasReplicaService,
- "replace_for_set",
- lambda **_kwargs: calls.__setitem__("replace_for_set", calls["replace_for_set"] + 1) or True,
- )
- res = _run(module.update_agent.__wrapped__("agent-1", "tenant-1"))
- assert res["code"] == module.RetCode.SUCCESS
- assert calls == {"update": 1, "save_or_replace_latest": 1, "replace_for_set": 1}
-
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: False)
-
- @module._require_canvas_owner_sync
- def _dummy_delete(agent_id, tenant_id):
- return module.get_json_result(data=True)
-
- res = _dummy_delete(agent_id="agent-1", tenant_id="tenant-1")
- assert res["code"] == module.RetCode.OPERATING_ERROR
-
-
-def test_webhook_prechecks(monkeypatch):
- module = _load_agents_app(monkeypatch)
- monkeypatch.setattr(module, "request", _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}))
-
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (False, None))
- _assert_bad_request(_run(module.webhook("agent-1")), "Canvas not found")
-
- cvs = _make_webhook_cvs(module, canvas_category=module.CanvasCategory.DataFlow)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Dataflow can not be triggered")
-
- cvs = _make_webhook_cvs(module, dsl="invalid-dsl")
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Invalid DSL format")
-
- cvs = _make_webhook_cvs(
- module,
- dsl={"components": {"begin": {"obj": {"component_name": "Begin", "params": {"mode": "Chat"}}}}},
- )
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Webhook not configured")
-
- params = _default_webhook_params(methods=["GET"])
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "not allowed")
-
-
-def test_webhook_security_dispatch(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}, args={"a": "b"}),
- )
-
- for security, message in (
- ({}, "Webhook security is required"),
- ({"auth_type": "none"}, "Anonymous webhook access requires allow_anonymous"),
- ):
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), message)
-
- for security in (_anonymous_security(), _anonymous_security(allow_anonymous="true")):
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code"), res
- assert res.status_code == 200
-
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security={"auth_type": "unsupported"}))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Unsupported auth_type")
-
-
-def test_webhook_test_requires_owner(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(path="/api/v1/agents/agent-1/webhook/test", headers={"Content-Type": "application/json"}, json_body={}),
- )
-
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [])
- denied = _run(module.webhook_test(agent_id="agent-1"))
- assert denied["code"] == module.RetCode.OPERATING_ERROR
- assert "Only the owner" in denied["message"]
-
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=_anonymous_security()))
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [cvs])
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- allowed = _run(module.webhook_test(agent_id="agent-1"))
- assert hasattr(allowed, "status_code")
- assert allowed.status_code == 200
-
-
-def test_webhook_max_body_size(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- base_request = _DummyRequest(headers={"Content-Type": "application/json"}, json_body={})
- monkeypatch.setattr(module, "request", base_request)
-
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=_anonymous_security()))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
-
- security = _anonymous_security(max_body_size="123")
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Invalid max_body_size format")
-
- security = _anonymous_security(max_body_size="11mb")
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "exceeds maximum allowed size")
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}, content_length=2048),
- )
- security = _anonymous_security(max_body_size="1kb")
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Request body too large")
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}, content_length=10 * 1024 * 1024 + 1),
- )
- security = _anonymous_security()
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Request body too large")
-
- token_security = {"auth_type": "token", "token": {"token_header": "X-TOKEN", "token_value": "ok"}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=token_security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(
- headers={"Content-Type": "application/json", "X-TOKEN": "ok"},
- json_body={},
- content_length=10 * 1024 * 1024 + 1,
- ),
- )
- _assert_bad_request(_run(module.webhook("agent-1")), "Request body too large")
-
-
-def test_webhook_ip_whitelist(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}, remote_addr="127.0.0.1"),
- )
-
- for whitelist in ([], ["127.0.0.0/24"], ["127.0.0.1"]):
- security = _anonymous_security(ip_whitelist=whitelist)
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code"), res
- assert res.status_code == 200
-
- security = _anonymous_security(ip_whitelist=["10.0.0.1"])
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "is not allowed")
-
-
-def test_webhook_rate_limit(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- monkeypatch.setattr(module, "request", _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}))
-
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=_anonymous_security()))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
-
- module.REDIS_CONN.bucket_result = [0]
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=_anonymous_security()))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Too many requests")
-
- module.REDIS_CONN.bucket_result = [1]
- token_security = {"auth_type": "token", "token": {"token_header": "X-TOKEN", "token_value": "ok"}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=token_security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json", "X-TOKEN": "ok"}, json_body={}),
- )
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
-
- module.REDIS_CONN.bucket_result = [0]
- _assert_bad_request(_run(module.webhook("agent-1")), "Too many requests")
-
- module.REDIS_CONN.bucket_result = [1]
- bad_limit = _anonymous_security(rate_limit={"limit": 0, "per": "minute"})
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=bad_limit))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "rate_limit.limit must be > 0")
-
- bad_per = _anonymous_security(rate_limit={"limit": 1, "per": "week"})
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=bad_per))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Invalid rate_limit.per")
-
- module.REDIS_CONN.bucket_result = [0]
- module.REDIS_CONN.bucket_exc = None
- denied = _anonymous_security(rate_limit={"limit": 1, "per": "minute"})
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=denied))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Too many requests")
-
- module.REDIS_CONN.bucket_result = [1]
- module.REDIS_CONN.bucket_exc = RuntimeError("redis failure")
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=denied))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Rate limit error")
-
-
-def test_webhook_token_basic_jwt_auth(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- monkeypatch.setattr(module, "request", _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}))
-
- token_security = {"auth_type": "token", "token": {"token_header": "X-TOKEN", "token_value": "ok"}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=token_security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Invalid token authentication")
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(
- headers={"Content-Type": "application/json"},
- json_body={},
- authorization=SimpleNamespace(username="u", password="bad"),
- ),
- )
- basic_security = {"auth_type": "basic", "basic_auth": {"username": "u", "password": "p"}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=basic_security))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Invalid Basic Auth credentials")
-
- monkeypatch.setattr(module, "request", _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}))
- jwt_missing_secret = {"auth_type": "jwt", "jwt": {}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=jwt_missing_secret))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "JWT secret not configured")
-
- jwt_base = {"auth_type": "jwt", "jwt": {"secret": "secret"}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=jwt_base))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Missing Bearer token")
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json", "Authorization": "Bearer "}, json_body={}),
- )
- _assert_bad_request(_run(module.webhook("agent-1")), "Empty Bearer token")
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json", "Authorization": "Bearer token"}, json_body={}),
- )
- monkeypatch.setattr(module.jwt, "decode", lambda *_args, **_kwargs: (_ for _ in ()).throw(Exception("decode boom")))
- _assert_bad_request(_run(module.webhook("agent-1")), "Invalid JWT")
-
- monkeypatch.setattr(module.jwt, "decode", lambda *_args, **_kwargs: {"exp": 1})
- jwt_reserved = {"auth_type": "jwt", "jwt": {"secret": "secret", "required_claims": ["exp"]}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=jwt_reserved))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Reserved JWT claim cannot be required")
-
- monkeypatch.setattr(module.jwt, "decode", lambda *_args, **_kwargs: {})
- jwt_missing_claim = {"auth_type": "jwt", "jwt": {"secret": "secret", "required_claims": ["role"]}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=jwt_missing_claim))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- _assert_bad_request(_run(module.webhook("agent-1")), "Missing JWT claim")
-
- captured = {}
-
- def fake_decode(token, options, **kwargs):
- captured["token"] = token
- captured["options"] = options
- captured["kwargs"] = kwargs
- return {"role": "admin"}
-
- monkeypatch.setattr(module.jwt, "decode", fake_decode)
- jwt_success = {
- "auth_type": "jwt",
- "jwt": {
- "secret": "secret",
- "audience": "aud",
- "issuer": "iss",
- "required_claims": "role",
- },
- }
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=jwt_success))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
- assert captured["kwargs"]["audience"] == "aud"
- assert captured["kwargs"]["issuer"] == "iss"
- assert captured["options"]["verify_aud"] is True
- assert captured["options"]["verify_iss"] is True
-
- monkeypatch.setattr(module.jwt, "decode", lambda *_args, **_kwargs: {})
- jwt_success_invalid_type = {"auth_type": "jwt", "jwt": {"secret": "secret", "required_claims": 123}}
- cvs = _make_webhook_cvs(module, params=_default_webhook_params(security=jwt_success_invalid_type))
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
-
-
-def test_webhook_parse_request_branches(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- security = _anonymous_security()
- params = _default_webhook_params(security=security, content_types="application/json")
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "text/plain"}, raw_body=b'{"x":1}', json_body={}),
- )
- with pytest.raises(ValueError, match="Invalid Content-Type"):
- _run(module.webhook("agent-1"))
-
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json"}, json_body={"x": 1}, args={"q": "1"}),
- )
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
-
- params = _default_webhook_params(security=security, content_types="multipart/form-data")
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- files = {f"file{i}": object() for i in range(11)}
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(
- headers={"Content-Type": "multipart/form-data"},
- form={"key": "value"},
- files=files,
- json_body={},
- ),
- )
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
-
- uploaded = {"count": 0}
- monkeypatch.setattr(
- module.FileService,
- "upload_info",
- lambda *_args, **_kwargs: uploaded.__setitem__("count", uploaded["count"] + 1) or {"id": "uploaded"},
- )
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(
- headers={"Content-Type": "multipart/form-data"},
- form={"k": "v"},
- files={"file1": object()},
- json_body={},
- ),
- )
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code")
- assert res.status_code == 200
- assert uploaded["count"] == 1
-
-
-def test_webhook_canvas_constructor_exception(monkeypatch):
- module = _load_agents_app(monkeypatch)
-
- params = _default_webhook_params(security=_anonymous_security())
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}),
- )
- monkeypatch.setattr(sys.modules["agent.canvas"], "Canvas", lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("canvas init failed")))
-
- def fake_error_result(*, code, message):
- return SimpleNamespace(code=code, message=message)
-
- monkeypatch.setattr(module, "get_data_error_result", fake_error_result)
- res = _run(module.webhook("agent-1"))
- assert isinstance(res, SimpleNamespace)
- assert res.code == module.RetCode.BAD_REQUEST
- assert "canvas init failed" in res.message
- assert res.status_code == module.RetCode.BAD_REQUEST
-
-
-def test_webhook_trace_polling_branches(monkeypatch):
- module = _load_agents_app(monkeypatch)
- monkeypatch.setattr(
- module.UserCanvasService,
- "get_by_id",
- lambda _id: (True, _CanvasRecord(canvas_category=module.CanvasCategory.Agent, dsl={}, user_id="tenant-1")),
- )
-
- # Missing since_ts.
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args()))
- res = _run(module.webhook_trace("agent-1"))
- assert res["code"] == module.RetCode.SUCCESS
- assert res["data"]["webhook_id"] is None
- assert res["data"]["events"] == []
- assert res["data"]["finished"] is False
-
- # since_ts provided but no Redis data.
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({"since_ts": "100.0"})))
- monkeypatch.setattr(module.REDIS_CONN, "get", lambda _k: None)
- res = _run(module.webhook_trace("agent-1"))
- assert res["code"] == module.RetCode.SUCCESS
- assert res["data"]["webhook_id"] is None
- assert res["data"]["next_since_ts"] == 100.0
- assert res["data"]["events"] == []
- assert res["data"]["finished"] is False
-
- webhooks_obj = {
- "webhooks": {
- "101.0": {
- "events": [
- {"event": "message", "ts": 101.2, "data": {"content": "a"}},
- {"event": "finished", "ts": 102.5},
- ]
- },
- "99.0": {"events": [{"event": "message", "ts": 99.1}]},
- }
- }
- raw = json.dumps(webhooks_obj)
- monkeypatch.setattr(module.REDIS_CONN, "get", lambda _k: raw)
-
- # No candidates newer than since_ts.
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({"since_ts": "200.0"})))
- res = _run(module.webhook_trace("agent-1"))
- assert res["code"] == module.RetCode.SUCCESS
- assert res["data"]["webhook_id"] is None
- assert res["data"]["next_since_ts"] == 200.0
- assert res["data"]["events"] == []
- assert res["data"]["finished"] is False
-
- # Candidate exists and webhook id is assigned.
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({"since_ts": "100.0"})))
- res = _run(module.webhook_trace("agent-1"))
- assert res["code"] == module.RetCode.SUCCESS
- webhook_id = res["data"]["webhook_id"]
- assert webhook_id
- assert res["data"]["events"] == []
- assert res["data"]["next_since_ts"] == 101.0
- assert res["data"]["finished"] is False
-
- # Invalid webhook id.
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(args=_Args({"since_ts": "100.0", "webhook_id": "bad-id"})),
- )
- res = _run(module.webhook_trace("agent-1"))
- assert res["code"] == module.RetCode.SUCCESS
- assert res["data"]["webhook_id"] == "bad-id"
- assert res["data"]["events"] == []
- assert res["data"]["next_since_ts"] == 100.0
- assert res["data"]["finished"] is True
-
- # Valid webhook id with event filtering and finished flag.
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(args=_Args({"since_ts": "101.0", "webhook_id": webhook_id})),
- )
- res = _run(module.webhook_trace("agent-1"))
- assert res["code"] == module.RetCode.SUCCESS
- assert res["data"]["webhook_id"] == webhook_id
- assert [event["ts"] for event in res["data"]["events"]] == [101.2, 102.5]
- assert res["data"]["next_since_ts"] == 102.5
- assert res["data"]["finished"] is True
-
-
-def test_webhook_parse_request_form_and_raw_body_paths(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- security = _anonymous_security()
-
- def _run_with(params, req):
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- monkeypatch.setattr(module, "request", req)
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code"), res
- assert res.status_code == 200
-
- _run_with(
- _default_webhook_params(security=security, content_types="application/x-www-form-urlencoded"),
- _DummyRequest(
- headers={"Content-Type": "application/x-www-form-urlencoded"},
- form={"a": "1", "b": "2"},
- json_body={},
- ),
- )
-
- _run_with(
- _default_webhook_params(security=security, content_types="text/plain"),
- _DummyRequest(headers={"Content-Type": "text/plain"}, raw_body=b'{"k": 1}', json_body={}),
- )
-
- _run_with(
- _default_webhook_params(security=security, content_types="text/plain"),
- _DummyRequest(headers={"Content-Type": "text/plain"}, raw_body=b"{bad-json}", json_body={}),
- )
-
- _run_with(
- _default_webhook_params(security=security, content_types="text/plain"),
- _DummyRequest(headers={"Content-Type": "text/plain"}, raw_body=b"", json_body={}),
- )
-
- class _BrokenRawRequest(_DummyRequest):
- async def get_data(self):
- raise RuntimeError("raw read failed")
-
- _run_with(
- _default_webhook_params(security=security, content_types="text/plain"),
- _BrokenRawRequest(headers={"Content-Type": "text/plain"}, json_body={}),
- )
-
-
-def test_webhook_schema_extract_cast_defaults_and_validation_errors(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- base_schema = {
- "query": {
- "properties": {
- "q_file": {"type": "file"},
- "q_object": {"type": "object"},
- "q_boolean": {"type": "boolean"},
- "q_number": {"type": "number"},
- "q_string": {"type": "string"},
- "q_array": {"type": "array<string>"},
- "q_null": {"type": "null"},
- "q_default_none": {},
- },
- "required": [],
- },
- "headers": {"properties": {"Content-Type": {"type": "string"}}, "required": []},
- "body": {
- "properties": {
- "bool_true": {"type": "boolean"},
- "bool_false": {"type": "boolean"},
- "number_int": {"type": "number"},
- "number_float": {"type": "number"},
- "obj": {"type": "object"},
- "arr": {"type": "array<number>"},
- "text": {"type": "string"},
- "file_list": {"type": "file"},
- "unknown": {"type": "mystery"},
- },
- "required": [
- "bool_true",
- "number_int",
- "obj",
- "arr",
- "text",
- "file_list",
- "unknown",
- ],
- },
- }
-
- params = _default_webhook_params(
- security=_anonymous_security(),
- content_types="application/json",
- schema=base_schema,
- )
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(
- headers={"Content-Type": "application/json"},
- args={},
- json_body={
- "bool_true": "true",
- "bool_false": "0",
- "number_int": "-3",
- "number_float": "2.5",
- "obj": '{"a": 1}',
- "arr": "[1, 2]",
- "text": "hello",
- "file_list": ["f1"],
- "unknown": "mystery",
- },
- ),
- )
- res = _run(module.webhook("agent-1"))
- assert hasattr(res, "status_code"), res
- assert res.status_code == 200
-
- failure_cases = [
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"must": {"type": "string"}}, "required": ["must"]}},
- {},
- "missing required field",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"flag": {"type": "boolean"}}, "required": ["flag"]}},
- {"flag": "maybe"},
- "auto-cast failed",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"num": {"type": "number"}}, "required": ["num"]}},
- {"num": "abc"},
- "auto-cast failed",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"obj": {"type": "object"}}, "required": ["obj"]}},
- {"obj": "[]"},
- "auto-cast failed",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"arr": {"type": "array<number>"}}, "required": ["arr"]}},
- {"arr": "{}"},
- "auto-cast failed",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"num": {"type": "number"}}, "required": ["num"]}},
- {"num": []},
- "type mismatch",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"arr": {"type": "array<number>"}}, "required": ["arr"]}},
- {"arr": 3},
- "type mismatch",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"arr": {"type": "array<number>"}}, "required": ["arr"]}},
- {"arr": [1, "x"]},
- "type mismatch",
- ),
- (
- {"query": {"properties": {}, "required": []}, "headers": {"properties": {}, "required": []}, "body": {"properties": {"file": {"type": "file"}}, "required": ["file"]}},
- {"file": "inline-file"},
- "type mismatch",
- ),
- ]
-
- for schema, body_payload, expected_substring in failure_cases:
- params = _default_webhook_params(
- security=_anonymous_security(),
- content_types="application/json",
- schema=schema,
- )
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(headers={"Content-Type": "application/json"}, json_body=body_payload),
- )
- res = _run(module.webhook("agent-1"))
- _assert_bad_request(res, expected_substring)
-
-
-def test_webhook_immediate_response_status_and_template_validation(monkeypatch):
- module = _load_agents_app(monkeypatch)
- _patch_background_task(monkeypatch, module)
-
- def _run_case(response_cfg):
- params = _default_webhook_params(
- security=_anonymous_security(),
- content_types="application/json",
- response=response_cfg,
- )
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- monkeypatch.setattr(module, "request", _DummyRequest(headers={"Content-Type": "application/json"}, json_body={}))
- return _run(module.webhook("agent-1"))
-
- _assert_bad_request(_run_case({"status": "abc"}), "Invalid response status code")
- _assert_bad_request(_run_case({"status": 500}), "must be between 200 and 399")
-
- empty_res = _run_case({"status": 204, "body_template": ""})
- assert empty_res.status_code == 204
- assert empty_res.content_type == "application/json"
- assert _run(empty_res.get_data(as_text=True)) == "null"
-
- json_res = _run_case({"status": 201, "body_template": '{"ok": true}'})
- assert json_res.status_code == 201
- assert json_res.content_type == "application/json"
- assert json.loads(_run(json_res.get_data(as_text=True))) == {"ok": True}
-
- plain_res = _run_case({"status": 202, "body_template": "plain-text"})
- assert plain_res.status_code == 202
- assert plain_res.content_type == "text/plain"
- assert _run(plain_res.get_data(as_text=True)) == "plain-text"
-
-
-def test_append_webhook_trace_retries_conflicts_without_losing_events(monkeypatch):
- module = _load_agents_app(monkeypatch)
- start_ts = 101.0
- key = "webhook-trace-agent-1-logs"
- concurrent_value = json.dumps(
- {
- "webhooks": {
- str(start_ts): {
- "start_ts": start_ts,
- "events": [{"event": "first", "ts": 200.0}],
- },
- "102.0": {
- "start_ts": 102.0,
- "events": [{"event": "other-run", "ts": 201.0}],
- },
- }
- }
- )
- redis_client = _FakeRedisClient(
- conflict_value=(key, concurrent_value),
- watch_error=module.WatchError,
- )
- monkeypatch.setattr(module.time, "time", lambda: 100.0)
-
- module._append_webhook_trace(
- redis_client,
- "agent-1",
- start_ts,
- {"event": "second", "ts": 1.0},
- )
-
- trace = json.loads(redis_client.values[key])
- events = trace["webhooks"][str(start_ts)]["events"]
- assert [event["event"] for event in events] == ["first", "second"]
- assert events[1]["ts"] > events[0]["ts"]
- assert "102.0" in trace["webhooks"]
- assert redis_client.execute_calls == 2
- assert redis_client.ttls[key] == 600
-
-
-def test_append_webhook_trace_stops_after_retry_budget(monkeypatch):
- module = _load_agents_app(monkeypatch)
- redis_client = _FakeRedisClient(
- conflicts_remaining=module._WEBHOOK_TRACE_MAX_RETRIES,
- watch_error=module.WatchError,
- )
-
- with pytest.raises(RuntimeError, match="after 3 retries"):
- module._append_webhook_trace(
- redis_client,
- "agent-1",
- 101.0,
- {"event": "message"},
- )
-
- assert redis_client.execute_calls == 3
- assert redis_client.values == {}
-
-
-def test_webhook_background_run_success_and_error_trace_paths(monkeypatch):
- module = _load_agents_app(monkeypatch)
- redis_client = _FakeRedisClient()
- module.REDIS_CONN.REDIS = redis_client
-
- update_calls = []
- monkeypatch.setattr(module.UserCanvasService, "update_by_id", lambda *_args, **_kwargs: update_calls.append(True))
-
- tasks = []
-
- def _capture_task(coro):
- tasks.append(coro)
- return SimpleNamespace()
-
- monkeypatch.setattr(module.asyncio, "create_task", _capture_task)
-
- class _CanvasSuccess(_StubCanvas):
- async def run(self, **_kwargs):
- yield {"event": "message", "data": {"content": "ok"}}
-
- def __str__(self):
- return "{}"
-
- monkeypatch.setattr(sys.modules["agent.canvas"], "Canvas", _CanvasSuccess)
-
- params = _default_webhook_params(security=_anonymous_security(), content_types="application/json")
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [cvs])
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(path="/api/v1/agents/agent-1/webhook/test", headers={"Content-Type": "application/json"}, json_body={}),
- )
-
- res = _run(module.webhook_test(agent_id="agent-1"))
- assert res.status_code == 200
- assert len(tasks) == 1
- _run(tasks.pop(0))
- assert update_calls == [True]
-
- key = "webhook-trace-agent-1-logs"
- trace_obj = json.loads(redis_client.values[key])
- ws = next(iter(trace_obj["webhooks"].values()))
- events = ws["events"]
- assert any(event.get("event") == "message" for event in events)
- assert any(event.get("event") == "finished" and event.get("success") is True for event in events)
-
- class _CanvasError(_StubCanvas):
- async def run(self, **_kwargs):
- raise RuntimeError("run failed")
- yield {}
-
- monkeypatch.setattr(sys.modules["agent.canvas"], "Canvas", _CanvasError)
- tasks.clear()
- redis_client.values.clear()
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [cvs])
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- res = _run(module.webhook_test(agent_id="agent-1"))
- assert res.status_code == 200
- _run(tasks.pop(0))
- trace_obj = json.loads(redis_client.values[key])
- ws = next(iter(trace_obj["webhooks"].values()))
- events = ws["events"]
- assert any(event.get("event") == "error" for event in events)
- assert any(event.get("event") == "finished" and event.get("success") is False for event in events)
-
- log_messages = []
- monkeypatch.setattr(module.logging, "exception", lambda msg, *_args, **_kwargs: log_messages.append(str(msg)))
- redis_client.values[key] = "{"
- tasks.clear()
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [cvs])
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id, _cvs=cvs: (True, _cvs))
- _run(module.webhook_test(agent_id="agent-1"))
- _run(tasks.pop(0))
- assert any("Failed to append webhook trace" in msg for msg in log_messages)
-
-
-def test_webhook_sse_success_and_exception_paths(monkeypatch):
- module = _load_agents_app(monkeypatch)
- module.REDIS_CONN.REDIS = _FakeRedisClient()
-
- params = _default_webhook_params(
- security=_anonymous_security(),
- content_types="application/json",
- execution_mode="Deferred",
- )
- cvs = _make_webhook_cvs(module, params=params)
- monkeypatch.setattr(module.UserCanvasService, "query", lambda **_kwargs: [cvs])
- monkeypatch.setattr(module.UserCanvasService, "get_by_id", lambda _id: (True, cvs))
-
- class _CanvasSSESuccess(_StubCanvas):
- async def run(self, **_kwargs):
- yield {"event": "message", "data": {"content": "x", "start_to_think": True}}
- yield {"event": "message", "data": {"content": "y", "end_to_think": True}}
- yield {"event": "message", "data": {"content": "Hello"}}
- yield {"event": "message_end", "data": {"status": "201"}}
-
- monkeypatch.setattr(sys.modules["agent.canvas"], "Canvas", _CanvasSSESuccess)
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(path="/api/v1/agents/agent-1/webhook/test", headers={"Content-Type": "application/json"}, json_body={}),
- )
- res = _run(module.webhook_test(agent_id="agent-1"))
- assert res.status_code == 201
- payload = json.loads(_run(res.get_data(as_text=True)))
- assert payload == {"message": "<think></think>Hello", "success": True, "code": 201}
-
- class _CanvasSSEError(_StubCanvas):
- async def run(self, **_kwargs):
- raise RuntimeError("sse failed")
- yield {}
-
- monkeypatch.setattr(sys.modules["agent.canvas"], "Canvas", _CanvasSSEError)
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(path="/api/v1/agents/agent-1/webhook/test", headers={"Content-Type": "application/json"}, json_body={}),
- )
- res = _run(module.webhook_test(agent_id="agent-1"))
- assert res.status_code == 400
- payload = json.loads(_run(res.get_data(as_text=True)))
- assert payload["code"] == 400
- assert payload["success"] is False
- assert "sse failed" in payload["message"]
-
-
-def test_webhook_trace_encoded_id_generation(monkeypatch):
- module = _load_agents_app(monkeypatch)
- monkeypatch.setattr(
- module.UserCanvasService,
- "get_by_id",
- lambda _id: (True, _CanvasRecord(canvas_category=module.CanvasCategory.Agent, dsl={}, user_id="tenant-1")),
- )
-
- webhooks_obj = {
- "webhooks": {
- "101.0": {
- "events": [{"event": "message", "ts": 101.2}],
- }
- }
- }
- monkeypatch.setattr(module.REDIS_CONN, "get", lambda _key: json.dumps(webhooks_obj))
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({"since_ts": "100.0"})))
- res = _run(module.webhook_trace("agent-1"))
- assert res["code"] == module.RetCode.SUCCESS
-
- expected = (
- base64.urlsafe_b64encode(
- hmac.new(
- b"webhook_id_secret",
- b"101.0",
- hashlib.sha256,
- ).digest()
- )
- .decode("utf-8")
- .rstrip("=")
- )
- assert res["data"]["webhook_id"] == expected
diff --git a/test/testcases/test_web_api/test_auth_app/test_oauth_client_unit.py b/test/testcases/test_web_api/test_auth_app/test_oauth_client_unit.py
deleted file mode 100644
index e65fad17ec..0000000000
--- a/test/testcases/test_web_api/test_auth_app/test_oauth_client_unit.py
+++ /dev/null
@@ -1,195 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import sys
-import urllib.parse
-from pathlib import Path
-from types import ModuleType
-
-import pytest
-
-
-class _FakeResponse:
- def __init__(self, payload=None, err=None):
- self._payload = payload or {}
- self._err = err
-
- def raise_for_status(self):
- if self._err:
- raise self._err
-
- def json(self):
- return self._payload
-
-
-def _base_config(scope="openid profile"):
- return {
- "client_id": "client-1",
- "client_secret": "secret-1",
- "authorization_url": "https://issuer.example/authorize",
- "token_url": "https://issuer.example/token",
- "userinfo_url": "https://issuer.example/userinfo",
- "redirect_uri": "https://app.example/callback",
- "scope": scope,
- }
-
-
-def _load_oauth_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- http_client_mod = ModuleType("common.http_client")
-
- async def _default_async_request(*_args, **_kwargs):
- return _FakeResponse({})
-
- def _default_sync_request(*_args, **_kwargs):
- return _FakeResponse({})
-
- http_client_mod.async_request = _default_async_request
- http_client_mod.sync_request = _default_sync_request
- monkeypatch.setitem(sys.modules, "common.http_client", http_client_mod)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- apps_pkg = ModuleType("api.apps")
- apps_pkg.__path__ = [str(repo_root / "api" / "apps")]
- auth_pkg = ModuleType("api.apps.auth")
- auth_pkg.__path__ = [str(repo_root / "api" / "apps" / "auth")]
-
- monkeypatch.setitem(sys.modules, "api", api_pkg)
- monkeypatch.setitem(sys.modules, "api.apps", apps_pkg)
- monkeypatch.setitem(sys.modules, "api.apps.auth", auth_pkg)
-
- sys.modules.pop("api.apps.auth.oauth", None)
- oauth_path = repo_root / "api" / "apps" / "auth" / "oauth.py"
- oauth_spec = importlib.util.spec_from_file_location("api.apps.auth.oauth", oauth_path)
- oauth_module = importlib.util.module_from_spec(oauth_spec)
- monkeypatch.setitem(sys.modules, "api.apps.auth.oauth", oauth_module)
- oauth_spec.loader.exec_module(oauth_module)
- return oauth_module
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def test_oauth_client_sync_matrix_unit(monkeypatch):
- oauth_module = _load_oauth_module(monkeypatch)
- client = oauth_module.OAuthClient(_base_config())
-
- assert client.client_id == "client-1"
- assert client.client_secret == "secret-1"
- assert client.authorization_url.endswith("/authorize")
- assert client.token_url.endswith("/token")
- assert client.userinfo_url.endswith("/userinfo")
- assert client.redirect_uri.endswith("/callback")
- assert client.scope == "openid profile"
- assert client.http_request_timeout == 7
-
- info = oauth_module.UserInfo("[email protected]", "user1", "User One", "avatar-url")
- assert info.to_dict() == {
- "email": "[email protected]",
- "username": "user1",
- "nickname": "User One",
- "avatar_url": "avatar-url",
- }
-
- auth_url = client.get_authorization_url(state="s p/a?ce")
- parsed = urllib.parse.urlparse(auth_url)
- query = urllib.parse.parse_qs(parsed.query)
- assert parsed.scheme == "https"
- assert query["client_id"] == ["client-1"]
- assert query["redirect_uri"] == ["https://app.example/callback"]
- assert query["response_type"] == ["code"]
- assert query["scope"] == ["openid profile"]
- assert query["state"] == ["s p/a?ce"]
-
- no_scope_client = oauth_module.OAuthClient(_base_config(scope=None))
- no_scope_query = urllib.parse.parse_qs(urllib.parse.urlparse(no_scope_client.get_authorization_url()).query)
- assert "scope" not in no_scope_query
-
- call_log = []
-
- def _sync_ok(method, url, data=None, headers=None, timeout=None):
- call_log.append((method, url, data, headers, timeout))
- if url.endswith("/token"):
- return _FakeResponse({"access_token": "token-1"})
- return _FakeResponse({"email": "[email protected]", "picture": "id-picture"})
-
- monkeypatch.setattr(oauth_module, "sync_request", _sync_ok)
- token = client.exchange_code_for_token("code-1")
- assert token["access_token"] == "token-1"
- user_info = client.fetch_user_info("access-1")
- assert isinstance(user_info, oauth_module.UserInfo)
- assert user_info.to_dict() == {
- "email": "[email protected]",
- "username": "user",
- "nickname": "user",
- "avatar_url": "id-picture",
- }
- assert call_log[0][0] == "POST"
- assert call_log[0][3]["Accept"] == "application/json"
- assert call_log[1][0] == "GET"
- assert call_log[1][3]["Authorization"] == "Bearer access-1"
-
- normalized = client.normalize_user_info({"email": "[email protected]", "username": "fallback-user", "nickname": "fallback-nick", "avatar_url": "direct-avatar"})
- assert normalized.to_dict()["avatar_url"] == "direct-avatar"
-
- monkeypatch.setattr(oauth_module, "sync_request", lambda *_args, **_kwargs: _FakeResponse(err=RuntimeError("status boom")))
- with pytest.raises(ValueError, match="Failed to exchange authorization code for token: status boom"):
- client.exchange_code_for_token("code-2")
- with pytest.raises(ValueError, match="Failed to fetch user info: status boom"):
- client.fetch_user_info("access-2")
-
-
-def test_oauth_client_async_matrix_unit(monkeypatch):
- oauth_module = _load_oauth_module(monkeypatch)
- client = oauth_module.OAuthClient(_base_config())
-
- async def _async_ok(method, url, data=None, headers=None, **kwargs):
- _ = (method, data, headers, kwargs.get("timeout"))
- if url.endswith("/token"):
- return _FakeResponse({"access_token": "token-async"})
- return _FakeResponse({"email": "[email protected]", "username": "async-user", "nickname": "Async User", "avatar_url": "async-avatar"})
-
- monkeypatch.setattr(oauth_module, "async_request", _async_ok)
- token = asyncio.run(client.async_exchange_code_for_token("code-a"))
- assert token["access_token"] == "token-async"
- info = asyncio.run(client.async_fetch_user_info("async-token"))
- assert info.to_dict() == {
- "email": "[email protected]",
- "username": "async-user",
- "nickname": "Async User",
- "avatar_url": "async-avatar",
- }
-
- async def _async_fail(*_args, **_kwargs):
- return _FakeResponse(err=RuntimeError("async boom"))
-
- monkeypatch.setattr(oauth_module, "async_request", _async_fail)
- with pytest.raises(ValueError, match="Failed to exchange authorization code for token: async boom"):
- asyncio.run(client.async_exchange_code_for_token("code-b"))
- with pytest.raises(ValueError, match="Failed to fetch user info: async boom"):
- asyncio.run(client.async_fetch_user_info("async-token-2"))
diff --git a/test/testcases/test_web_api/test_auth_app/test_oidc_client_unit.py b/test/testcases/test_web_api/test_auth_app/test_oidc_client_unit.py
deleted file mode 100644
index de45a0b7f3..0000000000
--- a/test/testcases/test_web_api/test_auth_app/test_oidc_client_unit.py
+++ /dev/null
@@ -1,689 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _FakeResponse:
- def __init__(self, payload=None, err=None):
- self._payload = payload or {}
- self._err = err
-
- def raise_for_status(self):
- if self._err:
- raise self._err
-
- def json(self):
- return self._payload
-
-
-class _DummyJwkClient:
- def __init__(self, _jwks_uri):
- self._key = "dummy-signing-key"
-
- def get_signing_key_from_jwt(self, _id_token):
- return SimpleNamespace(key=self._key)
-
-
-def _load_auth_modules(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- apps_pkg = ModuleType("api.apps")
- apps_pkg.__path__ = [str(repo_root / "api" / "apps")]
- auth_pkg = ModuleType("api.apps.auth")
- auth_pkg.__path__ = [str(repo_root / "api" / "apps" / "auth")]
-
- monkeypatch.setitem(sys.modules, "api", api_pkg)
- monkeypatch.setitem(sys.modules, "api.apps", apps_pkg)
- monkeypatch.setitem(sys.modules, "api.apps.auth", auth_pkg)
-
- for mod_name in ["api.apps.auth.oauth", "api.apps.auth.oidc"]:
- sys.modules.pop(mod_name, None)
-
- oauth_path = repo_root / "api" / "apps" / "auth" / "oauth.py"
- oauth_spec = importlib.util.spec_from_file_location("api.apps.auth.oauth", oauth_path)
- oauth_module = importlib.util.module_from_spec(oauth_spec)
- monkeypatch.setitem(sys.modules, "api.apps.auth.oauth", oauth_module)
- oauth_spec.loader.exec_module(oauth_module)
-
- oidc_path = repo_root / "api" / "apps" / "auth" / "oidc.py"
- oidc_spec = importlib.util.spec_from_file_location("api.apps.auth.oidc", oidc_path)
- oidc_module = importlib.util.module_from_spec(oidc_spec)
- monkeypatch.setitem(sys.modules, "api.apps.auth.oidc", oidc_module)
- oidc_spec.loader.exec_module(oidc_module)
-
- return oauth_module, oidc_module
-
-
-def _load_github_module(monkeypatch):
- _load_auth_modules(monkeypatch)
- repo_root = Path(__file__).resolve().parents[4]
-
- sys.modules.pop("api.apps.auth.github", None)
- github_path = repo_root / "api" / "apps" / "auth" / "github.py"
- github_spec = importlib.util.spec_from_file_location("api.apps.auth.github", github_path)
- github_module = importlib.util.module_from_spec(github_spec)
- monkeypatch.setitem(sys.modules, "api.apps.auth.github", github_module)
- github_spec.loader.exec_module(github_module)
- return github_module
-
-
-def _load_auth_init_module(monkeypatch):
- _load_auth_modules(monkeypatch)
- repo_root = Path(__file__).resolve().parents[4]
-
- github_mod = ModuleType("api.apps.auth.github")
-
- class _StubGithubOAuthClient:
- def __init__(self, config):
- self.config = config
-
- github_mod.GithubOAuthClient = _StubGithubOAuthClient
- monkeypatch.setitem(sys.modules, "api.apps.auth.github", github_mod)
-
- init_path = repo_root / "api" / "apps" / "auth" / "__init__.py"
- init_spec = importlib.util.spec_from_file_location(
- "api.apps.auth",
- init_path,
- submodule_search_locations=[str(repo_root / "api" / "apps" / "auth")],
- )
- init_module = importlib.util.module_from_spec(init_spec)
- monkeypatch.setitem(sys.modules, "api.apps.auth", init_module)
- init_spec.loader.exec_module(init_module)
- return init_module
-
-
-def _base_config():
- return {
- "issuer": "https://issuer.example",
- "client_id": "client-1",
- "client_secret": "secret-1",
- "redirect_uri": "https://app.example/callback",
- }
-
-
-def _metadata(issuer, signing_algs=None):
- md = {
- "issuer": issuer,
- "jwks_uri": f"{issuer}/jwks",
- "authorization_endpoint": f"{issuer}/authorize",
- "token_endpoint": f"{issuer}/token",
- "userinfo_endpoint": f"{issuer}/userinfo",
- }
- if signing_algs is not None:
- md["id_token_signing_alg_values_supported"] = signing_algs
- return md
-
-
-def _make_client(monkeypatch, oidc_module, signing_algs=None):
- monkeypatch.setattr(
- oidc_module.OIDCClient,
- "_load_oidc_metadata",
- staticmethod(lambda issuer: _metadata(issuer, signing_algs=signing_algs)),
- )
- return oidc_module.OIDCClient(_base_config())
-
-
-def test_oidc_init_requires_issuer(monkeypatch):
- _, oidc_module = _load_auth_modules(monkeypatch)
-
- with pytest.raises(ValueError) as exc_info:
- oidc_module.OIDCClient({"client_id": "cid"})
-
- assert str(exc_info.value) == "Missing issuer in configuration."
-
-
-def test_oidc_init_loads_metadata_and_sets_endpoints(monkeypatch):
- _, oidc_module = _load_auth_modules(monkeypatch)
- monkeypatch.setattr(oidc_module.OIDCClient, "_load_oidc_metadata", staticmethod(lambda issuer: _metadata(issuer)))
-
- client = oidc_module.OIDCClient(_base_config())
-
- assert client.issuer == "https://issuer.example"
- assert client.jwks_uri == "https://issuer.example/jwks"
- assert client.authorization_url == "https://issuer.example/authorize"
- assert client.token_url == "https://issuer.example/token"
- assert client.userinfo_url == "https://issuer.example/userinfo"
-
-
-def test_load_oidc_metadata_success_and_wraps_failure(monkeypatch):
- _, oidc_module = _load_auth_modules(monkeypatch)
-
- calls = {}
-
- def _ok_sync_request(method, url, timeout):
- calls.update({"method": method, "url": url, "timeout": timeout})
- return _FakeResponse(_metadata("https://issuer.example"))
-
- monkeypatch.setattr(oidc_module, "sync_request", _ok_sync_request)
- metadata = oidc_module.OIDCClient._load_oidc_metadata("https://issuer.example")
- assert metadata["jwks_uri"] == "https://issuer.example/jwks"
- assert calls == {
- "method": "GET",
- "url": "https://issuer.example/.well-known/openid-configuration",
- "timeout": 7,
- }
-
- def _boom_sync_request(*_args, **_kwargs):
- raise RuntimeError("metadata boom")
-
- monkeypatch.setattr(oidc_module, "sync_request", _boom_sync_request)
- with pytest.raises(ValueError) as exc_info:
- oidc_module.OIDCClient._load_oidc_metadata("https://issuer.example")
- assert str(exc_info.value) == "Failed to fetch OIDC metadata: metadata boom"
-
-
-def test_load_oidc_metadata_strips_trailing_slash_from_issuer(monkeypatch):
- _, oidc_module = _load_auth_modules(monkeypatch)
-
- calls = {}
-
- def _ok_sync_request(method, url, timeout):
- calls["url"] = url
- return _FakeResponse(_metadata("https://issuer.example"))
-
- monkeypatch.setattr(oidc_module, "sync_request", _ok_sync_request)
- oidc_module.OIDCClient._load_oidc_metadata("https://issuer.example/")
- assert calls["url"] == "https://issuer.example/.well-known/openid-configuration"
-
-
-def test_parse_id_token_success_and_error(monkeypatch):
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module)
-
- seen = {}
-
- class _JwkClient(_DummyJwkClient):
- def __init__(self, jwks_uri):
- super().__init__(jwks_uri)
- seen["jwks_uri"] = jwks_uri
-
- def get_signing_key_from_jwt(self, id_token):
- seen["id_token"] = id_token
- return super().get_signing_key_from_jwt(id_token)
-
- monkeypatch.setattr(oidc_module.jwt, "PyJWKClient", _JwkClient)
-
- def _decode(id_token, key, algorithms, audience, issuer):
- seen.update(
- {
- "decode_id_token": id_token,
- "decode_key": key,
- "algorithms": algorithms,
- "audience": audience,
- "issuer": issuer,
- }
- )
- return {"sub": "user-1", "email": "[email protected]"}
-
- monkeypatch.setattr(oidc_module.jwt, "decode", _decode)
- parsed = client.parse_id_token("id-token-1")
-
- assert parsed["sub"] == "user-1"
- assert seen["jwks_uri"] == "https://issuer.example/jwks"
- assert seen["decode_key"] == "dummy-signing-key"
- assert seen["algorithms"] == ["RS256"]
- assert seen["audience"] == "client-1"
- assert seen["issuer"] == "https://issuer.example"
-
- def _raise_decode(*_args, **_kwargs):
- raise RuntimeError("decode boom")
-
- monkeypatch.setattr(oidc_module.jwt, "decode", _raise_decode)
- with pytest.raises(ValueError) as exc_info:
- client.parse_id_token("id-token-2")
- assert str(exc_info.value) == "Error parsing ID Token: decode boom"
-
-
-# ===================================================================== #
-# JWT algorithm-confusion regression tests #
-# #
-# Before the fix, ``parse_id_token`` read the signing algorithm from #
-# the unverified JWT header. An attacker who presents a JWT with #
-# ``"alg": "none"`` would have signature verification disabled, and an #
-# attacker who presents ``"alg": "HS256"`` and signs the JWT with the #
-# public key bytes (RSA / HMAC confusion) would get the forged token #
-# accepted. The tests below pin the contract: #
-# #
-# - the algorithm allowlist is pinned at construction time from the #
-# provider's discovery metadata intersected with the safe allowlist #
-# - the JWT header's ``alg`` claim is never read at decode time #
-# ===================================================================== #
-
-
-def test_id_token_signing_algs_default_to_rs256_when_metadata_missing(monkeypatch):
- """No ``id_token_signing_alg_values_supported`` in metadata → RS256 only.
-
- Crucially the fallback is RS256, never whatever the JWT header claims.
- """
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module)
- assert client.id_token_signing_algs == ["RS256"]
-
-
-def test_id_token_signing_algs_intersect_metadata_with_safe_allowlist(monkeypatch):
- """Metadata advertises a mix of safe and unsafe algs — only safe ones kept."""
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(
- monkeypatch,
- oidc_module,
- signing_algs=["RS256", "ES256", "HS256", "none", "PS512"],
- )
- assert set(client.id_token_signing_algs) == {"RS256", "ES256", "PS512"}
- # The dangerous algorithms must not appear in the verification allowlist.
- assert "HS256" not in client.id_token_signing_algs
- assert "none" not in client.id_token_signing_algs
-
-
-def test_id_token_signing_algs_fall_back_when_only_unsafe_advertised(monkeypatch):
- """Provider advertises only HS256 / none → fall back to RS256, do not trust."""
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(
- monkeypatch,
- oidc_module,
- signing_algs=["HS256", "none", "bogus"],
- )
- assert client.id_token_signing_algs == ["RS256"]
-
-
-def test_id_token_signing_algs_ignores_non_string_entries(monkeypatch):
- """Malformed entries (None / dict / int) are filtered out, not crashed on."""
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(
- monkeypatch,
- oidc_module,
- signing_algs=["RS256", None, 42, {"x": 1}, "ES384"],
- )
- assert set(client.id_token_signing_algs) == {"RS256", "ES384"}
-
-
-def test_id_token_signing_algs_handles_non_list_metadata_field(monkeypatch):
- """If metadata gives a non-list type for the field, fall back to default."""
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module, signing_algs="RS256")
- assert client.id_token_signing_algs == ["RS256"]
-
-
-def test_parse_id_token_passes_pinned_algorithms_to_jwt_decode(monkeypatch):
- """``jwt.decode`` receives the pinned allowlist, regardless of JWT header."""
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module, signing_algs=["RS256", "ES256"])
-
- # Even if the unverified header claims something dangerous, the
- # verification path must not consult it. We sabotage
- # ``jwt.get_unverified_header`` to prove the code never calls it.
- def _explode(_token): # pragma: no cover - must not be called
- raise AssertionError("parse_id_token must not read the algorithm from the unverified JWT header")
-
- monkeypatch.setattr(oidc_module.jwt, "get_unverified_header", _explode)
- monkeypatch.setattr(oidc_module.jwt, "PyJWKClient", _DummyJwkClient)
-
- seen = {}
-
- def _decode(id_token, key, algorithms, audience, issuer):
- seen["algorithms"] = list(algorithms)
- return {"sub": "user-2"}
-
- monkeypatch.setattr(oidc_module.jwt, "decode", _decode)
- client.parse_id_token("malicious-header-token")
-
- assert set(seen["algorithms"]) == {"RS256", "ES256"}
- # Hard-stop: dangerous algorithms must never reach ``jwt.decode``.
- assert "none" not in seen["algorithms"]
- assert "HS256" not in seen["algorithms"]
-
-
-def test_parse_id_token_rejects_alg_none(monkeypatch):
- """End-to-end: an ``alg: "none"`` JWT must not authenticate.
-
- Uses the real PyJWT decoder so the test exercises the actual contract
- between ``parse_id_token`` and the upstream library.
- """
- import jwt as real_jwt
-
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module) # defaults to RS256
-
- # PyJWT requires explicit opt-in to encode ``alg=none``; even then it
- # produces a token with no signature segment.
- forged = real_jwt.encode(
- {
- "sub": "victim-subject",
- "email": "[email protected]",
- "aud": "client-1",
- "iss": "https://issuer.example",
- },
- key="",
- algorithm="none",
- )
- # Force the JWKS step into a no-op so we exercise *just* the alg gate.
- monkeypatch.setattr(oidc_module.jwt, "PyJWKClient", _DummyJwkClient)
-
- with pytest.raises(ValueError) as exc_info:
- client.parse_id_token(forged)
- assert "Error parsing ID Token" in str(exc_info.value)
-
-
-def test_parse_id_token_rejects_hs256_when_allowlist_is_asymmetric(monkeypatch):
- """End-to-end: a JWT whose header claims ``alg: HS256`` must not be
- accepted when the pinned allowlist is asymmetric-only.
-
- This is the algorithm half of the RSA / HMAC confusion attack
- (CWE-347). The attacker forges a JWT with ``"alg": "HS256"`` so the
- server picks the HMAC verifier; pre-fix the server would read that alg
- straight from the header and call
- ``jwt.decode(..., algorithms=["HS256"], key=public_key)`` which lets
- the attacker forge tokens with the public key bytes. After the fix the
- allowlist pinned at construction time wins — HS* is never in it — so
- PyJWT raises ``InvalidAlgorithmError`` before the HMAC verifier is
- ever invoked.
-
- Note: modern PyJWT (>=2.0) also independently refuses to use a
- PEM-encoded key as an HMAC secret, so the public-key-bytes leg of the
- full attack is partially mitigated at the library level. The fix here
- is defense in depth and the only mitigation for non-PEM key formats
- (raw bytes, DER, JWK octet keys, older PyJWT versions).
- """
- import jwt as real_jwt
-
- _, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module) # defaults to RS256
-
- # Use a non-PEM byte string so we exercise the alg gate (not PyJWT's
- # incidental PEM-as-HMAC-secret refusal).
- shared_secret = b"shared-secret-bytes-not-a-pem-key"
- forged = real_jwt.encode(
- {
- "sub": "victim-subject",
- "email": "[email protected]",
- "aud": "client-1",
- "iss": "https://issuer.example",
- },
- key=shared_secret,
- algorithm="HS256",
- )
-
- class _SecretJwkClient(_DummyJwkClient):
- def get_signing_key_from_jwt(self, _id_token):
- return SimpleNamespace(key=shared_secret)
-
- monkeypatch.setattr(oidc_module.jwt, "PyJWKClient", _SecretJwkClient)
-
- with pytest.raises(ValueError) as exc_info:
- client.parse_id_token(forged)
- assert "Error parsing ID Token" in str(exc_info.value)
-
-
-def test_fetch_user_info_merges_id_token_and_oauth_userinfo(monkeypatch):
- oauth_module, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module)
-
- monkeypatch.setattr(
- oidc_module.OIDCClient,
- "parse_id_token",
- lambda self, _id_token: {"picture": "id-picture", "email": "[email protected]"},
- )
-
- def _fake_parent_fetch(self, access_token, **_kwargs):
- assert access_token == "access-1"
- return oauth_module.UserInfo(
- username="oauth-user",
- nickname="oauth-nick",
- avatar_url=None,
- )
-
- monkeypatch.setattr(oauth_module.OAuthClient, "fetch_user_info", _fake_parent_fetch)
-
- info = client.fetch_user_info("access-1", id_token="id-token")
-
- assert info.email == "[email protected]"
- assert info.username == "oauth-user"
- assert info.nickname == "oauth-nick"
- assert info.avatar_url == "id-picture"
-
-
-def test_async_fetch_user_info_merges_id_token_and_oauth_userinfo(monkeypatch):
- oauth_module, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module)
-
- monkeypatch.setattr(
- oidc_module.OIDCClient,
- "parse_id_token",
- lambda self, _id_token: {"picture": "id-picture-async", "email": "[email protected]"},
- )
-
- async def _fake_parent_async_fetch(self, access_token, **_kwargs):
- assert access_token == "access-2"
- return oauth_module.UserInfo(
- username="oauth-async-user",
- nickname="oauth-async-nick",
- avatar_url=None,
- )
-
- monkeypatch.setattr(oauth_module.OAuthClient, "async_fetch_user_info", _fake_parent_async_fetch)
-
- info = asyncio.run(client.async_fetch_user_info("access-2", id_token="id-token"))
-
- assert info.email == "[email protected]"
- assert info.username == "oauth-async-user"
- assert info.nickname == "oauth-async-nick"
- assert info.avatar_url == "id-picture-async"
-
-
-def test_normalize_user_info_passthrough(monkeypatch):
- oauth_module, oidc_module = _load_auth_modules(monkeypatch)
- client = _make_client(monkeypatch, oidc_module)
-
- result = client.normalize_user_info(
- {
- "email": "[email protected]",
- "username": "user",
- "nickname": "User",
- "picture": "picture-url",
- }
- )
-
- assert isinstance(result, oauth_module.UserInfo)
- assert result.to_dict() == {
- "email": "[email protected]",
- "username": "user",
- "nickname": "User",
- "avatar_url": "picture-url",
- }
-
-
-def test_get_auth_client_type_inference_and_unsupported(monkeypatch):
- auth_module = _load_auth_init_module(monkeypatch)
-
- class _FakeOAuth2Client:
- def __init__(self, config):
- self.config = config
-
- class _FakeOidcClient:
- def __init__(self, config):
- self.config = config
-
- class _FakeGithubClient:
- def __init__(self, config):
- self.config = config
-
- monkeypatch.setattr(
- auth_module,
- "CLIENT_TYPES",
- {
- "oauth2": _FakeOAuth2Client,
- "oidc": _FakeOidcClient,
- "github": _FakeGithubClient,
- },
- )
-
- oidc_client = auth_module.get_auth_client({"issuer": "https://issuer.example"})
- assert isinstance(oidc_client, _FakeOidcClient)
-
- oauth_client = auth_module.get_auth_client({})
- assert isinstance(oauth_client, _FakeOAuth2Client)
-
- with pytest.raises(ValueError, match="Unsupported type: invalid"):
- auth_module.get_auth_client({"type": "invalid"})
-
-
-def test_github_oauth_client_init_and_normalize_unit(monkeypatch):
- github_module = _load_github_module(monkeypatch)
-
- client = github_module.GithubOAuthClient(_base_config())
- assert client.authorization_url == "https://github.com/login/oauth/authorize"
- assert client.token_url == "https://github.com/login/oauth/access_token"
- assert client.userinfo_url == "https://api.github.com/user"
- assert client.scope == "user:email"
-
- normalized = client.normalize_user_info(
- {
- "email": "[email protected]",
- "login": "octocat",
- "name": "Octo Cat",
- "avatar_url": "https://avatar.example/octocat.png",
- }
- )
- assert normalized.to_dict() == {
- "email": "[email protected]",
- "username": "octocat",
- "nickname": "Octo Cat",
- "avatar_url": "https://avatar.example/octocat.png",
- }
-
- normalized_fallback = client.normalize_user_info({"email": "[email protected]"})
- assert normalized_fallback.to_dict() == {
- "email": "[email protected]",
- "username": "fallback",
- "nickname": "fallback",
- "avatar_url": "",
- }
-
-
-def test_github_fetch_user_info_sync_success_and_error_unit(monkeypatch):
- github_module = _load_github_module(monkeypatch)
- client = github_module.GithubOAuthClient(_base_config())
-
- calls = []
-
- def _fake_sync_request(method, url, headers=None, timeout=None):
- calls.append((method, url, headers, timeout))
- if url.endswith("/emails"):
- return _FakeResponse(
- [
- {"email": "[email protected]", "primary": False},
- {"email": "[email protected]", "primary": True},
- ]
- )
- return _FakeResponse({"login": "octocat", "name": "Octo Cat", "avatar_url": "https://avatar.example/octocat.png"})
-
- monkeypatch.setattr(github_module, "sync_request", _fake_sync_request)
- info = client.fetch_user_info("sync-token")
-
- assert info.to_dict() == {
- "email": "[email protected]",
- "username": "octocat",
- "nickname": "Octo Cat",
- "avatar_url": "https://avatar.example/octocat.png",
- }
- assert [call[1] for call in calls] == [
- "https://api.github.com/user",
- "https://api.github.com/user/emails",
- ]
- assert all(call[2]["Authorization"] == "Bearer sync-token" for call in calls)
- assert all(call[3] == 7 for call in calls)
-
- def _sync_request_raises(*_args, **_kwargs):
- return _FakeResponse(err=RuntimeError("status boom"))
-
- monkeypatch.setattr(github_module, "sync_request", _sync_request_raises)
- with pytest.raises(ValueError, match="Failed to fetch github user info: status boom"):
- client.fetch_user_info("sync-token")
-
-
-def test_github_fetch_user_info_async_success_and_error_unit(monkeypatch):
- github_module = _load_github_module(monkeypatch)
- client = github_module.GithubOAuthClient(_base_config())
-
- calls = []
-
- async def _fake_async_request(method, url, headers=None, **kwargs):
- calls.append((method, url, headers, kwargs.get("timeout")))
- if url.endswith("/emails"):
- return _FakeResponse(
- [
- {"email": "[email protected]", "primary": False},
- {"email": "[email protected]", "primary": True},
- ]
- )
- return _FakeResponse({"login": "octocat-async", "name": "Octo Async", "avatar_url": "https://avatar.example/octo-async.png"})
-
- monkeypatch.setattr(github_module, "async_request", _fake_async_request)
- info = asyncio.run(client.async_fetch_user_info("async-token"))
-
- assert info.to_dict() == {
- "email": "[email protected]",
- "username": "octocat-async",
- "nickname": "Octo Async",
- "avatar_url": "https://avatar.example/octo-async.png",
- }
- assert [call[1] for call in calls] == [
- "https://api.github.com/user",
- "https://api.github.com/user/emails",
- ]
- assert all(call[2]["Authorization"] == "Bearer async-token" for call in calls)
- assert all(call[3] == 7 for call in calls)
-
- async def _async_request_raises(*_args, **_kwargs):
- return _FakeResponse(err=RuntimeError("async status boom"))
-
- monkeypatch.setattr(github_module, "async_request", _async_request_raises)
- with pytest.raises(ValueError, match="Failed to fetch github user info: async status boom"):
- asyncio.run(client.async_fetch_user_info("async-token"))
diff --git a/test/testcases/test_web_api/test_canvas_app/test_code_exec_contract_unit.py b/test/testcases/test_web_api/test_canvas_app/test_code_exec_contract_unit.py
deleted file mode 100644
index d783196768..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_code_exec_contract_unit.py
+++ /dev/null
@@ -1,444 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import importlib.util
-import sys
-import types
-from pathlib import Path
-from unittest.mock import patch
-
-import pytest
-
-
-CODE_EXEC_MODULE_PATH = next(parent / "agent" / "tools" / "code_exec.py" for parent in Path(__file__).resolve().parents if (parent / "agent" / "tools" / "code_exec.py").exists())
-
-
-def _load_module():
- return _load_code_exec_runtime_module()
-
-
-def _build_code_exec(output_type: str):
- return _build_code_exec_with_outputs({"result": {"value": None, "type": output_type}})
-
-
-def _build_code_exec_with_outputs(outputs: dict[str, dict]):
- module = _load_module()
- tool = module.CodeExec.__new__(module.CodeExec)
- tool._param = types.SimpleNamespace(outputs=outputs)
- tool._canvas = types.SimpleNamespace(get_tenant_id=lambda: "tenant-1")
- return tool
-
-
-def _load_code_exec_runtime_module():
- agent_module = types.ModuleType("agent")
- tools_module = types.ModuleType("agent.tools")
- base_module = types.ModuleType("agent.tools.base")
-
- class _FakeToolParamBase:
- def __init__(self):
- self.outputs = {}
-
- class _FakeToolBase:
- def output(self, var_nm=None):
- if var_nm:
- return self._param.outputs.get(var_nm, {}).get("value", "")
- return {k: v.get("value") for k, v in self._param.outputs.items()}
-
- def set_output(self, key, value):
- if key not in self._param.outputs:
- self._param.outputs[key] = {"value": None, "type": str(type(value))}
- self._param.outputs[key]["value"] = value
-
- def check_if_canceled(self, *_args, **_kwargs):
- return False
-
- base_module.ToolBase = _FakeToolBase
- base_module.ToolMeta = dict
- base_module.ToolParamBase = _FakeToolParamBase
-
- api_module = types.ModuleType("api")
- api_db_module = types.ModuleType("api.db")
- api_db_services_module = types.ModuleType("api.db.services")
- file_service_module = types.ModuleType("api.db.services.file_service")
-
- class _FakeFileService:
- @staticmethod
- def parse(*_args, **_kwargs):
- return ""
-
- file_service_module.FileService = _FakeFileService
-
- common_module = types.ModuleType("common")
- common_settings_module = types.ModuleType("common.settings")
- common_settings_module.SANDBOX_HOST = "sandbox"
- common_settings_module.STORAGE_IMPL = types.SimpleNamespace(put=lambda *_args, **_kwargs: None)
-
- connection_utils_module = types.ModuleType("common.connection_utils")
-
- def _timeout(_seconds):
- def _decorator(func):
- return func
-
- return _decorator
-
- connection_utils_module.timeout = _timeout
-
- constants_module = types.ModuleType("common.constants")
- constants_module.SANDBOX_ARTIFACT_BUCKET = "bucket"
- constants_module.SANDBOX_ARTIFACT_EXPIRE_DAYS = 7
-
- agent_module.tools = tools_module
- tools_module.base = base_module
- api_module.db = api_db_module
- api_db_module.services = api_db_services_module
- api_db_services_module.file_service = file_service_module
- common_module.settings = common_settings_module
-
- stub_modules = {
- "agent": agent_module,
- "agent.tools": tools_module,
- "agent.tools.base": base_module,
- "api": api_module,
- "api.db": api_db_module,
- "api.db.services": api_db_services_module,
- "api.db.services.file_service": file_service_module,
- "common": common_module,
- "common.settings": common_settings_module,
- "common.connection_utils": connection_utils_module,
- "common.constants": constants_module,
- }
-
- spec = importlib.util.spec_from_file_location("code_exec_runtime", CODE_EXEC_MODULE_PATH)
- module = importlib.util.module_from_spec(spec)
- assert spec.loader is not None
- with patch.dict(sys.modules, stub_modules):
- spec.loader.exec_module(module)
- return module
-
-
-def test_select_business_output_ignores_system_outputs():
- module = _load_module()
- outputs = {
- "content": {"value": "", "type": "string"},
- "actual_type": {"value": "", "type": "string"},
- "_ERROR": {"value": "", "type": "string"},
- "_ARTIFACTS": {"value": [], "type": "Array<Object>"},
- "attachments": {"value": [], "type": "Array<String>"},
- "raw_result": {"value": None, "type": "Any"},
- "_created_time": {"value": 1.0, "type": "Number"},
- "_elapsed_time": {"value": 2.0, "type": "Number"},
- "result": {"value": None, "type": "Array<Number>"},
- }
-
- name, meta = module.select_business_output(outputs)
-
- assert name == "result"
- assert meta["type"] == "Array<Number>"
-
-
-def test_array_result_is_preserved_as_single_business_value():
- module = _load_module()
- contract = module.build_code_exec_contract(
- {"result": {"value": None, "type": "Array<Number>"}},
- (1, 2, 3),
- )
-
- assert contract["business_output"] == "result"
- assert contract["value"] == [1, 2, 3]
- assert contract["actual_type"] == "Array<Number>"
- assert contract["content"] == "[\n 1,\n 2,\n 3\n]"
-
-
-def test_object_result_is_not_wrapped_by_business_name():
- module = _load_module()
- contract = module.build_code_exec_contract(
- {"result": {"value": None, "type": "Object"}},
- {"foo": "bar", "n": 1},
- )
-
- assert contract["business_output"] == "result"
- assert contract["value"] == {"foo": "bar", "n": 1}
- assert contract["content"] == '{\n "foo": "bar",\n "n": 1\n}'
-
-
-def test_canonical_object_rendering_is_key_order_stable():
- module = _load_module()
- assert module.render_canonical_content({"b": 1, "a": 2}) == '{\n "a": 2,\n "b": 1\n}'
-
-
-def test_lowercase_object_expected_type_validates():
- module = _load_module()
- contract = module.build_code_exec_contract(
- {"result": {"value": None, "type": "object"}},
- {"foo": "bar"},
- )
-
- assert contract["actual_type"] == "Object"
- assert contract["value"] == {"foo": "bar"}
-
-
-def test_tuple_is_normalized_to_array_semantics():
- module = _load_module()
- assert module.normalize_output_value((1, 2, 3)) == [1, 2, 3]
- assert module.infer_actual_type((1, 2, 3)) == "Array<Number>"
-
-
-def test_list_is_preserved_as_list_without_normalization_changes():
- module = _load_module()
- values = [1, 2, 3]
- normalized = module.normalize_output_value(values)
- assert normalized == [1, 2, 3]
- assert isinstance(normalized, list)
-
-
-def test_canonical_content_rendering_handles_common_shapes():
- module = _load_module()
- assert module.render_canonical_content("hello") == "hello"
- assert module.render_canonical_content(None) == ""
- assert module.render_canonical_content(1.5) == "1.5"
- assert module.render_canonical_content({"x": [1, 2]}) == '{\n "x": [\n 1,\n 2\n ]\n}'
-
-
-def test_any_does_not_allow_unsupported_top_level_python_types():
- module = _load_module()
- with pytest.raises(module.ContractError, match="unsupported top-level result type"):
- module.build_code_exec_contract(
- {"result": {"value": None, "type": "Any"}},
- {1, 2},
- )
-
-
-def test_mismatch_raises_contract_error():
- module = _load_module()
- with pytest.raises(module.ContractError, match="expected type Number"):
- module.build_code_exec_contract({"result": {"value": None, "type": "Number"}}, "not-a-number")
-
-
-def test_array_number_rejects_string_elements_without_coercion():
- module = _load_module()
- with pytest.raises(module.ContractError, match=r"expected type Number, got String"):
- module.build_code_exec_contract({"result": {"value": None, "type": "Array<Number>"}}, ["1", 2])
-
-
-def test_boolean_rejects_string_form_without_coercion():
- module = _load_module()
- with pytest.raises(module.ContractError, match=r"expected type Boolean, got String"):
- module.build_code_exec_contract({"result": {"value": None, "type": "Boolean"}}, "true")
-
-
-def test_lowercase_array_number_expected_type_validates():
- module = _load_module()
- contract = module.build_code_exec_contract(
- {"result": {"value": None, "type": "array<number>"}},
- (1, 2, 3),
- )
-
- assert contract["actual_type"] == "Array<Number>"
- assert contract["value"] == [1, 2, 3]
-
-
-def test_lowercase_array_string_expected_type_validates():
- module = _load_module()
- contract = module.build_code_exec_contract(
- {"result": {"value": None, "type": "array<string>"}},
- ("a", "b"),
- )
-
- assert contract["actual_type"] == "Array<String>"
- assert contract["value"] == ["a", "b"]
-
-
[email protected]("schema", ["Array<>", "Array< >", "array<>", "array< >"])
-def test_malformed_array_schema_is_rejected(schema):
- module = _load_module()
- with pytest.raises(module.ContractError, match="Unsupported expected type"):
- module.build_code_exec_contract({"result": {"value": None, "type": schema}}, [1, 2])
-
-
-def test_any_and_empty_expected_type_skip_validation():
- module = _load_module()
- assert module.build_code_exec_contract({"result": {"value": None, "type": "Any"}}, {"foo": "bar"})["value"] == {"foo": "bar"}
- assert module.build_code_exec_contract({"result": {"value": None, "type": ""}}, {"foo": "bar"})["value"] == {"foo": "bar"}
- assert module.build_code_exec_contract({"result": {"value": None, "type": None}}, {"foo": "bar"})["value"] == {"foo": "bar"}
-
-
-def test_legacy_multi_output_schema_is_rejected():
- module = _load_module()
- with pytest.raises(module.ContractError, match="exactly one business output"):
- module.select_business_output(
- {
- "result": {"value": None, "type": "Number"},
- "answer": {"value": None, "type": "String"},
- "_ERROR": {"value": "", "type": "string"},
- }
- )
-
-
[email protected]("name", ["content", "actual_type", "attachments", "_ERROR", "_ARTIFACTS", "raw_result"])
-def test_reserved_business_output_names_are_rejected(name):
- module = _load_module()
- with pytest.raises(module.ContractError, match="reserved output name"):
- module.build_code_exec_contract(
- {name: {"value": None, "type": "String"}},
- "ok",
- )
-
-
-def test_dotted_business_output_name_is_rejected():
- module = _load_module()
- with pytest.raises(module.ContractError, match=r"must not contain '.'"):
- module.build_code_exec_contract(
- {"payload.items": {"value": None, "type": "Array<String>"}},
- ["a"],
- )
-
-
-def test_process_execution_result_preserves_whole_array_for_single_business_output():
- tool = _build_code_exec("Array<String>")
-
- result = tool._process_execution_result('["a", "b"]', None, "unit-test")
-
- assert result["result"] == ["a", "b"]
- assert result["content"] == '[\n "a",\n "b"\n]'
- assert result["raw_result"] == ["a", "b"]
-
-
-def test_process_execution_result_sets_actual_type_from_contract_value():
- tool = _build_code_exec("Object")
-
- result = tool._process_execution_result('{"foo": "bar"}', None, "unit-test")
-
- assert result["result"] == {"foo": "bar"}
- assert result["actual_type"] == "Object"
-
-
-def test_process_execution_result_contract_mismatch_sets_error_and_clears_business_output():
- tool = _build_code_exec("Number")
-
- result = tool._process_execution_result('["a", "b"]', None, "unit-test")
-
- assert "expected type Number" in result["_ERROR"]
- assert result["result"] is None
- assert result["actual_type"] == "Array<String>"
- assert result["raw_result"] == ["a", "b"]
-
-
-def test_process_execution_result_invalid_schema_clears_stale_business_outputs():
- tool = _build_code_exec_with_outputs(
- {
- "result": {"value": "stale-result", "type": "String"},
- "answer": {"value": {"stale": True}, "type": "Object"},
- "_ERROR": {"value": "", "type": "string"},
- }
- )
-
- result = tool._process_execution_result('["a", "b"]', None, "unit-test")
-
- assert "exactly one business output" in result["_ERROR"]
- assert result["result"] is None
- assert result["answer"] is None
- assert result["actual_type"] == "Array<String>"
- assert result["raw_result"] == ["a", "b"]
-
-
-def test_process_execution_result_keeps_business_output_when_stderr_is_non_fatal():
- tool = _build_code_exec("Object")
-
- result = tool._process_execution_result('{"foo": "bar"}', "warning on stderr", "unit-test")
-
- assert result["_ERROR"] == ""
- assert result["result"] == {"foo": "bar"}
- assert result["content"] == '{\n "foo": "bar"\n}'
-
-
-def test_process_execution_result_returns_early_for_stderr_only_without_artifacts():
- tool = _build_code_exec("String")
-
- result = tool._process_execution_result("", "hard failure", "unit-test")
-
- assert result["_ERROR"] == "hard failure"
- assert result.get("result") is None
- assert result.get("content") is None
-
-
-def test_process_execution_result_appends_artifact_content_to_canonical_content():
- tool = _build_code_exec("Object")
- tool._upload_artifacts = lambda _artifacts: [{"name": "chart.png", "url": "/artifact/chart.png", "mime_type": "image/png", "size": 12}]
-
- result = tool._process_execution_result(
- '{"foo": "bar"}',
- None,
- "unit-test",
- artifacts=[{"name": "chart.png", "content_b64": "ZmFrZQ==", "mime_type": "image/png", "size": 12}],
- )
-
- assert result["result"] == {"foo": "bar"}
- assert result["content"] == '{\n "foo": "bar"\n}\n\nattachment_count: 1\n\nattachment1 (image): chart.png\nparsed artifact'
- assert result["_ARTIFACTS"] == [{"name": "chart.png", "url": "/artifact/chart.png", "mime_type": "image/png", "size": 12}]
- assert result["_ARTIFACTS"][0]["mime_type"] == "image/png"
- assert result["attachments"] == ["![chart.png](/artifact/chart.png)"]
-
-
-def test_process_execution_result_without_artifacts_clears_stale_artifacts_output():
- tool = _build_code_exec_with_outputs(
- {
- "result": {"value": None, "type": "String"},
- "_ARTIFACTS": {"value": [{"name": "stale"}], "type": "Array<Object>"},
- }
- )
-
- result = tool._process_execution_result('"ok"', None, "unit-test")
-
- assert result["result"] == "ok"
- assert result["_ARTIFACTS"] is None
-
-
-def test_process_execution_result_prefers_structured_result_metadata_over_stdout_guessing():
- tool = _build_code_exec("Object")
-
- result = tool._process_execution_result(
- '{"fake":"stdout-log"}',
- None,
- "unit-test",
- execution_metadata={
- "result_present": True,
- "result_value": {"real": "value"},
- "result_type": "json",
- },
- )
-
- assert result["result"] == {"real": "value"}
- assert result["actual_type"] == "Object"
- assert result["content"] == '{\n "real": "value"\n}'
-
-
-def test_process_execution_result_preserves_json_looking_string_when_metadata_marks_string():
- tool = _build_code_exec("String")
-
- result = tool._process_execution_result(
- '{"a":1}',
- None,
- "unit-test",
- execution_metadata={
- "result_present": True,
- "result_value": '{"a":1}',
- "result_type": "json",
- },
- )
-
- assert result["result"] == '{"a":1}'
- assert result["actual_type"] == "String"
- assert result["content"] == '{"a":1}'
diff --git a/test/testcases/test_web_api/test_canvas_app/test_fillup_unit.py b/test/testcases/test_web_api/test_canvas_app/test_fillup_unit.py
deleted file mode 100644
index 9b96486363..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_fillup_unit.py
+++ /dev/null
@@ -1,233 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-def _load_fillup_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = [str(repo_root / "agent")]
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
-
- component_pkg = ModuleType("agent.component")
- component_pkg.__path__ = [str(repo_root / "agent" / "component")]
- monkeypatch.setitem(sys.modules, "agent.component", component_pkg)
-
- base_mod = ModuleType("agent.component.base")
-
- class _ComponentParamBase:
- def __init__(self):
- self.inputs = {}
- self.outputs = {}
-
- class _ComponentBase:
- def get_input_elements(self):
- return self._param.inputs
-
- def check_if_canceled(self, *_args, **_kwargs):
- return False
-
- def get_input_elements_from_text(self, *_args, **_kwargs):
- return {}
-
- def set_output(self, key, value):
- if key not in self._param.outputs:
- self._param.outputs[key] = {"value": None}
- self._param.outputs[key]["value"] = value
-
- def set_input_value(self, key, value):
- if key not in self._param.inputs:
- self._param.inputs[key] = {"value": None}
- self._param.inputs[key]["value"] = value
-
- base_mod.ComponentBase = _ComponentBase
- base_mod.ComponentParamBase = _ComponentParamBase
- monkeypatch.setitem(sys.modules, "agent.component.base", base_mod)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = [str(repo_root / "api" / "db" / "services")]
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- file_service_mod = ModuleType("api.db.services.file_service")
-
- class _FileService:
- @staticmethod
- def get_files(files, layout_recognize=None):
- return {"files": files, "layout_recognize": layout_recognize}
-
- file_service_mod.FileService = _FileService
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
-
- module_path = repo_root / "agent" / "component" / "fillup.py"
- spec = importlib.util.spec_from_file_location("test_fillup_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- monkeypatch.setitem(sys.modules, "test_fillup_unit_module", module)
- spec.loader.exec_module(module)
- return module
-
-
-def _make_fillup(module, *, query, inputs):
- component = module.UserFillUp.__new__(module.UserFillUp)
- component._canvas = SimpleNamespace(
- globals={
- "sys.query": query,
- "sys.__initial_user_input_consumed__": False,
- }
- )
- component._param = SimpleNamespace(
- enable_tips=False,
- tips="",
- layout_recognize="",
- inputs=inputs,
- outputs={},
- )
- return component
-
-
-def test_user_fillup_auto_consumes_initial_query_for_single_field(monkeypatch):
- module = _load_fillup_module(monkeypatch)
- component = _make_fillup(
- module,
- query="code",
- inputs={"demo": {"type": "options", "name": "Demo"}},
- )
-
- component._invoke(inputs={})
-
- assert component._param.inputs["demo"]["value"] == "code"
- assert component._param.outputs["demo"]["value"] == "code"
- assert component._canvas.globals["sys.__initial_user_input_consumed__"] is True
-
-
-def test_user_fillup_only_auto_consumes_initial_query_once(monkeypatch):
- module = _load_fillup_module(monkeypatch)
- component = _make_fillup(
- module,
- query="code",
- inputs={"demo": {"type": "options", "name": "Demo"}},
- )
-
- component._invoke(inputs={})
- component._param.outputs = {}
- component._invoke(inputs={})
-
- assert component._param.outputs == {}
-
-
-def test_user_fillup_does_not_consume_unmatched_structured_query(monkeypatch):
- module = _load_fillup_module(monkeypatch)
- component = _make_fillup(
- module,
- query={"x": 8},
- inputs={"demo": {"type": "options", "name": "Demo"}},
- )
-
- component._invoke(inputs={})
-
- assert component._param.outputs == {}
- assert component._canvas.globals["sys.__initial_user_input_consumed__"] is False
-
-
-def test_user_fillup_clears_stale_values_on_reentry_without_answer(monkeypatch):
- # A fresh entry with no user answer (e.g. a new Loop iteration) must clear
- # any value retained from a previous response so the form is treated as
- # unsatisfied and the workflow pauses for input again.
- module = _load_fillup_module(monkeypatch)
- component = _make_fillup(
- module,
- query="",
- inputs={"demo": {"type": "options", "name": "Demo", "value": "previous answer"}},
- )
-
- component._invoke(inputs={})
-
- assert component._param.inputs["demo"]["value"] is None
-
-
-def test_user_fillup_keeps_values_when_answer_supplied(monkeypatch):
- # When the user actually answers, the supplied value must be applied and
- # not cleared.
- module = _load_fillup_module(monkeypatch)
- component = _make_fillup(
- module,
- query="",
- inputs={"demo": {"type": "options", "name": "Demo", "value": "previous answer"}},
- )
-
- component._invoke(inputs={"demo": {"value": "new answer"}})
-
- assert component._param.inputs["demo"]["value"] == "new answer"
- assert component._param.outputs["demo"]["value"] == "new answer"
-
-
-def test_user_fillup_reentry_keeps_optional_file_value(monkeypatch):
- # An optional file input is already treated as satisfied when empty, so
- # clearing it would not force a re-prompt and would only drop a previously
- # uploaded file. It must be preserved on re-entry.
- module = _load_fillup_module(monkeypatch)
- component = _make_fillup(
- module,
- query="",
- inputs={
- "text": {"type": "line", "name": "Text", "value": "previous answer"},
- "doc": {"type": "file", "name": "Doc", "optional": True, "value": ["file-id"]},
- },
- )
-
- component._invoke(inputs={})
-
- assert component._param.inputs["text"]["value"] is None
- assert component._param.inputs["doc"]["value"] == ["file-id"]
-
-
-def test_user_fillup_tips_substitution_is_literal_for_regex_like_values(monkeypatch):
- # A form value containing a regex backreference-like sequence (e.g. a Windows
- # path such as "C:\\1files") must be inserted into the tips template verbatim.
- # It must not raise "invalid group reference" nor be reinterpreted as a regex
- # replacement group.
- module = _load_fillup_module(monkeypatch)
-
- component = module.UserFillUp.__new__(module.UserFillUp)
- component._param = SimpleNamespace(
- enable_tips=True,
- tips="Path is {path}",
- layout_recognize="",
- inputs={},
- outputs={},
- )
- component.get_input_elements_from_text = lambda _text: {"path": {"value": r"C:\1files"}}
-
- component._invoke(inputs={})
-
- assert component._param.outputs["tips"]["value"] == r"Path is C:\1files"
diff --git a/test/testcases/test_web_api/test_canvas_app/test_invoke_component_unit.py b/test/testcases/test_web_api/test_canvas_app/test_invoke_component_unit.py
deleted file mode 100644
index 5823eae21c..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_invoke_component_unit.py
+++ /dev/null
@@ -1,382 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-"""
-Unit tests for the Invoke component's header variable interpolation.
-
-These tests exercise the real Invoke._invoke method, verifying that
-{variable} placeholders in HTTP header values are resolved via canvas
-variable lookup (issue #13277).
-"""
-
-import importlib.util
-import json
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-from unittest.mock import MagicMock
-
-import pytest
-
-
-def _load_invoke_module(monkeypatch):
- """Load the real Invoke class with monkeypatched stubs that are
- automatically cleaned up after each test."""
- repo_root = Path(__file__).resolve().parents[4]
-
- # -- lightweight stubs (auto-restored by monkeypatch) --------------------
-
- quart = ModuleType("quart")
- quart.make_response = lambda *a, **kw: None
- quart.jsonify = lambda *a, **kw: None
- monkeypatch.setitem(sys.modules, "quart", quart)
-
- pd = ModuleType("pandas")
- pd.DataFrame = type("DataFrame", (), {})
- monkeypatch.setitem(sys.modules, "pandas", pd)
-
- deepdoc = ModuleType("deepdoc")
- deepdoc.__path__ = []
- monkeypatch.setitem(sys.modules, "deepdoc", deepdoc)
- deepdoc_parser = ModuleType("deepdoc.parser")
- deepdoc_parser.HtmlParser = MagicMock
- monkeypatch.setitem(sys.modules, "deepdoc.parser", deepdoc_parser)
- monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))
-
- # -- common package and submodules ---------------------------------------
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- constants = ModuleType("common.constants")
-
- class _RetCode:
- SUCCESS = 0
- EXCEPTION_ERROR = 100
-
- constants.RetCode = _RetCode
- monkeypatch.setitem(sys.modules, "common.constants", constants)
-
- conn_spec = importlib.util.spec_from_file_location("common.connection_utils", repo_root / "common" / "connection_utils.py")
- conn_mod = importlib.util.module_from_spec(conn_spec)
- monkeypatch.setitem(sys.modules, "common.connection_utils", conn_mod)
- conn_spec.loader.exec_module(conn_mod)
-
- misc_spec = importlib.util.spec_from_file_location("common.misc_utils", repo_root / "common" / "misc_utils.py")
- misc_mod = importlib.util.module_from_spec(misc_spec)
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_mod)
- misc_spec.loader.exec_module(misc_mod)
-
- # -- agent package (bare stubs to skip __init__ auto-import) -------------
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = [str(repo_root / "agent")]
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
-
- agent_settings = ModuleType("agent.settings")
- agent_settings.FLOAT_ZERO = 1e-8
- agent_settings.PARAM_MAXDEPTH = 5
- monkeypatch.setitem(sys.modules, "agent.settings", agent_settings)
-
- component_pkg = ModuleType("agent.component")
- component_pkg.__path__ = [str(repo_root / "agent" / "component")]
- monkeypatch.setitem(sys.modules, "agent.component", component_pkg)
-
- # -- load the real base.py and invoke.py ---------------------------------
-
- base_spec = importlib.util.spec_from_file_location("agent.component.base", repo_root / "agent" / "component" / "base.py")
- base_mod = importlib.util.module_from_spec(base_spec)
- monkeypatch.setitem(sys.modules, "agent.component.base", base_mod)
- base_spec.loader.exec_module(base_mod)
-
- invoke_spec = importlib.util.spec_from_file_location("agent.component.invoke", repo_root / "agent" / "component" / "invoke.py")
- invoke_mod = importlib.util.module_from_spec(invoke_spec)
- monkeypatch.setitem(sys.modules, "agent.component.invoke", invoke_mod)
- invoke_spec.loader.exec_module(invoke_mod)
-
- return invoke_mod
-
-
-def _make_invoke(module, *, url="http://example.com", method="get", headers="", variables=None, proxy="", timeout_sec=60, clean_html=False, datatype="json", variable_values=None):
- """Build an Invoke instance with a mocked canvas."""
- variable_values = variable_values or {}
-
- canvas = MagicMock()
- canvas.get_variable_value = MagicMock(side_effect=lambda k: variable_values.get(k, ""))
- canvas.is_canceled = MagicMock(return_value=False)
-
- param = module.InvokeParam.__new__(module.InvokeParam)
- param.url = url
- param.method = method
- param.headers = headers
- param.variables = variables or []
- param.proxy = proxy
- param.timeout = timeout_sec
- param.clean_html = clean_html
- param.datatype = datatype
- param.max_retries = 0
- param.delay_after_error = 0
- param.outputs = {}
- param.inputs = {}
-
- inst = module.Invoke.__new__(module.Invoke)
- inst._canvas = canvas
- inst._param = param
- inst._id = "invoke_test"
-
- return inst
-
-
-def test_header_single_variable(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- headers=json.dumps({"Authorization": "Bearer {auth_token}"}),
- variable_values={"auth_token": "secret123"},
- )
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- assert mock_get.call_args[1]["headers"]["Authorization"] == "Bearer secret123"
-
-
-def test_header_multiple_variables(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- headers=json.dumps(
- {
- "Authorization": "Bearer {token}",
- "X-Request-Id": "{req_id}",
- "Content-Type": "application/json",
- }
- ),
- variable_values={"token": "tok_abc", "req_id": "id-42"},
- )
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- h = mock_get.call_args[1]["headers"]
- assert h["Authorization"] == "Bearer tok_abc"
- assert h["X-Request-Id"] == "id-42"
- assert h["Content-Type"] == "application/json"
-
-
-def test_header_no_variables_unchanged(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- headers=json.dumps({"Content-Type": "application/json"}),
- )
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- assert mock_get.call_args[1]["headers"]["Content-Type"] == "application/json"
-
-
-def test_header_empty(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, headers="")
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- assert mock_get.call_args[1]["headers"] == {}
-
-
[email protected]("headers", ['{"Authorization":"Bearer secret-token"', '["secret-token"]', "null"])
-def test_header_invalid_input_is_ignored_without_logging_raw_value(monkeypatch, caplog, headers):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, headers=headers)
-
- with caplog.at_level("WARNING"):
- assert invoke._build_headers({}) == {}
-
- assert "secret-token" not in caplog.text
-
-
-def test_header_dict_is_accepted_and_interpolated(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- headers={"Authorization": "Bearer {token}"},
- variable_values={"token": "secret-token"},
- )
-
- assert invoke._build_headers({}) == {"Authorization": "Bearer secret-token"}
-
-
-def test_header_unsupported_type_is_ignored(monkeypatch, caplog):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, headers=object())
-
- with caplog.at_level("WARNING"):
- assert invoke._build_headers({}) == {}
-
- assert "object" in caplog.text
-
-
-def test_header_component_ref_variable(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- headers=json.dumps({"Authorization": "Bearer {begin@token}"}),
- variable_values={"begin@token": "my_token"},
- )
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- assert mock_get.call_args[1]["headers"]["Authorization"] == "Bearer my_token"
-
-
-def test_header_env_variable(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- headers=json.dumps({"Authorization": "Bearer {env.api_key}"}),
- variable_values={"env.api_key": "env_secret"},
- )
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- assert mock_get.call_args[1]["headers"]["Authorization"] == "Bearer env_secret"
-
-
-def test_header_missing_variable_becomes_empty(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- headers=json.dumps({"Authorization": "Bearer {nonexistent}"}),
- variable_values={},
- )
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- assert mock_get.call_args[1]["headers"]["Authorization"] == "Bearer "
-
-
-def test_header_variable_with_post(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- method="post",
- headers=json.dumps({"Authorization": "Bearer {token}"}),
- variable_values={"token": "post_token"},
- )
- mock_post = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "post", mock_post)
- invoke._invoke()
- assert mock_post.call_args[1]["headers"]["Authorization"] == "Bearer post_token"
-
-
-def test_header_variable_with_put(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(
- module,
- method="put",
- headers=json.dumps({"Authorization": "Bearer {token}"}),
- variable_values={"token": "put_token"},
- )
- mock_put = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "put", mock_put)
- invoke._invoke()
- assert mock_put.call_args[1]["headers"]["Authorization"] == "Bearer put_token"
-
-
-def test_invoke_blocks_loopback_url_with_ssrf_guard(monkeypatch):
- """Invoke must use the shared SSRF guard before requests.* (issue Invoke SSRF)."""
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, url="http://127.0.0.1:8123/api")
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- result = invoke._invoke()
- mock_get.assert_not_called()
- assert result == "Http request error: URL not valid"
- assert invoke.output("_ERROR") == "URL not valid"
-
-
-def test_invoke_blocks_metadata_ip(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, url="http://169.254.169.254/latest/meta-data/")
- mock_get = MagicMock(return_value=SimpleNamespace(text="should not run"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- result = invoke._invoke()
- mock_get.assert_not_called()
- assert "URL not valid" in result
- assert invoke.output("_ERROR") == "URL not valid"
-
-
-def test_invoke_url_without_scheme_gets_scheme_then_validated(monkeypatch):
- """Bare hostnames are prefixed with http:// before SSRF validation."""
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, url="127.0.0.1:9380/")
- mock_get = MagicMock(return_value=SimpleNamespace(text="should not run"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- result = invoke._invoke()
- mock_get.assert_not_called()
- assert "URL not valid" in result
-
-
-def test_invoke_blocks_loopback_proxy(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, url="http://example.com", proxy="http://127.0.0.1:8080")
- mock_get = MagicMock(return_value=SimpleNamespace(text="should not run"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- result = invoke._invoke()
- mock_get.assert_not_called()
- assert "URL not valid" in result
- assert invoke.output("_ERROR") == "URL not valid"
-
-
-def test_invoke_disables_redirect_following(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, url="http://example.com")
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
- invoke._invoke()
- assert mock_get.call_args[1]["allow_redirects"] is False
-
-
-def test_invoke_pins_dns_and_disables_redirects(monkeypatch):
- module = _load_invoke_module(monkeypatch)
- invoke = _make_invoke(module, url="http://example.com")
- pin_ctx = MagicMock()
- pin_ctx.__enter__ = MagicMock(return_value=None)
- pin_ctx.__exit__ = MagicMock(return_value=None)
- monkeypatch.setattr(module, "assert_url_is_safe", MagicMock(return_value=("example.com", "93.184.216.34")))
- monkeypatch.setattr(module, "pin_dns", MagicMock(return_value=pin_ctx))
- mock_get = MagicMock(return_value=SimpleNamespace(text="ok"))
- monkeypatch.setattr(module.requests, "get", mock_get)
-
- invoke._invoke()
-
- module.pin_dns.assert_called_once_with("example.com", "93.184.216.34")
- assert mock_get.call_args[1]["allow_redirects"] is False
diff --git a/test/testcases/test_web_api/test_canvas_app/test_iteration_runtime_unit.py b/test/testcases/test_web_api/test_canvas_app/test_iteration_runtime_unit.py
deleted file mode 100644
index 4ba1ea0371..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_iteration_runtime_unit.py
+++ /dev/null
@@ -1,507 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import json
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-def _load_canvas_runtime(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart = ModuleType("quart")
- quart.make_response = lambda *a, **kw: None
- quart.jsonify = lambda *a, **kw: None
- monkeypatch.setitem(sys.modules, "quart", quart)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- common_constants = ModuleType("common.constants")
- common_constants.LLMType = SimpleNamespace(TTS="tts")
- monkeypatch.setitem(sys.modules, "common.constants", common_constants)
-
- common_misc = ModuleType("common.misc_utils")
- common_misc.get_uuid = lambda: "uuid"
- common_misc.hash_str2int = lambda x: 1
-
- async def _thread_pool_exec(fn, *args, **kwargs):
- return fn(*args, **kwargs)
-
- common_misc.thread_pool_exec = _thread_pool_exec
- monkeypatch.setitem(sys.modules, "common.misc_utils", common_misc)
-
- common_conn = ModuleType("common.connection_utils")
-
- def timeout(_seconds):
- def decorator(fn):
- return fn
-
- return decorator
-
- common_conn.timeout = timeout
- monkeypatch.setitem(sys.modules, "common.connection_utils", common_conn)
-
- common_ex = ModuleType("common.exceptions")
-
- class TaskCanceledException(Exception):
- pass
-
- common_ex.TaskCanceledException = TaskCanceledException
- monkeypatch.setitem(sys.modules, "common.exceptions", common_ex)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
- api_db_pkg = ModuleType("api.db")
- api_db_pkg.__path__ = [str(repo_root / "api" / "db")]
- monkeypatch.setitem(sys.modules, "api.db", api_db_pkg)
- api_db_services_pkg = ModuleType("api.db.services")
- api_db_services_pkg.__path__ = [str(repo_root / "api" / "db" / "services")]
- monkeypatch.setitem(sys.modules, "api.db.services", api_db_services_pkg)
- api_db_joint_pkg = ModuleType("api.db.joint_services")
- api_db_joint_pkg.__path__ = [str(repo_root / "api" / "db" / "joint_services")]
- monkeypatch.setitem(sys.modules, "api.db.joint_services", api_db_joint_pkg)
-
- file_service = ModuleType("api.db.services.file_service")
- file_service.FileService = object
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service)
-
- llm_service = ModuleType("api.db.services.llm_service")
- llm_service.LLMBundle = object
- llm_service.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service)
-
- task_service = ModuleType("api.db.services.task_service")
- task_service.has_canceled = lambda _task_id: False
- monkeypatch.setitem(sys.modules, "api.db.services.task_service", task_service)
-
- tenant_model_service = ModuleType("api.db.joint_services.tenant_model_service")
- tenant_model_service.get_tenant_default_model_by_type = lambda *_a, **_kw: None
- tenant_model_service.resolve_model_config = lambda *_a, **_kw: None
- monkeypatch.setitem(
- sys.modules,
- "api.db.joint_services.tenant_model_service",
- tenant_model_service,
- )
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = [str(repo_root / "rag")]
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
- rag_prompts_pkg = ModuleType("rag.prompts")
- rag_prompts_pkg.__path__ = [str(repo_root / "rag" / "prompts")]
- monkeypatch.setitem(sys.modules, "rag.prompts", rag_prompts_pkg)
- rag_prompts = ModuleType("rag.prompts.generator")
- rag_prompts.chunks_format = lambda *_a, **_kw: ""
- monkeypatch.setitem(sys.modules, "rag.prompts.generator", rag_prompts)
-
- rag_utils_pkg = ModuleType("rag.utils")
- rag_utils_pkg.__path__ = [str(repo_root / "rag" / "utils")]
- monkeypatch.setitem(sys.modules, "rag.utils", rag_utils_pkg)
- rag_redis = ModuleType("rag.utils.redis_conn")
- rag_redis.REDIS_CONN = SimpleNamespace(delete=lambda *_a, **_kw: None, set=lambda *_a, **_kw: None)
- monkeypatch.setitem(sys.modules, "rag.utils.redis_conn", rag_redis)
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = [str(repo_root / "agent")]
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
-
- agent_settings = ModuleType("agent.settings")
- agent_settings.FLOAT_ZERO = 1e-8
- agent_settings.PARAM_MAXDEPTH = 5
- monkeypatch.setitem(sys.modules, "agent.settings", agent_settings)
-
- dsl_migration = ModuleType("agent.dsl_migration")
- dsl_migration.normalize_chunker_dsl = lambda dsl: dsl
- monkeypatch.setitem(sys.modules, "agent.dsl_migration", dsl_migration)
-
- component_pkg = ModuleType("agent.component")
- component_pkg.__path__ = [str(repo_root / "agent" / "component")]
- monkeypatch.setitem(sys.modules, "agent.component", component_pkg)
-
- base_spec = importlib.util.spec_from_file_location("agent.component.base", repo_root / "agent" / "component" / "base.py")
- base_mod = importlib.util.module_from_spec(base_spec)
- monkeypatch.setitem(sys.modules, "agent.component.base", base_mod)
- base_spec.loader.exec_module(base_mod)
-
- iteration_spec = importlib.util.spec_from_file_location("agent.component.iteration", repo_root / "agent" / "component" / "iteration.py")
- iteration_mod = importlib.util.module_from_spec(iteration_spec)
- monkeypatch.setitem(sys.modules, "agent.component.iteration", iteration_mod)
- iteration_spec.loader.exec_module(iteration_mod)
-
- iterationitem_spec = importlib.util.spec_from_file_location(
- "agent.component.iterationitem",
- repo_root / "agent" / "component" / "iterationitem.py",
- )
- iterationitem_mod = importlib.util.module_from_spec(iterationitem_spec)
- monkeypatch.setitem(sys.modules, "agent.component.iterationitem", iterationitem_mod)
- iterationitem_spec.loader.exec_module(iterationitem_mod)
-
- class BeginParam(base_mod.ComponentParamBase):
- def check(self):
- return True
-
- class Begin(base_mod.ComponentBase):
- component_name = "Begin"
-
- def _invoke(self, **kwargs):
- return
-
- def thoughts(self):
- return "begin"
-
- class ProbeParam(base_mod.ComponentParamBase):
- def __init__(self):
- super().__init__()
- self.query = ""
- self.inputs = {"query": {"value": None}}
-
- def get_input_form(self):
- return {"query": {"name": "Query", "type": "line"}}
-
- def check(self):
- return True
-
- class Probe(base_mod.ComponentBase):
- component_name = "Probe"
-
- def _invoke(self, **kwargs):
- query_text = kwargs.get("query")
- vars_map = self.get_input_elements_from_text(query_text)
- query = self.string_format(query_text, {key: value["value"] for key, value in vars_map.items()})
- calls = self._canvas.globals.setdefault("probe.calls", [])
- calls.append(query)
- self.set_output("result", query)
-
- def thoughts(self):
- return "probe"
-
- class SinkParam(base_mod.ComponentParamBase):
- def check(self):
- return True
-
- class Sink(base_mod.ComponentBase):
- component_name = "Sink"
-
- def _invoke(self, **kwargs):
- self.set_output("done", True)
-
- def thoughts(self):
- return "sink"
-
- class UserFillUpParam(base_mod.ComponentParamBase):
- def __init__(self):
- super().__init__()
- self.enable_tips = True
- self.tips = "Please fill"
- self.inputs = {"value": {"type": "line", "name": "Value"}}
-
- def get_input_form(self):
- return self.inputs
-
- def check(self):
- return True
-
- class UserFillUp(base_mod.ComponentBase):
- component_name = "UserFillUp"
-
- def _invoke(self, **kwargs):
- incoming = kwargs.get("inputs", {})
- if "value" in incoming:
- raw = incoming["value"]
- value = raw.get("value") if isinstance(raw, dict) else raw
- self.set_output("value", value)
- if self._param.enable_tips:
- self.set_output("tips", self._param.tips)
-
- def get_input_elements(self):
- return self._param.inputs
-
- def thoughts(self):
- return "fill"
-
- class MessageParam(base_mod.ComponentParamBase):
- def __init__(self):
- super().__init__()
- self.content = "{UserFillUp:1@value}"
-
- def check(self):
- return True
-
- class Message(base_mod.ComponentBase):
- component_name = "Message"
-
- def _invoke(self, **kwargs):
- self.set_output("content", self.string_format(self._param.content, {}))
-
- def thoughts(self):
- return "message"
-
- class_map = {
- "Begin": Begin,
- "BeginParam": BeginParam,
- "Iteration": iteration_mod.Iteration,
- "IterationParam": iteration_mod.IterationParam,
- "IterationItem": iterationitem_mod.IterationItem,
- "IterationItemParam": iterationitem_mod.IterationItemParam,
- "Probe": Probe,
- "ProbeParam": ProbeParam,
- "Sink": Sink,
- "SinkParam": SinkParam,
- "UserFillUp": UserFillUp,
- "UserFillUpParam": UserFillUpParam,
- "Message": Message,
- "MessageParam": MessageParam,
- }
-
- component_pkg.component_class = lambda name: class_map[name]
-
- canvas_spec = importlib.util.spec_from_file_location("agent.canvas", repo_root / "agent" / "canvas.py")
- canvas_mod = importlib.util.module_from_spec(canvas_spec)
- monkeypatch.setitem(sys.modules, "agent.canvas", canvas_mod)
- canvas_spec.loader.exec_module(canvas_mod)
-
- return canvas_mod
-
-
-async def _collect_events(stream):
- events = []
- async for event in stream:
- events.append(event)
- return events
-
-
-def test_iteration_runtime_processes_all_array_items(monkeypatch):
- canvas_mod = _load_canvas_runtime(monkeypatch)
-
- dsl = {
- "components": {
- "begin": {
- "obj": {"component_name": "Begin", "params": {}},
- "downstream": ["Iteration:1"],
- "upstream": [],
- },
- "Iteration:1": {
- "obj": {
- "component_name": "Iteration",
- "params": {"items_ref": "env.items"},
- },
- "downstream": ["Sink:1"],
- "upstream": ["begin"],
- },
- "IterationItem:1": {
- "obj": {"component_name": "IterationItem", "params": {}},
- "parent_id": "Iteration:1",
- "downstream": ["Probe:1"],
- "upstream": [],
- },
- "Probe:1": {
- "obj": {
- "component_name": "Probe",
- "params": {"query": "IterationItem:1@result"},
- },
- "parent_id": "Iteration:1",
- "downstream": [],
- "upstream": ["IterationItem:1"],
- },
- "Sink:1": {
- "obj": {"component_name": "Sink", "params": {}},
- "downstream": [],
- "upstream": ["Iteration:1"],
- },
- },
- "graph": {
- "nodes": [
- {"id": "begin", "data": {"name": "Begin"}},
- {"id": "Iteration:1", "data": {"name": "Iteration"}},
- {"id": "IterationItem:1", "data": {"name": "IterationItem"}},
- {"id": "Probe:1", "data": {"name": "Probe"}},
- {"id": "Sink:1", "data": {"name": "Sink"}},
- ]
- },
- "history": [],
- "path": [],
- "retrieval": [],
- "globals": {
- "sys.query": "",
- "sys.user_id": "",
- "sys.conversation_turns": 0,
- "sys.files": [],
- "sys.history": [],
- "sys.date": "",
- "env.items": ["a", "b", "c"],
- },
- }
-
- canvas = canvas_mod.Canvas(json.dumps(dsl))
- events = asyncio.run(_collect_events(canvas.run()))
-
- assert canvas.globals["probe.calls"] == ["a", "b", "c"]
- assert any(event["event"] == "workflow_finished" for event in events)
-
-
- ("query", "expected_calls"),
- [
- ("{item}", ["a", "b", "c"]),
- ("{index}", ["0", "1", "2"]),
- ("{result}", ["a", "b", "c"]),
- ],
-)
-def test_iteration_runtime_supports_bare_iteration_aliases(monkeypatch, query, expected_calls):
- canvas_mod = _load_canvas_runtime(monkeypatch)
-
- dsl = {
- "components": {
- "begin": {
- "obj": {"component_name": "Begin", "params": {}},
- "downstream": ["Iteration:1"],
- "upstream": [],
- },
- "Iteration:1": {
- "obj": {
- "component_name": "Iteration",
- "params": {"items_ref": "env.items"},
- },
- "downstream": ["Sink:1"],
- "upstream": ["begin"],
- },
- "IterationItem:1": {
- "obj": {"component_name": "IterationItem", "params": {}},
- "parent_id": "Iteration:1",
- "downstream": ["Probe:1"],
- "upstream": [],
- },
- "Probe:1": {
- "obj": {
- "component_name": "Probe",
- "params": {"query": query},
- },
- "parent_id": "Iteration:1",
- "downstream": [],
- "upstream": ["IterationItem:1"],
- },
- "Sink:1": {
- "obj": {"component_name": "Sink", "params": {}},
- "downstream": [],
- "upstream": ["Iteration:1"],
- },
- },
- "graph": {
- "nodes": [
- {"id": "begin", "data": {"name": "Begin"}},
- {"id": "Iteration:1", "data": {"name": "Iteration"}},
- {"id": "IterationItem:1", "data": {"name": "IterationItem"}},
- {"id": "Probe:1", "data": {"name": "Probe"}},
- {"id": "Sink:1", "data": {"name": "Sink"}},
- ]
- },
- "history": [],
- "path": [],
- "retrieval": [],
- "globals": {
- "sys.query": "",
- "sys.user_id": "",
- "sys.conversation_turns": 0,
- "sys.files": [],
- "sys.history": [],
- "sys.date": "",
- "env.items": ["a", "b", "c"],
- },
- }
-
- canvas = canvas_mod.Canvas(json.dumps(dsl))
- asyncio.run(_collect_events(canvas.run()))
-
- assert canvas.globals["probe.calls"] == expected_calls
-
-
-def test_canvas_resume_does_not_emit_duplicate_workflow_started(monkeypatch):
- canvas_mod = _load_canvas_runtime(monkeypatch)
-
- dsl = {
- "components": {
- "begin": {
- "obj": {"component_name": "Begin", "params": {}},
- "downstream": ["UserFillUp:1"],
- "upstream": [],
- },
- "UserFillUp:1": {
- "obj": {
- "component_name": "UserFillUp",
- "params": {
- "enable_tips": True,
- "tips": "Enter value",
- "inputs": {"value": {"type": "line", "name": "Value"}},
- },
- },
- "downstream": ["Message:1"],
- "upstream": ["begin"],
- },
- "Message:1": {
- "obj": {
- "component_name": "Message",
- "params": {"content": "{UserFillUp:1@value}"},
- },
- "downstream": [],
- "upstream": ["UserFillUp:1"],
- },
- },
- "graph": {
- "nodes": [
- {"id": "begin", "data": {"name": "Begin"}},
- {"id": "UserFillUp:1", "data": {"name": "UserFillUp"}},
- {"id": "Message:1", "data": {"name": "Message"}},
- ]
- },
- "history": [],
- "path": [],
- "retrieval": [],
- "globals": {
- "sys.query": "",
- "sys.user_id": "",
- "sys.conversation_turns": 0,
- "sys.files": [],
- "sys.history": [],
- "sys.date": "",
- },
- }
-
- canvas = canvas_mod.Canvas(json.dumps(dsl))
- first_events = asyncio.run(_collect_events(canvas.run()))
- first_kinds = [event["event"] for event in first_events]
- assert first_kinds == [
- "workflow_started",
- "node_started",
- "node_finished",
- "user_inputs",
- ]
-
- resumed_events = asyncio.run(_collect_events(canvas.run(query="hello", inputs={"value": {"value": "hello"}})))
- resumed_kinds = [event["event"] for event in resumed_events]
- assert resumed_kinds[0] == "node_started"
- assert "workflow_started" not in resumed_kinds
- assert "message" in resumed_kinds
- assert resumed_kinds[-1] == "workflow_finished"
diff --git a/test/testcases/test_web_api/test_canvas_app/test_iterationitem_unit.py b/test/testcases/test_web_api/test_canvas_app/test_iterationitem_unit.py
deleted file mode 100644
index dd732ca344..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_iterationitem_unit.py
+++ /dev/null
@@ -1,140 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-from unittest.mock import MagicMock
-
-import pytest
-
-
-def _load_iterationitem_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart = ModuleType("quart")
- quart.make_response = lambda *a, **kw: None
- quart.jsonify = lambda *a, **kw: None
- monkeypatch.setitem(sys.modules, "quart", quart)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- constants = ModuleType("common.constants")
-
- class _RetCode:
- SUCCESS = 0
- EXCEPTION_ERROR = 100
-
- constants.RetCode = _RetCode
- monkeypatch.setitem(sys.modules, "common.constants", constants)
-
- conn_spec = importlib.util.spec_from_file_location("common.connection_utils", repo_root / "common" / "connection_utils.py")
- conn_mod = importlib.util.module_from_spec(conn_spec)
- monkeypatch.setitem(sys.modules, "common.connection_utils", conn_mod)
- conn_spec.loader.exec_module(conn_mod)
-
- misc_spec = importlib.util.spec_from_file_location("common.misc_utils", repo_root / "common" / "misc_utils.py")
- misc_mod = importlib.util.module_from_spec(misc_spec)
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_mod)
- misc_spec.loader.exec_module(misc_mod)
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = [str(repo_root / "agent")]
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
-
- agent_settings = ModuleType("agent.settings")
- agent_settings.FLOAT_ZERO = 1e-8
- agent_settings.PARAM_MAXDEPTH = 5
- monkeypatch.setitem(sys.modules, "agent.settings", agent_settings)
-
- component_pkg = ModuleType("agent.component")
- component_pkg.__path__ = [str(repo_root / "agent" / "component")]
- monkeypatch.setitem(sys.modules, "agent.component", component_pkg)
-
- canvas_mod = ModuleType("agent.canvas")
-
- class Graph:
- pass
-
- canvas_mod.Graph = Graph
- monkeypatch.setitem(sys.modules, "agent.canvas", canvas_mod)
-
- base_spec = importlib.util.spec_from_file_location("agent.component.base", repo_root / "agent" / "component" / "base.py")
- base_mod = importlib.util.module_from_spec(base_spec)
- monkeypatch.setitem(sys.modules, "agent.component.base", base_mod)
- base_spec.loader.exec_module(base_mod)
-
- iterationitem_spec = importlib.util.spec_from_file_location(
- "agent.component.iterationitem",
- repo_root / "agent" / "component" / "iterationitem.py",
- )
- iterationitem_mod = importlib.util.module_from_spec(iterationitem_spec)
- monkeypatch.setitem(sys.modules, "agent.component.iterationitem", iterationitem_mod)
- iterationitem_spec.loader.exec_module(iterationitem_mod)
-
- return iterationitem_mod
-
-
-def _make_iterationitem(module, values):
- canvas = MagicMock()
- canvas.is_canceled = MagicMock(return_value=False)
- canvas.get_variable_value = MagicMock(return_value=values)
- canvas.components = {}
-
- param = module.IterationItemParam()
- param.outputs = {}
- param.inputs = {}
-
- inst = module.IterationItem.__new__(module.IterationItem)
- inst._canvas = canvas
- inst._id = "IterationItem:test"
- inst._param = param
- inst._idx = 0
- inst.get_parent = MagicMock(
- return_value=SimpleNamespace(
- _id="Iteration:test",
- _param=SimpleNamespace(items_ref="code:1@tempList"),
- component_name="Iteration",
- )
- )
- return inst
-
-
-def test_iterationitem_exposes_result_alias_for_each_item(monkeypatch):
- module = _load_iterationitem_module(monkeypatch)
- item = _make_iterationitem(module, ["a", "b", "c"])
-
- item._invoke()
- assert item.output("item") == "a"
- assert item.output("result") == "a"
- assert item.output("index") == 0
-
- item._invoke()
- assert item.output("item") == "b"
- assert item.output("result") == "b"
- assert item.output("index") == 1
-
- item._invoke()
- assert item.output("item") == "c"
- assert item.output("result") == "c"
- assert item.output("index") == 2
-
- item._invoke()
- assert item.end() is True
diff --git a/test/testcases/test_web_api/test_canvas_app/test_list_operations_unit.py b/test/testcases/test_web_api/test_canvas_app/test_list_operations_unit.py
deleted file mode 100644
index 724b1c6c61..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_list_operations_unit.py
+++ /dev/null
@@ -1,221 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-def _load_list_operations_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = [str(repo_root / "agent")]
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
-
- component_pkg = ModuleType("agent.component")
- component_pkg.__path__ = [str(repo_root / "agent" / "component")]
- monkeypatch.setitem(sys.modules, "agent.component", component_pkg)
-
- base_mod = ModuleType("agent.component.base")
-
- class _ComponentParamBase:
- def __init__(self):
- self.outputs = {}
-
- def check_empty(self, *_args, **_kwargs):
- return None
-
- def check_valid_value(self, *_args, **_kwargs):
- return None
-
- class _ComponentBase:
- def set_input_value(self, *_args, **_kwargs):
- return None
-
- base_mod.ComponentBase = _ComponentBase
- base_mod.ComponentParamBase = _ComponentParamBase
- monkeypatch.setitem(sys.modules, "agent.component.base", base_mod)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.timeout = lambda *_args, **_kwargs: lambda func: func
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- module_path = repo_root / "agent" / "component" / "list_operations.py"
- spec = importlib.util.spec_from_file_location("test_list_operations_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- monkeypatch.setitem(sys.modules, "test_list_operations_unit_module", module)
- spec.loader.exec_module(module)
- return module
-
-
-def _make_component(module, *, inputs, operation, n, strict=False):
- component = module.ListOperations.__new__(module.ListOperations)
- component.inputs = inputs
- component._param = SimpleNamespace(
- n=n,
- strict=strict,
- outputs={
- "result": {"value": []},
- "first": {"value": None},
- "last": {"value": None},
- },
- )
- return component
-
-
- ("n", "expected"),
- [
- (0, []),
- (-1, ["e"]),
- (-5, ["a"]),
- (-6, []),
- (2, ["b"]),
- (5, ["e"]),
- (6, []),
- ],
-)
-def test_nth_behaves_like_lenient_indexing(monkeypatch, n, expected):
- module = _load_list_operations_module(monkeypatch)
- component = _make_component(module, inputs=["a", "b", "c", "d", "e"], operation="nth", n=n)
- component._nth()
- assert component._param.outputs["result"]["value"] == expected
-
-
- ("strict", "n", "expected"),
- [
- (False, 0, []),
- (False, 2, ["a", "b"]),
- (False, 10, ["a", "b", "c", "d", "e"]),
- (True, 2, ["a", "b"]),
- ],
-)
-def test_head_supports_lenient_and_strict(monkeypatch, strict, n, expected):
- module = _load_list_operations_module(monkeypatch)
- component = _make_component(module, inputs=["a", "b", "c", "d", "e"], operation="head", n=n, strict=strict)
- component._head()
- assert component._param.outputs["result"]["value"] == expected
-
-
[email protected]("n", [0, 10])
-def test_head_strict_raises_for_out_of_range(monkeypatch, n):
- module = _load_list_operations_module(monkeypatch)
- component = _make_component(module, inputs=["a", "b", "c", "d", "e"], operation="head", n=n, strict=True)
- with pytest.raises(ValueError, match="head requires n"):
- component._head()
-
-
- ("strict", "n", "expected"),
- [
- (False, 0, []),
- (False, 2, ["d", "e"]),
- (False, 10, ["a", "b", "c", "d", "e"]),
- (True, 2, ["d", "e"]),
- ],
-)
-def test_tail_supports_lenient_and_strict(monkeypatch, strict, n, expected):
- module = _load_list_operations_module(monkeypatch)
- component = _make_component(module, inputs=["a", "b", "c", "d", "e"], operation="tail", n=n, strict=strict)
- component._tail()
- assert component._param.outputs["result"]["value"] == expected
-
-
[email protected]("n", [0, 10])
-def test_tail_strict_raises_for_out_of_range(monkeypatch, n):
- module = _load_list_operations_module(monkeypatch)
- component = _make_component(module, inputs=["a", "b", "c", "d", "e"], operation="tail", n=n, strict=True)
- with pytest.raises(ValueError, match="tail requires n"):
- component._tail()
-
-
[email protected]("n", [0, 6, -6])
-def test_nth_strict_raises_for_out_of_range(monkeypatch, n):
- module = _load_list_operations_module(monkeypatch)
- component = _make_component(module, inputs=["a", "b", "c", "d", "e"], operation="nth", n=n, strict=True)
- with pytest.raises(ValueError, match="nth requires n"):
- component._nth()
-
-
-def test_set_outputs_tracks_first_and_last(monkeypatch):
- module = _load_list_operations_module(monkeypatch)
- component = _make_component(module, inputs=["a", "b", "c", "d", "e"], operation="tail", n=3)
- component._tail()
- assert component._param.outputs["result"]["value"] == ["c", "d", "e"]
- assert component._param.outputs["first"]["value"] == "c"
- assert component._param.outputs["last"]["value"] == "e"
-
-
-def test_topn_operation_alias_normalizes_to_head(monkeypatch):
- module = _load_list_operations_module(monkeypatch)
- param = module.ListOperationsParam()
- param.query = "items"
- param.operations = "topN"
- param.check()
- assert param.operations == "head"
-
-
-def _make_invoke_component(module, *, resolved):
- component = module.ListOperations.__new__(module.ListOperations)
- component._canvas = SimpleNamespace(get_variable_value=lambda _ref: resolved)
- component._param = SimpleNamespace(
- query="cpn_0@result",
- operations="nth",
- n=1,
- strict=False,
- outputs={
- "result": {"value": []},
- "first": {"value": None},
- "last": {"value": None},
- },
- )
- return component
-
-
-def test_invoke_treats_missing_variable_as_empty_list(monkeypatch):
- module = _load_list_operations_module(monkeypatch)
- component = _make_invoke_component(module, resolved=None)
- component._invoke()
- assert component.inputs == []
- assert component._param.outputs["result"]["value"] == []
- assert component._param.outputs["first"]["value"] is None
- assert component._param.outputs["last"]["value"] is None
-
-
-def test_invoke_still_raises_for_non_list_input(monkeypatch):
- module = _load_list_operations_module(monkeypatch)
- component = _make_invoke_component(module, resolved="not-a-list")
- with pytest.raises(TypeError, match="should be an array"):
- component._invoke()
diff --git a/test/testcases/test_web_api/test_canvas_app/test_pubmed_unit.py b/test/testcases/test_web_api/test_canvas_app/test_pubmed_unit.py
deleted file mode 100644
index ed4f8bf274..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_pubmed_unit.py
+++ /dev/null
@@ -1,91 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import xml.etree.ElementTree as ET
-
-import pytest
-
-# PubMed depends on biopython (`Bio`); skip cleanly where it isn't installed.
-pytest.importorskip("Bio")
-
-from agent.tools.pubmed import PubMed # noqa: E402
-
-
-SAMPLE_ARTICLE = """
-<PubmedArticle>
- <MedlineCitation>
- <PMID>12345678</PMID>
- <Article>
- <ArticleTitle>Deep learning for retrieval augmented generation</ArticleTitle>
- <Abstract><AbstractText>A short abstract.</AbstractText></Abstract>
- <Journal>
- <Title>Nature Machine Intelligence</Title>
- <JournalIssue><Volume>10</Volume><Issue>2</Issue></JournalIssue>
- </Journal>
- <Pagination><MedlinePgn>101-110</MedlinePgn></Pagination>
- <AuthorList>
- <Author><LastName>Khan</LastName><ForeName>Furqan</ForeName></Author>
- <Author><LastName>Smith</LastName><ForeName>Jane</ForeName></Author>
- </AuthorList>
- </Article>
- </MedlineCitation>
- <PubmedData>
- <ArticleIdList>
- <ArticleId IdType="doi">10.1000/example.doi</ArticleId>
- </ArticleIdList>
- </PubmedData>
-</PubmedArticle>
-"""
-
-
-def _format(article_xml: str) -> str:
- # _format_pubmed_content only reads its `child` argument, so we can bypass
- # the canvas-bound __init__ and exercise the pure parsing logic directly.
- pm = PubMed.__new__(PubMed)
- return pm._format_pubmed_content(ET.fromstring(article_xml))
-
-
-def test_authors_are_parsed_per_author():
- """Regression: authors used to collapse to 'Unknown Authors' because the
- safe_find closure searched from the article root instead of each <Author>."""
- out = _format(SAMPLE_ARTICLE)
- assert "Authors: Furqan Khan, Jane Smith" in out
- assert "Unknown Authors" not in out
-
-
-def test_other_fields_still_parse():
- out = _format(SAMPLE_ARTICLE)
- assert "Title: Deep learning for retrieval augmented generation" in out
- assert "Journal: Nature Machine Intelligence" in out
- assert "DOI: 10.1000/example.doi" in out
-
-
-NO_AUTHORS_ARTICLE = """
-<PubmedArticle>
- <MedlineCitation>
- <PMID>87654321</PMID>
- <Article>
- <ArticleTitle>An article without an author list</ArticleTitle>
- <Abstract><AbstractText>No authors here.</AbstractText></Abstract>
- </Article>
- </MedlineCitation>
-</PubmedArticle>
-"""
-
-
-def test_missing_authors_falls_back():
- out = _format(NO_AUTHORS_ARTICLE)
- assert "Authors: Unknown Authors" in out
diff --git a/test/testcases/test_web_api/test_canvas_app/test_switch_component_unit.py b/test/testcases/test_web_api/test_canvas_app/test_switch_component_unit.py
deleted file mode 100644
index 6121370cb6..0000000000
--- a/test/testcases/test_web_api/test_canvas_app/test_switch_component_unit.py
+++ /dev/null
@@ -1,380 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-"""
-Unit tests for the Switch component, focused on issue #14235:
-the Switch condition should be able to target nested fields inside
-object outputs from upstream components, not just the whole output.
-
-The Switch component passes ``item["cpn_id"]`` to
-``canvas.get_variable_value``, which already understands the
-``[email protected]`` syntax (see ``Graph.get_variable_value``
-and ``Graph.get_variable_param_value`` in agent/canvas.py). These
-tests pin that contract down end-to-end through Switch._invoke.
-"""
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType
-from unittest.mock import MagicMock
-
-import pytest
-
-pytestmark = pytest.mark.p2
-
-
-def _load_switch_module(monkeypatch):
- """Load the real Switch component module with lightweight stubs for the
- heavy ``agent``/``common`` packages so the test runs without the full
- RAGFlow runtime."""
- repo_root = Path(__file__).resolve().parents[4]
-
- quart = ModuleType("quart")
- quart.make_response = lambda *a, **kw: None
- quart.jsonify = lambda *a, **kw: None
- monkeypatch.setitem(sys.modules, "quart", quart)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- constants = ModuleType("common.constants")
-
- class _RetCode:
- SUCCESS = 0
- EXCEPTION_ERROR = 100
-
- constants.RetCode = _RetCode
- monkeypatch.setitem(sys.modules, "common.constants", constants)
-
- conn_spec = importlib.util.spec_from_file_location("common.connection_utils", repo_root / "common" / "connection_utils.py")
- conn_mod = importlib.util.module_from_spec(conn_spec)
- monkeypatch.setitem(sys.modules, "common.connection_utils", conn_mod)
- conn_spec.loader.exec_module(conn_mod)
-
- misc_spec = importlib.util.spec_from_file_location("common.misc_utils", repo_root / "common" / "misc_utils.py")
- misc_mod = importlib.util.module_from_spec(misc_spec)
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_mod)
- misc_spec.loader.exec_module(misc_mod)
-
- agent_pkg = ModuleType("agent")
- agent_pkg.__path__ = [str(repo_root / "agent")]
- monkeypatch.setitem(sys.modules, "agent", agent_pkg)
-
- agent_settings = ModuleType("agent.settings")
- agent_settings.FLOAT_ZERO = 1e-8
- agent_settings.PARAM_MAXDEPTH = 5
- monkeypatch.setitem(sys.modules, "agent.settings", agent_settings)
-
- component_pkg = ModuleType("agent.component")
- component_pkg.__path__ = [str(repo_root / "agent" / "component")]
- monkeypatch.setitem(sys.modules, "agent.component", component_pkg)
-
- canvas_mod = ModuleType("agent.canvas")
-
- class Graph:
- pass
-
- canvas_mod.Graph = Graph
- monkeypatch.setitem(sys.modules, "agent.canvas", canvas_mod)
-
- base_spec = importlib.util.spec_from_file_location("agent.component.base", repo_root / "agent" / "component" / "base.py")
- base_mod = importlib.util.module_from_spec(base_spec)
- monkeypatch.setitem(sys.modules, "agent.component.base", base_mod)
- base_spec.loader.exec_module(base_mod)
-
- switch_spec = importlib.util.spec_from_file_location("agent.component.switch", repo_root / "agent" / "component" / "switch.py")
- switch_mod = importlib.util.module_from_spec(switch_spec)
- monkeypatch.setitem(sys.modules, "agent.component.switch", switch_mod)
- switch_spec.loader.exec_module(switch_mod)
-
- return switch_mod
-
-
-# --- helpers ---------------------------------------------------------------
-
-
-def _walk_path(obj, path):
- """Walk a dotted ``path`` through nested dicts/lists/attrs — a subset of
- ``Graph.get_variable_param_value`` used by the canvas resolver."""
- cur = obj
- for key in path.split("."):
- if cur is None:
- return None
- if isinstance(cur, dict):
- cur = cur.get(key)
- continue
- if isinstance(cur, (list, tuple)):
- try:
- cur = cur[int(key)]
- except Exception:
- return None
- continue
- cur = getattr(cur, key, None)
- return cur
-
-
-def _build_canvas(component_outputs):
- """
- Build a mock canvas whose ``get_variable_value`` resolves
- ``[email protected]`` against the supplied ``component_outputs``
- dict (mapping cpn_id -> {root_key: root_value}). This mimics the
- real Graph.get_variable_value parsing rules so the test exercises
- the same contract the Switch component depends on.
- """
- canvas = MagicMock()
- canvas.is_canceled = MagicMock(return_value=False)
-
- def resolve(exp):
- exp = exp.strip("{").strip("}").strip(" ").strip("{").strip("}")
- if "@" not in exp:
- return None
- cpn_id, var_nm = exp.split("@")
- parts = var_nm.split(".", 1)
- root_key = parts[0]
- rest = parts[1] if len(parts) > 1 else ""
- root_val = component_outputs.get(cpn_id, {}).get(root_key)
- if not rest:
- return root_val
- return _walk_path(root_val, rest)
-
- canvas.get_variable_value = MagicMock(side_effect=resolve)
- canvas.get_component_name = MagicMock(side_effect=lambda cid: cid)
- return canvas
-
-
-def _make_switch(module, canvas, conditions, end_cpn_ids):
- """Build a Switch instance bound to the supplied mock canvas, bypassing
- ``ComponentBase.__init__`` (which would require a real Graph)."""
- param = module.SwitchParam.__new__(module.SwitchParam)
- param.conditions = conditions
- param.end_cpn_ids = end_cpn_ids
- param.outputs = {}
- param.inputs = {}
- param.debug_inputs = {}
-
- inst = module.Switch.__new__(module.Switch)
- inst._canvas = canvas
- inst._param = param
- inst._id = "Switch:test"
- return inst
-
-
-# --- tests -----------------------------------------------------------------
-
-
-def test_switch_compares_nested_string_field_with_equals(monkeypatch):
- """Switch routes to the matching branch when a single-level nested string
- field equals the literal value."""
- module = _load_switch_module(monkeypatch)
- canvas = _build_canvas({"categorize:0": {"output": {"category": "tech", "score": 0.9, "meta": {"lang": "en"}}}})
- sw = _make_switch(
- module,
- canvas,
- conditions=[
- {
- "logical_operator": "and",
- "items": [
- {
- "cpn_id": "categorize:[email protected]",
- "operator": "=",
- "value": "tech",
- }
- ],
- "to": ["llm:tech"],
- },
- {
- "logical_operator": "and",
- "items": [
- {
- "cpn_id": "categorize:[email protected]",
- "operator": "=",
- "value": "news",
- }
- ],
- "to": ["llm:news"],
- },
- ],
- end_cpn_ids=["llm:fallback"],
- )
- sw._invoke()
- assert sw.output("_next") == ["llm:tech"]
-
-
-def test_switch_compares_doubly_nested_string_field(monkeypatch):
- """Switch resolves a two-level dotted path (`output.meta.lang`)."""
- module = _load_switch_module(monkeypatch)
- canvas = _build_canvas({"categorize:0": {"output": {"category": "tech", "meta": {"lang": "fr"}}}})
- sw = _make_switch(
- module,
- canvas,
- conditions=[
- {
- "logical_operator": "and",
- "items": [
- {
- "cpn_id": "categorize:[email protected]",
- "operator": "=",
- "value": "fr",
- }
- ],
- "to": ["fr_branch"],
- }
- ],
- end_cpn_ids=["else"],
- )
- sw._invoke()
- assert sw.output("_next") == ["fr_branch"]
-
-
-def test_switch_compares_nested_number_field_with_greater_than(monkeypatch):
- """Switch coerces the literal to float when the resolved value is a
- number, so `> 0.5` works on a nested score field."""
- module = _load_switch_module(monkeypatch)
- canvas = _build_canvas({"categorize:0": {"output": {"score": 0.87}}})
- sw = _make_switch(
- module,
- canvas,
- conditions=[
- {
- "logical_operator": "and",
- "items": [
- {
- "cpn_id": "categorize:[email protected]",
- "operator": ">",
- "value": "0.5",
- }
- ],
- "to": ["high"],
- }
- ],
- end_cpn_ids=["low"],
- )
- sw._invoke()
- assert sw.output("_next") == ["high"]
-
-
-def test_switch_nested_field_contains_operator(monkeypatch):
- """The `contains` operator works against a nested string field."""
- module = _load_switch_module(monkeypatch)
- canvas = _build_canvas({"begin": {"output": {"user": {"email": "[email protected]"}}}})
- sw = _make_switch(
- module,
- canvas,
- conditions=[
- {
- "logical_operator": "and",
- "items": [
- {
- "cpn_id": "[email protected]",
- "operator": "contains",
- "value": "@example.com",
- }
- ],
- "to": ["internal"],
- }
- ],
- end_cpn_ids=["external"],
- )
- sw._invoke()
- assert sw.output("_next") == ["internal"]
-
-
-def test_switch_nested_field_falls_through_to_else(monkeypatch):
- """When no nested-field condition matches, Switch routes to
- ``end_cpn_ids`` (the ELSE branch)."""
- module = _load_switch_module(monkeypatch)
- canvas = _build_canvas({"categorize:0": {"output": {"category": "music"}}})
- sw = _make_switch(
- module,
- canvas,
- conditions=[
- {
- "logical_operator": "and",
- "items": [
- {
- "cpn_id": "categorize:[email protected]",
- "operator": "=",
- "value": "tech",
- }
- ],
- "to": ["tech"],
- }
- ],
- end_cpn_ids=["else_branch"],
- )
- sw._invoke()
- assert sw.output("_next") == ["else_branch"]
-
-
-def test_switch_nested_list_index_access(monkeypatch):
- """The canvas resolver supports list indexing via the dotted path,
- so Switch can compare e.g. `chunks.0.text`."""
- module = _load_switch_module(monkeypatch)
- canvas = _build_canvas({"retrieval:0": {"output": {"chunks": [{"text": "hello world"}, {"text": "bye"}]}}})
- sw = _make_switch(
- module,
- canvas,
- conditions=[
- {
- "logical_operator": "and",
- "items": [
- {
- "cpn_id": "retrieval:[email protected]",
- "operator": "contains",
- "value": "hello",
- }
- ],
- "to": ["matched"],
- }
- ],
- end_cpn_ids=["unmatched"],
- )
- sw._invoke()
- assert sw.output("_next") == ["matched"]
-
-
-def test_switch_or_logical_operator_short_circuits_on_nested_match(monkeypatch):
- """With ``logical_operator: or``, a single matching nested-field
- condition wins even when an earlier condition fails."""
- module = _load_switch_module(monkeypatch)
- canvas = _build_canvas({"categorize:0": {"output": {"category": "news", "score": 0.1}}})
- sw = _make_switch(
- module,
- canvas,
- conditions=[
- {
- "logical_operator": "or",
- "items": [
- {
- "cpn_id": "categorize:[email protected]",
- "operator": ">",
- "value": "0.9",
- },
- {
- "cpn_id": "categorize:[email protected]",
- "operator": "=",
- "value": "news",
- },
- ],
- "to": ["news_branch"],
- }
- ],
- end_cpn_ids=["else"],
- )
- sw._invoke()
- assert sw.output("_next") == ["news_branch"]
diff --git a/test/testcases/test_web_api/test_chunk_app/conftest.py b/test/testcases/test_web_api/test_chunk_app/conftest.py
deleted file mode 100644
index ebbe74f02b..0000000000
--- a/test/testcases/test_web_api/test_chunk_app/conftest.py
+++ /dev/null
@@ -1,49 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-from time import sleep
-
-import pytest
-from test_common import batch_add_chunks, delete_chunks, list_chunks, list_documents, parse_documents
-from utils import wait_for
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_auth, _kb_id):
- res = list_documents(_auth, {"kb_id": _kb_id})
- for doc in res["data"]["docs"]:
- if doc["run"] != "DONE":
- return False
- return True
-
-
[email protected](scope="function")
-def add_chunks_func(request, WebApiAuth, add_document):
- def cleanup():
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- chunk_ids = [chunk["id"] for chunk in res["data"]["chunks"]]
- delete_chunks(WebApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
-
- request.addfinalizer(cleanup)
-
- dataset_id, document_id = add_document
- parse_documents(WebApiAuth, {"doc_ids": [document_id], "run": "1"})
- condition(WebApiAuth, dataset_id)
- chunk_ids = batch_add_chunks(WebApiAuth, dataset_id, document_id, 4)
- # issues/6487
- sleep(1)
- return dataset_id, document_id, chunk_ids
diff --git a/test/testcases/test_web_api/test_chunk_app/test_chunk_routes_unit.py b/test/testcases/test_web_api/test_chunk_app/test_chunk_routes_unit.py
deleted file mode 100644
index c529879da5..0000000000
--- a/test/testcases/test_web_api/test_chunk_app/test_chunk_routes_unit.py
+++ /dev/null
@@ -1,963 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import contextlib
-import inspect
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _Vec(list):
- def __mul__(self, scalar):
- return _Vec([scalar * x for x in self])
-
- __rmul__ = __mul__
-
- def __add__(self, other):
- return _Vec([a + b for a, b in zip(self, other)])
-
- def tolist(self):
- return list(self)
-
-
-class _DummyDoc:
- def __init__(self, *, doc_id="doc-1", kb_id="kb-1", name="Doc", parser_id="naive"):
- self.id = doc_id
- self.kb_id = kb_id
- self.name = name
- self.parser_id = parser_id
-
- def to_dict(self):
- return {"id": self.id, "kb_id": self.kb_id, "name": self.name}
-
-
-class _DummyRetCode:
- SUCCESS = 0
- DATA_ERROR = 102
- EXCEPTION_ERROR = 100
- OPERATING_ERROR = 103
- NOT_FOUND = 404
-
-
-class _DummyParserType:
- QA = "qa"
- NAIVE = "naive"
-
-
-class _DummyRetriever:
- async def search(self, query, _index_name, _kb_ids, *args, highlight=None, **kwargs):
- class _SRes:
- total = 1
- ids = ["chunk-1"]
- field = {
- "chunk-1": {
- "content_with_weight": "chunk content",
- "doc_id": "doc-1",
- "docnm_kwd": "Doc",
- "important_kwd": ["k1"],
- "question_kwd": ["q1"],
- "img_id": "img-1",
- "available_int": 1,
- "position_int": [],
- "doc_type_kwd": "text",
- }
- }
- highlight = {"chunk-1": " highlighted content "}
-
- _ = (query, highlight)
- return _SRes()
-
-
-class _DummyDocStore:
- def __init__(self):
- self.updated = []
- self.inserted = []
- self.deleted_inputs = []
- self.to_delete = [1]
- self.chunk = {
- "id": "chunk-1",
- "doc_id": "doc-1",
- "kb_id": "kb-1",
- "content_with_weight": "chunk content",
- "docnm_kwd": "Doc",
- "q_2_vec": [0.1, 0.2],
- "content_tks": ["a"],
- "content_ltks": ["b"],
- "content_sm_ltks": ["c"],
- }
-
- def get(self, *_args, **_kwargs):
- return dict(self.chunk) if self.chunk is not None else None
-
- def update(self, condition, payload, *_args, **_kwargs):
- self.updated.append((condition, payload))
- return True
-
- def delete(self, condition, *_args, **_kwargs):
- self.deleted_inputs.append(condition)
- if not self.to_delete:
- return 0
- return self.to_delete.pop(0)
-
- def insert(self, docs, *_args, **_kwargs):
- self.inserted.extend(docs)
-
- def index_exist(self, *_args, **_kwargs):
- return True
-
-
-class _DummyStorage:
- def __init__(self):
- self.put_calls = []
- self.rm_calls = []
-
- def put(self, bucket, name, binary):
- self.put_calls.append((bucket, name, binary))
-
- def obj_exist(self, _bucket, _name):
- return True
-
- def rm(self, bucket, name):
- self.rm_calls.append((bucket, name))
-
-
-class _DummyTenant:
- def __init__(self, tenant_id="tenant-1"):
- self.tenant_id = tenant_id
-
-
-class _DummyLLMBundle:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def encode(self, _inputs):
- return [_Vec([1.0, 2.0]), _Vec([3.0, 4.0])], 9
-
-
-class _DummyXXHash:
- def __init__(self, data):
- self._data = data
-
- def hexdigest(self):
- return f"chunk-{len(self._data)}"
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _route_core(func):
- return inspect.unwrap(func)
-
-
-def _load_chunk_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart_mod = ModuleType("quart")
- quart_mod.request = SimpleNamespace(args={}, headers={})
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- xxhash_mod = ModuleType("xxhash")
- xxhash_mod.xxh64 = lambda data: _DummyXXHash(data)
- monkeypatch.setitem(sys.modules, "xxhash", xxhash_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- settings_mod = ModuleType("common.settings")
- settings_mod.retriever = _DummyRetriever()
- settings_mod.docStoreConn = _DummyDocStore()
- settings_mod.STORAGE_IMPL = _DummyStorage()
- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)
- common_pkg.settings = settings_mod
-
- constants_mod = ModuleType("common.constants")
-
- class _DummyLLMType:
- EMBEDDING = SimpleNamespace(value="embedding")
- CHAT = SimpleNamespace(value="chat")
- RERANK = SimpleNamespace(value="rerank")
- ASR = SimpleNamespace(value="asr")
- VISION = SimpleNamespace(value="vision")
- TTS = SimpleNamespace(value="tts")
- OCR = SimpleNamespace(value="ocr")
-
- constants_mod.RetCode = _DummyRetCode
- constants_mod.LLMType = _DummyLLMType
- constants_mod.ParserType = _DummyParserType
- constants_mod.PAGERANK_FLD = "pagerank_flt"
- constants_mod.TaskStatus = SimpleNamespace(
- UNSTART=SimpleNamespace(value="0"),
- RUNNING=SimpleNamespace(value="1"),
- CANCEL=SimpleNamespace(value="2"),
- DONE=SimpleNamespace(value="3"),
- FAIL=SimpleNamespace(value="4"),
- SCHEDULE=SimpleNamespace(value="5"),
- )
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- string_utils_mod = ModuleType("common.string_utils")
- string_utils_mod.remove_redundant_spaces = lambda text: " ".join(str(text).split())
- string_utils_mod.is_content_empty = lambda content: content is None or not str(content).strip()
- monkeypatch.setitem(sys.modules, "common.string_utils", string_utils_mod)
-
- metadata_utils_mod = ModuleType("common.metadata_utils")
- metadata_utils_mod.apply_meta_data_filter = lambda *_args, **_kwargs: {}
- metadata_utils_mod.convert_conditions = lambda *_args, **_kwargs: {}
- metadata_utils_mod.meta_filter = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "common.metadata_utils", metadata_utils_mod)
-
- doc_store_base_mod = ModuleType("common.doc_store.doc_store_base")
- doc_store_base_mod.OrderByExpr = type("OrderByExpr", (), {})
- monkeypatch.setitem(sys.modules, "common.doc_store", ModuleType("common.doc_store"))
- monkeypatch.setitem(sys.modules, "common.doc_store.doc_store_base", doc_store_base_mod)
-
- tag_feature_utils_mod = ModuleType("common.tag_feature_utils")
- tag_feature_utils_mod.validate_tag_features = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "common.tag_feature_utils", tag_feature_utils_mod)
-
- pagination_utils_mod = ModuleType("api.utils.pagination_utils")
- pagination_utils_mod.validate_rest_api_page_size = lambda *_args, **_kwargs: (1, 30)
- monkeypatch.setitem(sys.modules, "api.utils.pagination_utils", pagination_utils_mod)
-
- reference_metadata_utils_mod = ModuleType("api.utils.reference_metadata_utils")
- reference_metadata_utils_mod.enrich_chunks_with_document_metadata = lambda chunks, *_args, **_kwargs: chunks
- reference_metadata_utils_mod.resolve_reference_metadata_preferences = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.utils.reference_metadata_utils", reference_metadata_utils_mod)
-
- misc_utils_mod = ModuleType("common.misc_utils")
-
- async def _thread_pool_exec(func):
- return func()
-
- misc_utils_mod.thread_pool_exec = _thread_pool_exec
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
-
- rag_app_pkg = ModuleType("rag.app")
- rag_app_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag.app", rag_app_pkg)
-
- rag_qa_mod = ModuleType("rag.app.qa")
- rag_qa_mod.rmPrefix = lambda text: str(text).strip("Q: ").strip("A: ")
- rag_qa_mod.beAdoc = lambda d, q, a, _latin: {**d, "question_kwd": [q], "content_with_weight": f"{q}\n{a}"}
- monkeypatch.setitem(sys.modules, "rag.app.qa", rag_qa_mod)
-
- rag_tag_mod = ModuleType("rag.app.tag")
- rag_tag_mod.label_question = lambda *_args, **_kwargs: []
- monkeypatch.setitem(sys.modules, "rag.app.tag", rag_tag_mod)
-
- rag_nlp_mod = ModuleType("rag.nlp")
- rag_nlp_mod.rag_tokenizer = SimpleNamespace(
- tokenize=lambda text: [str(text)],
- fine_grained_tokenize=lambda toks: [f"fg:{t}" for t in toks],
- is_chinese=lambda _text: False,
- )
- rag_nlp_mod.search = SimpleNamespace(index_name=lambda tenant_id: f"idx-{tenant_id}")
- monkeypatch.setitem(sys.modules, "rag.nlp", rag_nlp_mod)
-
- rag_prompts_pkg = ModuleType("rag.prompts")
- rag_prompts_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag.prompts", rag_prompts_pkg)
-
- rag_generator_mod = ModuleType("rag.prompts.generator")
- rag_generator_mod.cross_languages = lambda *_args, **_kwargs: []
- rag_generator_mod.keyword_extraction = lambda *_args, **_kwargs: []
- monkeypatch.setitem(sys.modules, "rag.prompts.generator", rag_generator_mod)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.current_user = SimpleNamespace(id="user-1")
- apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.get_json_result = lambda data=None, message="", code=0: {"code": code, "message": message, "data": data}
- api_utils_mod.get_data_error_result = lambda message="": {"code": _DummyRetCode.DATA_ERROR, "message": message, "data": False}
- api_utils_mod.get_result = lambda data=None, message="", code=0: {"code": code, "message": message, "data": data}
- api_utils_mod.get_error_data_result = lambda message="": {"code": _DummyRetCode.DATA_ERROR, "message": message, "data": False}
- api_utils_mod.server_error_response = lambda exc: {"code": _DummyRetCode.EXCEPTION_ERROR, "message": repr(exc), "data": False}
- api_utils_mod.validate_request = lambda *_args, **_kwargs: lambda fn: fn
- api_utils_mod.add_tenant_id_to_kwargs = lambda func: func
- api_utils_mod.check_duplicate_ids = lambda ids, _kind: (list(dict.fromkeys(ids)), [] if len(ids) == len(set(ids)) else [f"Duplicate {_kind} ids"])
- api_utils_mod.get_request_json = lambda: _AwaitableValue({})
- api_utils_mod.construct_json_result = lambda code=0, message="success", data=None: {"code": code, "message": message, "data": data}
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- image_utils_mod = ModuleType("api.utils.image_utils")
- image_utils_mod.store_chunk_image = lambda *_args, **_kwargs: None
- image_utils_mod.remove_chunk_image = lambda *_args, **_kwargs: None
- image_utils_mod.IMAGE_UPDATE_MODE_REMOVE = "remove"
- image_utils_mod.IMAGE_UPDATE_MODES = frozenset({"append", "replace", "remove"})
- monkeypatch.setitem(sys.modules, "api.utils.image_utils", image_utils_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- joint_services_pkg = ModuleType("api.db.joint_services")
- joint_services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.joint_services", joint_services_pkg)
-
- tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
- tenant_model_service_mod.get_model_config_by_id = lambda *_args, **_kwargs: {"llm_name": "embed", "model_type": "embedding"}
- tenant_model_service_mod.get_model_config_from_provider_instance = lambda *_args, **_kwargs: {"llm_name": "embed", "model_type": "embedding"}
- tenant_model_service_mod.resolve_model_config = lambda *_args, **_kwargs: {"llm_name": "embed", "model_type": "embedding"}
- tenant_model_service_mod.get_tenant_default_model_by_type = lambda *_args, **_kwargs: {"llm_name": "chat", "model_type": "chat"}
- tenant_model_service_mod.split_model_name = lambda model_name: (model_name.rsplit("@", 2) + ["", ""])[:3]
- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
-
- # chunk_api imports structure_graph_common from api.apps.services; stub it in
- # sys.modules so the real module and its transitive imports are not loaded.
- stub_apps_services = ModuleType("api.apps.services")
- monkeypatch.setitem(sys.modules, "api.apps.services", stub_apps_services)
-
- sgc_mod = ModuleType("api.apps.services.structure_graph_common")
-
- async def _sgc_keyword_subgraph(*_args, **_kwargs):
- return {}, [], []
-
- async def _sgc_build_bucket(*_args, **_kwargs):
- return [], []
-
- sgc_mod.keyword_subgraph = _sgc_keyword_subgraph
- sgc_mod.build_bucket = _sgc_build_bucket
- monkeypatch.setitem(sys.modules, "api.apps.services.structure_graph_common", sgc_mod)
-
- # chunk_api imports DB, Document, Task from db_models; stub them so the real
- # module (which pulls in quart_auth) is never loaded.
- class _FakeExpr:
- def __or__(self, other):
- return self
-
- def __and__(self, other):
- return self
-
- class _FakeField:
- def __eq__(self, other):
- return _FakeExpr()
-
- def __ne__(self, other):
- return _FakeExpr()
-
- def is_null(self, value=True):
- return _FakeExpr()
-
- class _StubFreshDoc:
- id = "doc-1"
- kb_id = "kb-1"
- token_num = 2
- chunk_num = 1
- process_duration = 0.0
-
- class _StubDocQuery:
- def where(self, *_args, **_kwargs):
- return self
-
- def for_update(self):
- return self
-
- def first(self):
- return _StubDocumentModel.fresh_doc
-
- class _StubDocumentModel:
- id = _FakeField()
- run = _FakeField()
- fresh_doc = _StubFreshDoc()
-
- @classmethod
- def select(cls, *_args, **_kwargs):
- return _StubDocQuery()
-
- class _StubTaskModel:
- doc_id = _FakeField()
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.Document = _StubDocumentModel
- db_models_mod.Task = _StubTaskModel
- db_models_mod.DB = SimpleNamespace(atomic=lambda: contextlib.nullcontext())
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- file2document_service_mod = ModuleType("api.db.services.file2document_service")
- file2document_service_mod.File2DocumentService = SimpleNamespace(get_storage_address=lambda **_kwargs: ("", ""))
- monkeypatch.setitem(sys.modules, "api.db.services.file2document_service", file2document_service_mod)
-
- task_service_mod = ModuleType("api.db.services.task_service")
- task_service_mod.TaskService = SimpleNamespace(filter_delete=lambda *_args, **_kwargs: None)
- task_service_mod.cancel_all_task_of = lambda *_args, **_kwargs: None
- task_service_mod.queue_tasks = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.db.services.task_service", task_service_mod)
-
- document_counter_service_mod = ModuleType("api.db.services.document_counter_service")
- document_counter_service_mod.release_reparse_counters = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.db.services.document_counter_service", document_counter_service_mod)
-
- document_service_mod = ModuleType("api.db.services.document_service")
-
- class _DocumentService:
- decrement_calls = []
- increment_calls = []
-
- @staticmethod
- def get_tenant_id(_doc_id):
- return "tenant-1"
-
- @staticmethod
- def get_by_id(doc_id):
- return True, _DummyDoc(doc_id=doc_id, parser_id=_DummyParserType.NAIVE)
-
- @staticmethod
- def query(**kwargs):
- return [_DummyDoc(doc_id=kwargs.get("id", "doc-1"), kb_id=kwargs.get("kb_id", "kb-1"))]
-
- @staticmethod
- def get_by_ids(ids):
- return [_DummyDoc(doc_id=ids[0] if ids else "doc-1")]
-
- @staticmethod
- def delete_chunk_images(*_args, **_kwargs):
- return None
-
- @staticmethod
- def get_embd_id(_doc_id):
- return "embed-1"
-
- @staticmethod
- def get_tenant_embd_id(_doc_id):
- return "tm-embd-1"
-
- @staticmethod
- def decrement_chunk_num(*args):
- _DocumentService.decrement_calls.append(args)
-
- @staticmethod
- def increment_chunk_num(*args):
- _DocumentService.increment_calls.append(args)
-
- document_service_mod.DocumentService = _DocumentService
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", document_service_mod)
- services_pkg.document_service = document_service_mod
-
- doc_metadata_service_mod = ModuleType("api.db.services.doc_metadata_service")
- doc_metadata_service_mod.DocMetadataService = type("DocMetadataService", (), {})
- monkeypatch.setitem(sys.modules, "api.db.services.doc_metadata_service", doc_metadata_service_mod)
- services_pkg.doc_metadata_service = doc_metadata_service_mod
-
- kb_service_mod = ModuleType("api.db.services.knowledgebase_service")
-
- class _KnowledgebaseService:
- @staticmethod
- def get_kb_ids(_tenant_id):
- return ["kb-1"]
-
- @staticmethod
- def accessible(**_kwargs):
- return True
-
- @staticmethod
- def get_by_id(_kb_id):
- return True, SimpleNamespace(pagerank=0.6, tenant_id="tenant-1", tenant_embd_id="tm-embd-2", tenant_llm_id="tm-llm-1")
-
- kb_service_mod.KnowledgebaseService = _KnowledgebaseService
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_service_mod)
- services_pkg.knowledgebase_service = kb_service_mod
-
- class _DummyLLMService:
- @staticmethod
- def query(**_kwargs):
- return [SimpleNamespace(llm_name="gpt-3.5-turbo", model_type="chat", max_tokens=8192, is_tools=True)]
-
- llm_service_mod = ModuleType("api.db.services.llm_service")
- llm_service_mod.LLMService = _DummyLLMService
- llm_service_mod.LLMBundle = _DummyLLMBundle
- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)
- services_pkg.llm_service = llm_service_mod
-
- search_service_mod = ModuleType("api.db.services.search_service")
- search_service_mod.SearchService = type("SearchService", (), {})
- monkeypatch.setitem(sys.modules, "api.db.services.search_service", search_service_mod)
- services_pkg.search_service = search_service_mod
-
- tenant_llm_service_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _MockTableObject:
- def __init__(self, **kwargs):
- for key, value in kwargs.items():
- setattr(self, key, value)
-
- def to_dict(self):
- return {k: v for k, v in self.__dict__.items()}
-
- class _TenantLLMService:
- @staticmethod
- def get_by_id(tenant_model_id):
- return True, _MockTableObject(
- id=tenant_model_id,
- tenant_id="tenant-1",
- llm_factory="",
- model_type="chat",
- llm_name="gpt-3.5-turbo",
- api_key="fake-api-key",
- api_base="https://api.example.com",
- max_tokens=8192,
- used_tokens=0,
- status=1,
- )
-
- @staticmethod
- def get_api_key(tenant_id, model_name):
- return _MockTableObject(
- id=1, tenant_id=tenant_id, llm_factory="", model_type="chat", llm_name=model_name, api_key="fake-api-key", api_base="https://api.example.com", max_tokens=8192, used_tokens=0, status=1
- )
-
- @staticmethod
- def split_model_name_and_factory(model_name):
- if "@" in model_name:
- parts = model_name.rsplit("@", 1)
- return parts[0], parts[1]
- return model_name, None
-
- @staticmethod
- def increase_usage_by_id(model_id, used_tokens):
- return True
-
- @staticmethod
- def model_instance(_model_config):
- return _DummyLLMBundle()
-
- class _TenantService:
- @staticmethod
- def get_by_id(tenant_id):
- return True, SimpleNamespace(
- llm_id="gpt-3.5-turbo",
- tenant_llm_id="tm-llm-1",
- embd_id="text-embedding-ada-002",
- tenant_embd_id="tm-embd-2",
- asr_id="whisper-1",
- img2txt_id="gpt-4-vision-preview",
- rerank_id="bge-reranker",
- tts_id="tts-1",
- )
-
- tenant_llm_service_mod.TenantLLMService = _TenantLLMService
- tenant_llm_service_mod.TenantService = _TenantService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_service_mod)
- services_pkg.tenant_llm_service = tenant_llm_service_mod
-
- user_service_mod = ModuleType("api.db.services.user_service")
-
- class _UserTenantService:
- @staticmethod
- def query(**_kwargs):
- return [_DummyTenant("tenant-1")]
-
- user_service_mod.UserTenantService = _UserTenantService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
- services_pkg.user_service = user_service_mod
-
- module_path = repo_root / "api" / "apps" / "chunk_app.py"
- module = None
- if module_path.exists():
- module_name = "test_chunk_routes_unit_module"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def _load_chunk_api_module(monkeypatch):
- _load_chunk_module(monkeypatch)
- repo_root = Path(__file__).resolve().parents[4]
- module_name = "test_chunk_api_routes_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "chunk_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- # chunk_api imports these inside the chunk-write helpers; re-expose the shared
- # stubs so tests can reach them via module.<name> (setattr / method patching).
- module.rag_tokenizer = sys.modules["rag.nlp"].rag_tokenizer
- module.beAdoc = sys.modules["rag.app.qa"].beAdoc
- module.rmPrefix = sys.modules["rag.app.qa"].rmPrefix
- module.label_question = sys.modules["rag.app.tag"].label_question
- module.cross_languages = sys.modules["rag.prompts.generator"].cross_languages
- module.keyword_extraction = sys.modules["rag.prompts.generator"].keyword_extraction
- return module
-
-
-def _set_request_json(monkeypatch, module, payload):
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(payload))
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def test_restful_chunk_list_get_and_delete_unit(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.request = SimpleNamespace(args={"keywords": "chunk", "available": "true"}, headers={})
-
- res = _run(_route_core(module.list_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == 0, res
- assert res["data"]["total"] == 1, res
- assert res["data"]["chunks"][0]["id"] == "chunk-1", res
- assert res["data"]["chunks"][0]["available"] is True, res
- assert res["data"]["chunks"][0]["doc_type_kwd"] == "text", res
-
- module.request = SimpleNamespace(args={"id": "chunk-1"}, headers={})
- module.settings.docStoreConn.chunk["doc_type_kwd"] = "image"
- res = _run(_route_core(module.list_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == 0, res
- assert res["data"]["chunks"][0]["doc_type_kwd"] == "image", res
-
- res = _run(_route_core(module.get_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == 0, res
- assert "q_2_vec" not in res["data"], res
- assert "content_tks" not in res["data"], res
- assert "content_ltks" not in res["data"], res
- assert "content_sm_ltks" not in res["data"], res
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"chunk_ids": ["chunk-1"]}))
- res = _run(_route_core(module.rm_chunk)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == 0, res
- assert module.settings.docStoreConn.deleted_inputs[-1]["doc_id"] == "doc-1"
-
-
-def test_restful_chunk_add_update_and_switch_unit(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.request = SimpleNamespace(args={}, headers={})
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "content": "chunk",
- "important_keywords": ["i1"],
- "questions": ["q1"],
- "tag_kwd": ["tag"],
- "tag_feas": {"tag": 0.2},
- }
- ),
- )
- res = _run(_route_core(module.add_chunk)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == 0, res
- assert res["data"]["chunk"]["content"] == "chunk", res
- assert module.settings.docStoreConn.inserted, "insert should be called"
- assert module.DocumentService.increment_calls, "increment_chunk_num should be called"
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue(
- {
- "content": "updated chunk",
- "important_keywords": ["i2"],
- "questions": ["q2"],
- "tag_kwd": ["tag2"],
- "positions": [[1, 2, 3, 4, 5]],
- "available": False,
- }
- ),
- )
- res = _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == 0, res
- updated = module.settings.docStoreConn.updated[-1][1]
- assert updated["content_with_weight"] == "updated chunk"
- assert updated["available_int"] == 0
- assert updated["position_int"] == [[1, 2, 3, 4, 5]]
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"chunk_ids": ["chunk-1"], "available": True}))
- res = _run(_route_core(module.switch_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == 0, res
- assert res["data"] is True, res
-
-
-def test_restful_chunk_guard_branches_unit(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.request = SimpleNamespace(args={}, headers={})
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: False)
- res = _run(_route_core(module.list_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["message"] == "You don't own the dataset kb-1.", res
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: True)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- res = _run(_route_core(module.list_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["message"] == "you don't own the document doc-1", res
-
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [_DummyDoc()])
- module.request = SimpleNamespace(args={"id": "chunk-1"}, headers={})
- module.settings.docStoreConn.chunk = None
- res = _run(_route_core(module.list_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "Chunk not found" in res["message"], res
-
- module.settings.docStoreConn.chunk = {
- "id": "chunk-1",
- "doc_id": "other-doc",
- "content_with_weight": "chunk",
- "docnm_kwd": "Doc",
- }
- res = _run(_route_core(module.list_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "Chunk not found" in res["message"], res
-
- module.settings.docStoreConn.chunk = None
- module.request = SimpleNamespace(args={}, headers={})
- res = _run(_route_core(module.get_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "Chunk not found" in res["message"], res
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"content": ""}))
- res = _run(_route_core(module.add_chunk)("tenant-1", "kb-1", "doc-1"))
- assert res["message"] == "`content` is required", res
-
- module.settings.docStoreConn.chunk = {"id": "chunk-1", "doc_id": "doc-1", "content_with_weight": "chunk"}
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"important_keywords": "bad"}))
- res = _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["message"] == "`important_keywords` should be a list", res
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"chunk_ids": []}))
- res = _run(_route_core(module.switch_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["message"] == "`chunk_ids` is required.", res
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"chunk_ids": ["chunk-1"]}))
- res = _run(_route_core(module.switch_chunks)("tenant-1", "kb-1", "doc-1"))
- assert res["message"] == "`available_int` or `available` is required.", res
-
-
-def test_restful_add_chunk_invalid_image_base64_does_not_index_chunk(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.request = SimpleNamespace(args={}, headers={})
- module.settings.docStoreConn.inserted.clear()
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"content": "chunk with bad image", "image_base64": "not-valid-base64!!!"}),
- )
- res = _run(_route_core(module.add_chunk)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert res["message"] == "Invalid `image_base64`", res
- assert module.settings.docStoreConn.inserted == [], res
- assert module.DocumentService.increment_calls == [], res
-
-
-def test_restful_add_chunk_empty_image_base64_does_not_index_chunk(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.request = SimpleNamespace(args={}, headers={})
- module.settings.docStoreConn.inserted.clear()
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"content": "chunk with empty image", "image_base64": ""}),
- )
- res = _run(_route_core(module.add_chunk)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert res["message"] == "`image_base64` must be a non-empty string", res
- assert module.settings.docStoreConn.inserted == [], res
- assert module.DocumentService.increment_calls == [], res
-
-
-def test_restful_update_chunk_invalid_image_base64_does_not_update_chunk(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.request = SimpleNamespace(args={}, headers={})
- module.settings.docStoreConn.updated.clear()
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"content": "updated chunk", "image_base64": "not-valid-base64!!!"}),
- )
- res = _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert res["message"] == "Invalid `image_base64`", res
- assert module.settings.docStoreConn.updated == [], res
-
-
-def test_restful_add_chunk_valid_image_base64_stores_before_insert(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.request = SimpleNamespace(args={}, headers={})
- module.settings.docStoreConn.inserted.clear()
- store_calls = []
- monkeypatch.setattr(module, "store_chunk_image", lambda bucket, name, binary: store_calls.append((bucket, name, binary)))
-
- valid_b64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"content": "chunk with image", "image_base64": valid_b64}),
- )
- res = _run(_route_core(module.add_chunk)("tenant-1", "kb-1", "doc-1"))
- assert res["code"] == 0, res
- assert store_calls, "store_chunk_image should run before doc-store insert"
- assert module.settings.docStoreConn.inserted, "chunk should be indexed after image stored"
- inserted = module.settings.docStoreConn.inserted[-1]
- assert inserted.get("img_id"), inserted
- assert inserted.get("doc_type_kwd") == "image", inserted
- assert res["data"]["chunk"]["doc_type_kwd"] == "image", res
-
-
-def test_restful_update_chunk_replace_image_mode(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.settings.docStoreConn.chunk = {
- "id": "chunk-1",
- "doc_id": "doc-1",
- "content_with_weight": "chunk",
- "img_id": "kb-1-chunk-1",
- }
- store_calls = []
- module.store_chunk_image = lambda bucket, name, binary, mode="append": store_calls.append((bucket, name, mode))
-
- valid_b64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"image_base64": valid_b64, "image_update_mode": "replace"}),
- )
- res = _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == 0, res
- assert store_calls == [("kb-1", "chunk-1", "replace")], store_calls
- updated = module.settings.docStoreConn.updated[-1][1]
- assert updated["img_id"] == "kb-1-chunk-1", updated
- assert updated["doc_type_kwd"] == "image", updated
-
-
-def test_restful_update_chunk_remove_image_mode(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.settings.docStoreConn.chunk = {
- "id": "chunk-1",
- "doc_id": "doc-1",
- "content_with_weight": "chunk",
- "img_id": "kb-1-chunk-1",
- }
- call_order = []
- original_update = module.settings.docStoreConn.update
-
- def tracked_update(*args, **kwargs):
- call_order.append("update")
- return original_update(*args, **kwargs)
-
- module.settings.docStoreConn.update = tracked_update
- module.remove_chunk_image = lambda bucket, name: call_order.append(("remove", bucket, name))
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"image_update_mode": "remove"}))
- res = _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == 0, res
- assert call_order == ["update", ("remove", "kb-1", "chunk-1")], call_order
- updated = module.settings.docStoreConn.updated[-1][1]
- assert updated["img_id"] == "", updated
- assert updated["doc_type_kwd"] == "text", updated
-
-
-def test_restful_update_chunk_remove_image_skips_delete_when_encode_fails(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.settings.docStoreConn.chunk = {
- "id": "chunk-1",
- "doc_id": "doc-1",
- "content_with_weight": "chunk",
- "img_id": "kb-1-chunk-1",
- }
- remove_calls = []
-
- class _FailingLLMBundle(_DummyLLMBundle):
- def encode(self, _inputs):
- raise RuntimeError("embedding failed")
-
- module.TenantLLMService.model_instance = staticmethod(lambda _model_config: _FailingLLMBundle())
- module.remove_chunk_image = lambda bucket, name: remove_calls.append((bucket, name))
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"image_update_mode": "remove"}))
- with pytest.raises(RuntimeError, match="embedding failed"):
- _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert remove_calls == [], remove_calls
- assert module.settings.docStoreConn.updated == [], module.settings.docStoreConn.updated
-
-
-def test_restful_update_chunk_remove_image_skips_delete_when_update_fails(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.settings.docStoreConn.chunk = {
- "id": "chunk-1",
- "doc_id": "doc-1",
- "content_with_weight": "chunk",
- "img_id": "kb-1-chunk-1",
- }
- remove_calls = []
- module.settings.docStoreConn.update = lambda *_args, **_kwargs: False
- module.remove_chunk_image = lambda bucket, name: remove_calls.append((bucket, name))
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"image_update_mode": "remove"}))
- res = _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "Index updating failure" in res["message"], res
- assert remove_calls == [], remove_calls
-
-
-def test_restful_update_chunk_replace_requires_image_base64(monkeypatch):
- module = _load_chunk_api_module(monkeypatch)
- module.settings.docStoreConn.chunk = {"id": "chunk-1", "doc_id": "doc-1", "content_with_weight": "chunk"}
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"image_update_mode": "replace"}))
- res = _run(_route_core(module.update_chunk)("tenant-1", "kb-1", "doc-1", "chunk-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "`image_base64` is required" in res["message"], res
- assert module.settings.docStoreConn.updated == [], res
diff --git a/test/testcases/test_web_api/test_chunk_app/test_create_chunk.py b/test/testcases/test_web_api/test_chunk_app/test_create_chunk.py
deleted file mode 100644
index bc236229d8..0000000000
--- a/test/testcases/test_web_api/test_chunk_app/test_create_chunk.py
+++ /dev/null
@@ -1,195 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from test_common import add_chunk, delete_document, get_chunk, list_chunks
-
-
-def validate_chunk_details(auth, dataset_id, document_id, payload, res):
- chunk = res["data"]["chunk"]
- assert chunk["dataset_id"] == dataset_id
- assert chunk["document_id"] == document_id
- assert chunk["content"] == payload["content"]
- if "important_keywords" in payload:
- assert chunk["important_keywords"] == payload["important_keywords"]
- if "questions" in payload:
- expected = [str(q).strip() for q in payload.get("questions", []) if str(q).strip()]
- assert chunk["questions"] == expected
- if "tag_kwd" in payload:
- assert chunk["tag_kwd"] == payload["tag_kwd"]
-
- fetched = get_chunk(auth, dataset_id, document_id, chunk["id"])
- assert fetched["code"] == 0, fetched
- assert fetched["data"]["id"] == chunk["id"]
- assert fetched["data"]["doc_id"] == document_id
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = add_chunk(invalid_auth, "dataset_id", "document_id", {"content": "chunk test"})
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestAddChunk:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": None}, 102, "`content` is required"),
- ({"content": ""}, 102, "`content` is required"),
- ({"content": "a"}, 0, ""),
- ({"content": " "}, 102, "`content` is required"),
- ({"content": "\n!?。;!?\"'"}, 0, ""),
- ],
- )
- def test_content(self, WebApiAuth, add_document, payload, expected_code, expected_message):
- dataset_id, document_id = add_document
- chunks_count = list_chunks(WebApiAuth, dataset_id, document_id)["data"]["doc"]["chunk_count"]
- res = add_chunk(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- validate_chunk_details(WebApiAuth, dataset_id, document_id, payload, res)
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 1, res
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": "chunk test", "important_keywords": ["a", "b", "c"]}, 0, ""),
- ({"content": "chunk test", "important_keywords": [""]}, 0, ""),
- ({"content": "chunk test", "important_keywords": [1]}, 100, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"content": "chunk test", "important_keywords": ["a", "a"]}, 0, ""),
- ({"content": "chunk test", "important_keywords": "abc"}, 102, "`important_keywords` is required to be a list"),
- ({"content": "chunk test", "important_keywords": 123}, 102, "`important_keywords` is required to be a list"),
- ],
- )
- def test_important_keywords(self, WebApiAuth, add_document, payload, expected_code, expected_message):
- dataset_id, document_id = add_document
- res = add_chunk(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- validate_chunk_details(WebApiAuth, dataset_id, document_id, payload, res)
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": "chunk test", "questions": ["a", "b", "c"]}, 0, ""),
- ({"content": "chunk test", "questions": [""]}, 0, ""),
- ({"content": "chunk test", "questions": [1]}, 100, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"content": "chunk test", "questions": ["a", "a"]}, 0, ""),
- ({"content": "chunk test", "questions": "abc"}, 102, "`questions` is required to be a list"),
- ({"content": "chunk test", "questions": 123}, 102, "`questions` is required to be a list"),
- ],
- )
- def test_questions(self, WebApiAuth, add_document, payload, expected_code, expected_message):
- dataset_id, document_id = add_document
- res = add_chunk(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- validate_chunk_details(WebApiAuth, dataset_id, document_id, payload, res)
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- def test_add_chunk_with_tag_fields(self, WebApiAuth, add_document):
- dataset_id, document_id = add_document
- payload = {
- "content": "chunk with tags",
- "tag_kwd": ["tag1", "tag2"],
- "important_keywords": ["tag"],
- "questions": ["question"],
- }
- res = add_chunk(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 0, res
- validate_chunk_details(WebApiAuth, dataset_id, document_id, payload, res)
-
- @pytest.mark.p2
- def test_get_chunk_not_found(self, WebApiAuth, add_document):
- dataset_id, document_id = add_document
- res = get_chunk(WebApiAuth, dataset_id, document_id, "missing_chunk_id")
- assert res["code"] == 102, res
- assert "Chunk not found" in res["message"], res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- ("invalid_document_id", 102, "you don't own the document invalid_document_id"),
- ],
- )
- def test_invalid_document_id(self, WebApiAuth, add_document, document_id, expected_code, expected_message):
- dataset_id, _ = add_document
- res = add_chunk(WebApiAuth, dataset_id, document_id, {"content": "chunk test"})
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
- @pytest.mark.p3
- def test_repeated_add_chunk(self, WebApiAuth, add_document):
- payload = {"content": "chunk test"}
- dataset_id, document_id = add_document
- chunks_count = list_chunks(WebApiAuth, dataset_id, document_id)["data"]["doc"]["chunk_count"]
-
- res = add_chunk(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 0, res
- validate_chunk_details(WebApiAuth, dataset_id, document_id, payload, res)
-
- res = add_chunk(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 0, res
- validate_chunk_details(WebApiAuth, dataset_id, document_id, payload, res)
-
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 2, res
-
- @pytest.mark.p2
- def test_add_chunk_to_deleted_document(self, WebApiAuth, add_document):
- dataset_id, document_id = add_document
- delete_document(WebApiAuth, dataset_id, {"ids": [document_id]})
- res = add_chunk(WebApiAuth, dataset_id, document_id, {"content": "chunk test"})
- assert res["code"] == 102, res
- assert res["message"] == f"you don't own the document {document_id}", res
-
- @pytest.mark.skip(reason="issues/6411")
- @pytest.mark.p3
- def test_concurrent_add_chunk(self, WebApiAuth, add_document):
- count = 50
- dataset_id, document_id = add_document
- chunks_count = list_chunks(WebApiAuth, dataset_id, document_id)["data"]["doc"]["chunk_count"]
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(add_chunk, WebApiAuth, dataset_id, document_id, {"content": f"chunk test {i}"}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["data"]["doc"]["chunk_count"] == chunks_count + count
diff --git a/test/testcases/test_web_api/test_chunk_app/test_list_chunks.py b/test/testcases/test_web_api/test_chunk_app/test_list_chunks.py
deleted file mode 100644
index 12b083b512..0000000000
--- a/test/testcases/test_web_api/test_chunk_app/test_list_chunks.py
+++ /dev/null
@@ -1,162 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from test_common import batch_add_chunks, list_chunks, update_chunk
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = list_chunks(invalid_auth, "dataset_id", "document_id")
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestChunksList:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 2}, 0, 2, ""),
- pytest.param({"page": 0, "page_size": 2}, 100, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- ({"page": 2, "page_size": 2}, 0, 2, ""),
- ({"page": 3, "page_size": 2}, 0, 1, ""),
- ({"page": "3", "page_size": 2}, 0, 1, ""),
- pytest.param({"page": -1, "page_size": 2}, 100, 0, "ValueError('Search does not support negative slicing.')", marks=pytest.mark.skip),
- pytest.param({"page": "a", "page_size": 2}, 100, 0, """ValueError("invalid literal for int() with base 10: 'a'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page(self, WebApiAuth, add_chunks, params, expected_code, expected_page_size, expected_message):
- dataset_id, document_id, _ = add_chunks
- res = list_chunks(WebApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size, res
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page_size": None}, 0, 5, ""),
- pytest.param({"page_size": 0}, 0, 5, ""),
- ({"page_size": 1}, 0, 1, ""),
- ({"page_size": 6}, 0, 5, ""),
- ({"page_size": "1"}, 0, 1, ""),
- pytest.param({"page_size": -1}, 0, 5, "", marks=pytest.mark.skip),
- pytest.param({"page_size": "a"}, 100, 0, """ValueError("invalid literal for int() with base 10: 'a'")""", marks=pytest.mark.skip),
- ],
- )
- def test_page_size(self, WebApiAuth, add_chunks, params, expected_code, expected_page_size, expected_message):
- dataset_id, document_id, _ = add_chunks
- res = list_chunks(WebApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- assert len(res["data"]["chunks"]) == expected_page_size, res
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- def test_available_filter(self, WebApiAuth, add_chunks):
- dataset_id, document_id, chunk_ids = add_chunks
- chunk_id = chunk_ids[0]
-
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_id, {"content": "unchanged content", "available": False})
- assert res["code"] == 0, res
-
- from time import sleep, time
-
- deadline = time() + 5
- res = None
- while time() < deadline:
- res = list_chunks(WebApiAuth, dataset_id, document_id, params={"available": "false"})
- assert res["code"] == 0, res
- if res["data"]["chunks"]:
- break
- sleep(0.5)
- assert res is not None
- assert len(res["data"]["chunks"]) >= 1, res
- assert all(chunk["available"] is False for chunk in res["data"]["chunks"]), res
-
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_id, {"content": "chunk test 0", "available": True})
- assert res["code"] == 0, res
- sleep(1)
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_page_size, minimum_page_size",
- [
- ({"keywords": None}, 5, None),
- ({"keywords": ""}, 5, None),
- ({"keywords": "1"}, 1, None),
- pytest.param({"keywords": "chunk"}, None, 3, marks=pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6509")),
- ({"keywords": "unknown"}, 0, None),
- ],
- )
- def test_keywords(self, WebApiAuth, add_chunks, params, expected_page_size, minimum_page_size):
- dataset_id, document_id, _ = add_chunks
- res = list_chunks(WebApiAuth, dataset_id, document_id, params=params)
- assert res["code"] == 0, res
- if minimum_page_size is not None:
- assert len(res["data"]["chunks"]) >= minimum_page_size, res
- else:
- assert len(res["data"]["chunks"]) == expected_page_size, res
-
- @pytest.mark.p3
- def test_invalid_params(self, WebApiAuth, add_chunks):
- dataset_id, document_id, _ = add_chunks
- res = list_chunks(WebApiAuth, dataset_id, document_id, params={"a": "b"})
- assert res["code"] == 0, res
- assert len(res["data"]["chunks"]) == 5, res
-
- @pytest.mark.p3
- def test_concurrent_list(self, WebApiAuth, add_chunks):
- dataset_id, document_id, _ = add_chunks
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_chunks, WebApiAuth, dataset_id, document_id) for _ in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(len(future.result()["data"]["chunks"]) == 5 for future in futures)
-
- @pytest.mark.p1
- def test_default(self, WebApiAuth, add_document):
- dataset_id, document_id = add_document
-
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- chunks_count = res["data"]["doc"]["chunk_count"]
- batch_add_chunks(WebApiAuth, dataset_id, document_id, 31)
-
- from time import sleep
-
- sleep(3)
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["code"] == 0
- assert len(res["data"]["chunks"]) == 30
- assert res["data"]["doc"]["chunk_count"] == chunks_count + 31
diff --git a/test/testcases/test_web_api/test_chunk_app/test_rm_chunks.py b/test/testcases/test_web_api/test_chunk_app/test_rm_chunks.py
deleted file mode 100644
index 7d965ec99f..0000000000
--- a/test/testcases/test_web_api/test_chunk_app/test_rm_chunks.py
+++ /dev/null
@@ -1,152 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from test_common import batch_add_chunks, delete_chunks, list_chunks
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = delete_chunks(invalid_auth, "dataset_id", "document_id", {"chunk_ids": ["1"]})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestChunksDeletion:
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- ("invalid_document_id", 100, "Can't find the document with ID invalid_document_id!"),
- ],
- )
- def test_invalid_document_id(self, WebApiAuth, add_chunks_func, document_id, expected_code, expected_message):
- dataset_id, _, chunk_ids = add_chunks_func
- res = delete_chunks(WebApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
- assert res["code"] == expected_code, res
- assert expected_message in res["message"], res
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"chunk_ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"chunk_ids": r[:1] + ["invalid_id"] + r[1:4]}, marks=pytest.mark.p1),
- pytest.param(lambda r: {"chunk_ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
- ],
- )
- def test_delete_partial_invalid_id(self, WebApiAuth, add_chunks_func, payload):
- dataset_id, document_id, chunk_ids = add_chunks_func
- payload = payload(chunk_ids)
- res = delete_chunks(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 102, res
- assert "rm_chunk deleted chunks" in res["message"], res
-
- @pytest.mark.p3
- def test_repeated_deletion(self, WebApiAuth, add_chunks_func):
- dataset_id, document_id, chunk_ids = add_chunks_func
- payload = {"chunk_ids": chunk_ids}
- res = delete_chunks(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 0, res
-
- res = delete_chunks(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == 102, res
- assert res["message"] == f"rm_chunk deleted chunks 0, expect {len(chunk_ids)}", res
-
- @pytest.mark.p3
- def test_duplicate_deletion(self, WebApiAuth, add_chunks_func):
- dataset_id, document_id, chunk_ids = add_chunks_func
- res = delete_chunks(WebApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids * 2})
- assert res["code"] == 0, res
-
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["code"] == 0, res
- assert len(res["data"]["chunks"]) == 0, res
- assert res["data"]["total"] == 0, res
-
- @pytest.mark.p2
- def test_delete_duplicate_ids_dedup_behavior(self, WebApiAuth, add_chunks_func):
- dataset_id, document_id, chunk_ids = add_chunks_func
- res = delete_chunks(WebApiAuth, dataset_id, document_id, {"chunk_ids": [chunk_ids[0], chunk_ids[0]]})
- assert res["code"] == 0, res
-
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["code"] == 0, res
- assert len(res["data"]["chunks"]) == 3, res
- assert res["data"]["total"] == 3, res
-
- @pytest.mark.p3
- def test_concurrent_deletion(self, WebApiAuth, add_document):
- count = 100
- dataset_id, document_id = add_document
- chunk_ids = batch_add_chunks(WebApiAuth, dataset_id, document_id, count)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(delete_chunks, WebApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids[i : i + 1]}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_delete_1k(self, WebApiAuth, add_document):
- chunks_num = 1_000
- dataset_id, document_id = add_document
- chunk_ids = batch_add_chunks(WebApiAuth, dataset_id, document_id, chunks_num)
-
- from time import sleep
-
- sleep(1)
-
- res = delete_chunks(WebApiAuth, dataset_id, document_id, {"chunk_ids": chunk_ids})
- assert res["code"] == 0
-
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["code"] == 0, res
- assert len(res["data"]["chunks"]) == 0, res
- assert res["data"]["total"] == 0, res
-
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message, remaining",
- [
- pytest.param({"chunk_ids": ["invalid_id"]}, 102, "rm_chunk deleted chunks 0, expect 1", 4, marks=pytest.mark.p3),
- pytest.param(lambda r: {"chunk_ids": r[:1]}, 0, "", 3, marks=pytest.mark.p3),
- pytest.param(lambda r: {"chunk_ids": r}, 0, "", 0, marks=pytest.mark.p1),
- pytest.param({"chunk_ids": []}, 0, "", 4, marks=pytest.mark.p3),
- ],
- )
- def test_basic_scenarios(self, WebApiAuth, add_chunks_func, payload, expected_code, expected_message, remaining):
- dataset_id, document_id, chunk_ids = add_chunks_func
- if callable(payload):
- payload = payload(chunk_ids)
- res = delete_chunks(WebApiAuth, dataset_id, document_id, payload)
- assert res["code"] == expected_code, res
- if res["code"] != 0:
- assert res["message"] == expected_message, res
-
- res = list_chunks(WebApiAuth, dataset_id, document_id)
- assert res["code"] == 0, res
- assert len(res["data"]["chunks"]) == remaining, res
- assert res["data"]["total"] == remaining, res
diff --git a/test/testcases/test_web_api/test_chunk_app/test_update_chunk.py b/test/testcases/test_web_api/test_chunk_app/test_update_chunk.py
deleted file mode 100644
index bfe99a40b1..0000000000
--- a/test/testcases/test_web_api/test_chunk_app/test_update_chunk.py
+++ /dev/null
@@ -1,220 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from random import randint
-from time import sleep
-
-import pytest
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from test_common import delete_document, list_chunks, update_chunk
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = update_chunk(invalid_auth, "dataset_id", "document_id", "chunk_id", {"content": "test"})
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-def _find_chunk(auth, dataset_id, document_id, chunk_id):
- res = list_chunks(auth, dataset_id, document_id, params={"id": chunk_id})
- assert res["code"] == 0, res
- return res["data"]["chunks"][0]
-
-
-class TestUpdateChunk:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"content": None}, 0, ""),
- ({"content": ""}, 102, "`content` is required"),
- pytest.param({"content": 1}, 100, "TypeError('expected string or bytes-like object')", marks=pytest.mark.skip),
- ({"content": "update chunk"}, 0, ""),
- ({"content": " "}, 102, "`content` is required"),
- ({"content": "\n!?。;!?\"'"}, 0, ""),
- ],
- )
- def test_content(self, WebApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- chunk_id = chunk_ids[0]
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_id, payload)
- assert res["code"] == expected_code, res
- if expected_code != 0:
- assert res["message"] == expected_message, res
- else:
- sleep(1)
- chunk = _find_chunk(WebApiAuth, dataset_id, document_id, chunk_id)
- if payload["content"] is not None:
- assert chunk["content"] == payload["content"]
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"important_keywords": ["a", "b", "c"]}, 0, ""),
- ({"important_keywords": [""]}, 0, ""),
- ({"important_keywords": [1]}, 100, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"important_keywords": ["a", "a"]}, 0, ""),
- ({"important_keywords": "abc"}, 102, "`important_keywords` should be a list"),
- ({"important_keywords": 123}, 102, "`important_keywords` should be a list"),
- ],
- )
- def test_important_keywords(self, WebApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- chunk_id = chunk_ids[0]
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_id, payload)
- assert res["code"] == expected_code, res
- if expected_code != 0:
- assert res["message"] == expected_message, res
- else:
- sleep(1)
- chunk = _find_chunk(WebApiAuth, dataset_id, document_id, chunk_id)
- assert chunk["important_keywords"] == payload["important_keywords"]
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"questions": ["a", "b", "c"]}, 0, ""),
- ({"questions": [""]}, 0, ""),
- ({"questions": [1]}, 100, "TypeError('sequence item 0: expected str instance, int found')"),
- ({"questions": ["a", "a"]}, 0, ""),
- ({"questions": "abc"}, 102, "`questions` should be a list"),
- ({"questions": 123}, 102, "`questions` should be a list"),
- ],
- )
- def test_questions(self, WebApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- chunk_id = chunk_ids[0]
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_id, payload)
- assert res["code"] == expected_code, res
- if expected_code != 0:
- assert res["message"] == expected_message, res
- else:
- sleep(1)
- chunk = _find_chunk(WebApiAuth, dataset_id, document_id, chunk_id)
- assert chunk["questions"] == [str(q).strip() for q in payload["questions"] if str(q).strip()]
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"available": True}, 0, ""),
- ({"available": 1}, 0, ""),
- ({"available": False}, 0, ""),
- ({"available": 0}, 0, ""),
- ],
- )
- def test_available(self, WebApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- chunk_id = chunk_ids[0]
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_id, payload)
- assert res["code"] == expected_code, res
- if expected_code != 0:
- assert res["message"] == expected_message, res
- else:
- sleep(1)
- chunk = _find_chunk(WebApiAuth, dataset_id, document_id, chunk_id)
- assert chunk["available"] == bool(payload["available"])
-
- @pytest.mark.p2
- def test_update_chunk_qa_multiline_content(self, WebApiAuth, add_chunks):
- dataset_id, document_id, chunk_ids = add_chunks
- payload = {"content": "Question line\nAnswer line"}
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == 0, res
-
- sleep(1)
- chunk = _find_chunk(WebApiAuth, dataset_id, document_id, chunk_ids[0])
- assert chunk["content"] == payload["content"], chunk
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "document_id, expected_code, expected_message",
- [
- ("invalid_doc_id", 102, "you don't own the document invalid_doc_id"),
- ],
- )
- def test_invalid_document_id_for_update(self, WebApiAuth, add_chunks, document_id, expected_code, expected_message):
- dataset_id, _, chunk_ids = add_chunks
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_ids[0], {"content": "test content"})
- assert res["code"] == expected_code
- assert expected_message in res["message"]
-
- @pytest.mark.p3
- def test_repeated_update_chunk(self, WebApiAuth, add_chunks):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_ids[0], {"content": "chunk test 1"})
- assert res["code"] == 0
-
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_ids[0], {"content": "chunk test 2"})
- assert res["code"] == 0
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- ({"unknown_key": "unknown_value"}, 0, ""),
- ({}, 0, ""),
- ],
- )
- def test_invalid_params(self, WebApiAuth, add_chunks, payload, expected_code, expected_message):
- dataset_id, document_id, chunk_ids = add_chunks
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_ids[0], payload)
- assert res["code"] == expected_code, res
- if expected_code != 0:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p3
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="issues/6554")
- def test_concurrent_update_chunk(self, WebApiAuth, add_chunks):
- count = 50
- dataset_id, document_id, chunk_ids = add_chunks
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- update_chunk,
- WebApiAuth,
- dataset_id,
- document_id,
- chunk_ids[randint(0, 3)],
- {"content": f"update chunk test {i}"},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- @pytest.mark.p3
- def test_update_chunk_to_deleted_document(self, WebApiAuth, add_chunks):
- dataset_id, document_id, chunk_ids = add_chunks
- delete_document(WebApiAuth, dataset_id, {"ids": [document_id]})
- res = update_chunk(WebApiAuth, dataset_id, document_id, chunk_ids[0], {"content": "test content"})
- assert res["code"] == 102, res
- assert res["message"] in [f"you don't own the document {document_id}", f"Can't find this chunk {chunk_ids[0]}"]
diff --git a/test/testcases/test_web_api/test_chunk_feedback/test_chunk_feedback_service.py b/test/testcases/test_web_api/test_chunk_feedback/test_chunk_feedback_service.py
deleted file mode 100644
index 79e3d2a66d..0000000000
--- a/test/testcases/test_web_api/test_chunk_feedback/test_chunk_feedback_service.py
+++ /dev/null
@@ -1,539 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-"""
-Tests for ChunkFeedbackService - adjusting chunk weights based on user feedback.
-
-Uses importlib to load chunk_feedback_service.py in isolation so that
-test/testcases/test_web_api/common.py (a test-helper module) does not shadow
-the project-level common/ package during collection.
-"""
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-from unittest.mock import MagicMock
-
-import pytest
-
-pytestmark = pytest.mark.p2
-
-_REPO_ROOT = Path(__file__).resolve().parents[4]
-
-
-def _load_feedback_module(monkeypatch):
- """Load chunk_feedback_service.py with lightweight stubs for its deps."""
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(_REPO_ROOT / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- constants_mod = ModuleType("common.constants")
- constants_mod.PAGERANK_FLD = "pagerank_fea"
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- settings_mod = ModuleType("common.settings")
- settings_mod.docStoreConn = MagicMock()
- # Non-ES engines accept pagerank_fea=0; tests below override for elasticsearch/opensearch.
- settings_mod.DOC_ENGINE = "infinity"
- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)
- common_pkg.settings = settings_mod
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
-
- rag_nlp_pkg = ModuleType("rag.nlp")
- rag_nlp_pkg.__path__ = []
- rag_nlp_pkg.search = SimpleNamespace(index_name=lambda tid: f"idx-{tid}")
- monkeypatch.setitem(sys.modules, "rag.nlp", rag_nlp_pkg)
-
- rag_nlp_search_mod = ModuleType("rag.nlp.search")
- rag_nlp_search_mod.index_name = lambda tid: f"idx-{tid}"
- monkeypatch.setitem(sys.modules, "rag.nlp.search", rag_nlp_search_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- spec = importlib.util.spec_from_file_location(
- "api.db.services.chunk_feedback_service",
- _REPO_ROOT / "api" / "db" / "services" / "chunk_feedback_service.py",
- )
- mod = importlib.util.module_from_spec(spec)
- monkeypatch.setitem(sys.modules, "api.db.services.chunk_feedback_service", mod)
- spec.loader.exec_module(mod)
-
- return mod, settings_mod
-
-
-def feedback_env(monkeypatch):
- """Provide (module, settings_stub) for chunk feedback tests."""
- return _load_feedback_module(monkeypatch)
-
-
-class TestFeedbackRowsFromReference:
- """Chunk id + kb resolution via _feedback_rows_from_reference (single pass)."""
-
- def test_empty_reference(self, feedback_env):
- mod, _ = feedback_env
- assert mod.ChunkFeedbackService._feedback_rows_from_reference({}) == []
- assert mod.ChunkFeedbackService._feedback_rows_from_reference(None) == []
-
- def test_reference_with_id_and_dataset(self, feedback_env):
- mod, _ = feedback_env
- reference = {
- "chunks": [
- {"id": "chunk1", "content": "test", "dataset_id": "kb1"},
- {"id": "chunk2", "content": "test2", "dataset_id": "kb1"},
- ]
- }
- rows = mod.ChunkFeedbackService._feedback_rows_from_reference(reference)
- assert [r[0] for r in rows] == ["chunk1", "chunk2"]
-
- def test_reference_with_chunk_id_and_kb_id(self, feedback_env):
- mod, _ = feedback_env
- reference = {
- "chunks": [
- {"chunk_id": "chunk1", "content": "test", "kb_id": "kb1"},
- {"chunk_id": "chunk2", "content": "test2", "kb_id": "kb1"},
- ]
- }
- rows = mod.ChunkFeedbackService._feedback_rows_from_reference(reference)
- assert [r[0] for r in rows] == ["chunk1", "chunk2"]
-
- def test_reference_skips_chunks_without_kb(self, feedback_env):
- mod, _ = feedback_env
- reference = {
- "chunks": [
- {"id": "chunk1", "dataset_id": "kb1"},
- {"id": "chunk2", "content": "no kb"},
- ]
- }
- rows = mod.ChunkFeedbackService._feedback_rows_from_reference(reference)
- assert [r[0] for r in rows] == ["chunk1"]
-
- def test_reference_with_no_chunks(self, feedback_env):
- mod, _ = feedback_env
- reference = {"doc_aggs": [{"doc_id": "doc1"}]}
- assert mod.ChunkFeedbackService._feedback_rows_from_reference(reference) == []
-
- def test_chunk_id_to_kb_map_matches_row_pairs(self, feedback_env):
- mod, _ = feedback_env
- reference = {
- "chunks": [
- {"id": "a", "dataset_id": "kb1"},
- {"chunk_id": "b", "kb_id": "kb2"},
- ]
- }
- rows = mod.ChunkFeedbackService._feedback_rows_from_reference(reference)
- assert {r[0]: r[1] for r in rows} == {"a": "kb1", "b": "kb2"}
-
-
-class TestUpdateChunkWeight:
- """Tests for update_chunk_weight method."""
-
- def test_update_weight_success(self, feedback_env):
- """Should update chunk weight successfully."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "mysql"
- mock_doc_store = MagicMock()
- mock_doc_store.adjust_chunk_pagerank_fea = None
- mock_doc_store.get.return_value = {"pagerank_fea": 10}
- mock_doc_store.update.return_value = True
- settings_mod.docStoreConn = mock_doc_store
-
- result = mod.ChunkFeedbackService.update_chunk_weight(tenant_id="tenant1", chunk_id="chunk1", kb_id="kb1", delta=1)
-
- assert result is True
- mock_doc_store.update.assert_called_once()
-
- def test_update_weight_chunk_not_found(self, feedback_env):
- """Should return False if chunk not found."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "mysql"
- mock_doc_store = MagicMock()
- mock_doc_store.adjust_chunk_pagerank_fea = None
- mock_doc_store.get.return_value = None
- settings_mod.docStoreConn = mock_doc_store
-
- result = mod.ChunkFeedbackService.update_chunk_weight(tenant_id="tenant1", chunk_id="chunk1", kb_id="kb1", delta=1)
-
- assert result is False
-
- def test_update_weight_clamp_max(self, feedback_env):
- """Should clamp weight to MAX_PAGERANK_WEIGHT."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "mysql"
- mock_doc_store = MagicMock()
- mock_doc_store.adjust_chunk_pagerank_fea = None
- mock_doc_store.get.return_value = {"pagerank_fea": mod.MAX_PAGERANK_WEIGHT}
- mock_doc_store.update.return_value = True
- settings_mod.docStoreConn = mock_doc_store
-
- mod.ChunkFeedbackService.update_chunk_weight(
- tenant_id="tenant1",
- chunk_id="chunk1",
- kb_id="kb1",
- delta=10, # Would exceed max
- )
-
- # Verify the new_value passed to update has clamped weight
- call_args = mock_doc_store.update.call_args
- new_value = call_args[0][1]
- assert new_value["pagerank_fea"] == mod.MAX_PAGERANK_WEIGHT
-
- def test_update_weight_clamp_min(self, feedback_env):
- """Should clamp weight to MIN_PAGERANK_WEIGHT."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "mysql"
- mock_doc_store = MagicMock()
- mock_doc_store.adjust_chunk_pagerank_fea = None
- mock_doc_store.get.return_value = {"pagerank_fea": 0}
- mock_doc_store.update.return_value = True
- settings_mod.docStoreConn = mock_doc_store
-
- mod.ChunkFeedbackService.update_chunk_weight(
- tenant_id="tenant1",
- chunk_id="chunk1",
- kb_id="kb1",
- delta=-10, # Would go below min
- )
-
- call_args = mock_doc_store.update.call_args
- new_value = call_args[0][1]
- assert new_value["pagerank_fea"] == mod.MIN_PAGERANK_WEIGHT
-
- def test_update_weight_elasticsearch_uses_atomic_adjust(self, feedback_env):
- """Elasticsearch uses script-based adjust (rank_feature zero handled in script)."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "elasticsearch"
- mock_doc_store = MagicMock()
- mock_adjust = MagicMock(return_value=True)
- mock_doc_store.adjust_chunk_pagerank_fea = mock_adjust
- settings_mod.docStoreConn = mock_doc_store
-
- assert mod.ChunkFeedbackService.update_chunk_weight(
- tenant_id="tenant1",
- chunk_id="chunk1",
- kb_id="kb1",
- delta=-1,
- )
- mock_adjust.assert_called_once_with(
- "chunk1",
- "idx-tenant1",
- "kb1",
- -1,
- mod.MIN_PAGERANK_WEIGHT,
- mod.MAX_PAGERANK_WEIGHT,
- )
-
- def test_update_weight_elasticsearch_forwards_row_id(self, feedback_env):
- """Elasticsearch adjust accepts and forwards row_id without TypeError."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "elasticsearch"
- mock_doc_store = MagicMock()
- mock_adjust = MagicMock(return_value=True)
- mock_doc_store.adjust_chunk_pagerank_fea = mock_adjust
- settings_mod.docStoreConn = mock_doc_store
-
- assert mod.ChunkFeedbackService.update_chunk_weight(
- tenant_id="tenant1",
- chunk_id="chunk1",
- kb_id="kb1",
- delta=-1,
- row_id=42,
- )
- mock_adjust.assert_called_once_with(
- "chunk1",
- "idx-tenant1",
- "kb1",
- -1,
- mod.MIN_PAGERANK_WEIGHT,
- mod.MAX_PAGERANK_WEIGHT,
- row_id=42,
- )
-
- def test_update_weight_opensearch_uses_atomic_adjust(self, feedback_env):
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "opensearch"
- mock_doc_store = MagicMock()
- mock_adjust = MagicMock(return_value=True)
- mock_doc_store.adjust_chunk_pagerank_fea = mock_adjust
- settings_mod.docStoreConn = mock_doc_store
-
- mod.ChunkFeedbackService.update_chunk_weight(
- tenant_id="tenant1",
- chunk_id="chunk1",
- kb_id="kb1",
- delta=-2,
- )
- mock_adjust.assert_called_once_with(
- "chunk1",
- "idx-tenant1",
- "kb1",
- -2,
- mod.MIN_PAGERANK_WEIGHT,
- mod.MAX_PAGERANK_WEIGHT,
- )
-
- def test_update_weight_opensearch_forwards_row_id(self, feedback_env):
- """OpenSearch adjust accepts and forwards row_id without TypeError."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "opensearch"
- mock_doc_store = MagicMock()
- mock_adjust = MagicMock(return_value=True)
- mock_doc_store.adjust_chunk_pagerank_fea = mock_adjust
- settings_mod.docStoreConn = mock_doc_store
-
- mod.ChunkFeedbackService.update_chunk_weight(
- tenant_id="tenant1",
- chunk_id="chunk1",
- kb_id="kb1",
- delta=-2,
- row_id=77,
- )
- mock_adjust.assert_called_once_with(
- "chunk1",
- "idx-tenant1",
- "kb1",
- -2,
- mod.MIN_PAGERANK_WEIGHT,
- mod.MAX_PAGERANK_WEIGHT,
- row_id=77,
- )
-
- def test_update_weight_infinity_uses_adjust_with_row_id(self, feedback_env):
- """Infinity path passes row_id to adjust_chunk_pagerank_fea."""
- mod, settings_mod = feedback_env
- settings_mod.DOC_ENGINE = "infinity"
- mock_doc_store = MagicMock()
- mock_adjust = MagicMock(return_value=True)
- mock_doc_store.adjust_chunk_pagerank_fea = mock_adjust
- settings_mod.docStoreConn = mock_doc_store
-
- ok = mod.ChunkFeedbackService.update_chunk_weight(
- tenant_id="tenant1",
- chunk_id="chunk1",
- kb_id="kb1",
- delta=1,
- row_id=42,
- )
- assert ok is True
- mock_adjust.assert_called_once_with(
- "chunk1",
- "idx-tenant1",
- "kb1",
- 1,
- mod.MIN_PAGERANK_WEIGHT,
- mod.MAX_PAGERANK_WEIGHT,
- row_id=42,
- )
-
-
-class TestApplyFeedback:
- """Tests for apply_feedback method."""
-
- def test_apply_feedback_disabled(self, feedback_env, monkeypatch):
- """Should return early when feature is disabled."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", False)
-
- result = mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference={"chunks": [{"id": "chunk1", "dataset_id": "kb1"}]}, is_positive=True)
-
- assert result["success_count"] == 0
- assert result["fail_count"] == 0
- assert result.get("disabled") is True
-
- def test_apply_positive_feedback(self, feedback_env, monkeypatch):
- """Relevance mode splits the per-event budget across chunks (equal when no scores)."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
- mock_update = MagicMock(return_value=True)
- monkeypatch.setattr(mod.ChunkFeedbackService, "update_chunk_weight", mock_update)
-
- reference = {
- "chunks": [
- {"id": "chunk1", "dataset_id": "kb1"},
- {"id": "chunk2", "dataset_id": "kb1"},
- ]
- }
- result = mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference=reference, is_positive=True)
-
- assert result["success_count"] == 1
- assert result["fail_count"] == 0
- assert mock_update.call_count == 1
- mock_update.assert_called_once_with("tenant1", "chunk1", "kb1", 1, row_id=None)
-
- def test_apply_negative_feedback(self, feedback_env, monkeypatch):
- """Should apply negative feedback with full budget when only one chunk."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
- mock_update = MagicMock(return_value=True)
- monkeypatch.setattr(mod.ChunkFeedbackService, "update_chunk_weight", mock_update)
-
- reference = {"chunks": [{"id": "chunk1", "dataset_id": "kb1"}]}
- result = mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference=reference, is_positive=False)
-
- assert result["success_count"] == 1
- mock_update.assert_called_with("tenant1", "chunk1", "kb1", -1, row_id=None)
-
- def test_apply_feedback_no_chunks(self, feedback_env, monkeypatch):
- """Should handle empty chunk list gracefully."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
-
- result = mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference={}, is_positive=True)
-
- assert result["success_count"] == 0
- assert result["fail_count"] == 0
- assert result["chunk_ids"] == []
-
- def test_apply_feedback_partial_failure(self, feedback_env, monkeypatch):
- """Should count failures correctly (uniform gives each chunk a unit)."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_WEIGHTING", "uniform")
- mock_update = MagicMock(side_effect=[True, False])
- monkeypatch.setattr(mod.ChunkFeedbackService, "update_chunk_weight", mock_update)
-
- reference = {
- "chunks": [
- {"id": "chunk1", "dataset_id": "kb1"},
- {"id": "chunk2", "dataset_id": "kb1"},
- ]
- }
- result = mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference=reference, is_positive=True)
-
- assert result["success_count"] == 1
- assert result["fail_count"] == 1
-
- def test_apply_positive_feedback_uniform_mode(self, feedback_env, monkeypatch):
- """uniform: each cited chunk gets the full increment (legacy)."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_WEIGHTING", "uniform")
- mock_update = MagicMock(return_value=True)
- monkeypatch.setattr(mod.ChunkFeedbackService, "update_chunk_weight", mock_update)
- reference = {
- "chunks": [
- {"id": "chunk1", "dataset_id": "kb1"},
- {"id": "chunk2", "dataset_id": "kb1"},
- ]
- }
- mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference=reference, is_positive=True)
- mock_update.assert_any_call("tenant1", "chunk1", "kb1", mod.UPVOTE_WEIGHT_INCREMENT, row_id=None)
- mock_update.assert_any_call("tenant1", "chunk2", "kb1", mod.UPVOTE_WEIGHT_INCREMENT, row_id=None)
-
- def test_apply_positive_feedback_relevance_weighted(self, feedback_env, monkeypatch):
- """Higher retrieval similarity receives a larger share of the budget."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_WEIGHTING", "relevance")
- mock_update = MagicMock(return_value=True)
- monkeypatch.setattr(mod.ChunkFeedbackService, "update_chunk_weight", mock_update)
- reference = {
- "chunks": [
- {"id": "a", "dataset_id": "kb1", "similarity": 0.9},
- {"id": "b", "dataset_id": "kb1", "similarity": 0.1},
- ]
- }
- mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference=reference, is_positive=True)
- mock_update.assert_called_once_with("tenant1", "a", "kb1", 1, row_id=None)
-
- def test_apply_feedback_passes_row_id_from_reference(self, feedback_env, monkeypatch):
- """row_id from retrieval results flows through to update_chunk_weight."""
- mod, _ = feedback_env
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_WEIGHTING", "relevance")
- mock_update = MagicMock(return_value=True)
- monkeypatch.setattr(mod.ChunkFeedbackService, "update_chunk_weight", mock_update)
- reference = {
- "chunks": [
- {"id": "c1", "dataset_id": "kb1", "similarity": 0.8, "row_id": 99},
- ]
- }
- mod.ChunkFeedbackService.apply_feedback(tenant_id="tenant1", reference=reference, is_positive=True)
- mock_update.assert_called_once_with("tenant1", "c1", "kb1", 1, row_id=99)
-
-
-class TestThumbFlipFeedback:
- """Verify that toggling thumbup↔thumbdown applies undo + new (two calls)."""
-
- @staticmethod
- def _simulate_feedback(mod, monkeypatch, reference, prior_thumb, new_thumb):
- """Reproduce the chat_api thumb-flip logic in isolation."""
- monkeypatch.setattr(mod, "CHUNK_FEEDBACK_ENABLED", True)
- mock_update = MagicMock(return_value=True)
- monkeypatch.setattr(mod.ChunkFeedbackService, "update_chunk_weight", mock_update)
-
- calls = []
-
- apply_chunk_feedback = False
- if new_thumb is True:
- apply_chunk_feedback = prior_thumb is not True
- else:
- apply_chunk_feedback = prior_thumb is not False
-
- if apply_chunk_feedback and reference:
- if isinstance(prior_thumb, bool) and prior_thumb != new_thumb:
- r = mod.ChunkFeedbackService.apply_feedback(
- tenant_id="t1",
- reference=reference,
- is_positive=not prior_thumb,
- )
- calls.append(("undo", r))
- r = mod.ChunkFeedbackService.apply_feedback(
- tenant_id="t1",
- reference=reference,
- is_positive=new_thumb is True,
- )
- calls.append(("new", r))
-
- return calls, mock_update
-
- def test_toggle_thumbup_to_thumbdown(self, feedback_env, monkeypatch):
- """thumbup→thumbdown: undo (+1→-1) then apply new (-1). Two calls."""
- mod, _ = feedback_env
- ref = {"chunks": [{"id": "c1", "dataset_id": "kb1"}]}
- calls, mock = self._simulate_feedback(mod, monkeypatch, ref, True, False)
- assert len(calls) == 2
- assert calls[0][0] == "undo"
- assert calls[1][0] == "new"
-
- def test_toggle_thumbdown_to_thumbup(self, feedback_env, monkeypatch):
- """thumbdown→thumbup: undo (-1→+1) then apply new (+1). Two calls."""
- mod, _ = feedback_env
- ref = {"chunks": [{"id": "c1", "dataset_id": "kb1"}]}
- calls, mock = self._simulate_feedback(mod, monkeypatch, ref, False, True)
- assert len(calls) == 2
- assert calls[0][0] == "undo"
- assert calls[1][0] == "new"
-
- def test_no_prior_to_thumbup(self, feedback_env, monkeypatch):
- """None→thumbup: single apply, no undo."""
- mod, _ = feedback_env
- ref = {"chunks": [{"id": "c1", "dataset_id": "kb1"}]}
- calls, mock = self._simulate_feedback(mod, monkeypatch, ref, None, True)
- assert len(calls) == 1
- assert calls[0][0] == "new"
-
- def test_same_thumb_no_op(self, feedback_env, monkeypatch):
- """thumbup→thumbup: no feedback at all (apply_chunk_feedback is False)."""
- mod, _ = feedback_env
- ref = {"chunks": [{"id": "c1", "dataset_id": "kb1"}]}
- calls, mock = self._simulate_feedback(mod, monkeypatch, ref, True, True)
- assert len(calls) == 0
diff --git a/test/testcases/test_web_api/test_common.py b/test/testcases/test_web_api/test_common.py
deleted file mode 100644
index 5fa53baa35..0000000000
--- a/test/testcases/test_web_api/test_common.py
+++ /dev/null
@@ -1,647 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import json
-import os
-from pathlib import Path
-from uuid import uuid4
-
-import requests
-from configs import HOST_ADDRESS, VERSION
-from requests_toolbelt import MultipartEncoder
-from utils.file_utils import create_txt_file
-
-HEADERS = {"Content-Type": "application/json"}
-
-DATASETS_URL = f"/api/{VERSION}/datasets"
-CHUNK_APP_URL = f"/{VERSION}/chunk"
-CHUNK_API_URL = f"/api/{VERSION}/datasets/{{dataset_id}}/documents/{{document_id}}/chunks"
-# SESSION_WITH_CHAT_ASSISTANT_API_URL = "/api/v1/chats/{chat_id}/sessions"
-# SESSION_WITH_AGENT_API_URL = "/api/v1/agents/{agent_id}/sessions"
-MEMORY_API_URL = f"/api/{VERSION}/memories"
-MESSAGE_API_URL = f"/api/{VERSION}/messages"
-API_APP_URL = f"/{VERSION}/api"
-SYSTEM_APP_URL = f"/{VERSION}/system"
-SYSTEM_API_URL = f"/api/{VERSION}/system"
-LLM_APP_URL = f"/{VERSION}/llm"
-PLUGIN_APP_URL = f"/api/{VERSION}/plugin"
-SEARCHES_URL = f"/api/{VERSION}/searches"
-CHATS_URL = f"/api/{VERSION}/chats"
-
-
-def _http_debug_enabled():
- return os.getenv("TEST_HTTP_DEBUG") == "1"
-
-
-def _redact_payload(payload):
- if not isinstance(payload, dict):
- return payload
- redacted = {}
- for key, value in payload.items():
- if any(token in key.lower() for token in ("api_key", "password", "token", "secret", "authorization")):
- redacted[key] = "***redacted***"
- else:
- redacted[key] = value
- return redacted
-
-
-def _log_http_debug(method, url, req_id, payload, status, text, resp_json, elapsed_ms):
- if not _http_debug_enabled():
- return
- payload_summary = _redact_payload(payload)
- print(f"[HTTP DEBUG] {method} {url} req_id={req_id} elapsed_ms={elapsed_ms:.1f}")
- print(f"[HTTP DEBUG] request_payload={json.dumps(payload_summary, default=str)}")
- print(f"[HTTP DEBUG] status={status}")
- print(f"[HTTP DEBUG] response_text={text}")
- print(f"[HTTP DEBUG] response_json={json.dumps(resp_json, default=str) if resp_json is not None else None}")
-
-
-def api_stats(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{API_APP_URL}/stats", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-# SYSTEM APP
-def system_new_token(auth, payload=None, *, headers=HEADERS, data=None):
- res = requests.post(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/tokens", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def system_token_list(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/tokens", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def system_delete_token(auth, token, *, headers=HEADERS):
- res = requests.delete(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/tokens/{token}", headers=headers, auth=auth)
- return res.json()
-
-
-def system_status(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/status", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def system_version(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/version", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def system_config(auth=None, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{SYSTEM_API_URL}/config", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-# LLM APP
-def llm_factories(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{LLM_APP_URL}/factories", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def llm_list(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{LLM_APP_URL}/list", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-# PLUGIN APP
-def plugin_llm_tools(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{PLUGIN_APP_URL}/tools", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-# SEARCH APP
-def search_create(auth, payload=None, *, headers=HEADERS, data=None):
- res = requests.post(url=f"{HOST_ADDRESS}{SEARCHES_URL}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def search_update(auth, search_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.put(url=f"{HOST_ADDRESS}{SEARCHES_URL}/{search_id}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def search_detail(auth, search_id, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{SEARCHES_URL}/{search_id}", headers=headers, auth=auth)
- return res.json()
-
-
-def search_list(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{SEARCHES_URL}", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def search_rm(auth, search_id, *, headers=HEADERS):
- res = requests.delete(url=f"{HOST_ADDRESS}{SEARCHES_URL}/{search_id}", headers=headers, auth=auth)
- return res.json()
-
-
-# CHAT APP
-def create_chat(auth, payload=None, *, headers=HEADERS, data=None):
- if payload is None:
- payload = {}
- res = requests.post(url=f"{HOST_ADDRESS}{CHATS_URL}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def list_chats(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{CHATS_URL}", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def delete_chat(auth, chat_id, *, headers=HEADERS):
- res = requests.delete(url=f"{HOST_ADDRESS}{CHATS_URL}/{chat_id}", headers=headers, auth=auth)
- return res.json()
-
-
-def delete_chats(auth, payload=None, *, headers=HEADERS, data=None):
- if payload is None:
- payload = {"delete_all": True}
- res = requests.delete(url=f"{HOST_ADDRESS}{CHATS_URL}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def batch_create_chats(auth, num):
- ids = []
- for i in range(num):
- res = create_chat(auth, {"name": f"chat_{uuid4().hex}_{i}"})
- ids.append(res["data"]["id"])
- return ids
-
-
-# KB APP
-def create_dataset(auth, payload=None, *, headers=HEADERS, data=None):
- res = requests.post(url=f"{HOST_ADDRESS}{DATASETS_URL}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def list_datasets(auth, params=None, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def update_dataset(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.put(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def delete_datasets(auth, payload=None, *, headers=HEADERS, data=None):
- """
- Delete datasets.
- The endpoint is DELETE /api/{VERSION}/datasets with payload {"ids": [...]}
- This is the standard SDK REST API endpoint for dataset deletion.
- """
- res = requests.delete(url=f"{HOST_ADDRESS}{DATASETS_URL}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def detail_kb(auth, dataset_id, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}", headers=headers, auth=auth)
- return res.json()
-
-
-def kb_get_meta(auth, dataset_ids, *, headers=HEADERS):
- params = {"dataset_ids": dataset_ids}
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/metadata/flattened", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def kb_basic_info(auth, dataset_id, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/ingestions/summary", headers=headers, auth=auth)
- return res.json()
-
-
-def kb_update_metadata_setting(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.put(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/metadata/config", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def kb_list_pipeline_logs(auth, dataset_id, params=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/ingestions"
- res = requests.get(url=url, headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def kb_list_pipeline_dataset_logs(auth, dataset_id, params=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/ingestions"
- res = requests.get(url=url, headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def kb_pipeline_log_detail(auth, dataset_id, log_id, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/ingestions/{log_id}", headers=headers, auth=auth)
- return res.json()
-
-
-def list_tags_from_kbs(auth, dataset_ids, *, headers=HEADERS):
- params = {"dataset_ids": dataset_ids}
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/tags/aggregation", headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def list_tags(auth, dataset_id, *, headers=HEADERS):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/tags", headers=headers, auth=auth)
- return res.json()
-
-
-def rm_tags(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.delete(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/tags", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def rename_tags(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.put(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/tags", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def batch_create_datasets(auth, num):
- ids = []
- for i in range(num):
- res = create_dataset(auth, {"name": f"kb_{i}"})
- ids.append(res["data"]["id"])
- return ids
-
-
-# DOCUMENT APP
-def upload_documents(auth, payload=None, files_path=None, *, filename_override=None):
- # New endpoint: /api/v1/datasets/{kb_id}/documents
- kb_id = payload.get("kb_id") if payload else None
- url = f"{HOST_ADDRESS}/api/{VERSION}/datasets/{kb_id}/documents"
-
- if files_path is None:
- files_path = []
-
- fields = []
- file_objects = []
- try:
- # Note: kb_id is now in the URL path, not in the form data
- if payload:
- for k, v in payload.items():
- if k != "kb_id": # Skip kb_id as it's in the URL
- fields.append((k, str(v)))
-
- for fp in files_path:
- p = Path(fp)
- f = p.open("rb")
- filename = filename_override if filename_override is not None else p.name
- fields.append(("file", (filename, f)))
- file_objects.append(f)
- m = MultipartEncoder(fields=fields)
-
- res = requests.post(
- url=url,
- headers={"Content-Type": m.content_type},
- auth=auth,
- data=m,
- )
- return res.json()
- finally:
- for f in file_objects:
- f.close()
-
-
-def upload_info(auth, files_path=None, *, url=None):
- """
- Call the /api/v1/documents/upload endpoint to get upload info.
- This is used to get file metadata before actually uploading to a dataset.
-
- Args:
- auth: Authentication object
- files_path: List of file paths to upload (optional)
- url: URL to fetch file from (optional, can be used alone or with files_path to test mixed input rejection)
-
- Returns:
- Response JSON with upload info
- """
- url_endpoint = f"{HOST_ADDRESS}/api/{VERSION}/documents/upload"
-
- fields = []
- file_objects = []
- try:
- if files_path:
- for fp in files_path:
- p = Path(fp)
- f = p.open("rb")
- fields.append(("file", (p.name, f)))
- file_objects.append(f)
-
- # Add url as query parameter if provided
- if url:
- url_endpoint = f"{url_endpoint}?url={url}"
-
- # Handle empty fields (no files) - create empty MultipartEncoder
- if not fields:
- fields = [("empty", ("", ""))]
-
- m = MultipartEncoder(fields=fields)
-
- res = requests.post(
- url=url_endpoint,
- headers={"Content-Type": m.content_type},
- auth=auth,
- data=m,
- )
- return res.json()
- finally:
- for f in file_objects:
- f.close()
-
-
-def create_document(auth, payload=None, *, headers=HEADERS, data=None):
- kb_id = payload.get("kb_id") if payload else None
- request_payload = dict(payload or {})
- request_payload.pop("kb_id", None)
- res = requests.post(
- url=f"{HOST_ADDRESS}{DATASETS_URL}/{kb_id}/documents?type=empty",
- headers=headers,
- auth=auth,
- json=request_payload,
- data=data,
- )
- return res.json()
-
-
-def list_documents(auth, params=None, payload=None, *, headers=HEADERS, data=None):
- kb_id = params.get("kb_id") if params else None
- url = f"{HOST_ADDRESS}{DATASETS_URL}/{kb_id}/documents"
- if payload is None:
- payload = {}
- res = requests.get(url=url, headers=headers, auth=auth, params=params, json=payload, data=data)
- return res.json()
-
-
-def delete_document(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- # New API: DELETE /api/v1/datasets/<dataset_id>/documents
- url = f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/documents"
- res = requests.delete(url=url, headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def parse_documents(auth, payload=None, *, headers=HEADERS, data=None):
- res = requests.post(url=f"{HOST_ADDRESS}/api/{VERSION}/documents/ingest", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def document_filter(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/documents?type=filter", params=payload, headers=headers, auth=auth, data=data)
- return res.json()
-
-
-def document_infos(auth, dataset_id, params=None, payload=None, *, headers=HEADERS, data=None):
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/documents", params=params, json=payload, headers=headers, auth=auth, data=data)
- return res.json()
-
-
-def document_metadata_summary(auth, payload=None, *, headers=HEADERS, data=None):
- dataset_id = (payload or {}).get("kb_id")
- doc_ids = (payload or {}).get("doc_ids")
- if not dataset_id:
- return {"code": 101, "message": "KB ID is required"}
- params = {}
- if doc_ids:
- params["doc_ids"] = ",".join(doc_ids)
- res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/metadata/summary", headers=headers, auth=auth, params=params, data=data)
- return res.json()
-
-
-def document_get(auth, document_id, *, headers=HEADERS, data=None):
- res = requests.get(url=f"{HOST_ADDRESS}/api/{VERSION}/documents/{document_id}/preview", headers=headers, auth=auth, data=data)
- return res
-
-
-def document_download(auth, attachment_id, *, ext="markdown", headers=HEADERS, data=None):
- res = requests.get(
- url=f"{HOST_ADDRESS}/api/{VERSION}/agents/attachments/{attachment_id}/download",
- headers=headers,
- auth=auth,
- params={"ext": ext},
- data=data,
- )
- return res
-
-
-def document_metadata_update(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- """New unified API for updating document metadata.
-
- Uses PATCH method at /api/v1/datasets/{dataset_id}/documents/metadatas
- """
- res = requests.patch(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/documents/metadatas", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def document_update_metadata_setting(auth, dataset_id, doc_id, payload=None, *, headers=HEADERS, data=None):
- res = requests.put(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/documents/{doc_id}/metadata/config", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def document_change_status(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
- """
- Batch update document status within a dataset.
-
- Args:
- auth: Authentication credentials
- dataset_id: ID of the dataset
- payload: Request body containing doc_ids and status
- """
- res = requests.post(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/documents/batch-update-status", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def document_update(auth, dataset_id, doc_id, payload=None, *, headers=HEADERS, data=None):
- """Update document via PATCH /api/v1/datasets/<dataset_id>/documents/<doc_id>"""
- res = requests.patch(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/documents/{doc_id}", headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def document_thumbnails(auth, params=None, *, headers=HEADERS, data=None):
- """Get document thumbnails.
-
- Args:
- auth: Authentication object
- params: Query parameters (e.g., {"doc_ids": ["doc1", "doc2"]})
- """
- res = requests.get(url=f"{HOST_ADDRESS}/api/v1/thumbnails", params=params, headers=headers, auth=auth, data=data)
- return res.json()
-
-
-def bulk_upload_documents(auth, kb_id, num, tmp_path):
- fps = []
- for i in range(num):
- fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
- fps.append(fp)
-
- res = upload_documents(auth, {"kb_id": kb_id}, fps)
- document_ids = []
- for document in res["data"]:
- document_ids.append(document["id"])
- return document_ids
-
-
-# CHUNK MANAGEMENT
-def add_chunk(auth, dataset_id, document_id, payload=None, *, headers=HEADERS, data=None):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.post(url=url, headers=headers, auth=auth, json=payload, data=data)
- return res.json()
-
-
-def list_chunks(auth, dataset_id, document_id, params=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.get(url=url, headers=headers, auth=auth, params=params)
- return res.json()
-
-
-def get_chunk(auth, dataset_id, document_id, chunk_id, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}/{chunk_id}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.get(url=url, headers=headers, auth=auth)
- return res.json()
-
-
-def update_chunk(auth, dataset_id, document_id, chunk_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}/{chunk_id}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.patch(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def switch_chunks(auth, dataset_id, document_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.patch(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def delete_chunks(auth, dataset_id, document_id, payload=None, *, headers=HEADERS):
- url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
- res = requests.delete(url=url, headers=headers, auth=auth, json=payload)
- return res.json()
-
-
-def batch_add_chunks(auth, dataset_id, document_id, num):
- chunk_ids = []
- for i in range(num):
- res = add_chunk(auth, dataset_id, document_id, {"content": f"chunk test {i}"})
- chunk_ids.append(res["data"]["chunk"]["id"])
- return chunk_ids
-
-
-# MEMORY APP
-def create_memory(auth, payload=None):
- url = f"{HOST_ADDRESS}{MEMORY_API_URL}"
- res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def update_memory(auth, memory_id: str, payload=None):
- url = f"{HOST_ADDRESS}{MEMORY_API_URL}/{memory_id}"
- res = requests.put(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def delete_memory(auth, memory_id: str):
- url = f"{HOST_ADDRESS}{MEMORY_API_URL}/{memory_id}"
- res = requests.delete(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def list_memory(auth, params=None):
- url = f"{HOST_ADDRESS}{MEMORY_API_URL}"
- if params:
- query_parts = []
- for key, value in params.items():
- if isinstance(value, list):
- for item in value:
- query_parts.append(f"{key}={item}")
- else:
- query_parts.append(f"{key}={value}")
- query_string = "&".join(query_parts)
- url = f"{url}?{query_string}"
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def get_memory_config(auth, memory_id: str):
- url = f"{HOST_ADDRESS}{MEMORY_API_URL}/{memory_id}/config"
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def list_memory_message(auth, memory_id, params=None):
- url = f"{HOST_ADDRESS}{MEMORY_API_URL}/{memory_id}"
- if params:
- query_parts = []
- for key, value in params.items():
- if isinstance(value, list):
- for item in value:
- query_parts.append(f"{key}={item}")
- else:
- query_parts.append(f"{key}={value}")
- query_string = "&".join(query_parts)
- url = f"{url}?{query_string}"
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def add_message(auth, payload=None):
- url = f"{HOST_ADDRESS}{MESSAGE_API_URL}"
- res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def forget_message(auth, memory_id: str, message_id: int):
- url = f"{HOST_ADDRESS}{MESSAGE_API_URL}/{memory_id}:{message_id}"
- res = requests.delete(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def update_message_status(auth, memory_id: str, message_id: int, status: bool):
- url = f"{HOST_ADDRESS}{MESSAGE_API_URL}/{memory_id}:{message_id}"
- payload = {"status": status}
- res = requests.put(url=url, headers=HEADERS, auth=auth, json=payload)
- return res.json()
-
-
-def search_message(auth, params=None):
- url = f"{HOST_ADDRESS}{MESSAGE_API_URL}/search"
- if params:
- query_parts = []
- for key, value in params.items():
- if isinstance(value, list):
- for item in value:
- query_parts.append(f"{key}={item}")
- else:
- query_parts.append(f"{key}={value}")
- query_string = "&".join(query_parts)
- url = f"{url}?{query_string}"
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def get_recent_message(auth, params=None):
- url = f"{HOST_ADDRESS}{MESSAGE_API_URL}"
- if params:
- query_parts = []
- for key, value in params.items():
- if isinstance(value, list):
- for item in value:
- query_parts.append(f"{key}={item}")
- else:
- query_parts.append(f"{key}={value}")
- query_string = "&".join(query_parts)
- url = f"{url}?{query_string}"
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
-
-
-def get_message_content(auth, memory_id: str, message_id: int):
- url = f"{HOST_ADDRESS}{MESSAGE_API_URL}/{memory_id}:{message_id}/content"
- res = requests.get(url=url, headers=HEADERS, auth=auth)
- return res.json()
diff --git a/test/testcases/test_web_api/test_connector_app/test_connector_oauth_contract.py b/test/testcases/test_web_api/test_connector_app/test_connector_oauth_contract.py
deleted file mode 100644
index a2d647ebdb..0000000000
--- a/test/testcases/test_web_api/test_connector_app/test_connector_oauth_contract.py
+++ /dev/null
@@ -1,150 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-
-import pytest
-import requests
-
-from configs import HOST_ADDRESS, VERSION
-
-CONNECTOR_BASE_URL = f"{HOST_ADDRESS}/api/{VERSION}/connectors"
-LLM_API_KEY_URL = f"{HOST_ADDRESS}/{VERSION}/llm/set_api_key"
-LANGFUSE_API_KEY_URL = f"{HOST_ADDRESS}/api/{VERSION}/langfuse/api-key"
-
-pytestmark = pytest.mark.p3
-
-
[email protected](autouse=True)
-def _require_oauth_env(require_env_flag):
- require_env_flag("RAGFLOW_E2E_OAUTH")
-
-
-def _skip_unless_provider(allowed):
- provider = os.getenv("RAGFLOW_OAUTH_PROVIDER")
- if provider and provider not in allowed:
- pytest.skip(f"RAGFLOW_OAUTH_PROVIDER={provider} not in {sorted(allowed)}")
-
-
-def _assert_unauthorized(payload):
- assert payload["code"] == 401, payload
- assert "Unauthorized" in payload["message"], payload
-
-
-def _assert_unauthorized_response(res, *, allow_405=False):
- if allow_405 and res.status_code == 405:
- pytest.skip("method not supported in this deployment")
- content_type = res.headers.get("Content-Type", "")
- payload = None
- if "json" in content_type:
- payload = res.json()
- else:
- try:
- payload = res.json()
- except ValueError:
- assert False, f"Expected JSON response, status={res.status_code}, content_type={content_type}"
- _assert_unauthorized(payload)
-
-
-def _assert_callback_response(res, expected_fragment):
- assert res.status_code in {200, 302}, {"status": res.status_code, "headers": dict(res.headers)}
- if res.status_code == 200:
- assert "text/html" in res.headers.get("Content-Type", ""), res.headers
- assert expected_fragment in res.text
- else:
- location = res.headers.get("Location", "")
- assert location, res.headers
- markers = ("error", "oauth", "callback", "state", "code")
- assert any(marker in location for marker in markers), location
-
-
-def test_google_oauth_start_requires_auth():
- _skip_unless_provider({"google", "google-drive", "gmail"})
- res = requests.post(f"{CONNECTOR_BASE_URL}/google/oauth/web/start")
- _assert_unauthorized(res.json())
-
-
-def test_google_oauth_start_missing_credentials(WebApiAuth):
- _skip_unless_provider({"google", "google-drive", "gmail"})
- res = requests.post(f"{CONNECTOR_BASE_URL}/google/oauth/web/start", auth=WebApiAuth, json={})
- payload = res.json()
- assert payload["code"] == 101, payload
- assert "required argument are missing" in payload["message"], payload
- assert "credentials" in payload["message"], payload
-
-
[email protected]("path", ["google-drive/oauth/web/callback", "gmail/oauth/web/callback"])
-def test_google_oauth_callback_missing_state(path):
- _skip_unless_provider({"google", "google-drive", "gmail"})
- res = requests.get(f"{CONNECTOR_BASE_URL}/{path}", allow_redirects=False)
- _assert_callback_response(res, "Missing OAuth state parameter.")
-
-
-def test_google_oauth_result_missing_flow_id(WebApiAuth):
- _skip_unless_provider({"google", "google-drive", "gmail"})
- res = requests.post(
- f"{CONNECTOR_BASE_URL}/google/oauth/web/result",
- params={"type": "google-drive"},
- auth=WebApiAuth,
- json={},
- )
- payload = res.json()
- assert payload["code"] == 101, payload
- assert "required argument are missing" in payload["message"], payload
- assert "flow_id" in payload["message"], payload
-
-
-def test_box_oauth_start_missing_params(WebApiAuth):
- _skip_unless_provider({"box"})
- res = requests.post(f"{CONNECTOR_BASE_URL}/box/oauth/web/start", auth=WebApiAuth, json={})
- payload = res.json()
- assert payload["code"] == 101, payload
- assert "client_id" in payload["message"], payload
- assert "client_secret" in payload["message"], payload
-
-
-def test_box_oauth_callback_missing_state():
- _skip_unless_provider({"box"})
- res = requests.get(f"{CONNECTOR_BASE_URL}/box/oauth/web/callback", allow_redirects=False)
- _assert_callback_response(res, "Missing OAuth parameters.")
-
-
-def test_box_oauth_result_missing_flow_id(WebApiAuth):
- _skip_unless_provider({"box"})
- res = requests.post(f"{CONNECTOR_BASE_URL}/box/oauth/web/result", auth=WebApiAuth, json={})
- payload = res.json()
- assert payload["code"] == 101, payload
- assert "required argument are missing" in payload["message"], payload
- assert "flow_id" in payload["message"], payload
-
-
-def test_langfuse_api_key_requires_auth():
- res = requests.post(LANGFUSE_API_KEY_URL, json={})
- _assert_unauthorized_response(res)
-
-
-def test_langfuse_api_key_requires_auth_get():
- res = requests.get(LANGFUSE_API_KEY_URL)
- _assert_unauthorized_response(res, allow_405=True)
-
-
-def test_langfuse_api_key_requires_auth_put():
- res = requests.put(LANGFUSE_API_KEY_URL, json={})
- _assert_unauthorized_response(res, allow_405=True)
-
-
-def test_llm_set_api_key_requires_auth():
- res = requests.post(LLM_API_KEY_URL, json={})
- _assert_unauthorized_response(res)
diff --git a/test/testcases/test_web_api/test_connector_app/test_connector_routes_unit.py b/test/testcases/test_web_api/test_connector_app/test_connector_routes_unit.py
deleted file mode 100644
index 413c9771a8..0000000000
--- a/test/testcases/test_web_api/test_connector_app/test_connector_routes_unit.py
+++ /dev/null
@@ -1,799 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import json
-import sys
-from enum import StrEnum
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _Args(dict):
- def get(self, key, default=None, type=None):
- value = super().get(key, default)
- if type is None:
- return value
- try:
- return type(value)
- except (TypeError, ValueError):
- return default
-
- def to_dict(self, flat=True):
- return dict(self)
-
-
-class _FakeResponse:
- def __init__(self, body, status_code):
- self.body = body
- self.status_code = status_code
- self.headers = {}
-
-
-class _FakeConnectorRecord:
- def __init__(self, payload):
- self._payload = payload
-
- def to_dict(self):
- return dict(self._payload)
-
-
-class _FakeCredentials:
- def __init__(self, raw='{"refresh_token":"rt","access_token":"at"}'):
- self._raw = raw
-
- def to_json(self):
- return self._raw
-
-
-class _FakeFlow:
- def __init__(self, client_config, scopes):
- self.client_config = client_config
- self.scopes = scopes
- self.redirect_uri = None
- self.credentials = _FakeCredentials()
- self.auth_kwargs = None
- self.token_code = None
- self.token_code_verifier = None
- self.code_verifier = "fake-code-verifier"
-
- def authorization_url(self, **kwargs):
- self.auth_kwargs = dict(kwargs)
- return f"https://oauth.example/{kwargs['state']}", kwargs["state"]
-
- def fetch_token(self, code, code_verifier=None):
- self.token_code = code
- self.token_code_verifier = code_verifier
-
-
-class _FakeBoxToken:
- def __init__(self, access_token, refresh_token):
- self.access_token = access_token
- self.refresh_token = refresh_token
-
-
-class _FakeBoxOAuth:
- def __init__(self, config):
- self.config = config
- self.exchange_code = None
-
- def get_authorize_url(self, options):
- return f"https://box.example/auth?state={options.state}&redirect={options.redirect_uri}"
-
- def get_tokens_authorization_code_grant(self, code):
- self.exchange_code = code
-
- def retrieve_token(self):
- return _FakeBoxToken("box-access", "box-refresh")
-
-
-class _FakeRedis:
- def __init__(self):
- self.store = {}
- self.set_calls = []
- self.deleted = []
-
- def get(self, key):
- return self.store.get(key)
-
- def set_obj(self, key, obj, ttl):
- self.set_calls.append((key, obj, ttl))
- self.store[key] = json.dumps(obj)
-
- def delete(self, key):
- self.deleted.append(key)
- self.store.pop(key, None)
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _set_request(module, *, args=None, json_body=None):
- module.request = SimpleNamespace(
- args=_Args(args or {}),
- json=_AwaitableValue({} if json_body is None else json_body),
- )
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_connector_app(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.current_user = SimpleNamespace(id="tenant-1")
- apps_mod.login_required = lambda fn: fn
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- db_mod = ModuleType("api.db")
- db_mod.InputType = SimpleNamespace(POLL="POLL")
- monkeypatch.setitem(sys.modules, "api.db", db_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- connector_service_mod = ModuleType("api.db.services.connector_service")
-
- class _StubConnectorService:
- @staticmethod
- def update_by_id(*_args, **_kwargs):
- return True
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def get_by_id(_connector_id):
- return True, _FakeConnectorRecord({"id": _connector_id})
-
- @staticmethod
- def list(_tenant_id):
- return []
-
- @staticmethod
- def accessible(*_args, **_kwargs):
- return True
-
- @staticmethod
- def cancel_tasks(*_args, **_kwargs):
- return True
-
- @staticmethod
- def rebuild(*_args, **_kwargs):
- return None
-
- @staticmethod
- def delete_by_id(*_args, **_kwargs):
- return True
-
- class _StubSyncLogsService:
- @staticmethod
- def list_sync_tasks(*_args, **_kwargs):
- return [], 0
-
- connector_service_mod.ConnectorService = _StubConnectorService
- connector_service_mod.SyncLogsService = _StubSyncLogsService
- monkeypatch.setitem(sys.modules, "api.db.services.connector_service", connector_service_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- async def _get_request_json():
- return {}
-
- api_utils_mod.get_request_json = _get_request_json
- api_utils_mod.get_json_result = lambda data=None, message="", code=0: {
- "code": code,
- "message": message,
- "data": data,
- }
- api_utils_mod.get_data_error_result = lambda message="", code=400, data=None: {
- "code": code,
- "message": message,
- "data": data,
- }
- api_utils_mod.validate_request = lambda *_args, **_kwargs: lambda fn: fn
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- constants_mod = ModuleType("common.constants")
- constants_mod.RetCode = SimpleNamespace(
- ARGUMENT_ERROR=101,
- SERVER_ERROR=500,
- RUNNING=102,
- PERMISSION_ERROR=403,
- AUTHENTICATION_ERROR=109,
- )
- constants_mod.TaskStatus = SimpleNamespace(SCHEDULE="schedule", CANCEL="cancel")
-
- class _FileSource(StrEnum):
- LOCAL = ""
- RSS = "rss"
-
- constants_mod.FileSource = _FileSource
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- config_mod = ModuleType("common.data_source.config")
- config_mod.GOOGLE_DRIVE_WEB_OAUTH_REDIRECT_URI = "https://example.com/drive"
- config_mod.GMAIL_WEB_OAUTH_REDIRECT_URI = "https://example.com/gmail"
- config_mod.BOX_WEB_OAUTH_REDIRECT_URI = "https://example.com/box"
- config_mod.DocumentSource = SimpleNamespace(GMAIL="gmail", GOOGLE_DRIVE="google-drive")
- monkeypatch.setitem(sys.modules, "common.data_source.config", config_mod)
-
- google_constants_mod = ModuleType("common.data_source.google_util.constant")
- google_constants_mod.WEB_OAUTH_POPUP_TEMPLATE = (
- "<html><head><title>{title}</title></head><body><h1>{heading}</h1><p>{message}</p><script>{payload_json}</script><script>{auto_close}</script></body></html>"
- )
- google_constants_mod.GOOGLE_SCOPES = {
- config_mod.DocumentSource.GMAIL: ["scope-gmail"],
- config_mod.DocumentSource.GOOGLE_DRIVE: ["scope-drive"],
- }
- monkeypatch.setitem(sys.modules, "common.data_source.google_util.constant", google_constants_mod)
-
- data_source_mod = ModuleType("common.data_source")
-
- def _stub_build_connector_for_source(_source, _config):
- raise NotImplementedError("patch build_connector_for_source in test")
-
- data_source_mod.build_connector_for_source = _stub_build_connector_for_source
- monkeypatch.setitem(sys.modules, "common.data_source", data_source_mod)
-
- data_source_exceptions_mod = ModuleType("common.data_source.exceptions")
-
- class _ConnectorMissingCredentialError(Exception):
- pass
-
- class _ConnectorValidationError(Exception):
- pass
-
- data_source_exceptions_mod.ConnectorMissingCredentialError = _ConnectorMissingCredentialError
- data_source_exceptions_mod.ConnectorValidationError = _ConnectorValidationError
- monkeypatch.setitem(sys.modules, "common.data_source.exceptions", data_source_exceptions_mod)
-
- misc_mod = ModuleType("common.misc_utils")
- misc_mod.get_uuid = lambda: "uuid-from-helper"
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_mod)
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = [str(repo_root / "rag")]
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
-
- rag_utils_pkg = ModuleType("rag.utils")
- rag_utils_pkg.__path__ = [str(repo_root / "rag" / "utils")]
- monkeypatch.setitem(sys.modules, "rag.utils", rag_utils_pkg)
-
- redis_mod = ModuleType("rag.utils.redis_conn")
- redis_mod.REDIS_CONN = _FakeRedis()
- monkeypatch.setitem(sys.modules, "rag.utils.redis_conn", redis_mod)
-
- quart_mod = ModuleType("quart")
- quart_mod.request = SimpleNamespace(args=_Args(), json=_AwaitableValue({}))
-
- async def _make_response(body, status_code):
- return _FakeResponse(body, status_code)
-
- quart_mod.make_response = _make_response
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- google_pkg = ModuleType("google_auth_oauthlib")
- google_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "google_auth_oauthlib", google_pkg)
-
- google_flow_mod = ModuleType("google_auth_oauthlib.flow")
-
- class _StubFlow:
- @classmethod
- def from_client_config(cls, client_config, scopes):
- return _FakeFlow(client_config, scopes)
-
- google_flow_mod.Flow = _StubFlow
- monkeypatch.setitem(sys.modules, "google_auth_oauthlib.flow", google_flow_mod)
-
- box_mod = ModuleType("box_sdk_gen")
-
- class _OAuthConfig:
- def __init__(self, client_id, client_secret):
- self.client_id = client_id
- self.client_secret = client_secret
-
- class _GetAuthorizeUrlOptions:
- def __init__(self, redirect_uri, state):
- self.redirect_uri = redirect_uri
- self.state = state
-
- box_mod.BoxOAuth = _FakeBoxOAuth
- box_mod.OAuthConfig = _OAuthConfig
- box_mod.GetAuthorizeUrlOptions = _GetAuthorizeUrlOptions
- monkeypatch.setitem(sys.modules, "box_sdk_gen", box_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "connector_api.py"
- spec = importlib.util.spec_from_file_location("test_connector_routes_unit", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- return module
-
-
-def test_connector_basic_routes_and_task_controls(monkeypatch):
- module = _load_connector_app(monkeypatch)
-
- async def _no_sleep(_secs):
- return None
-
- monkeypatch.setattr(module.asyncio, "sleep", _no_sleep)
-
- records = {"conn-1": _FakeConnectorRecord({"id": "conn-1", "source": "drive"})}
- update_calls = []
- save_calls = []
- cancel_calls = []
- delete_calls = []
-
- monkeypatch.setattr(module.ConnectorService, "update_by_id", lambda cid, payload: update_calls.append((cid, payload)))
-
- def _save(**payload):
- save_calls.append(payload)
- records[payload["id"]] = _FakeConnectorRecord(payload)
-
- monkeypatch.setattr(module.ConnectorService, "save", _save)
- monkeypatch.setattr(module.ConnectorService, "get_by_id", lambda cid: (True, records[cid]))
- monkeypatch.setattr(module.ConnectorService, "list", lambda tenant_id: [{"id": "listed", "tenant": tenant_id}])
- monkeypatch.setattr(module.SyncLogsService, "list_sync_tasks", lambda cid, page, page_size: ([{"id": "log-1"}], 9))
- monkeypatch.setattr(module.ConnectorService, "cancel_tasks", lambda cid: cancel_calls.append(cid))
- monkeypatch.setattr(module.ConnectorService, "delete_by_id", lambda cid: delete_calls.append(cid))
- monkeypatch.setattr(module, "get_uuid", lambda: "generated-id")
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"id": "conn-1", "refresh_freq": 7, "config": {"x": 1}}),
- )
- res = _run(module.update_connector("conn-1"))
- assert update_calls == [("conn-1", {"id": "conn-1", "refresh_freq": 7, "config": {"x": 1}})]
- assert res["data"]["id"] == "conn-1"
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"name": "new", "source": "gmail", "config": {"y": 2}}),
- )
- res = _run(module.create_connector())
- assert save_calls[-1]["id"] == "generated-id"
- assert save_calls[-1]["tenant_id"] == "tenant-1"
- assert save_calls[-1]["input_type"] == module.InputType.POLL
- assert res["data"]["id"] == "generated-id"
-
- list_res = module.list_connector()
- assert list_res["data"] == [{"id": "listed", "tenant": "tenant-1"}]
-
- monkeypatch.setattr(module.ConnectorService, "get_by_id", lambda _cid: (False, None))
- missing_res = module.get_connector("missing")
- assert missing_res["message"] == "Can't find this Connector!"
-
- monkeypatch.setattr(module.ConnectorService, "get_by_id", lambda cid: (True, _FakeConnectorRecord({"id": cid})))
- found_res = module.get_connector("conn-2")
- assert found_res["data"]["id"] == "conn-2"
-
- _set_request(module, args={"page": "2", "page_size": "7"})
- logs_res = module.list_logs("conn-log")
- assert logs_res["data"] == {"total": 9, "logs": [{"id": "log-1"}]}
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"kb_id": "kb-1"}))
- monkeypatch.setattr(module.ConnectorService, "rebuild", lambda *_args: "rebuild-failed")
- failed_rebuild = _run(module.rebuild("conn-rb"))
- assert failed_rebuild["code"] == module.RetCode.SERVER_ERROR
- assert failed_rebuild["data"] is False
-
- monkeypatch.setattr(module.ConnectorService, "rebuild", lambda *_args: None)
- ok_rebuild = _run(module.rebuild("conn-rb"))
- assert ok_rebuild["data"] is True
-
- rm_res = module.rm_connector("conn-rm")
- assert rm_res["data"] is True
- assert cancel_calls == ["conn-rm"]
- assert delete_calls == ["conn-rm"]
-
-
-def test_connector_by_id_routes_reject_cross_tenant_access(monkeypatch):
- """Verify per-id connector routes stop before body parsing or service access."""
- module = _load_connector_app(monkeypatch)
-
- touched = []
- monkeypatch.setattr(module.ConnectorService, "accessible", lambda cid, uid: False)
- monkeypatch.setattr(module.ConnectorService, "get_by_id", lambda *_args: touched.append("get_by_id"))
- monkeypatch.setattr(module.SyncLogsService, "list_sync_tasks", lambda *_args: touched.append("list_sync_tasks"))
- monkeypatch.setattr(module.ConnectorService, "cancel_tasks", lambda *_args: touched.append("cancel_tasks"))
- monkeypatch.setattr(module.ConnectorService, "delete_by_id", lambda *_args: touched.append("delete_by_id"))
- monkeypatch.setattr(module.ConnectorService, "update_by_id", lambda *_args: touched.append("update_by_id"))
- monkeypatch.setattr(module.ConnectorService, "rebuild", lambda *_args: touched.append("rebuild"))
-
- def _get_request_json():
- touched.append("get_request_json")
- return _AwaitableValue({"config": {"x": 1}})
-
- monkeypatch.setattr(module, "get_request_json", _get_request_json)
-
- responses = [
- _run(module.update_connector("conn-victim")),
- module.get_connector("conn-victim"),
- module.list_logs("conn-victim"),
- _run(module.rebuild("conn-victim")),
- module.rm_connector("conn-victim"),
- _run(module.test_connector("conn-victim")),
- ]
-
- assert all(res["code"] == module.RetCode.AUTHENTICATION_ERROR for res in responses)
- assert all(res["message"] == "no authorization" for res in responses)
- assert all(res["data"] is False for res in responses)
- assert touched == []
-
- class _FakeConnector:
- def validate_connector_settings(self):
- return None
-
- monkeypatch.setattr(
- sys.modules["common.data_source"],
- "build_connector_for_source",
- lambda source, config: _FakeConnector(),
- )
- monkeypatch.setattr(module.ConnectorService, "get_by_id", lambda _connector_id: (False, None))
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"source": "rss", "config": {"feed_url": "https://example.com"}}),
- )
- ok_res = _run(module.test_connector("rss"))
- assert ok_res["data"] is True
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"source": "rss", "config": "bad"}),
- )
- bad_config_res = _run(module.test_connector("rss"))
- assert bad_config_res["code"] == module.RetCode.ARGUMENT_ERROR
-
-
-def test_connector_oauth_helper_functions(monkeypatch):
- module = _load_connector_app(monkeypatch)
-
- assert module._web_state_cache_key("flow-a", "gmail") == "gmail_web_flow_state:flow-a"
- assert module._web_result_cache_key("flow-b", "google-drive") == "google-drive_web_flow_result:flow-b"
-
- creds_dict = {"web": {"client_id": "id"}}
- assert module._load_credentials(creds_dict) == creds_dict
- assert module._load_credentials(json.dumps(creds_dict)) == creds_dict
-
- with pytest.raises(ValueError, match="Invalid Google credentials JSON"):
- module._load_credentials("{not-json")
-
- assert module._get_web_client_config(creds_dict) == {"web": {"client_id": "id"}}
- with pytest.raises(ValueError, match="must include a 'web'"):
- module._get_web_client_config({"installed": {"client_id": "id"}})
-
- popup_ok = _run(module._render_web_oauth_popup("flow-1", True, "done", "gmail"))
- assert popup_ok.status_code == 200
- assert popup_ok.headers["Content-Type"] == "text/html; charset=utf-8"
- assert "Authorization complete" in popup_ok.body
- assert "ragflow-gmail-oauth" in popup_ok.body
-
- popup_error = _run(module._render_web_oauth_popup("flow-2", False, "<denied>", "google-drive"))
- assert popup_error.status_code == 200
- assert "Authorization failed" in popup_error.body
- assert "&lt;denied&gt;" in popup_error.body
-
-
-def test_start_google_web_oauth_matrix(monkeypatch):
- module = _load_connector_app(monkeypatch)
-
- redis = _FakeRedis()
- monkeypatch.setattr(module, "REDIS_CONN", redis)
- monkeypatch.setattr(module.time, "time", lambda: 1700000000)
-
- flow_calls = []
-
- def _from_client_config(client_config, scopes):
- flow = _FakeFlow(client_config, scopes)
- flow_calls.append(flow)
- return flow
-
- monkeypatch.setattr(module.Flow, "from_client_config", staticmethod(_from_client_config))
-
- _set_request(module, args={"type": "invalid"})
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"credentials": "{}"}))
- invalid_type = _run(module.start_google_web_oauth())
- assert invalid_type["code"] == module.RetCode.ARGUMENT_ERROR
-
- monkeypatch.setattr(module, "GMAIL_WEB_OAUTH_REDIRECT_URI", "")
- _set_request(module, args={"type": "gmail"})
- missing_redirect = _run(module.start_google_web_oauth())
- assert missing_redirect["code"] == module.RetCode.SERVER_ERROR
-
- monkeypatch.setattr(module, "GMAIL_WEB_OAUTH_REDIRECT_URI", "https://example.com/gmail")
- monkeypatch.setattr(module, "GOOGLE_DRIVE_WEB_OAUTH_REDIRECT_URI", "https://example.com/drive")
-
- _set_request(module, args={"type": "google-drive"})
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"credentials": "{invalid-json"}))
- invalid_credentials = _run(module.start_google_web_oauth())
- assert invalid_credentials["code"] == module.RetCode.ARGUMENT_ERROR
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"credentials": json.dumps({"web": {"client_id": "id"}, "refresh_token": "rt"})}),
- )
- has_refresh_token = _run(module.start_google_web_oauth())
- assert has_refresh_token["code"] == module.RetCode.ARGUMENT_ERROR
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"credentials": json.dumps({"installed": {"x": 1}})}))
- missing_web = _run(module.start_google_web_oauth())
- assert missing_web["code"] == module.RetCode.ARGUMENT_ERROR
-
- ids = iter(["flow-gmail", "flow-drive"])
- monkeypatch.setattr(module.uuid, "uuid4", lambda: next(ids))
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"credentials": json.dumps({"web": {"client_id": "id", "client_secret": "secret"}})}),
- )
-
- _set_request(module, args={"type": "gmail"})
- gmail_ok = _run(module.start_google_web_oauth())
- assert gmail_ok["code"] == 0
- assert gmail_ok["data"]["flow_id"] == "flow-gmail"
- assert gmail_ok["data"]["authorization_url"].endswith("flow-gmail")
-
- _set_request(module, args={})
- drive_ok = _run(module.start_google_web_oauth())
- assert drive_ok["code"] == 0
- assert drive_ok["data"]["flow_id"] == "flow-drive"
- assert drive_ok["data"]["authorization_url"].endswith("flow-drive")
-
- assert any(call.scopes == module.GOOGLE_SCOPES[module.DocumentSource.GMAIL] for call in flow_calls)
- assert any(call.scopes == module.GOOGLE_SCOPES[module.DocumentSource.GOOGLE_DRIVE] for call in flow_calls)
- assert "gmail_web_flow_state:flow-gmail" in redis.store
- assert "google-drive_web_flow_state:flow-drive" in redis.store
- assert json.loads(redis.store["gmail_web_flow_state:flow-gmail"])["code_verifier"] == "fake-code-verifier"
- assert json.loads(redis.store["google-drive_web_flow_state:flow-drive"])["code_verifier"] == "fake-code-verifier"
-
-
-def test_google_web_oauth_callbacks_matrix(monkeypatch):
- module = _load_connector_app(monkeypatch)
-
- flow_calls = []
-
- def _from_client_config(client_config, scopes):
- flow = _FakeFlow(client_config, scopes)
- flow_calls.append(flow)
- return flow
-
- monkeypatch.setattr(module.Flow, "from_client_config", staticmethod(_from_client_config))
-
- callback_specs = [
- (
- module.google_gmail_web_oauth_callback,
- "gmail",
- module.GMAIL_WEB_OAUTH_REDIRECT_URI,
- module.GOOGLE_SCOPES[module.DocumentSource.GMAIL],
- ),
- (
- module.google_drive_web_oauth_callback,
- "google-drive",
- module.GOOGLE_DRIVE_WEB_OAUTH_REDIRECT_URI,
- module.GOOGLE_SCOPES[module.DocumentSource.GOOGLE_DRIVE],
- ),
- ]
-
- for callback, source, expected_redirect, expected_scopes in callback_specs:
- redis = _FakeRedis()
- monkeypatch.setattr(module, "REDIS_CONN", redis)
-
- _set_request(module, args={})
- missing_state = _run(callback())
- assert "Missing OAuth state parameter." in missing_state.body
-
- _set_request(module, args={"state": "sid"})
- expired_state = _run(callback())
- assert "Authorization session expired" in expired_state.body
-
- redis.store[module._web_state_cache_key("sid", source)] = json.dumps({"user_id": "tenant-1"})
- _set_request(module, args={"state": "sid"})
- invalid_state = _run(callback())
- assert "Authorization session was invalid" in invalid_state.body
- assert module._web_state_cache_key("sid", source) in redis.deleted
-
- redis.store[module._web_state_cache_key("sid", source)] = json.dumps(
- {
- "user_id": "tenant-1",
- "client_config": {"web": {"client_id": "cid"}},
- }
- )
- _set_request(module, args={"state": "sid", "error": "denied", "error_description": "permission denied"})
- oauth_error = _run(callback())
- assert "permission denied" in oauth_error.body
-
- redis.store[module._web_state_cache_key("sid", source)] = json.dumps(
- {
- "user_id": "tenant-1",
- "client_config": {"web": {"client_id": "cid"}},
- }
- )
- _set_request(module, args={"state": "sid"})
- missing_code = _run(callback())
- assert "Missing authorization code" in missing_code.body
-
- redis.store[module._web_state_cache_key("sid", source)] = json.dumps(
- {
- "user_id": "tenant-1",
- "client_config": {"web": {"client_id": "cid"}},
- "code_verifier": "state-code-verifier",
- }
- )
- _set_request(module, args={"state": "sid", "code": "code-123"})
- success = _run(callback())
- assert "Authorization completed successfully." in success.body
-
- result_key = module._web_result_cache_key("sid", source)
- assert result_key in redis.store
- assert module._web_state_cache_key("sid", source) in redis.deleted
-
- assert flow_calls[-1].redirect_uri == expected_redirect
- assert flow_calls[-1].scopes == expected_scopes
- assert flow_calls[-1].token_code == "code-123"
- assert flow_calls[-1].token_code_verifier == "state-code-verifier"
-
-
-def test_poll_google_web_result_matrix(monkeypatch):
- module = _load_connector_app(monkeypatch)
- redis = _FakeRedis()
- monkeypatch.setattr(module, "REDIS_CONN", redis)
-
- _set_request(module, args={"type": "invalid"}, json_body={"flow_id": "flow-1"})
- invalid_type = _run(module.poll_google_web_result())
- assert invalid_type["code"] == module.RetCode.ARGUMENT_ERROR
-
- _set_request(module, args={"type": "gmail"}, json_body={"flow_id": "flow-1"})
- pending = _run(module.poll_google_web_result())
- assert pending["code"] == module.RetCode.RUNNING
-
- redis.store[module._web_result_cache_key("flow-1", "gmail")] = json.dumps({"user_id": "another-user", "credentials": "token-x"})
- _set_request(module, args={"type": "gmail"}, json_body={"flow_id": "flow-1"})
- permission_error = _run(module.poll_google_web_result())
- assert permission_error["code"] == module.RetCode.PERMISSION_ERROR
-
- redis.store[module._web_result_cache_key("flow-1", "gmail")] = json.dumps({"user_id": "tenant-1", "credentials": "token-ok"})
- _set_request(module, args={"type": "gmail"}, json_body={"flow_id": "flow-1"})
- success = _run(module.poll_google_web_result())
- assert success["code"] == 0
- assert success["data"] == {"credentials": "token-ok"}
- assert module._web_result_cache_key("flow-1", "gmail") in redis.deleted
-
-
-def test_box_oauth_start_callback_and_poll_matrix(monkeypatch):
- module = _load_connector_app(monkeypatch)
- redis = _FakeRedis()
- monkeypatch.setattr(module, "REDIS_CONN", redis)
-
- created_auth = []
-
- class _TrackingBoxOAuth(_FakeBoxOAuth):
- def __init__(self, config):
- super().__init__(config)
- created_auth.append(self)
-
- monkeypatch.setattr(module, "BoxOAuth", _TrackingBoxOAuth)
- monkeypatch.setattr(module.uuid, "uuid4", lambda: "flow-box")
- monkeypatch.setattr(module.time, "time", lambda: 1800000000)
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({}))
- missing_params = _run(module.start_box_web_oauth())
- assert missing_params["code"] == module.RetCode.ARGUMENT_ERROR
-
- monkeypatch.setattr(
- module,
- "get_request_json",
- lambda: _AwaitableValue({"client_id": "cid", "client_secret": "sec", "redirect_uri": "https://box.local/callback"}),
- )
- start_ok = _run(module.start_box_web_oauth())
- assert start_ok["code"] == 0
- assert start_ok["data"]["flow_id"] == "flow-box"
- assert "authorization_url" in start_ok["data"]
- assert module._web_state_cache_key("flow-box", "box") in redis.store
-
- _set_request(module, args={})
- missing_state = _run(module.box_web_oauth_callback())
- assert "Missing OAuth parameters." in missing_state.body
-
- _set_request(module, args={"state": "flow-box"})
- missing_code = _run(module.box_web_oauth_callback())
- assert "Missing authorization code from Box." in missing_code.body
-
- redis.store[module._web_state_cache_key("flow-null", "box")] = "null"
- _set_request(module, args={"state": "flow-null", "code": "abc"})
- invalid_session = _run(module.box_web_oauth_callback())
- assert invalid_session["code"] == module.RetCode.ARGUMENT_ERROR
-
- redis.store[module._web_state_cache_key("flow-box", "box")] = json.dumps({"user_id": "tenant-1", "client_id": "cid", "client_secret": "sec"})
- _set_request(module, args={"state": "flow-box", "code": "abc", "error": "access_denied", "error_description": "denied"})
- callback_error = _run(module.box_web_oauth_callback())
- assert "denied" in callback_error.body
-
- redis.store[module._web_state_cache_key("flow-ok", "box")] = json.dumps({"user_id": "tenant-1", "client_id": "cid", "client_secret": "sec"})
- _set_request(module, args={"state": "flow-ok", "code": "code-ok"})
- callback_success = _run(module.box_web_oauth_callback())
- assert "Authorization completed successfully." in callback_success.body
- assert created_auth[-1].exchange_code == "code-ok"
- assert module._web_result_cache_key("flow-ok", "box") in redis.store
- assert module._web_state_cache_key("flow-ok", "box") in redis.deleted
-
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"flow_id": "flow-ok"}))
- redis.store.pop(module._web_result_cache_key("flow-ok", "box"), None)
- pending = _run(module.poll_box_web_result())
- assert pending["code"] == module.RetCode.RUNNING
-
- redis.store[module._web_result_cache_key("flow-ok", "box")] = json.dumps({"user_id": "another-user"})
- permission_error = _run(module.poll_box_web_result())
- assert permission_error["code"] == module.RetCode.PERMISSION_ERROR
-
- redis.store[module._web_result_cache_key("flow-ok", "box")] = json.dumps({"user_id": "tenant-1", "access_token": "at", "refresh_token": "rt"})
- poll_success = _run(module.poll_box_web_result())
- assert poll_success["code"] == 0
- assert poll_success["data"]["credentials"]["access_token"] == "at"
- assert module._web_result_cache_key("flow-ok", "box") in redis.deleted
diff --git a/test/testcases/test_web_api/test_connector_app/test_langfuse_app_unit.py b/test/testcases/test_web_api/test_connector_app/test_langfuse_app_unit.py
deleted file mode 100644
index 8e6bef31fc..0000000000
--- a/test/testcases/test_web_api/test_connector_app/test_langfuse_app_unit.py
+++ /dev/null
@@ -1,219 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _DummyAtomic:
- def __enter__(self):
- return self
-
- def __exit__(self, _exc_type, _exc, _tb):
- return False
-
-
-class _FakeApiError(Exception):
- pass
-
-
-class _FakeLangfuseClient:
- def __init__(self, *, auth_result=True, auth_exc=None, project_payload=None):
- self._auth_result = auth_result
- self._auth_exc = auth_exc
- if project_payload is None:
- project_payload = {"data": [{"id": "project-id", "name": "project-name"}]}
- self.api = SimpleNamespace(
- projects=SimpleNamespace(get=lambda: SimpleNamespace(dict=lambda: project_payload)),
- core=SimpleNamespace(api_error=SimpleNamespace(ApiError=_FakeApiError)),
- )
-
- def auth_check(self):
- if self._auth_exc is not None:
- raise self._auth_exc
- return self._auth_result
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _load_langfuse_app(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- stub_apps = ModuleType("api.apps")
- stub_apps.current_user = SimpleNamespace(id="tenant-1")
- stub_apps.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", stub_apps)
-
- stub_langfuse = ModuleType("langfuse")
- stub_langfuse.Langfuse = _FakeLangfuseClient
- monkeypatch.setitem(sys.modules, "langfuse", stub_langfuse)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "langfuse_api.py"
- spec = importlib.util.spec_from_file_location("test_langfuse_app_unit", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- return module
-
-
-def test_set_api_key_missing_fields_and_invalid_auth(monkeypatch):
- module = _load_langfuse_app(monkeypatch)
- monkeypatch.setattr(module.DB, "atomic", lambda: _DummyAtomic())
-
- async def missing_fields():
- return {"secret_key": "", "public_key": "pub", "host": "http://host"}
-
- monkeypatch.setattr(module, "get_request_json", missing_fields)
- res = _run(module.set_api_key.__wrapped__())
- assert res["code"] == 102
- assert res["message"] == "Missing required fields"
-
- async def invalid_auth():
- return {"secret_key": "sec", "public_key": "pub", "host": "http://host"}
-
- monkeypatch.setattr(module, "get_request_json", invalid_auth)
- monkeypatch.setattr(module, "Langfuse", lambda **_kwargs: _FakeLangfuseClient(auth_result=False))
- res = _run(module.set_api_key.__wrapped__())
- assert res["code"] == 102
- assert res["message"] == "Invalid Langfuse keys"
-
-
-def test_set_api_key_create_update_and_atomic_exception(monkeypatch):
- module = _load_langfuse_app(monkeypatch)
- monkeypatch.setattr(module.DB, "atomic", lambda: _DummyAtomic())
- monkeypatch.setattr(module, "Langfuse", lambda **_kwargs: _FakeLangfuseClient(auth_result=True))
-
- async def payload():
- return {"secret_key": "sec", "public_key": "pub", "host": "http://host"}
-
- monkeypatch.setattr(module, "get_request_json", payload)
-
- calls = {"save": 0, "update": 0}
- monkeypatch.setattr(module.TenantLangfuseService, "filter_by_tenant", lambda **_kwargs: None)
- monkeypatch.setattr(
- module.TenantLangfuseService,
- "save",
- lambda **_kwargs: calls.__setitem__("save", calls["save"] + 1),
- )
- monkeypatch.setattr(
- module.TenantLangfuseService,
- "update_by_tenant",
- lambda **_kwargs: calls.__setitem__("update", calls["update"] + 1),
- )
- res = _run(module.set_api_key.__wrapped__())
- assert res["code"] == 0
- assert calls["save"] == 1
-
- monkeypatch.setattr(module.TenantLangfuseService, "filter_by_tenant", lambda **_kwargs: {"id": "existing"})
- res = _run(module.set_api_key.__wrapped__())
- assert res["code"] == 0
- assert calls["update"] == 1
-
- monkeypatch.setattr(module.TenantLangfuseService, "filter_by_tenant", lambda **_kwargs: None)
-
- def raise_save(**_kwargs):
- raise RuntimeError("save failed")
-
- monkeypatch.setattr(module.TenantLangfuseService, "save", raise_save)
- res = _run(module.set_api_key.__wrapped__())
- assert res["code"] == 100
- assert "save failed" in res["message"]
-
-
-def test_get_api_key_no_record_invalid_auth_api_error_generic_error_success(monkeypatch):
- module = _load_langfuse_app(monkeypatch)
-
- monkeypatch.setattr(module.TenantLangfuseService, "filter_by_tenant_with_info", lambda **_kwargs: None)
- res = module.get_api_key.__wrapped__()
- assert res["code"] == 0
- assert res["message"] == "Have not record any Langfuse keys."
-
- base_entry = {"secret_key": "sec", "public_key": "pub", "host": "http://host"}
- monkeypatch.setattr(module.TenantLangfuseService, "filter_by_tenant_with_info", lambda **_kwargs: dict(base_entry))
- monkeypatch.setattr(module, "Langfuse", lambda **_kwargs: _FakeLangfuseClient(auth_result=False))
- res = module.get_api_key.__wrapped__()
- assert res["code"] == 102
- assert res["message"] == "Invalid Langfuse keys loaded"
-
- monkeypatch.setattr(
- module,
- "Langfuse",
- lambda **_kwargs: _FakeLangfuseClient(auth_exc=_FakeApiError("api exploded")),
- )
- res = module.get_api_key.__wrapped__()
- assert res["code"] == 0
- assert "Error from Langfuse" in res["message"]
-
- monkeypatch.setattr(
- module,
- "Langfuse",
- lambda **_kwargs: _FakeLangfuseClient(auth_exc=RuntimeError("generic exploded")),
- )
- res = module.get_api_key.__wrapped__()
- assert res["code"] == 100
- assert "generic exploded" in res["message"]
-
- monkeypatch.setattr(module, "Langfuse", lambda **_kwargs: _FakeLangfuseClient(auth_result=True))
- res = module.get_api_key.__wrapped__()
- assert res["code"] == 0
- assert res["data"]["project_id"] == "project-id"
- assert res["data"]["project_name"] == "project-name"
-
-
-def test_delete_api_key_no_record_success_exception(monkeypatch):
- module = _load_langfuse_app(monkeypatch)
- monkeypatch.setattr(module.DB, "atomic", lambda: _DummyAtomic())
-
- monkeypatch.setattr(module.TenantLangfuseService, "filter_by_tenant", lambda **_kwargs: None)
- res = module.delete_api_key.__wrapped__()
- assert res["code"] == 0
- assert res["message"] == "Have not record any Langfuse keys."
-
- monkeypatch.setattr(module.TenantLangfuseService, "filter_by_tenant", lambda **_kwargs: {"id": "entry"})
- monkeypatch.setattr(module.TenantLangfuseService, "delete_model", lambda _entry: None)
- res = module.delete_api_key.__wrapped__()
- assert res["code"] == 0
- assert res["data"] is True
-
- def raise_delete(_entry):
- raise RuntimeError("delete failed")
-
- monkeypatch.setattr(module.TenantLangfuseService, "delete_model", raise_delete)
- res = module.delete_api_key.__wrapped__()
- assert res["code"] == 100
- assert "delete failed" in res["message"]
diff --git a/test/testcases/test_web_api/test_dataset_management/test_dataset_sdk_routes_unit.py b/test/testcases/test_web_api/test_dataset_management/test_dataset_sdk_routes_unit.py
deleted file mode 100644
index 55bd787638..0000000000
--- a/test/testcases/test_web_api/test_dataset_management/test_dataset_sdk_routes_unit.py
+++ /dev/null
@@ -1,841 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import inspect
-import json
-import os
-import sys
-from copy import deepcopy
-from enum import Enum
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _DummyArgs(dict):
- def get(self, key, default=None, type=None):
- value = super().get(key, default)
- if value is None or type is None:
- return value
- try:
- return type(value)
- except (TypeError, ValueError):
- return default
-
-
-class _Field:
- def __init__(self, name):
- self.name = name
-
- def __eq__(self, other):
- return (self.name, "==", other)
-
-
-class _KB:
- def __init__(
- self,
- *,
- kb_id="kb-1",
- name="old",
- tenant_id="tenant-1",
- parser_id="naive",
- parser_config=None,
- embd_id="embd-1",
- chunk_num=0,
- pagerank=0,
- graphrag_task_id="",
- raptor_task_id="",
- ):
- self.id = kb_id
- self.name = name
- self.tenant_id = tenant_id
- self.parser_id = parser_id
- self.parser_config = parser_config or {}
- self.embd_id = embd_id
- self.chunk_num = chunk_num
- self.pagerank = pagerank
- self.graphrag_task_id = graphrag_task_id
- self.raptor_task_id = raptor_task_id
-
- def to_dict(self):
- return {
- "id": self.id,
- "name": self.name,
- "tenant_id": self.tenant_id,
- "parser_id": self.parser_id,
- "parser_config": deepcopy(self.parser_config),
- "embd_id": self.embd_id,
- "pagerank": self.pagerank,
- }
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _set_request_args(monkeypatch, module, args):
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs(args)))
-
-
-def _patch_json_parser(monkeypatch, module, payload_state, err_state=None):
- async def _parse_json(*_args, **_kwargs):
- return deepcopy(payload_state), err_state
-
- monkeypatch.setattr(module, "validate_and_parse_json_request", _parse_json)
-
-
-def _load_dataset_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart_mod = ModuleType("quart")
- quart_mod.Request = type("Request", (), {})
- quart_mod.request = SimpleNamespace(args=_DummyArgs())
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- api_constants_mod = ModuleType("api.constants")
- api_constants_mod.DATASET_NAME_LIMIT = 128
- api_constants_mod.FILE_NAME_LEN_LIMIT = 255
- monkeypatch.setitem(sys.modules, "api.constants", api_constants_mod)
- api_pkg.constants = api_constants_mod
-
- utils_pkg = ModuleType("api.utils")
- utils_pkg.__path__ = [str(repo_root / "api" / "utils")]
- monkeypatch.setitem(sys.modules, "api.utils", utils_pkg)
- api_pkg.utils = utils_pkg
-
- apps_pkg = ModuleType("api.apps")
- apps_pkg.__path__ = [str(repo_root / "api" / "apps")]
- apps_pkg.login_required = lambda func: func
- apps_pkg.current_user = SimpleNamespace(id="tenant-current")
- monkeypatch.setitem(sys.modules, "api.apps", apps_pkg)
- api_pkg.apps = apps_pkg
-
- sdk_pkg = ModuleType("api.apps.sdk")
- sdk_pkg.__path__ = [str(repo_root / "api" / "apps" / "sdk")]
- monkeypatch.setitem(sys.modules, "api.apps.sdk", sdk_pkg)
- apps_pkg.sdk = sdk_pkg
-
- db_pkg = ModuleType("api.db")
- db_pkg.__path__ = []
- db_pkg.FileType = SimpleNamespace()
- monkeypatch.setitem(sys.modules, "api.db", db_pkg)
- api_pkg.db = db_pkg
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.File = SimpleNamespace(
- source_type=_Field("source_type"),
- id=_Field("id"),
- type=_Field("type"),
- name=_Field("name"),
- )
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- document_service_mod = ModuleType("api.db.services.document_service")
-
- class _StubDocumentService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def remove_document(*_args, **_kwargs):
- return True
-
- @staticmethod
- def get_by_kb_id(**_kwargs):
- return [], 0
-
- document_service_mod.DocumentService = _StubDocumentService
- document_service_mod.queue_raptor_o_graphrag_tasks = lambda **_kwargs: "task-queued"
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", document_service_mod)
- services_pkg.document_service = document_service_mod
-
- file2document_service_mod = ModuleType("api.db.services.file2document_service")
-
- class _StubFile2DocumentService:
- @staticmethod
- def get_by_document_id(_doc_id):
- return [SimpleNamespace(file_id="file-1")]
-
- @staticmethod
- def delete_by_document_id(_doc_id):
- return None
-
- file2document_service_mod.File2DocumentService = _StubFile2DocumentService
- monkeypatch.setitem(sys.modules, "api.db.services.file2document_service", file2document_service_mod)
- services_pkg.file2document_service = file2document_service_mod
-
- file_service_mod = ModuleType("api.db.services.file_service")
-
- class _StubFileService:
- @staticmethod
- def filter_delete(_filters):
- return None
-
- file_service_mod.FileService = _StubFileService
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
- services_pkg.file_service = file_service_mod
-
- connector_service_mod = ModuleType("api.db.services.connector_service")
-
- class _StubConnector2KbService:
- @staticmethod
- def link_connectors(*_args, **_kwargs):
- return []
-
- connector_service_mod.Connector2KbService = _StubConnector2KbService
- monkeypatch.setitem(sys.modules, "api.db.services.connector_service", connector_service_mod)
- services_pkg.connector_service = connector_service_mod
-
- knowledgebase_service_mod = ModuleType("api.db.services.knowledgebase_service")
-
- class _StubKnowledgebaseService:
- @staticmethod
- def create_with_name(**_kwargs):
- return True, {"id": "kb-1"}
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def get_by_id(_kb_id):
- return True, _KB()
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_or_none(**_kwargs):
- return _KB()
-
- @staticmethod
- def delete_by_id(_kb_id):
- return True
-
- @staticmethod
- def update_by_id(_kb_id, _payload):
- return True
-
- @staticmethod
- def get_kb_by_id(_kb_id, _tenant_id):
- return [SimpleNamespace(id=_kb_id)]
-
- @staticmethod
- def get_kb_by_name(_name, _tenant_id):
- return [SimpleNamespace(name=_name)]
-
- @staticmethod
- def get_list(*_args, **_kwargs):
- return [], 0
-
- @staticmethod
- def accessible(_dataset_id, _tenant_id):
- return True
-
- knowledgebase_service_mod.KnowledgebaseService = _StubKnowledgebaseService
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", knowledgebase_service_mod)
- services_pkg.knowledgebase_service = knowledgebase_service_mod
-
- task_service_mod = ModuleType("api.db.services.task_service")
-
- class _StubTaskService:
- @staticmethod
- def get_by_id(_task_id):
- return False, None
-
- task_service_mod.GRAPH_RAPTOR_FAKE_DOC_ID = "fake-doc"
- task_service_mod.TaskService = _StubTaskService
- monkeypatch.setitem(sys.modules, "api.db.services.task_service", task_service_mod)
- services_pkg.task_service = task_service_mod
-
- user_service_mod = ModuleType("api.db.services.user_service")
-
- class _StubTenantService:
- @staticmethod
- def get_by_id(_tenant_id):
- return True, SimpleNamespace(embd_id="embd-default")
-
- @staticmethod
- def get_joined_tenants_by_user_id(_tenant_id):
- return [{"tenant_id": "tenant-1"}]
-
- class _StubUserService:
- @staticmethod
- def get_by_ids(_ids):
- return []
-
- class _StubUserTenantService:
- @staticmethod
- def get_tenants_by_user_id(_user_id):
- return []
-
- user_service_mod.TenantService = _StubTenantService
- user_service_mod.UserService = _StubUserService
- user_service_mod.UserTenantService = _StubUserTenantService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
- services_pkg.user_service = user_service_mod
-
- constants_mod = ModuleType("common.constants")
-
- class _RetCode:
- SUCCESS = 0
- ARGUMENT_ERROR = 101
- DATA_ERROR = 102
- AUTHENTICATION_ERROR = 108
-
- class _FileSource:
- KNOWLEDGEBASE = "knowledgebase"
-
- class _StatusEnum(Enum):
- VALID = "valid"
-
- constants_mod.RetCode = _RetCode
- constants_mod.FileSource = _FileSource
- constants_mod.StatusEnum = _StatusEnum
- constants_mod.PAGERANK_FLD = "pagerank"
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- common_pkg.settings = SimpleNamespace(
- docStoreConn=SimpleNamespace(
- delete_idx=lambda *_args, **_kwargs: None,
- delete=lambda *_args, **_kwargs: None,
- update=lambda *_args, **_kwargs: None,
- index_exist=lambda *_args, **_kwargs: False,
- ),
- retriever=SimpleNamespace(search=lambda *_args, **_kwargs: _AwaitableValue(SimpleNamespace(ids=[], field={}))),
- )
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- def _deep_merge(base, updates):
- merged = deepcopy(base)
- for key, value in updates.items():
- if isinstance(value, dict) and isinstance(merged.get(key), dict):
- merged[key] = _deep_merge(merged[key], value)
- else:
- merged[key] = value
- return merged
-
- def _get_result(*, data=None, message="", code=_RetCode.SUCCESS, total=None):
- payload = {"code": code, "data": data, "message": message}
- if total is not None:
- payload["total"] = total
- return payload
-
- def _get_error_argument_result(message=""):
- return _get_result(code=_RetCode.ARGUMENT_ERROR, message=message)
-
- def _get_error_data_result(message=""):
- return _get_result(code=_RetCode.DATA_ERROR, message=message)
-
- def _get_error_permission_result(message=""):
- return _get_result(code=_RetCode.AUTHENTICATION_ERROR, message=message)
-
- api_utils_mod.deep_merge = _deep_merge
- api_utils_mod.get_error_argument_result = _get_error_argument_result
- api_utils_mod.get_error_data_result = _get_error_data_result
- api_utils_mod.get_error_permission_result = _get_error_permission_result
- api_utils_mod.get_parser_config = lambda _chunk_method, _unused: {"auto": True}
- api_utils_mod.get_result = _get_result
- api_utils_mod.remap_dictionary_keys = lambda data: data
- api_utils_mod.add_tenant_id_to_kwargs = lambda func: func
- api_utils_mod.verify_embedding_availability = lambda _embd_id, _tenant_id: (True, None)
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- async def _parse_json(*_args, **_kwargs):
- return {}, None
-
- def _parse_args(*_args, **_kwargs):
- return {"name": "", "page": 1, "page_size": 30, "orderby": "create_time", "desc": True}, None
-
- validation_spec = importlib.util.spec_from_file_location("api.utils.validation_utils", repo_root / "api" / "utils" / "validation_utils.py")
- validation_mod = importlib.util.module_from_spec(validation_spec)
- monkeypatch.setitem(sys.modules, "api.utils.validation_utils", validation_mod)
- validation_spec.loader.exec_module(validation_mod)
- validation_mod.validate_and_parse_json_request = _parse_json
- validation_mod.validate_and_parse_request_args = _parse_args
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
-
- rag_nlp_pkg = ModuleType("rag.nlp")
- rag_nlp_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag.nlp", rag_nlp_pkg)
-
- search_mod = ModuleType("rag.nlp.search")
- search_mod.index_name = lambda _tenant_id: "idx"
- monkeypatch.setitem(sys.modules, "rag.nlp.search", search_mod)
- rag_nlp_pkg.search = search_mod
-
- module_name = "test_dataset_sdk_routes_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "dataset_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- # Backward-compatible aliases used by this unit test module.
- module.KnowledgebaseService = module.dataset_api_service.KnowledgebaseService
- module.DocumentService = module.dataset_api_service.DocumentService
- module.File2DocumentService = module.dataset_api_service.File2DocumentService
- module.FileService = module.dataset_api_service.FileService
- module.TaskService = module.dataset_api_service.TaskService
- module.TenantService = module.dataset_api_service.TenantService
- module.settings = module.dataset_api_service.settings
- module.search = search_mod
- module.queue_raptor_o_graphrag_tasks = module.dataset_api_service.queue_raptor_o_graphrag_tasks
- return module
-
-
-def test_create_route_error_matrix_unit(monkeypatch):
- module = _load_dataset_module(monkeypatch)
- req_state = {"name": "kb"}
- _patch_json_parser(monkeypatch, module, req_state)
-
- monkeypatch.setattr(module.KnowledgebaseService, "create_with_name", lambda **_kwargs: (False, {"code": 777, "message": "early"}))
- res = _run(inspect.unwrap(module.create)("tenant-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert res["message"] == {"code": 777, "message": "early"}, res
-
- monkeypatch.setattr(module.KnowledgebaseService, "create_with_name", lambda **_kwargs: (True, {"id": "kb-1"}))
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tenant_id: (False, None))
- res = _run(inspect.unwrap(module.create)("tenant-1"))
- assert res["message"] == "Tenant not found", res
-
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tenant_id: (True, SimpleNamespace(embd_id="embd-1")))
- monkeypatch.setattr(module.KnowledgebaseService, "save", lambda **_kwargs: False)
- res = _run(inspect.unwrap(module.create)("tenant-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
-
- monkeypatch.setattr(module.KnowledgebaseService, "save", lambda **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
- res = _run(inspect.unwrap(module.create)("tenant-1"))
- assert "Dataset created failed" in res["message"], res
-
- monkeypatch.setattr(module.KnowledgebaseService, "save", lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("save boom")))
- res = _run(inspect.unwrap(module.create)("tenant-1"))
- assert res["message"] == "Internal server error", res
-
-
-def test_delete_route_error_summary_matrix_unit(monkeypatch):
- module = _load_dataset_module(monkeypatch)
- req_state = {"ids": ["kb-1"]}
- _patch_json_parser(monkeypatch, module, req_state)
-
- kb = _KB(kb_id="kb-1", name="kb-1", tenant_id="tenant-1")
- monkeypatch.setattr(module.KnowledgebaseService, "get_or_none", lambda **_kwargs: kb)
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [SimpleNamespace(id="doc-1")])
- monkeypatch.setattr(module.DocumentService, "remove_document", lambda *_args, **_kwargs: False)
- monkeypatch.setattr(module.settings, "docStoreConn", SimpleNamespace(delete_idx=lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("drop failed"))))
- monkeypatch.setattr(module.KnowledgebaseService, "delete_by_id", lambda _kb_id: False)
- res = _run(inspect.unwrap(module.delete)("tenant-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "Successfully deleted 0 datasets" in res["message"], res
-
- monkeypatch.setattr(module.settings, "docStoreConn", SimpleNamespace(delete_idx=lambda *_args, **_kwargs: None))
- monkeypatch.setattr(module.KnowledgebaseService, "delete_by_id", lambda _kb_id: True)
- res = _run(inspect.unwrap(module.delete)("tenant-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["data"]["success_count"] == 1, res
- assert res["data"]["errors"], res
-
- req_state["ids"] = None
- res = _run(inspect.unwrap(module.delete)("tenant-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
-
-
-def test_update_route_branch_matrix_unit(monkeypatch):
- module = _load_dataset_module(monkeypatch)
- req_state = {"name": "new"}
- _patch_json_parser(monkeypatch, module, req_state)
-
- monkeypatch.setattr(module.KnowledgebaseService, "get_or_none", lambda **_kwargs: None)
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "lacks permission for dataset" in res["message"], res
-
- kb = _KB(kb_id="kb-1", name="old", chunk_num=0)
-
- def _get_or_none_duplicate(**kwargs):
- if kwargs.get("id"):
- return kb
- if kwargs.get("name"):
- return SimpleNamespace(id="dup")
- return None
-
- monkeypatch.setattr(module.KnowledgebaseService, "get_or_none", _get_or_none_duplicate)
- req_state.clear()
- req_state.update({"name": "new"})
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert "already exists" in res["message"], res
-
- kb_chunked = _KB(kb_id="kb-1", name="old", chunk_num=2, embd_id="embd-1")
- monkeypatch.setattr(module.KnowledgebaseService, "get_or_none", lambda **kwargs: kb_chunked if kwargs.get("id") else None)
- monkeypatch.setattr(module.KnowledgebaseService, "update_by_id", lambda *_args, **_kwargs: True)
- req_state.clear()
- req_state.update({"embd_id": "embd-2"})
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
-
- kb_rank = _KB(kb_id="kb-1", name="old", pagerank=0)
- monkeypatch.setattr(module.KnowledgebaseService, "get_or_none", lambda **kwargs: kb_rank if kwargs.get("id") else None)
- req_state.clear()
- req_state.update({"pagerank": 3})
- os.environ["DOC_ENGINE"] = "infinity"
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert "doc_engine" in res["message"], res
- os.environ.pop("DOC_ENGINE", None)
-
- update_calls = []
- monkeypatch.setattr(module.settings, "docStoreConn", SimpleNamespace(update=lambda *args, **_kwargs: update_calls.append(args)))
- monkeypatch.setattr(module.KnowledgebaseService, "update_by_id", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, _KB(kb_id="kb-1", pagerank=3)))
-
- req_state.clear()
- req_state.update({"pagerank": 3})
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
- assert update_calls and update_calls[-1][0] == {"kb_id": "kb-1"}, update_calls
-
- update_calls.clear()
- monkeypatch.setattr(module.KnowledgebaseService, "get_or_none", lambda **kwargs: _KB(kb_id="kb-1", pagerank=3) if kwargs.get("id") else None)
- req_state.clear()
- req_state.update({"pagerank": 0})
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
- assert update_calls and update_calls[-1][0] == {"exists": module.dataset_api_service.PAGERANK_FLD}, update_calls
-
- monkeypatch.setattr(module.KnowledgebaseService, "update_by_id", lambda *_args, **_kwargs: False)
- req_state.clear()
- req_state.update({"description": "changed"})
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert "Update dataset error" in res["message"], res
-
- monkeypatch.setattr(module.KnowledgebaseService, "update_by_id", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert "Dataset updated failed" in res["message"], res
-
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_or_none",
- lambda **_kwargs: (_ for _ in ()).throw(module.OperationalError("update down")),
- )
- res = _run(inspect.unwrap(module.update)("tenant-1", "kb-1"))
- assert res["message"] == "Database operation failed", res
-
-
-def test_list_knowledge_graph_delete_kg_matrix_unit(monkeypatch):
- module = _load_dataset_module(monkeypatch)
-
- _set_request_args(monkeypatch, module, {"id": "", "name": "", "page": 1, "page_size": 30, "orderby": "create_time", "desc": True})
- monkeypatch.setattr(
- module,
- "validate_and_parse_request_args",
- lambda *_args, **_kwargs: ({"name": "", "page": 1, "page_size": 30, "orderby": "create_time", "desc": True}, None),
- )
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_list",
- lambda *_args, **_kwargs: (_ for _ in ()).throw(module.OperationalError("list down")),
- )
- res = module.list_datasets("tenant-1")
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert res["message"] == "Database operation failed", res
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: False)
- res = _run(inspect.unwrap(module.knowledge_graph)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, _KB(tenant_id="tenant-1")))
- monkeypatch.setattr(module.search, "index_name", lambda _tenant_id: "idx")
- monkeypatch.setattr(module.settings, "docStoreConn", SimpleNamespace(index_exist=lambda *_args, **_kwargs: False))
- res = _run(inspect.unwrap(module.knowledge_graph)("tenant-1", "kb-1"))
- assert res["data"] == {"graph": {}, "mind_map": {}}, res
-
- monkeypatch.setattr(module.settings, "docStoreConn", SimpleNamespace(index_exist=lambda *_args, **_kwargs: True))
-
- class _EmptyRetriever:
- async def search(self, *_args, **_kwargs):
- return SimpleNamespace(ids=[], field={})
-
- monkeypatch.setattr(module.settings, "retriever", _EmptyRetriever())
- res = _run(inspect.unwrap(module.knowledge_graph)("tenant-1", "kb-1"))
- assert res["data"] == {"graph": {}, "mind_map": {}}, res
-
- class _BadRetriever:
- async def search(self, *_args, **_kwargs):
- return SimpleNamespace(ids=["bad"], field={"bad": {"knowledge_graph_kwd": "graph", "content_with_weight": "{bad"}})
-
- monkeypatch.setattr(module.settings, "retriever", _BadRetriever())
- res = _run(inspect.unwrap(module.knowledge_graph)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["data"]["graph"] == {}, res
-
- payload = {
- "nodes": [{"id": "n2", "pagerank": 2}, {"id": "n1", "pagerank": 5}],
- "edges": [
- {"source": "n1", "target": "n2", "weight": 2},
- {"source": "n1", "target": "n1", "weight": 10},
- {"source": "n1", "target": "n3", "weight": 9},
- ],
- }
-
- class _GoodRetriever:
- async def search(self, *_args, **_kwargs):
- return SimpleNamespace(ids=["good"], field={"good": {"knowledge_graph_kwd": "graph", "content_with_weight": json.dumps(payload)}})
-
- monkeypatch.setattr(module.settings, "retriever", _GoodRetriever())
- res = _run(inspect.unwrap(module.knowledge_graph)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
- assert len(res["data"]["graph"]["nodes"]) == 2, res
- assert len(res["data"]["graph"]["edges"]) == 1, res
-
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: False)
- res = inspect.unwrap(module.delete_knowledge_graph)("tenant-1", "kb-1")
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
-
-
-def test_run_index_matrix_unit(monkeypatch):
- module = _load_dataset_module(monkeypatch)
-
- warnings = []
- monkeypatch.setattr(module.logging, "warning", lambda msg, *_args, **_kwargs: warnings.append(msg))
-
- # Invalid index type
- _set_request_args(monkeypatch, module, {"type": "invalid"})
- res = _run(inspect.unwrap(module.run_index)("tenant-1", "kb-1"))
- assert "Invalid index type" in res["message"], res
-
- # Missing dataset ID
- _set_request_args(monkeypatch, module, {"type": "graph"})
- res = _run(inspect.unwrap(module.run_index)("tenant-1", ""))
- assert "Dataset ID" in res["message"], res
-
- # No authorization
- _set_request_args(monkeypatch, module, {"type": "graph"})
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: False)
- res = _run(inspect.unwrap(module.run_index)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.DATA_ERROR, res
-
- # Invalid dataset ID
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
- res = _run(inspect.unwrap(module.run_index)("tenant-1", "kb-1"))
- assert "Invalid Dataset ID" in res["message"], res
-
- # Stale graphrag task + successful re-queue
- stale_kb = _KB(kb_id="kb-1", graphrag_task_id="task-old")
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, stale_kb))
- monkeypatch.setattr(module.TaskService, "get_by_id", lambda _task_id: (False, None))
- monkeypatch.setattr(module.DocumentService, "get_by_kb_id", lambda **_kwargs: ([{"id": "doc-1"}], 1))
- monkeypatch.setattr(module.dataset_api_service, "queue_raptor_o_graphrag_tasks", lambda **_kwargs: "task-new")
- monkeypatch.setattr(module.KnowledgebaseService, "update_by_id", lambda *_args, **_kwargs: True)
- _set_request_args(monkeypatch, module, {"type": "graph"})
- res = _run(inspect.unwrap(module.run_index)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
- assert any("Graph" in msg for msg in warnings), warnings
-
- # Task already running
- monkeypatch.setattr(module.TaskService, "get_by_id", lambda _task_id: (True, SimpleNamespace(progress=0)))
- res = _run(inspect.unwrap(module.run_index)("tenant-1", "kb-1"))
- assert "already running" in res["message"], res
-
- # Successful raptor run with save warning
- warnings.clear()
- no_task_kb = _KB(kb_id="kb-1", raptor_task_id="")
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, no_task_kb))
- monkeypatch.setattr(module.TaskService, "get_by_id", lambda _task_id: (False, None))
- monkeypatch.setattr(module.DocumentService, "get_by_kb_id", lambda **_kwargs: ([{"id": "doc-1"}, {"id": "doc-2"}], 2))
-
- queue_calls = {}
-
- def _queue(**kwargs):
- queue_calls.update(kwargs)
- return "queued-raptor"
-
- monkeypatch.setattr(module.dataset_api_service, "queue_raptor_o_graphrag_tasks", _queue)
- monkeypatch.setattr(module.KnowledgebaseService, "update_by_id", lambda *_args, **_kwargs: False)
- _set_request_args(monkeypatch, module, {"type": "raptor"})
- res = _run(inspect.unwrap(module.run_index)("tenant-1", "kb-1"))
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["data"]["task_id"] == "queued-raptor", res
- assert queue_calls["doc_ids"] == ["doc-1", "doc-2"], queue_calls
- assert any("Cannot save" in msg for msg in warnings), warnings
-
-
-def test_trace_index_matrix_unit(monkeypatch):
- module = _load_dataset_module(monkeypatch)
-
- # Invalid index type
- _set_request_args(monkeypatch, module, {"type": "invalid"})
- res = inspect.unwrap(module.trace_index)("tenant-1", "kb-1")
- assert "Invalid index type" in res["message"], res
-
- # Missing dataset ID
- _set_request_args(monkeypatch, module, {"type": "graph"})
- res = inspect.unwrap(module.trace_index)("tenant-1", "")
- assert "Dataset ID" in res["message"], res
-
- # No authorization
- _set_request_args(monkeypatch, module, {"type": "graph"})
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: False)
- res = inspect.unwrap(module.trace_index)("tenant-1", "kb-1")
- assert res["code"] == module.RetCode.DATA_ERROR, res
-
- # Invalid dataset ID
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
- res = inspect.unwrap(module.trace_index)("tenant-1", "kb-1")
- assert "Invalid Dataset ID" in res["message"], res
-
- # No existing task — returns empty
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, _KB(kb_id="kb-1", graphrag_task_id="")))
- res = inspect.unwrap(module.trace_index)("tenant-1", "kb-1")
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["data"] == {}, res
-
- # Task ID set but task not found — returns empty
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, _KB(kb_id="kb-1", graphrag_task_id="task-1")))
- monkeypatch.setattr(module.TaskService, "get_by_id", lambda _task_id: (False, None))
- res = inspect.unwrap(module.trace_index)("tenant-1", "kb-1")
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["data"] == {}, res
-
- # Task found — returns task data
- monkeypatch.setattr(module.TaskService, "get_by_id", lambda _task_id: (True, SimpleNamespace(to_dict=lambda: {"id": _task_id, "progress": 1})))
- res = inspect.unwrap(module.trace_index)("tenant-1", "kb-1")
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["data"]["id"] == "task-1", res
-
-
-def test_delete_index_wipe_flag_unit(monkeypatch):
- """`?wipe=false` cancels the task without deleting graph artefacts.
-
- Backend plumbing for pausing/resuming GraphRAG without losing the
- partial knowledge graph (PR #14238).
- """
- module = _load_dataset_module(monkeypatch)
-
- deleted = []
- cleared_phase_markers = []
- redis_calls = []
- deleted_tasks = []
-
- # Stub the lazy imports inside dataset_api_service.delete_index.
- redis_conn_mod = ModuleType("rag.utils.redis_conn")
-
- class _RedisConn:
- @staticmethod
- def set(key, value):
- redis_calls.append((key, value))
-
- redis_conn_mod.REDIS_CONN = _RedisConn
- monkeypatch.setitem(sys.modules, "rag.utils.redis_conn", redis_conn_mod)
-
- phase_markers_mod = ModuleType("rag.graphrag.phase_markers")
- phase_markers_mod.clear_phase_markers = lambda dataset_id: cleared_phase_markers.append(dataset_id)
- monkeypatch.setitem(sys.modules, "rag.graphrag.phase_markers", phase_markers_mod)
-
- monkeypatch.setattr(
- module.settings,
- "docStoreConn",
- SimpleNamespace(delete=lambda *args, **_kwargs: deleted.append(args)),
- )
- monkeypatch.setattr(module.TaskService, "delete_by_id", lambda task_id: deleted_tasks.append(task_id), raising=False)
-
- kb = _KB(kb_id="kb-1", graphrag_task_id="graph-task", raptor_task_id="raptor-task")
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module.KnowledgebaseService, "update_by_id", lambda *_args, **_kwargs: True)
-
- # wipe=false (graph): cancel, but no docStore.delete and no marker clear.
- _set_request_args(monkeypatch, module, {"wipe": "false"})
- res = inspect.unwrap(module.delete_index)("tenant-1", "kb-1", "graph")
- assert res["code"] == module.RetCode.SUCCESS, res
- assert ("graph-task-cancel", "x") in redis_calls, redis_calls
- assert deleted == [], f"docStore.delete must not be called when wipe=false: {deleted}"
- assert cleared_phase_markers == [], cleared_phase_markers
- assert deleted_tasks == ["graph-task"], deleted_tasks
-
- # wipe=0 (raptor): cancel, but no docStore.delete.
- deleted_tasks.clear()
- _set_request_args(monkeypatch, module, {"wipe": "0"})
- res = inspect.unwrap(module.delete_index)("tenant-1", "kb-1", "raptor")
- assert res["code"] == module.RetCode.SUCCESS, res
- assert deleted == [], f"docStore.delete must not be called when wipe=0: {deleted}"
-
- # Default (no wipe arg) preserves historical behaviour for graph: docStore
- # IS deleted and phase markers ARE cleared.
- _set_request_args(monkeypatch, module, {})
- res = inspect.unwrap(module.delete_index)("tenant-1", "kb-1", "graph")
- assert res["code"] == module.RetCode.SUCCESS, res
- assert len(deleted) == 1, f"default wipe must call docStore.delete once: {deleted}"
- assert cleared_phase_markers == ["kb-1"], cleared_phase_markers
diff --git a/test/testcases/test_web_api/test_document_app/conftest.py b/test/testcases/test_web_api/test_document_app/conftest.py
deleted file mode 100644
index cc67d82793..0000000000
--- a/test/testcases/test_web_api/test_document_app/conftest.py
+++ /dev/null
@@ -1,273 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-from test_common import bulk_upload_documents, delete_document, list_documents
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _StubKBRecord(dict):
- def __getattr__(self, item):
- try:
- return self[item]
- except KeyError as exc:
- raise AttributeError(item) from exc
-
-
[email protected](scope="function")
-def add_document_func(request, WebApiAuth, add_dataset, ragflow_tmp_dir):
- def cleanup():
- res = list_documents(WebApiAuth, {"kb_id": dataset_id})
- for doc in res["data"]["docs"]:
- delete_document(WebApiAuth, dataset_id, {"ids": [doc["id"]]})
-
- request.addfinalizer(cleanup)
-
- dataset_id = add_dataset
- return dataset_id, bulk_upload_documents(WebApiAuth, dataset_id, 1, ragflow_tmp_dir)[0]
-
-
[email protected](scope="class")
-def add_documents(request, WebApiAuth, add_dataset, ragflow_tmp_dir):
- def cleanup():
- res = list_documents(WebApiAuth, {"kb_id": dataset_id})
- for doc in res["data"]["docs"]:
- delete_document(WebApiAuth, dataset_id, {"ids": [doc["id"]]})
-
- request.addfinalizer(cleanup)
-
- dataset_id = add_dataset
- return dataset_id, bulk_upload_documents(WebApiAuth, dataset_id, 5, ragflow_tmp_dir)
-
-
[email protected](scope="function")
-def add_documents_func(request, WebApiAuth, add_dataset_func, ragflow_tmp_dir):
- def cleanup():
- res = list_documents(WebApiAuth, {"kb_id": dataset_id})
- for doc in res["data"]["docs"]:
- delete_document(WebApiAuth, dataset_id, {"ids": [doc["id"]]})
-
- request.addfinalizer(cleanup)
-
- dataset_id = add_dataset_func
- return dataset_id, bulk_upload_documents(WebApiAuth, dataset_id, 3, ragflow_tmp_dir)
-
-
-def document_app_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- deepdoc_pkg = ModuleType("deepdoc")
- deepdoc_parser_pkg = ModuleType("deepdoc.parser")
- deepdoc_parser_pkg.__path__ = []
-
- class _StubPdfParser:
- pass
-
- class _StubExcelParser:
- pass
-
- deepdoc_parser_pkg.PdfParser = _StubPdfParser
- deepdoc_pkg.parser = deepdoc_parser_pkg
- monkeypatch.setitem(sys.modules, "deepdoc", deepdoc_pkg)
- monkeypatch.setitem(sys.modules, "deepdoc.parser", deepdoc_parser_pkg)
- deepdoc_excel_module = ModuleType("deepdoc.parser.excel_parser")
- deepdoc_excel_module.RAGFlowExcelParser = _StubExcelParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.excel_parser", deepdoc_excel_module)
- deepdoc_html_module = ModuleType("deepdoc.parser.html_parser")
-
- class _StubHtmlParser:
- pass
-
- deepdoc_html_module.RAGFlowHtmlParser = _StubHtmlParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.html_parser", deepdoc_html_module)
- deepdoc_mineru_module = ModuleType("deepdoc.parser.mineru_parser")
-
- class _StubMinerUParser:
- pass
-
- deepdoc_mineru_module.MinerUParser = _StubMinerUParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.mineru_parser", deepdoc_mineru_module)
- deepdoc_mistral_module = ModuleType("deepdoc.parser.mistral_parser")
- deepdoc_mistral_module.MistralParser = _StubMinerUParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.mistral_parser", deepdoc_mistral_module)
- deepdoc_paddleocr_module = ModuleType("deepdoc.parser.paddleocr_parser")
-
- class _StubPaddleOCRParser:
- pass
-
- deepdoc_paddleocr_module.PaddleOCRParser = _StubPaddleOCRParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.paddleocr_parser", deepdoc_paddleocr_module)
- monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))
- rag_llm_cv_module = ModuleType("rag.llm.cv_model")
- rag_llm_cv_module.GptV4 = type("GptV4", (), {})
- monkeypatch.setitem(sys.modules, "rag.llm.cv_model", rag_llm_cv_module)
-
- stub_apps = ModuleType("api.apps")
- stub_apps.__path__ = [str(repo_root / "api" / "apps")]
- stub_apps.AUTH_JWT = "jwt"
- stub_apps.AUTH_API = "api"
- stub_apps.AUTH_BETA = "beta"
- stub_apps.current_user = SimpleNamespace(id="user-1")
- stub_apps.login_required = lambda func=None, **_kwargs: (lambda wrapped: wrapped) if func is None else func
- monkeypatch.setitem(sys.modules, "api.apps", stub_apps)
-
- stub_apps_services = ModuleType("api.apps.services")
- stub_apps_services.__path__ = [str(repo_root / "api" / "apps" / "services")]
- monkeypatch.setitem(sys.modules, "api.apps.services", stub_apps_services)
-
- document_api_service_mod = ModuleType("api.apps.services.document_api_service")
- document_api_service_mod.validate_document_update_fields = lambda *_args, **_kwargs: (None, None)
- document_api_service_mod.map_doc_keys = lambda doc: doc.to_dict() if hasattr(doc, "to_dict") else doc
-
- def _map_doc_keys_with_run_status(doc, run_status="0"):
- payload = doc if isinstance(doc, dict) else doc.to_dict()
- return {**payload, "run": run_status}
-
- document_api_service_mod.map_doc_keys_with_run_status = _map_doc_keys_with_run_status
- document_api_service_mod.update_document_name_only = lambda *_args, **_kwargs: None
- document_api_service_mod.update_chunk_method = lambda *_args, **_kwargs: None
- document_api_service_mod.update_document_status_only = lambda *_args, **_kwargs: None
- document_api_service_mod.reset_document_for_reparse = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.apps.services.document_api_service", document_api_service_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "document_api.py"
- spec = importlib.util.spec_from_file_location("test_document_app_unit", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- return module
-
-
-def document_rest_api_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- deepdoc_pkg = ModuleType("deepdoc")
- deepdoc_parser_pkg = ModuleType("deepdoc.parser")
- deepdoc_parser_pkg.__path__ = []
-
- class _StubPdfParser:
- pass
-
- class _StubExcelParser:
- pass
-
- deepdoc_parser_pkg.PdfParser = _StubPdfParser
- deepdoc_pkg.parser = deepdoc_parser_pkg
- monkeypatch.setitem(sys.modules, "deepdoc", deepdoc_pkg)
- monkeypatch.setitem(sys.modules, "deepdoc.parser", deepdoc_parser_pkg)
- deepdoc_excel_module = ModuleType("deepdoc.parser.excel_parser")
- deepdoc_excel_module.RAGFlowExcelParser = _StubExcelParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.excel_parser", deepdoc_excel_module)
- deepdoc_html_module = ModuleType("deepdoc.parser.html_parser")
-
- class _StubHtmlParser:
- pass
-
- deepdoc_html_module.RAGFlowHtmlParser = _StubHtmlParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.html_parser", deepdoc_html_module)
- deepdoc_mineru_module = ModuleType("deepdoc.parser.mineru_parser")
-
- class _StubMinerUParser:
- pass
-
- deepdoc_mineru_module.MinerUParser = _StubMinerUParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.mineru_parser", deepdoc_mineru_module)
- deepdoc_mistral_module = ModuleType("deepdoc.parser.mistral_parser")
- deepdoc_mistral_module.MistralParser = _StubMinerUParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.mistral_parser", deepdoc_mistral_module)
- deepdoc_paddleocr_module = ModuleType("deepdoc.parser.paddleocr_parser")
-
- class _StubPaddleOCRParser:
- pass
-
- deepdoc_paddleocr_module.PaddleOCRParser = _StubPaddleOCRParser
- monkeypatch.setitem(sys.modules, "deepdoc.parser.paddleocr_parser", deepdoc_paddleocr_module)
- monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))
- rag_llm_cv_module = ModuleType("rag.llm.cv_model")
- rag_llm_cv_module.GptV4 = type("GptV4", (), {})
- monkeypatch.setitem(sys.modules, "rag.llm.cv_model", rag_llm_cv_module)
-
- stub_apps = ModuleType("api.apps")
- stub_apps.__path__ = [str(repo_root / "api" / "apps")]
- stub_apps.AUTH_JWT = "jwt"
- stub_apps.AUTH_API = "api"
- stub_apps.AUTH_BETA = "beta"
- stub_apps.current_user = SimpleNamespace(id="user-1")
- stub_apps.login_required = lambda func=None, **_kwargs: (lambda wrapped: wrapped) if func is None else func
- monkeypatch.setitem(sys.modules, "api.apps", stub_apps)
-
- stub_apps_services = ModuleType("api.apps.services")
- stub_apps_services.__path__ = [str(repo_root / "api" / "apps" / "services")]
- monkeypatch.setitem(sys.modules, "api.apps.services", stub_apps_services)
-
- document_api_service_mod = ModuleType("api.apps.services.document_api_service")
- document_api_service_mod.validate_document_update_fields = lambda *_args, **_kwargs: (None, None)
- document_api_service_mod.map_doc_keys = lambda doc: doc.to_dict() if hasattr(doc, "to_dict") else doc
-
- def _map_doc_keys_with_run_status(doc, run_status="0"):
- payload = doc if isinstance(doc, dict) else doc.to_dict()
- return {**payload, "run": run_status}
-
- document_api_service_mod.map_doc_keys_with_run_status = _map_doc_keys_with_run_status
- document_api_service_mod.update_document_name_only = lambda *_args, **_kwargs: None
- document_api_service_mod.update_chunk_method = lambda *_args, **_kwargs: None
- document_api_service_mod.update_document_status_only = lambda *_args, **_kwargs: None
- document_api_service_mod.reset_document_for_reparse = lambda *_args, **_kwargs: None
- monkeypatch.setitem(sys.modules, "api.apps.services.document_api_service", document_api_service_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "document_api.py"
- spec = importlib.util.spec_from_file_location("test_document_api_unit", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_by_id",
- lambda dataset_id: (
- True,
- _StubKBRecord(
- id=dataset_id,
- tenant_id="tenant1",
- name="kb",
- parser_id="parser",
- pipeline_id="pipe",
- parser_config={},
- ),
- ),
- )
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: True)
- return module
diff --git a/test/testcases/test_web_api/test_document_app/test_create_document.py b/test/testcases/test_web_api/test_document_app/test_create_document.py
deleted file mode 100644
index c40bbd9167..0000000000
--- a/test/testcases/test_web_api/test_document_app/test_create_document.py
+++ /dev/null
@@ -1,226 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-import string
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from types import SimpleNamespace
-
-import pytest
-from test_common import create_document, list_datasets
-from configs import DOCUMENT_NAME_LIMIT, INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from utils.file_utils import create_txt_file
-from api.constants import FILE_NAME_LEN_LIMIT
-
-
-class _StubKBRecord(dict):
- def __getattr__(self, item):
- try:
- return self[item]
- except KeyError as exc:
- raise AttributeError(item) from exc
-
-
[email protected]("clear_datasets")
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = create_document(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestDocumentCreate:
- @pytest.mark.p3
- def test_filename_empty(self, WebApiAuth, add_dataset_func):
- kb_id = add_dataset_func
- payload = {"name": "", "kb_id": kb_id}
- res = create_document(WebApiAuth, payload)
- assert res["code"] == 101, res
- assert res["message"] == "File name can't be empty.", res
-
- @pytest.mark.p2
- def test_filename_max_length(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
- fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt")
- res = create_document(WebApiAuth, {"name": fp.name, "kb_id": kb_id})
- assert res["code"] == 0, res
- assert res["data"]["name"] == fp.name, res
-
- @pytest.mark.p2
- def test_invalid_kb_id(self, WebApiAuth):
- res = create_document(WebApiAuth, {"name": "ragflow_test.txt", "kb_id": "invalid_kb_id"})
- assert res["code"] == 102, res
- assert res["message"] == "Can't find the dataset with ID invalid_kb_id!", res
-
- @pytest.mark.p3
- def test_filename_special_characters(self, WebApiAuth, add_dataset_func):
- kb_id = add_dataset_func
- illegal_chars = '<>:"/\\|?*'
- translation_table = str.maketrans({char: "_" for char in illegal_chars})
- safe_filename = string.punctuation.translate(translation_table)
- filename = f"{safe_filename}.txt"
-
- res = create_document(WebApiAuth, {"name": filename, "kb_id": kb_id})
- assert res["code"] == 0, res
- assert res["data"]["dataset_id"] == kb_id, res
- assert res["data"]["name"] == filename, f"Expected: {filename}, Got: {res['data']['name']}"
-
- @pytest.mark.p3
- def test_concurrent_upload(self, WebApiAuth, add_dataset_func):
- kb_id = add_dataset_func
-
- count = 20
- filenames = [f"ragflow_test_{i}.txt" for i in range(count)]
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(create_document, WebApiAuth, {"name": name, "kb_id": kb_id}) for name in filenames]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures), responses
-
- res = list_datasets(WebApiAuth, {"id": kb_id})
- assert res["data"][0]["document_count"] == count, res
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-class TestDocumentCreateUnit:
- def test_filename_too_long(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- long_name = "a" * (FILE_NAME_LEN_LIMIT + 1)
-
- async def fake_request_json():
- return {"name": long_name}
-
- monkeypatch.setattr(module, "get_request_json", fake_request_json)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"type": "empty"}))
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 101
- assert res["message"] == f"File name must be {FILE_NAME_LEN_LIMIT} bytes or less."
-
- def test_filename_whitespace(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
-
- async def fake_request_json():
- return {"name": " "}
-
- monkeypatch.setattr(module, "get_request_json", fake_request_json)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"type": "empty"}))
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 101
- assert res["message"] == "File name can't be empty."
-
- def test_kb_not_found(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
-
- async def fake_request_json():
- return {"name": "doc.txt"}
-
- monkeypatch.setattr(module, "get_request_json", fake_request_json)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"type": "empty"}))
- res = _run(module.upload_document(dataset_id="missing"))
- assert res["code"] == 102
- assert res["message"] == "Can't find the dataset with ID missing!"
-
- def test_duplicate_name(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = _StubKBRecord(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [object()])
-
- async def fake_request_json():
- return {"name": "doc.txt"}
-
- monkeypatch.setattr(module, "get_request_json", fake_request_json)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"type": "empty"}))
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 102
- assert "Duplicated document name" in res["message"]
-
- def test_root_folder_missing(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = _StubKBRecord(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.FileService, "get_kb_folder", lambda *_args, **_kwargs: None)
-
- async def fake_request_json():
- return {"name": "doc.txt"}
-
- monkeypatch.setattr(module, "get_request_json", fake_request_json)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"type": "empty"}))
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 102
- assert res["message"] == "Cannot find the root folder."
-
- def test_kb_folder_missing(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = _StubKBRecord(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.FileService, "get_kb_folder", lambda *_args, **_kwargs: {"id": "root"})
- monkeypatch.setattr(module.FileService, "new_a_file_from_kb", lambda *_args, **_kwargs: None)
-
- async def fake_request_json():
- return {"name": "doc.txt"}
-
- monkeypatch.setattr(module, "get_request_json", fake_request_json)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"type": "empty"}))
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 102
- assert res["message"] == "Cannot find the kb folder for this file."
-
- def test_success(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = _StubKBRecord(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module.DocumentService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.FileService, "get_kb_folder", lambda *_args, **_kwargs: {"id": "root"})
- monkeypatch.setattr(module.FileService, "new_a_file_from_kb", lambda *_args, **_kwargs: {"id": "folder"})
-
- class _Doc:
- def __init__(self, doc_id):
- self.id = doc_id
-
- def to_json(self):
- return {"id": self.id, "name": "doc.txt", "kb_id": "kb1"}
-
- def to_dict(self):
- return {"id": self.id, "name": "doc.txt", "kb_id": "kb1"}
-
- monkeypatch.setattr(module.DocumentService, "insert", lambda _doc: _Doc("doc1"))
- monkeypatch.setattr(module.FileService, "add_file_from_kb", lambda *_args, **_kwargs: None)
-
- async def fake_request_json():
- return {"name": "doc.txt"}
-
- monkeypatch.setattr(module, "get_request_json", fake_request_json)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"type": "empty"}))
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 0
- assert res["data"]["id"] == "doc1"
diff --git a/test/testcases/test_web_api/test_document_app/test_document_metadata.py b/test/testcases/test_web_api/test_document_app/test_document_metadata.py
deleted file mode 100644
index 23c157a1ec..0000000000
--- a/test/testcases/test_web_api/test_document_app/test_document_metadata.py
+++ /dev/null
@@ -1,649 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-from types import SimpleNamespace
-
-import pytest
-from test_common import (
- document_change_status,
- document_filter,
- document_infos,
- document_metadata_summary,
- document_metadata_update,
- document_update_metadata_setting,
- bulk_upload_documents,
- delete_document,
- document_update,
-)
-
-from common.constants import RetCode
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-INVALID_AUTH_CASES = [
- (None, 401, "Unauthorized"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "Unauthorized"),
-]
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_filter_auth_invalid(self, invalid_auth, expected_code, expected_fragment):
- res = document_filter(invalid_auth, "kb_id", {})
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_infos_auth_invalid(self, invalid_auth, expected_code, expected_fragment):
- res = document_infos(invalid_auth, "kb_id", {"doc_ids": ["doc_id"]})
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- ## The inputs has been changed to add 'doc_ids'
- ## TODO:
- # @pytest.mark.p2
- # @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- # def test_metadata_summary_auth_invalid(self, invalid_auth, expected_code, expected_fragment):
- # res = document_metadata_summary(invalid_auth, {"kb_id": "kb_id"})
- # assert res["code"] == expected_code, res
- # assert expected_fragment in res["message"], res
-
- ## The inputs has been changed to deprecate 'selector'
- ## TODO:
- # @pytest.mark.p2
- # @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- # def test_metadata_update_auth_invalid(self, invalid_auth, expected_code, expected_fragment):
- # res = document_metadata_update(invalid_auth, {"kb_id": "kb_id", "selector": {"document_ids": ["doc_id"]}, "updates": []})
- # assert res["code"] == expected_code, res
- # assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_update_metadata_setting_auth_invalid(self, invalid_auth, expected_code, expected_fragment):
- res = document_update_metadata_setting(invalid_auth, "kb_id", "doc_id", {"metadata": {}})
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_change_status_auth_invalid(self, invalid_auth, expected_code, expected_fragment, add_dataset_func):
- dataset_id = add_dataset_func
- res = document_change_status(invalid_auth, dataset_id, {"doc_ids": ["doc_id"], "status": "1"})
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
-
-class TestDocumentMetadata:
- @pytest.mark.p2
- def test_filter(self, WebApiAuth, add_dataset_func):
- kb_id = add_dataset_func
- res = document_filter(WebApiAuth, kb_id, {})
- assert res["code"] == 0, res
- assert "filter" in res["data"], res
- assert "total" in res["data"], res
-
- @pytest.mark.p2
- def test_infos(self, WebApiAuth, add_document_func):
- dataset_id, doc_id = add_document_func
- res = document_infos(WebApiAuth, dataset_id, {"ids": [doc_id]})
- assert res["code"] == 0, res
- docs = res["data"]["docs"]
- assert len(docs) == 1, docs
- assert docs[0]["id"] == doc_id, res
-
- ## The inputs has been changed to add 'doc_ids'
- ## TODO:
- # @pytest.mark.p2
- # def test_metadata_summary(self, WebApiAuth, add_document_func):
- # kb_id, _ = add_document_func
- # res = document_metadata_summary(WebApiAuth, {"kb_id": kb_id})
- # assert res["code"] == 0, res
- # assert isinstance(res["data"]["summary"], dict), res
-
- ## The inputs has been changed to deprecate 'selector'
- ## TODO:
- # @pytest.mark.p2
- # def test_metadata_update(self, WebApiAuth, add_document_func):
- # kb_id, doc_id = add_document_func
- # payload = {
- # "kb_id": kb_id,
- # "selector": {"document_ids": [doc_id]},
- # "updates": [{"key": "author", "value": "alice"}],
- # "deletes": [],
- # }
- # res = document_metadata_update(WebApiAuth, payload)
- # assert res["code"] == 0, res
- # assert res["data"]["matched_docs"] == 1, res
- # info_res = document_infos(WebApiAuth, {"doc_ids": [doc_id]})
- # assert info_res["code"] == 0, info_res
- # meta_fields = info_res["data"][0].get("meta_fields", {})
- # assert meta_fields.get("author") == "alice", info_res
-
- ## The inputs has been changed to deprecate 'selector'
- ## TODO:
- # @pytest.mark.p2
- # def test_update_metadata_setting(self, WebApiAuth, add_document_func):
- # _, doc_id = add_document_func
- # metadata = {"source": "test"}
- # res = document_update_metadata_setting(WebApiAuth, {"doc_id": doc_id, "metadata": metadata})
- # assert res["code"] == 0, res
- # assert res["data"]["id"] == doc_id, res
- # assert res["data"]["parser_config"]["metadata"] == metadata, res
-
- @pytest.mark.p2
- def test_change_status(self, WebApiAuth, add_document_func):
- dataset_id, doc_id = add_document_func
- res = document_change_status(WebApiAuth, dataset_id, {"doc_ids": [doc_id], "status": "1"})
-
- assert res["code"] == 0, res
- assert res["data"][doc_id]["status"] == "1", res
- info_res = document_infos(WebApiAuth, dataset_id, {"ids": [doc_id]})
-
- assert info_res["code"] == 0, info_res
- assert info_res["data"]["docs"][0]["status"] == "1", info_res
-
- @pytest.mark.p2
- def test_update_document_change_parser(self, WebApiAuth, add_document_func):
- """Test updating document chunk_method via PATCH /api/v1/datasets/<dataset_id>/documents/<doc_id>."""
- dataset_id, doc_id = add_document_func
-
- # Get initial document info
- res = document_infos(WebApiAuth, dataset_id, {"doc_ids": [doc_id]})
-
- assert res["code"] == 0, res
- original_parser_id = res["data"]["docs"][0].get("parser_id")
-
- res = document_update(WebApiAuth, dataset_id, doc_id, {"chunk_method": "invalid_chunk_method"})
- assert res["code"] == 102, res
- assert res["message"] == "Field: <chunk_method> - Message: <`chunk_method` invalid_chunk_method doesn't exist> - Value: <invalid_chunk_method>", res
-
- # Change to a different parser (naive bayes)
- # valid_chunk_method = {"naive", "manual", "qa", "table", "paper", "book", "laws", "presentation", "picture", "one", "knowledge_graph", "email", "tag"}
- new_parser_id = "naive"
- if original_parser_id == new_parser_id:
- new_parser_id = "paper"
- document_update(WebApiAuth, dataset_id, doc_id, {"chunk_method": new_parser_id})
-
- # Verify the document was updated
- res = document_infos(WebApiAuth, dataset_id, {"doc_ids": [doc_id]})
-
- assert res["code"] == 0, res
- assert res["data"]["docs"][0]["chunk_method"] == new_parser_id, res
-
- @pytest.mark.p2
- def test_update_document_change_pipeline(self, WebApiAuth, add_document_func):
- """Test updating document pipeline via PATCH /api/v1/datasets/<dataset_id>/documents/<doc_id>."""
- dataset_id, doc_id = add_document_func
-
- # Get initial document info
- res = document_infos(WebApiAuth, dataset_id, {"doc_ids": [doc_id]})
- assert res["code"] == 0, res
- original_pipeline_id = res["data"]["docs"][0].get("pipeline_id")
-
- # Change to a different pipeline (if available)
- # Note: This test assumes there's at least one other pipeline available
- new_pipeline_id = "general" if original_pipeline_id != "general" else "resume"
- res = document_update(WebApiAuth, dataset_id, doc_id, {"pipeline_id": new_pipeline_id})
- assert res["code"] == 0, res
-
- # Verify the document was updated
- res = document_infos(WebApiAuth, dataset_id, {"doc_ids": [doc_id]})
- assert res["code"] == 0, res
- assert res["data"]["docs"][0]["pipeline_id"] == new_pipeline_id, res
-
-
-class TestDocumentMetadataNegative:
- @pytest.mark.p2
- def test_filter_missing_kb_id(self, WebApiAuth, add_document_func):
- kb_id, doc_id = add_document_func
- res = document_filter(WebApiAuth, "", {"ids": [doc_id]})
- assert res["code"] == 102, res
- assert "lacks permission for dataset" in res["message"], res
-
- @pytest.mark.p3
- def test_metadata_summary_missing_kb_id(self, WebApiAuth, add_document_func):
- _, doc_id = add_document_func
- res = document_metadata_summary(WebApiAuth, {"doc_ids": [doc_id]})
- assert res["code"] == 101, res
- assert "KB ID" in res["message"], res
-
- ## The inputs has been changed to deprecate 'selector'
- ## TODO:
- # @pytest.mark.p3
- # def test_metadata_update_missing_kb_id(self, WebApiAuth, add_document_func):
- # _, doc_id = add_document_func
- # res = document_metadata_update(WebApiAuth, {"selector": {"document_ids": [doc_id]}, "updates": []})
- # assert res["code"] == 101, res
- # assert "KB ID" in res["message"], res
-
- @pytest.mark.p3
- def test_infos_invalid_doc_id(self, WebApiAuth):
- res = document_infos(WebApiAuth, {"doc_ids": ["invalid_id"]})
- assert res["code"] == 109, res
- assert "No authorization" in res["message"], res
-
- @pytest.mark.p3
- def test_update_metadata_setting_missing_metadata(self, WebApiAuth, add_document_func):
- _, doc_id = add_document_func
- res = document_update_metadata_setting(WebApiAuth, {"doc_id": doc_id})
- assert res["code"] == 101, res
- assert "required argument are missing" in res["message"], res
- assert "metadata" in res["message"], res
-
- @pytest.mark.p2
- def test_update_metadata_setting_not_found(self, WebApiAuth, add_document_func):
- """Test updating metadata setting for a non-existent document returns error."""
- dataset_id, doc_id = add_document_func
- # First delete the document
- delete_res = delete_document(WebApiAuth, dataset_id, {"ids": [doc_id]})
- assert delete_res["code"] == 0, delete_res
-
- # Now try to update metadata setting for the deleted document
- res = document_update_metadata_setting(WebApiAuth, dataset_id, doc_id, {"metadata": {"author": "test"}})
- assert res["code"] == 102, res
- assert f"document {doc_id} not found in dataset {dataset_id}" in res["message"], res
-
- @pytest.mark.p3
- def test_change_status_invalid_status(self, WebApiAuth, add_document_func):
- dataset_id, doc_id = add_document_func
- res = document_change_status(WebApiAuth, dataset_id, {"doc_ids": [doc_id], "status": "2"})
- assert res["code"] == 101, res
- assert "Status" in res["message"], res
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-class _DummyArgs:
- def __init__(self, args=None):
- self._args = args or {}
-
- def get(self, key, default=None):
- return self._args.get(key, default)
-
- def getlist(self, key):
- value = self._args.get(key, [])
- if isinstance(value, list):
- return value
- return [value]
-
-
-class _DummyRequest:
- def __init__(self, args=None):
- self.args = _DummyArgs(args)
-
-
-class _DummyResponse:
- def __init__(self, data=None):
- self.data = data
- self.headers = {}
-
-
-class TestDocumentMetadataUnit:
- def _allow_kb(self, module, monkeypatch, kb_id="kb1", tenant_id="tenant1"):
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(tenant_id=tenant_id)])
- monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: True if _kwargs.get("id") == kb_id else False)
-
- @pytest.mark.p3
- def test_update_metadata_missing_dataset_id(self, WebApiAuth, add_document_func):
- """Test the new unified update_metadata API - missing dataset_id."""
- # Call with empty dataset_id (should fail validation)
- res = document_metadata_update(WebApiAuth, "", {"dataset_id": "", "selector": {"document_ids": ["doc1"]}, "updates": []})
- assert res["code"] == 100
- assert res["message"] == "<MethodNotAllowed '405: Method Not Allowed'>", res
-
- @pytest.mark.p3
- def test_update_metadata_success(self, WebApiAuth, add_document_func):
- """Test the new unified update_metadata API - success case."""
- kb_id, doc_id = add_document_func
- res = document_metadata_update(WebApiAuth, kb_id, {"selector": {"document_ids": [doc_id]}, "updates": [{"key": "author", "value": "test_author"}], "deletes": []})
- assert res["code"] == 0, res
-
- @pytest.mark.p3
- def test_update_metadata_invalid_delete_item(self, WebApiAuth, add_document_func):
- """Test the new unified update_metadata API - invalid delete item."""
- kb_id, doc_id = add_document_func
- res = document_metadata_update(
- WebApiAuth,
- kb_id,
- {
- "selector": {"document_ids": [doc_id]},
- "updates": [],
- "deletes": [{}], # Invalid - missing key
- },
- )
- assert res["code"] == 102
- assert "Each delete requires key" in res["message"], res
-
- def test_get_route_not_found_success_and_exception_unit(self, document_app_module, monkeypatch):
- module = document_app_module
-
- # Cross-tenant access is denied -> "document not found" (no ID enumeration).
- # Stub get_by_id to a valid document so the test can only pass via the
- # accessible() early return; if that check ever regresses, the route would
- # proceed and the assertions below would no longer match.
- accessible_calls = []
-
- def fake_accessible_denied(doc_id, user_id):
- accessible_calls.append((doc_id, user_id))
- return False
-
- monkeypatch.setattr(module.DocumentService, "accessible", fake_accessible_denied)
- monkeypatch.setattr(
- module.DocumentService,
- "get_by_id",
- lambda _doc_id: (True, SimpleNamespace(name="real.bin", type=module.FileType.OTHER.value)),
- )
- res = _run(module.get("doc1"))
- assert res["code"] == RetCode.DATA_ERROR
- assert "document not found" in res["message"]
- assert accessible_calls == [("doc1", "user-1")]
-
- # From here on the user is authorized; exercise the original branches.
- monkeypatch.setattr(module.DocumentService, "accessible", lambda _doc_id, _user_id: True)
-
- monkeypatch.setattr(module.DocumentService, "get_by_id", lambda _doc_id: (False, None))
- res = _run(module.get("doc1"))
- assert res["code"] == RetCode.DATA_ERROR
- assert "document not found" in res["message"]
-
- async def fake_thread_pool_exec(*_args, **_kwargs):
- return b"blob-data"
-
- async def fake_make_response(data):
- return _DummyResponse(data)
-
- monkeypatch.setattr(module.DocumentService, "get_by_id", lambda _doc_id: (True, SimpleNamespace(name="image.abc", type=module.FileType.VISUAL.value)))
- monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("bucket", "name"))
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", SimpleNamespace(get=lambda *_args, **_kwargs: b"blob-data"))
- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)
- monkeypatch.setattr(module, "make_response", fake_make_response)
- monkeypatch.setattr(
- module,
- "apply_safe_file_response_headers",
- lambda response, content_type, extension: response.headers.update({"content_type": content_type, "extension": extension}),
- )
- res = _run(module.get("doc1"))
- assert isinstance(res, _DummyResponse)
- assert res.data == b"blob-data"
- assert res.headers["content_type"] == "image/abc"
- assert res.headers["extension"] == "abc"
-
- monkeypatch.setattr(module.DocumentService, "get_by_id", lambda _doc_id: (_ for _ in ()).throw(RuntimeError("get boom")))
- monkeypatch.setattr(module, "server_error_response", lambda e: {"code": 500, "message": str(e)})
- res = _run(module.get("doc1"))
- assert res["code"] == 500
- assert "get boom" in res["message"]
-
- def test_download_attachment_success_and_exception_unit(self, document_app_module, monkeypatch):
- module = document_app_module
- monkeypatch.setattr(module, "request", _DummyRequest(args={"ext": "abc"}))
-
- # Cross-tenant access is denied -> "document not found" (no ID enumeration).
- accessible_calls = []
-
- def fake_accessible_denied(doc_id, user_id):
- accessible_calls.append((doc_id, user_id))
- return False
-
- monkeypatch.setattr(module.DocumentService, "accessible", fake_accessible_denied)
- res = _run(module.download_attachment(attachment_id="att1"))
- assert res["code"] == RetCode.DATA_ERROR
- assert "document not found" in res["message"]
- assert accessible_calls == [("att1", "user-1")]
-
- # From here on the user is authorized; exercise the original branches.
- monkeypatch.setattr(module.DocumentService, "accessible", lambda _doc_id, _user_id: True)
-
- async def fake_thread_pool_exec(*_args, **_kwargs):
- return b"attachment"
-
- async def fake_make_response(data):
- return _DummyResponse(data)
-
- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)
- monkeypatch.setattr(module, "make_response", fake_make_response)
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", SimpleNamespace(get=lambda *_args, **_kwargs: b"attachment"))
- monkeypatch.setattr(
- module,
- "apply_safe_file_response_headers",
- lambda response, content_type, extension: response.headers.update({"content_type": content_type, "extension": extension}),
- )
- res = _run(module.download_attachment(attachment_id="att1"))
- assert isinstance(res, _DummyResponse)
- assert res.data == b"attachment"
- assert res.headers["content_type"] == "application/abc"
- assert res.headers["extension"] == "abc"
-
- async def raise_error(*_args, **_kwargs):
- raise RuntimeError("download boom")
-
- monkeypatch.setattr(module, "thread_pool_exec", raise_error)
- monkeypatch.setattr(module, "server_error_response", lambda e: {"code": 500, "message": str(e)})
- res = _run(module.download_attachment(attachment_id="att1"))
- assert res["code"] == 500
- assert "download boom" in res["message"]
-
- def test_download_document_rejects_other_tenant_unit(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- monkeypatch.setattr(module.DocumentService, "accessible", lambda _doc_id, _user_id: False)
-
- res = _run(module.download_document("doc1"))
- assert res["code"] == RetCode.DATA_ERROR
- assert "document not found" in res["message"]
-
- def test_dataset_document_download_rejects_other_tenant_unit(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda kb_id, user_id: False)
- monkeypatch.setattr(module.DocumentService, "accessible", lambda _doc_id, _user_id: True)
-
- res = _run(module.download("kb1", "doc1"))
- assert res["code"] == RetCode.DATA_ERROR
- assert "document not found" in res["message"]
-
- @pytest.mark.p2
- def test_get_preview_missing_blob_unit(self, document_app_module, monkeypatch):
- module = document_app_module
-
- async def fake_thread_pool_exec(*_args, **_kwargs):
- return None
-
- monkeypatch.setattr(module.DocumentService, "accessible", lambda _doc_id, _user_id: True)
- monkeypatch.setattr(
- module.DocumentService,
- "get_by_id",
- lambda _doc_id: (True, SimpleNamespace(name="report.pdf", type=module.FileType.OTHER.value)),
- )
- monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("bucket", "name"))
- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)
- res = _run(module.get("doc1"))
- assert res["code"] == RetCode.DATA_ERROR
- assert res["message"] == "This file is empty."
-
- @pytest.mark.p2
- def test_get_artifact_denied_without_session_reference_unit(self, document_app_module, monkeypatch):
- module = document_app_module
- filename = "a1b2c3d4e5f6789012345678901234abcd.png"
-
- monkeypatch.setattr(module, "_sandbox_artifact_dialog_ids_for_user", lambda *_args, **_kwargs: [])
- res = _run(module.get_artifact(filename))
- assert res["code"] == RetCode.DATA_ERROR
- assert res["message"] == "Artifact not found."
-
- @pytest.mark.p2
- def test_get_artifact_denied_when_agent_not_accessible_unit(self, document_app_module, monkeypatch):
- module = document_app_module
- filename = "a1b2c3d4e5f6789012345678901234abcd.png"
-
- monkeypatch.setattr(module, "_sandbox_artifact_dialog_ids_for_user", lambda *_args, **_kwargs: ["agent-1"])
- monkeypatch.setattr(module.UserCanvasService, "accessible", lambda *_args, **_kwargs: False)
- res = _run(module.get_artifact(filename))
- assert res["code"] == RetCode.DATA_ERROR
- assert res["message"] == "Artifact not found."
-
- @pytest.mark.p2
- def test_get_artifact_success_and_missing_blob_unit(self, document_app_module, monkeypatch):
- module = document_app_module
- filename = "a1b2c3d4e5f6789012345678901234abcd.png"
-
- class _Headers(dict):
- def set(self, key, value):
- self[key] = value
-
- class _ArtifactResponse:
- def __init__(self, data):
- self.data = data
- self.headers = _Headers()
-
- monkeypatch.setattr(module, "_sandbox_artifact_dialog_ids_for_user", lambda *_args, **_kwargs: ["agent-1"])
- monkeypatch.setattr(module.UserCanvasService, "accessible", lambda *_args, **_kwargs: True)
-
- async def fake_thread_pool_exec(fn, *args, **kwargs):
- return fn(*args, **kwargs)
-
- async def fake_make_response(data):
- return _ArtifactResponse(data)
-
- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)
- monkeypatch.setattr(module, "make_response", fake_make_response)
- monkeypatch.setattr(
- module,
- "apply_safe_file_response_headers",
- lambda response, content_type, extension: response.headers.update({"content_type": content_type, "extension": extension}),
- )
- monkeypatch.setattr(
- module.settings,
- "STORAGE_IMPL",
- SimpleNamespace(get=lambda *_args, **_kwargs: b"artifact-bytes"),
- )
-
- res = _run(module.get_artifact(filename))
- assert isinstance(res, _ArtifactResponse)
- assert res.data == b"artifact-bytes"
- assert res.headers["content_type"] == "image/png"
-
- monkeypatch.setattr(
- module.settings,
- "STORAGE_IMPL",
- SimpleNamespace(get=lambda *_args, **_kwargs: None),
- )
- res = _run(module.get_artifact(filename))
- assert res["code"] == RetCode.DATA_ERROR
- assert res["message"] == "Artifact not found."
-
-
-class TestDocumentBatchChangeStatus:
- @pytest.mark.p2
- def test_change_status_partial_failure_matrix(self, WebApiAuth, add_dataset, ragflow_tmp_dir):
- """
- E2E test for partial failure matrix in batch document status change.
-
- This test creates multiple documents and verifies that the batch status change
- operation handles various failure scenarios correctly.
- """
-
- dataset_id = add_dataset
-
- # Create multiple documents for testing
- doc_ids = bulk_upload_documents(WebApiAuth, dataset_id, 3, ragflow_tmp_dir)
- assert len(doc_ids) == 3, f"Expected 3 documents, got {len(doc_ids)}"
-
- try:
- # Test batch status change with all valid documents
- # This should succeed since all documents are valid
- res = document_change_status(WebApiAuth, dataset_id, {"doc_ids": doc_ids, "status": "1"})
-
- # Verify the response structure
- assert res["code"] == 0, f"Expected success code 0, got {res}"
- assert res["data"] is not None, "Response data should not be None"
-
- # Verify each document status was updated
- for doc_id in doc_ids:
- assert doc_id in res["data"], f"Document {doc_id} should be in response"
- assert res["data"][doc_id]["status"] == "1", f"Document {doc_id} status should be 1"
-
- # Verify the status was actually updated in the database
- info_res = document_infos(WebApiAuth, dataset_id, {"ids": doc_ids})
- assert info_res["code"] == 0, info_res
-
- for doc in info_res["data"]["docs"]:
- assert doc["status"] == "1", f"Document {doc['id']} status should be 1 in database"
-
- finally:
- # Cleanup: delete all documents
- delete_document(WebApiAuth, dataset_id, {"ids": doc_ids})
-
- @pytest.mark.p2
- def test_change_status_invalid_status(self, WebApiAuth, add_document_func):
- """
- E2E test for invalid status value in batch document status change.
-
- This test verifies that the API returns an error when an invalid status
- value (not 0 or 1) is provided.
- """
-
- dataset_id, doc_id = add_document_func
-
- # Try to update with invalid status "2" (only 0 and 1 are valid)
- res = document_change_status(WebApiAuth, dataset_id, {"doc_ids": [doc_id], "status": "2"})
-
- # Verify the error response
- assert res["code"] == 101, f"Expected error code 101, got {res}"
- assert "Status" in res["message"], f"Error message should mention Status: {res}"
-
- @pytest.mark.p2
- def test_change_status_all_success(self, WebApiAuth, add_document_func):
- """
- E2E test for successful batch document status change.
-
- This test verifies that all documents are successfully updated
- when valid status values are provided.
- """
-
- dataset_id, doc_id = add_document_func
-
- # Verify initial status is "1" (enabled)
- info_res = document_infos(WebApiAuth, dataset_id, {"ids": [doc_id]})
- assert info_res["code"] == 0, info_res
- assert info_res["data"]["docs"][0]["status"] == "1", "Initial status should be 1"
-
- # Update status to "0" (disabled)
- res = document_change_status(WebApiAuth, dataset_id, {"doc_ids": [doc_id], "status": "0"})
-
- # Verify success
- assert res["code"] == 0, f"Expected success code 0, got {res}"
- assert res["data"][doc_id]["status"] == "0", "Document status should be 0"
-
- # Verify the status was actually updated in the database
- info_res = document_infos(WebApiAuth, dataset_id, {"ids": [doc_id]})
- assert info_res["code"] == 0, info_res
- assert info_res["data"]["docs"][0]["status"] == "0", "Document status should be 0 in database"
-
- # Update status to "1" (enabled)
- res = document_change_status(WebApiAuth, dataset_id, {"doc_ids": [doc_id], "status": "1"})
-
- # Verify success
- assert res["code"] == 0, f"Expected success code 0, got {res}"
- assert res["data"][doc_id]["status"] == "1", "Document status should be 0"
-
- # Verify the status was actually updated in the database
- info_res = document_infos(WebApiAuth, dataset_id, {"ids": [doc_id]})
- assert info_res["code"] == 0, info_res
- assert info_res["data"]["docs"][0]["status"] == "1", "Document status should be 1 in database"
diff --git a/test/testcases/test_web_api/test_document_app/test_list_documents.py b/test/testcases/test_web_api/test_document_app/test_list_documents.py
deleted file mode 100644
index b3452a8a7c..0000000000
--- a/test/testcases/test_web_api/test_document_app/test_list_documents.py
+++ /dev/null
@@ -1,282 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from types import SimpleNamespace
-
-import pytest
-from test_common import list_documents
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from utils import is_sorted
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = list_documents(invalid_auth, {"id": "dataset_id"})
- assert res["code"] == expected_code
- assert res["message"] == expected_message
-
-
-class TestDocumentsList:
- @pytest.mark.p1
- def test_default(self, WebApiAuth, add_documents):
- kb_id, _ = add_documents
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- assert res["code"] == 0, f", kb_id:{kb_id} +, res:{str(res)}"
- assert len(res["data"]["docs"]) == 5
- assert res["data"]["total"] == 5
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page": None, "page_size": 5}, 0, 5, ""),
- ({"page": 0, "page_size": 5}, 0, 5, ""),
- ({"page": 2, "page_size": 2}, 0, 2, ""),
- ({"page": 3, "page_size": 2}, 0, 1, ""),
- ({"page": "3", "page_size": 2}, 0, 1, ""),
- pytest.param({"page": -1, "page_size": 2}, 100, 0, "1064", marks=pytest.mark.skip(reason="issues/5851")),
- pytest.param({"page": "a", "page_size": 2}, 100, 0, """ValueError("invalid literal for int() with base 10: 'a'")""", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_page(self, WebApiAuth, add_documents, params, expected_code, expected_page_size, expected_message):
- kb_id, _ = add_documents
- res = list_documents(WebApiAuth, {"kb_id": kb_id, **params})
- assert res["code"] == expected_code, res
- if expected_code == 0:
- assert len(res["data"]["docs"]) == expected_page_size, res
- assert res["data"]["total"] == 5, res
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_code, expected_page_size, expected_message",
- [
- ({"page_size": None}, 0, 5, ""),
- ({"page_size": 5}, 0, 5, ""),
- ({"page_size": 1}, 0, 1, ""),
- ({"page_size": 6}, 0, 5, ""),
- ({"page_size": "1"}, 0, 1, ""),
- pytest.param({"page_size": -1}, 100, 0, "1064", marks=pytest.mark.skip(reason="issues/5851")),
- pytest.param({"page_size": "a"}, 100, 0, """ValueError("invalid literal for int() with base 10: 'a'")""", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_page_size(self, WebApiAuth, add_documents, params, expected_code, expected_page_size, expected_message):
- kb_id, _ = add_documents
- res = list_documents(WebApiAuth, {"kb_id": kb_id, **params})
- assert res["code"] == expected_code, res
- if expected_code == 0:
- assert len(res["data"]["docs"]) == expected_page_size, res
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"orderby": None}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"orderby": "create_time"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"orderby": "update_time"}, 0, lambda r: is_sorted(r["data"]["docs"], "update_time", True), ""),
- pytest.param({"orderby": "name", "desc": "False"}, 0, lambda r: is_sorted(r["data"]["docs"], "name", False), "", marks=pytest.mark.skip(reason="issues/5851")),
- pytest.param({"orderby": "unknown"}, 102, 0, "orderby should be create_time or update_time", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_orderby(self, WebApiAuth, add_documents, params, expected_code, assertions, expected_message):
- kb_id, _ = add_documents
- res = list_documents(WebApiAuth, {"kb_id": kb_id, **params})
- assert res["code"] == expected_code, res
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "params, expected_code, assertions, expected_message",
- [
- ({"desc": None}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"desc": "true"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"desc": "True"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- ({"desc": True}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", True), ""),
- pytest.param({"desc": "false"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", False), "", marks=pytest.mark.skip(reason="issues/5851")),
- ({"desc": "False"}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", False), ""),
- ({"desc": False}, 0, lambda r: is_sorted(r["data"]["docs"], "create_time", False), ""),
- ({"desc": "False", "orderby": "update_time"}, 0, lambda r: is_sorted(r["data"]["docs"], "update_time", False), ""),
- pytest.param({"desc": "unknown"}, 102, 0, "desc should be true or false", marks=pytest.mark.skip(reason="issues/5851")),
- ],
- )
- def test_desc(self, WebApiAuth, add_documents, params, expected_code, assertions, expected_message):
- kb_id, _ = add_documents
- res = list_documents(WebApiAuth, {"kb_id": kb_id, **params})
- assert res["code"] == expected_code, res
- if expected_code == 0:
- if callable(assertions):
- assert assertions(res)
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "params, expected_num",
- [
- ({"keywords": None}, 5),
- ({"keywords": ""}, 5),
- ({"keywords": "0"}, 1),
- ({"keywords": "ragflow_test_upload"}, 5),
- ({"keywords": "unknown"}, 0),
- ],
- )
- def test_keywords(self, WebApiAuth, add_documents, params, expected_num):
- kb_id, _ = add_documents
- res = list_documents(WebApiAuth, {"kb_id": kb_id, **params})
- assert res["code"] == 0, res
- assert len(res["data"]["docs"]) == expected_num, res
- assert res["data"]["total"] == expected_num, res
-
- @pytest.mark.p3
- def test_concurrent_list(self, WebApiAuth, add_documents):
- kb_id, _ = add_documents
- count = 100
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_documents, WebApiAuth, {"kb_id": kb_id}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures), responses
-
- # Tests moved from TestDocumentsListUnit
- @pytest.mark.p2
- def test_missing_kb_id(self, WebApiAuth):
- """Test missing KB ID returns error."""
- res = list_documents(WebApiAuth, {"kb_id": ""})
- assert res["code"] == 102
- assert res["message"]
-
- @pytest.mark.p2
- def test_unauthorized_dataset(self, WebApiAuth):
- """Test unauthorized dataset returns error."""
- res = list_documents(WebApiAuth, {"kb_id": "non_existent_kb_id"})
- assert res["code"] == 102
- assert res["message"]
-
- @pytest.mark.p3
- def test_invalid_run_status_filter(self, WebApiAuth, add_documents):
- """Test invalid run status filter returns error."""
- kb_id, _ = add_documents
- res = list_documents(WebApiAuth, {"kb_id": kb_id, "run": "INVALID"})
- assert res["code"] == 102
- assert "Invalid filter run status" in res["message"]
-
- @pytest.mark.p3
- def test_invalid_document_id_filter(self, WebApiAuth, add_documents):
- """Test invalid document ID filter returns error."""
- kb_id, _ = add_documents
- # Use a non-existent document ID
- res = list_documents(WebApiAuth, {"kb_id": kb_id, "id": "non_existent_doc_id"})
- assert res["code"] == 102
- assert "you don't own the document" in res["message"]
-
- @pytest.mark.p3
- def test_create_time_filter(self, WebApiAuth, add_documents):
- """Test create time range filter."""
- kb_id, _ = add_documents
- # Get current time range
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- assert res["code"] == 0
- if res["data"]["docs"]:
- create_time = res["data"]["docs"][0].get("create_time", 0)
- # Test with time range that should include the document
- res = list_documents(WebApiAuth, {"kb_id": kb_id, "create_time_from": 0, "create_time_to": create_time + 1000})
- assert res["code"] == 0
- assert len(res["data"]["docs"]) > 0
- # Test with time range that should not include the document
- res = list_documents(WebApiAuth, {"kb_id": kb_id, "create_time_from": create_time + 1000, "create_time_to": create_time + 2000})
- assert res["code"] == 0
- assert len(res["data"]["docs"]) == 0
-
-
-class _DummyArgs(dict):
- def getlist(self, key):
- value = self.get(key, [])
- if value is None:
- return []
- if isinstance(value, list):
- return value
- return [value]
-
-
-class TestDocumentsListUnit:
- def test_run_status_query_param_filters_documents(self, document_app_module, monkeypatch):
- captured = {}
-
- def fake_get_by_kb_id(kb_id, page, page_size, orderby, desc, keywords, run_status, types, suffix, **kwargs):
- captured["run_status"] = run_status
- return [], 0
-
- monkeypatch.setattr(document_app_module.DocumentService, "get_by_kb_id", fake_get_by_kb_id)
-
- err_code, _, _, _ = document_app_module._get_docs_with_request(
- SimpleNamespace(args=_DummyArgs({"run_status": "DONE"})),
- "kb-1",
- )
-
- assert err_code == 0
- assert captured["run_status"] == ["3"]
-
- def test_empty_run_status_query_param_is_ignored(self, document_app_module, monkeypatch):
- captured = {}
-
- def fake_get_by_kb_id(kb_id, page, page_size, orderby, desc, keywords, run_status, types, suffix, **kwargs):
- captured["run_status"] = run_status
- return [], 0
-
- monkeypatch.setattr(document_app_module.DocumentService, "get_by_kb_id", fake_get_by_kb_id)
-
- err_code, _, _, _ = document_app_module._get_docs_with_request(
- SimpleNamespace(args=_DummyArgs({"run_status": ""})),
- "kb-1",
- )
-
- assert err_code == 0
- assert captured["run_status"] == []
-
- def test_run_status_query_param_filters_aggregated_filters(self, document_app_module, monkeypatch):
- captured = {}
-
- def fake_get_filter_by_kb_id(kb_id, keywords, run_status, types, suffix):
- captured["run_status"] = run_status
- return {}, 0
-
- monkeypatch.setattr(document_app_module.DocumentService, "get_filter_by_kb_id", fake_get_filter_by_kb_id)
-
- err_code, _, _, _ = document_app_module._get_doc_filters_with_request(
- SimpleNamespace(args=_DummyArgs({"run_status": "RUNNING"})),
- "kb-1",
- )
-
- assert err_code == 0
- assert captured["run_status"] == ["1"]
diff --git a/test/testcases/test_web_api/test_document_app/test_paser_documents.py b/test/testcases/test_web_api/test_document_app/test_paser_documents.py
deleted file mode 100644
index abc3b038c3..0000000000
--- a/test/testcases/test_web_api/test_document_app/test_paser_documents.py
+++ /dev/null
@@ -1,356 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from test_common import bulk_upload_documents, list_documents, parse_documents
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from utils import wait_for
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-@wait_for(30, 1, "Document parsing timeout")
-def condition(_auth, _kb_id, _document_ids=None):
- res = list_documents(_auth, {"kb_id": _kb_id})
- target_docs = res["data"]["docs"]
- if _document_ids is None:
- for doc in target_docs:
- if doc["run"] != "DONE":
- return False
- return True
-
- target_ids = set(_document_ids)
- for doc in target_docs:
- if doc["id"] in target_ids:
- if doc.get("run") != "DONE":
- return False
- return True
-
-
-def validate_document_parse_done(auth, _kb_id, _document_ids):
- res = list_documents(auth, {"kb_id": _kb_id})
- for doc in res["data"]["docs"]:
- if doc["id"] not in _document_ids:
- continue
- assert doc["run"] == "DONE"
- assert len(doc["process_begin_at"]) > 0
- assert doc["process_duration"] > 0
- assert doc["progress"] > 0
- assert "Task done" in doc["progress_msg"]
-
-
-def validate_document_parse_cancel(auth, _kb_id, _document_ids):
- res = list_documents(auth, {"kb_id": _kb_id})
- for doc in res["data"]["docs"]:
- if doc["id"] not in _document_ids:
- continue
- assert doc["run"] == "CANCEL"
- assert len(doc["process_begin_at"]) > 0
- assert doc["progress"] == 0.0
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = parse_documents(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestDocumentsParse:
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param(None, 101, "required argument are missing: doc_ids, run; ", marks=pytest.mark.skip),
- pytest.param({"doc_ids": [], "run": "1"}, 0, "", marks=pytest.mark.p1),
- pytest.param({"doc_ids": ["invalid_id"], "run": "1"}, 109, "no authorization", marks=pytest.mark.p3),
- pytest.param({"doc_ids": ["\n!?。;!?\"'"], "run": "1"}, 109, "no authorization", marks=pytest.mark.p3),
- pytest.param("not json", 101, "required argument are missing: doc_ids, run; ", marks=pytest.mark.skip),
- pytest.param(lambda r: {"doc_ids": r[:1], "run": "1"}, 0, "", marks=pytest.mark.p1),
- pytest.param(lambda r: {"doc_ids": r, "run": "1"}, 0, "", marks=pytest.mark.p1),
- ],
- )
- def test_basic_scenarios(self, WebApiAuth, add_documents_func, payload, expected_code, expected_message):
- kb_id, document_ids = add_documents_func
- if callable(payload):
- payload = payload(document_ids)
- res = parse_documents(WebApiAuth, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- condition(WebApiAuth, kb_id, payload["doc_ids"])
- validate_document_parse_done(WebApiAuth, kb_id, payload["doc_ids"])
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.parametrize(
- "payload",
- [
- pytest.param(lambda r: {"doc_ids": ["invalid_id"] + r, "run": "1"}, marks=pytest.mark.p3),
- pytest.param(lambda r: {"doc_ids": r[:1] + ["invalid_id"] + r[1:3], "run": "1"}, marks=pytest.mark.p1),
- pytest.param(lambda r: {"doc_ids": r + ["invalid_id"], "run": "1"}, marks=pytest.mark.p3),
- ],
- )
- def test_parse_partial_invalid_document_id(self, WebApiAuth, add_documents_func, payload):
- _, document_ids = add_documents_func
- if callable(payload):
- payload = payload(document_ids)
- res = parse_documents(WebApiAuth, payload)
- assert res["code"] == 109, res
- assert res["message"] == "no authorization", res
-
- @pytest.mark.p2
- def test_document_not_found(self, WebApiAuth, add_documents_func):
- """Test document not found error."""
- kb_id, document_ids = add_documents_func
-
- # Try to parse a non-existent document
- res = parse_documents(WebApiAuth, {"doc_ids": ["non_existent_doc_id"], "run": "1"})
- assert res["code"] == 109, res
- assert "No authorization" in res["message"], res
-
- @pytest.mark.p2
- def test_cancel_non_running_task_error(self, WebApiAuth, add_documents_func):
- """Test cancel error when task is not in RUNNING status."""
- kb_id, document_ids = add_documents_func
- doc_id = document_ids[0]
-
- # First, run the document parsing
- res = parse_documents(WebApiAuth, {"doc_ids": [doc_id], "run": "1"})
- assert res["code"] == 0, res
-
- # Wait for parsing to complete
- condition(WebApiAuth, kb_id, [doc_id])
- validate_document_parse_done(WebApiAuth, kb_id, [doc_id])
-
- # Now try to cancel a completed task - should fail
- res = parse_documents(WebApiAuth, {"doc_ids": [doc_id], "run": "2"})
- assert res["code"] == 102, res
- assert res["message"] == "Cannot cancel a task that is not in RUNNING status", res
-
- @pytest.mark.p2
- def test_rerun_with_delete(self, WebApiAuth, add_documents_func):
- """Test rerun with delete scenario."""
- kb_id, document_ids = add_documents_func
- doc_id = document_ids[0]
-
- # First, run the document parsing
- res = parse_documents(WebApiAuth, {"doc_ids": [doc_id], "run": "1"})
- assert res["code"] == 0, res
-
- # Wait for parsing to complete
- condition(WebApiAuth, kb_id, [doc_id])
- validate_document_parse_done(WebApiAuth, kb_id, [doc_id])
-
- # Verify document has chunks
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- doc = next((d for d in res["data"]["docs"] if d["id"] == doc_id), None)
- assert doc is not None
- assert doc["chunk_count"] > 0, "Document should have chunks after parsing"
-
- # Now rerun with delete - this should clear chunks and re-parse
- res = parse_documents(WebApiAuth, {"doc_ids": [doc_id], "run": "1", "delete": True})
- assert res["code"] == 0, res
-
- # Wait for parsing to complete
- condition(WebApiAuth, kb_id, [doc_id])
- validate_document_parse_done(WebApiAuth, kb_id, [doc_id])
-
- @pytest.mark.p2
- def test_apply_kb_dataset_not_found(self, WebApiAuth, add_documents_func):
- """Test apply_kb when dataset is not found."""
- kb_id, document_ids = add_documents_func
- doc_id = document_ids[0]
-
- # Try to apply_kb with a non-existent dataset - this is tricky to test
- # because we can't easily delete the dataset after getting the doc_id
- # This test verifies the happy path works
- res = parse_documents(WebApiAuth, {"doc_ids": [doc_id], "run": "1"})
- assert res["code"] == 0, res
-
- # Wait for parsing to complete
- condition(WebApiAuth, kb_id, [doc_id])
- validate_document_parse_done(WebApiAuth, kb_id, [doc_id])
-
- @pytest.mark.p2
- def test_successful_parse(self, WebApiAuth, add_documents_func):
- """Test successful document parsing."""
- kb_id, document_ids = add_documents_func
- doc_id = document_ids[0]
-
- # Run the document parsing
- res = parse_documents(WebApiAuth, {"doc_ids": [doc_id], "run": "1"})
- assert res["code"] == 0, res
-
- # Wait for parsing to complete
- condition(WebApiAuth, kb_id, [doc_id])
- validate_document_parse_done(WebApiAuth, kb_id, [doc_id])
-
- # Verify the document is properly parsed
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- doc = next((d for d in res["data"]["docs"] if d["id"] == doc_id), None)
- assert doc is not None
- assert doc["run"] == "DONE"
- assert doc["chunk_count"] > 0
- assert len(doc["process_begin_at"]) > 0
- assert doc["process_duration"] > 0
-
- @pytest.mark.p3
- def test_repeated_parse(self, WebApiAuth, add_documents_func):
- kb_id, document_ids = add_documents_func
- res = parse_documents(WebApiAuth, {"doc_ids": document_ids, "run": "1"})
- assert res["code"] == 0, res
-
- condition(WebApiAuth, kb_id, document_ids)
-
- res = parse_documents(WebApiAuth, {"doc_ids": document_ids, "run": "1"})
- assert res["code"] == 0, res
-
- @pytest.mark.p3
- def test_duplicate_parse(self, WebApiAuth, add_documents_func):
- kb_id, document_ids = add_documents_func
- res = parse_documents(WebApiAuth, {"doc_ids": document_ids + document_ids, "run": "1"})
- assert res["code"] == 0, res
- assert res["message"] == "success", res
-
- condition(WebApiAuth, kb_id, document_ids)
- validate_document_parse_done(WebApiAuth, kb_id, document_ids)
-
-
-def test_parse_100_files(WebApiAuth, add_dataset_func, tmp_path):
- @wait_for(100, 1, "Document parsing timeout")
- def condition(_auth, _kb_id, _document_num):
- res = list_documents(_auth, {"kb_id": _kb_id, "page_size": _document_num})
- for doc in res["data"]["docs"]:
- if doc["run"] != "DONE":
- return False
- return True
-
- document_num = 100
- kb_id = add_dataset_func
- document_ids = bulk_upload_documents(WebApiAuth, kb_id, document_num, tmp_path)
- res = parse_documents(WebApiAuth, {"doc_ids": document_ids, "run": "1"})
- assert res["code"] == 0, res
-
- condition(WebApiAuth, kb_id, document_num)
-
- validate_document_parse_done(WebApiAuth, kb_id, document_ids)
-
-
-def test_concurrent_parse(WebApiAuth, add_dataset_func, tmp_path):
- @wait_for(120, 1, "Document parsing timeout")
- def condition(_auth, _kb_id, _document_num):
- res = list_documents(_auth, {"kb_id": _kb_id, "page_size": _document_num})
- for doc in res["data"]["docs"]:
- if doc["run"] != "3":
- return False
- return True
-
- count = 100
- kb_id = add_dataset_func
- document_ids = bulk_upload_documents(WebApiAuth, kb_id, count, tmp_path)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [
- executor.submit(
- parse_documents,
- WebApiAuth,
- {"doc_ids": [document_ids[i]], "run": "1"},
- )
- for i in range(count)
- ]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
- condition(WebApiAuth, kb_id, count)
-
- validate_document_parse_done(WebApiAuth, kb_id, document_ids)
-
-
-# @pytest.mark.skip
-class TestDocumentsParseStop:
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message",
- [
- pytest.param(None, 101, "required argument are missing: doc_ids, run; ", marks=pytest.mark.skip),
- pytest.param({"doc_ids": [], "run": "2"}, 0, "", marks=pytest.mark.p1),
- pytest.param({"doc_ids": ["invalid_id"], "run": "2"}, 109, "no authorization", marks=pytest.mark.p3),
- pytest.param({"doc_ids": ["\n!?。;!?\"'"], "run": "2"}, 109, "no authorization", marks=pytest.mark.p3),
- pytest.param("not json", 101, "required argument are missing: doc_ids, run; ", marks=pytest.mark.skip),
- pytest.param(lambda r: {"doc_ids": r[:1], "run": "2"}, 0, "", marks=pytest.mark.p1),
- pytest.param(lambda r: {"doc_ids": r, "run": "2"}, 0, "", marks=pytest.mark.p1),
- ],
- )
- def test_basic_scenarios(self, WebApiAuth, add_documents_func, payload, expected_code, expected_message):
- @wait_for(30, 1, "Document parsing timeout")
- def condition(_auth, _kb_id, _doc_ids):
- res = list_documents(_auth, {"kb_id": _kb_id})
- for doc in res["data"]["docs"]:
- if doc["id"] in _doc_ids:
- if doc["run"] != "DONE":
- return False
- return True
-
- kb_id, document_ids = add_documents_func
- parse_documents(WebApiAuth, {"doc_ids": document_ids, "run": "1"})
-
- if callable(payload):
- payload = payload(document_ids)
-
- res = parse_documents(WebApiAuth, payload)
- assert res["code"] == expected_code, res
- if expected_code == 0:
- completed_document_ids = list(set(document_ids) - set(payload["doc_ids"]))
- condition(WebApiAuth, kb_id, completed_document_ids)
- validate_document_parse_cancel(WebApiAuth, kb_id, payload["doc_ids"])
- validate_document_parse_done(WebApiAuth, kb_id, completed_document_ids)
- else:
- assert res["message"] == expected_message, res
-
- @pytest.mark.skip
- @pytest.mark.parametrize(
- "payload",
- [
- lambda r: {"doc_ids": ["invalid_id"] + r, "run": "2"},
- lambda r: {"doc_ids": r[:1] + ["invalid_id"] + r[1:3], "run": "2"},
- lambda r: {"doc_ids": r + ["invalid_id"], "run": "2"},
- ],
- )
- def test_stop_parse_partial_invalid_document_id(self, WebApiAuth, add_documents_func, payload):
- kb_id, document_ids = add_documents_func
- parse_documents(WebApiAuth, {"doc_ids": document_ids, "run": "1"})
-
- if callable(payload):
- payload = payload(document_ids)
- res = parse_documents(WebApiAuth, payload)
- assert res["code"] == 109, res
- assert res["message"] == "no authorization", res
-
- validate_document_parse_cancel(WebApiAuth, kb_id, document_ids)
diff --git a/test/testcases/test_web_api/test_document_app/test_rm_documents.py b/test/testcases/test_web_api/test_document_app/test_rm_documents.py
deleted file mode 100644
index 48a77eba79..0000000000
--- a/test/testcases/test_web_api/test_document_app/test_rm_documents.py
+++ /dev/null
@@ -1,125 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from test_common import bulk_upload_documents, delete_document, list_documents
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = delete_document(invalid_auth, "kb_id")
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestDocumentsDeletion:
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "payload, expected_code, expected_message, remaining",
- [
- ({}, 102, "should either provide doc ids or set delete_all(true), dataset:", 3),
- ({"invalid_key": []}, 101, "Field: <invalid_key> - Message: <Extra inputs are not permitted> - Value: <[]>", 3),
- ({"ids": ""}, 101, "Field: <ids> - Message: <Input should be a valid list> - Value: <>", 3),
- ({"ids": ["invalid_id"]}, 102, "These documents do not belong to dataset", 3),
- ("not json", 101, "Invalid request payload: expected object, got str", 3),
- (lambda r: {"ids": r[0]}, 101, "Field: <ids> - Message: <Input should be a valid list> - Value", 3),
- (lambda r: {"ids": r}, 0, "", 0),
- ],
- )
- def test_basic_scenarios(self, WebApiAuth, add_documents_func, payload, expected_code, expected_message, remaining):
- kb_id, document_ids = add_documents_func
- if callable(payload):
- payload = payload(document_ids)
- res = delete_document(WebApiAuth, kb_id, payload)
- assert res["code"] == expected_code, res
- if res["code"] != 0:
- assert expected_message in res["message"], res
-
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- assert len(res["data"]["docs"]) == remaining, res
- assert res["data"]["total"] == remaining, res
-
- @pytest.mark.p2
- def test_repeated_deletion(self, WebApiAuth, add_documents_func):
- kb_id, document_ids = add_documents_func
- for doc_id in document_ids:
- res = delete_document(WebApiAuth, kb_id, {"ids": [doc_id]})
- assert res["code"] == 0, res
-
- for doc_id in document_ids:
- res = delete_document(WebApiAuth, kb_id, {"ids": [doc_id]})
- assert res["code"] == 102, res
- assert "Document not found" in res["message"], res
-
- @pytest.mark.p2
- def test_delete_all(self, WebApiAuth, add_documents_func):
- kb_id, document_ids = add_documents_func
-
- res = delete_document(WebApiAuth, kb_id, {"delete_all": True})
- assert res["code"] == 0, res
-
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- assert len(res["data"]["docs"]) == 0, res
- assert res["data"]["total"] == 0, res
-
-
-def test_concurrent_deletion(WebApiAuth, add_dataset, tmp_path):
- count = 100
- kb_id = add_dataset
- document_ids = bulk_upload_documents(WebApiAuth, kb_id, count, tmp_path)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(delete_document, WebApiAuth, kb_id, {"ids": [document_ids[i]]}) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures), responses
-
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- assert len(res["data"]["docs"]) == 0, res
- assert res["data"]["total"] == 0, res
-
-
-def test_delete_100(WebApiAuth, add_dataset, tmp_path):
- documents_num = 100
- kb_id = add_dataset
- document_ids = bulk_upload_documents(WebApiAuth, kb_id, documents_num, tmp_path)
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- assert res["data"]["total"] == documents_num, res
-
- for doc_id in document_ids:
- res = delete_document(WebApiAuth, kb_id, {"ids": [doc_id]})
- assert res["code"] == 0, res
-
- res = list_documents(WebApiAuth, {"kb_id": kb_id})
- assert res["data"]["total"] == 0, res
diff --git a/test/testcases/test_web_api/test_document_app/test_upload_documents.py b/test/testcases/test_web_api/test_document_app/test_upload_documents.py
deleted file mode 100644
index 0abd99246c..0000000000
--- a/test/testcases/test_web_api/test_document_app/test_upload_documents.py
+++ /dev/null
@@ -1,483 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import string
-
-import pytest
-from test_common import list_datasets, upload_documents
-from configs import DOCUMENT_NAME_LIMIT, INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from utils.file_utils import create_txt_file
-
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-
[email protected]("clear_datasets")
-class TestAuthorization:
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
- res = upload_documents(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestDocumentsUpload:
- @pytest.mark.p1
- def test_valid_single_upload(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- assert res["code"] == 0, res
- # New API returns "dataset_id" instead of "kb_id" due to key mapping
- assert res["data"][0]["dataset_id"] == kb_id, res
- assert res["data"][0]["name"] == fp.name, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "generate_test_files",
- [
- "docx",
- "excel",
- "ppt",
- "image",
- "pdf",
- "txt",
- "md",
- "json",
- "eml",
- "html",
- ],
- indirect=True,
- )
- def test_file_type_validation(self, WebApiAuth, add_dataset_func, generate_test_files, request):
- kb_id = add_dataset_func
- fp = generate_test_files[request.node.callspec.params["generate_test_files"]]
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- assert res["code"] == 0, res
- # New API returns "dataset_id" instead of "kb_id" due to key mapping
- assert res["data"][0]["dataset_id"] == kb_id, res
- assert res["data"][0]["name"] == fp.name, res
-
- @pytest.mark.p3
- @pytest.mark.parametrize(
- "file_type",
- ["exe", "unknown"],
- )
- def test_unsupported_file_type(self, WebApiAuth, add_dataset_func, tmp_path, file_type):
- kb_id = add_dataset_func
- fp = tmp_path / f"ragflow_test.{file_type}"
- fp.touch()
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- assert res["code"] == 500, res
- assert res["message"] == f"ragflow_test.{file_type}: This type of file has not been supported yet!", res
-
- @pytest.mark.p2
- def test_missing_file(self, WebApiAuth, add_dataset_func):
- kb_id = add_dataset_func
- res = upload_documents(WebApiAuth, {"kb_id": kb_id})
- assert res["code"] == 101, res
- assert res["message"] == "No file part!", res
-
- @pytest.mark.p3
- def test_empty_file(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
- fp = tmp_path / "empty.txt"
- fp.touch()
-
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- assert res["code"] == 0, res
- assert res["data"][0]["size"] == 0, res
-
- @pytest.mark.p3
- def test_filename_empty(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
-
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp], filename_override="")
- assert res["code"] == 101, res
- assert res["message"] == "No file selected!", res
-
- @pytest.mark.p3
- def test_filename_exceeds_max_length(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
- fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt")
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- assert res["code"] == 0, res
- assert res["data"][0]["name"] == fp.name, res
-
- @pytest.mark.p2
- def test_invalid_kb_id(self, WebApiAuth, tmp_path):
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(WebApiAuth, {"kb_id": "invalid_kb_id"}, [fp])
- assert res["code"] == 102, res
- assert res["message"] == "Can't find the dataset with ID invalid_kb_id!", res
-
- @pytest.mark.p2
- def test_duplicate_files(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp, fp])
- assert res["code"] == 0, res
- assert len(res["data"]) == 2, res
- for i in range(len(res["data"])):
- # New API returns "dataset_id" instead of "kb_id" due to key mapping
- assert res["data"][i]["dataset_id"] == kb_id, res
- expected_name = fp.name
- if i != 0:
- expected_name = f"{fp.stem}({i}){fp.suffix}"
- assert res["data"][i]["name"] == expected_name, res
-
- @pytest.mark.p3
- def test_filename_special_characters(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
- illegal_chars = '<>:"/\\|?*'
- translation_table = str.maketrans({char: "_" for char in illegal_chars})
- safe_filename = string.punctuation.translate(translation_table)
- fp = tmp_path / f"{safe_filename}.txt"
- fp.write_text("Sample text content")
-
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- assert res["code"] == 0, res
- assert len(res["data"]) == 1, res
- # New API returns "dataset_id" instead of "kb_id" due to key mapping
- assert res["data"][0]["dataset_id"] == kb_id, res
- assert res["data"][0]["name"] == fp.name, res
-
- @pytest.mark.p1
- def test_multiple_files(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
- expected_document_count = 20
- fps = []
- for i in range(expected_document_count):
- fp = create_txt_file(tmp_path / f"ragflow_test_{i}.txt")
- fps.append(fp)
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, fps)
- assert res["code"] == 0, res
-
- res = list_datasets(WebApiAuth)
- assert res["data"][0]["document_count"] == expected_document_count, res
-
- @pytest.mark.p3
- def test_concurrent_upload(self, WebApiAuth, add_dataset_func, tmp_path):
- kb_id = add_dataset_func
-
- count = 20
- fps = []
- for i in range(count):
- fp = create_txt_file(tmp_path / f"ragflow_test_{i}.txt")
- fps.append(fp)
-
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(upload_documents, WebApiAuth, {"kb_id": kb_id}, fps[i : i + 1]) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures), responses
-
- res = list_datasets(WebApiAuth)
- assert res["data"][0]["document_count"] == count, res
-
-
-import asyncio
-from types import SimpleNamespace
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _coro():
- return self._value
-
- return _coro().__await__()
-
-
-class _DummyFiles(dict):
- def getlist(self, key):
- value = self.get(key, [])
- if isinstance(value, list):
- return value
- return [value]
-
-
-class _DummyFile:
- def __init__(self, filename):
- self.filename = filename
- self.closed = False
- self.stream = self
-
- def close(self):
- self.closed = True
-
-
-class _DummyRequest:
- def __init__(self, form=None, files=None, args=None):
- self._form = form or {}
- self._files = files or _DummyFiles()
- self.args = args or {}
-
- @property
- def form(self):
- return _AwaitableValue(self._form)
-
- @property
- def files(self):
- return _AwaitableValue(self._files)
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-class TestDocumentsUploadUnit:
- """Unit tests for document upload using upload_documents helper function"""
-
- def test_missing_kb_id(self, WebApiAuth, tmp_path):
- """Test that missing KB ID returns error"""
- # When kb_id is empty, the API should return an error
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(WebApiAuth, {"kb_id": ""}, [fp])
- assert res["code"] == 100
- assert res["message"] == "<MethodNotAllowed '405: Method Not Allowed'>"
-
- def test_missing_file_part(self, WebApiAuth, add_dataset_func):
- """Test that missing file part returns error"""
- kb_id = add_dataset_func
- # Call without files - should return error for missing file
- res = upload_documents(WebApiAuth, {"kb_id": kb_id})
- assert res["code"] == 101
- assert "file" in res["message"].lower()
-
- def test_empty_filename_closes_files(self, WebApiAuth, add_dataset_func, tmp_path):
- """Test that empty filename returns error"""
- kb_id = add_dataset_func
- # Create a file with empty name by using filename_override
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp], filename_override="")
- assert res["code"] == 101
- assert "file" in res["message"].lower() or "selected" in res["message"].lower()
-
- def test_invalid_kb_id_raises(self, WebApiAuth, tmp_path):
- """Test that invalid KB ID returns error"""
- fp = create_txt_file(tmp_path / "ragflow_test.txt")
- res = upload_documents(WebApiAuth, {"kb_id": "invalid_kb_id"}, [fp])
- # The API should return an error for invalid KB ID
- assert res["code"] == 102
- assert "Can't find the dataset" in res["message"] or "not found" in res["message"].lower()
-
- def test_no_permission(self, WebApiAuth, tmp_path):
- """Test that no permission returns error"""
- # Create a file and try to upload to a dataset we don't have access to
- # This test would require setting up a dataset without permission
- # For now, we skip this test as it requires specific setup
- pytest.skip("Requires dataset without permission setup")
-
- def test_thread_pool_errors(self, WebApiAuth, add_dataset_func, tmp_path):
- """Test that thread pool errors are handled"""
- kb_id = add_dataset_func
- # Upload a file with unsupported type
- fp = tmp_path / "test.exe"
- fp.write_text("test")
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- # Should return error for unsupported file type
- assert res["code"] == 500
- assert "supported" in res["message"].lower() or "type" in res["message"].lower()
-
- def test_empty_upload_result(self, WebApiAuth, add_dataset_func, tmp_path):
- """Test that empty upload result returns error"""
- kb_id = add_dataset_func
- # Create an empty file
- fp = tmp_path / "empty.txt"
- fp.write_text("")
- res = upload_documents(WebApiAuth, {"kb_id": kb_id}, [fp])
- # Empty file might cause issues
- # The exact behavior depends on the implementation
- # Just verify we get a response
- assert "code" in res
-
-
-class TestWebCrawlUnit:
- def test_invalid_url(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(form={"name": "doc", "url": "not-a-url"}, args={"type": "web"}),
- )
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 101
- assert res["message"] == "The URL format is invalid"
-
- def test_invalid_kb_id(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- monkeypatch.setattr(module, "is_valid_url", lambda _url: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (False, None))
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(form={"name": "doc", "url": "http://example.com"}, args={"type": "web"}),
- )
- res = _run(module.upload_document(dataset_id="missing"))
- assert res["code"] == 102
- assert "Can't find the dataset" in res["message"]
-
- def test_no_permission(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = SimpleNamespace(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- monkeypatch.setattr(module, "is_valid_url", lambda _url: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: False)
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(form={"name": "doc", "url": "http://example.com"}, args={"type": "web"}),
- )
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 109
- assert res["message"] == "no authorization"
-
- def test_download_failure(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = SimpleNamespace(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- monkeypatch.setattr(module, "is_valid_url", lambda _url: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module, "html2pdf", lambda _url: None)
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(form={"name": "doc", "url": "http://example.com"}, args={"type": "web"}),
- )
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 100
- assert "Download failure" in res["message"]
-
- def test_unsupported_type(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = SimpleNamespace(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- monkeypatch.setattr(module, "is_valid_url", lambda _url: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module, "html2pdf", lambda _url: b"%PDF-1.4")
- monkeypatch.setattr(module.FileService, "get_root_folder", lambda _uid: {"id": "root"})
- monkeypatch.setattr(module.FileService, "init_knowledgebase_docs", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module.FileService, "get_kb_folder", lambda *_args, **_kwargs: {"id": "kb_root"})
- monkeypatch.setattr(module.FileService, "new_a_file_from_kb", lambda *_args, **_kwargs: {"id": "kb_folder"})
- monkeypatch.setattr(module, "duplicate_name", lambda *_args, **_kwargs: "bad.exe")
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(form={"name": "doc", "url": "http://example.com"}, args={"type": "web"}),
- )
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 100
- assert "supported yet" in res["message"]
-
- @pytest.mark.parametrize(
- "filename,filetype,expected_parser",
- [
- ("image.png", "visual", "picture"),
- ("sound.mp3", "aural", "audio"),
- ("deck.pptx", "doc", "presentation"),
- ("mail.eml", "doc", "email"),
- ],
- )
- def test_success_parser_overrides(self, document_rest_api_module, monkeypatch, filename, filetype, expected_parser):
- module = document_rest_api_module
- kb = SimpleNamespace(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
- captured = {}
-
- class _Storage:
- def obj_exist(self, *_args, **_kwargs):
- return False
-
- def put(self, *_args, **_kwargs):
- captured["put"] = True
-
- def insert_doc(doc):
- captured["doc"] = doc
-
- monkeypatch.setattr(module, "is_valid_url", lambda _url: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module, "html2pdf", lambda _url: b"%PDF-1.4")
- monkeypatch.setattr(module.FileService, "get_root_folder", lambda _uid: {"id": "root"})
- monkeypatch.setattr(module.FileService, "init_knowledgebase_docs", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module.FileService, "get_kb_folder", lambda *_args, **_kwargs: {"id": "kb_root"})
- monkeypatch.setattr(module.FileService, "new_a_file_from_kb", lambda *_args, **_kwargs: {"id": "kb_folder"})
- monkeypatch.setattr(module, "duplicate_name", lambda *_args, **_kwargs: filename)
- monkeypatch.setattr(module, "filename_type", lambda _name: filetype)
- monkeypatch.setattr(module, "thumbnail", lambda *_args, **_kwargs: "")
- monkeypatch.setattr(module, "get_uuid", lambda: "doc-1")
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", _Storage())
- monkeypatch.setattr(module.DocumentService, "insert", insert_doc)
- monkeypatch.setattr(module.FileService, "add_file_from_kb", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(form={"name": "doc", "url": "http://example.com"}, args={"type": "web"}),
- )
-
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 0
- assert captured["doc"]["parser_id"] == expected_parser
- assert captured["put"] is True
-
- def test_exception_path(self, document_rest_api_module, monkeypatch):
- module = document_rest_api_module
- kb = SimpleNamespace(id="kb1", tenant_id="tenant1", name="kb", parser_id="parser", pipeline_id="pipe", parser_config={})
-
- class _Storage:
- def obj_exist(self, *_args, **_kwargs):
- return False
-
- def put(self, *_args, **_kwargs):
- return None
-
- def insert_doc(_doc):
- raise RuntimeError("boom")
-
- monkeypatch.setattr(module, "is_valid_url", lambda _url: True)
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module, "html2pdf", lambda _url: b"%PDF-1.4")
- monkeypatch.setattr(module.FileService, "get_root_folder", lambda _uid: {"id": "root"})
- monkeypatch.setattr(module.FileService, "init_knowledgebase_docs", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(module.FileService, "get_kb_folder", lambda *_args, **_kwargs: {"id": "kb_root"})
- monkeypatch.setattr(module.FileService, "new_a_file_from_kb", lambda *_args, **_kwargs: {"id": "kb_folder"})
- monkeypatch.setattr(module, "duplicate_name", lambda *_args, **_kwargs: "doc.pdf")
- monkeypatch.setattr(module, "filename_type", lambda _name: "pdf")
- monkeypatch.setattr(module, "thumbnail", lambda *_args, **_kwargs: "")
- monkeypatch.setattr(module, "get_uuid", lambda: "doc-1")
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", _Storage())
- monkeypatch.setattr(module.DocumentService, "insert", insert_doc)
- monkeypatch.setattr(module.FileService, "add_file_from_kb", lambda *_args, **_kwargs: None)
- monkeypatch.setattr(
- module,
- "request",
- _DummyRequest(form={"name": "doc", "url": "http://example.com"}, args={"type": "web"}),
- )
-
- res = _run(module.upload_document(dataset_id="kb1"))
- assert res["code"] == 100
diff --git a/test/testcases/test_web_api/test_document_app/test_upload_info_unit.py b/test/testcases/test_web_api/test_document_app/test_upload_info_unit.py
deleted file mode 100644
index f44edf3e66..0000000000
--- a/test/testcases/test_web_api/test_document_app/test_upload_info_unit.py
+++ /dev/null
@@ -1,116 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-
-import pytest
-from test_common import upload_info
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from utils.file_utils import create_txt_file
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _DummyFiles(dict):
- def getlist(self, key):
- value = self.get(key, [])
- if isinstance(value, list):
- return value
- return [value]
-
-
-class _DummyFile:
- def __init__(self, filename):
- self.filename = filename
-
-
-class _DummyRequest:
- def __init__(self, *, files=None, args=None):
- self._files = files or _DummyFiles()
- self.args = args or {}
-
- @property
- def files(self):
- return _AwaitableValue(self._files)
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-# ============================================================================
-# End-to-End Tests
-# ============================================================================
-
-
-class TestUploadInfoE2E:
- """End-to-end tests for the /api/v1/documents/upload endpoint"""
-
- def test_upload_info_requires_file_or_url_e2e(self, WebApiAuth):
- """Test that missing both file and url returns error"""
- # Call without files and without url
- res = upload_info(WebApiAuth)
- assert res["code"] == 101, res
- assert "Missing input" in res["message"] or "file" in res["message"].lower() or "url" in res["message"].lower()
-
- def test_upload_info_rejects_mixed_inputs_e2e(self, WebApiAuth, tmp_path):
- """Test that providing both file and url returns error"""
- # Create a file
- fp = create_txt_file(tmp_path / "test.txt")
-
- # Call with both file and url - the API should reject this
- res = upload_info(WebApiAuth, files_path=[fp], url="https://example.com/test.txt")
- # The API should return an error when both file and url are provided
- assert res["code"] == 101, res
- assert "not both" in res["message"].lower() and "either" in res["message"].lower()
-
- def test_upload_info_supports_url_single_and_multiple_files_e2e(self, WebApiAuth, tmp_path):
- """Test that the endpoint supports URL, single file, and multiple files"""
- # Test with URL
- # Note: Using a real URL might fail if the URL is not accessible
- # For E2E testing, we test with actual file uploads
-
- # Test with single file
- fp1 = create_txt_file(tmp_path / "single_file.txt")
- res = upload_info(WebApiAuth, files_path=[fp1])
- assert res["code"] == 0, res
- assert "data" in res, res
-
- # Test with multiple files
- fp2 = create_txt_file(tmp_path / "file_a.txt")
- fp3 = create_txt_file(tmp_path / "file_b.txt")
- res = upload_info(WebApiAuth, files_path=[fp2, fp3])
- assert res["code"] == 0, res
- assert "data" in res, res
- # Should return a list for multiple files
- if isinstance(res["data"], list):
- assert len(res["data"]) == 2, res
-
- def test_upload_info_invalid_auth(self):
- """Test that invalid authentication returns error"""
- res = upload_info(RAGFlowWebApiAuth(INVALID_API_TOKEN), files_path=[])
- assert res["code"] == 401, res
diff --git a/test/testcases/test_web_api/test_file_app/test_file2document_routes_unit.py b/test/testcases/test_web_api/test_file_app/test_file2document_routes_unit.py
deleted file mode 100644
index f593ac4efa..0000000000
--- a/test/testcases/test_web_api/test_file_app/test_file2document_routes_unit.py
+++ /dev/null
@@ -1,325 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import functools
-import importlib.util
-import sys
-from copy import deepcopy
-from enum import Enum
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _DummyFile:
- def __init__(self, file_id, file_type, *, name="file.txt", location="loc", size=1):
- self.id = file_id
- self.type = file_type
- self.name = name
- self.location = location
- self.size = size
-
-
-class _FalsyFile(_DummyFile):
- def __bool__(self):
- return False
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _set_request_json(monkeypatch, module, payload_state):
- async def _req_json():
- return deepcopy(payload_state)
-
- monkeypatch.setattr(module, "get_request_json", _req_json)
-
-
-def _set_request_args(monkeypatch, module, args):
- monkeypatch.setattr(module, "request", SimpleNamespace(args=args))
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_file2document_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.current_user = SimpleNamespace(id="user-1")
- apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
- api_pkg.apps = apps_mod
-
- db_pkg = ModuleType("api.db")
- db_pkg.__path__ = []
-
- class _FileType(Enum):
- FOLDER = "folder"
- DOC = "doc"
-
- db_pkg.FileType = _FileType
- monkeypatch.setitem(sys.modules, "api.db", db_pkg)
- api_pkg.db = db_pkg
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- services_pkg.duplicate_name = lambda _query_func, **kwargs: "file(1).txt" if kwargs.get("name") == "file.txt" and kwargs.get("kb_id") == "kb-dup" else kwargs.get("name")
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- common_pkg = ModuleType("api.common")
- common_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.common", common_pkg)
-
- permission_mod = ModuleType("api.common.check_team_permission")
- permission_mod.check_file_team_permission = lambda *_args, **_kwargs: True
- permission_mod.check_kb_team_permission = lambda *_args, **_kwargs: True
- monkeypatch.setitem(sys.modules, "api.common.check_team_permission", permission_mod)
- common_pkg.check_team_permission = permission_mod
-
- file2document_mod = ModuleType("api.db.services.file2document_service")
-
- class _StubFile2DocumentService:
- @staticmethod
- def get_by_file_id(_file_id):
- return []
-
- @staticmethod
- def delete_by_file_id(*_args, **_kwargs):
- return None
-
- @staticmethod
- def delete_by_document_id(*_args, **_kwargs):
- return None
-
- @staticmethod
- def insert(_payload):
- return SimpleNamespace(to_json=lambda: {})
-
- file2document_mod.File2DocumentService = _StubFile2DocumentService
- monkeypatch.setitem(sys.modules, "api.db.services.file2document_service", file2document_mod)
- services_pkg.file2document_service = file2document_mod
-
- file_service_mod = ModuleType("api.db.services.file_service")
-
- class _StubFileService:
- @staticmethod
- def get_by_ids(_file_ids):
- return []
-
- @staticmethod
- def get_all_innermost_file_ids(_file_id, _acc):
- return []
-
- @staticmethod
- def get_by_id(_file_id):
- return True, _DummyFile(_file_id, _FileType.DOC.value)
-
- @staticmethod
- def get_parser(_file_type, _file_name, parser_id):
- return parser_id
-
- file_service_mod.FileService = _StubFileService
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
- services_pkg.file_service = file_service_mod
-
- kb_service_mod = ModuleType("api.db.services.knowledgebase_service")
-
- class _StubKnowledgebaseService:
- @staticmethod
- def get_by_id(_kb_id):
- return False, None
-
- kb_service_mod.KnowledgebaseService = _StubKnowledgebaseService
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_service_mod)
- services_pkg.knowledgebase_service = kb_service_mod
-
- document_service_mod = ModuleType("api.db.services.document_service")
-
- class _StubDocumentService:
- @staticmethod
- def get_by_id(doc_id):
- return True, SimpleNamespace(id=doc_id)
-
- @staticmethod
- def get_tenant_id(_doc_id):
- return "tenant-1"
-
- @staticmethod
- def remove_document(*_args, **_kwargs):
- return True
-
- @staticmethod
- def query(*_args, **_kwargs):
- return False
-
- @staticmethod
- def insert(_payload):
- return SimpleNamespace(id="doc-1")
-
- document_service_mod.DocumentService = _StubDocumentService
- monkeypatch.setitem(sys.modules, "api.db.services.document_service", document_service_mod)
- services_pkg.document_service = document_service_mod
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- def get_json_result(data=None, message="", code=0):
- return {"code": code, "data": data, "message": message}
-
- def get_data_error_result(message=""):
- return {"code": 102, "data": None, "message": message}
-
- async def get_request_json():
- return {}
-
- def server_error_response(err):
- return {"code": 500, "data": None, "message": str(err)}
-
- def validate_request(*_keys):
- def _decorator(func):
- @functools.wraps(func)
- async def _wrapper(*args, **kwargs):
- return await func(*args, **kwargs)
-
- return _wrapper
-
- return _decorator
-
- api_utils_mod.get_json_result = get_json_result
- api_utils_mod.get_data_error_result = get_data_error_result
- api_utils_mod.get_request_json = get_request_json
- api_utils_mod.server_error_response = server_error_response
- api_utils_mod.validate_request = validate_request
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- misc_utils_mod = ModuleType("common.misc_utils")
- misc_utils_mod.get_uuid = lambda: "uuid"
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
-
- constants_mod = ModuleType("common.constants")
-
- class _RetCode:
- ARGUMENT_ERROR = 101
-
- constants_mod.RetCode = _RetCode
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- module_name = "test_file2document_routes_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "file2document_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_convert_branch_matrix_unit(monkeypatch):
- module = _load_file2document_module(monkeypatch)
- req_state = {"kb_ids": ["kb-1"], "file_ids": ["f1"]}
- _set_request_json(monkeypatch, module, req_state)
- _set_request_args(monkeypatch, module, {})
-
- # Falsy file returns "File not found!" during synchronous validation.
- monkeypatch.setattr(module.FileService, "get_by_ids", lambda _ids: [_FalsyFile("f1", module.FileType.DOC.value)])
- res = _run(module.convert())
- assert res["message"] == "File not found!"
-
- # Valid file but invalid kb returns "Can't find this dataset!" during synchronous validation.
- monkeypatch.setattr(module.FileService, "get_by_ids", lambda _ids: [_DummyFile("f1", module.FileType.DOC.value)])
- res = _run(module.convert())
- assert res["message"] == "Can't find this dataset!"
-
- kb = SimpleNamespace(id="kb-1", parser_id="naive", pipeline_id="p1", parser_config={})
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
-
- # Unauthorized file access is rejected before scheduling background work.
- monkeypatch.setattr(module, "check_file_team_permission", lambda *_args, **_kwargs: False)
- res = _run(module.convert())
- assert res["message"] == "no authorization"
-
- # Unauthorized dataset access is rejected before scheduling background work.
- monkeypatch.setattr(module, "check_file_team_permission", lambda *_args, **_kwargs: True)
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: False)
- res = _run(module.convert())
- assert res["message"] == "no authorization"
-
- # Valid file and kb schedule background work and return data=True immediately.
- monkeypatch.setattr(module, "check_kb_team_permission", lambda *_args, **_kwargs: True)
- res = _run(module.convert())
- assert res["code"] == 0
- assert res["data"] is True
-
- # Folder expansion schedules background work and returns data=True immediately.
- req_state["file_ids"] = ["folder-1"]
- monkeypatch.setattr(module.FileService, "get_by_ids", lambda _ids: [_DummyFile("folder-1", module.FileType.FOLDER.value, name="folder")])
- monkeypatch.setattr(module.FileService, "get_all_innermost_file_ids", lambda _file_id, _acc: ["inner-1"])
- res = _run(module.convert())
- assert res["code"] == 0
- assert res["data"] is True
-
- # Exception in file lookup returns 500.
- req_state["file_ids"] = ["f1"]
- monkeypatch.setattr(
- module.FileService,
- "get_by_ids",
- lambda _ids: (_ for _ in ()).throw(RuntimeError("convert boom")),
- )
- res = _run(module.convert())
- assert res["code"] == 500
- assert "convert boom" in res["message"]
-
-
-def test_convert_files_renames_duplicate_document_name_unit(monkeypatch):
- module = _load_file2document_module(monkeypatch)
- inserted_docs = []
- file = _DummyFile("f1", module.FileType.DOC.value, name="file.txt")
- kb = SimpleNamespace(id="kb-dup", parser_id="naive", pipeline_id="p1", parser_config={})
-
- monkeypatch.setattr(module.FileService, "get_by_id", lambda _file_id: (True, file))
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
- monkeypatch.setattr(module.DocumentService, "insert", lambda payload: inserted_docs.append(payload) or SimpleNamespace(id="doc-1"))
-
- module._convert_files(["f1"], ["kb-dup"], "user-1", "add")
-
- assert inserted_docs[0]["name"] == "file(1).txt"
- assert inserted_docs[0]["suffix"] == "txt"
- assert inserted_docs[0]["location"] == "loc"
diff --git a/test/testcases/test_web_api/test_file_app/test_file_routes_unit.py b/test/testcases/test_web_api/test_file_app/test_file_routes_unit.py
deleted file mode 100644
index 93862c65af..0000000000
--- a/test/testcases/test_web_api/test_file_app/test_file_routes_unit.py
+++ /dev/null
@@ -1,357 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import sys
-from enum import Enum
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _DummyFiles(dict):
- def __init__(self, file_objs=None):
- super().__init__()
- self._file_objs = list(file_objs or [])
- if file_objs is not None:
- self["file"] = self._file_objs
-
- def getlist(self, key):
- if key == "file":
- return list(self._file_objs)
- return []
-
-
-class _DummyUploadFile:
- def __init__(self, filename, blob=b"blob"):
- self.filename = filename
- self._blob = blob
-
- def read(self):
- return self._blob
-
-
-class _DummyRequest:
- def __init__(self, *, content_type="", form=None, files=None, args=None):
- self.content_type = content_type
- self.form = _AwaitableValue(form or {})
- self.files = _AwaitableValue(files if files is not None else _DummyFiles())
- self.args = args or {}
-
-
-class _DummyResponse:
- def __init__(self, data):
- self.data = data
- self.headers = {}
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _load_file_api_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart_mod = ModuleType("quart")
- quart_mod.request = _DummyRequest()
-
- async def _make_response(data):
- return _DummyResponse(data)
-
- quart_mod.make_response = _make_response
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_pkg = ModuleType("api.apps")
- apps_pkg.__path__ = [str(repo_root / "api" / "apps")]
- apps_pkg.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", apps_pkg)
- api_pkg.apps = apps_pkg
-
- services_pkg = ModuleType("api.apps.services")
- services_pkg.__path__ = [str(repo_root / "api" / "apps" / "services")]
- monkeypatch.setitem(sys.modules, "api.apps.services", services_pkg)
- apps_pkg.services = services_pkg
-
- file_api_service_mod = ModuleType("api.apps.services.file_api_service")
-
- async def _upload_file(_tenant_id, _pf_id, _file_objs):
- return True, [{"id": "f1"}]
-
- async def _create_folder(_tenant_id, _name, _parent_id=None, _file_type=None):
- return True, {"id": "folder1"}
-
- async def _delete_files(_tenant_id, _ids):
- return True, True
-
- async def _move_files(_tenant_id, _src_file_ids, _dest_file_id=None, _new_name=None):
- return True, True
-
- file_api_service_mod.upload_file = _upload_file
- file_api_service_mod.create_folder = _create_folder
- file_api_service_mod.list_files = lambda _tenant_id, _args: (True, {"files": [], "total": 0})
- file_api_service_mod.delete_files = _delete_files
- file_api_service_mod.move_files = _move_files
- file_api_service_mod.get_file_content = lambda _tenant_id, _file_id: (
- True,
- SimpleNamespace(parent_id="bucket1", location="path1", name="doc.txt", type="doc"),
- )
- file_api_service_mod.get_parent_folder = lambda _file_id, user_id=None: (True, {"parent_folder": {"id": "parent1"}})
- file_api_service_mod.get_all_parent_folders = lambda _file_id, user_id=None: (True, {"parent_folders": [{"id": "root"}]})
- monkeypatch.setitem(sys.modules, "api.apps.services.file_api_service", file_api_service_mod)
- services_pkg.file_api_service = file_api_service_mod
-
- db_pkg = ModuleType("api.db")
- db_pkg.__path__ = []
-
- class _FileType(Enum):
- DOC = "doc"
- VISUAL = "visual"
-
- db_pkg.FileType = _FileType
- monkeypatch.setitem(sys.modules, "api.db", db_pkg)
- api_pkg.db = db_pkg
-
- file2doc_mod = ModuleType("api.db.services.file2document_service")
- file2doc_mod.File2DocumentService = SimpleNamespace(get_storage_address=lambda **_kwargs: ("bucket2", "path2"))
- monkeypatch.setitem(sys.modules, "api.db.services.file2document_service", file2doc_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.add_tenant_id_to_kwargs = lambda func: func
- api_utils_mod.get_error_argument_result = lambda message: {"code": 400, "data": None, "message": message}
- api_utils_mod.get_error_data_result = lambda message: {"code": 500, "data": None, "message": message}
- api_utils_mod.get_result = lambda data=None: {"code": 0, "data": data, "message": ""}
- api_utils_mod.get_json_result = lambda code=0, message="success", data=None: {"code": code, "data": data, "message": message}
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- validation_mod = ModuleType("api.utils.validation_utils")
- validation_mod.CreateFolderReq = object
- validation_mod.DeleteFileReq = object
- validation_mod.ListFileReq = object
- validation_mod.MoveFileReq = object
-
- async def _validate_json_request(_request, _schema):
- return {}, None
-
- validation_mod.validate_and_parse_json_request = _validate_json_request
- validation_mod.validate_and_parse_request_args = lambda _request, _schema: ({}, None)
- monkeypatch.setitem(sys.modules, "api.utils.validation_utils", validation_mod)
-
- web_utils_mod = ModuleType("api.utils.web_utils")
- web_utils_mod.CONTENT_TYPE_MAP = {"txt": "text/plain"}
- web_utils_mod.apply_download_file_response_headers = lambda response, content_type, ext, filename=None: response.headers.update({"content_type": content_type, "ext": ext, "filename": filename})
- monkeypatch.setitem(sys.modules, "api.utils.web_utils", web_utils_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- common_pkg.settings = SimpleNamespace(
- STORAGE_IMPL=SimpleNamespace(
- get=lambda *_args, **_kwargs: b"blob",
- )
- )
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- misc_utils_mod = ModuleType("common.misc_utils")
-
- async def thread_pool_exec(func, *args, **kwargs):
- return func(*args, **kwargs)
-
- misc_utils_mod.thread_pool_exec = thread_pool_exec
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "file_api.py"
- spec = importlib.util.spec_from_file_location("api.apps.restful_apis.file_api", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, "api.apps.restful_apis.file_api", module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_create_or_upload_multipart_requires_file(monkeypatch):
- module = _load_file_api_module(monkeypatch)
- monkeypatch.setattr(module, "request", _DummyRequest(content_type="multipart/form-data", form={}, files=_DummyFiles()))
-
- res = _run(module.create_or_upload("tenant1"))
- assert res["code"] == 400
- assert res["message"] == "No file part!"
-
-
-def test_create_or_upload_uploads_via_new_service(monkeypatch):
- module = _load_file_api_module(monkeypatch)
- files = _DummyFiles([_DummyUploadFile("a.txt")])
- monkeypatch.setattr(module, "request", _DummyRequest(content_type="multipart/form-data", form={"parent_id": "pf1"}, files=files))
-
- seen = {}
-
- async def _upload_file(tenant_id, pf_id, file_objs):
- seen["args"] = (tenant_id, pf_id, [f.filename for f in file_objs])
- return True, [{"id": "f1"}]
-
- monkeypatch.setattr(module.file_api_service, "upload_file", _upload_file)
- res = _run(module.create_or_upload("tenant1"))
-
- assert seen["args"] == ("tenant1", "pf1", ["a.txt"])
- assert res["code"] == 0
- assert res["data"] == [{"id": "f1"}]
-
-
-def test_create_or_upload_creates_folder_from_json(monkeypatch):
- module = _load_file_api_module(monkeypatch)
- monkeypatch.setattr(module, "request", _DummyRequest(content_type="application/json"))
-
- async def _validate(_request, _schema):
- return {"name": "folder-a", "parent_id": "pf1", "type": "folder"}, None
-
- async def _create_folder(tenant_id, name, parent_id=None, file_type=None):
- return True, {"tenant_id": tenant_id, "name": name, "parent_id": parent_id, "type": file_type}
-
- monkeypatch.setattr(module, "validate_and_parse_json_request", _validate)
- monkeypatch.setattr(module.file_api_service, "create_folder", _create_folder)
-
- res = _run(module.create_or_upload("tenant1"))
- assert res["code"] == 0
- assert res["data"]["tenant_id"] == "tenant1"
- assert res["data"]["name"] == "folder-a"
-
-
-def test_list_files_validation_error(monkeypatch):
- module = _load_file_api_module(monkeypatch)
- monkeypatch.setattr(module, "validate_and_parse_request_args", lambda _request, _schema: (None, "bad args"))
-
- res = _run(module.list_files("tenant1"))
- assert res["code"] == 400
- assert res["message"] == "bad args"
-
-
-def test_move_uses_new_payload_shape(monkeypatch):
- module = _load_file_api_module(monkeypatch)
-
- async def _validate(_request, _schema):
- return {"src_file_ids": ["f1"], "dest_file_id": "pf2"}, None
-
- seen = {}
-
- async def _move_files(tenant_id, src_file_ids, dest_file_id=None, new_name=None):
- seen["args"] = (tenant_id, src_file_ids, dest_file_id, new_name)
- return True, True
-
- monkeypatch.setattr(module, "validate_and_parse_json_request", _validate)
- monkeypatch.setattr(module.file_api_service, "move_files", _move_files)
-
- res = _run(module.move("tenant1"))
- assert seen["args"] == ("tenant1", ["f1"], "pf2", None)
- assert res["code"] == 0
- assert res["data"] is True
-
-
-def test_rename_via_move_route(monkeypatch):
- module = _load_file_api_module(monkeypatch)
-
- async def _validate(_request, _schema):
- return {"src_file_ids": ["file1"], "new_name": "renamed.txt"}, None
-
- seen = {}
-
- async def _move_files(tenant_id, src_file_ids, dest_file_id=None, new_name=None):
- seen["args"] = (tenant_id, src_file_ids, dest_file_id, new_name)
- return True, True
-
- monkeypatch.setattr(module, "validate_and_parse_json_request", _validate)
- monkeypatch.setattr(module.file_api_service, "move_files", _move_files)
-
- res = _run(module.move("tenant1"))
- assert seen["args"] == ("tenant1", ["file1"], None, "renamed.txt")
- assert res["code"] == 0
- assert res["data"] is True
-
-
-def test_download_falls_back_to_document_storage(monkeypatch):
- module = _load_file_api_module(monkeypatch)
- storage_calls = []
-
- def _get(bucket, location):
- storage_calls.append((bucket, location))
- return b"" if len(storage_calls) == 1 else b"fallback-blob"
-
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", SimpleNamespace(get=_get))
- res = _run(module.download("tenant1", "file1"))
-
- assert storage_calls == [("bucket1", "path1"), ("bucket2", "path2")]
- assert res.data == b"fallback-blob"
- assert res.headers["content_type"] == "text/plain"
- assert res.headers["ext"] == "txt"
- assert res.headers["filename"] == "doc.txt"
-
-
-def test_download_missing_blob_returns_error(monkeypatch):
- module = _load_file_api_module(monkeypatch)
- storage_calls = []
-
- def _get(bucket, location):
- storage_calls.append((bucket, location))
- return None
-
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", SimpleNamespace(get=_get))
- res = _run(module.download("tenant1", "file1"))
-
- assert storage_calls == [("bucket1", "path1"), ("bucket2", "path2")]
- assert res["message"] == "This file is empty."
-
-
-def test_parent_and_ancestors_use_new_routes(monkeypatch):
- module = _load_file_api_module(monkeypatch)
-
- parent_res = _run(module.parent_folder("tenant1", "file1"))
- ancestors_res = _run(module.ancestors("tenant1", "file1"))
-
- assert parent_res["code"] == 0
- assert parent_res["data"]["parent_folder"]["id"] == "parent1"
- assert ancestors_res["code"] == 0
- assert ancestors_res["data"]["parent_folders"][0]["id"] == "root"
diff --git a/test/testcases/test_web_api/test_llm_app/test_llm_list.py b/test/testcases/test_web_api/test_llm_app/test_llm_list.py
deleted file mode 100644
index 2abb9bb3df..0000000000
--- a/test/testcases/test_web_api/test_llm_app/test_llm_list.py
+++ /dev/null
@@ -1,55 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from test_common import llm_factories, llm_list
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-INVALID_AUTH_CASES = [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
-]
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_message", INVALID_AUTH_CASES)
- def test_auth_invalid_factories(self, invalid_auth, expected_code, expected_message):
- res = llm_factories(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_message", INVALID_AUTH_CASES)
- def test_auth_invalid_list(self, invalid_auth, expected_code, expected_message):
- res = llm_list(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestLLMList:
- @pytest.mark.p1
- def test_factories(self, WebApiAuth):
- res = llm_factories(WebApiAuth)
- assert res["code"] == 0, res
- assert isinstance(res["data"], list), res
-
- @pytest.mark.p1
- def test_list(self, WebApiAuth):
- res = llm_list(WebApiAuth)
- assert res["code"] == 0, res
- assert isinstance(res["data"], dict), res
diff --git a/test/testcases/test_web_api/test_llm_app/test_llm_list_unit.py b/test/testcases/test_web_api/test_llm_app/test_llm_list_unit.py
deleted file mode 100644
index 386188c648..0000000000
--- a/test/testcases/test_web_api/test_llm_app/test_llm_list_unit.py
+++ /dev/null
@@ -1,913 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import json
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _ExprField:
- def __init__(self, name):
- self.name = name
-
- def __eq__(self, other):
- return (self.name, other)
-
-
-class _StrEnum(str):
- @property
- def value(self):
- return str(self)
-
-
-class _DummyTenantLLMModel:
- tenant_id = _ExprField("tenant_id")
- llm_factory = _ExprField("llm_factory")
- llm_name = _ExprField("llm_name")
-
- def __init__(self, id=None, **kwargs):
- self.id = id
- self.api_key = None
- self.status = None
- for key, value in kwargs.items():
- setattr(self, key, value)
-
-
-class _TenantLLMRow:
- def __init__(
- self,
- *,
- id,
- llm_name,
- llm_factory,
- model_type,
- api_key="key",
- status="1",
- used_tokens=0,
- api_base="",
- max_tokens=8192,
- ):
- self.id = id
- self.llm_name = llm_name
- self.llm_factory = llm_factory
- self.model_type = model_type
- self.api_key = api_key
- self.status = status
- self.used_tokens = used_tokens
- self.api_base = api_base
- self.max_tokens = max_tokens
-
- def to_dict(self):
- return {
- "id": self.id,
- "llm_name": self.llm_name,
- "llm_factory": self.llm_factory,
- "model_type": self.model_type,
- "status": self.status,
- "used_tokens": self.used_tokens,
- "api_base": self.api_base,
- "max_tokens": self.max_tokens,
- }
-
-
-class _LLMRow:
- def __init__(self, *, llm_name, fid, model_type, status="1", max_tokens=2048):
- self.llm_name = llm_name
- self.fid = fid
- self.model_type = model_type
- self.status = status
- self.max_tokens = max_tokens
-
- def to_dict(self):
- return {
- "llm_name": self.llm_name,
- "fid": self.fid,
- "model_type": self.model_type,
- "status": self.status,
- "max_tokens": self.max_tokens,
- }
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _set_request_json(monkeypatch, module, payload):
- async def _get_request_json():
- return dict(payload)
-
- monkeypatch.setattr(module, "get_request_json", _get_request_json)
-
-
-def _load_llm_app(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart_mod = ModuleType("quart")
- quart_mod.request = SimpleNamespace(args={})
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.login_required = lambda fn: fn
- apps_mod.current_user = SimpleNamespace(id="tenant-1")
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- tenant_llm_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _StubLLMFactoriesService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- class _StubTenantLLMService:
- @staticmethod
- def ensure_mineru_from_env(_tenant_id):
- return None
-
- @staticmethod
- def ensure_opendataloader_from_env(_tenant_id):
- return None
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_my_llms(_tenant_id):
- return []
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def filter_delete(_filters):
- return True
-
- @staticmethod
- def filter_update(_filters, _payload):
- return True
-
- tenant_llm_mod.LLMFactoriesService = _StubLLMFactoriesService
- tenant_llm_mod.TenantLLMService = _StubTenantLLMService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_mod)
-
- llm_service_mod = ModuleType("api.db.services.llm_service")
-
- class _StubLLMService:
- @staticmethod
- def get_all():
- return []
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- llm_service_mod.LLMService = _StubLLMService
- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.get_allowed_llm_factories = lambda: []
- api_utils_mod.get_data_error_result = lambda message="", code=400, data=None: {
- "code": code,
- "message": message,
- "data": data,
- }
- api_utils_mod.get_json_result = lambda data=None, message="", code=0: {
- "code": code,
- "message": message,
- "data": data,
- }
-
- async def _get_request_json():
- return {}
-
- api_utils_mod.get_request_json = _get_request_json
- api_utils_mod.server_error_response = lambda exc: {"code": 500, "message": str(exc), "data": None}
- api_utils_mod.validate_request = lambda *_args, **_kwargs: lambda fn: fn
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- constants_mod = ModuleType("common.constants")
- constants_mod.StatusEnum = SimpleNamespace(VALID=SimpleNamespace(value="1"), INVALID=SimpleNamespace(value="0"))
- constants_mod.LLMType = SimpleNamespace(
- CHAT=_StrEnum("chat"),
- EMBEDDING=_StrEnum("embedding"),
- ASR=_StrEnum("asr"),
- VISION=_StrEnum("vision"),
- RERANK=_StrEnum("rerank"),
- TTS=_StrEnum("tts"),
- OCR=_StrEnum("ocr"),
- )
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.TenantLLM = _DummyTenantLLMModel
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- base64_mod = ModuleType("rag.utils.base64_image")
- base64_mod.test_image = b"image-bytes"
- monkeypatch.setitem(sys.modules, "rag.utils.base64_image", base64_mod)
-
- rag_llm_mod = ModuleType("rag.llm")
- rag_llm_mod.EmbeddingModel = {}
- rag_llm_mod.ChatModel = {}
- rag_llm_mod.RerankModel = {}
- rag_llm_mod.CvModel = {}
- rag_llm_mod.TTSModel = {}
- rag_llm_mod.OcrModel = {}
- rag_llm_mod.Seq2txtModel = {}
- monkeypatch.setitem(sys.modules, "rag.llm", rag_llm_mod)
-
- module_path = repo_root / "api" / "apps" / "llm_app.py"
- spec = importlib.util.spec_from_file_location("test_llm_list_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- return module
-
-
-def test_openai_catalog_contains_latest_gpt_models_unit():
- repo_root = Path(__file__).resolve().parents[4]
-
- openai_provider_path = repo_root / "conf" / "llm_factories.json"
- openai_model_path = repo_root / "conf" / "models" / "openai.json"
-
- with open(openai_provider_path, "r", encoding="utf-8") as f:
- factories = json.load(f)["factory_llm_infos"]
-
- openai_factory = next(item for item in factories if item["name"] == "OpenAI")
- factory_model_names = {item["llm_name"] for item in openai_factory["llm"]}
-
- with open(openai_model_path, "r", encoding="utf-8") as f:
- openai_models = json.load(f)["models"]
- model_file_names = {item["name"] for item in openai_models}
-
- for model_name in ["gpt-5.5", "gpt-5.4", "gpt-5.4-mini", "gpt-5.4-nano"]:
- assert model_name in factory_model_names
- assert model_name in model_file_names
-
-
-def test_list_app_grouping_availability_and_merge(monkeypatch):
- module = _load_llm_app(monkeypatch)
-
- ensure_calls = []
- monkeypatch.setattr(module.TenantLLMService, "ensure_mineru_from_env", lambda tenant_id: ensure_calls.append(tenant_id))
-
- tenant_rows = [
- _TenantLLMRow(id=1, llm_name="fast-emb", llm_factory="FastEmbed", model_type="embedding", api_key="k1", status="1"),
- _TenantLLMRow(id=2, llm_name="tenant-only", llm_factory="CustomFactory", model_type="chat", api_key="k2", status="1"),
- _TenantLLMRow(id=3, llm_name="gpt-5.5", llm_factory="OpenAI", model_type="chat", api_key="k3", status="1"),
- _TenantLLMRow(id=4, llm_name="gpt-5.4", llm_factory="OpenAI", model_type="chat", api_key="k4", status="1"),
- ]
- monkeypatch.setattr(module.TenantLLMService, "query", lambda **_kwargs: tenant_rows)
-
- all_llms = [
- _LLMRow(llm_name="tei-embed", fid="Builtin", model_type="embedding", status="1"),
- _LLMRow(llm_name="fast-emb", fid="FastEmbed", model_type="embedding", status="1"),
- _LLMRow(llm_name="gpt-5.5", fid="OpenAI", model_type="chat", status="1"),
- _LLMRow(llm_name="gpt-5.4", fid="OpenAI", model_type="chat", status="1"),
- _LLMRow(llm_name="not-in-status", fid="Other", model_type="chat", status="1"),
- ]
- monkeypatch.setattr(module.LLMService, "get_all", lambda: all_llms)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(args={}))
- monkeypatch.setenv("COMPOSE_PROFILES", "tei-cpu")
- monkeypatch.setenv("TEI_MODEL", "tei-embed")
-
- res = _run(module.list_app())
- assert res["code"] == 0, res["message"]
- assert ensure_calls == ["tenant-1"]
-
- data = res["data"]
- assert {"Builtin", "FastEmbed", "CustomFactory", "OpenAI"}.issubset(set(data.keys()))
-
- builtin = data["Builtin"][0]
- assert builtin["llm_name"] == "tei-embed"
- assert builtin["available"] is True
-
- fastembed = data["FastEmbed"][0]
- assert fastembed["llm_name"] == "fast-emb"
- assert fastembed["available"] is True
-
- tenant_only = data["CustomFactory"][0]
- assert tenant_only["llm_name"] == "tenant-only"
- assert tenant_only["available"] is True
-
- # Response-level assertion: /llm/list output includes latest OpenAI IDs.
- openai_names = {item["llm_name"] for item in data["OpenAI"]}
- assert {"gpt-5.5", "gpt-5.4"}.issubset(openai_names)
-
-
-def test_list_app_model_type_filter(monkeypatch):
- module = _load_llm_app(monkeypatch)
-
- monkeypatch.setattr(module.TenantLLMService, "ensure_mineru_from_env", lambda _tenant_id: None)
- monkeypatch.setattr(
- module.TenantLLMService,
- "query",
- lambda **_kwargs: [
- _TenantLLMRow(id=1, llm_name="fast-emb", llm_factory="FastEmbed", model_type="embedding", api_key="k1", status="1"),
- _TenantLLMRow(id=2, llm_name="tenant-only", llm_factory="CustomFactory", model_type="chat", api_key="k2", status="1"),
- ],
- )
- monkeypatch.setattr(
- module.LLMService,
- "get_all",
- lambda: [
- _LLMRow(llm_name="tei-embed", fid="Builtin", model_type="embedding", status="1"),
- _LLMRow(llm_name="fast-emb", fid="FastEmbed", model_type="embedding", status="1"),
- ],
- )
-
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"model_type": "chat"}))
- res = _run(module.list_app())
- assert res["code"] == 0, res["message"]
- assert list(res["data"].keys()) == ["CustomFactory"]
- assert res["data"]["CustomFactory"][0]["model_type"] == "chat"
-
-
-def test_list_app_exception_path(monkeypatch):
- module = _load_llm_app(monkeypatch)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(args={}))
- monkeypatch.setattr(module.TenantLLMService, "ensure_mineru_from_env", lambda _tenant_id: None)
- monkeypatch.setattr(
- module.TenantLLMService,
- "query",
- lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("query boom")),
- )
-
- res = _run(module.list_app())
- assert res["code"] == 500
- assert "query boom" in res["message"]
-
-
-def test_factories_route_success_and_exception_unit(monkeypatch):
- module = _load_llm_app(monkeypatch)
-
- def _factory(name):
- return SimpleNamespace(name=name, to_dict=lambda n=name: {"name": n})
-
- monkeypatch.setattr(
- module,
- "get_allowed_llm_factories",
- lambda: [
- _factory("OpenAI"),
- _factory("CustomFactory"),
- _factory("FastEmbed"),
- _factory("Builtin"),
- ],
- )
- monkeypatch.setattr(
- module.LLMService,
- "get_all",
- lambda: [
- _LLMRow(llm_name="m1", fid="OpenAI", model_type="chat", status="1"),
- _LLMRow(llm_name="m2", fid="OpenAI", model_type="embedding", status="1"),
- _LLMRow(llm_name="m3", fid="OpenAI", model_type="rerank", status="0"),
- ],
- )
- res = module.factories()
- assert res["code"] == 0
- names = [item["name"] for item in res["data"]]
- assert "FastEmbed" not in names
- assert "Builtin" not in names
- assert {"OpenAI", "CustomFactory"} == set(names)
- openai = next(item for item in res["data"] if item["name"] == "OpenAI")
- assert {"chat", "embedding"} == set(openai["model_types"])
-
- monkeypatch.setattr(module, "get_allowed_llm_factories", lambda: (_ for _ in ()).throw(RuntimeError("factories boom")))
- res = module.factories()
- assert res["code"] == 500
- assert "factories boom" in res["message"]
-
-
-def test_set_api_key_model_probe_matrix_unit(monkeypatch):
- module = _load_llm_app(monkeypatch)
-
- async def _wait_for(coro, *_args, **_kwargs):
- return await coro
-
- async def _to_thread(fn, *args, **kwargs):
- return fn(*args, **kwargs)
-
- monkeypatch.setattr(module.asyncio, "wait_for", _wait_for)
- monkeypatch.setattr(module.asyncio, "to_thread", _to_thread)
-
- class _EmbeddingFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def encode(self, _texts):
- return [[]], 1
-
- class _EmbeddingPass:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def encode(self, _texts):
- return [[0.1]], 1
-
- class _ChatFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- async def async_chat(self, *_args, **_kwargs):
- return "**ERROR** chat fail", 1
-
- class _RerankFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def similarity(self, *_args, **_kwargs):
- return [], 0
-
- factory = "FactoryA"
- monkeypatch.setattr(
- module.LLMService,
- "query",
- lambda **_kwargs: [
- _LLMRow(llm_name="emb", fid=factory, model_type=module.LLMType.EMBEDDING.value, max_tokens=321),
- _LLMRow(llm_name="chat", fid=factory, model_type=module.LLMType.CHAT.value, max_tokens=654),
- _LLMRow(llm_name="rerank", fid=factory, model_type=module.LLMType.RERANK.value, max_tokens=987),
- ],
- )
- monkeypatch.setattr(module, "EmbeddingModel", {factory: _EmbeddingFail})
- monkeypatch.setattr(module, "ChatModel", {factory: _ChatFail})
- monkeypatch.setattr(module, "RerankModel", {factory: _RerankFail})
-
- req = {"llm_factory": factory, "api_key": "k", "base_url": "http://x", "verify": True}
- _set_request_json(monkeypatch, module, req)
- res = _run(module.set_api_key())
- assert res["code"] == 0
- assert res["data"]["success"] is False
- assert "Fail to access embedding model(emb)" in res["data"]["message"]
- assert "Fail to access model(FactoryA/chat)" in res["data"]["message"]
- assert "Fail to access model(FactoryA/rerank)" in res["data"]["message"]
-
- req["verify"] = False
- _set_request_json(monkeypatch, module, req)
- res = _run(module.set_api_key())
- assert res["code"] == 400
- assert "Fail to access embedding model(emb)" in res["message"]
-
- calls = {"filter_update": [], "save": []}
-
- def _filter_update(filters, payload):
- calls["filter_update"].append((filters, dict(payload)))
- return False
-
- def _save(**kwargs):
- calls["save"].append(kwargs)
- return True
-
- monkeypatch.setattr(module, "EmbeddingModel", {factory: _EmbeddingPass})
- monkeypatch.setattr(module.LLMService, "query", lambda **_kwargs: [_LLMRow(llm_name="emb-pass", fid=factory, model_type=module.LLMType.EMBEDDING.value, max_tokens=2049)])
- monkeypatch.setattr(module.TenantLLMService, "filter_update", _filter_update)
- monkeypatch.setattr(module.TenantLLMService, "save", _save)
-
- success_req = {
- "llm_factory": factory,
- "api_key": "k2",
- "base_url": "http://y",
- "model_type": "chat",
- "llm_name": "manual-model",
- }
- _set_request_json(monkeypatch, module, success_req)
- res = _run(module.set_api_key())
- assert res["code"] == 0
- assert res["data"] is True
- assert calls["filter_update"]
- assert calls["filter_update"][0][1]["model_type"] == "chat"
- assert calls["filter_update"][0][1]["llm_name"] == "manual-model"
- assert calls["filter_update"][0][1]["max_tokens"] == 2049
- assert calls["save"][0]["max_tokens"] == 2049
- assert calls["save"][0]["llm_name"] == "emb-pass"
-
-
-def test_add_llm_factory_specific_key_assembly_unit(monkeypatch):
- module = _load_llm_app(monkeypatch)
-
- async def _wait_for(coro, *_args, **_kwargs):
- return await coro
-
- async def _to_thread(fn, *args, **kwargs):
- return fn(*args, **kwargs)
-
- monkeypatch.setattr(module.asyncio, "wait_for", _wait_for)
- monkeypatch.setattr(module.asyncio, "to_thread", _to_thread)
-
- allowed = [
- "VolcEngine",
- "Tencent Cloud",
- "Bedrock",
- "LocalAI",
- "HuggingFace",
- "OpenAI-API-Compatible",
- "VLLM",
- "XunFei Spark",
- "BaiduYiyan",
- "Fish Audio",
- "Google Cloud",
- "Azure-OpenAI",
- "OpenRouter",
- "MinerU",
- "PaddleOCR",
- ]
- monkeypatch.setattr(module, "get_allowed_llm_factories", lambda: [SimpleNamespace(name=name) for name in allowed])
-
- captured = {"chat": [], "tts": [], "filter_payloads": []}
-
- class _ChatOK:
- def __init__(self, key, model_name, base_url="", **_kwargs):
- captured["chat"].append((key, model_name, base_url))
-
- async def async_chat(self, *_args, **_kwargs):
- return "ok", 1
-
- async def async_chat_streamly(self, *_args, **_kwargs):
- yield "ok"
- yield 1
-
- class _TTSOK:
- def __init__(self, key, model_name, base_url="", **_kwargs):
- captured["tts"].append((key, model_name, base_url))
-
- def tts(self, _text):
- yield b"ok"
-
- monkeypatch.setattr(module, "ChatModel", {name: _ChatOK for name in allowed})
- monkeypatch.setattr(module, "TTSModel", {"XunFei Spark": _TTSOK})
- monkeypatch.setattr(module.TenantLLMService, "filter_update", lambda _filters, payload: captured["filter_payloads"].append(dict(payload)) or True)
-
- reject_req = {"llm_factory": "NotAllowed", "llm_name": "x", "model_type": module.LLMType.CHAT.value}
- _set_request_json(monkeypatch, module, reject_req)
- res = _run(module.add_llm())
- assert res["code"] == 400
- assert "is not allowed" in res["message"]
-
- def _run_case(factory, *, model_type=module.LLMType.CHAT.value, extra=None):
- req = {"llm_factory": factory, "llm_name": "model", "model_type": model_type, "api_key": "k", "api_base": "http://api"}
- if extra:
- req.update(extra)
- _set_request_json(monkeypatch, module, req)
- out = _run(module.add_llm())
- assert out["code"] == 0
- assert out["data"] is True
- return captured["filter_payloads"][-1]
-
- volc = _run_case("VolcEngine", extra={"ark_api_key": "ak", "endpoint_id": "eid"})
- assert json.loads(volc["api_key"]) == {"ark_api_key": "ak", "endpoint_id": "eid"}
-
- bedrock = _run_case(
- "Bedrock",
- extra={"auth_mode": "iam", "bedrock_ak": "ak", "bedrock_sk": "sk", "bedrock_region": "r", "aws_role_arn": "arn"},
- )
- assert json.loads(bedrock["api_key"]) == {
- "auth_mode": "iam",
- "bedrock_ak": "ak",
- "bedrock_sk": "sk",
- "bedrock_region": "r",
- "aws_role_arn": "arn",
- }
-
- localai = _run_case("LocalAI")
- assert localai["llm_name"] == "model___LocalAI"
- huggingface = _run_case("HuggingFace")
- assert huggingface["llm_name"] == "model___HuggingFace"
- openapi = _run_case("OpenAI-API-Compatible")
- assert openapi["llm_name"] == "model___OpenAI-API"
- vllm = _run_case("VLLM")
- assert vllm["llm_name"] == "model___VLLM"
-
- spark_chat = _run_case("XunFei Spark", extra={"spark_api_password": "spark-pass"})
- assert spark_chat["api_key"] == "spark-pass"
- spark_tts = _run_case(
- "XunFei Spark",
- model_type=module.LLMType.TTS.value,
- extra={"spark_app_id": "app", "spark_api_secret": "secret", "spark_api_key": "key"},
- )
- assert json.loads(spark_tts["api_key"]) == {
- "spark_app_id": "app",
- "spark_api_secret": "secret",
- "spark_api_key": "key",
- }
-
- baidu = _run_case("BaiduYiyan", extra={"yiyan_ak": "ak", "yiyan_sk": "sk"})
- assert json.loads(baidu["api_key"]) == {"yiyan_ak": "ak", "yiyan_sk": "sk"}
- fish = _run_case("Fish Audio", extra={"fish_audio_ak": "ak", "fish_audio_refid": "rid"})
- assert json.loads(fish["api_key"]) == {"fish_audio_ak": "ak", "fish_audio_refid": "rid"}
- google = _run_case(
- "Google Cloud",
- extra={"google_project_id": "pid", "google_region": "us", "google_service_account_key": "sak"},
- )
- assert json.loads(google["api_key"]) == {
- "google_project_id": "pid",
- "google_region": "us",
- "google_service_account_key": "sak",
- }
- azure = _run_case("Azure-OpenAI", extra={"api_key": "real-key", "api_version": "2024-01-01"})
- assert json.loads(azure["api_key"]) == {"api_key": "real-key", "api_version": "2024-01-01"}
- openrouter = _run_case("OpenRouter", extra={"api_key": "or-key", "provider_order": "a,b"})
- assert json.loads(openrouter["api_key"]) == {"api_key": "or-key", "provider_order": "a,b"}
- mineru = _run_case("MinerU", extra={"api_key": "m-key", "provider_order": "p1"})
- assert json.loads(mineru["api_key"]) == {"api_key": "m-key", "provider_order": "p1"}
- paddle = _run_case("PaddleOCR", extra={"api_key": "p-key", "provider_order": "p2"})
- assert json.loads(paddle["api_key"]) == {"api_key": "p-key", "provider_order": "p2"}
-
- tencent_req = {
- "llm_factory": "Tencent Cloud",
- "llm_name": "model",
- "model_type": module.LLMType.CHAT.value,
- "tencent_cloud_sid": "sid",
- "tencent_cloud_sk": "sk",
- }
-
- async def _tencent_request_json():
- return tencent_req
-
- monkeypatch.setattr(module, "get_request_json", _tencent_request_json)
- delegated = {}
-
- async def _fake_set_api_key():
- delegated["api_key"] = tencent_req.get("api_key")
- return {"code": 0, "data": "delegated"}
-
- monkeypatch.setattr(module, "set_api_key", _fake_set_api_key)
- res = _run(module.add_llm())
- assert res["code"] == 0
- assert res["data"] == "delegated"
- assert json.loads(delegated["api_key"]) == {"tencent_cloud_sid": "sid", "tencent_cloud_sk": "sk"}
-
-
-def test_add_llm_model_type_probe_and_persistence_matrix_unit(monkeypatch):
- module = _load_llm_app(monkeypatch)
-
- async def _wait_for(coro, *_args, **_kwargs):
- return await coro
-
- async def _to_thread(fn, *args, **kwargs):
- return fn(*args, **kwargs)
-
- monkeypatch.setattr(module.asyncio, "wait_for", _wait_for)
- monkeypatch.setattr(module.asyncio, "to_thread", _to_thread)
- monkeypatch.setattr(
- module,
- "get_allowed_llm_factories",
- lambda: [
- SimpleNamespace(name=name)
- for name in [
- "FEmbFail",
- "FEmbPass",
- "FChatFail",
- "FChatPass",
- "FRKey",
- "FRFail",
- "FImgFail",
- "FTTSFail",
- "FOcrFail",
- "FSttFail",
- "FUnknown",
- ]
- ],
- )
-
- class _EmbeddingFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def encode(self, _texts):
- return [[]], 1
-
- class _EmbeddingPass:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def encode(self, _texts):
- return [[0.5]], 1
-
- class _ChatFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- async def async_chat(self, *_args, **_kwargs):
- return "**ERROR**: chat failed", 0
-
- async def async_chat_streamly(self, *_args, **_kwargs):
- yield "**ERROR**: chat failed"
- yield 0
-
- class _ChatPass:
- def __init__(self, *_args, **_kwargs):
- pass
-
- async def async_chat(self, *_args, **_kwargs):
- return "ok", 1
-
- async def async_chat_streamly(self, *_args, **_kwargs):
- yield "ok"
- yield 1
-
- class _RerankFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def similarity(self, *_args, **_kwargs):
- return [], 1
-
- class _CvFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def describe(self, _image_data):
- return "**ERROR**: image failed", 0
-
- class _TTSFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def tts(self, _text):
- raise RuntimeError("tts fail")
- yield b"x"
-
- class _OcrFail:
- def __init__(self, *_args, **_kwargs):
- pass
-
- def check_available(self):
- return False, "ocr unavailable"
-
- class _SttFail:
- def __init__(self, *_args, **_kwargs):
- raise RuntimeError("stt fail")
-
- class _RerankKeyMap(dict):
- def __contains__(self, key):
- if key == "FRKey":
- return True
- return super().__contains__(key)
-
- def __getitem__(self, key):
- if key == "FRKey":
- raise KeyError("rerank key fail")
- return super().__getitem__(key)
-
- monkeypatch.setattr(module, "EmbeddingModel", {"FEmbFail": _EmbeddingFail, "FEmbPass": _EmbeddingPass})
- monkeypatch.setattr(module, "ChatModel", {"FChatFail": _ChatFail, "FChatPass": _ChatPass})
- monkeypatch.setattr(module, "RerankModel", _RerankKeyMap({"FRFail": _RerankFail}))
- monkeypatch.setattr(module, "CvModel", {"FImgFail": _CvFail})
- monkeypatch.setattr(module, "TTSModel", {"FTTSFail": _TTSFail})
- monkeypatch.setattr(module, "OcrModel", {"FOcrFail": _OcrFail})
- monkeypatch.setattr(module, "Seq2txtModel", {"FSttFail": _SttFail})
-
- def _call(req):
- _set_request_json(monkeypatch, module, req)
- return _run(module.add_llm())
-
- res = _call({"llm_factory": "FEmbFail", "llm_name": "m", "model_type": module.LLMType.EMBEDDING.value, "verify": True})
- assert res["code"] == 0
- assert res["data"]["success"] is False
- assert "Fail to access embedding model(m)." in res["data"]["message"]
-
- res = _call({"llm_factory": "FEmbFail", "llm_name": "m", "model_type": module.LLMType.EMBEDDING.value})
- assert res["code"] == 400
- assert "Fail to access embedding model(m)." in res["message"]
-
- res = _call({"llm_factory": "FChatFail", "llm_name": "m", "model_type": module.LLMType.CHAT.value, "verify": True})
- assert res["code"] == 0
- assert "Fail to access model(FChatFail/m)." in res["data"]["message"]
-
- res = _call({"llm_factory": "FRKey", "llm_name": "m", "model_type": module.LLMType.RERANK.value, "verify": True})
- assert res["code"] == 0
- assert "does not support this model(FRKey/m)" in res["data"]["message"]
-
- res = _call({"llm_factory": "FRFail", "llm_name": "m", "model_type": module.LLMType.RERANK.value, "verify": True})
- assert res["code"] == 0
- assert "Fail to access model(FRFail/m)." in res["data"]["message"]
-
- res = _call({"llm_factory": "FImgFail", "llm_name": "m", "model_type": module.LLMType.VISION.value, "verify": True})
- assert res["code"] == 0
- assert "Fail to access model(FImgFail/m)." in res["data"]["message"]
-
- res = _call({"llm_factory": "FTTSFail", "llm_name": "m", "model_type": module.LLMType.TTS.value, "verify": True})
- assert res["code"] == 0
- assert "Fail to access model(FTTSFail/m)." in res["data"]["message"]
-
- res = _call({"llm_factory": "FOcrFail", "llm_name": "m", "model_type": module.LLMType.OCR.value, "verify": True})
- assert res["code"] == 0
- assert "Fail to access model(FOcrFail/m)." in res["data"]["message"]
-
- res = _call({"llm_factory": "FSttFail", "llm_name": "m", "model_type": module.LLMType.ASR.value, "verify": True})
- assert res["code"] == 0
- assert "Fail to access model(FSttFail/m)." in res["data"]["message"]
-
- _set_request_json(monkeypatch, module, {"llm_factory": "FUnknown", "llm_name": "m", "model_type": "unknown"})
- with pytest.raises(RuntimeError, match="Unknown model type: unknown"):
- _run(module.add_llm())
-
- saved = []
- monkeypatch.setattr(module.TenantLLMService, "filter_update", lambda _filters, _payload: False)
- monkeypatch.setattr(module.TenantLLMService, "save", lambda **kwargs: saved.append(kwargs) or True)
- res = _call({"llm_factory": "FChatPass", "llm_name": "m", "model_type": module.LLMType.CHAT.value, "api_key": "k"})
- assert res["code"] == 0, res["message"]
- assert res["data"] is True
- assert saved
- assert saved[0]["llm_factory"] == "FChatPass"
-
-
-def test_llm_mutation_routes_unit(monkeypatch):
- module = _load_llm_app(monkeypatch)
- calls = {"delete": [], "update": []}
- monkeypatch.setattr(module.TenantLLMService, "filter_delete", lambda filters: calls["delete"].append(filters) or True)
- monkeypatch.setattr(module.TenantLLMService, "filter_update", lambda filters, payload: calls["update"].append((filters, payload)) or True)
-
- _set_request_json(monkeypatch, module, {"llm_factory": "OpenAI", "llm_name": "gpt"})
- res = _run(module.delete_llm())
- assert res["code"] == 0
- assert res["data"] is True
-
- _set_request_json(monkeypatch, module, {"llm_factory": "OpenAI", "llm_name": "gpt", "status": 0})
- res = _run(module.enable_llm())
- assert res["code"] == 0
- assert res["data"] is True
- assert calls["update"][0][1]["status"] == "0"
-
- _set_request_json(monkeypatch, module, {"llm_factory": "OpenAI"})
- res = _run(module.delete_factory())
- assert res["code"] == 0
- assert res["data"] is True
- assert len(calls["delete"]) == 2
-
-
-def test_my_llms_include_details_and_exception_unit(monkeypatch):
- module = _load_llm_app(monkeypatch)
- monkeypatch.setattr(module, "request", SimpleNamespace(args={"include_details": "true"}))
- ensure_calls = []
- monkeypatch.setattr(module.TenantLLMService, "ensure_mineru_from_env", lambda tenant_id: ensure_calls.append(tenant_id))
- monkeypatch.setattr(module.TenantLLMService, "ensure_opendataloader_from_env", lambda _tenant_id: None)
- monkeypatch.setattr(
- module.TenantLLMService,
- "query",
- lambda **_kwargs: [
- _TenantLLMRow(
- id=1,
- llm_name="chat-model",
- llm_factory="FactoryX",
- model_type="chat",
- used_tokens=42,
- api_base="",
- max_tokens=4096,
- status="1",
- )
- ],
- )
- monkeypatch.setattr(module.LLMFactoriesService, "query", lambda **_kwargs: [SimpleNamespace(name="FactoryX", tags=["tag-a"])])
- res = module.my_llms()
- assert res["code"] == 0
- assert ensure_calls == ["tenant-1"]
- assert "FactoryX" in res["data"]
- assert res["data"]["FactoryX"]["tags"] == ["tag-a"]
- assert res["data"]["FactoryX"]["llm"][0]["used_token"] == 42
- assert res["data"]["FactoryX"]["llm"][0]["max_tokens"] == 4096
-
- monkeypatch.setattr(module.TenantLLMService, "ensure_mineru_from_env", lambda _tenant_id: (_ for _ in ()).throw(RuntimeError("my llms boom")))
- res = module.my_llms()
- assert res["code"] == 500
- assert "my llms boom" in res["message"]
diff --git a/test/testcases/test_web_api/test_mcp_server_app/test_mcp_server_app_unit.py b/test/testcases/test_web_api/test_mcp_server_app/test_mcp_server_app_unit.py
deleted file mode 100644
index 6fcc1fc727..0000000000
--- a/test/testcases/test_web_api/test_mcp_server_app/test_mcp_server_app_unit.py
+++ /dev/null
@@ -1,794 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-import importlib.util
-import inspect
-import json
-import sys
-from contextlib import nullcontext
-from functools import wraps
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _Args(dict):
- def getlist(self, key):
- value = self.get(key, [])
- if isinstance(value, list):
- return value
- return [value]
-
-
-class _Field:
- def __init__(self, name):
- self.name = name
-
- def __eq__(self, other):
- return (self.name, other)
-
-
-class _DummyMCPServer:
- id = _Field("id")
- tenant_id = _Field("tenant_id")
-
- def __init__(self, **kwargs):
- self.id = kwargs.get("id", "")
- self.name = kwargs.get("name", "")
- self.url = kwargs.get("url", "")
- self.server_type = kwargs.get("server_type", "sse")
- self.tenant_id = kwargs.get("tenant_id", "tenant_1")
- self.variables = kwargs.get("variables", {})
- self.headers = kwargs.get("headers", {})
-
- def to_dict(self):
- return {
- "id": self.id,
- "name": self.name,
- "url": self.url,
- "server_type": self.server_type,
- "tenant_id": self.tenant_id,
- "variables": self.variables,
- "headers": self.headers,
- }
-
-
-class _DummyMCPServerService:
- @staticmethod
- def get_servers(*_args, **_kwargs):
- return []
-
- @staticmethod
- def get_or_none(*_args, **_kwargs):
- return None
-
- @staticmethod
- def get_by_id(*_args, **_kwargs):
- return False, None
-
- @staticmethod
- def get_by_name_and_tenant(*_args, **_kwargs):
- return False, None
-
- @staticmethod
- def insert(**_kwargs):
- return True
-
- @staticmethod
- def filter_update(*_args, **_kwargs):
- return True
-
- @staticmethod
- def delete_by_ids(*_args, **_kwargs):
- return True
-
-
-class _DummyTenantService:
- @staticmethod
- def get_by_id(*_args, **_kwargs):
- return True, SimpleNamespace(id="tenant_1")
-
-
-class _DummyTool:
- def __init__(self, name):
- self._name = name
-
- def model_dump(self):
- return {"name": self._name}
-
-
-class _DummyMCPToolCallSession:
- def __init__(self, _mcp_server, _variables):
- self._tools = [_DummyTool("tool_a"), _DummyTool("tool_b")]
-
- def get_tools(self, _timeout):
- return self._tools
-
- def tool_call(self, _name, _arguments, _timeout):
- return "ok"
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _set_request_json(monkeypatch, module, payload):
- async def _request_json():
- return payload
-
- monkeypatch.setattr(module, "get_request_json", _request_json)
-
-
-def _stub_url_safety(monkeypatch, module, unsafe_urls=None):
- unsafe_urls = set(unsafe_urls or [])
-
- def _assert_url_is_safe(url):
- if url in unsafe_urls:
- raise ValueError("blocked unsafe url")
- return "safe.example", "93.184.216.34"
-
- monkeypatch.setattr(module, "assert_url_is_safe", _assert_url_is_safe)
- monkeypatch.setattr(module, "pin_dns_global", lambda *_args, **_kwargs: nullcontext())
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_mcp_api(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart_mod = ModuleType("quart")
- quart_mod.Response = object
- quart_mod.request = SimpleNamespace(args=_Args({}))
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- constants_mod = ModuleType("common.constants")
- constants_mod.VALID_MCP_SERVER_TYPES = {"sse", "streamable-http"}
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.current_user = SimpleNamespace(id="tenant_1")
- apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.MCPServer = _DummyMCPServer
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- mcp_service_mod = ModuleType("api.db.services.mcp_server_service")
- mcp_service_mod.MCPServerService = _DummyMCPServerService
- monkeypatch.setitem(sys.modules, "api.db.services.mcp_server_service", mcp_service_mod)
-
- user_service_mod = ModuleType("api.db.services.user_service")
- user_service_mod.TenantService = _DummyTenantService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- mcp_conn_mod = ModuleType("common.mcp_tool_call_conn")
- mcp_conn_mod.MCPToolCallSession = _DummyMCPToolCallSession
- mcp_conn_mod.close_multiple_mcp_toolcall_sessions = lambda _sessions: None
- monkeypatch.setitem(sys.modules, "common.mcp_tool_call_conn", mcp_conn_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- async def _default_request_json():
- return {}
-
- def _get_json_result(code=0, message="success", data=None):
- return {"code": code, "message": message, "data": data}
-
- def _get_data_error_result(code=102, message="Sorry! Data missing!"):
- return {"code": code, "message": message}
-
- def _server_error_response(error):
- return {"code": 100, "message": repr(error)}
-
- async def _get_mcp_tools(*_args, **_kwargs):
- return {}
-
- def _validate_request(*_args, **_kwargs):
- def _decorator(func):
- @wraps(func)
- async def _wrapped(*func_args, **func_kwargs):
- if inspect.iscoroutinefunction(func):
- return await func(*func_args, **func_kwargs)
- return func(*func_args, **func_kwargs)
-
- return _wrapped
-
- return _decorator
-
- api_utils_mod.get_request_json = _default_request_json
- api_utils_mod.get_json_result = _get_json_result
- api_utils_mod.get_data_error_result = _get_data_error_result
- api_utils_mod.server_error_response = _server_error_response
- api_utils_mod.validate_request = _validate_request
- api_utils_mod.get_mcp_tools = _get_mcp_tools
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- web_utils_mod = ModuleType("api.utils.web_utils")
-
- def _get_float(data, key, default):
- try:
- return float(data.get(key, default))
- except (TypeError, ValueError):
- return default
-
- def _safe_json_parse(value):
- if isinstance(value, (dict, list)):
- return value
- if value in (None, ""):
- return {}
- try:
- return json.loads(value)
- except (TypeError, ValueError):
- return {}
-
- web_utils_mod.get_float = _get_float
- web_utils_mod.safe_json_parse = _safe_json_parse
- monkeypatch.setitem(sys.modules, "api.utils.web_utils", web_utils_mod)
-
- module_name = "test_mcp_api_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "mcp_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_list_mcp_desc_pagination_and_exception(monkeypatch):
- module = _load_mcp_api(monkeypatch)
-
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(args=_Args({"keywords": "k", "page": "2", "page_size": "1", "orderby": "create_time", "desc": "false"})),
- )
- _set_request_json(monkeypatch, module, {"mcp_ids": []})
- monkeypatch.setattr(module.MCPServerService, "get_servers", lambda *_args, **_kwargs: [{"id": "a"}, {"id": "b"}])
-
- res = _run(module.list_mcp())
- assert res["code"] == 0
- assert res["data"]["total"] == 2
- assert res["data"]["mcp_servers"] == [{"id": "b"}]
-
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({})))
- _set_request_json(monkeypatch, module, {"mcp_ids": []})
-
- def _raise_list(*_args, **_kwargs):
- raise RuntimeError("list explode")
-
- monkeypatch.setattr(module.MCPServerService, "get_servers", _raise_list)
- res = _run(module.list_mcp())
- assert res["code"] == 100
- assert "list explode" in res["message"]
-
-
-def test_detail_not_found_success_and_exception(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({})))
-
- monkeypatch.setattr(module.MCPServerService, "get_or_none", lambda **_kwargs: None)
- res = module.detail("mcp-1")
- assert res["code"] == 102
- assert "Cannot find MCP server mcp-1 for user tenant_1" in res["message"]
-
- monkeypatch.setattr(
- module.MCPServerService,
- "get_or_none",
- lambda **_kwargs: _DummyMCPServer(id="mcp-1", name="srv", url="http://a", server_type="sse", tenant_id="tenant_1"),
- )
- res = module.detail("mcp-1")
- assert res["code"] == 0
- assert res["data"]["id"] == "mcp-1"
-
- def _raise_detail(**_kwargs):
- raise RuntimeError("detail explode")
-
- monkeypatch.setattr(module.MCPServerService, "get_or_none", _raise_detail)
- res = module.detail("mcp-1")
- assert res["code"] == 100
- assert "detail explode" in res["message"]
-
-
-def test_create_validation_guards(monkeypatch):
- module = _load_mcp_api(monkeypatch)
-
- monkeypatch.setattr(module.MCPServerService, "get_by_name_and_tenant", lambda **_kwargs: (False, None))
-
- _set_request_json(monkeypatch, module, {"name": "srv", "url": "http://a", "server_type": "invalid"})
- res = _run(module.create.__wrapped__())
- assert "Unsupported MCP server type" in res["message"]
-
- _set_request_json(monkeypatch, module, {"name": "", "url": "http://a", "server_type": "sse"})
- res = _run(module.create.__wrapped__())
- assert "Invalid MCP name" in res["message"]
-
- monkeypatch.setattr(module.MCPServerService, "get_by_name_and_tenant", lambda **_kwargs: (True, object()))
- _set_request_json(monkeypatch, module, {"name": "srv", "url": "http://a", "server_type": "sse"})
- res = _run(module.create.__wrapped__())
- assert "Duplicated MCP server name" in res["message"]
-
- monkeypatch.setattr(module.MCPServerService, "get_by_name_and_tenant", lambda **_kwargs: (False, None))
- _set_request_json(monkeypatch, module, {"name": "srv", "url": "", "server_type": "sse"})
- res = _run(module.create.__wrapped__())
- assert "Invalid url" in res["message"]
-
- _set_request_json(monkeypatch, module, {"name": "srv", "url": 123, "server_type": "sse"})
- res = _run(module.create.__wrapped__())
- assert "Invalid url" in res["message"]
-
- _set_request_json(monkeypatch, module, {"name": "srv", "url": "http://unsafe", "server_type": "sse"})
- _stub_url_safety(monkeypatch, module, {"http://unsafe"})
- res = _run(module.create.__wrapped__())
- assert "blocked unsafe url" in res["message"]
-
-
-def test_create_service_paths(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- _stub_url_safety(monkeypatch, module)
-
- base_payload = {
- "name": "srv",
- "url": "http://server",
- "server_type": "sse",
- "headers": '{"Authorization": "x"}',
- "variables": '{"tools": {"old": 1}, "token": "abc"}',
- "timeout": "2.5",
- }
-
- monkeypatch.setattr(module, "get_uuid", lambda: "uuid-create")
- monkeypatch.setattr(module.MCPServerService, "get_by_name_and_tenant", lambda **_kwargs: (False, None))
-
- _set_request_json(monkeypatch, module, dict(base_payload))
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda *_args, **_kwargs: (False, None))
- res = _run(module.create.__wrapped__())
- assert "Tenant not found" in res["message"]
-
- _set_request_json(monkeypatch, module, dict(base_payload))
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda *_args, **_kwargs: (True, object()))
-
- async def _thread_pool_tools_error(_func, _servers, _timeout):
- return None, "tools error"
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_tools_error)
- res = _run(module.create.__wrapped__())
- assert res["code"] == 102
- assert "tools error" in res["message"]
-
- _set_request_json(monkeypatch, module, dict(base_payload))
-
- async def _thread_pool_ok(_func, servers, _timeout):
- return {servers[0].name: [{"name": "tool_a"}, {"invalid": True}]}, None
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_ok)
- monkeypatch.setattr(module.MCPServerService, "insert", lambda **_kwargs: False)
- res = _run(module.create.__wrapped__())
- assert res["code"] == 102
- assert "Failed to create MCP server" in res["message"]
-
- _set_request_json(monkeypatch, module, dict(base_payload))
- monkeypatch.setattr(module.MCPServerService, "insert", lambda **_kwargs: True)
- res = _run(module.create.__wrapped__())
- assert res["code"] == 0
- assert res["data"]["id"] == "uuid-create"
- assert res["data"]["tenant_id"] == "tenant_1"
- assert res["data"]["variables"]["tools"] == {"tool_a": {"name": "tool_a"}}
-
- _set_request_json(monkeypatch, module, dict(base_payload))
-
- async def _thread_pool_raises(_func, _servers, _timeout):
- raise RuntimeError("create explode")
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_raises)
- res = _run(module.create.__wrapped__())
- assert res["code"] == 100
- assert "create explode" in res["message"]
-
-
-def test_update_validation_guards(monkeypatch):
- module = _load_mcp_api(monkeypatch)
-
- existing = _DummyMCPServer(id="mcp-1", name="srv", url="http://server", server_type="sse", tenant_id="tenant_1", variables={}, headers={})
-
- _set_request_json(monkeypatch, module, {"mcp_id": "mcp-1"})
- monkeypatch.setattr(module.MCPServerService, "get_by_id", lambda _mcp_id: (False, None))
- res = _run(module.update("mcp-1"))
- assert "Cannot find MCP server" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcp_id": "mcp-1"})
- monkeypatch.setattr(
- module.MCPServerService,
- "get_by_id",
- lambda _mcp_id: (True, _DummyMCPServer(id="mcp-1", name="srv", url="http://server", server_type="sse", tenant_id="other", variables={}, headers={})),
- )
- res = _run(module.update("mcp-1"))
- assert "Cannot find MCP server" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcp_id": "mcp-1", "server_type": "invalid"})
- monkeypatch.setattr(module.MCPServerService, "get_by_id", lambda _mcp_id: (True, existing))
- res = _run(module.update("mcp-1"))
- assert "Unsupported MCP server type" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcp_id": "mcp-1", "name": "a" * 256})
- res = _run(module.update("mcp-1"))
- assert "Invalid MCP name" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcp_id": "mcp-1", "url": ""})
- res = _run(module.update("mcp-1"))
- assert "Invalid url" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcp_id": "mcp-1", "url": {"raw": "http://a"}})
- res = _run(module.update("mcp-1"))
- assert "Invalid url" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcp_id": "mcp-1", "url": "http://unsafe"})
- _stub_url_safety(monkeypatch, module, {"http://unsafe"})
- res = _run(module.update("mcp-1"))
- assert "blocked unsafe url" in res["message"]
-
-
-def test_update_service_paths(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- _stub_url_safety(monkeypatch, module)
-
- existing = _DummyMCPServer(
- id="mcp-1",
- name="srv",
- url="http://server",
- server_type="sse",
- tenant_id="tenant_1",
- variables={"tools": {"old": {"enabled": True}}, "token": "abc"},
- headers={"Authorization": "old"},
- )
- updated = _DummyMCPServer(
- id="mcp-1",
- name="srv-new",
- url="http://server-new",
- server_type="sse",
- tenant_id="tenant_1",
- variables={"tools": {"tool_a": {"name": "tool_a"}}},
- headers={"Authorization": "new"},
- )
-
- base_payload = {
- "mcp_id": "mcp-1",
- "name": "srv-new",
- "url": "http://server-new",
- "server_type": "sse",
- "headers": '{"Authorization": "new"}',
- "variables": '{"tools": {"ignore": 1}, "token": "new"}',
- "timeout": "3.0",
- }
-
- _set_request_json(monkeypatch, module, dict(base_payload))
- monkeypatch.setattr(module.MCPServerService, "get_by_id", lambda _mcp_id: (True, existing))
-
- async def _thread_pool_tools_error(_func, _servers, _timeout):
- return None, "update tools error"
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_tools_error)
- res = _run(module.update("mcp-1"))
- assert res["code"] == 102
- assert "update tools error" in res["message"]
-
- _set_request_json(monkeypatch, module, dict(base_payload))
-
- async def _thread_pool_ok(_func, servers, _timeout):
- return {servers[0].name: [{"name": "tool_a"}, {"bad": True}]}, None
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_ok)
- monkeypatch.setattr(module.MCPServerService, "filter_update", lambda *_args, **_kwargs: False)
- res = _run(module.update("mcp-1"))
- assert "Failed to updated MCP server" in res["message"]
-
- _set_request_json(monkeypatch, module, dict(base_payload))
- monkeypatch.setattr(module.MCPServerService, "filter_update", lambda *_args, **_kwargs: True)
-
- def _get_by_id_fetch_fail(_mcp_id):
- if _get_by_id_fetch_fail.calls == 0:
- _get_by_id_fetch_fail.calls += 1
- return True, existing
- return False, None
-
- _get_by_id_fetch_fail.calls = 0
- monkeypatch.setattr(module.MCPServerService, "get_by_id", _get_by_id_fetch_fail)
- res = _run(module.update("mcp-1"))
- assert "Failed to fetch updated MCP server" in res["message"]
-
- _set_request_json(monkeypatch, module, dict(base_payload))
-
- def _get_by_id_success(_mcp_id):
- if _get_by_id_success.calls == 0:
- _get_by_id_success.calls += 1
- return True, existing
- return True, updated
-
- _get_by_id_success.calls = 0
- monkeypatch.setattr(module.MCPServerService, "get_by_id", _get_by_id_success)
- res = _run(module.update("mcp-1"))
- assert res["code"] == 0
- assert res["data"]["id"] == "mcp-1"
-
- _set_request_json(monkeypatch, module, dict(base_payload))
- monkeypatch.setattr(module.MCPServerService, "get_by_id", lambda _mcp_id: (True, existing))
-
- async def _thread_pool_raises(_func, _servers, _timeout):
- raise RuntimeError("update explode")
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_raises)
- res = _run(module.update("mcp-1"))
- assert res["code"] == 100
- assert "update explode" in res["message"]
-
-
-def test_rm_failure_success_and_exception(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- server = _DummyMCPServer(id="id1", name="srv", url="http://a", server_type="sse", tenant_id="tenant_1", variables={})
- monkeypatch.setattr(module.MCPServerService, "get_by_id", lambda _mcp_id: (True, server))
-
- _set_request_json(monkeypatch, module, {"mcp_ids": ["a", "b"]})
- monkeypatch.setattr(module.MCPServerService, "delete_by_ids", lambda _ids: False)
- res = _run(module.rm("id1"))
- assert "Failed to delete MCP servers" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcp_ids": ["a", "b"]})
- monkeypatch.setattr(module.MCPServerService, "delete_by_ids", lambda _ids: True)
- res = _run(module.rm("id1"))
- assert res["code"] == 0
- assert res["data"] is True
-
- _set_request_json(monkeypatch, module, {"mcp_ids": ["a", "b"]})
-
- def _raise_rm(_ids):
- raise RuntimeError("rm explode")
-
- monkeypatch.setattr(module.MCPServerService, "delete_by_ids", _raise_rm)
- res = _run(module.rm("id1"))
- assert res["code"] == 100
- assert "rm explode" in res["message"]
-
-
-def test_import_multiple_missing_servers_and_exception(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- _stub_url_safety(monkeypatch, module)
-
- _set_request_json(monkeypatch, module, {"mcpServers": {}})
- res = _run(module.import_multiple.__wrapped__())
- assert "No MCP servers provided" in res["message"]
-
- _set_request_json(monkeypatch, module, {"mcpServers": {"srv": {"type": "sse", "url": "http://x"}}, "timeout": "1"})
-
- def _raise_import(**_kwargs):
- raise RuntimeError("import explode")
-
- monkeypatch.setattr(module.MCPServerService, "get_by_name_and_tenant", _raise_import)
- res = _run(module.import_multiple.__wrapped__())
- assert res["code"] == 100
- assert "import explode" in res["message"]
-
-
-def test_import_multiple_mixed_results(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- _stub_url_safety(monkeypatch, module, {"http://unsafe"})
-
- payload = {
- "mcpServers": {
- "missing_fields": {"type": "sse"},
- "": {"type": "sse", "url": "http://empty"},
- "invalid_type": {"type": "invalid", "url": "http://invalid"},
- "non_string_url": {"type": "sse", "url": True},
- "unsafe": {"type": "sse", "url": "http://unsafe"},
- "dup": {"type": "sse", "url": "http://dup", "authorization_token": "dup-token"},
- "tool_err": {"type": "sse", "url": "http://err"},
- "insert_fail": {"type": "sse", "url": "http://fail"},
- },
- "timeout": "3",
- }
- _set_request_json(monkeypatch, module, payload)
-
- monkeypatch.setattr(module, "get_uuid", lambda: "uuid-import")
-
- def _get_by_name_and_tenant(name, tenant_id):
- if name == "dup" and not _get_by_name_and_tenant.first_dup_seen:
- _get_by_name_and_tenant.first_dup_seen = True
- return True, object()
- return False, None
-
- _get_by_name_and_tenant.first_dup_seen = False
- monkeypatch.setattr(module.MCPServerService, "get_by_name_and_tenant", _get_by_name_and_tenant)
-
- async def _thread_pool_exec(func, servers, _timeout):
- mcp_server = servers[0]
- if mcp_server.name == "tool_err":
- return None, "tool call failed"
- return {mcp_server.name: [{"name": "tool_a"}, {"invalid": True}]}, None
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_exec)
-
- def _insert(**kwargs):
- return kwargs["name"] != "insert_fail"
-
- monkeypatch.setattr(module.MCPServerService, "insert", _insert)
-
- res = _run(module.import_multiple.__wrapped__())
- assert res["code"] == 0
-
- results = {item["server"]: item for item in res["data"]["results"]}
- assert results["missing_fields"]["success"] is False
- assert "Missing required fields" in results["missing_fields"]["message"]
- assert results[""]["success"] is False
- assert "Invalid MCP name" in results[""]["message"]
- assert results["invalid_type"]["success"] is False
- assert "Unsupported MCP server type" in results["invalid_type"]["message"]
- assert results["non_string_url"]["success"] is False
- assert "Invalid url" in results["non_string_url"]["message"]
- assert results["unsafe"]["success"] is False
- assert "blocked unsafe url" in results["unsafe"]["message"]
- assert results["tool_err"]["success"] is False
- assert "tool call failed" in results["tool_err"]["message"]
- assert results["insert_fail"]["success"] is False
- assert "Failed to create MCP server" in results["insert_fail"]["message"]
- assert results["dup"]["success"] is True
- assert results["dup"]["new_name"] == "dup_0"
- assert "Renamed from 'dup' to 'dup_0' avoid duplication" == results["dup"]["message"]
-
-
-def test_detail_download_success_and_exception(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args({"mode": "download"})))
-
- monkeypatch.setattr(
- module.MCPServerService,
- "get_by_id",
- lambda _mcp_id: (
- True,
- _DummyMCPServer(
- id="id1",
- name="srv-one",
- url="http://one",
- server_type="sse",
- tenant_id="tenant_1",
- variables={"authorization_token": "tok", "tools": {"tool_a": {"enabled": True}}},
- ),
- ),
- )
- res = module.detail("id1")
- assert res["code"] == 0
- assert list(res["data"]["mcpServers"].keys()) == ["srv-one"]
-
- monkeypatch.setattr(module.MCPServerService, "get_by_id", lambda _mcp_id: (False, None))
- res = module.detail("missing")
- assert res["code"] == 102
- assert "Cannot find MCP server missing for user tenant_1" in res["message"]
-
- monkeypatch.setattr(
- module.MCPServerService,
- "get_by_id",
- lambda _mcp_id: (
- True,
- _DummyMCPServer(
- id="id2",
- name="srv-two",
- url="http://two",
- server_type="sse",
- tenant_id="other",
- variables={},
- ),
- ),
- )
- res = module.detail("id2")
- assert res["code"] == 102
- assert "Cannot find MCP server id2 for user tenant_1" in res["message"]
-
- def _raise_export(_mcp_id):
- raise RuntimeError("export explode")
-
- monkeypatch.setattr(module.MCPServerService, "get_by_id", _raise_export)
- res = module.detail("id1")
- assert res["code"] == 100
- assert "export explode" in res["message"]
-
-
-def test_test_mcp_route_matrix_unit(monkeypatch):
- module = _load_mcp_api(monkeypatch)
- _stub_url_safety(monkeypatch, module)
-
- _set_request_json(monkeypatch, module, {"url": "", "server_type": "sse"})
- res = _run(module.test_mcp("mcp-1"))
- assert "Invalid MCP url" in res["message"]
-
- _set_request_json(monkeypatch, module, {"url": ["http://a"], "server_type": "sse"})
- res = _run(module.test_mcp("mcp-1"))
- assert "Invalid MCP url" in res["message"]
-
- _set_request_json(monkeypatch, module, {"url": "http://a", "server_type": "invalid"})
- res = _run(module.test_mcp("mcp-1"))
- assert "Unsupported MCP server type" in res["message"]
-
- close_calls = []
-
- async def _thread_pool_exec_inner_error(func, *args):
- if func is module.close_multiple_mcp_toolcall_sessions:
- close_calls.append(args[0])
- return None
- if getattr(func, "__name__", "") == "get_tools":
- raise RuntimeError("get tools explode")
- return func(*args)
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_exec_inner_error)
- _set_request_json(monkeypatch, module, {"url": "http://a", "server_type": "sse"})
- res = _run(module.test_mcp("mcp-1"))
- assert res["code"] == 102
- assert "Test MCP error: get tools explode" in res["message"]
- assert close_calls and len(close_calls[-1]) == 1
-
- close_calls_success = []
-
- async def _thread_pool_exec_success(func, *args):
- if func is module.close_multiple_mcp_toolcall_sessions:
- close_calls_success.append(args[0])
- return None
- return func(*args)
-
- monkeypatch.setattr(module, "thread_pool_exec", _thread_pool_exec_success)
- _set_request_json(monkeypatch, module, {"url": "http://a", "server_type": "sse"})
- res = _run(module.test_mcp("mcp-1"))
- assert res["code"] == 0
- assert res["data"][0]["name"] == "tool_a"
- assert all(tool["enabled"] is True for tool in res["data"])
- assert close_calls_success and len(close_calls_success[-1]) == 1
-
- def _raise_session(*_args, **_kwargs):
- raise RuntimeError("session explode")
-
- monkeypatch.setattr(module, "MCPToolCallSession", _raise_session)
- _set_request_json(monkeypatch, module, {"url": "http://a", "server_type": "sse"})
- res = _run(module.test_mcp("mcp-1"))
- assert res["code"] == 100
- assert "session explode" in res["message"]
diff --git a/test/testcases/test_web_api/test_memory_app/conftest.py b/test/testcases/test_web_api/test_memory_app/conftest.py
deleted file mode 100644
index 678c83d231..0000000000
--- a/test/testcases/test_web_api/test_memory_app/conftest.py
+++ /dev/null
@@ -1,41 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-import random
-from test_common import create_memory, list_memory, delete_memory
-
-
[email protected](scope="function")
-def add_memory_func(request, WebApiAuth):
- def cleanup():
- memory_list_res = list_memory(WebApiAuth)
- exist_memory_ids = [memory["id"] for memory in memory_list_res["data"]["memory_list"]]
- for memory_id in exist_memory_ids:
- delete_memory(WebApiAuth, memory_id)
-
- request.addfinalizer(cleanup)
-
- memory_ids = []
- for i in range(3):
- payload = {
- "name": f"test_memory_{i}",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = create_memory(WebApiAuth, payload)
- memory_ids.append(res["data"]["id"])
- return memory_ids
diff --git a/test/testcases/test_web_api/test_memory_app/test_create_memory.py b/test/testcases/test_web_api/test_memory_app/test_create_memory.py
deleted file mode 100644
index 9ecaf7c78f..0000000000
--- a/test/testcases/test_web_api/test_memory_app/test_create_memory.py
+++ /dev/null
@@ -1,97 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-import re
-
-import pytest
-from test_common import create_memory
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- ids=["empty_auth", "invalid_api_token"],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = create_memory(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestMemoryCreate:
- @pytest.mark.p1
- @pytest.mark.parametrize("name", ["test_memory_name", "d" * 128])
- def test_name(self, WebApiAuth, name):
- payload = {
- "name": name,
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = create_memory(WebApiAuth, payload)
- assert res["code"] == 0, res
- pattern = rf"^{name}|{name}(?:\((\d+)\))?$"
- escaped_name = re.escape(res["data"]["name"])
- assert re.match(pattern, escaped_name), res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "Memory name cannot be empty or whitespace."),
- (" ", "Memory name cannot be empty or whitespace."),
- ("a" * 129, f"Memory name '{'a' * 129}' exceeds limit of 128."),
- ],
- ids=["empty_name", "space_name", "too_long_name"],
- )
- def test_name_invalid(self, WebApiAuth, name, expected_message):
- payload = {
- "name": name,
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = create_memory(WebApiAuth, payload)
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("name", ["invalid_type_name", "memory_alpha"])
- def test_type_invalid(self, WebApiAuth, name):
- payload = {"name": name, "memory_type": ["something"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
- res = create_memory(WebApiAuth, payload)
- assert res["message"] == f"Memory type '{ {'something'} }' is not supported.", res
-
- @pytest.mark.p3
- def test_name_duplicated(self, WebApiAuth):
- name = "duplicated_name_test"
- payload = {
- "name": name,
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(0, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res1 = create_memory(WebApiAuth, payload)
- assert res1["code"] == 0, res1
-
- res2 = create_memory(WebApiAuth, payload)
- assert res2["code"] == 0, res2
diff --git a/test/testcases/test_web_api/test_memory_app/test_list_memory.py b/test/testcases/test_web_api/test_memory_app/test_list_memory.py
deleted file mode 100644
index 3bb6878731..0000000000
--- a/test/testcases/test_web_api/test_memory_app/test_list_memory.py
+++ /dev/null
@@ -1,133 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-import pytest
-from test_common import list_memory, get_memory_config
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = list_memory(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestCapability:
- @pytest.mark.p3
- def test_capability(self, WebApiAuth):
- count = 100
- with ThreadPoolExecutor(max_workers=5) as executor:
- futures = [executor.submit(list_memory, WebApiAuth) for i in range(count)]
- responses = list(as_completed(futures))
- assert len(responses) == count, responses
- assert all(future.result()["code"] == 0 for future in futures)
-
-
[email protected]("add_memory_func")
-class TestMemoryList:
- @pytest.mark.p2
- def test_params_unset(self, WebApiAuth):
- res = list_memory(WebApiAuth, None)
- assert res["code"] == 0, res
-
- @pytest.mark.p2
- def test_params_empty(self, WebApiAuth):
- res = list_memory(WebApiAuth, {})
- assert res["code"] == 0, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page": 1, "page_size": 10}, 3),
- ({"page": 2, "page_size": 10}, 0),
- ({"page": 1, "page_size": 2}, 2),
- ({"page": 2, "page_size": 2}, 1),
- ({"page": 5, "page_size": 10}, 0),
- ],
- ids=["normal_first_page", "beyond_max_page", "normal_last_partial_page", "normal_middle_page", "full_data_single_page"],
- )
- def test_page(self, WebApiAuth, params, expected_page_size):
- # have added 3 memories in fixture
- res = list_memory(WebApiAuth, params)
- assert res["code"] == 0, res
- assert len(res["data"]["memory_list"]) == expected_page_size, res
-
- @pytest.mark.p2
- def test_filter_memory_type(self, WebApiAuth):
- res = list_memory(WebApiAuth, {"memory_type": ["semantic"]})
- assert res["code"] == 0, res
- for memory in res["data"]["memory_list"]:
- assert "semantic" in memory["memory_type"], res
-
- @pytest.mark.p2
- def test_filter_multi_memory_type(self, WebApiAuth):
- res = list_memory(WebApiAuth, {"memory_type": ["episodic", "procedural"]})
- assert res["code"] == 0, res
- for memory in res["data"]["memory_list"]:
- assert "episodic" in memory["memory_type"] or "procedural" in memory["memory_type"], res
-
- @pytest.mark.p2
- def test_filter_storage_type(self, WebApiAuth):
- res = list_memory(WebApiAuth, {"storage_type": "table"})
- assert res["code"] == 0, res
- for memory in res["data"]["memory_list"]:
- assert memory["storage_type"] == "table", res
-
- @pytest.mark.p2
- def test_match_keyword(self, WebApiAuth):
- res = list_memory(WebApiAuth, {"keywords": "s"})
- assert res["code"] == 0, res
- for memory in res["data"]["memory_list"]:
- assert "s" in memory["name"], res
-
- @pytest.mark.p1
- def test_get_config(self, WebApiAuth):
- memory_list = list_memory(WebApiAuth, {})
- assert memory_list["code"] == 0, memory_list
-
- memory_config = get_memory_config(WebApiAuth, memory_list["data"]["memory_list"][0]["id"])
- assert memory_config["code"] == 0, memory_config
- assert memory_config["data"]["id"] == memory_list["data"]["memory_list"][0]["id"], memory_config
- for field in [
- "name",
- "avatar",
- "tenant_id",
- "owner_name",
- "memory_type",
- "storage_type",
- "embd_id",
- "llm_id",
- "permissions",
- "description",
- "memory_size",
- "forgetting_policy",
- "temperature",
- "system_prompt",
- "user_prompt",
- ]:
- assert field in memory_config["data"], memory_config
diff --git a/test/testcases/test_web_api/test_memory_app/test_rm_memory.py b/test/testcases/test_web_api/test_memory_app/test_rm_memory.py
deleted file mode 100644
index a9866f4db8..0000000000
--- a/test/testcases/test_web_api/test_memory_app/test_rm_memory.py
+++ /dev/null
@@ -1,54 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from test_common import list_memory, delete_memory
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = delete_memory(invalid_auth, "some_memory_id")
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestMemoryDelete:
- @pytest.mark.p1
- def test_memory_id(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- res = delete_memory(WebApiAuth, memory_ids[0])
- assert res["code"] == 0, res
-
- res = list_memory(WebApiAuth)
- assert res["data"]["total_count"] == 2, res
-
- @pytest.mark.p2
- @pytest.mark.usefixtures("add_memory_func")
- def test_id_wrong_uuid(self, WebApiAuth):
- res = delete_memory(WebApiAuth, "d94a8dc02c9711f0930f7fbc369eab6d")
- assert res["code"] == 404, res
-
- res = list_memory(WebApiAuth)
- assert len(res["data"]["memory_list"]) == 3, res
diff --git a/test/testcases/test_web_api/test_memory_app/test_update_memory.py b/test/testcases/test_web_api/test_memory_app/test_update_memory.py
deleted file mode 100644
index de47b55a95..0000000000
--- a/test/testcases/test_web_api/test_memory_app/test_update_memory.py
+++ /dev/null
@@ -1,159 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import re
-
-import pytest
-from test_common import update_memory
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from utils import encode_avatar
-from utils.file_utils import create_image_file
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- ids=["empty_auth", "invalid_api_token"],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = update_memory(invalid_auth, "memory_id")
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestMemoryUpdate:
- @pytest.mark.p1
- @pytest.mark.parametrize("name", ["updated_memory", "f" * 128])
- def test_name(self, WebApiAuth, add_memory_func, name):
- memory_ids = add_memory_func
- payload = {"name": name}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- pattern = rf"^{re.escape(name)}(?:\(\d+\))?$"
- assert re.match(pattern, res["data"]["name"]), res
-
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "name, expected_message",
- [
- ("", "Memory name cannot be empty or whitespace."),
- (" ", "Memory name cannot be empty or whitespace."),
- ("a" * 129, f"Memory name '{'a' * 129}' exceeds limit of 128."),
- ],
- )
- def test_name_invalid(self, WebApiAuth, add_memory_func, name, expected_message):
- memory_ids = add_memory_func
- payload = {"name": name}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 101, res
- assert res["message"] == expected_message, res
-
- @pytest.mark.p2
- def test_duplicate_name(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- payload = {"name": "Test_Memory"}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
-
- payload = {"name": "Test_Memory"}
- res = update_memory(WebApiAuth, memory_ids[1], payload)
- assert res["code"] == 0, res
- assert res["data"]["name"] == "Test_Memory(1)", res
-
- @pytest.mark.p2
- def test_avatar(self, WebApiAuth, add_memory_func, tmp_path):
- memory_ids = add_memory_func
- fn = create_image_file(tmp_path / "ragflow_test.png")
- payload = {"avatar": f"data:image/png;base64,{encode_avatar(fn)}"}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["avatar"] == f"data:image/png;base64,{encode_avatar(fn)}", res
-
- @pytest.mark.p2
- def test_description(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- description = "This is a test description."
- payload = {"description": description}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["description"] == description, res
-
- @pytest.mark.p1
- def test_llm(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- llm_id = "glm-4@ZHIPU-AI"
- payload = {"llm_id": llm_id}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["llm_id"] == llm_id, res
-
- @pytest.mark.p2
- def test_reject_direct_tenant_model_ids(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- payload = {"tenant_llm_id": 999999, "tenant_embd_id": 999998}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 101, res
- assert "Do not set tenant_llm_id or tenant_embd_id directly" in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("permission", ["me", "team"], ids=["me", "team"])
- def test_permission(self, WebApiAuth, add_memory_func, permission):
- memory_ids = add_memory_func
- payload = {"permissions": permission}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["permissions"] == permission.lower().strip(), res
-
- @pytest.mark.p1
- def test_memory_size(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- memory_size = 1048576 # 1 MB
- payload = {"memory_size": memory_size}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["memory_size"] == memory_size, res
-
- @pytest.mark.p1
- def test_temperature(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- temperature = 0.7
- payload = {"temperature": temperature}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["temperature"] == temperature, res
-
- @pytest.mark.p1
- def test_system_prompt(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- system_prompt = "This is a system prompt."
- payload = {"system_prompt": system_prompt}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["system_prompt"] == system_prompt, res
-
- @pytest.mark.p1
- def test_user_prompt(self, WebApiAuth, add_memory_func):
- memory_ids = add_memory_func
- user_prompt = "This is a user prompt."
- payload = {"user_prompt": user_prompt}
- res = update_memory(WebApiAuth, memory_ids[0], payload)
- assert res["code"] == 0, res
- assert res["data"]["user_prompt"] == user_prompt, res
diff --git a/test/testcases/test_web_api/test_message_app/conftest.py b/test/testcases/test_web_api/test_message_app/conftest.py
deleted file mode 100644
index 69b15c82fb..0000000000
--- a/test/testcases/test_web_api/test_message_app/conftest.py
+++ /dev/null
@@ -1,159 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import time
-import uuid
-
-import pytest
-import random
-from test_common import create_memory, list_memory, add_message, delete_memory
-
-
[email protected](scope="class")
-def add_empty_raw_type_memory(request, WebApiAuth):
- def cleanup():
- memory_list_res = list_memory(WebApiAuth)
- exist_memory_ids = [memory["id"] for memory in memory_list_res["data"]["memory_list"]]
- for _memory_id in exist_memory_ids:
- delete_memory(WebApiAuth, _memory_id)
-
- request.addfinalizer(cleanup)
- payload = {"name": "test_memory_0", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
- res = create_memory(WebApiAuth, payload)
- memory_id = res["data"]["id"]
- request.cls.memory_id = memory_id
- request.cls.memory_type = payload["memory_type"]
- return memory_id
-
-
[email protected](scope="class")
-def add_empty_multiple_type_memory(request, WebApiAuth):
- def cleanup():
- memory_list_res = list_memory(WebApiAuth)
- exist_memory_ids = [memory["id"] for memory in memory_list_res["data"]["memory_list"]]
- for _memory_id in exist_memory_ids:
- delete_memory(WebApiAuth, _memory_id)
-
- request.addfinalizer(cleanup)
- payload = {
- "name": "test_memory_0",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = create_memory(WebApiAuth, payload)
- memory_id = res["data"]["id"]
- request.cls.memory_id = memory_id
- request.cls.memory_type = payload["memory_type"]
- return memory_id
-
-
[email protected](scope="class")
-def add_2_multiple_type_memory(request, WebApiAuth):
- def cleanup():
- memory_list_res = list_memory(WebApiAuth)
- exist_memory_ids = [memory["id"] for memory in memory_list_res["data"]["memory_list"]]
- for _memory_id in exist_memory_ids:
- delete_memory(WebApiAuth, _memory_id)
-
- request.addfinalizer(cleanup)
- memory_ids = []
- for i in range(2):
- payload = {
- "name": f"test_memory_{i}",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = create_memory(WebApiAuth, payload)
- memory_ids.append(res["data"]["id"])
- request.cls.memory_ids = memory_ids
- return memory_ids
-
-
[email protected](scope="class")
-def add_memory_with_multiple_type_message_func(request, WebApiAuth):
- def cleanup():
- memory_list_res = list_memory(WebApiAuth)
- exist_memory_ids = [memory["id"] for memory in memory_list_res["data"]["memory_list"]]
- for _memory_id in exist_memory_ids:
- delete_memory(WebApiAuth, _memory_id)
-
- request.addfinalizer(cleanup)
-
- payload = {
- "name": "test_memory_0",
- "memory_type": ["raw"] + random.choices(["semantic", "episodic", "procedural"], k=random.randint(1, 3)),
- "embd_id": "BAAI/bge-small-en-v1.5@Builtin",
- "llm_id": "glm-4-flash@ZHIPU-AI",
- }
- res = create_memory(WebApiAuth, payload)
- memory_id = res["data"]["id"]
- agent_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_id,
- "session_id": uuid.uuid4().hex,
- "user_id": "",
- "user_input": "what is coriander?",
- "agent_response": """
-Coriander is a versatile herb with two main edible parts, and its name can refer to both:
-1. Leaves and Stems (often called Cilantro or Fresh Coriander): These are the fresh, green, fragrant leaves and tender stems of the plant Coriandrum sativum. They have a bright, citrusy, and sometimes pungent flavor. Cilantro is widely used as a garnish or key ingredient in cuisines like Mexican, Indian, Thai, and Middle Eastern.
-2. Seeds (called Coriander Seeds): These are the dried, golden-brown seeds of the same plant. When ground, they become coriander powder. The seeds have a warm, nutty, floral, and slightly citrusy taste, completely different from the fresh leaves. They are a fundamental spice in curries, stews, pickles, and baking.
-Key Point of Confusion: The naming differs by region. In North America, "coriander" typically refers to the seeds, while "cilantro" refers to the fresh leaves. In the UK, Europe, and many other parts of the world, "coriander" refers to the fresh herb, and the seeds are called "coriander seeds."
-""",
- }
- add_message(WebApiAuth, message_payload)
- request.cls.memory_id = memory_id
- request.cls.agent_id = agent_id
- time.sleep(2) # make sure refresh to index before search
- return memory_id
-
-
[email protected](scope="class")
-def add_memory_with_5_raw_message_func(request, WebApiAuth):
- def cleanup():
- memory_list_res = list_memory(WebApiAuth)
- exist_memory_ids = [memory["id"] for memory in memory_list_res["data"]["memory_list"]]
- for _memory_id in exist_memory_ids:
- delete_memory(WebApiAuth, _memory_id)
-
- request.addfinalizer(cleanup)
-
- payload = {"name": "test_memory_1", "memory_type": ["raw"], "embd_id": "BAAI/bge-small-en-v1.5@Builtin", "llm_id": "glm-4-flash@ZHIPU-AI"}
- res = create_memory(WebApiAuth, payload)
- memory_id = res["data"]["id"]
- agent_ids = [uuid.uuid4().hex for _ in range(2)]
- session_ids = [uuid.uuid4().hex for _ in range(5)]
- for i in range(5):
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_ids[i % 2],
- "session_id": session_ids[i],
- "user_id": "",
- "user_input": "what is coriander?",
- "agent_response": """
-Coriander is a versatile herb with two main edible parts, and its name can refer to both:
-1. Leaves and Stems (often called Cilantro or Fresh Coriander): These are the fresh, green, fragrant leaves and tender stems of the plant Coriandrum sativum. They have a bright, citrusy, and sometimes pungent flavor. Cilantro is widely used as a garnish or key ingredient in cuisines like Mexican, Indian, Thai, and Middle Eastern.
-2. Seeds (called Coriander Seeds): These are the dried, golden-brown seeds of the same plant. When ground, they become coriander powder. The seeds have a warm, nutty, floral, and slightly citrusy taste, completely different from the fresh leaves. They are a fundamental spice in curries, stews, pickles, and baking.
-Key Point of Confusion: The naming differs by region. In North America, "coriander" typically refers to the seeds, while "cilantro" refers to the fresh leaves. In the UK, Europe, and many other parts of the world, "coriander" refers to the fresh herb, and the seeds are called "coriander seeds."
-""",
- }
- add_message(WebApiAuth, message_payload)
- request.cls.memory_id = memory_id
- request.cls.agent_ids = agent_ids
- request.cls.session_ids = session_ids
- time.sleep(2) # make sure refresh to index before search
- return memory_id
diff --git a/test/testcases/test_web_api/test_message_app/test_add_message.py b/test/testcases/test_web_api/test_message_app/test_add_message.py
deleted file mode 100644
index 544aceb63d..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_add_message.py
+++ /dev/null
@@ -1,157 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import time
-import uuid
-import pytest
-
-from test_common import list_memory_message, add_message
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = add_message(invalid_auth, {})
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("add_empty_raw_type_memory")
-class TestAddRawMessage:
- @pytest.mark.p1
- def test_add_raw_message(self, WebApiAuth):
- memory_id = self.memory_id
- agent_id = uuid.uuid4().hex
- session_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_id,
- "session_id": session_id,
- "user_id": "",
- "user_input": "what is pineapple?",
- "agent_response": """
-A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
-Scientific Name: Ananas comosus
-Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
-Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
-Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
-Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
-Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
-Are you asking about the fruit itself, or its use in a specific context?
-""",
- }
- add_res = add_message(WebApiAuth, message_payload)
- assert add_res["code"] == 0, add_res
- time.sleep(2) # make sure refresh to index before search
- message_res = list_memory_message(WebApiAuth, memory_id, params={"agent_id": agent_id, "keywords": session_id})
- assert message_res["code"] == 0, message_res
- assert message_res["data"]["messages"]["total_count"] > 0
- for message in message_res["data"]["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
- assert message["session_id"] == session_id, message
-
- @pytest.mark.p2
- def test_add_message_invalid_memory_id(self, WebApiAuth):
- message_payload = {
- "memory_id": ["missing_memory_id"],
- "agent_id": uuid.uuid4().hex,
- "session_id": uuid.uuid4().hex,
- "user_id": "",
- "user_input": "what is pineapple?",
- "agent_response": "pineapple response",
- }
- res = add_message(WebApiAuth, message_payload)
- assert res["code"] == 500, res
- assert "Some messages failed to add" in res["message"], res
-
-
[email protected]("add_empty_multiple_type_memory")
-class TestAddMultipleTypeMessage:
- @pytest.mark.p1
- def test_add_multiple_type_message(self, WebApiAuth):
- memory_id = self.memory_id
- agent_id = uuid.uuid4().hex
- session_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": [memory_id],
- "agent_id": agent_id,
- "session_id": session_id,
- "user_id": "",
- "user_input": "what is pineapple?",
- "agent_response": """
-A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
-Scientific Name: Ananas comosus
-Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
-Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
-Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
-Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
-Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
-Are you asking about the fruit itself, or its use in a specific context?
-""",
- }
- add_res = add_message(WebApiAuth, message_payload)
- assert add_res["code"] == 0, add_res
- time.sleep(2) # make sure refresh to index before search
- message_res = list_memory_message(WebApiAuth, memory_id, params={"agent_id": agent_id, "keywords": session_id})
- assert message_res["code"] == 0, message_res
- assert message_res["data"]["messages"]["total_count"] > 0
- for message in message_res["data"]["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
- assert message["session_id"] == session_id, message
-
-
[email protected]("add_2_multiple_type_memory")
-class TestAddToMultipleMemory:
- @pytest.mark.p1
- def test_add_to_multiple_memory(self, WebApiAuth):
- memory_ids = self.memory_ids
- agent_id = uuid.uuid4().hex
- session_id = uuid.uuid4().hex
- message_payload = {
- "memory_id": memory_ids,
- "agent_id": agent_id,
- "session_id": session_id,
- "user_id": "",
- "user_input": "what is pineapple?",
- "agent_response": """
-A pineapple is a tropical fruit known for its sweet, tangy flavor and distinctive, spiky appearance. Here are the key facts:
-Scientific Name: Ananas comosus
-Physical Description: It has a tough, spiky, diamond-patterned outer skin (rind) that is usually green, yellow, or brownish. Inside, the juicy yellow flesh surrounds a fibrous core.
-Growth: Unlike most fruits, pineapples do not grow on trees. They grow from a central stem as a composite fruit, meaning they are formed from many individual berries that fuse together around the core. They grow on a short, leafy plant close to the ground.
-Uses: Pineapples are eaten fresh, cooked, grilled, juiced, or canned. They are a popular ingredient in desserts, fruit salads, savory dishes (like pizzas or ham glazes), smoothies, and cocktails.
-Nutrition: They are a good source of Vitamin C, manganese, and contain an enzyme called bromelain, which aids in digestion and can tenderize meat.
-Symbolism: The pineapple is a traditional symbol of hospitality and welcome in many cultures.
-Are you asking about the fruit itself, or its use in a specific context?
-""",
- }
- add_res = add_message(WebApiAuth, message_payload)
- assert add_res["code"] == 0, add_res
- time.sleep(2) # make sure refresh to index before search
- for memory_id in memory_ids:
- message_res = list_memory_message(WebApiAuth, memory_id, params={"agent_id": agent_id, "keywords": session_id})
- assert message_res["code"] == 0, message_res
- assert message_res["data"]["messages"]["total_count"] > 0
- for message in message_res["data"]["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
- assert message["session_id"] == session_id, message
diff --git a/test/testcases/test_web_api/test_message_app/test_forget_message.py b/test/testcases/test_web_api/test_message_app/test_forget_message.py
deleted file mode 100644
index afc44eeb7b..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_forget_message.py
+++ /dev/null
@@ -1,68 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-import pytest
-import requests
-from test_common import forget_message, list_memory_message, get_message_content
-from configs import HOST_ADDRESS, INVALID_API_TOKEN, VERSION
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = forget_message(invalid_auth, "empty_memory_id", 0)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestForgetMessage:
- @pytest.mark.p1
- def test_forget_message(self, WebApiAuth):
- memory_id = self.memory_id
- list_res = list_memory_message(WebApiAuth, memory_id)
- assert list_res["code"] == 0, list_res
- assert len(list_res["data"]["messages"]["message_list"]) > 0
-
- message = random.choice(list_res["data"]["messages"]["message_list"])
- res = forget_message(WebApiAuth, memory_id, message["message_id"])
- assert res["code"] == 0, res
-
- forgot_message_res = get_message_content(WebApiAuth, memory_id, message["message_id"])
- assert forgot_message_res["code"] == 0, forgot_message_res
- assert forgot_message_res["data"]["forget_at"] not in ["-", ""], forgot_message_res
-
- @pytest.mark.p2
- def test_forget_message_invalid_memory_id(self, WebApiAuth):
- res = forget_message(WebApiAuth, "missing_memory_id", 1)
- assert res["code"] == 404, res
- assert "not found" in res["message"].lower(), res
-
- @pytest.mark.p2
- def test_forget_message_invalid_message_id(self, WebApiAuth):
- memory_id = self.memory_id
- url = f"{HOST_ADDRESS}/api/{VERSION}/messages/{memory_id}:invalid_message_id"
- res = requests.delete(url=url, headers={"Content-Type": "application/json"}, auth=WebApiAuth).json()
- assert res["code"] == 500, res
- assert "Internal server error" in res["message"], res
diff --git a/test/testcases/test_web_api/test_message_app/test_get_message_content.py b/test/testcases/test_web_api/test_message_app/test_get_message_content.py
deleted file mode 100644
index 098ddf80a8..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_get_message_content.py
+++ /dev/null
@@ -1,64 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-
-import pytest
-from test_common import get_message_content, get_recent_message
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = get_message_content(invalid_auth, "empty_memory_id", 0)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("add_memory_with_multiple_type_message_func")
-class TestGetMessageContent:
- @pytest.mark.p1
- def test_get_message_content(self, WebApiAuth):
- memory_id = self.memory_id
- recent_messages = get_recent_message(WebApiAuth, {"memory_id": memory_id})
- assert len(recent_messages["data"]) > 0, recent_messages
- message = random.choice(recent_messages["data"])
- message_id = message["message_id"]
- content_res = get_message_content(WebApiAuth, memory_id, message_id)
- for field in ["content", "content_embed"]:
- assert field in content_res["data"]
- assert content_res["data"][field] is not None, content_res
-
- @pytest.mark.p2
- def test_get_message_content_invalid_memory_id(self, WebApiAuth):
- res = get_message_content(WebApiAuth, "missing_memory_id", 1)
- assert res["code"] == 404, res
- assert "not found" in res["message"].lower(), res
-
- @pytest.mark.p2
- def test_get_message_content_invalid_message_id(self, WebApiAuth):
- memory_id = self.memory_id
- res = get_message_content(WebApiAuth, memory_id, 999999999)
- assert res["code"] == 404, res
- assert "not found" in res["message"].lower(), res
diff --git a/test/testcases/test_web_api/test_message_app/test_get_recent_message.py b/test/testcases/test_web_api/test_message_app/test_get_recent_message.py
deleted file mode 100644
index 3c912c636a..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_get_recent_message.py
+++ /dev/null
@@ -1,79 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-
-import pytest
-from test_common import get_recent_message
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = get_recent_message(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestGetRecentMessage:
- @pytest.mark.p1
- def test_get_recent_messages(self, WebApiAuth):
- memory_id = self.memory_id
- res = get_recent_message(WebApiAuth, params={"memory_id": memory_id})
- assert res["code"] == 0, res
- assert len(res["data"]) == 5, res
-
- @pytest.mark.p2
- def test_filter_recent_messages_by_agent(self, WebApiAuth):
- memory_id = self.memory_id
- agent_ids = self.agent_ids
- agent_id = random.choice(agent_ids)
- res = get_recent_message(WebApiAuth, params={"agent_id": agent_id, "memory_id": memory_id})
- assert res["code"] == 0, res
- for message in res["data"]:
- assert message["agent_id"] == agent_id, message
-
- @pytest.mark.p2
- def test_filter_recent_messages_by_session(self, WebApiAuth):
- memory_id = self.memory_id
- session_ids = self.session_ids
- session_id = random.choice(session_ids)
- res = get_recent_message(WebApiAuth, params={"session_id": session_id, "memory_id": memory_id})
- assert res["code"] == 0, res
- for message in res["data"]:
- assert message["session_id"] == session_id, message
-
- @pytest.mark.p2
- def test_get_recent_messages_missing_memory_id(self, WebApiAuth):
- res = get_recent_message(WebApiAuth, params={})
- assert res["code"] == 101, res
- assert "memory_ids is required" in res["message"], res
-
- @pytest.mark.p2
- def test_get_recent_messages_csv_memory_ids(self, WebApiAuth):
- memory_id = self.memory_id
- res = get_recent_message(WebApiAuth, params={"memory_id": f"{memory_id},{memory_id}"})
- assert res["code"] == 0, res
- assert isinstance(res["data"], list), res
diff --git a/test/testcases/test_web_api/test_message_app/test_list_message.py b/test/testcases/test_web_api/test_message_app/test_list_message.py
deleted file mode 100644
index e1c5a2aaeb..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_list_message.py
+++ /dev/null
@@ -1,98 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import os
-import random
-
-import pytest
-from test_common import list_memory_message
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = list_memory_message(invalid_auth, "")
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestMessageList:
- @pytest.mark.p2
- def test_params_unset(self, WebApiAuth):
- memory_id = self.memory_id
- res = list_memory_message(WebApiAuth, memory_id, params=None)
- assert res["code"] == 0, res
- assert len(res["data"]["messages"]["message_list"]) == 5, res
-
- @pytest.mark.p2
- def test_params_empty(self, WebApiAuth):
- memory_id = self.memory_id
- res = list_memory_message(WebApiAuth, memory_id, params={})
- assert res["code"] == 0, res
- assert len(res["data"]["messages"]["message_list"]) == 5, res
-
- @pytest.mark.p1
- @pytest.mark.parametrize(
- "params, expected_page_size",
- [
- ({"page": 1, "page_size": 10}, 5),
- ({"page": 2, "page_size": 10}, 0),
- ({"page": 1, "page_size": 2}, 2),
- ({"page": 3, "page_size": 2}, 1),
- ({"page": 5, "page_size": 10}, 0),
- ],
- ids=["normal_first_page", "beyond_max_page", "normal_last_partial_page", "normal_middle_page", "full_data_single_page"],
- )
- def test_page_size(self, WebApiAuth, params, expected_page_size):
- # have added 5 messages in fixture
- memory_id = self.memory_id
- res = list_memory_message(WebApiAuth, memory_id, params=params)
- assert res["code"] == 0, res
- assert len(res["data"]["messages"]["message_list"]) == expected_page_size, res
-
- @pytest.mark.p2
- def test_filter_agent_id(self, WebApiAuth):
- memory_id = self.memory_id
- agent_ids = self.agent_ids
- agent_id = random.choice(agent_ids)
- res = list_memory_message(WebApiAuth, memory_id, params={"agent_id": agent_id})
- assert res["code"] == 0, res
- for message in res["data"]["messages"]["message_list"]:
- assert message["agent_id"] == agent_id, message
-
- @pytest.mark.p2
- @pytest.mark.skipif(os.getenv("DOC_ENGINE") == "infinity", reason="Not support.")
- def test_search_keyword(self, WebApiAuth):
- memory_id = self.memory_id
- session_ids = self.session_ids
- session_id = random.choice(session_ids)
- slice_start = random.randint(0, len(session_id) - 2)
- slice_end = random.randint(slice_start + 1, len(session_id) - 1)
- keyword = session_id[slice_start:slice_end]
- res = list_memory_message(WebApiAuth, memory_id, params={"keywords": keyword})
- assert res["code"] == 0, res
- assert len(res["data"]["messages"]["message_list"]) > 0, res
- for message in res["data"]["messages"]["message_list"]:
- assert keyword in message["session_id"], message
diff --git a/test/testcases/test_web_api/test_message_app/test_message_routes_unit.py b/test/testcases/test_web_api/test_message_app/test_message_routes_unit.py
deleted file mode 100644
index cbf1d1fac8..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_message_routes_unit.py
+++ /dev/null
@@ -1,183 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import inspect
-import sys
-from copy import deepcopy
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _DummyArgs(dict):
- def getlist(self, key):
- value = self.get(key)
- if value is None:
- return []
- if isinstance(value, list):
- return value
- return [value]
-
-
-class _DummyMemoryApiService:
- async def add_message(self, *_args, **_kwargs):
- return True, "ok"
-
- async def get_messages(self, *_args, **_kwargs):
- return []
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _load_memory_routes_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.AUTH_API = "API"
- apps_mod.current_user = SimpleNamespace(id="user-1")
- apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- services_mod = ModuleType("api.apps.services")
- services_mod.memory_api_service = _DummyMemoryApiService()
- monkeypatch.setitem(sys.modules, "api.apps.services", services_mod)
-
- module_name = "test_message_routes_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "memory_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def _set_request_json(monkeypatch, module, payload):
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(deepcopy(payload)))
-
-
-def test_add_message_partial_failure_branch(monkeypatch):
- module = _load_memory_routes_module(monkeypatch)
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "memory_id": ["memory-1"],
- "agent_id": "agent-1",
- "session_id": "session-1",
- "user_input": "hello",
- "agent_response": "world",
- },
- )
-
- async def _add_message(_memory_ids, _message_dict):
- return False, "cannot enqueue"
-
- monkeypatch.setattr(module.memory_api_service, "add_message", _add_message)
-
- res = _run(inspect.unwrap(module.add_message)())
- assert res["code"] == module.RetCode.SERVER_ERROR, res
- assert "Some messages failed to add" in res["message"], res
-
-
-def test_get_messages_csv_and_missing_memory_ids(monkeypatch):
- module = _load_memory_routes_module(monkeypatch)
-
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs({})))
- res = _run(inspect.unwrap(module.get_messages)())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
- assert "memory_ids is required." in res["message"], res
-
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(args=_DummyArgs({"memory_id": "m1,m2", "agent_id": "a1", "session_id": "s1", "limit": "5"})),
- )
-
- async def _get_messages(memory_ids, agent_id, session_id, limit):
- assert memory_ids == ["m1", "m2"]
- assert agent_id == "a1"
- assert session_id == "s1"
- assert limit == 5
- return [{"message_id": 1}]
-
- monkeypatch.setattr(module.memory_api_service, "get_messages", _get_messages)
- res = _run(inspect.unwrap(module.get_messages)())
- assert res["code"] == module.RetCode.SUCCESS, res
- assert isinstance(res["data"], list), res
-
-
-def test_get_messages_rejects_limit_above_rest_api_max(monkeypatch):
- module = _load_memory_routes_module(monkeypatch)
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(args=_DummyArgs({"memory_id": "m1", "limit": "1000"})),
- )
- res = _run(inspect.unwrap(module.get_messages)())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
- assert "page_size must be less than or equal to 100" in res["message"], res
-
-
-def test_search_message_rejects_top_n_above_rest_api_max(monkeypatch):
- module = _load_memory_routes_module(monkeypatch)
- monkeypatch.setattr(
- module,
- "request",
- SimpleNamespace(args=_DummyArgs({"memory_id": "m1", "query": "hello", "top_n": "500"})),
- )
-
- async def _search_message(_filter_dict, _params):
- return []
-
- monkeypatch.setattr(module.memory_api_service, "search_message", _search_message)
- res = _run(inspect.unwrap(module.search_message)())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
- assert "page_size must be less than or equal to 100" in res["message"], res
diff --git a/test/testcases/test_web_api/test_message_app/test_search_message.py b/test/testcases/test_web_api/test_message_app/test_search_message.py
deleted file mode 100644
index beeac7fc62..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_search_message.py
+++ /dev/null
@@ -1,98 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from test_common import search_message, list_memory_message
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = search_message(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("add_memory_with_multiple_type_message_func")
-class TestSearchMessage:
- @pytest.mark.p1
- def test_query(self, WebApiAuth):
- memory_id = self.memory_id
- list_res = list_memory_message(WebApiAuth, memory_id)
- assert list_res["code"] == 0, list_res
- assert list_res["data"]["messages"]["total_count"] > 0
-
- query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
- res = search_message(WebApiAuth, {"memory_id": memory_id, "query": query})
- assert res["code"] == 0, res
- assert len(res["data"]) > 0
-
- @pytest.mark.p2
- def test_query_with_agent_filter(self, WebApiAuth):
- memory_id = self.memory_id
- list_res = list_memory_message(WebApiAuth, memory_id)
- assert list_res["code"] == 0, list_res
- assert list_res["data"]["messages"]["total_count"] > 0
-
- agent_id = self.agent_id
- query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
- res = search_message(WebApiAuth, {"memory_id": memory_id, "query": query, "agent_id": agent_id})
- assert res["code"] == 0, res
- assert len(res["data"]) > 0
- for message in res["data"]:
- assert message["agent_id"] == agent_id, message
-
- @pytest.mark.p2
- def test_query_with_not_default_params(self, WebApiAuth):
- memory_id = self.memory_id
- list_res = list_memory_message(WebApiAuth, memory_id)
- assert list_res["code"] == 0, list_res
- assert list_res["data"]["messages"]["total_count"] > 0
-
- query = "Coriander is a versatile herb with two main edible parts. What's its name can refer to?"
- params = {"similarity_threshold": 0.1, "keywords_similarity_weight": 0.6, "top_n": 4}
- res = search_message(WebApiAuth, {"memory_id": memory_id, "query": query, **params})
- assert res["code"] == 0, res
- assert len(res["data"]) > 0
- assert len(res["data"]) <= params["top_n"]
-
- @pytest.mark.p2
- def test_query_missing_query(self, WebApiAuth):
- memory_id = self.memory_id
- res = search_message(WebApiAuth, {"memory_id": memory_id})
- assert res["code"] in [100, 500], res
-
- @pytest.mark.p2
- def test_query_missing_memory_id(self, WebApiAuth):
- res = search_message(WebApiAuth, {"query": "what is coriander"})
- assert res["code"] == 0, res
- assert isinstance(res["data"], list), res
-
- @pytest.mark.p2
- def test_query_with_csv_memory_ids(self, WebApiAuth):
- memory_id = self.memory_id
- query = "Coriander is a versatile herb."
- res = search_message(WebApiAuth, {"memory_id": f"{memory_id},{memory_id}", "query": query})
- assert res["code"] == 0, res
- assert isinstance(res["data"], list), res
diff --git a/test/testcases/test_web_api/test_message_app/test_update_message_status.py b/test/testcases/test_web_api/test_message_app/test_update_message_status.py
deleted file mode 100644
index fa10ddadc9..0000000000
--- a/test/testcases/test_web_api/test_message_app/test_update_message_status.py
+++ /dev/null
@@ -1,107 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import random
-
-import pytest
-import requests
-from test_common import update_message_status, list_memory_message, get_message_content
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-from configs import HOST_ADDRESS, VERSION
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize(
- "invalid_auth, expected_code, expected_message",
- [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
- ],
- )
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = update_message_status(invalid_auth, "empty_memory_id", 0, False)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
[email protected]("add_memory_with_5_raw_message_func")
-class TestUpdateMessageStatus:
- @pytest.mark.p1
- def test_update_to_false(self, WebApiAuth):
- memory_id = self.memory_id
- list_res = list_memory_message(WebApiAuth, memory_id)
- assert list_res["code"] == 0, list_res
- assert len(list_res["data"]["messages"]["message_list"]) > 0
-
- message = random.choice(list_res["data"]["messages"]["message_list"])
- res = update_message_status(WebApiAuth, memory_id, message["message_id"], False)
- assert res["code"] == 0, res
-
- updated_message_res = get_message_content(WebApiAuth, memory_id, message["message_id"])
- assert updated_message_res["code"] == 0, res
- assert not updated_message_res["data"]["status"], res
-
- @pytest.mark.p1
- def test_update_to_true(self, WebApiAuth):
- memory_id = self.memory_id
- list_res = list_memory_message(WebApiAuth, memory_id)
- assert list_res["code"] == 0, list_res
- assert len(list_res["data"]["messages"]["message_list"]) > 0
- # set 1 random message to false first
- message = random.choice(list_res["data"]["messages"]["message_list"])
- set_to_false_res = update_message_status(WebApiAuth, memory_id, message["message_id"], False)
- assert set_to_false_res["code"] == 0, set_to_false_res
- updated_message_res = get_message_content(WebApiAuth, memory_id, message["message_id"])
- assert updated_message_res["code"] == 0, set_to_false_res
- assert not updated_message_res["data"]["status"], updated_message_res
- # set to true
- set_to_true_res = update_message_status(WebApiAuth, memory_id, message["message_id"], True)
- assert set_to_true_res["code"] == 0, set_to_true_res
- res = get_message_content(WebApiAuth, memory_id, message["message_id"])
- assert res["code"] == 0, res
- assert res["data"]["status"], res
-
- @pytest.mark.p2
- def test_update_invalid_status_type(self, WebApiAuth):
- memory_id = self.memory_id
- list_res = list_memory_message(WebApiAuth, memory_id)
- assert list_res["code"] == 0, list_res
- message_id = list_res["data"]["messages"]["message_list"][0]["message_id"]
-
- url = f"{HOST_ADDRESS}/api/{VERSION}/messages/{memory_id}:{message_id}"
- res = requests.put(url=url, headers={"Content-Type": "application/json"}, auth=WebApiAuth, json={"status": "false"}).json()
- assert res["code"] == 101, res
- assert "Status must be a boolean." in res["message"], res
-
- @pytest.mark.p2
- def test_update_invalid_memory_id(self, WebApiAuth):
- res = update_message_status(WebApiAuth, "missing_memory_id", 1, False)
- assert res["code"] == 404, res
- assert "not found" in res["message"].lower(), res
-
- @pytest.mark.p2
- def test_update_invalid_message_id(self, WebApiAuth):
- memory_id = self.memory_id
- url = f"{HOST_ADDRESS}/api/{VERSION}/messages/{memory_id}:invalid_message_id"
- res = requests.put(
- url=url,
- headers={"Content-Type": "application/json"},
- auth=WebApiAuth,
- json={"status": True},
- ).json()
- assert res["code"] == 500, res
- assert "Internal server error" in res["message"], res
diff --git a/test/testcases/test_web_api/test_plugin_app/test_llm_tools.py b/test/testcases/test_web_api/test_plugin_app/test_llm_tools.py
deleted file mode 100644
index 81db453519..0000000000
--- a/test/testcases/test_web_api/test_plugin_app/test_llm_tools.py
+++ /dev/null
@@ -1,99 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType
-
-import pytest
-from test_common import plugin_llm_tools
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-INVALID_AUTH_CASES = [
- (None, 401, "<Unauthorized '401: Unauthorized'>"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
-]
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_message", INVALID_AUTH_CASES)
- def test_auth_invalid(self, invalid_auth, expected_code, expected_message):
- res = plugin_llm_tools(invalid_auth)
- assert res["code"] == expected_code, res
- assert res["message"] == expected_message, res
-
-
-class TestPluginTools:
- @pytest.mark.p1
- def test_llm_tools(self, WebApiAuth):
- res = plugin_llm_tools(WebApiAuth)
- assert res["code"] == 0, res
- assert isinstance(res["data"], list), res
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-def _load_plugin_app(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- stub_apps = ModuleType("api.apps")
- stub_apps.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", stub_apps)
-
- stub_plugin = ModuleType("agent.plugin")
-
- class _StubGlobalPluginManager:
- @staticmethod
- def get_llm_tools():
- return []
-
- stub_plugin.GlobalPluginManager = _StubGlobalPluginManager
- monkeypatch.setitem(sys.modules, "agent.plugin", stub_plugin)
-
- module_path = Path(__file__).resolve().parents[4] / "api" / "apps" / "restful_apis" / "plugin_api.py"
- spec = importlib.util.spec_from_file_location("test_plugin_app_unit", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- spec.loader.exec_module(module)
- return module
-
-
-def test_llm_tools_metadata_shape_unit(monkeypatch):
- module = _load_plugin_app(monkeypatch)
-
- class _DummyTool:
- def get_metadata(self):
- return {"name": "dummy", "description": "test"}
-
- monkeypatch.setattr(module.GlobalPluginManager, "get_llm_tools", staticmethod(lambda: [_DummyTool()]))
- res = module.llm_tools()
- assert res["code"] == 0
- assert isinstance(res["data"], list)
- assert res["data"][0]["name"] == "dummy"
- assert res["data"][0]["description"] == "test"
diff --git a/test/testcases/test_web_api/test_search_app/test_search_crud.py b/test/testcases/test_web_api/test_search_app/test_search_crud.py
deleted file mode 100644
index 84d2bf5bd0..0000000000
--- a/test/testcases/test_web_api/test_search_app/test_search_crud.py
+++ /dev/null
@@ -1,131 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import uuid
-
-import pytest
-from test_common import search_create, search_detail, search_list, search_rm, search_update
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-INVALID_AUTH_CASES = [
- (None, 401, "Unauthorized"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "Unauthorized"),
-]
-
-
-def _search_name(prefix="search"):
- return f"{prefix}_{uuid.uuid4().hex[:8]}"
-
-
-def search_app(WebApiAuth):
- name = _search_name()
- create_res = search_create(WebApiAuth, {"name": name, "description": "test search"})
- assert create_res["code"] == 0, create_res
- search_id = create_res["data"]["search_id"]
- yield search_id
- rm_res = search_rm(WebApiAuth, search_id)
- assert rm_res["code"] == 0, rm_res
- assert rm_res["data"] is True, rm_res
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_create(self, invalid_auth, expected_code, expected_fragment):
- res = search_create(invalid_auth, {"name": "dummy"})
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_list(self, invalid_auth, expected_code, expected_fragment):
- res = search_list(invalid_auth)
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_detail(self, invalid_auth, expected_code, expected_fragment):
- res = search_detail(invalid_auth, "dummy_search_id")
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_update(self, invalid_auth, expected_code, expected_fragment):
- res = search_update(invalid_auth, "dummy", {"name": "dummy", "search_config": {}})
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_rm(self, invalid_auth, expected_code, expected_fragment):
- res = search_rm(invalid_auth, "dummy_search_id")
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
-
-class TestSearchCrud:
- @pytest.mark.p2
- def test_create_and_rm(self, WebApiAuth):
- name = _search_name("create")
- create_res = search_create(WebApiAuth, {"name": name, "description": "test search"})
- assert create_res["code"] == 0, create_res
- search_id = create_res["data"]["search_id"]
-
- rm_res = search_rm(WebApiAuth, search_id)
- assert rm_res["code"] == 0, rm_res
- assert rm_res["data"] is True, rm_res
-
- @pytest.mark.p2
- def test_list(self, WebApiAuth, search_app):
- res = search_list(WebApiAuth)
- assert res["code"] == 0, res
- assert any(app.get("id") == search_app for app in res["data"]["search_apps"]), res
-
- @pytest.mark.p2
- def test_detail(self, WebApiAuth, search_app):
- res = search_detail(WebApiAuth, search_app)
- assert res["code"] == 0, res
- assert res["data"].get("id") == search_app, res
-
- @pytest.mark.p2
- def test_update(self, WebApiAuth, search_app):
- new_name = _search_name("updated")
- res = search_update(WebApiAuth, search_app, {"name": new_name, "search_config": {"top_k": 3}})
- assert res["code"] == 0, res
- assert res["data"].get("name") == new_name, res
-
- @pytest.mark.p3
- def test_create_invalid_name(self, WebApiAuth):
- res = search_create(WebApiAuth, {"name": ""})
- assert res["code"] == 102, res
- assert "empty" in res["message"], res
-
- @pytest.mark.p3
- def test_update_invalid_search_id(self, WebApiAuth):
- create_res = search_create(WebApiAuth, {"name": _search_name("invalid"), "description": "test search"})
- assert create_res["code"] == 0, create_res
- search_id = create_res["data"]["search_id"]
- try:
- res = search_update(WebApiAuth, "invalid_search_id", {"name": "invalid", "search_config": {}})
- assert res["code"] == 109, res
- assert "No authorization" in res["message"], res
- finally:
- rm_res = search_rm(WebApiAuth, search_id)
- assert rm_res["code"] == 0, rm_res
diff --git a/test/testcases/test_web_api/test_search_app/test_search_routes_unit.py b/test/testcases/test_web_api/test_search_app/test_search_routes_unit.py
deleted file mode 100644
index 3de9f3c156..0000000000
--- a/test/testcases/test_web_api/test_search_app/test_search_routes_unit.py
+++ /dev/null
@@ -1,544 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-from copy import deepcopy
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _DummyAtomic:
- def __enter__(self):
- return self
-
- def __exit__(self, _exc_type, _exc, _tb):
- return False
-
-
-class _StubResponse:
- def __init__(self, data=None, mimetype=None):
- self.data = data
- self.mimetype = mimetype
- self.headers = {}
-
-
-class _Args(dict):
- def get(self, key, default=None):
- return super().get(key, default)
-
- def getlist(self, key):
- val = self.get(key)
- if val is None:
- return []
- if isinstance(val, list):
- return val
- return [val]
-
-
-class _EnumValue:
- def __init__(self, value):
- self.value = value
-
-
-class _DummyStatusEnum:
- VALID = _EnumValue("1")
-
-
-class _DummyRetCode:
- SUCCESS = 0
- EXCEPTION_ERROR = 100
- ARGUMENT_ERROR = 101
- DATA_ERROR = 102
- OPERATING_ERROR = 103
- AUTHENTICATION_ERROR = 109
-
-
-class _SearchRecord:
- def __init__(self, search_id="search-1", name="search", search_config=None):
- self.id = search_id
- self.name = name
- self.search_config = {} if search_config is None else dict(search_config)
-
- def to_dict(self):
- return {"id": self.id, "name": self.name, "search_config": dict(self.search_config)}
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _set_request_json(monkeypatch, module, payload):
- async def _request_json():
- return deepcopy(payload)
-
- monkeypatch.setattr(module, "get_request_json", _request_json)
-
-
-def _set_request_args(monkeypatch, module, args=None):
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args(args or {})))
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_search_api(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart_mod = ModuleType("quart")
- quart_mod.request = SimpleNamespace(args=_Args())
- quart_mod.Response = _StubResponse
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- misc_utils_mod = ModuleType("common.misc_utils")
- misc_utils_mod.get_uuid = lambda: "search-uuid-1"
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
- common_pkg.misc_utils = misc_utils_mod
-
- constants_mod = ModuleType("common.constants")
- constants_mod.RetCode = _DummyRetCode
- constants_mod.StatusEnum = _DummyStatusEnum
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
- common_pkg.constants = constants_mod
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.current_user = SimpleNamespace(id="tenant-1")
- apps_mod.login_required = lambda func: func
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
- api_pkg.apps = apps_mod
-
- constants_api_mod = ModuleType("api.constants")
- constants_api_mod.DATASET_NAME_LIMIT = 255
- monkeypatch.setitem(sys.modules, "api.constants", constants_api_mod)
-
- db_pkg = ModuleType("api.db")
- db_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db", db_pkg)
- api_pkg.db = db_pkg
-
- db_models_mod = ModuleType("api.db.db_models")
-
- class _DummyDB:
- @staticmethod
- def atomic():
- return _DummyAtomic()
-
- db_models_mod.DB = _DummyDB
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- services_pkg.duplicate_name = lambda _checker, **kwargs: kwargs.get("name", "")
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- search_service_mod = ModuleType("api.db.services.search_service")
-
- class _SearchService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def accessible4deletion(_search_id, _user_id):
- return True
-
- @staticmethod
- def update_by_id(_search_id, _req):
- return True
-
- @staticmethod
- def get_by_id(_search_id):
- return True, _SearchRecord(search_id=_search_id, name="updated")
-
- @staticmethod
- def get_detail(_search_id):
- return {"id": _search_id}
-
- @staticmethod
- def get_by_tenant_ids(_tenants, _user_id, _page_number, _items_per_page, _orderby, _desc, _keywords):
- return [], 0
-
- @staticmethod
- def delete_by_id(_search_id):
- return True
-
- search_service_mod.SearchService = _SearchService
- monkeypatch.setitem(sys.modules, "api.db.services.search_service", search_service_mod)
-
- dialog_service_mod = ModuleType("api.db.services.dialog_service")
-
- async def _async_ask(*_args, **_kwargs):
- if False:
- yield None
-
- dialog_service_mod.async_ask = _async_ask
- monkeypatch.setitem(sys.modules, "api.db.services.dialog_service", dialog_service_mod)
-
- user_service_mod = ModuleType("api.db.services.user_service")
-
- class _TenantService:
- @staticmethod
- def get_by_id(_tenant_id):
- return True, SimpleNamespace(id=_tenant_id)
-
- class _UserTenantService:
- @staticmethod
- def query(**_kwargs):
- return [SimpleNamespace(tenant_id="tenant-1")]
-
- user_service_mod.TenantService = _TenantService
- user_service_mod.UserTenantService = _UserTenantService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- utils_pkg = ModuleType("api.utils")
- utils_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.utils", utils_pkg)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- async def _default_request_json():
- return {}
-
- def _get_data_error_result(code=_DummyRetCode.DATA_ERROR, message="Sorry! Data missing!"):
- return {"code": code, "message": message}
-
- def _get_json_result(code=_DummyRetCode.SUCCESS, message="success", data=None):
- return {"code": code, "message": message, "data": data}
-
- def _server_error_response(error):
- return {"code": _DummyRetCode.EXCEPTION_ERROR, "message": repr(error)}
-
- def _validate_request(*_args, **_kwargs):
- def _decorator(func):
- return func
-
- return _decorator
-
- api_utils_mod.get_request_json = _default_request_json
- api_utils_mod.get_data_error_result = _get_data_error_result
- api_utils_mod.get_json_result = _get_json_result
- api_utils_mod.server_error_response = _server_error_response
- api_utils_mod.validate_request = _validate_request
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
- utils_pkg.api_utils = api_utils_mod
-
- module_name = "test_search_api_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "search_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_create_route_matrix_unit(monkeypatch):
- module = _load_search_api(monkeypatch)
-
- _set_request_json(monkeypatch, module, {"name": 1})
- res = _run(module.create())
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "must be string" in res["message"]
-
- _set_request_json(monkeypatch, module, {"name": " "})
- res = _run(module.create())
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "empty" in res["message"].lower()
-
- _set_request_json(monkeypatch, module, {"name": "a" * 256})
- res = _run(module.create())
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "255" in res["message"]
-
- _set_request_json(monkeypatch, module, {"name": "create-auth-fail"})
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tenant_id: (False, None))
- res = _run(module.create())
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "authorized identity" in res["message"].lower()
-
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tenant_id: (True, SimpleNamespace(id=_tenant_id)))
- monkeypatch.setattr(module, "duplicate_name", lambda _checker, **kwargs: kwargs["name"] + "_dedup")
- _set_request_json(monkeypatch, module, {"name": "create-fail", "description": "d"})
- monkeypatch.setattr(module.SearchService, "save", lambda **_kwargs: False)
- res = _run(module.create())
- assert res["code"] == module.RetCode.DATA_ERROR
-
- _set_request_json(monkeypatch, module, {"name": "create-ok", "description": "d"})
- monkeypatch.setattr(module.SearchService, "save", lambda **_kwargs: True)
- res = _run(module.create())
- assert res["code"] == 0
- assert res["data"]["search_id"] == "search-uuid-1"
-
- def _raise_save(**_kwargs):
- raise RuntimeError("save boom")
-
- monkeypatch.setattr(module.SearchService, "save", _raise_save)
- _set_request_json(monkeypatch, module, {"name": "create-exception", "description": "d"})
- res = _run(module.create())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR
- assert "save boom" in res["message"]
-
-
-def test_update_and_detail_route_matrix_unit(monkeypatch):
- module = _load_search_api(monkeypatch)
-
- # update: name not string
- _set_request_json(monkeypatch, module, {"name": 1, "search_config": {}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "must be string" in res["message"]
-
- # update: empty name
- _set_request_json(monkeypatch, module, {"name": " ", "search_config": {}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "empty" in res["message"].lower()
-
- # update: name too long
- _set_request_json(monkeypatch, module, {"name": "a" * 256, "search_config": {}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "large than" in res["message"]
-
- # update: tenant not found
- _set_request_json(monkeypatch, module, {"name": "ok", "search_config": {}})
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tenant_id: (False, None))
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "authorized identity" in res["message"].lower()
-
- # update: no access
- monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tenant_id: (True, SimpleNamespace(id=_tenant_id)))
- monkeypatch.setattr(module.SearchService, "accessible4deletion", lambda _search_id, _user_id: False)
- _set_request_json(monkeypatch, module, {"name": "ok", "search_config": {}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR
- assert "authorization" in res["message"].lower()
-
- # update: search not found (query returns [None])
- monkeypatch.setattr(module.SearchService, "accessible4deletion", lambda _search_id, _user_id: True)
- monkeypatch.setattr(module.SearchService, "query", lambda **_kwargs: [None])
- _set_request_json(monkeypatch, module, {"name": "ok", "search_config": {}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "cannot find search" in res["message"].lower()
-
- existing = _SearchRecord(search_id="s1", name="old-name", search_config={"existing": 1})
-
- def _query_duplicate(**kwargs):
- if "id" in kwargs:
- return [existing]
- if "name" in kwargs:
- return [SimpleNamespace(id="dup")]
- return []
-
- # update: duplicate name
- monkeypatch.setattr(module.SearchService, "query", _query_duplicate)
- _set_request_json(monkeypatch, module, {"name": "new-name", "search_config": {}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "duplicated" in res["message"].lower()
-
- # update: search_config not a dict
- monkeypatch.setattr(module.SearchService, "query", lambda **_kwargs: [existing])
- _set_request_json(monkeypatch, module, {"name": "old-name", "search_config": []})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "json object" in res["message"].lower()
-
- # update: update_by_id fails, verifies config merge and field exclusion
- captured = {}
-
- def _update_fail(search_id, req):
- captured["search_id"] = search_id
- captured["req"] = dict(req)
- return False
-
- monkeypatch.setattr(module.SearchService, "update_by_id", _update_fail)
- _set_request_json(monkeypatch, module, {"name": "old-name", "search_config": {"top_k": 3}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "failed to update" in res["message"].lower()
- assert captured["search_id"] == "s1"
- assert captured["req"]["search_config"] == {"existing": 1, "top_k": 3}
-
- # update: get_by_id fails after successful update
- monkeypatch.setattr(module.SearchService, "update_by_id", lambda _search_id, _req: True)
- monkeypatch.setattr(module.SearchService, "get_by_id", lambda _search_id: (False, None))
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "failed to fetch" in res["message"].lower()
-
- # update: success
- monkeypatch.setattr(
- module.SearchService,
- "get_by_id",
- lambda _search_id: (True, _SearchRecord(search_id=_search_id, name="old-name", search_config={"existing": 1, "top_k": 3})),
- )
- res = _run(module.update(search_id="s1"))
- assert res["code"] == 0
- assert res["data"]["id"] == "s1"
-
- # update: exception
- def _raise_query(**_kwargs):
- raise RuntimeError("update boom")
-
- monkeypatch.setattr(module.SearchService, "query", _raise_query)
- _set_request_json(monkeypatch, module, {"name": "old-name", "search_config": {"top_k": 3}})
- res = _run(module.update(search_id="s1"))
- assert res["code"] == module.RetCode.EXCEPTION_ERROR
- assert "update boom" in res["message"]
-
- # detail: no permission
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-a")])
- monkeypatch.setattr(module.SearchService, "query", lambda **_kwargs: [])
- res = module.detail(search_id="s1")
- assert res["code"] == module.RetCode.OPERATING_ERROR
- assert "permission" in res["message"].lower()
-
- # detail: search not found
- monkeypatch.setattr(module.SearchService, "query", lambda **_kwargs: [SimpleNamespace(id="s1")])
- monkeypatch.setattr(module.SearchService, "get_detail", lambda _search_id: None)
- res = module.detail(search_id="s1")
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "can't find" in res["message"].lower()
-
- # detail: success
- monkeypatch.setattr(module.SearchService, "get_detail", lambda _search_id: {"id": _search_id, "name": "detail-name"})
- res = module.detail(search_id="s1")
- assert res["code"] == 0
- assert res["data"]["id"] == "s1"
-
- # detail: exception
- def _raise_detail(_search_id):
- raise RuntimeError("detail boom")
-
- monkeypatch.setattr(module.SearchService, "get_detail", _raise_detail)
- res = module.detail(search_id="s1")
- assert res["code"] == module.RetCode.EXCEPTION_ERROR
- assert "detail boom" in res["message"]
-
-
-def test_list_and_delete_route_matrix_unit(monkeypatch):
- module = _load_search_api(monkeypatch)
-
- # list: no owner_ids, with pagination
- _set_request_args(
- monkeypatch,
- module,
- {"keywords": "k", "page": "1", "page_size": "2", "orderby": "create_time", "desc": "false"},
- )
- monkeypatch.setattr(
- module.SearchService,
- "get_by_tenant_ids",
- lambda _tenants, _uid, _page, _size, _orderby, _desc, _keywords: ([{"id": "a", "tenant_id": "tenant-1"}], 1),
- )
- res = module.list_searches()
- assert res["code"] == 0
- assert res["data"]["total"] == 1
- assert res["data"]["search_apps"][0]["id"] == "a"
-
- # list: with owner_ids filter and pagination
- _set_request_args(
- monkeypatch,
- module,
- {"keywords": "k", "page": "1", "page_size": "1", "orderby": "create_time", "desc": "true", "owner_ids": ["tenant-1"]},
- )
- monkeypatch.setattr(
- module.SearchService,
- "get_by_tenant_ids",
- lambda _tenants, _uid, _page, _size, _orderby, _desc, _keywords: (
- [{"id": "x", "tenant_id": "tenant-1"}, {"id": "y", "tenant_id": "tenant-2"}],
- 2,
- ),
- )
- res = module.list_searches()
- assert res["code"] == 0
- assert res["data"]["total"] == 1
- assert len(res["data"]["search_apps"]) == 1
- assert res["data"]["search_apps"][0]["tenant_id"] == "tenant-1"
-
- # list: exception
- def _raise_list(*_args, **_kwargs):
- raise RuntimeError("list boom")
-
- monkeypatch.setattr(module.SearchService, "get_by_tenant_ids", _raise_list)
- _set_request_args(monkeypatch, module, {})
- res = module.list_searches()
- assert res["code"] == module.RetCode.EXCEPTION_ERROR
- assert "list boom" in res["message"]
-
- # delete: no authorization
- monkeypatch.setattr(module.SearchService, "accessible4deletion", lambda _search_id, _user_id: False)
- res = module.delete_search(search_id="search-1")
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR
- assert "authorization" in res["message"].lower()
-
- # delete: delete_by_id fails
- monkeypatch.setattr(module.SearchService, "accessible4deletion", lambda _search_id, _user_id: True)
- monkeypatch.setattr(module.SearchService, "delete_by_id", lambda _search_id: False)
- res = module.delete_search(search_id="search-1")
- assert res["code"] == module.RetCode.DATA_ERROR
- assert "failed to delete" in res["message"].lower()
-
- # delete: success
- monkeypatch.setattr(module.SearchService, "delete_by_id", lambda _search_id: True)
- res = module.delete_search(search_id="search-1")
- assert res["code"] == 0
- assert res["data"] is True
-
- # delete: exception
- def _raise_delete(_search_id):
- raise RuntimeError("rm boom")
-
- monkeypatch.setattr(module.SearchService, "delete_by_id", _raise_delete)
- res = module.delete_search(search_id="search-1")
- assert res["code"] == module.RetCode.EXCEPTION_ERROR
- assert "rm boom" in res["message"]
diff --git a/test/testcases/test_web_api/test_system_app/test_apps_init_unit.py b/test/testcases/test_web_api/test_system_app/test_apps_init_unit.py
deleted file mode 100644
index eb367c2f94..0000000000
--- a/test/testcases/test_web_api/test_system_app/test_apps_init_unit.py
+++ /dev/null
@@ -1,363 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import asyncio
-import importlib.util
-import logging
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-from werkzeug.exceptions import Unauthorized as WerkzeugUnauthorized
-
-
-class _DummyAPIToken:
- @staticmethod
- def query(**_kwargs):
- return []
-
-
-class _DummyUserService:
- @staticmethod
- def query(**_kwargs):
- return []
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _load_apps_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- settings_mod = ModuleType("common.settings")
- settings_mod.SECRET_KEY = "test-secret-key"
- settings_mod.get_secret_key = lambda: "test-secret-key"
- settings_mod.init_settings = lambda: None
- settings_mod.decrypt_database_config = lambda name=None: {}
- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)
- common_pkg.settings = settings_mod
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.APIToken = _DummyAPIToken
- db_models_mod.close_connection = lambda: None
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- services_mod = ModuleType("api.db.services")
- services_mod.UserService = _DummyUserService
- monkeypatch.setitem(sys.modules, "api.db.services", services_mod)
-
- commands_mod = ModuleType("api.utils.commands")
- commands_mod.register_commands = lambda _app: None
- monkeypatch.setitem(sys.modules, "api.utils.commands", commands_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- def _get_json_result(code=0, message="success", data=None):
- return {"code": code, "message": message, "data": data}
-
- def _server_error_response(error):
- return {"code": 100, "message": repr(error)}
-
- api_utils_mod.get_json_result = _get_json_result
- api_utils_mod.server_error_response = _server_error_response
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- backward_compat_mod = ModuleType("api.apps.backward_compat")
- backward_compat_mod.register_backward_compat_routes = lambda _app: None
- monkeypatch.setitem(sys.modules, "api.apps.backward_compat", backward_compat_mod)
-
- module_name = "test_apps_init_unit_module"
- module_path = repo_root / "api" / "apps" / "__init__.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- monkeypatch.setitem(sys.modules, module_name, module)
-
- monkeypatch.setattr(Path, "glob", lambda self, _pattern: [])
- spec.loader.exec_module(module)
- return module.app, module
-
-
-def test_module_init_and_unauthorized_message_variants(monkeypatch):
- _quart_app, apps_module = _load_apps_module(monkeypatch)
-
- assert apps_module.client_urls_prefix == []
-
- class _BrokenRepr:
- def __repr__(self):
- raise RuntimeError("repr explode")
-
- class _ExactUnauthorizedRepr:
- def __repr__(self):
- return apps_module.UNAUTHORIZED_MESSAGE
-
- class _Unauthorized401Repr:
- def __repr__(self):
- return "Unauthorized 401 from upstream"
-
- class _WithDescription:
- description = "Custom description"
-
- assert apps_module._unauthorized_message(None) == apps_module.UNAUTHORIZED_MESSAGE
- assert apps_module._unauthorized_message(_BrokenRepr()) == apps_module.UNAUTHORIZED_MESSAGE
- assert apps_module._unauthorized_message(_ExactUnauthorizedRepr()) == apps_module.UNAUTHORIZED_MESSAGE
- assert apps_module._unauthorized_message(_Unauthorized401Repr()) == "Unauthorized 401 from upstream"
- assert apps_module._unauthorized_message(_WithDescription()) == "Custom description"
-
-
-def test_load_user_token_edge_cases(monkeypatch):
- quart_app, apps_module = _load_apps_module(monkeypatch)
-
- user_with_empty_token = SimpleNamespace(email="[email protected]", access_token="")
-
- async def _case():
- async with quart_app.test_request_context("/", headers={"Authorization": "token"}):
- monkeypatch.setattr(apps_module.Serializer, "loads", lambda _self, _auth: "")
- assert apps_module._load_user() is None
-
- async with quart_app.test_request_context("/", headers={"Authorization": "token"}):
- monkeypatch.setattr(apps_module.Serializer, "loads", lambda _self, _auth: "short-token")
- assert apps_module._load_user() is None
-
- async with quart_app.test_request_context("/", headers={"Authorization": "token"}):
- monkeypatch.setattr(apps_module.Serializer, "loads", lambda _self, _auth: "a" * 32)
- monkeypatch.setattr(apps_module.UserService, "query", lambda **_kwargs: [user_with_empty_token])
- assert apps_module._load_user() is None
-
- _run(_case())
-
-
-def test_load_user_api_token_fallback_and_fallback_exception(monkeypatch, caplog):
- quart_app, apps_module = _load_apps_module(monkeypatch)
-
- def _raise_decode(_self, _auth):
- raise RuntimeError("decode failed")
-
- monkeypatch.setattr(apps_module.Serializer, "loads", _raise_decode)
-
- fallback_user_empty_token = SimpleNamespace(email="[email protected]", access_token="")
- valid_token = "a" * 32
- beta_user = SimpleNamespace(
- id="tenant-1",
- access_token=valid_token,
- )
-
- async def _case():
- monkeypatch.setattr(apps_module.APIToken, "query", lambda **_kwargs: [SimpleNamespace(tenant_id="tenant-1")])
- monkeypatch.setattr(apps_module.UserService, "query", lambda **_kwargs: [fallback_user_empty_token])
- async with quart_app.test_request_context("/", headers={"Authorization": "Bearer api-token"}):
- assert apps_module._load_user() is None
-
- def _raise_api_token(**_kwargs):
- raise RuntimeError("api token fallback failed")
-
- monkeypatch.setattr(apps_module.APIToken, "query", _raise_api_token)
- async with quart_app.test_request_context("/", headers={"Authorization": "Bearer api-token"}):
- with caplog.at_level(logging.WARNING):
- assert apps_module._load_user() is None
-
- def _query_api_token(**kwargs):
- if kwargs.get("beta") == "embed-beta":
- return [SimpleNamespace(tenant_id="tenant-1")]
- return []
-
- def _query_user(**kwargs):
- if kwargs.get("id") == "tenant-1" and kwargs.get("status") == apps_module.StatusEnum.VALID.value:
- return [beta_user]
- return []
-
- monkeypatch.setattr(apps_module.APIToken, "query", _query_api_token)
- monkeypatch.setattr(apps_module.UserService, "query", _query_user)
- async with quart_app.test_request_context("/", headers={"Authorization": "Bearer embed-beta"}):
- user = apps_module._load_user(auth_types=[apps_module.AUTH_BETA])
- assert user is beta_user
- assert apps_module.g.auth_type == apps_module.AUTH_BETA
-
- async with quart_app.test_request_context("/", headers={"Authorization": "Bearer invalid-beta"}):
- assert apps_module._load_user(auth_types=[apps_module.AUTH_BETA]) is None
-
- _run(_case())
- assert "api token fallback failed" in caplog.text
-
-
-def test_load_user_session_fallback(monkeypatch, caplog):
- quart_app, apps_module = _load_apps_module(monkeypatch)
-
- valid_token = "a" * 32
- valid_user = SimpleNamespace(id="user-1", email="[email protected]", access_token=valid_token)
- invalid_token_user = SimpleNamespace(id="user-1", email="[email protected]", access_token="INVALID_deadbeef")
- short_token_user = SimpleNamespace(id="user-1", email="[email protected]", access_token="too-short")
-
- async def _case():
- # No Authorization header but a valid session: helper resolves the user.
- async with quart_app.test_request_context("/"):
- from quart import session
-
- session["_user_id"] = "user-1"
- monkeypatch.setattr(apps_module.UserService, "query", lambda **_kw: [valid_user])
- assert apps_module._load_user() is valid_user
-
- # Malformed bearer header still falls back to session.
- async with quart_app.test_request_context("/", headers={"Authorization": "Bearer"}):
- from quart import session
-
- session["_user_id"] = "user-1"
- monkeypatch.setattr(apps_module.UserService, "query", lambda **_kw: [valid_user])
- assert apps_module._load_user() is valid_user
-
- # Logout-revoked tokens (INVALID_ prefix) are rejected even with a session.
- async with quart_app.test_request_context("/"):
- from quart import session
-
- session["_user_id"] = "user-1"
- monkeypatch.setattr(apps_module.UserService, "query", lambda **_kw: [invalid_token_user])
- assert apps_module._load_user() is None
-
- # Short tokens are rejected (matches the JWT-path length floor).
- async with quart_app.test_request_context("/"):
- from quart import session
-
- session["_user_id"] = "user-1"
- monkeypatch.setattr(apps_module.UserService, "query", lambda **_kw: [short_token_user])
- assert apps_module._load_user() is None
-
- # No session and no header → still None.
- async with quart_app.test_request_context("/"):
- assert apps_module._load_user() is None
-
- # Database errors during the session lookup are swallowed and logged.
- async with quart_app.test_request_context("/"):
- from quart import session
-
- session["_user_id"] = "user-1"
-
- def _raise(**_kw):
- raise RuntimeError("db down")
-
- monkeypatch.setattr(apps_module.UserService, "query", _raise)
- with caplog.at_level(logging.ERROR):
- assert apps_module._load_user() is None
-
- _run(_case())
- assert "load_user from session failed" in caplog.text
-
-
-def test_load_user_session_fallback_after_token_paths_fail(monkeypatch):
- """JWT-decode failures and API-token exhaustion must still fall through
- to the session and return the user, not None."""
- quart_app, apps_module = _load_apps_module(monkeypatch)
-
- valid_token = "b" * 32
- valid_user = SimpleNamespace(id="user-1", email="[email protected]", access_token=valid_token)
-
- def _raise_decode(_self, _auth):
- raise RuntimeError("jwt decode boom")
-
- monkeypatch.setattr(apps_module.Serializer, "loads", _raise_decode)
- monkeypatch.setattr(apps_module.APIToken, "query", lambda **_kw: [])
-
- async def _case():
- # JWT decode fails AND API-token query returns nothing → session wins.
- async with quart_app.test_request_context("/", headers={"Authorization": "Bearer junk"}):
- from quart import session
-
- session["_user_id"] = "user-1"
- monkeypatch.setattr(apps_module.UserService, "query", lambda **_kw: [valid_user])
- assert apps_module._load_user() is valid_user
-
- _run(_case())
-
-
-def test_login_required_timing_and_login_user_inactive(monkeypatch, caplog):
- quart_app, apps_module = _load_apps_module(monkeypatch)
-
- monkeypatch.setenv("RAGFLOW_API_TIMING", "1")
- monkeypatch.setattr(apps_module, "current_user", SimpleNamespace(id="tenant-1"))
-
- @apps_module.login_required
- async def _timed_handler():
- return {"ok": True}
-
- async def _case():
- async with quart_app.test_request_context("/timed"):
- with caplog.at_level(logging.INFO):
- assert await _timed_handler() == {"ok": True}
-
- inactive_user = SimpleNamespace(id="user-1", is_active=False)
- assert apps_module.login_user(inactive_user) is False
-
- _run(_case())
- assert "api_timing login_required" in caplog.text
-
-
-def test_logout_user_not_found_and_unauthorized_handlers(monkeypatch):
- quart_app, apps_module = _load_apps_module(monkeypatch)
-
- async def _case():
- async with quart_app.test_request_context("/logout", headers={"Cookie": "remember_token=abc"}):
- from quart import session
-
- session["_user_id"] = "user-1"
- session["_fresh"] = True
- session["_id"] = "session-id"
- session["_remember_seconds"] = 5
-
- assert apps_module.logout_user() is True
- assert "_user_id" not in session
- assert "_fresh" not in session
- assert "_id" not in session
- assert session.get("_remember") == "clear"
- assert "_remember_seconds" not in session
-
- async with quart_app.test_request_context("/missing/path"):
- not_found_resp, status = await apps_module.not_found(RuntimeError("missing"))
- assert status == apps_module.RetCode.NOT_FOUND
- payload = await not_found_resp.get_json()
- assert payload["code"] == apps_module.RetCode.NOT_FOUND
- assert payload["error"] == "Not Found"
- assert "Not Found:" in payload["message"]
-
- async with quart_app.test_request_context("/protected"):
-
- @apps_module.login_required
- async def _protected():
- return {"ok": True}
-
- monkeypatch.setattr(apps_module, "current_user", None)
- with pytest.raises(apps_module.QuartAuthUnauthorized) as exc_info:
- await _protected()
-
- quart_payload, quart_status = await apps_module.unauthorized_quart_auth(exc_info.value)
- assert quart_status == apps_module.RetCode.UNAUTHORIZED
- assert quart_payload["code"] == apps_module.RetCode.UNAUTHORIZED
-
- werk_payload, werk_status = await apps_module.unauthorized_werkzeug(WerkzeugUnauthorized("Unauthorized 401"))
- assert werk_status == apps_module.RetCode.UNAUTHORIZED
- assert werk_payload["code"] == apps_module.RetCode.UNAUTHORIZED
-
- _run(_case())
diff --git a/test/testcases/test_web_api/test_system_app/test_system_basic.py b/test/testcases/test_web_api/test_system_app/test_system_basic.py
deleted file mode 100644
index f9443ec23f..0000000000
--- a/test/testcases/test_web_api/test_system_app/test_system_basic.py
+++ /dev/null
@@ -1,101 +0,0 @@
-#
-# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-import pytest
-from test_common import (
- system_config,
- system_delete_token,
- system_new_token,
- system_status,
- system_token_list,
- system_version,
-)
-from configs import INVALID_API_TOKEN
-from libs.auth import RAGFlowWebApiAuth
-
-
-INVALID_AUTH_CASES = [
- (None, 401, "Unauthorized"),
- (RAGFlowWebApiAuth(INVALID_API_TOKEN), 401, "Unauthorized"),
-]
-
-
-class TestAuthorization:
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_status(self, invalid_auth, expected_code, expected_fragment):
- res = system_status(invalid_auth)
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_token_list(self, invalid_auth, expected_code, expected_fragment):
- res = system_token_list(invalid_auth)
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth, expected_code, expected_fragment", INVALID_AUTH_CASES)
- def test_auth_invalid_delete_token(self, invalid_auth, expected_code, expected_fragment):
- res = system_delete_token(invalid_auth, "dummy_token")
- assert res["code"] == expected_code, res
- assert expected_fragment in res["message"], res
-
-
-class TestSystemConfig:
- @pytest.mark.p2
- @pytest.mark.parametrize("invalid_auth", [None, RAGFlowWebApiAuth(INVALID_API_TOKEN)])
- def test_config_no_auth_required(self, invalid_auth):
- res = system_config(invalid_auth)
- assert res["code"] == 0, res
- assert "registerEnabled" in res["data"], res
-
-
-class TestSystemEndpoints:
- @pytest.mark.p2
- def test_status(self, WebApiAuth):
- res = system_status(WebApiAuth)
- assert res["code"] == 0, res
- for key in ["doc_engine", "storage", "database", "redis"]:
- assert key in res["data"], res
-
- @pytest.mark.p2
- def test_version(self, WebApiAuth):
- res = system_version(WebApiAuth)
- assert res["code"] == 0, res
- assert res["data"], res
-
- @pytest.mark.p2
- def test_token_list(self, WebApiAuth):
- res = system_token_list(WebApiAuth)
- assert res["code"] == 0, res
- assert isinstance(res["data"], list), res
-
- @pytest.mark.p2
- def test_delete_token(self, WebApiAuth):
- create_res = system_new_token(WebApiAuth)
- assert create_res["code"] == 0, create_res
- token = create_res["data"]["token"]
-
- delete_res = system_delete_token(WebApiAuth, token)
- assert delete_res["code"] == 0, delete_res
- assert delete_res["data"] is True, delete_res
-
- @pytest.mark.p3
- def test_delete_missing_token(self, WebApiAuth):
- res = system_delete_token(WebApiAuth, "missing_token")
- assert res["code"] == 0, res
- assert res["data"] is True, res
diff --git a/test/testcases/test_web_api/test_system_app/test_system_routes_unit.py b/test/testcases/test_web_api/test_system_app/test_system_routes_unit.py
deleted file mode 100644
index 4793a97234..0000000000
--- a/test/testcases/test_web_api/test_system_app/test_system_routes_unit.py
+++ /dev/null
@@ -1,234 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _ExprField:
- def __init__(self, name):
- self.name = name
-
- def __eq__(self, other):
- return (self.name, other)
-
-
-class _DummyAPITokenModel:
- tenant_id = _ExprField("tenant_id")
- token = _ExprField("token")
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_system_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.login_required = lambda fn: fn
- apps_mod.current_user = SimpleNamespace(id="user-1")
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- settings_mod = ModuleType("common.settings")
- settings_mod.docStoreConn = SimpleNamespace(health=lambda: {"type": "doc", "status": "green"})
- settings_mod.STORAGE_IMPL = SimpleNamespace(health=lambda: True)
- settings_mod.STORAGE_IMPL_TYPE = "MINIO"
- settings_mod.DATABASE_TYPE = "MYSQL"
- settings_mod.REGISTER_ENABLED = True
- settings_mod.DISABLE_PASSWORD_LOGIN = False
- common_pkg.settings = settings_mod
- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)
-
- versions_mod = ModuleType("common.versions")
- versions_mod.get_ragflow_version = lambda: "0.0.0-unit"
- monkeypatch.setitem(sys.modules, "common.versions", versions_mod)
-
- time_utils_mod = ModuleType("common.time_utils")
- time_utils_mod.current_timestamp = lambda: 111
- time_utils_mod.datetime_format = lambda _dt: "2026-01-01 00:00:00"
- monkeypatch.setitem(sys.modules, "common.time_utils", time_utils_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.get_json_result = lambda data=None, message="success", code=0: {
- "code": code,
- "message": message,
- "data": data,
- }
- api_utils_mod.get_data_error_result = lambda message="", code=102, data=None: {
- "code": code,
- "message": message,
- "data": data,
- }
- api_utils_mod.server_error_response = lambda exc: {
- "code": 100,
- "message": repr(exc),
- "data": None,
- }
- api_utils_mod.generate_confirmation_token = lambda: "ragflow-abcdefghijklmnopqrstuvwxyz0123456789"
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- api_service_mod = ModuleType("api.db.services.api_service")
- api_service_mod.APITokenService = SimpleNamespace(
- save=lambda **_kwargs: True,
- query=lambda **_kwargs: [],
- filter_update=lambda *_args, **_kwargs: True,
- filter_delete=lambda *_args, **_kwargs: True,
- )
- monkeypatch.setitem(sys.modules, "api.db.services.api_service", api_service_mod)
-
- kb_service_mod = ModuleType("api.db.services.knowledgebase_service")
- kb_service_mod.KnowledgebaseService = SimpleNamespace(get_by_id=lambda _kb_id: True)
- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_service_mod)
-
- user_service_mod = ModuleType("api.db.services.user_service")
- user_service_mod.UserTenantService = SimpleNamespace(query=lambda **_kwargs: [SimpleNamespace(role="owner", tenant_id="tenant-1")])
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.APIToken = _DummyAPITokenModel
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
-
- rag_utils_pkg = ModuleType("rag.utils")
- rag_utils_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "rag.utils", rag_utils_pkg)
-
- redis_mod = ModuleType("rag.utils.redis_conn")
- redis_mod.REDIS_CONN = SimpleNamespace(
- health=lambda: True,
- smembers=lambda *_args, **_kwargs: set(),
- zrangebyscore=lambda *_args, **_kwargs: [],
- )
- monkeypatch.setitem(sys.modules, "rag.utils.redis_conn", redis_mod)
-
- health_utils_mod = ModuleType("api.utils.health_utils")
- health_utils_mod.run_health_checks = lambda: ({"status": "ok"}, True)
- health_utils_mod.get_oceanbase_status = lambda: {"status": "alive"}
- health_utils_mod.get_gaussdb_status = lambda: {"status": "alive"}
- monkeypatch.setitem(sys.modules, "api.utils.health_utils", health_utils_mod)
-
- quart_mod = ModuleType("quart")
- quart_mod.jsonify = lambda payload: payload
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- module_path = repo_root / "api" / "apps" / "restful_apis" / "system_api.py"
- spec = importlib.util.spec_from_file_location("test_system_routes_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, "test_system_routes_unit_module", module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_status_branch_matrix_unit(monkeypatch):
- module = _load_system_module(monkeypatch)
-
- monkeypatch.setattr(module.settings, "docStoreConn", SimpleNamespace(health=lambda: {"type": "es", "status": "green"}))
- monkeypatch.setattr(module.settings, "STORAGE_IMPL", SimpleNamespace(health=lambda: True))
- monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: True)
- monkeypatch.setattr(module.REDIS_CONN, "health", lambda: True)
- monkeypatch.setattr(module.REDIS_CONN, "smembers", lambda _key: {"executor-1"})
- monkeypatch.setattr(module.REDIS_CONN, "zrangebyscore", lambda *_args, **_kwargs: ['{"beat": 1}'])
-
- res = module.status()
- assert res["code"] == 0
- assert res["data"]["doc_engine"]["status"] == "green"
- assert res["data"]["storage"]["status"] == "green"
- assert res["data"]["database"]["status"] == "green"
- assert res["data"]["redis"]["status"] == "green"
- assert res["data"]["task_executor_heartbeats"]["executor-1"][0]["beat"] == 1
-
- monkeypatch.setattr(
- module.settings,
- "docStoreConn",
- SimpleNamespace(health=lambda: (_ for _ in ()).throw(RuntimeError("doc down"))),
- )
- monkeypatch.setattr(
- module.settings,
- "STORAGE_IMPL",
- SimpleNamespace(health=lambda: (_ for _ in ()).throw(RuntimeError("storage down"))),
- )
- monkeypatch.setattr(
- module.KnowledgebaseService,
- "get_by_id",
- lambda _kb_id: (_ for _ in ()).throw(RuntimeError("db down")),
- )
- monkeypatch.setattr(module.REDIS_CONN, "health", lambda: False)
- monkeypatch.setattr(module.REDIS_CONN, "smembers", lambda _key: (_ for _ in ()).throw(RuntimeError("hb down")))
-
- res = module.status()
- assert res["code"] == 0
- assert res["data"]["doc_engine"]["status"] == "red"
- assert "doc down" in res["data"]["doc_engine"]["error"]
- assert res["data"]["storage"]["status"] == "red"
- assert "storage down" in res["data"]["storage"]["error"]
- assert res["data"]["database"]["status"] == "red"
- assert "db down" in res["data"]["database"]["error"]
- assert res["data"]["redis"]["status"] == "red"
- assert "Lost connection!" in res["data"]["redis"]["error"]
- assert res["data"]["task_executor_heartbeats"] == {}
-
-
-def test_get_config_returns_register_enabled_unit(monkeypatch):
- module = _load_system_module(monkeypatch)
- monkeypatch.setattr(module.settings, "REGISTER_ENABLED", False)
- res = module.get_config()
- assert res["code"] == 0
- assert res["data"]["registerEnabled"] is False
-
-
-def test_gaussdb_status_route_unit(monkeypatch):
- module = _load_system_module(monkeypatch)
- monkeypatch.setattr(module, "get_gaussdb_status", lambda: {"status": "alive"})
-
- res = module.gaussdb_status()
-
- assert res["code"] == 0
- assert res["data"]["status"] == "alive"
diff --git a/test/testcases/test_web_api/test_user_app/test_tenant_app_unit.py b/test/testcases/test_web_api/test_user_app/test_tenant_app_unit.py
deleted file mode 100644
index 1fc6077025..0000000000
--- a/test/testcases/test_web_api/test_user_app/test_tenant_app_unit.py
+++ /dev/null
@@ -1,319 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _ExprField:
- def __init__(self, name):
- self.name = name
-
- def __eq__(self, other):
- return (self.name, other)
-
-
-class _Invitee:
- def __init__(self, user_id="invitee-1", email="[email protected]"):
- self.id = user_id
- self.email = email
-
- def to_dict(self):
- return {
- "id": self.id,
- "avatar": "avatar-url",
- "email": self.email,
- "nickname": "Invitee",
- "password": "ignored",
- }
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _set_request_json(monkeypatch, module, payload):
- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(payload))
-
-
-def _load_tenant_module(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.current_user = SimpleNamespace(id="tenant-1", email="[email protected]")
- apps_mod.login_required = lambda fn: fn
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
-
- db_mod = ModuleType("api.db")
- db_mod.UserTenantRole = SimpleNamespace(NORMAL="normal", OWNER="owner", INVITE="invite")
- monkeypatch.setitem(sys.modules, "api.db", db_mod)
-
- db_models_mod = ModuleType("api.db.db_models")
- db_models_mod.UserTenant = type(
- "UserTenant",
- (),
- {
- "tenant_id": _ExprField("tenant_id"),
- "user_id": _ExprField("user_id"),
- },
- )
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- user_service_mod = ModuleType("api.db.services.user_service")
-
- class _UserTenantService:
- @staticmethod
- def get_by_tenant_id(_tenant_id):
- return []
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def filter_delete(_conditions):
- return True
-
- @staticmethod
- def get_tenants_by_user_id(_user_id):
- return []
-
- @staticmethod
- def filter_update(_conditions, _payload):
- return True
-
- class _UserService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def get_by_id(_user_id):
- return False, None
-
- user_service_mod.UserTenantService = _UserTenantService
- user_service_mod.UserService = _UserService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
- api_utils_mod.get_json_result = lambda data=None, message="", code=0: {"code": code, "message": message, "data": data}
- api_utils_mod.get_data_error_result = lambda message="": {"code": 102, "message": message, "data": False}
- api_utils_mod.server_error_response = lambda exc: {"code": 100, "message": repr(exc), "data": False}
- api_utils_mod.validate_request = lambda *_args, **_kwargs: lambda fn: fn
- api_utils_mod.get_request_json = lambda: _AwaitableValue({})
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- web_utils_mod = ModuleType("api.utils.web_utils")
- web_utils_mod.send_invite_email = lambda **_kwargs: {"ok": True}
- monkeypatch.setitem(sys.modules, "api.utils.web_utils", web_utils_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- constants_mod = ModuleType("common.constants")
- constants_mod.RetCode = SimpleNamespace(AUTHENTICATION_ERROR=401, SERVER_ERROR=500, DATA_ERROR=102)
- constants_mod.StatusEnum = SimpleNamespace(VALID=SimpleNamespace(value=1))
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- misc_utils_mod = ModuleType("common.misc_utils")
- misc_utils_mod.get_uuid = lambda: "uuid-1"
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
-
- time_utils_mod = ModuleType("common.time_utils")
- time_utils_mod.delta_seconds = lambda _value: 0
- monkeypatch.setitem(sys.modules, "common.time_utils", time_utils_mod)
-
- settings_mod = ModuleType("common.settings")
- settings_mod.MAIL_FRONTEND_URL = "https://frontend.example/invite"
- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)
- common_pkg.settings = settings_mod
-
- sys.modules.pop("test_tenant_app_unit_module", None)
- module_path = repo_root / "api" / "apps" / "restful_apis" / "tenant_api.py"
- spec = importlib.util.spec_from_file_location("test_tenant_app_unit_module", module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, "test_tenant_app_unit_module", module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_user_list_auth_success_exception_matrix_unit(monkeypatch):
- module = _load_tenant_module(monkeypatch)
-
- module.current_user.id = "other-user"
- res = module.user_list("tenant-1")
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
- assert res["message"] == "no authorization", res
-
- module.current_user.id = "tenant-1"
- monkeypatch.setattr(
- module.UserTenantService,
- "get_by_tenant_id",
- lambda _tenant_id: [{"id": "u1", "update_date": "2024-01-01 00:00:00"}],
- )
- monkeypatch.setattr(module, "delta_seconds", lambda _value: 42)
- res = module.user_list("tenant-1")
- assert res["code"] == 0, res
- assert res["data"][0]["delta_seconds"] == 42, res
-
- monkeypatch.setattr(module.UserTenantService, "get_by_tenant_id", lambda _tenant_id: (_ for _ in ()).throw(RuntimeError("list boom")))
- res = module.user_list("tenant-1")
- assert res["code"] == 100, res
- assert "list boom" in res["message"], res
-
-
-def test_create_invite_role_and_email_failure_matrix_unit(monkeypatch):
- module = _load_tenant_module(monkeypatch)
-
- module.current_user.id = "other-user"
- _set_request_json(monkeypatch, module, {"email": "[email protected]"})
- res = _run(module.create("tenant-1"))
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
- assert res["message"] == "no authorization", res
-
- module.current_user.id = "tenant-1"
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [])
- res = _run(module.create("tenant-1"))
- assert res["message"] == "User not found.", res
-
- invitee = _Invitee()
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [invitee])
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(role=module.UserTenantRole.NORMAL)])
- res = _run(module.create("tenant-1"))
- assert "already in the team." in res["message"], res
-
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(role=module.UserTenantRole.OWNER)])
- res = _run(module.create("tenant-1"))
- assert "owner of the team." in res["message"], res
-
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(role="strange-role")])
- res = _run(module.create("tenant-1"))
- assert "role: strange-role is invalid." in res["message"], res
-
- saved = []
- scheduled = []
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module.UserTenantService, "save", lambda **kwargs: saved.append(kwargs) or True)
- monkeypatch.setattr(module.UserService, "get_by_id", lambda _user_id: (True, SimpleNamespace(nickname="Inviter Nick")))
- monkeypatch.setattr(module, "send_invite_email", lambda **kwargs: kwargs)
- monkeypatch.setattr(module.asyncio, "create_task", lambda payload: scheduled.append(payload) or SimpleNamespace())
- res = _run(module.create("tenant-1"))
- assert res["code"] == 0, res
- assert saved and saved[-1]["role"] == module.UserTenantRole.INVITE, saved
- assert scheduled and scheduled[-1]["inviter"] == "Inviter Nick", scheduled
- assert sorted(res["data"].keys()) == ["avatar", "email", "id", "nickname"], res
-
- monkeypatch.setattr(module.asyncio, "create_task", lambda _payload: (_ for _ in ()).throw(RuntimeError("send boom")))
- res = _run(module.create("tenant-1"))
- assert res["code"] == module.RetCode.SERVER_ERROR, res
- assert "Failed to send invite email." in res["message"], res
-
-
-def test_rm_and_tenant_list_matrix_unit(monkeypatch):
- module = _load_tenant_module(monkeypatch)
-
- module.current_user.id = "outsider"
- _set_request_json(monkeypatch, module, {"user_id": "user-2"})
- res = _run(module.rm("tenant-1"))
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
- assert res["message"] == "no authorization", res
-
- module.current_user.id = "tenant-1"
- deleted = []
- monkeypatch.setattr(module.UserTenantService, "filter_delete", lambda conditions: deleted.append(conditions) or True)
- res = _run(module.rm("tenant-1"))
- assert res["code"] == 0, res
- assert res["data"] is True, res
- assert deleted, "filter_delete should be called"
-
- monkeypatch.setattr(module.UserTenantService, "filter_delete", lambda _conditions: (_ for _ in ()).throw(RuntimeError("rm boom")))
- res = _run(module.rm("tenant-1"))
- assert res["code"] == 100, res
- assert "rm boom" in res["message"], res
-
- monkeypatch.setattr(
- module.UserTenantService,
- "get_tenants_by_user_id",
- lambda _user_id: [{"id": "tenant-1", "update_date": "2024-01-01 00:00:00"}],
- )
- monkeypatch.setattr(module, "delta_seconds", lambda _value: 9)
- res = module.tenant_list()
- assert res["code"] == 0, res
- assert res["data"][0]["delta_seconds"] == 9, res
-
- monkeypatch.setattr(module.UserTenantService, "get_tenants_by_user_id", lambda _user_id: (_ for _ in ()).throw(RuntimeError("tenant boom")))
- res = module.tenant_list()
- assert res["code"] == 100, res
- assert "tenant boom" in res["message"], res
-
-
-def test_agree_success_and_exception_unit(monkeypatch):
- module = _load_tenant_module(monkeypatch)
-
- calls = []
- monkeypatch.setattr(module.UserTenantService, "filter_update", lambda conditions, payload: calls.append((conditions, payload)) or True)
- res = module.agree("tenant-1")
- assert res["code"] == 0, res
- assert res["data"] is True, res
- assert calls and calls[-1][1]["role"] == module.UserTenantRole.NORMAL
-
- monkeypatch.setattr(module.UserTenantService, "filter_update", lambda _conditions, _payload: (_ for _ in ()).throw(RuntimeError("agree boom")))
- res = module.agree("tenant-1")
- assert res["code"] == 100, res
- assert "agree boom" in res["message"], res
diff --git a/test/testcases/test_web_api/test_user_app/test_user_app_unit.py b/test/testcases/test_web_api/test_user_app/test_user_app_unit.py
deleted file mode 100644
index d8c8b76a8f..0000000000
--- a/test/testcases/test_web_api/test_user_app/test_user_app_unit.py
+++ /dev/null
@@ -1,1140 +0,0 @@
-#
-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import asyncio
-import base64
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType, SimpleNamespace
-
-import pytest
-
-
-class _DummyManager:
- def route(self, *_args, **_kwargs):
- def decorator(func):
- return func
-
- return decorator
-
-
-class _AwaitableValue:
- def __init__(self, value):
- self._value = value
-
- def __await__(self):
- async def _co():
- return self._value
-
- return _co().__await__()
-
-
-class _Args(dict):
- def get(self, key, default=None, type=None):
- value = super().get(key, default)
- if type is None:
- return value
- try:
- return type(value)
- except (TypeError, ValueError):
- return default
-
-
-class _DummyResponse:
- def __init__(self, data):
- self.data = data
- self.headers = {}
-
-
-class _DummyHTTPResponse:
- def __init__(self, payload):
- self._payload = payload
-
- def json(self):
- return self._payload
-
-
-class _DummyRedis:
- def __init__(self):
- self.store = {}
-
- def get(self, key):
- return self.store.get(key)
-
- def set(self, key, value, _ttl=None):
- self.store[key] = value
-
- def delete(self, key):
- self.store.pop(key, None)
-
-
-class _DummyUser:
- def __init__(self, user_id, email, *, password="stored-password", is_active="1", nickname="nick"):
- self.id = user_id
- self.email = email
- self.password = password
- self.is_active = is_active
- self.nickname = nickname
- self.access_token = ""
- self.save_calls = 0
-
- def save(self):
- self.save_calls += 1
-
- def get_id(self):
- return self.id
-
- def to_json(self):
- return {"id": self.id, "email": self.email, "nickname": self.nickname}
-
- def to_dict(self):
- return {"id": self.id, "email": self.email}
-
-
-class _Field:
- def __init__(self, name):
- self.name = name
-
- def __eq__(self, other):
- return (self.name, other)
-
-
-def _run(coro):
- return asyncio.run(coro)
-
-
-def _set_request_json(monkeypatch, module, payload):
- async def _request_json():
- return payload
-
- monkeypatch.setattr(module, "get_request_json", _request_json)
-
-
-def _set_request_args(monkeypatch, module, args=None):
- monkeypatch.setattr(module, "request", SimpleNamespace(args=_Args(args or {})))
-
-
[email protected](scope="session")
-def auth():
- return "unit-auth"
-
-
[email protected](scope="session", autouse=True)
-def set_tenant_info():
- return None
-
-
-def _load_user_app(monkeypatch):
- repo_root = Path(__file__).resolve().parents[4]
-
- quart_mod = ModuleType("quart")
- quart_mod.session = {}
- quart_mod.request = SimpleNamespace(args=_Args({}), path="/v1/user/setting")
-
- async def _make_response(data):
- return _DummyResponse(data)
-
- quart_mod.make_response = _make_response
- quart_mod.redirect = lambda url: {"redirect": url}
- monkeypatch.setitem(sys.modules, "quart", quart_mod)
-
- api_pkg = ModuleType("api")
- api_pkg.__path__ = [str(repo_root / "api")]
- monkeypatch.setitem(sys.modules, "api", api_pkg)
-
- apps_mod = ModuleType("api.apps")
- apps_mod.__path__ = [str(repo_root / "api" / "apps")]
- apps_mod.current_user = _DummyUser("current-user", "[email protected]")
- apps_mod.login_required = lambda fn: fn
- apps_mod.login_user = lambda _user: True
- apps_mod.logout_user = lambda: True
- monkeypatch.setitem(sys.modules, "api.apps", apps_mod)
- api_pkg.apps = apps_mod
-
- apps_auth_mod = ModuleType("api.apps.auth")
- apps_auth_mod.get_auth_client = lambda _config: SimpleNamespace(get_authorization_url=lambda state: f"https://oauth.example/{state}")
- monkeypatch.setitem(sys.modules, "api.apps.auth", apps_auth_mod)
-
- db_mod = ModuleType("api.db")
- db_mod.FileType = SimpleNamespace(FOLDER=SimpleNamespace(value="folder"))
- db_mod.UserTenantRole = SimpleNamespace(OWNER="owner")
- monkeypatch.setitem(sys.modules, "api.db", db_mod)
- api_pkg.db = db_mod
-
- tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
- tenant_model_service_mod.ensure_tenant_model_ids_for_params = lambda _tenant_id, params: params
- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
-
- db_models_mod = ModuleType("api.db.db_models")
-
- class _DummyTenantLLMModel:
- tenant_id = _Field("tenant_id")
-
- @staticmethod
- def delete():
- class _DeleteQuery:
- def where(self, *_args, **_kwargs):
- return self
-
- def execute(self):
- return 1
-
- return _DeleteQuery()
-
- db_models_mod.TenantLLM = _DummyTenantLLMModel
- monkeypatch.setitem(sys.modules, "api.db.db_models", db_models_mod)
-
- services_pkg = ModuleType("api.db.services")
- services_pkg.__path__ = []
- monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
-
- file_service_mod = ModuleType("api.db.services.file_service")
-
- class _StubFileService:
- @staticmethod
- def insert(_data):
- return True
-
- file_service_mod.FileService = _StubFileService
- monkeypatch.setitem(sys.modules, "api.db.services.file_service", file_service_mod)
-
- llm_service_mod = ModuleType("api.db.services.llm_service")
- llm_service_mod.get_init_tenant_llm = lambda _user_id: []
- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}
- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)
-
- tenant_llm_service_mod = ModuleType("api.db.services.tenant_llm_service")
-
- class _MockTableObject:
- def __init__(self, **kwargs):
- for key, value in kwargs.items():
- setattr(self, key, value)
-
- def to_dict(self):
- return {k: v for k, v in self.__dict__.items()}
-
- class _StubTenantLLMService:
- @staticmethod
- def insert_many(_payload):
- return True
-
- @staticmethod
- def get_api_key(tenant_id, model_name, model_type=None):
- return _MockTableObject(
- id=1, tenant_id=tenant_id, llm_factory="", model_type="chat", llm_name=model_name, api_key="fake-api-key", api_base="https://api.example.com", max_tokens=8192, used_tokens=0, status=1
- )
-
- tenant_llm_service_mod.TenantLLMService = _StubTenantLLMService
- monkeypatch.setitem(sys.modules, "api.db.services.tenant_llm_service", tenant_llm_service_mod)
-
- user_service_mod = ModuleType("api.db.services.user_service")
-
- class _StubTenantService:
- @staticmethod
- def insert(**_kwargs):
- return True
-
- @staticmethod
- def delete_by_id(_tenant_id):
- return True
-
- @staticmethod
- def get_by_id(_tenant_id):
- return True, SimpleNamespace(id=_tenant_id)
-
- @staticmethod
- def get_info_by(_user_id):
- return []
-
- @staticmethod
- def update_by_id(_tenant_id, _payload):
- return True
-
- class _StubUserService:
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def query_user(_email, _password):
- return None
-
- @staticmethod
- def query_user_by_email(**_kwargs):
- return []
-
- @staticmethod
- def save(**_kwargs):
- return True
-
- @staticmethod
- def delete_by_id(_user_id):
- return True
-
- @staticmethod
- def update_by_id(_user_id, _payload):
- return True
-
- @staticmethod
- def update_user_password(_user_id, _new_password):
- return True
-
- class _StubUserTenantService:
- @staticmethod
- def insert(**_kwargs):
- return True
-
- @staticmethod
- def query(**_kwargs):
- return []
-
- @staticmethod
- def delete_by_id(_user_tenant_id):
- return True
-
- user_service_mod.TenantService = _StubTenantService
- user_service_mod.UserService = _StubUserService
- user_service_mod.UserTenantService = _StubUserTenantService
- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)
-
- api_utils_mod = ModuleType("api.utils.api_utils")
-
- async def _default_request_json():
- return {}
-
- def _get_json_result(code=0, message="success", data=None):
- return {"code": code, "message": message, "data": data}
-
- def _get_data_error_result(code=102, message="Sorry! Data missing!", data=None):
- return {"code": code, "message": message, "data": data}
-
- def _server_error_response(error):
- return {"code": 100, "message": repr(error)}
-
- def _validate_request(*_args, **_kwargs):
- def _decorator(func):
- return func
-
- return _decorator
-
- api_utils_mod.get_request_json = _default_request_json
- api_utils_mod.get_json_result = _get_json_result
- api_utils_mod.get_data_error_result = _get_data_error_result
- api_utils_mod.server_error_response = _server_error_response
- api_utils_mod.validate_request = _validate_request
- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
-
- crypt_mod = ModuleType("api.utils.crypt")
- crypt_mod.decrypt = lambda value: value
- monkeypatch.setitem(sys.modules, "api.utils.crypt", crypt_mod)
-
- web_utils_mod = ModuleType("api.utils.web_utils")
- web_utils_mod.send_email_html = lambda *_args, **_kwargs: _AwaitableValue(True)
- web_utils_mod.OTP_LENGTH = 6
- web_utils_mod.OTP_TTL_SECONDS = 600
- web_utils_mod.ATTEMPT_LIMIT = 5
- web_utils_mod.ATTEMPT_LOCK_SECONDS = 600
- web_utils_mod.RESEND_COOLDOWN_SECONDS = 60
- web_utils_mod.otp_keys = lambda email: (
- f"otp:{email}:code",
- f"otp:{email}:attempts",
- f"otp:{email}:last",
- f"otp:{email}:lock",
- )
- web_utils_mod.hash_code = lambda code, _salt: f"hash:{code}"
- web_utils_mod.captcha_key = lambda email: f"captcha:{email}"
- monkeypatch.setitem(sys.modules, "api.utils.web_utils", web_utils_mod)
-
- common_pkg = ModuleType("common")
- common_pkg.__path__ = [str(repo_root / "common")]
- monkeypatch.setitem(sys.modules, "common", common_pkg)
-
- settings_mod = ModuleType("common.settings")
- settings_mod.OAUTH_CONFIG = {
- "github": {"display_name": "GitHub", "icon": "gh"},
- "feishu": {"display_name": "Feishu", "icon": "fs"},
- }
- settings_mod.GITHUB_OAUTH = {"url": "https://github.example/oauth", "client_id": "cid", "secret_key": "sk"}
- settings_mod.FEISHU_OAUTH = {
- "app_access_token_url": "https://feishu.example/app_token",
- "user_access_token_url": "https://feishu.example/user_token",
- "app_id": "app-id",
- "app_secret": "app-secret",
- "grant_type": "authorization_code",
- }
- settings_mod.CHAT_MDL = "chat-mdl"
- settings_mod.EMBEDDING_MDL = "embd-mdl"
- settings_mod.ASR_MDL = "asr-mdl"
- settings_mod.PARSERS = []
- settings_mod.VISION_MDL = "img-mdl"
- settings_mod.RERANK_MDL = "rerank-mdl"
- settings_mod.REGISTER_ENABLED = True
- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)
- common_pkg.settings = settings_mod
-
- constants_mod = ModuleType("common.constants")
- constants_mod.RetCode = SimpleNamespace(
- AUTHENTICATION_ERROR=401,
- SERVER_ERROR=500,
- FORBIDDEN=403,
- EXCEPTION_ERROR=100,
- OPERATING_ERROR=300,
- ARGUMENT_ERROR=101,
- DATA_ERROR=102,
- NOT_EFFECTIVE=103,
- SUCCESS=0,
- )
- monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
-
- connection_utils_mod = ModuleType("common.connection_utils")
-
- async def _construct_response(data=None, auth=None, message=""):
- return {"code": 0, "message": message, "data": data, "auth": auth}
-
- connection_utils_mod.construct_response = _construct_response
- monkeypatch.setitem(sys.modules, "common.connection_utils", connection_utils_mod)
-
- time_utils_mod = ModuleType("common.time_utils")
- time_utils_mod.current_timestamp = lambda: 111
- time_utils_mod.datetime_format = lambda _dt: "2024-01-01 00:00:00"
- time_utils_mod.get_format_time = lambda: "2024-01-01 00:00:00"
- monkeypatch.setitem(sys.modules, "common.time_utils", time_utils_mod)
-
- misc_utils_mod = ModuleType("common.misc_utils")
- misc_utils_mod.download_img = lambda _url: "avatar"
- misc_utils_mod.get_uuid = lambda: "uuid-default"
- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)
-
- http_client_mod = ModuleType("common.http_client")
-
- async def _async_request(_method, _url, **_kwargs):
- return _DummyHTTPResponse({})
-
- http_client_mod.async_request = _async_request
- monkeypatch.setitem(sys.modules, "common.http_client", http_client_mod)
-
- rag_pkg = ModuleType("rag")
- rag_pkg.__path__ = [str(repo_root / "rag")]
- monkeypatch.setitem(sys.modules, "rag", rag_pkg)
-
- rag_utils_pkg = ModuleType("rag.utils")
- rag_utils_pkg.__path__ = [str(repo_root / "rag" / "utils")]
- monkeypatch.setitem(sys.modules, "rag.utils", rag_utils_pkg)
-
- redis_mod = ModuleType("rag.utils.redis_conn")
- redis_mod.REDIS_CONN = _DummyRedis()
- monkeypatch.setitem(sys.modules, "rag.utils.redis_conn", redis_mod)
-
- module_name = "test_user_app_unit_module"
- module_path = repo_root / "api" / "apps" / "restful_apis" / "user_api.py"
- spec = importlib.util.spec_from_file_location(module_name, module_path)
- module = importlib.util.module_from_spec(spec)
- module.manager = _DummyManager()
- monkeypatch.setitem(sys.modules, module_name, module)
- spec.loader.exec_module(module)
- return module
-
-
-def test_login_route_branch_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
-
- _set_request_json(monkeypatch, module, {})
- res = _run(module.login())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR
- assert "Unauthorized" in res["message"]
-
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "password": "enc"})
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [])
- res = _run(module.login())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR
- assert "not registered" in res["message"]
-
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "password": "enc"})
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [SimpleNamespace(email="[email protected]")])
-
- def _raise_decrypt(_value):
- raise RuntimeError("decrypt explode")
-
- monkeypatch.setattr(module, "decrypt", _raise_decrypt)
- res = _run(module.login())
- assert res["code"] == module.RetCode.SERVER_ERROR
- assert "Fail to crypt password" in res["message"]
-
- user_inactive = _DummyUser("u-inactive", "[email protected]", is_active="0")
- monkeypatch.setattr(module, "decrypt", lambda value: value)
- monkeypatch.setattr(module.UserService, "query_user", lambda _email, _password: user_inactive)
- res = _run(module.login())
- assert res["code"] == module.RetCode.FORBIDDEN
- assert "disabled" in res["message"]
-
- monkeypatch.setattr(module.UserService, "query_user", lambda _email, _password: None)
- res = _run(module.login())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR
- assert "do not match" in res["message"]
-
-
-def test_login_channels_and_oauth_login_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
-
- module.settings.OAUTH_CONFIG = {"github": {"display_name": "GitHub", "icon": "gh"}}
- res = _run(module.get_login_channels())
- assert res["code"] == 0
- assert res["data"][0]["channel"] == "github"
-
- class _BrokenOAuthConfig:
- @staticmethod
- def items():
- raise RuntimeError("broken oauth config")
-
- module.settings.OAUTH_CONFIG = _BrokenOAuthConfig()
- res = _run(module.get_login_channels())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR
- assert "Load channels failure" in res["message"]
-
- module.settings.OAUTH_CONFIG = {"github": {"display_name": "GitHub", "icon": "gh"}}
- with pytest.raises(ValueError, match="Invalid channel name: missing"):
- _run(module.oauth_login("missing"))
-
- module.session.clear()
- monkeypatch.setattr(module, "get_uuid", lambda: "state-123")
-
- class _AuthClient:
- @staticmethod
- def get_authorization_url(state):
- return f"https://oauth.example/{state}"
-
- monkeypatch.setattr(module, "get_auth_client", lambda _config: _AuthClient())
- res = _run(module.oauth_login("github"))
- assert res["redirect"] == "https://oauth.example/state-123"
- assert module.session["oauth_state"] == "state-123"
-
-
-def test_oauth_callback_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
- module.settings.OAUTH_CONFIG = {"github": {"display_name": "GitHub", "icon": "gh"}}
-
- class _SyncAuthClient:
- def __init__(self, token_info, user_info):
- self._token_info = token_info
- self._user_info = user_info
-
- def exchange_code_for_token(self, _code):
- return self._token_info
-
- def fetch_user_info(self, _token, id_token=None):
- _ = id_token
- return self._user_info
-
- class _AsyncAuthClient:
- def __init__(self, token_info, user_info):
- self._token_info = token_info
- self._user_info = user_info
-
- async def async_exchange_code_for_token(self, _code):
- return self._token_info
-
- async def async_fetch_user_info(self, _token, id_token=None):
- _ = id_token
- return self._user_info
-
- _set_request_args(monkeypatch, module, {"state": "x", "code": "c"})
- module.session.clear()
- res = _run(module.oauth_callback("missing"))
- assert "Invalid channel name: missing" in res["redirect"]
-
- sync_ok = _SyncAuthClient(
- token_info={"access_token": "token-sync", "id_token": "id-sync"},
- user_info=SimpleNamespace(email="[email protected]", avatar_url="http://img", nickname="sync"),
- )
- monkeypatch.setattr(module, "get_auth_client", lambda _config: sync_ok)
-
- module.session.clear()
- module.session["oauth_state"] = "expected"
- _set_request_args(monkeypatch, module, {"state": "wrong", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?error=invalid_state"
-
- module.session.clear()
- module.session["oauth_state"] = "ok-state"
- _set_request_args(monkeypatch, module, {"state": "ok-state"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?error=missing_code"
-
- sync_missing_token = _SyncAuthClient(
- token_info={"id_token": "id-only"},
- user_info=SimpleNamespace(email="[email protected]", avatar_url="http://img", nickname="sync"),
- )
- monkeypatch.setattr(module, "get_auth_client", lambda _config: sync_missing_token)
- module.session.clear()
- module.session["oauth_state"] = "token-state"
- _set_request_args(monkeypatch, module, {"state": "token-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?error=token_failed"
-
- sync_missing_email = _SyncAuthClient(
- token_info={"access_token": "token-sync", "id_token": "id-sync"},
- user_info=SimpleNamespace(email=None, avatar_url="http://img", nickname="sync"),
- )
- monkeypatch.setattr(module, "get_auth_client", lambda _config: sync_missing_email)
- module.session.clear()
- module.session["oauth_state"] = "email-state"
- _set_request_args(monkeypatch, module, {"state": "email-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?error=email_missing"
-
- async_new_user = _AsyncAuthClient(
- token_info={"access_token": "token-async", "id_token": "id-async"},
- user_info=SimpleNamespace(email="[email protected]", avatar_url="http://img", nickname="new-user"),
- )
- monkeypatch.setattr(module, "get_auth_client", lambda _config: async_new_user)
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [])
-
- module.settings.OAUTH_AUTO_REGISTER = False
- register_calls = []
- monkeypatch.setattr(module, "user_register", lambda *args: register_calls.append(args))
- avatar_calls = []
- monkeypatch.setattr(module, "download_img", lambda url: avatar_calls.append(url))
- module.session.clear()
- module.session["oauth_state"] = "blocked-state"
- _set_request_args(monkeypatch, module, {"state": "blocked-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?error=registration_disabled"
- assert not register_calls and not avatar_calls
- module.settings.OAUTH_AUTO_REGISTER = True
-
- def _raise_download(_url):
- raise RuntimeError("download explode")
-
- monkeypatch.setattr(module, "download_img", _raise_download)
- monkeypatch.setattr(module, "user_register", lambda _user_id, _user: None)
- rollback_calls = []
- monkeypatch.setattr(module, "rollback_user_registration", lambda user_id: rollback_calls.append(user_id))
- monkeypatch.setattr(module, "get_uuid", lambda: "new-user-id")
- module.session.clear()
- module.session["oauth_state"] = "new-user-state"
- _set_request_args(monkeypatch, module, {"state": "new-user-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert "Failed to register [email protected]" in res["redirect"]
- assert rollback_calls == ["new-user-id"]
-
- monkeypatch.setattr(module, "download_img", lambda _url: "avatar")
- monkeypatch.setattr(
- module,
- "user_register",
- lambda _user_id, _user: [_DummyUser("dup-1", "[email protected]"), _DummyUser("dup-2", "[email protected]")],
- )
- rollback_calls.clear()
- module.session.clear()
- module.session["oauth_state"] = "dup-user-state"
- _set_request_args(monkeypatch, module, {"state": "dup-user-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert "Same email: [email protected] exists!" in res["redirect"]
- assert rollback_calls == ["new-user-id"]
-
- new_user = _DummyUser("new-user", "[email protected]")
- login_calls = []
- monkeypatch.setattr(module, "login_user", lambda user: login_calls.append(user))
- monkeypatch.setattr(module, "user_register", lambda _user_id, _user: [new_user])
- module.session.clear()
- module.session["oauth_state"] = "create-user-state"
- _set_request_args(monkeypatch, module, {"state": "create-user-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?auth=new-user"
- assert login_calls and login_calls[-1] is new_user
-
- async_existing_inactive = _AsyncAuthClient(
- token_info={"access_token": "token-existing", "id_token": "id-existing"},
- user_info=SimpleNamespace(email="[email protected]", avatar_url="http://img", nickname="existing"),
- )
- monkeypatch.setattr(module, "get_auth_client", lambda _config: async_existing_inactive)
- inactive_user = _DummyUser("existing-user", "[email protected]", is_active="0")
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [inactive_user])
- module.session.clear()
- module.session["oauth_state"] = "inactive-state"
- _set_request_args(monkeypatch, module, {"state": "inactive-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?error=user_inactive"
-
- async_existing_ok = _AsyncAuthClient(
- token_info={"access_token": "token-existing", "id_token": "id-existing"},
- user_info=SimpleNamespace(email="[email protected]", avatar_url="http://img", nickname="existing"),
- )
- monkeypatch.setattr(module, "get_auth_client", lambda _config: async_existing_ok)
- existing_user = _DummyUser("existing-user", "[email protected]")
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [existing_user])
- login_calls.clear()
- monkeypatch.setattr(module, "login_user", lambda user: login_calls.append(user))
- monkeypatch.setattr(module, "get_uuid", lambda: "existing-token")
- module.session.clear()
- module.session["oauth_state"] = "existing-state"
- _set_request_args(monkeypatch, module, {"state": "existing-state", "code": "code"})
- res = _run(module.oauth_callback("github"))
- assert res["redirect"] == "/?auth=existing-user"
- assert existing_user.access_token == "existing-token"
- assert existing_user.save_calls == 1
- assert login_calls and login_calls[-1] is existing_user
-
-
-def test_logout_setting_profile_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
-
- current_user = _DummyUser("current-user", "[email protected]", password="stored-password")
- monkeypatch.setattr(module, "current_user", current_user)
- monkeypatch.setattr(module.secrets, "token_hex", lambda _n: "abcdef")
- logout_calls = []
- monkeypatch.setattr(module, "logout_user", lambda: logout_calls.append(True))
-
- res = _run(module.log_out())
- assert res["code"] == 0
- assert current_user.access_token == "INVALID_abcdef"
- assert current_user.save_calls == 1
- assert logout_calls == [True]
-
- _set_request_json(monkeypatch, module, {"password": "old-password", "new_password": "new-password"})
- monkeypatch.setattr(module, "decrypt", lambda value: value)
- monkeypatch.setattr(module, "check_password_hash", lambda _hashed, _plain: False)
- res = _run(module.setting_user())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR
- assert "Password error" in res["message"]
-
- _set_request_json(monkeypatch, module, {"nickname": "carh!@#$%^&*()_+WFAGD"})
- res = _run(module.setting_user())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR
- assert "invalid characters" in res["message"]
-
- _set_request_json(
- monkeypatch,
- module,
- {
- "password": "old-password",
- "new_password": "new-password",
- "nickname": "neo",
- "email": "[email protected]",
- "status": "disabled",
- "theme": "dark",
- },
- )
- monkeypatch.setattr(module, "check_password_hash", lambda _hashed, _plain: True)
- monkeypatch.setattr(module, "decrypt", lambda value: f"dec:{value}")
- monkeypatch.setattr(module, "generate_password_hash", lambda value: f"hash:{value}")
- update_calls = {}
-
- def _update_by_id(user_id, payload):
- update_calls["user_id"] = user_id
- update_calls["payload"] = payload
- return True
-
- monkeypatch.setattr(module.UserService, "update_by_id", _update_by_id)
- res = _run(module.setting_user())
- assert res["code"] == 0
- assert res["data"] is True
- assert update_calls["user_id"] == "current-user"
- assert update_calls["payload"]["password"] == "hash:dec:new-password"
- assert update_calls["payload"]["nickname"] == "neo"
- assert update_calls["payload"]["theme"] == "dark"
- assert "email" not in update_calls["payload"]
- assert "status" not in update_calls["payload"]
-
- _set_request_json(monkeypatch, module, {"nickname": "neo"})
-
- def _raise_update(_user_id, _payload):
- raise RuntimeError("update explode")
-
- monkeypatch.setattr(module.UserService, "update_by_id", _raise_update)
- res = _run(module.setting_user())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR
- assert "Update failure" in res["message"]
-
- res = _run(module.user_profile())
- assert res["code"] == 0
- assert res["data"] == current_user.to_dict()
-
-
-def test_registration_helpers_and_register_route_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
-
- deleted = {"user": 0, "tenant": 0, "user_tenant": 0, "tenant_llm": 0}
- monkeypatch.setattr(module.UserService, "delete_by_id", lambda _user_id: deleted.__setitem__("user", deleted["user"] + 1))
- monkeypatch.setattr(module.TenantService, "delete_by_id", lambda _tenant_id: deleted.__setitem__("tenant", deleted["tenant"] + 1))
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: [SimpleNamespace(id="ut-1")])
- monkeypatch.setattr(module.UserTenantService, "delete_by_id", lambda _ut_id: deleted.__setitem__("user_tenant", deleted["user_tenant"] + 1))
-
- class _DeleteQuery:
- def where(self, *_args, **_kwargs):
- return self
-
- def execute(self):
- deleted["tenant_llm"] += 1
- return 1
-
- monkeypatch.setattr(module.TenantLLM, "delete", lambda: _DeleteQuery())
- module.rollback_user_registration("user-1")
- assert deleted == {"user": 1, "tenant": 1, "user_tenant": 1, "tenant_llm": 1}, deleted
-
- monkeypatch.setattr(module.UserService, "delete_by_id", lambda _user_id: (_ for _ in ()).throw(RuntimeError("u boom")))
- monkeypatch.setattr(module.TenantService, "delete_by_id", lambda _tenant_id: (_ for _ in ()).throw(RuntimeError("t boom")))
- monkeypatch.setattr(module.UserTenantService, "query", lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("ut boom")))
-
- class _RaisingDeleteQuery:
- def where(self, *_args, **_kwargs):
- raise RuntimeError("llm boom")
-
- monkeypatch.setattr(module.TenantLLM, "delete", lambda: _RaisingDeleteQuery())
- module.rollback_user_registration("user-2")
-
- monkeypatch.setattr(module.UserService, "save", lambda **_kwargs: False)
- res = module.user_register(
- "new-user",
- {
- "nickname": "new",
- "email": "[email protected]",
- "password": "pw",
- "access_token": "tk",
- "login_channel": "password",
- "last_login_time": "2024-01-01 00:00:00",
- "is_superuser": False,
- },
- )
- assert res is None
-
- monkeypatch.setattr(module.settings, "REGISTER_ENABLED", False)
- _set_request_json(monkeypatch, module, {"nickname": "neo", "email": "[email protected]", "password": "enc"})
- res = _run(module.user_add())
- assert res["code"] == module.RetCode.OPERATING_ERROR, res
- assert "disabled" in res["message"], res
-
- monkeypatch.setattr(module.settings, "REGISTER_ENABLED", True)
- _set_request_json(monkeypatch, module, {"nickname": "neo", "email": "bad-email", "password": "enc"})
- res = _run(module.user_add())
- assert res["code"] == module.RetCode.OPERATING_ERROR, res
- assert "Invalid email address" in res["message"], res
-
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [])
- monkeypatch.setattr(module, "decrypt", lambda value: value)
- monkeypatch.setattr(module, "get_uuid", lambda: "new-user-id")
- rollback_calls = []
- monkeypatch.setattr(module, "rollback_user_registration", lambda user_id: rollback_calls.append(user_id))
-
- _set_request_json(monkeypatch, module, {"nickname": "neo", "email": "[email protected]", "password": "enc"})
- monkeypatch.setattr(module, "user_register", lambda _user_id, _payload: None)
- res = _run(module.user_add())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
- assert "Fail to register [email protected]." in res["message"], res
- assert rollback_calls == ["new-user-id"], rollback_calls
-
- rollback_calls.clear()
- monkeypatch.setattr(
- module,
- "user_register",
- lambda _user_id, _payload: [_DummyUser("dup-1", "[email protected]"), _DummyUser("dup-2", "[email protected]")],
- )
- _set_request_json(monkeypatch, module, {"nickname": "neo", "email": "[email protected]", "password": "enc"})
- res = _run(module.user_add())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
- assert "Same email: [email protected] exists!" in res["message"], res
- assert rollback_calls == ["new-user-id"], rollback_calls
-
-
-def test_tenant_info_and_set_tenant_info_exception_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
-
- monkeypatch.setattr(module.TenantService, "get_info_by", lambda _uid: [])
- res = _run(module.tenant_info())
- assert res["code"] == module.RetCode.DATA_ERROR, res
- assert "Tenant not found" in res["message"], res
-
- def _raise_tenant_info(_uid):
- raise RuntimeError("tenant info boom")
-
- monkeypatch.setattr(module.TenantService, "get_info_by", _raise_tenant_info)
- res = _run(module.tenant_info())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
- assert "tenant info boom" in res["message"], res
-
- # IDOR: tenant_id from request body must match the authenticated user
- _set_request_json(
- monkeypatch,
- module,
- {"tenant_id": "other-tenant", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
- )
- res = _run(module.set_tenant_info())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
- assert res["message"] == "No authorization.", res
-
- # Authorized request: tenant_id matches current_user.id ("current-user")
- _set_request_json(
- monkeypatch,
- module,
- {"tenant_id": "current-user", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
- )
-
- def _raise_update(_tenant_id, _payload):
- raise RuntimeError("tenant update boom")
-
- monkeypatch.setattr(module.TenantService, "update_by_id", _raise_update)
- res = _run(module.set_tenant_info())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
- assert "tenant update boom" in res["message"], res
-
-
-def test_forget_captcha_and_send_otp_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
-
- class _Headers(dict):
- def set(self, key, value):
- self[key] = value
-
- async def _make_response(data):
- return SimpleNamespace(data=data, headers=_Headers())
-
- monkeypatch.setattr(module, "make_response", _make_response)
-
- captcha_pkg = ModuleType("captcha")
- captcha_image_mod = ModuleType("captcha.image")
-
- class _ImageCaptcha:
- def __init__(self, **_kwargs):
- pass
-
- def generate(self, text):
- return SimpleNamespace(read=lambda: f"img:{text}".encode())
-
- captcha_image_mod.ImageCaptcha = _ImageCaptcha
- monkeypatch.setitem(sys.modules, "captcha", captcha_pkg)
- monkeypatch.setitem(sys.modules, "captcha.image", captcha_image_mod)
-
- _set_request_args(monkeypatch, module, {"email": ""})
- res = _run(module.forget_get_captcha())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
-
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [])
- _set_request_args(monkeypatch, module, {"email": "[email protected]"})
- res = _run(module.forget_get_captcha())
- assert res["code"] == module.RetCode.DATA_ERROR, res
-
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [_DummyUser("u1", "[email protected]")])
- monkeypatch.setattr(module.secrets, "choice", lambda _allowed: "A")
- _set_request_args(monkeypatch, module, {"email": "[email protected]"})
- res = _run(module.forget_get_captcha())
- assert res.data.startswith(b"img:"), res
- assert res.headers["Content-Type"] == "image/JPEG", res.headers
- assert module.REDIS_CONN.get(module.captcha_key("[email protected]")), module.REDIS_CONN.store
-
- _set_request_json(monkeypatch, module, {"email": "", "captcha": ""})
- res = _run(module.forget_send_otp())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
-
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [])
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "captcha": "AAAA"})
- res = _run(module.forget_send_otp())
- assert res["code"] == module.RetCode.DATA_ERROR, res
-
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [_DummyUser("u1", "[email protected]")])
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "captcha": "AAAA"})
- module.REDIS_CONN.store.pop(module.captcha_key("[email protected]"), None)
- res = _run(module.forget_send_otp())
- assert res["code"] == module.RetCode.NOT_EFFECTIVE, res
-
- module.REDIS_CONN.store[module.captcha_key("[email protected]")] = "ABCD"
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "captcha": "ZZZZ"})
- res = _run(module.forget_send_otp())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
-
- monkeypatch.setattr(module.time, "time", lambda: 1000)
- k_code, k_attempts, k_last, k_lock = module.otp_keys("[email protected]")
- module.REDIS_CONN.store[module.captcha_key("[email protected]")] = "ABCD"
- module.REDIS_CONN.store[k_last] = "990"
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "captcha": "ABCD"})
- res = _run(module.forget_send_otp())
- assert res["code"] == module.RetCode.NOT_EFFECTIVE, res
- assert "wait" in res["message"], res
-
- module.REDIS_CONN.store[module.captcha_key("[email protected]")] = "ABCD"
- module.REDIS_CONN.store[k_last] = "bad-timestamp"
- monkeypatch.setattr(module.secrets, "choice", lambda _allowed: "B")
- monkeypatch.setattr(module.os, "urandom", lambda _n: b"\x00" * 16)
- monkeypatch.setattr(module, "hash_code", lambda code, _salt: f"HASH_{code}")
-
- async def _raise_send_email(*_args, **_kwargs):
- raise RuntimeError("send email boom")
-
- monkeypatch.setattr(module, "send_email_html", _raise_send_email)
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "captcha": "ABCD"})
- res = _run(module.forget_send_otp())
- assert res["code"] == module.RetCode.SERVER_ERROR, res
- assert "failed to send email" in res["message"], res
-
- async def _ok_send_email(*_args, **_kwargs):
- return True
-
- module.REDIS_CONN.store[module.captcha_key("[email protected]")] = "ABCD"
- module.REDIS_CONN.store.pop(k_last, None)
- monkeypatch.setattr(module, "send_email_html", _ok_send_email)
- _set_request_json(monkeypatch, module, {"email": "[email protected]", "captcha": "ABCD"})
- res = _run(module.forget_send_otp())
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["data"] is True, res
- assert module.REDIS_CONN.get(k_code), module.REDIS_CONN.store
- assert module.REDIS_CONN.get(k_attempts) == 0, module.REDIS_CONN.store
- assert module.REDIS_CONN.get(k_lock) is None, module.REDIS_CONN.store
-
-
-def test_forget_verify_otp_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
- email = "[email protected]"
- k_code, k_attempts, k_last, k_lock = module.otp_keys(email)
- salt = b"\x01" * 16
- monkeypatch.setattr(module, "hash_code", lambda code, _salt: f"HASH_{code}")
-
- _set_request_json(monkeypatch, module, {})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
-
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [])
- _set_request_json(monkeypatch, module, {"email": email, "otp": "ABCDEF"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.DATA_ERROR, res
-
- monkeypatch.setattr(module.UserService, "query", lambda **_kwargs: [_DummyUser("u1", email)])
- module.REDIS_CONN.store[k_lock] = "1"
- _set_request_json(monkeypatch, module, {"email": email, "otp": "ABCDEF"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.NOT_EFFECTIVE, res
- module.REDIS_CONN.store.pop(k_lock, None)
-
- module.REDIS_CONN.store.pop(k_code, None)
- _set_request_json(monkeypatch, module, {"email": email, "otp": "ABCDEF"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.NOT_EFFECTIVE, res
-
- module.REDIS_CONN.store[k_code] = "broken"
- _set_request_json(monkeypatch, module, {"email": email, "otp": "ABCDEF"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
-
- module.REDIS_CONN.store[k_code] = f"HASH_CORRECT:{salt.hex()}"
- module.REDIS_CONN.store[k_attempts] = "bad-int"
- _set_request_json(monkeypatch, module, {"email": email, "otp": "wrong"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
- assert module.REDIS_CONN.get(k_attempts) == 1, module.REDIS_CONN.store
-
- module.REDIS_CONN.store[k_code] = f"HASH_CORRECT:{salt.hex()}"
- module.REDIS_CONN.store[k_attempts] = str(module.ATTEMPT_LIMIT - 1)
- _set_request_json(monkeypatch, module, {"email": email, "otp": "wrong"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
- assert module.REDIS_CONN.get(k_lock) is not None, module.REDIS_CONN.store
- module.REDIS_CONN.store.pop(k_lock, None)
-
- module.REDIS_CONN.store[k_code] = f"HASH_ABCDEF:{salt.hex()}"
- module.REDIS_CONN.store[k_attempts] = "0"
- module.REDIS_CONN.store[k_last] = "1000"
-
- def _set_with_verified_fail(key, value, _ttl=None):
- if key == module._verified_key(email):
- raise RuntimeError("verified set boom")
- module.REDIS_CONN.store[key] = value
-
- monkeypatch.setattr(module.REDIS_CONN, "set", _set_with_verified_fail)
- _set_request_json(monkeypatch, module, {"email": email, "otp": "abcdef"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.SERVER_ERROR, res
-
- monkeypatch.setattr(module.REDIS_CONN, "set", lambda key, value, _ttl=None: module.REDIS_CONN.store.__setitem__(key, value))
- module.REDIS_CONN.store[k_code] = f"HASH_ABCDEF:{salt.hex()}"
- module.REDIS_CONN.store[k_attempts] = "0"
- module.REDIS_CONN.store[k_last] = "1000"
- _set_request_json(monkeypatch, module, {"email": email, "otp": "abcdef"})
- res = _run(module.forget_verify_otp())
- assert res["code"] == module.RetCode.SUCCESS, res
- assert module.REDIS_CONN.get(k_code) is None, module.REDIS_CONN.store
- assert module.REDIS_CONN.get(k_attempts) is None, module.REDIS_CONN.store
- assert module.REDIS_CONN.get(k_last) is None, module.REDIS_CONN.store
- assert module.REDIS_CONN.get(k_lock) is None, module.REDIS_CONN.store
- assert module.REDIS_CONN.get(module._verified_key(email)) == "1", module.REDIS_CONN.store
-
-
-def test_forget_reset_password_matrix_unit(monkeypatch):
- module = _load_user_app(monkeypatch)
- email = "[email protected]"
- v_key = module._verified_key(email)
- user = _DummyUser("u-reset", email, nickname="reset-user")
- pwd_a = base64.b64encode(b"new-password").decode()
- pwd_b = base64.b64encode(b"confirm-password").decode()
- pwd_same = base64.b64encode(b"same-password").decode()
- monkeypatch.setattr(module, "decrypt", lambda value: value)
-
- _set_request_json(monkeypatch, module, {"email": email, "new_password": pwd_same, "confirm_new_password": pwd_same})
- module.REDIS_CONN.store.pop(v_key, None)
- res = _run(module.forget_reset_password())
- assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
-
- module.REDIS_CONN.store[v_key] = "1"
- monkeypatch.setattr(module, "decrypt", lambda _value: "")
- _set_request_json(monkeypatch, module, {"email": email, "new_password": "", "confirm_new_password": ""})
- res = _run(module.forget_reset_password())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
-
- monkeypatch.setattr(module, "decrypt", lambda value: value)
- module.REDIS_CONN.store[v_key] = "1"
- _set_request_json(monkeypatch, module, {"email": email, "new_password": pwd_a, "confirm_new_password": pwd_b})
- res = _run(module.forget_reset_password())
- assert res["code"] == module.RetCode.ARGUMENT_ERROR, res
- assert "do not match" in res["message"], res
-
- module.REDIS_CONN.store[v_key] = "1"
- monkeypatch.setattr(module.UserService, "query_user_by_email", lambda **_kwargs: [])
- _set_request_json(monkeypatch, module, {"email": email, "new_password": pwd_same, "confirm_new_password": pwd_same})
- res = _run(module.forget_reset_password())
- assert res["code"] == module.RetCode.DATA_ERROR, res
-
- module.REDIS_CONN.store[v_key] = "1"
- monkeypatch.setattr(module.UserService, "query_user_by_email", lambda **_kwargs: [user])
-
- def _raise_update_password(_user_id, _new_pwd):
- raise RuntimeError("reset boom")
-
- monkeypatch.setattr(module.UserService, "update_user_password", _raise_update_password)
- _set_request_json(monkeypatch, module, {"email": email, "new_password": pwd_same, "confirm_new_password": pwd_same})
- res = _run(module.forget_reset_password())
- assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
-
- module.REDIS_CONN.store[v_key] = "1"
- monkeypatch.setattr(module.UserService, "update_user_password", lambda _user_id, _new_pwd: True)
- monkeypatch.setattr(module.REDIS_CONN, "delete", lambda _key: (_ for _ in ()).throw(RuntimeError("delete boom")))
- _set_request_json(monkeypatch, module, {"email": email, "new_password": pwd_same, "confirm_new_password": pwd_same})
- res = _run(module.forget_reset_password())
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["auth"] == user.get_id(), res
-
- monkeypatch.setattr(module.REDIS_CONN, "delete", lambda key: module.REDIS_CONN.store.pop(key, None))
- module.REDIS_CONN.store[v_key] = "1"
- _set_request_json(monkeypatch, module, {"email": email, "new_password": pwd_same, "confirm_new_password": pwd_same})
- res = _run(module.forget_reset_password())
- assert res["code"] == module.RetCode.SUCCESS, res
- assert res["auth"] == user.get_id(), res
- assert module.REDIS_CONN.get(v_key) is None, module.REDIS_CONN.store