can debug when disable webhook

This commit is contained in:
hjlarry 2025-10-01 23:39:37 +08:00
parent 604651873e
commit e6a44a0860
3 changed files with 51 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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)