From ea4cd294c176c6bd0fdccba15121bd6c6851e548 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 27 Aug 2023 19:16:39 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fendpoints.py):=20fix?= =?UTF-8?q?=20typo=20in=20function=20name=20'created=5Fapi=5Fkey'=20to=20'?= =?UTF-8?q?create=5Fapi=5Fkey'=20for=20consistency=20=E2=9C=A8=20feat(test?= =?UTF-8?q?=5Fendpoints.py):=20add=20test=20case=20for=20handling=20invali?= =?UTF-8?q?d=20API=20key=20in=20'test=5Fprocess=5Fflow=5Finvalid=5Fapi=5Fk?= =?UTF-8?q?ey'=20function=20=E2=9C=A8=20feat(test=5Fendpoints.py):=20add?= =?UTF-8?q?=20test=20case=20for=20handling=20invalid=20flow=20ID=20in=20't?= =?UTF-8?q?est=5Fprocess=5Fflow=5Finvalid=5Fid'=20function=20=E2=9C=A8=20f?= =?UTF-8?q?eat(test=5Fendpoints.py):=20add=20test=20case=20for=20handling?= =?UTF-8?q?=20flow=20not=20found=20in=20'test=5Fprocess=5Fflow=5Finvalid?= =?UTF-8?q?=5Fid'=20function=20=E2=9C=A8=20feat(test=5Fendpoints.py):=20ad?= =?UTF-8?q?d=20test=20case=20for=20testing=20process=20flow=20without=20au?= =?UTF-8?q?tologin=20in=20'test=5Fprocess=5Fflow=5Fwithout=5Fautologin'=20?= =?UTF-8?q?function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_endpoints.py | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index f8596f9c9..cbb1eb08c 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -1,3 +1,4 @@ +import uuid from langflow.services.auth.utils import get_password_hash from langflow.services.database.models.api_key.api_key import ApiKey from langflow.services.utils import get_settings_manager @@ -102,6 +103,59 @@ def created_api_key(session, active_user): return api_key +def test_process_flow_invalid_api_key(client, flow, monkeypatch): + # Mock de process_graph_cached + def mock_process_graph_cached(*args, **kwargs): + return {}, "session_id_mock" + + settings_manager = get_settings_manager() + settings_manager.auth_settings.AUTO_LOGIN = False + from langflow.api.v1 import endpoints + + monkeypatch.setattr(endpoints, "process_graph_cached", mock_process_graph_cached) + + headers = {"api-key": "invalid_api_key"} + + post_data = { + "inputs": {"key": "value"}, + "tweaks": None, + "clear_cache": False, + "session_id": None, + } + + response = client.post(f"api/v1/process/{flow.id}", headers=headers, json=post_data) + + assert response.status_code == 403 + assert response.json() == {"detail": "Invalid or missing API key"} + + +def test_process_flow_invalid_id(client, monkeypatch, created_api_key): + def mock_process_graph_cached(*args, **kwargs): + return {}, "session_id_mock" + + from langflow.api.v1 import endpoints + + monkeypatch.setattr(endpoints, "process_graph_cached", mock_process_graph_cached) + + api_key = created_api_key.api_key + headers = {"api-key": api_key} + + post_data = { + "inputs": {"key": "value"}, + "tweaks": None, + "clear_cache": False, + "session_id": None, + } + + invalid_id = uuid.uuid4() + response = client.post( + f"api/v1/process/{invalid_id}", headers=headers, json=post_data + ) + + assert response.status_code == 404 + assert f"Flow {invalid_id} not found" in response.json()["detail"] + + def test_process_flow_without_autologin(client, flow, monkeypatch, created_api_key): # Mock de process_graph_cached from langflow.api.v1 import endpoints