test_delete_sessions_with_chat_assistant.py
Overview
This file contains comprehensive automated tests for the session deletion functionality associated with chat assistants in the InfiniFlow system. Using the pytest framework, it validates multiple aspects of the API endpoint responsible for deleting chat sessions linked to chat assistants. The tests cover:
Authorization and authentication checks.
Handling of invalid or unauthorized chat assistant IDs.
Partial and complete deletion scenarios, including edge cases like invalid session IDs or duplicate deletions.
Concurrency and performance tests for bulk session deletion.
Verification that session listing after deletion reflects expected results.
This ensures robustness, correctness, and security of the session deletion feature when interacting with chat assistants.
Classes and Functions
Class: TestAuthorization
Purpose:
Tests authorization-related behaviors when deleting chat sessions, specifically ensuring the API rejects requests with missing or invalid authentication tokens.
Methods:
test_invalid_auth(auth, expected_code, expected_message)Parameters:
auth: Authentication object orNoneto simulate missing auth.expected_code(int): Expected API response code.expected_message(str): Expected error message.
Returns: None (assertions within test)
Description:
Callsdelete_session_with_chat_assistantswith various invalid authentication scenarios and asserts that the response contains the correct error codes and messages.Example usage:
TestAuthorization().test_invalid_auth(None, 0, "`Authorization` can't be empty")
Class: TestSessionWithChatAssistantDelete
Purpose:
Contains a suite of tests for the session deletion API endpoint under different valid and invalid scenarios, including concurrency and large volume deletion.
Methods:
test_invalid_chat_assistant_id(get_http_api_auth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message)Parameters:
get_http_api_auth: Auth fixture providing a valid HTTP auth object.add_sessions_with_chat_assistant_func: Fixture that creates a chat assistant and sessions, returns(chat_assistant_id, session_ids).chat_assistant_id(str): The chat assistant ID to test.expected_code(int): Expected API response code.expected_message(str): Expected response message.
Returns: None (assertions within test)
Description:
Tests deletion attempts using invalid or unauthorized chat assistant IDs, expecting appropriate error responses.
test_delete_partial_invalid_id(get_http_api_auth, add_sessions_with_chat_assistant_func, payload)Parameters:
get_http_api_auth: Valid authentication object.add_sessions_with_chat_assistant_func: Fixture returning(chat_assistant_id, session_ids).payload: Either a callable that returns a dict with mixed valid and invalid session IDs or a dict directly.
Returns: None
Description:
Tests deletion requests where some session IDs are invalid or do not belong to the chat assistant. Verifies that the API deletes valid sessions and returns error info for invalid ones. Also checks that after deletion, no sessions remain.
test_repeated_deletion(get_http_api_auth, add_sessions_with_chat_assistant_func)Parameters:
get_http_api_auth: Auth object.add_sessions_with_chat_assistant_func: Returns(chat_assistant_id, session_ids).
Returns: None
Description:
Tests that deleting the same sessions twice results in an error on the second attempt because the sessions no longer exist.
test_duplicate_deletion(get_http_api_auth, add_sessions_with_chat_assistant_func)Parameters: Same as above.
Description:
Tests deletion requests where session IDs are duplicated in the payload. Checks that duplicates are detected and the API returns an error, but valid sessions are still deleted.
test_concurrent_deletion(get_http_api_auth, add_chat_assistants)Parameters:
get_http_api_auth: Auth object.add_chat_assistants: Fixture providing multiple chat assistants.
Description:
Adds 100 sessions, then concurrently deletes each session in separate threads to test thread safety and concurrency handling in the deletion API.
test_delete_1k(get_http_api_auth, add_chat_assistants)Parameters: Same as above.
Description:
Adds 1000 sessions and deletes them all in one bulk request. Tests performance and correctness at scale.
test_basic_scenarios(get_http_api_auth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message, remaining)Parameters:
get_http_api_auth: Auth object.add_sessions_with_chat_assistant_func: Returns(chat_assistant_id, session_ids).payload: Input payload for deletion API.expected_code: Expected response code.expected_message: Expected error message if any.remaining: Number of sessions expected to remain after deletion.
Description:
Covers multiple basic test cases including null payload, invalid IDs, non-JSON payloads, partial and full deletions, and empty ID lists.
Important Implementation Details
Testing Framework: Uses
pytestwith fixtures (likeget_http_api_authandadd_sessions_with_chat_assistant_func) presumably supplied elsewhere in the test suite to provide authenticated contexts and pre-created data.Parameterized Testing: Heavily uses
pytest.mark.parametrizeto run the same test logic with different input parameters and expectations.Concurrency Test: Utilizes Python's
ThreadPoolExecutorto simulate multiple concurrent deletion requests, ensuring the API can handle simultaneous operations safely.Error Handling Checks: Tests expect specific error codes and messages, validating that the system returns meaningful feedback on invalid operations (e.g., invalid token, unauthorized chat assistant, invalid session IDs).
Session Verification: After deletion operations, the tests fetch the list of sessions to confirm that the deletions actually took effect.
Duplicate and Partial Deletion: Tests confirm that the API handles duplicate IDs gracefully and partially invalid payloads without failing the entire operation.
Interaction with Other Parts of the System
Imports from
commonModule:INVALID_API_TOKEN: Used to simulate invalid authentication.batch_add_sessions_with_chat_assistant: Utility to bulk-add sessions to a chat assistant.delete_session_with_chat_assistants: Core API function under test.list_session_with_chat_assistants: Used to verify session state post-deletion.
Imports from
libs.auth:RAGFlowHttpApiAuth: Authentication helper class used to create authorization tokens.
Fixtures (Not defined in this file):
get_http_api_auth: Provides a valid authenticated API context.add_sessions_with_chat_assistant_func: Creates chat assistants and sessions.add_chat_assistants: Adds multiple chat assistants for testing.
This file depends on these utilities and fixtures for setup and API interaction, making it an integral part of the integration and functional testing suite for session management.
Usage Example
This file is intended to be run as part of the test suite:
pytest test_delete_sessions_with_chat_assistant.py
Or selectively:
pytest test_delete_sessions_with_chat_assistant.py::TestAuthorization::test_invalid_auth
Mermaid Diagram: Class Structure
classDiagram
class TestAuthorization {
+test_invalid_auth(auth, expected_code, expected_message)
}
class TestSessionWithChatAssistantDelete {
+test_invalid_chat_assistant_id(get_http_api_auth, add_sessions_with_chat_assistant_func, chat_assistant_id, expected_code, expected_message)
+test_delete_partial_invalid_id(get_http_api_auth, add_sessions_with_chat_assistant_func, payload)
+test_repeated_deletion(get_http_api_auth, add_sessions_with_chat_assistant_func)
+test_duplicate_deletion(get_http_api_auth, add_sessions_with_chat_assistant_func)
+test_concurrent_deletion(get_http_api_auth, add_chat_assistants)
+test_delete_1k(get_http_api_auth, add_chat_assistants)
+test_basic_scenarios(get_http_api_auth, add_sessions_with_chat_assistant_func, payload, expected_code, expected_message, remaining)
}
Summary
test_delete_sessions_with_chat_assistant.py is a critical test module that rigorously verifies the correctness, security, and performance of the chat assistant session deletion API in InfiniFlow. It uses parameterized tests, concurrency, and large data sets to ensure robustness against various edge cases and usage patterns, serving as a quality gate for this core functionality.