Add webhook test and fix webhook to support endpoint name

This commit is contained in:
ogabrielluiz 2024-06-14 16:30:17 -03:00
commit d060d5308a
4 changed files with 275 additions and 8 deletions

View file

@ -30,7 +30,7 @@ from langflow.schema.graph import Tweaks
from langflow.services.auth.utils import api_key_security, get_current_active_user
from langflow.services.cache.utils import save_uploaded_file
from langflow.services.database.models.flow import Flow
from langflow.services.database.models.flow.utils import get_all_webhook_components_in_flow, get_flow_by_id
from langflow.services.database.models.flow.utils import get_all_webhook_components_in_flow
from langflow.services.database.models.user.model import User
from langflow.services.deps import (
get_cache_service,
@ -211,10 +211,10 @@ async def simplified_run_flow(
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)) from exc
@router.post("/webhook/{flow_id}", response_model=dict, status_code=HTTPStatus.ACCEPTED)
@router.post("/webhook/{flow_id_or_name}", response_model=dict, status_code=HTTPStatus.ACCEPTED)
async def webhook_run_flow(
db: Annotated[Session, Depends(get_session)],
flow: Annotated[Flow, Depends(get_flow_by_id)],
flow: Annotated[Flow, Depends(get_flow_by_id_or_endpoint_name)],
request: Request,
background_tasks: BackgroundTasks,
session_service: SessionService = Depends(get_session_service),

View file

@ -14,9 +14,6 @@ from dotenv import load_dotenv
from fastapi.testclient import TestClient
from httpx import AsyncClient
from sqlmodel import Session, SQLModel, create_engine, select
from sqlmodel.pool import StaticPool
from typer.testing import CliRunner
from langflow.graph.graph.base import Graph
from langflow.initial_setup.setup import STARTER_FOLDER_NAME
from langflow.services.auth.utils import get_password_hash
@ -26,6 +23,9 @@ from langflow.services.database.models.folder.model import Folder
from langflow.services.database.models.user.model import User, UserCreate
from langflow.services.database.utils import session_getter
from langflow.services.deps import get_db_service
from sqlmodel import Session, SQLModel, create_engine, select
from sqlmodel.pool import StaticPool
from typer.testing import CliRunner
if TYPE_CHECKING:
from langflow.services.database.service import DatabaseService
@ -395,7 +395,9 @@ def added_vector_store(client, json_vector_store, logged_in_headers):
def added_webhook_test(client, json_webhook_test, logged_in_headers):
webhook_test = orjson.loads(json_webhook_test)
data = webhook_test["data"]
webhook_test = FlowCreate(name="Webhook Test", description="description", data=data)
webhook_test = FlowCreate(
name="Webhook Test", description="description", data=data, endpoint_name=webhook_test["endpoint_name"]
)
response = client.post("api/v1/flows/", json=webhook_test.model_dump(), headers=logged_in_headers)
assert response.status_code == 201
assert response.json()["name"] == webhook_test.name

File diff suppressed because one or more lines are too long

28
tests/test_webhook.py Normal file
View file

@ -0,0 +1,28 @@
import tempfile
from pathlib import Path
def test_webhook_endpoint(client, added_webhook_test):
# The test is as follows:
# 1. The flow when run will get a "path" from the payload and save a file with the path as the name.
# We will create a temporary file path and send it to the webhook endpoint, then check if the file exists.
# 2. we will delete the file, then send an invalid payload to the webhook endpoint and check if the file exists.
endpoint_name = added_webhook_test["endpoint_name"]
endpoint = f"api/v1/webhook/{endpoint_name}"
# Create a temporary file
with tempfile.TemporaryDirectory() as tmp:
file_path = Path(tmp) / "test_file.txt"
payload = {"path": str(file_path)}
response = client.post(endpoint, json=payload)
assert response.status_code == 202
assert file_path.exists()
assert not file_path.exists()
# Send an invalid payload
payload = {"invalid_key": "invalid_value"}
response = client.post(endpoint, json=payload)
assert response.status_code == 202
assert not file_path.exists()