From 7e7627670352ba4812e557552e4b239d69805eb0 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 5 Dec 2022 17:54:03 +0100 Subject: [PATCH] Adding test, fixing notification url --- changedetectionio/notification.py | 2 +- changedetectionio/tests/test_notification.py | 42 ++++++++++++++++++++ changedetectionio/tests/util.py | 3 ++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/changedetectionio/notification.py b/changedetectionio/notification.py index 1a328e3e..ac0df079 100644 --- a/changedetectionio/notification.py +++ b/changedetectionio/notification.py @@ -30,7 +30,6 @@ valid_notification_formats = { def process_notification(n_object, datastore): - # Insert variables into the notification content notification_parameters = create_notification_parameters(n_object, datastore) @@ -50,6 +49,7 @@ def process_notification(n_object, datastore): sent_objs=[] from .apprise_asset import asset for url in n_object['notification_urls']: + url = jinja2_env.from_string(url).render(**notification_parameters) apobj = apprise.Apprise(debug=True, asset=asset) url = url.strip() if len(url): diff --git a/changedetectionio/tests/test_notification.py b/changedetectionio/tests/test_notification.py index 5c46d9bf..aae88768 100644 --- a/changedetectionio/tests/test_notification.py +++ b/changedetectionio/tests/test_notification.py @@ -301,3 +301,45 @@ def test_notification_validation(client, live_server): url_for("form_delete", uuid="all"), follow_redirects=True ) + +def test_notification_jinaj2(client, live_server): + live_server_setup(live_server) + time.sleep(1) + + # test_endpoint - that sends the contents of a file + # test_notification_endpoint - that takes a POST and writes it to file (test-datastore/notification.txt) + + test_notification_url = url_for('test_notification_endpoint', _external=True).replace('http://', 'json://') + res = client.post( + url_for("settings_page"), + data={"application-notification_title": "New ChangeDetection.io Notification - {{ watch_url }}", + "application-notification_body": "Got {{ watch_url }}\n", + # https://github.com/caronc/apprise/wiki/Notify_Custom_JSON#get-parameter-manipulation + "application-notification_urls": test_notification_url+"?-XXX={{ watch_url }}", + "application-minutes_between_check": 180, + "application-fetch_backend": "html_requests" + }, + follow_redirects=True + ) + + test_url = url_for('test_endpoint', _external=True) + res = client.post( + url_for("form_quick_watch_add"), + data={"url": test_url, "tag": 'nice one'}, + follow_redirects=True + ) + + assert b"Watch added" in res.data + time.sleep(2) + set_more_modified_response() + client.get(url_for("form_watch_checknow"), follow_redirects=True) + time.sleep(3) + + + assert os.path.isfile("test-datastore/notification-url.txt") + + with open("test-datastore/notification-url.txt", 'r') as f: + notification = f.read() + + assert 'XXX=http' in notification + os.unlink("test-datastore/notification-url.txt") \ No newline at end of file diff --git a/changedetectionio/tests/util.py b/changedetectionio/tests/util.py index 39ee4166..fc23c0dd 100644 --- a/changedetectionio/tests/util.py +++ b/changedetectionio/tests/util.py @@ -149,6 +149,9 @@ def live_server_setup(live_server): if data != None: f.write(data) + with open("test-datastore/notification-url.txt", "w") as f: + f.write(request.url) + print("\n>> Test notification endpoint was hit.\n", data) return "Text was set"