From e6a44a086000a2a9f6688bc04948e811f5e3a479 Mon Sep 17 00:00:00 2001 From: hjlarry Date: Wed, 1 Oct 2025 23:39:37 +0800 Subject: [PATCH] can debug when disable webhook --- api/controllers/trigger/webhook.py | 15 +++++++--- api/services/webhook_service.py | 14 ++++++--- .../services/test_webhook_service.py | 30 +++++++++++++++++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/api/controllers/trigger/webhook.py b/api/controllers/trigger/webhook.py index 828103129d..85b27bda67 100644 --- a/api/controllers/trigger/webhook.py +++ b/api/controllers/trigger/webhook.py @@ -11,9 +11,16 @@ from services.webhook_service import WebhookService logger = logging.getLogger(__name__) -def _prepare_webhook_execution(webhook_id: str): - """Fetch trigger context, extract request data, and validate payload using unified processing.""" - webhook_trigger, workflow, node_config = WebhookService.get_webhook_trigger_and_workflow(webhook_id) +def _prepare_webhook_execution(webhook_id: str, is_debug: bool = False): + """Fetch trigger context, extract request data, and validate payload using unified processing. + + Args: + webhook_id: The webhook ID to process + is_debug: If True, skip status validation for debug mode + """ + webhook_trigger, workflow, node_config = WebhookService.get_webhook_trigger_and_workflow( + webhook_id, skip_status_check=is_debug + ) try: # Use new unified extraction and validation @@ -58,7 +65,7 @@ def handle_webhook(webhook_id: str): def handle_webhook_debug(webhook_id: str): """Handle webhook debug calls without triggering production workflow execution.""" try: - webhook_trigger, _, node_config, webhook_data, error = _prepare_webhook_execution(webhook_id) + webhook_trigger, _, node_config, webhook_data, error = _prepare_webhook_execution(webhook_id, is_debug=True) if error: return jsonify({"error": "Bad Request", "message": error}), 400 diff --git a/api/services/webhook_service.py b/api/services/webhook_service.py index de2f110abb..2e85431335 100644 --- a/api/services/webhook_service.py +++ b/api/services/webhook_service.py @@ -37,9 +37,14 @@ class WebhookService: @classmethod def get_webhook_trigger_and_workflow( - cls, webhook_id: str + cls, webhook_id: str, skip_status_check: bool = False ) -> tuple[WorkflowWebhookTrigger, Workflow, Mapping[str, Any]]: - """Get webhook trigger, workflow, and node configuration.""" + """Get webhook trigger, workflow, and node configuration. + + Args: + webhook_id: The webhook ID to look up + skip_status_check: If True, skip the enabled status check (for debug mode) + """ with Session(db.engine) as session: # Get webhook trigger webhook_trigger = ( @@ -48,7 +53,7 @@ class WebhookService: if not webhook_trigger: raise ValueError(f"Webhook not found: {webhook_id}") - # Check if the corresponding AppTrigger is enabled + # Check if the corresponding AppTrigger exists app_trigger = ( session.query(AppTrigger) .filter( @@ -62,7 +67,8 @@ class WebhookService: if not app_trigger: raise ValueError(f"App trigger not found for webhook {webhook_id}") - if app_trigger.status != AppTriggerStatus.ENABLED: + # Only check enabled status if not in debug mode + if not skip_status_check and app_trigger.status != AppTriggerStatus.ENABLED: raise ValueError(f"Webhook trigger is disabled for webhook {webhook_id}") # Get workflow diff --git a/api/tests/unit_tests/services/test_webhook_service.py b/api/tests/unit_tests/services/test_webhook_service.py index 0cb5b923a5..24375a6e7d 100644 --- a/api/tests/unit_tests/services/test_webhook_service.py +++ b/api/tests/unit_tests/services/test_webhook_service.py @@ -454,3 +454,33 @@ class TestWebhookServiceUnit: with pytest.raises(ValueError, match="HTTP method mismatch"): WebhookService.extract_and_validate_webhook_data(webhook_trigger, node_config) + + def test_debug_mode_parameter_handling(self): + """Test that the debug mode parameter is properly handled in _prepare_webhook_execution.""" + from controllers.trigger.webhook import _prepare_webhook_execution + + # Mock the WebhookService methods + with ( + patch.object(WebhookService, "get_webhook_trigger_and_workflow") as mock_get_trigger, + patch.object(WebhookService, "extract_and_validate_webhook_data") as mock_extract, + ): + mock_trigger = MagicMock() + mock_workflow = MagicMock() + mock_config = {"data": {"test": "config"}} + mock_data = {"test": "data"} + + mock_get_trigger.return_value = (mock_trigger, mock_workflow, mock_config) + mock_extract.return_value = mock_data + + # Test normal mode (skip_status_check=False) + result = _prepare_webhook_execution("test_webhook", is_debug=False) + mock_get_trigger.assert_called_with("test_webhook", skip_status_check=False) + assert result == (mock_trigger, mock_workflow, mock_config, mock_data, None) + + # Reset mock + mock_get_trigger.reset_mock() + + # Test debug mode (skip_status_check=True) + result = _prepare_webhook_execution("test_webhook", is_debug=True) + mock_get_trigger.assert_called_with("test_webhook", skip_status_check=True) + assert result == (mock_trigger, mock_workflow, mock_config, mock_data, None)