update param / header parsing

This commit is contained in:
shamoon 2024-11-02 21:55:28 -07:00
parent 558a0296a4
commit 6d4f102696
No known key found for this signature in database
2 changed files with 31 additions and 24 deletions

View File

@ -1,4 +1,3 @@
import json
import logging import logging
import os import os
import shutil import shutil
@ -938,8 +937,8 @@ def run_workflows(
try: try:
params = {} params = {}
params_json = json.loads(action.webhook_params) try:
for key, value in params_json.items(): for key, value in action.webhook_params.items():
params[key] = parse_w_workflow_placeholders( params[key] = parse_w_workflow_placeholders(
value, value,
document.correspondent.name if document.correspondent else "", document.correspondent.name if document.correspondent else "",
@ -951,14 +950,17 @@ def run_workflows(
title, title,
doc_url, doc_url,
) )
headers = None except Exception as e:
logger.error(
f"Error occurred parsing webhook params: {e}",
extra={"group": logging_group},
)
headers = {}
if action.webhook_headers: if action.webhook_headers:
try: try:
# headers are a JSON object with key-value pairs, needs to be converted to a Mapping[str, str] headers = {
header_mapping = json.loads( str(k): str(v) for k, v in action.webhook_headers.items()
action.webhook_headers, }
)
headers = {str(k): str(v) for k, v in header_mapping.items()}
except Exception as e: except Exception as e:
logger.error( logger.error(
f"Error occurred parsing webhook headers: {e}", f"Error occurred parsing webhook headers: {e}",

View File

@ -2248,7 +2248,10 @@ class TestWorkflows(
) )
action = WorkflowAction.objects.create( action = WorkflowAction.objects.create(
type=WorkflowAction.WorkflowActionType.WEBHOOK, type=WorkflowAction.WorkflowActionType.WEBHOOK,
webhook_params='{"title": "Test webhook: {doc_title}", "body": "Test message: {doc_url}"}', webhook_params={
"title": "Test webhook: {doc_title}",
"body": "Test message: {doc_url}",
},
webhook_url="http://paperless-ngx.com", webhook_url="http://paperless-ngx.com",
webhook_include_document=True, webhook_include_document=True,
) )
@ -2274,11 +2277,11 @@ class TestWorkflows(
self.assertIn(expected_str, cm.output[0]) self.assertIn(expected_str, cm.output[0])
@mock.patch("httpx.post") @mock.patch("httpx.post")
def test_workflow_notification_action_url_invalid_headers(self, mock_post): def test_workflow_notification_action_url_invalid_params_headers(self, mock_post):
""" """
GIVEN: GIVEN:
- Document updated workflow with webhook action - Document updated workflow with webhook action
- Invalid headers JSON - Invalid params and headers JSON
WHEN: WHEN:
- Document that matches is updated - Document that matches is updated
THEN: THEN:
@ -2290,7 +2293,7 @@ class TestWorkflows(
action = WorkflowAction.objects.create( action = WorkflowAction.objects.create(
type=WorkflowAction.WorkflowActionType.WEBHOOK, type=WorkflowAction.WorkflowActionType.WEBHOOK,
webhook_url="http://paperless-ngx.com", webhook_url="http://paperless-ngx.com",
webhook_params='{"title": "Test webhook: {doc_title}", "body": "Test message: {doc_url}"}', webhook_params="invalid",
webhook_headers="invalid", webhook_headers="invalid",
) )
w = Workflow.objects.create( w = Workflow.objects.create(
@ -2310,5 +2313,7 @@ class TestWorkflows(
with self.assertLogs("paperless.handlers", level="ERROR") as cm: with self.assertLogs("paperless.handlers", level="ERROR") as cm:
run_workflows(WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED, doc) run_workflows(WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED, doc)
expected_str = "Error occurred parsing webhook headers" expected_str = "Error occurred parsing webhook params"
self.assertIn(expected_str, cm.output[0]) self.assertIn(expected_str, cm.output[0])
expected_str = "Error occurred parsing webhook headers"
self.assertIn(expected_str, cm.output[1])