GET parms: title, type, format, and tag used if not in payload (#84)

This commit is contained in:
Chris Caron
2022-06-27 06:31:56 -04:00
committed by GitHub
parent d7824e24c7
commit 186c521ca6
3 changed files with 125 additions and 0 deletions

View File

@ -24,6 +24,7 @@
# THE SOFTWARE. # THE SOFTWARE.
from django.test import SimpleTestCase, override_settings from django.test import SimpleTestCase, override_settings
from unittest.mock import patch from unittest.mock import patch
import requests
from ..forms import NotifyForm from ..forms import NotifyForm
import json import json
import apprise import apprise
@ -73,6 +74,98 @@ class NotifyTests(SimpleTestCase):
assert response.status_code == 200 assert response.status_code == 200
assert mock_notify.call_count == 1 assert mock_notify.call_count == 1
@patch('requests.post')
def test_notify_with_tags(self, mock_post):
"""
Test notification handling when setting tags
"""
# Disable Throttling to speed testing
apprise.plugins.NotifyBase.request_rate_per_sec = 0
# Ensure we're enabled for the purpose of our testing
apprise.plugins.SCHEMA_MAP['json'].enabled = True
# Prepare our response
response = requests.Request()
response.status_code = requests.codes.ok
mock_post.return_value = response
# our key to use
key = 'test_notify_with_tags'
# Valid Yaml Configuration
config = """
urls:
- json://user:pass@localhost:
tag: home
"""
# Load our configuration (it will be detected as YAML)
response = self.client.post(
'/add/{}'.format(key),
{'config': config})
assert response.status_code == 200
# Preare our form data
form_data = {
'body': 'test notifiction',
'type': apprise.NotifyType.INFO,
'format': apprise.NotifyFormat.TEXT,
}
# Send our notification
response = self.client.post(
'/notify/{}'.format(key), form_data)
# Nothing could be notified as there were no tag matches
assert response.status_code == 424
assert mock_post.call_count == 0
# Now let's send our notification by specifying the tag in the
# parameters
response = self.client.post(
'/notify/{}?tag=home'.format(key), form_data)
# Our notification was sent
assert response.status_code == 200
assert mock_post.call_count == 1
# Test our posted data
response = json.loads(mock_post.call_args_list[0][1]['data'])
assert response['title'] == ''
assert response['message'] == form_data['body']
assert response['type'] == apprise.NotifyType.INFO
# Preare our form data (body is actually the minimum requirement)
# All of the rest of the variables can actually be over-ridden
# by the GET Parameter (ONLY if not otherwise identified in the
# payload). The Payload contents of the POST request always take
# priority to eliminate any ambiguity
form_data = {
'body': 'test notifiction',
}
# Reset our count
mock_post.reset_mock()
# Send our notification by specifying the tag in the parameters
response = self.client.post(
'/notify/{}?tag=home&format={}&type={}&title={}&body=ignored'
.format(
key, apprise.NotifyFormat.TEXT,
apprise.NotifyType.WARNING, "Test Title"),
form_data,
content_type='application/json')
# Our notification was sent
assert response.status_code == 200
assert mock_post.call_count == 1
response = json.loads(mock_post.call_args_list[0][1]['data'])
assert response['title'] == "Test Title"
assert response['message'] == form_data['body']
assert response['type'] == apprise.NotifyType.WARNING
@patch('apprise.NotifyBase.notify') @patch('apprise.NotifyBase.notify')
def test_partial_notify_by_loaded_urls(self, mock_notify): def test_partial_notify_by_loaded_urls(self, mock_notify):
""" """

View File

@ -366,6 +366,9 @@ class StatelessNotifyTests(SimpleTestCase):
# Ensure we're enabled for the purpose of our testing # Ensure we're enabled for the purpose of our testing
apprise.plugins.SCHEMA_MAP['json'].enabled = True apprise.plugins.SCHEMA_MAP['json'].enabled = True
# Reset Mock
mock_send.reset_mock()
# Send our service with the `json://` denied # Send our service with the `json://` denied
with override_settings(APPRISE_ALLOW_SERVICES=""): with override_settings(APPRISE_ALLOW_SERVICES=""):
with override_settings(APPRISE_DENY_SERVICES="json"): with override_settings(APPRISE_DENY_SERVICES="json"):

View File

@ -471,6 +471,7 @@ class NotifyView(View):
content = {} content = {}
if MIME_IS_FORM.match(request.content_type): if MIME_IS_FORM.match(request.content_type):
content = {} content = {}
form = NotifyForm(request.POST) form = NotifyForm(request.POST)
if form.is_valid(): if form.is_valid():
content.update(form.cleaned_data) content.update(form.cleaned_data)
@ -502,6 +503,34 @@ class NotifyView(View):
status=status, status=status,
) )
#
# Allow 'tag' value to be specified as part of the URL parameters
# if not found otherwise defined.
#
if not content.get('tag') and 'tag' in request.GET:
content['tag'] = request.GET['tag']
#
# Allow 'format' value to be specified as part of the URL
# parameters if not found otherwise defined.
#
if not content.get('format') and 'format' in request.GET:
content['format'] = request.GET['format']
#
# Allow 'type' value to be specified as part of the URL parameters
# if not found otherwise defined.
#
if not content.get('type') and 'type' in request.GET:
content['type'] = request.GET['type']
#
# Allow 'title' value to be specified as part of the URL parameters
# if not found otherwise defined.
#
if not content.get('title') and 'title' in request.GET:
content['title'] = request.GET['title']
# Some basic error checking # Some basic error checking
if not content.get('body') or \ if not content.get('body') or \
content.get('type', apprise.NotifyType.INFO) \ content.get('type', apprise.NotifyType.INFO) \