better stateless support

This commit is contained in:
Chris Caron
2019-10-28 21:54:51 -04:00
parent 2f21c0761d
commit 9a3648ca30
7 changed files with 291 additions and 47 deletions

View File

@@ -40,6 +40,10 @@ NOTIFICATION_TYPES = (
(apprise.NotifyType.FAILURE, _('Failure')),
)
URLS_MAX_LEN = 1024
URLS_PLACEHOLDER = 'mailto://user:pass@domain.com, ' \
'slack://tokena/tokenb/tokenc, ...'
class AddByUrlForm(forms.Form):
"""
@@ -51,11 +55,8 @@ class AddByUrlForm(forms.Form):
"""
urls = forms.CharField(
label=_('URLs'),
widget=forms.TextInput(
attrs={
'placeholder': 'mailto://user:pass@domain.com, '
'slack://tokena/tokenb/tokenc, ...'}),
max_length=1024,
widget=forms.TextInput(attrs={'placeholder': URLS_PLACEHOLDER}),
max_length=URLS_MAX_LEN,
)
@@ -121,17 +122,14 @@ class NotifyForm(forms.Form):
return data
class NotifyByUrlForm(AddByUrlForm, NotifyForm):
class NotifyByUrlForm(NotifyForm):
"""
Same as the NotifyForm but additionally processes a string of URLs to
notify directly.
"""
pass
class NotifyByConfigForm(AddByConfigForm, NotifyForm):
"""
Same as the NotifyForm but additionally process a configuration file as
well.
"""
pass
urls = forms.CharField(
label=_('URLs'),
widget=forms.TextInput(attrs={'placeholder': URLS_PLACEHOLDER}),
max_length=URLS_MAX_LEN,
required=False,
)

View File

@@ -0,0 +1,177 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 Chris Caron <lead2gold@gmail.com>
# All rights reserved.
#
# This code is licensed under the MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files(the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions :
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from django.test import SimpleTestCase
from django.test.utils import override_settings
from unittest.mock import patch
from ..forms import NotifyByUrlForm
import json
import apprise
class StatelessNotifyTests(SimpleTestCase):
"""
Test stateless notifications
"""
@patch('apprise.Apprise.notify')
def test_notify(self, mock_notify):
"""
Test sending a simple notification
"""
# Set our return value
mock_notify.return_value = True
# Preare our form data
form_data = {
'urls': 'mailto://user:pass@hotmail.com',
'body': 'test notifiction',
}
# At a minimum 'body' is requred
form = NotifyByUrlForm(data=form_data)
assert form.is_valid()
response = self.client.post('/notify', form.cleaned_data)
assert response.status_code == 200
assert mock_notify.call_count == 1
@override_settings(APPRISE_STATELESS_URLS="windows://")
@patch('apprise.Apprise.notify')
def test_notify_default_urls(self, mock_notify):
"""
Test fallback to default URLS if none were otherwise specified
in the post
"""
# Set our return value
mock_notify.return_value = True
# Preare our form data (without url specified)
# content will fall back to default configuration
form_data = {
'body': 'test notifiction',
}
# At a minimum 'body' is requred
form = NotifyByUrlForm(data=form_data)
assert form.is_valid()
# This still works as the environment variable kicks in
response = self.client.post('/notify', form.cleaned_data)
assert response.status_code == 200
assert mock_notify.call_count == 1
@patch('apprise.Apprise.notify')
def test_notify_by_loaded_urls_with_json(self, mock_notify):
"""
Test sending a simple notification using JSON
"""
# Set our return value
mock_notify.return_value = True
# Preare our JSON data without any urls
json_data = {
'urls': '',
'body': 'test notifiction',
'type': apprise.NotifyType.WARNING,
}
# Send our empty notification as a JSON object
response = self.client.post(
'/notify',
data=json.dumps(json_data),
content_type='application/json',
)
# Nothing notified
assert response.status_code == 204
assert mock_notify.call_count == 0
# Preare our JSON data
json_data = {
'urls': 'mailto://user:pass@yahoo.ca',
'body': 'test notifiction',
'type': apprise.NotifyType.WARNING,
}
# Send our notification as a JSON object
response = self.client.post(
'/notify',
data=json.dumps(json_data),
content_type='application/json',
)
# Still supported
assert response.status_code == 200
assert mock_notify.call_count == 1
# Reset our count
mock_notify.reset_mock()
# Test sending a garbage JSON object
response = self.client.post(
'/notify/',
data="{",
content_type='application/json',
)
assert response.status_code == 400
assert mock_notify.call_count == 0
# Test sending with an invalid content type
response = self.client.post(
'/notify',
data="{}",
content_type='application/xml',
)
assert response.status_code == 400
assert mock_notify.call_count == 0
# Test sending without any content at all
response = self.client.post(
'/notify/',
data="{}",
content_type='application/json',
)
assert response.status_code == 400
assert mock_notify.call_count == 0
# Test sending without a body
json_data = {
'type': apprise.NotifyType.WARNING,
}
response = self.client.post(
'/notify',
data=json.dumps(json_data),
content_type='application/json',
)
assert response.status_code == 400
assert mock_notify.call_count == 0

