Skip to content

Commit

Permalink
Notifications - Custom POST:// GET:// etc endpoints - returning 204 a…
Browse files Browse the repository at this point in the history
…nd other 20x responses are OK (don't show an error was detected)(#2897)
  • Loading branch information
dgtlmoon authored Jan 13, 2025
1 parent 1c1f1c6 commit 8960f40
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 3 additions & 5 deletions changedetectionio/apprise_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ def apprise_custom_api_call_wrapper(body, title, notify_type, *args, **kwargs):
method = re.sub(rf's$', '', schema)
requests_method = getattr(requests, method)

headers = CaseInsensitiveDict({})
params = CaseInsensitiveDict({}) # Added to requests
auth = None
has_error = False


# Convert /foobar?+some-header=hello to proper header dictionary
results = apprise_parse_url(url)

# Add our headers that the user can potentially over-ride if they wish
# to to our returned result set and tidy entries by unquoting them
headers = {unquote_plus(x): unquote_plus(y)
for x, y in results['qsd+'].items()}
headers = CaseInsensitiveDict({unquote_plus(x): unquote_plus(y)
for x, y in results['qsd+'].items()})

# https://github.com/caronc/apprise/wiki/Notify_Custom_JSON#get-parameter-manipulation
# In Apprise, it relies on prefixing each request arg with "-", because it uses say &method=update as a flag for apprise
Expand Down Expand Up @@ -81,7 +79,7 @@ def apprise_custom_api_call_wrapper(body, title, notify_type, *args, **kwargs):
params=params
)

if r.status_code not in (requests.codes.created, requests.codes.ok):
if not (200 <= r.status_code < 300):
status_str = f"Error sending '{method.upper()}' request to {url} - Status: {r.status_code}: '{r.reason}'"
logger.error(status_str)
has_error = True
Expand Down
16 changes: 14 additions & 2 deletions changedetectionio/tests/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_check_notification(client, live_server, measure_memory_usage):

# Re 360 - new install should have defaults set
res = client.get(url_for("settings_page"))
notification_url = url_for('test_notification_endpoint', _external=True).replace('http', 'json')
notification_url = url_for('test_notification_endpoint', _external=True).replace('http', 'json')+"?status_code=204"

assert default_notification_body.encode() in res.data
assert default_notification_title.encode() in res.data
Expand Down Expand Up @@ -135,7 +135,14 @@ def test_check_notification(client, live_server, measure_memory_usage):

# Trigger a check
client.get(url_for("form_watch_checknow"), follow_redirects=True)
wait_for_all_checks(client)
time.sleep(3)

# Check no errors were recorded
res = client.get(url_for("index"))
assert b'notification-error' not in res.data


# Verify what was sent as a notification, this file should exist
with open("test-datastore/notification.txt", "r") as f:
notification_submission = f.read()
Expand Down Expand Up @@ -284,7 +291,7 @@ def test_notification_custom_endpoint_and_jinja2(client, live_server, measure_me
# CUSTOM JSON BODY CHECK for POST://
set_original_response()
# https://github.com/caronc/apprise/wiki/Notify_Custom_JSON#header-manipulation
test_notification_url = url_for('test_notification_endpoint', _external=True).replace('http://', 'post://')+"?xxx={{ watch_url }}&+custom-header=123&+second=hello+world%20%22space%22"
test_notification_url = url_for('test_notification_endpoint', _external=True).replace('http://', 'post://')+"?status_code=204&xxx={{ watch_url }}&+custom-header=123&+second=hello+world%20%22space%22"

res = client.post(
url_for("settings_page"),
Expand Down Expand Up @@ -319,6 +326,11 @@ def test_notification_custom_endpoint_and_jinja2(client, live_server, measure_me

time.sleep(2) # plus extra delay for notifications to fire


# Check no errors were recorded, because we asked for 204 which is slightly uncommon but is still OK
res = client.get(url_for("index"))
assert b'notification-error' not in res.data

with open("test-datastore/notification.txt", 'r') as f:
x = f.read()
j = json.loads(x)
Expand Down
5 changes: 4 additions & 1 deletion changedetectionio/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,11 @@ def test_notification_endpoint():
f.write(request.content_type)

print("\n>> Test notification endpoint was hit.\n", data)
return "Text was set"

content = "Text was set"
status_code = request.args.get('status_code',200)
resp = make_response(content, status_code)
return resp

# Just return the verb in the request
@live_server.app.route('/test-basicauth', methods=['GET'])
Expand Down

0 comments on commit 8960f40

Please sign in to comment.