From 186c521ca62be06d4aee647046d64f1beae92145 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Mon, 27 Jun 2022 06:31:56 -0400 Subject: [PATCH] GET parms: title, type, format, and tag used if not in payload (#84) --- apprise_api/api/tests/test_notify.py | 93 +++++++++++++++++++ .../api/tests/test_stateless_notify.py | 3 + apprise_api/api/views.py | 29 ++++++ 3 files changed, 125 insertions(+) diff --git a/apprise_api/api/tests/test_notify.py b/apprise_api/api/tests/test_notify.py index 47c7262..203db56 100644 --- a/apprise_api/api/tests/test_notify.py +++ b/apprise_api/api/tests/test_notify.py @@ -24,6 +24,7 @@ # THE SOFTWARE. from django.test import SimpleTestCase, override_settings from unittest.mock import patch +import requests from ..forms import NotifyForm import json import apprise @@ -73,6 +74,98 @@ class NotifyTests(SimpleTestCase): assert response.status_code == 200 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') def test_partial_notify_by_loaded_urls(self, mock_notify): """ diff --git a/apprise_api/api/tests/test_stateless_notify.py b/apprise_api/api/tests/test_stateless_notify.py index 2005778..3345ee3 100644 --- a/apprise_api/api/tests/test_stateless_notify.py +++ b/apprise_api/api/tests/test_stateless_notify.py @@ -366,6 +366,9 @@ class StatelessNotifyTests(SimpleTestCase): # Ensure we're enabled for the purpose of our testing apprise.plugins.SCHEMA_MAP['json'].enabled = True + # Reset Mock + mock_send.reset_mock() + # Send our service with the `json://` denied with override_settings(APPRISE_ALLOW_SERVICES=""): with override_settings(APPRISE_DENY_SERVICES="json"): diff --git a/apprise_api/api/views.py b/apprise_api/api/views.py index f71679e..58c4157 100644 --- a/apprise_api/api/views.py +++ b/apprise_api/api/views.py @@ -471,6 +471,7 @@ class NotifyView(View): content = {} if MIME_IS_FORM.match(request.content_type): content = {} + form = NotifyForm(request.POST) if form.is_valid(): content.update(form.cleaned_data) @@ -502,6 +503,34 @@ class NotifyView(View): 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 if not content.get('body') or \ content.get('type', apprise.NotifyType.INFO) \