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 os
import shutil
@ -938,27 +937,30 @@ def run_workflows(
try:
params = {}
params_json = json.loads(action.webhook_params)
for key, value in params_json.items():
params[key] = parse_w_workflow_placeholders(
value,
document.correspondent.name if document.correspondent else "",
document.document_type.name if document.document_type else "",
document.owner.username if document.owner else "",
timezone.localtime(document.added),
document.original_filename or "",
timezone.localtime(document.created),
title,
doc_url,
try:
for key, value in action.webhook_params.items():
params[key] = parse_w_workflow_placeholders(
value,
document.correspondent.name if document.correspondent else "",
document.document_type.name if document.document_type else "",
document.owner.username if document.owner else "",
timezone.localtime(document.added),
document.original_filename or "",
timezone.localtime(document.created),
title,
doc_url,
)
except Exception as e:
logger.error(
f"Error occurred parsing webhook params: {e}",
extra={"group": logging_group},
)
headers = None
headers = {}
if action.webhook_headers:
try:
# headers are a JSON object with key-value pairs, needs to be converted to a Mapping[str, str]
header_mapping = json.loads(
action.webhook_headers,
)
headers = {str(k): str(v) for k, v in header_mapping.items()}
headers = {
str(k): str(v) for k, v in action.webhook_headers.items()
}
except Exception as e:
logger.error(
f"Error occurred parsing webhook headers: {e}",

View File

@ -2248,7 +2248,10 @@ class TestWorkflows(
)
action = WorkflowAction.objects.create(
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_include_document=True,
)
@ -2274,11 +2277,11 @@ class TestWorkflows(
self.assertIn(expected_str, cm.output[0])
@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:
- Document updated workflow with webhook action
- Invalid headers JSON
- Invalid params and headers JSON
WHEN:
- Document that matches is updated
THEN:
@ -2290,7 +2293,7 @@ class TestWorkflows(
action = WorkflowAction.objects.create(
type=WorkflowAction.WorkflowActionType.WEBHOOK,
webhook_url="http://paperless-ngx.com",
webhook_params='{"title": "Test webhook: {doc_title}", "body": "Test message: {doc_url}"}',
webhook_params="invalid",
webhook_headers="invalid",
)
w = Workflow.objects.create(
@ -2310,5 +2313,7 @@ class TestWorkflows(
with self.assertLogs("paperless.handlers", level="ERROR") as cm:
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])
expected_str = "Error occurred parsing webhook headers"
self.assertIn(expected_str, cm.output[1])