test_create_session_with_chat_assistant.py
Overview
This file contains a suite of automated tests for verifying the behavior and robustness of the session creation functionality associated with chat assistants in the InfiniFlow system. Specifically, it tests the function create_session_with_chat_assistant which allows users to create sessions linked to particular chat assistant instances.
The tests cover scenarios including:
Authorization and authentication validation.
Input validation for session names.
Handling of invalid chat assistant identifiers.
Concurrent session creation to test system scalability and race conditions.
Behavior when attempting to create sessions for deleted chat assistants.
These tests use the pytest framework and various fixtures and helper functions imported from other modules such as common and libs.auth.
Classes and Functions
Class: TestAuthorization
This class groups tests related to authorization validation for creating sessions.
Method: test_invalid_auth
Purpose: Ensures that the API properly rejects requests with missing or invalid authorization credentials.
Parameters:
auth: The authorization mechanism or credentials used to authenticate the API call.expected_code: The expected error code in the response.expected_message: The expected error message in the response.
Returns: None. The method asserts correctness by comparing the actual response with expected outcomes.
Usage Example:
auth = None
expected_code = 0
expected_message = "`Authorization` can't be empty"
test_instance = TestAuthorization()
test_instance.test_invalid_auth(auth, expected_code, expected_message)
Test cases include:
Missing authorization (should return an error that Authorization can't be empty).
Invalid API token (should return authentication error with code 109).
Class: TestSessionWithChatAssistantCreate
This class contains tests related to creating sessions associated with chat assistants, including validation of input parameters and system behavior under various conditions.
The class uses the pytest fixture clear_session_with_chat_assistants to ensure a clean state before each test.
Method: test_name
Purpose: Validates the behavior of the session creation endpoint when given different
namefield inputs.Parameters:
get_http_api_auth: A pytest fixture providing valid HTTP API authentication.add_chat_assistants: A fixture that adds chat assistants and returns their IDs.payload: Dictionary containing the payload for session creation, mainly thenamefield.expected_code: Expected result code.expected_message: Expected result message.
Returns: None. Uses assertions to verify response correctness.
Behavior:
Tests valid names, empty names, names exceeding length limit, non-string names, duplicated names, and case-insensitivity.
Skips some tests marked with issues.
Usage Example:
payload = {"name": "valid_name"}
expected_code = 0
expected_message = ""
test_instance = TestSessionWithChatAssistantCreate()
test_instance.test_name(get_http_api_auth, add_chat_assistants, payload, expected_code, expected_message)
Method: test_invalid_chat_assistant_id
Purpose: Checks the system's response when invalid or empty chat assistant IDs are used.
Parameters:
get_http_api_auth: Fixture for valid authentication.chat_assistant_id: The chat assistant ID to test.expected_code: Expected error code.expected_message: Expected error message.
Returns: None. Validates error handling for unauthorized or malformed chat assistant identifiers.
Test Cases:
Empty string as chat assistant ID.
Invalid or unowned chat assistant ID.
Method: test_concurrent_create_session
Purpose: Tests the system's ability to handle concurrent session creation requests to the same chat assistant.
Parameters:
get_http_api_auth: Valid API authentication.add_chat_assistants: Adds chat assistants and returns their IDs.
Implementation Details:
Uses
ThreadPoolExecutorfromconcurrent.futureswith 5 workers.Creates 1000 sessions concurrently, each with a unique name.
Verifies that all session creations succeed.
Verifies total session count increases accordingly.
Returns: None. Asserts successful concurrent session creation.
Method: test_add_session_to_deleted_chat_assistant
Purpose: Ensures that creating sessions for chat assistants that have been deleted is properly rejected.
Parameters:
get_http_api_auth: Valid authentication.add_chat_assistants: Fixture for adding chat assistants.
Behavior:
Deletes a chat assistant.
Attempts to create a session linked to the deleted chat assistant.
Expects an error indicating the assistant is not owned (code 102).
Returns: None. Validates proper authorization and resource ownership enforcement.
Important Implementation Details and Algorithms
Parameterization and Test Skipping: The use of
pytest.mark.parametrizeallows testing multiple input variations in a concise manner. Some tests are skipped due to known issues, marked withpytest.mark.skip.Concurrent Execution: To simulate real-world load and concurrency, the test
test_concurrent_create_sessionuses aThreadPoolExecutorto dispatch multiple simultaneous session creation requests.Authorization Handling: The tests rely on
RAGFlowHttpApiAuthclass to simulate authenticated API calls, and they verify the system's response to invalid tokens.Use of Fixtures: Fixtures like
get_http_api_auth,add_chat_assistants, andclear_session_with_chat_assistantsprovide reusable setup and teardown mechanisms to maintain test isolation and cleanliness.
Interaction with Other System Components
Imports from
commonmodule:INVALID_API_TOKEN: Used to simulate invalid authentication.SESSION_WITH_CHAT_NAME_LIMIT: Defines maximum allowed length for session names.create_session_with_chat_assistant: Core function under test, responsible for session creation.delete_chat_assistants: Used to delete chat assistants in tests.list_session_with_chat_assistants: Used to retrieve existing sessions for verification.
Imports from
libs.auth:RAGFlowHttpApiAuth: Represents the authorization mechanism used in API calls.
Testing Framework:
pytestis used for test execution, parameterization, and marking (e.g., priorities with@pytest.mark.p1).
Threading Module:
ThreadPoolExecutorfromconcurrent.futuresis used for concurrent test scenarios.
These interactions indicate this test file is tightly integrated with the API layer of the InfiniFlow system, particularly the chat assistant session management feature.
Visual Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(auth, expected_code, expected_message)
}
class TestSessionWithChatAssistantCreate {
+test_name(get_http_api_auth, add_chat_assistants, payload, expected_code, expected_message)
+test_invalid_chat_assistant_id(get_http_api_auth, chat_assistant_id, expected_code, expected_message)
+test_concurrent_create_session(get_http_api_auth, add_chat_assistants)
+test_add_session_to_deleted_chat_assistant(get_http_api_auth, add_chat_assistants)
}
TestAuthorization ..> create_session_with_chat_assistant : calls
TestSessionWithChatAssistantCreate ..> create_session_with_chat_assistant : calls
TestSessionWithChatAssistantCreate ..> delete_chat_assistants : calls
TestSessionWithChatAssistantCreate ..> list_session_with_chat_assistants : calls
TestAuthorization ..> RAGFlowHttpApiAuth : uses
TestSessionWithChatAssistantCreate ..> RAGFlowHttpApiAuth : uses
Summary
This test suite validates session creation for chat assistants.
Covers authorization, input validation, concurrency, and edge cases.
Uses parameterized tests and fixtures to ensure comprehensive coverage.
Employs multi-threading to verify concurrent access stability.
Integrates closely with the InfiniFlow API and authentication modules.
This file is essential for maintaining the reliability and correctness of session management within the chat assistant features of InfiniFlow.