From d11877aa99aa905111ebef23486b22bbd59884f2 Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Fri, 14 Jun 2024 15:33:48 -0300 Subject: [PATCH] refactor: Add Webhook Test flow fixture --- tests/conftest.py | 29 ++++++++++++++++++++++++----- tests/data/WebhookTest.json | 1 + 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 tests/data/WebhookTest.json diff --git a/tests/conftest.py b/tests/conftest.py index 89bc8008c..780a2c7e1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -44,6 +44,7 @@ def pytest_configure(config): pytest.GROUPED_CHAT_EXAMPLE_PATH = data_path / "grouped_chat.json" pytest.ONE_GROUPED_CHAT_EXAMPLE_PATH = data_path / "one_group_chat.json" pytest.VECTOR_STORE_GROUPED_EXAMPLE_PATH = data_path / "vector_store_grouped.json" + pytest.WEBHOOK_TEST = data_path / "WebhookTest.json" pytest.BASIC_CHAT_WITH_PROMPT_AND_HISTORY = data_path / "BasicChatwithPromptandHistory.json" pytest.CHAT_INPUT = data_path / "ChatInputTest.json" @@ -221,6 +222,12 @@ def json_vector_store(): return f.read() +@pytest.fixture +def json_webhook_test(): + with open(pytest.WEBHOOK_TEST, "r") as f: + return f.read() + + @pytest.fixture(name="client", autouse=True) def client_fixture(session: Session, monkeypatch, request, load_flows_dir): # Set the database url to a test database @@ -274,7 +281,7 @@ def test_user(client): username="testuser", password="testpassword", ) - response = client.post("/api/v1/users", json=user_data.dict()) + response = client.post("/api/v1/users", json=user_data.model_dump()) assert response.status_code == 201 return response.json() @@ -341,7 +348,7 @@ def added_flow_with_prompt_and_history(client, json_flow_with_prompt_and_history flow = orjson.loads(json_flow_with_prompt_and_history) data = flow["data"] flow = FlowCreate(name="Basic Chat", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) + response = client.post("api/v1/flows/", json=flow.model_dump(), headers=logged_in_headers) assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data @@ -353,7 +360,7 @@ def added_flow_chat_input(client, json_chat_input, logged_in_headers): flow = orjson.loads(json_chat_input) data = flow["data"] flow = FlowCreate(name="Chat Input", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) + response = client.post("api/v1/flows/", json=flow.model_dump(), headers=logged_in_headers) assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data @@ -365,7 +372,7 @@ def added_flow_two_outputs(client, json_two_outputs, logged_in_headers): flow = orjson.loads(json_two_outputs) data = flow["data"] flow = FlowCreate(name="Two Outputs", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) + response = client.post("api/v1/flows/", json=flow.model_dump(), headers=logged_in_headers) assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data @@ -377,13 +384,25 @@ def added_vector_store(client, json_vector_store, logged_in_headers): vector_store = orjson.loads(json_vector_store) data = vector_store["data"] vector_store = FlowCreate(name="Vector Store", description="description", data=data) - response = client.post("api/v1/flows/", json=vector_store.dict(), headers=logged_in_headers) + response = client.post("api/v1/flows/", json=vector_store.model_dump(), headers=logged_in_headers) assert response.status_code == 201 assert response.json()["name"] == vector_store.name assert response.json()["data"] == vector_store.data return response.json() +@pytest.fixture +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) + 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 + assert response.json()["data"] == webhook_test.data + return response.json() + + @pytest.fixture def created_api_key(active_user): hashed = get_password_hash("random_key") diff --git a/tests/data/WebhookTest.json b/tests/data/WebhookTest.json new file mode 100644 index 000000000..71a7141f4 --- /dev/null +++ b/tests/data/WebhookTest.json @@ -0,0 +1 @@ +{"id":"40b2ae66-384b-4978-85ab-f79706287a1a","data":{"nodes":[{"id":"Webhook-8rYwr","type":"genericNode","position":{"x":375.90734417043643,"y":261.63442970852185},"data":{"type":"Webhook","node":{"template":{"_type":"CustomComponent","code":{"type":"code","required":true,"placeholder":"","list":false,"show":true,"multiline":true,"value":"import json\nimport uuid\nfrom typing import Any, Optional\n\nfrom langflow.custom import CustomComponent\nfrom langflow.schema import Data\nfrom langflow.schema.dotdict import dotdict\n\n\nclass WebhookComponent(CustomComponent):\n display_name = \"Webhook Input\"\n description = \"Defines a webhook input for the flow.\"\n\n def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None):\n if field_name == \"webhook_id\":\n build_config[\"webhook_id\"][\"value\"] = uuid.uuid4().hex\n return build_config\n\n def build_config(self):\n return {\n \"data\": {\n \"display_name\": \"Data\",\n \"info\": \"Use this field to quickly test the webhook component by providing a JSON payload.\",\n \"multiline\": True,\n }\n }\n\n def build(self, data: Optional[str] = \"\") -> Data:\n message = \"\"\n try:\n body = json.loads(data or \"{}\")\n except json.JSONDecodeError:\n body = {\"payload\": data}\n message = f\"Invalid JSON payload. Please check the format.\\n\\n{data}\"\n record = Data(data=body)\n if not message:\n message = record\n self.status = message\n return record\n","fileTypes":[],"file_path":"","password":false,"name":"code","advanced":true,"dynamic":true,"info":"","load_from_db":false,"title_case":false},"data":{"type":"str","required":false,"placeholder":"","list":false,"show":true,"multiline":true,"value":"{\"bacon\":1}","fileTypes":[],"file_path":"","password":false,"name":"data","display_name":"Data","advanced":false,"dynamic":false,"info":"Use this field to quickly test the webhook component by providing a JSON payload.","load_from_db":false,"title_case":false,"input_types":["Text"]}},"description":"Defines a webhook input for the flow.","base_classes":["Data"],"display_name":"Webhook Input","documentation":"","custom_fields":{"data":null},"output_types":["Data"],"pinned":false,"conditional_paths":[],"frozen":false,"outputs":[{"types":["Data"],"selected":"Data","name":"data","hidden":false,"display_name":"Data","method":null,"value":"__UNDEFINED__","cache":true}],"field_order":[],"beta":false,"edited":true},"id":"Webhook-8rYwr","description":"Defines a webhook input for the flow.","display_name":"Webhook Input","edited":false},"selected":false,"width":384,"height":309,"dragging":false,"positionAbsolute":{"x":375.90734417043643,"y":261.63442970852185}},{"id":"CustomComponent-xbSW2","type":"genericNode","position":{"x":888.0012384532345,"y":274.9520639008431},"data":{"type":"CustomComponent","node":{"template":{"_type":"Component","code":{"type":"code","required":true,"placeholder":"","list":false,"show":true,"multiline":true,"value":"# from langflow.field_typing import Data\nfrom langflow.custom import Component\nfrom langflow.inputs import StrInput\nfrom langflow.schema import Data\nfrom langflow.template import Output\n\n\nclass CustomComponent(Component):\n display_name = \"Custom Component\"\n description = \"Use as a template to create your own component.\"\n documentation: str = \"http://docs.langflow.org/components/custom\"\n icon = \"custom_components\"\n\n inputs = [\n StrInput(name=\"input_value\", display_name=\"Input Value\", value=\"Hello, World!\", input_types=[\"Data\"]),\n ]\n\n outputs = [\n Output(display_name=\"Output\", name=\"output\", method=\"build_output\"),\n ]\n\n def build_output(self) -> Data:\n if isinstance(self.input_value, Data):\n data = self.input_value\n else:\n data = Data(value=self.input_value)\n \n if \"path\" in data:\n path = self.resolve_path(data.path)\n path_obj = Path(path)\n with open(path, \"w\") as f:\n f.write(data.model_dump())\n self.status = data\n return data\n","fileTypes":[],"file_path":"","password":false,"name":"code","advanced":true,"dynamic":true,"info":"","load_from_db":false,"title_case":false},"input_value":{"load_from_db":false,"list":false,"required":false,"placeholder":"","show":true,"value":"","name":"input_value","display_name":"Input Value","advanced":false,"input_types":["Data"],"dynamic":false,"info":"","title_case":false,"type":"str"}},"description":"Use as a template to create your own component.","icon":"custom_components","base_classes":["Data"],"display_name":"Custom Component","documentation":"http://docs.langflow.org/components/custom","custom_fields":{},"output_types":[],"pinned":false,"conditional_paths":[],"frozen":false,"outputs":[{"types":["Data"],"selected":"Data","name":"output","display_name":"Output","method":"build_output","value":"__UNDEFINED__","cache":true}],"field_order":["input_value"],"beta":false,"edited":true},"id":"CustomComponent-xbSW2","description":"Use as a template to create your own component.","display_name":"Custom Component"},"selected":false,"width":384,"height":337,"positionAbsolute":{"x":888.0012384532345,"y":274.9520639008431},"dragging":false}],"edges":[{"source":"Webhook-8rYwr","sourceHandle":"{œdataTypeœ:œWebhookœ,œidœ:œWebhook-8rYwrœ,œnameœ:œdataœ,œoutput_typesœ:[œDataœ]}","target":"CustomComponent-xbSW2","targetHandle":"{œfieldNameœ:œinput_valueœ,œidœ:œCustomComponent-xbSW2œ,œinputTypesœ:[œDataœ],œtypeœ:œstrœ}","data":{"targetHandle":{"fieldName":"input_value","id":"CustomComponent-xbSW2","inputTypes":["Data"],"type":"str"},"sourceHandle":{"dataType":"Webhook","id":"Webhook-8rYwr","name":"data","output_types":["Data"]}},"id":"reactflow__edge-Webhook-8rYwr{œdataTypeœ:œWebhookœ,œidœ:œWebhook-8rYwrœ,œnameœ:œdataœ,œoutput_typesœ:[œDataœ]}-CustomComponent-xbSW2{œfieldNameœ:œinput_valueœ,œidœ:œCustomComponent-xbSW2œ,œinputTypesœ:[œDataœ],œtypeœ:œstrœ}"}],"viewport":{"x":-150.0437656191234,"y":128.9195326354088,"zoom":0.7432914922155076}},"description":"The Power of Language at Your Fingertips.","name":"Webhook Test","last_tested_version":"1.0.0a52","is_component":false} \ No newline at end of file