test_update_chat_assistant.py
Overview
This file contains a comprehensive suite of automated tests for the update_chat_assistant API function within the InfiniFlow project. The primary purpose is to validate the correctness, robustness, and security of the chat assistant update functionality. The tests cover various input scenarios including authorization, chat assistant properties (such as name, avatar, linked datasets), LLM (large language model) configuration, and prompt customization.
The tests are implemented using the pytest framework and leverage parametrization to efficiently cover multiple test cases. This ensures that updates to chat assistants adhere to expected behaviors and constraints, such as validation rules and permission checks.
Test Classes and Methods
Imports
pytest: Testing framework used to define and run tests.CHAT_ASSISTANT_NAME_LIMIT,INVALID_API_TOKEN,list_chat_assistants,update_chat_assistant: Constants and functions imported from a common module related to chat assistant management.RAGFlowHttpApiAuth: Authentication class for HTTP API calls.encode_avatar: Utility function to convert an image file to a base64 encoded string for avatar uploads.create_image_file: Utility to generate image files for testing.
Class: TestAuthorization
Tests related to authorization and authentication when calling update_chat_assistant.
Method: test_invalid_auth(auth, expected_code, expected_message)
Purpose: Validates that the API correctly rejects requests with missing or invalid authorization tokens.
Parameters:
auth: An authorization object orNone.expected_code: Expected error code returned by the API.expected_message: Expected error message returned by the API.
Behavior: Calls
update_chat_assistantwith the providedauthand a dummy chat assistant ID, then asserts the response code and message.Example Usage:
auth = None
expected_code = 0
expected_message = "`Authorization` can't be empty"
res = update_chat_assistant(auth, "chat_assistant_id")
assert res["code"] == expected_code
assert res["message"] == expected_message
Class: TestChatAssistantUpdate
Contains multiple tests validating different fields and parameters of the chat assistant update process.
Method: test_name(get_http_api_auth, add_chat_assistants_func, payload, expected_code, expected_message)
Purpose: Tests validation and uniqueness constraints on the
namefield of a chat assistant.Parameters:
get_http_api_auth: Fixture providing valid authentication.add_chat_assistants_func: Fixture that sets up chat assistants and returns their IDs.payload: Dictionary containing thenamefield to test.expected_code: Expected response code.expected_message: Expected error message if any.
Details:
Tests valid names.
Tests name length exceeding the limit.
Tests invalid types for name.
Tests empty names.
Tests duplicate names (case insensitive).
Usage: Provides multiple test cases through
pytest.mark.parametrize.Post-Condition: On success, verifies that the chat assistant's name is updated correctly by fetching it with
list_chat_assistants.
Method: test_dataset_ids(get_http_api_auth, add_chat_assistants_func, dataset_ids, expected_code, expected_message)
Purpose: Validates the
dataset_idsfield, ensuring datasets belong to the user and are properly associated.Parameters:
Similar fixtures as above.
dataset_ids: Could be a list, a callable returning a list, or a string.expected_code,expected_message: Expected results.
Behavior:
Tests empty list.
Tests valid dataset IDs.
Tests invalid dataset IDs (not owned).
Notes: Some cases are skipped due to existing issues.
Method: test_avatar(get_http_api_auth, add_chat_assistants_func, tmp_path)
Purpose: Tests the ability to upload and update the avatar for a chat assistant.
Parameters:
tmp_path: Temporary directory fixture for file creation.
Process:
Creates an image file.
Encodes it to base64.
Updates the chat assistant with the avatar.
Verifies success.
Method: test_llm(get_http_api_auth, add_chat_assistants_func, llm, expected_code, expected_message)
Purpose: Tests various configurations of the large language model (LLM) parameters during update.
Parameters:
llm: Dictionary representing LLM configuration, e.g., model name, temperature, top_p, penalties, max tokens.Expected codes and messages.
Details:
Validates known models and parameters.
Checks default values when no LLM config is provided.
Skips tests for some invalid or edge values.
Post-Condition: On success, the updated LLM parameters are verified through
list_chat_assistants.
Method: test_prompt(get_http_api_auth, add_chat_assistants_func, prompt, expected_code, expected_message)
Purpose: Tests prompt-related settings for the chat assistant, including similarity thresholds, weights, variables, and other prompt customization.
Parameters:
prompt: Dictionary containing prompt configuration.Expected codes and messages.
Details:
Validates presence and format of keys.
Checks for required placeholders like
{knowledge}.Verifies default prompt settings if none provided.
Some invalid or edge cases are skipped.
Post-Condition: On success, the prompt settings are verified by fetching the chat assistant data.
Important Implementation Details
Parametrized Testing: Most tests use
pytest.mark.parametrizeto run multiple scenarios with different inputs and expected outcomes.Fixtures: The tests depend on several fixtures (
get_http_api_auth,add_chat_assistants_func,tmp_path) which provide authenticated contexts, pre-created chat assistants, and temporary filesystem paths.Validation Logic: The tests indirectly verify that the backend implements:
Authorization checks.
Name validation and uniqueness.
Dataset ownership validation.
Avatar encoding and storage.
LLM parameter validation with default values fallback.
Prompt configuration validation, including placeholders and logical constraints.
Skip Markers: Some test cases are marked to skip due to known issues or unsupported edge cases, indicating ongoing development or bug tracking.
Interaction with Other System Components
update_chat_assistant: The main API function under test that updates chat assistant details.list_chat_assistants: Used to verify the state of chat assistants after updates.Authentication (
RAGFlowHttpApiAuth): Used to simulate client authentication.Utilities (
encode_avatar,create_image_file): Used to prepare inputs, especially for avatar-related tests.Constants (
CHAT_ASSISTANT_NAME_LIMIT,INVALID_API_TOKEN): Used for validation boundary tests.
This file helps ensure that the chat assistant update functionality integrates properly with authentication modules, dataset ownership verification, and configuration management, thereby maintaining system integrity.
Visual Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(auth, expected_code, expected_message)
}
class TestChatAssistantUpdate {
+test_name(get_http_api_auth, add_chat_assistants_func, payload, expected_code, expected_message)
+test_dataset_ids(get_http_api_auth, add_chat_assistants_func, dataset_ids, expected_code, expected_message)
+test_avatar(get_http_api_auth, add_chat_assistants_func, tmp_path)
+test_llm(get_http_api_auth, add_chat_assistants_func, llm, expected_code, expected_message)
+test_prompt(get_http_api_auth, add_chat_assistants_func, prompt, expected_code, expected_message)
}
TestAuthorization ..> update_chat_assistant : calls
TestChatAssistantUpdate ..> update_chat_assistant : calls
TestChatAssistantUpdate ..> list_chat_assistants : verifies
TestChatAssistantUpdate ..> encode_avatar : uses
TestChatAssistantUpdate ..> create_image_file : uses
TestAuthorization ..> RAGFlowHttpApiAuth : uses
TestChatAssistantUpdate ..> RAGFlowHttpApiAuth : uses
Summary
This file is a pytest-based test suite focused on validating the update functionality of chat assistants.
Covers authorization, input validation (name, datasets, LLM config, prompt), and avatar updates.
Uses fixtures and parametrized tests to cover broad scenarios.
Ensures system enforces data integrity, security, and expected behavior.
Integrates closely with authentication and chat assistant listing APIs.
Skipped tests indicate areas of ongoing work or known bugs.
This testing module is essential for maintaining the reliability and correctness of chat assistant updates in the InfiniFlow platform.