tests: update tests to use httpx.AsyncClient (#3984)
* Add async support and dependencies to pyproject.toml files - Added `asgi-lifespan>=2.1.0` to dependencies. - Configured `asyncio_mode` and `asyncio_default_fixture_loop_scope` for pytest. - Updated `tool.uv` section with `asgi-lifespan` in dev-dependencies. * Convert test fixtures to async and use AsyncClient for HTTP requests * Handle 'ImportFrom' nodes in AST validation to support module attribute imports * Convert test cases to use async HTTP client - Updated test cases in `test_database.py`, `test_endpoints.py`, `test_user.py`, `test_variable.py`, `test_files.py`, `test_chat_endpoint.py`, `test_misc.py`, `test_messages_endpoints.py`, `test_api_key.py`, `test_webhook.py`, and `test_login.py` to use `httpx.AsyncClient` instead of `fastapi.TestClient`. - Modified test functions to be asynchronous and use `await` for HTTP requests. - Adjusted fixtures and helper functions to support asynchronous operations. - Ensured consistency in endpoint paths and request methods across all test cases. * Refactor string concatenation to f-string in test_chat_endpoint.py * [autofix.ci] apply automated fixes * Refactor import validation to use pattern matching for AST nodes * Set `startup_timeout` and `shutdown_timeout` to `None` in `LifespanManager` for test files. * Convert test functions to async in `test_messages_endpoints.py` * Add `api_key_required` marker to assistant component tests --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
7e3d470845
commit
6febae599b
19 changed files with 444 additions and 322 deletions
|
|
@ -1,44 +1,42 @@
|
|||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
from langflow.services.database.models.api_key import ApiKeyCreate
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api_key(client, logged_in_headers, active_user):
|
||||
async def api_key(client, logged_in_headers, active_user):
|
||||
api_key = ApiKeyCreate(name="test-api-key")
|
||||
|
||||
response = client.post("api/v1/api_key", data=api_key.model_dump_json(), headers=logged_in_headers)
|
||||
response = await client.post("api/v1/api_key/", data=api_key.model_dump_json(), headers=logged_in_headers)
|
||||
assert response.status_code == 200, response.text
|
||||
return response.json()
|
||||
|
||||
|
||||
def test_get_api_keys(client, logged_in_headers, api_key):
|
||||
response = client.get("api/v1/api_key", headers=logged_in_headers)
|
||||
async def test_get_api_keys(client: AsyncClient, logged_in_headers, api_key):
|
||||
response = await client.get("api/v1/api_key/", headers=logged_in_headers)
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "total_count" in data
|
||||
assert "user_id" in data
|
||||
assert "api_keys" in data
|
||||
assert any("test-api-key" in api_key["name"] for api_key in data["api_keys"])
|
||||
# assert all api keys in data["api_keys"] are masked
|
||||
assert all("**" in api_key["api_key"] for api_key in data["api_keys"])
|
||||
|
||||
|
||||
def test_create_api_key(client, logged_in_headers):
|
||||
async def test_create_api_key(client: AsyncClient, logged_in_headers):
|
||||
api_key_name = "test-api-key"
|
||||
response = client.post("api/v1/api_key", json={"name": api_key_name}, headers=logged_in_headers)
|
||||
response = await client.post("api/v1/api_key/", json={"name": api_key_name}, headers=logged_in_headers)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "name" in data and data["name"] == api_key_name
|
||||
assert "api_key" in data
|
||||
# When creating the API key is returned which is
|
||||
# the only time the API key is unmasked
|
||||
assert "**" not in data["api_key"]
|
||||
|
||||
|
||||
def test_delete_api_key(client, logged_in_headers, active_user, api_key):
|
||||
# Assuming a function to create a test API key, returning the key ID
|
||||
async def test_delete_api_key(client, logged_in_headers, active_user, api_key):
|
||||
api_key_id = api_key["id"]
|
||||
response = client.delete(f"api/v1/api_key/{api_key_id}", headers=logged_in_headers)
|
||||
response = await client.delete(f"api/v1/api_key/{api_key_id}", headers=logged_in_headers)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["detail"] == "API Key deleted"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue