Bug fix for alerting when xPath based filters are no longer present (#772)

refactor/regex-text-extract
dgtlmoon 2 years ago committed by GitHub
parent a82fad7059
commit 291700554e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,7 +21,7 @@ def css_filter(css_filter, html_content):
soup = BeautifulSoup(html_content, "html.parser") soup = BeautifulSoup(html_content, "html.parser")
html_block = "" html_block = ""
r = soup.select(css_filter, separator="") r = soup.select(css_filter, separator="")
if len(r) == 0: if len(html_content) > 0 and len(r) == 0:
raise FilterNotFoundInResponse(css_filter) raise FilterNotFoundInResponse(css_filter)
for item in r: for item in r:
html_block += str(item) html_block += str(item)
@ -49,8 +49,8 @@ def xpath_filter(xpath_filter, html_content):
html_block = "" html_block = ""
r = tree.xpath(xpath_filter.strip(), namespaces={'re': 'http://exslt.org/regular-expressions'}) r = tree.xpath(xpath_filter.strip(), namespaces={'re': 'http://exslt.org/regular-expressions'})
if len(r) == 0: if len(html_content) > 0 and len(r) == 0:
raise FilterNotFoundInResponse(css_filter) raise FilterNotFoundInResponse(xpath_filter)
for item in r: for item in r:
html_block += etree.tostring(item, pretty_print=True).decode('utf-8') + "<br/>" html_block += etree.tostring(item, pretty_print=True).decode('utf-8') + "<br/>"

@ -22,12 +22,7 @@ def set_response_with_filter():
f.write(test_return_data) f.write(test_return_data)
return None return None
def run_filter_test(client, content_filter):
# Hard to just add more live server URLs when one test is already running (I think)
# So we add our test here (was in a different file)
def test_check_notification(client, live_server):
live_server_setup(live_server)
set_original_response()
# Give the endpoint time to spin up # Give the endpoint time to spin up
time.sleep(1) time.sleep(1)
@ -72,7 +67,7 @@ def test_check_notification(client, live_server):
"tag": "my tag", "tag": "my tag",
"title": "my title", "title": "my title",
"headers": "", "headers": "",
"css_filter": '#nope-doesnt-exist', "css_filter": content_filter,
"fetch_backend": "html_requests"}) "fetch_backend": "html_requests"})
res = client.post( res = client.post(
@ -99,7 +94,7 @@ def test_check_notification(client, live_server):
with open("test-datastore/notification.txt", 'r') as f: with open("test-datastore/notification.txt", 'r') as f:
notification = f.read() notification = f.read()
assert 'CSS/xPath filter was not present in the page' in notification assert 'CSS/xPath filter was not present in the page' in notification
assert '#nope-doesnt-exist' in notification assert content_filter.replace('"', '\\"') in notification
# Remove it and prove that it doesnt trigger when not expected # Remove it and prove that it doesnt trigger when not expected
os.unlink("test-datastore/notification.txt") os.unlink("test-datastore/notification.txt")
@ -121,3 +116,19 @@ def test_check_notification(client, live_server):
url_for("form_delete", uuid="all"), url_for("form_delete", uuid="all"),
follow_redirects=True follow_redirects=True
) )
os.unlink("test-datastore/notification.txt")
def test_setup(live_server):
live_server_setup(live_server)
def test_check_css_filter_failure_notification(client, live_server):
set_original_response()
time.sleep(1)
run_filter_test(client, '#nope-doesnt-exist')
def test_check_xpath_filter_failure_notification(client, live_server):
set_original_response()
time.sleep(1)
run_filter_test(client, '//*[@id="nope-doesnt-exist"]')

Loading…
Cancel
Save