Re #257 - Handle bool val of json path better (#263)

* Re #257 - Handle bool val of json path better, with test
pull/266/head
dgtlmoon 3 years ago committed by GitHub
parent 3195ffa1c6
commit 6e4ddacaf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -45,7 +45,9 @@ def _parse_json(json_data, jsonpath_filter):
if len(match) == 1: if len(match) == 1:
s = match[0].value 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:', ''))) 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) stripped_text_from_html = json.dumps(s, indent=4)

@ -5,6 +5,10 @@ from flask import url_for
from . util import live_server_setup from . util import live_server_setup
import pytest import pytest
def test_setup(live_server):
live_server_setup(live_server)
def test_unittest_inline_html_extract(): def test_unittest_inline_html_extract():
# So lets pretend that the JSON we want is inside some HTML # So lets pretend that the JSON we want is inside some HTML
content=""" content="""
@ -60,7 +64,8 @@ def set_original_response():
], ],
"boss": { "boss": {
"name": "Fat guy" "name": "Fat guy"
} },
"available": true
} }
""" """
with open("test-datastore/endpoint-content.txt", "w") as f: with open("test-datastore/endpoint-content.txt", "w") as f:
@ -84,7 +89,8 @@ def set_modified_response():
], ],
"boss": { "boss": {
"name": "Foobar" "name": "Foobar"
} },
"available": false
} }
""" """
@ -93,11 +99,7 @@ def set_modified_response():
return None return None
def test_check_json_filter(client, live_server): def test_check_json_filter(client, live_server):
live_server_setup(live_server)
json_filter = 'json:boss.name' json_filter = 'json:boss.name'
set_original_response() 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")) 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 # 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 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

Loading…
Cancel
Save