test_create_session_with_chat_assistant.py
Overview
This file contains a suite of automated tests designed to verify the functionality, robustness, and security of the session creation feature associated with chat assistants in the InfiniFlow system. The primary focus is on the API endpoint or utility function create_session_with_chat_assistant, which facilitates the creation of sessions linked to specific chat assistants.
The tests cover various aspects including:
Authorization and authentication validation.
Input validation for session creation payloads.
Handling of invalid or non-existent chat assistant identifiers.
Concurrent creation of sessions to test system scalability and race conditions.
Behavior when attempting to create sessions for deleted chat assistants.
The tests are implemented using the pytest framework and leverage fixtures and parameterization to cover multiple scenarios efficiently.
Classes and Functions
1. Class: TestAuthorization
Purpose:
Tests the authorization mechanism when creating a session with a chat assistant.
Decorators:
@pytest.mark.p1 – Marks the test class as priority 1 (critical tests).
Methods:
test_invalid_auth(self, invalid_auth, expected_code, expected_message)
Parameters:
invalid_auth: An invalid or empty authentication instance or token.expected_code: The expected error code returned by the API.expected_message: The expected error message returned by the API.
Description:
This test method verifies that the system properly rejects session creation attempts without valid authorization. It tests two cases:No authorization provided (
None).An invalid API token wrapped in the
RAGFlowHttpApiAuthauthentication class.
Usage Example:
invalid_auth = RAGFlowHttpApiAuth("invalid_token")
res = create_session_with_chat_assistant(invalid_auth, "chat_assistant_id")
assert res["code"] == 109
assert "API key is invalid" in res["message"]
2. Class: TestSessionWithChatAssistantCreate
Purpose:
Contains tests for creating sessions with chat assistants, focusing on input validation, concurrency, and edge cases.
Decorators:
@pytest.mark.usefixtures("clear_session_with_chat_assistants")– Ensures that sessions and chat assistants are cleared before each test to maintain test isolation.
Methods:
test_name(self, HttpApiAuth, add_chat_assistants, payload, expected_code, expected_message)
Parameters:
HttpApiAuth: A valid authentication fixture.add_chat_assistants: A fixture that adds chat assistants and returns their IDs.payload: Dictionary containing the session creation payload, typically including"name".expected_code: Expected response code after session creation attempt.expected_message: Expected response message.
Description:
Verifies that session creation enforces correct naming rules:Valid names succeed.
Names exceeding the length limit are rejected.
Non-string names are rejected.
Empty names are rejected.
Duplicate names and case-insensitive duplicates are handled correctly.
Special Notes:
Some test cases are skipped due to known issues (e.g., name length limit).Usage Example:
payload = {"name": "valid_session_name"}
res = create_session_with_chat_assistant(HttpApiAuth, chat_assistant_ids[0], payload)
assert res["code"] == 0
assert res["data"]["name"] == payload["name"]
test_invalid_chat_assistant_id(self, HttpApiAuth, chat_assistant_id, expected_code, expected_message)
Parameters:
HttpApiAuth: Valid authentication.chat_assistant_id: The chat assistant ID to test, which may be invalid or empty.expected_code: Expected error code.expected_message: Expected error message.
Description:
Tests system behavior when the chat assistant ID provided is invalid or empty. It expects the system to reject such requests with appropriate error codes.Usage Example:
res = create_session_with_chat_assistant(HttpApiAuth, "", {"name": "test"})
assert res["code"] == 100
assert "Method Not Allowed" in res["message"]
test_concurrent_create_session(self, HttpApiAuth, add_chat_assistants)
Parameters:
HttpApiAuth: Valid authentication.add_chat_assistants: Fixture providing chat assistant IDs.
Description:
Tests the system's ability to handle a large number (1000) of concurrent session creation requests. It uses Python'sThreadPoolExecutorwith 5 worker threads to simulate concurrency.Implementation Details:
Retrieves the current number of sessions.
Submits 1000 asynchronous session creation calls.
Validates all responses are successful.
Confirms the total number of sessions increased by 1000.
Usage Example:
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [
executor.submit(
create_session_with_chat_assistant,
HttpApiAuth,
chat_assistant_ids[0],
{"name": f"session test {i}"}
)
for i in range(1000)
]
for future in futures:
assert future.result()["code"] == 0
test_add_session_to_deleted_chat_assistant(self, HttpApiAuth, add_chat_assistants)
Parameters:
HttpApiAuth: Valid authentication.add_chat_assistants: Fixture providing chat assistant IDs.
Description:
Tests that creating a session for a chat assistant that has been deleted fails with the appropriate error code and message.Workflow:
Deletes a chat assistant.
Attempts to create a session linked to the deleted chat assistant.
Expects an error stating the assistant is not owned.
Usage Example:
res = delete_chat_assistants(HttpApiAuth, {"ids": [chat_assistant_id]})
assert res["code"] == 0
res = create_session_with_chat_assistant(HttpApiAuth, chat_assistant_id, {"name": "valid_name"})
assert res["code"] == 102
assert "do not own the assistant" in res["message"]
Important Implementation Details and Algorithms
Concurrency Handling:
The testtest_concurrent_create_sessionusesThreadPoolExecutorto simulate multiple simultaneous requests to create sessions. This ensures that the underlying system can handle race conditions and concurrent writes without data corruption or errors.Parameterized Testing:
Many tests usepytest.mark.parametrizeto run the same test logic with different inputs and expected outputs, improving test coverage and reducing code duplication.Fixtures Usage:
HttpApiAuthprovides authenticated HTTP API access.add_chat_assistantsprovides pre-created chat assistants for session association.clear_session_with_chat_assistantsensures a clean slate before tests.
Error Handling Validation:
The tests check that error codes and messages conform to expected values, ensuring the API behaves predictably in erroneous scenarios.
Interaction with Other Modules
Imports from
common:create_session_with_chat_assistant: The main function under test, responsible for creating sessions.delete_chat_assistants: Used to delete chat assistants to test behavior with deleted resources.list_session_with_chat_assistants: Used to list existing sessions, useful for validation before and after tests.
Imports from
configs:INVALID_API_TOKEN: Used to simulate invalid authentication.SESSION_WITH_CHAT_NAME_LIMIT: Defines the maximum allowed length for session names.
Imports from
libs.auth:RAGFlowHttpApiAuth: Authentication class used to wrap API tokens.
pytest:
Used for test framework functionalities including fixtures, parameterization, and test markers.
Visual Diagram
classDiagram
class TestAuthorization {
+test_invalid_auth(invalid_auth, expected_code, expected_message)
}
class TestSessionWithChatAssistantCreate {
+test_name(HttpApiAuth, add_chat_assistants, payload, expected_code, expected_message)
+test_invalid_chat_assistant_id(HttpApiAuth, chat_assistant_id, expected_code, expected_message)
+test_concurrent_create_session(HttpApiAuth, add_chat_assistants)
+test_add_session_to_deleted_chat_assistant(HttpApiAuth, 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
Summary
The test_create_session_with_chat_assistant.py file is a critical test suite that ensures the integrity, security, and correctness of the session creation feature tied to chat assistants within the InfiniFlow platform. It employs thorough parameterized tests, concurrency simulations, and edge case validations to maintain high-quality standards for this component.
This file acts as a safeguard against regressions and improper usage, providing confidence that the API behaves as expected under a variety of scenarios. It interacts closely with auxiliary modules responsible for authentication, chat assistant management, and session listing, reflecting an integrated testing approach within the overall system.