🐛 fix(endpoints.py): import validate_api_key function from auth.utils to fix missing dependency

✨ feat(endpoints.py): add validation of API key in process_flow endpoint to ensure only valid requests are processed
🔧 chore(utils.py): add validate_api_key function to validate API key against database
🔧 chore(test_endpoints.py): add test case for process_flow endpoint to test API key validation and processing
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-25 20:02:53 -03:00
commit 282e6b0c18
3 changed files with 59 additions and 2 deletions

View file

@ -1,3 +1,5 @@
from langflow.services.auth.utils import get_password_hash
from langflow.services.database.models.api_key.api_key import ApiKey
import pytest
from fastapi.testclient import TestClient
from langflow.interface.tools.constants import CUSTOM_TOOLS
@ -83,6 +85,49 @@ PROMPT_REQUEST = {
}
@pytest.fixture
def created_api_key(active_user):
hashed = get_password_hash("random_key")
return ApiKey(
name="test_api_key",
user_id=active_user.id,
api_key="random_key",
hashed_api_key=hashed,
)
def test_process_flow(client, mocker, created_api_key):
# Mock de process_graph_cached
mock_process_graph_cached = mocker.patch(
"langflow.processing.process.process_graph_cached", autospec=True
)
# Defina o valor de retorno para o mock
mock_process_graph_cached.return_value = ("result_mock", "session_id_mock")
api_key = created_api_key.api_key
headers = {"Authorization": f"Bearer {api_key}"}
# Dummy POST data
post_data = {
"inputs": {"key": "value"},
"tweaks": None,
"clear_cache": False,
"session_id": None,
}
# Make the request to the FastAPI TestClient
response = client.post("api/v1/process/flow_test", headers=headers, json=post_data)
# Check the response
assert response.status_code == 200
assert response.json()["result"] == "result_mock"
assert response.json()["session_id"] == "session_id_mock"
# Ensure mock was called once
mock_process_graph_cached.assert_called_once()
def test_get_all(client: TestClient, logged_in_headers):
response = client.get("api/v1/all", headers=logged_in_headers)
assert response.status_code == 200