From 6e4ddacaf8dfc6964c4a873080fd7d9a90fec6fb Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Thu, 21 Oct 2021 23:25:38 +0200 Subject: [PATCH] Re #257 - Handle bool val of json path better (#263) * Re #257 - Handle bool val of json path better, with test --- changedetectionio/html_tools.py | 4 +- .../tests/test_jsonpath_selector.py | 64 +++++++++++++++++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/changedetectionio/html_tools.py b/changedetectionio/html_tools.py index 16fbf8e8..c905f677 100644 --- a/changedetectionio/html_tools.py +++ b/changedetectionio/html_tools.py @@ -45,7 +45,9 @@ def _parse_json(json_data, jsonpath_filter): if len(match) == 1: s = match[0].value - if not s: + # Re #257 - Better handling where it does not exist, in the case the original 's' value was False.. + if not match: + # Maybe better to just allow it through, maybe they are waiting on a value to become available? raise JSONNotFound("No Matching JSON could be found for the rule {}".format(jsonpath_filter.replace('json:', ''))) stripped_text_from_html = json.dumps(s, indent=4) diff --git a/changedetectionio/tests/test_jsonpath_selector.py b/changedetectionio/tests/test_jsonpath_selector.py index 91c46764..30dd59e8 100644 --- a/changedetectionio/tests/test_jsonpath_selector.py +++ b/changedetectionio/tests/test_jsonpath_selector.py @@ -5,6 +5,10 @@ from flask import url_for from . util import live_server_setup import pytest + +def test_setup(live_server): + live_server_setup(live_server) + def test_unittest_inline_html_extract(): # So lets pretend that the JSON we want is inside some HTML content=""" @@ -60,7 +64,8 @@ def set_original_response(): ], "boss": { "name": "Fat guy" - } + }, + "available": true } """ with open("test-datastore/endpoint-content.txt", "w") as f: @@ -84,7 +89,8 @@ def set_modified_response(): ], "boss": { "name": "Foobar" - } + }, + "available": false } """ @@ -93,11 +99,7 @@ def set_modified_response(): return None - - def test_check_json_filter(client, live_server): - live_server_setup(live_server) - json_filter = 'json:boss.name' set_original_response() @@ -161,3 +163,53 @@ def test_check_json_filter(client, live_server): res = client.get(url_for("diff_history_page", uuid="first")) # But the change should be there, tho its hard to test the change was detected because it will show old and new versions assert b'Foobar' in res.data + + +def test_check_json_filter_bool_val(client, live_server): + json_filter = "json:$['available']" + + set_original_response() + + # Give the endpoint time to spin up + time.sleep(1) + + test_url = url_for('test_endpoint', _external=True) + + res = client.post( + url_for("import_page"), + data={"urls": test_url}, + follow_redirects=True + ) + assert b"1 Imported" in res.data + + # Goto the edit page, add our ignore text + # Add our URL to the import page + res = client.post( + url_for("edit_page", uuid="first"), + data={"css_filter": json_filter, + "url": test_url, + "tag": "", + "headers": "", + "fetch_backend": "html_requests" + }, + follow_redirects=True + ) + assert b"Updated watch." in res.data + + + # Trigger a check + client.get(url_for("api_watch_checknow"), follow_redirects=True) + + # Give the thread time to pick it up + time.sleep(3) + # Make a change + set_modified_response() + + # Trigger a check + client.get(url_for("api_watch_checknow"), follow_redirects=True) + # Give the thread time to pick it up + time.sleep(3) + + res = client.get(url_for("diff_history_page", uuid="first")) + # But the change should be there, tho its hard to test the change was detected because it will show old and new versions + assert b'false' in res.data