View File

@@ -44,4 +44,7 @@ urlpatterns = [
re_path(
r'^notify/(?P<key>[\w_-]{1,64})/?',
views.NotifyView.as_view(), name='notify'),
re_path(
r'^notify/?',
views.StatelessNotifyView.as_view(), name='s_notify'),
]

View File

@@ -25,6 +25,7 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
from django.conf import settings
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
from django.views.decorators.gzip import gzip_page
@@ -33,6 +34,7 @@ from .utils import ConfigCache
from .forms import AddByUrlForm
from .forms import AddByConfigForm
from .forms import NotifyForm
from .forms import NotifyByUrlForm
from tempfile import NamedTemporaryFile
import apprise
@@ -428,3 +430,84 @@ class NotifyView(View):
return HttpResponse(
_('Notification(s) sent.'),
content_type=content_type, status=ResponseCode.okay)
@method_decorator((gzip_page, never_cache), name='dispatch')
class StatelessNotifyView(View):
"""
A Django view for sending a stateless notification
"""
def post(self, request):
"""
Handle a POST request
"""
# Our default response type
content_type = 'text/plain; charset=utf-8'
# our content
content = {}
if MIME_IS_FORM.match(request.content_type):
content = {}
form = NotifyByUrlForm(request.POST)
if form.is_valid():
content.update(form.cleaned_data)
elif MIME_IS_JSON.match(request.content_type):
# Prepare our default response
try:
# load our JSON content
content = json.loads(request.body)
except (AttributeError, ValueError):
# could not parse JSON response...
return HttpResponse(
_('Invalid JSON specified.'),
content_type=content_type,
status=ResponseCode.bad_request)
if not content:
# We could not handle the Content-Type
return HttpResponse(
_('The message format is not supported.'),
content_type=content_type,
status=ResponseCode.bad_request)
if not content.get('urls') and settings.APPRISE_STATELESS_URLS:
# fallback to settings.APPRISE_STATELESS_URLS if no urls were
# defined
content['urls'] = settings.APPRISE_STATELESS_URLS
# Some basic error checking
if not content.get('body') or \
content.get('type', apprise.NotifyType.INFO) \
not in apprise.NOTIFY_TYPES:
return HttpResponse(
_('An invalid payload was specified.'),
content_type=content_type,
status=ResponseCode.bad_request)
# Prepare our apprise object
a_obj = apprise.Apprise()
# Add URLs
a_obj.add(content.get('urls'))
if not len(a_obj):
return HttpResponse(
_('There was no services to notify.'),
content_type=content_type,
status=ResponseCode.no_content,
)
# Perform our notification at this point
a_obj.notify(
content.get('body'),
title=content.get('title', ''),
notify_type=content.get('type', apprise.NotifyType.INFO),
tag='all',
)
# Return our retrieved content
return HttpResponse(
_('Notification(s) sent.'),
content_type=content_type, status=ResponseCode.okay)