File size: 2,955 Bytes
99fdb5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pytest
from core.models import ToolUsage
from sqlalchemy.orm import Session
# You will need to import the admin token generator or manually get an admin token
# from your admin_auth.py router for the authenticated request.

def test_tool_usage_logging_and_retrieval(client, db: Session):
    # 1. SETUP: Find initial usage count for a specific tool
    TEST_TOOL = "pdf_merge"
    
    # Check current count in the database
    initial_count = db.query(ToolUsage).filter(
        ToolUsage.tool_name == TEST_TOOL
    ).count()

    # 2. ACTION: Simulate a successful request to the tool endpoint
    # NOTE: The client needs to send form-data or files as required by the endpoint.
    # We use a POST request that triggers the LogToolUsage dependency.
    
    # Since /pdf/merge takes files, we simulate the file upload:
    # (This is a simplified example, actual FastAPI testing requires a BytesIO object)
    
    # --- Actual Code Execution (Simulated Tool Call) ---
    response = client.post(
        "/pdf/merge",
        files=[
            ("files", ("file1.pdf", b"pdf_content_1", "application/pdf")),
            ("files", ("file2.pdf", b"pdf_content_2", "application/pdf")),
        ],
        # Add necessary headers, like the admin token, if the endpoint was protected
        # but here we test the logging on the public endpoint first.
    )

    # Check that the tool execution was successful (e.g., returns a file)
    assert response.status_code == 200
    
    # 3. VERIFY LOGGING: Check the database directly
    
    # Check the count again after the request
    new_count = db.query(ToolUsage).filter(
        ToolUsage.tool_name == TEST_TOOL
    ).count()
    
    # The count should have increased by exactly 1
    assert new_count == initial_count + 1
    
    # 4. VERIFY RETRIEVAL (The new stats router)
    # ⚠️ For this step, you must get an admin token first
    
    # Simulate admin login to get a token
    auth_response = client.post("/admin/login", data={"password": "YOUR_ADMIN_SECRET"})
    admin_token = auth_response.json().get("token")
    
    # Request the statistics endpoint (which requires the admin token)
    stats_response = client.get(
        "/admin/stats/tool_usage",
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    
    # Check that the stats endpoint is accessible and returns data
    assert stats_response.status_code == 200
    stats_data = stats_response.json().get("data", [])
    
    # 5. FINAL VERIFICATION: Check the stats data for the updated count
    
    # Find the statistics for our test tool
    tool_stat = next((item for item in stats_data if item["tool_name"] == TEST_TOOL), None)
    
    # Check that the tool exists in the returned stats and the count matches the database
    assert tool_stat is not None
    assert tool_stat["count"] == new_count
    
    print(f"Test Passed: {TEST_TOOL} count increased from {initial_count} to {new_count}")