424 error code returned if one or more notifications fail (#12)

This commit is contained in:
Chris Caron 2020-03-17 12:08:06 -04:00 committed by GitHub
parent 3b870c07e3
commit df04dfb3ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 2 deletions

View File

@ -70,6 +70,50 @@ 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('apprise.NotifyBase.notify')
def test_partial_notify_by_loaded_urls(self, mock_notify):
"""
Test notification handling when one or more of the services
can not be notified.
"""
# our key to use
key = 'test_partial_notify_by_loaded_urls'
# Add some content
response = self.client.post(
'/add/{}'.format(key),
{
'urls': ', '.join([
'mailto://user:pass@hotmail.com',
'mailto://user:pass@gmail.com',
],
),
})
assert response.status_code == 200
# Preare our form data
form_data = {
'body': 'test notifiction',
}
# At a minimum, just a body is required
form = NotifyForm(data=form_data)
assert form.is_valid()
# we always set a type if one wasn't done so already
assert form.cleaned_data['type'] == apprise.NotifyType.INFO
# Set our return value; first we return a true, then we fail
# on the second call
mock_notify.side_effect = (True, False)
# Send our notification
response = self.client.post(
'/notify/{}'.format(key), form.cleaned_data)
assert response.status_code == 424
assert mock_notify.call_count == 2
@patch('apprise.Apprise.notify') @patch('apprise.Apprise.notify')
def test_notify_by_loaded_urls_with_json(self, mock_notify): def test_notify_by_loaded_urls_with_json(self, mock_notify):
""" """

View File

@ -58,6 +58,33 @@ class StatelessNotifyTests(SimpleTestCase):
assert response.status_code == 200 assert response.status_code == 200
assert mock_notify.call_count == 1 assert mock_notify.call_count == 1
@patch('apprise.NotifyBase.notify')
def test_partial_notify(self, mock_notify):
"""
Test sending multiple notifications where one fails
"""
# Set our return value; first we return a true, then we fail
# on the second call
mock_notify.side_effect = (True, False)
# Preare our form data
form_data = {
'urls': ', '.join([
'mailto://user:pass@hotmail.com',
'mailto://user:pass@gmail.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 == 424
assert mock_notify.call_count == 2
@override_settings(APPRISE_STATELESS_URLS="windows://") @override_settings(APPRISE_STATELESS_URLS="windows://")
@patch('apprise.Apprise.notify') @patch('apprise.Apprise.notify')
def test_notify_default_urls(self, mock_notify): def test_notify_default_urls(self, mock_notify):

View File

@ -80,6 +80,7 @@ class ResponseCode(object):
bad_request = 400 bad_request = 400
not_found = 404 not_found = 404
method_not_allowed = 405 method_not_allowed = 405
failed_dependency = 424
internal_server_error = 500 internal_server_error = 500
@ -407,13 +408,20 @@ class NotifyView(View):
a_obj.add(ac_obj) a_obj.add(ac_obj)
# Perform our notification at this point # Perform our notification at this point
a_obj.notify( result = a_obj.notify(
content.get('body'), content.get('body'),
title=content.get('title', ''), title=content.get('title', ''),
notify_type=content.get('type', apprise.NotifyType.INFO), notify_type=content.get('type', apprise.NotifyType.INFO),
tag=content.get('tag'), tag=content.get('tag'),
) )
if not result:
# If at least one notification couldn't be sent; change up
# the response to a 424 error code
return HttpResponse(
_('One or more notification could not be sent.'),
status=ResponseCode.failed_dependency)
except OSError: except OSError:
# We could not write the temporary file to disk # We could not write the temporary file to disk
return HttpResponse( return HttpResponse(
@ -488,13 +496,20 @@ class StatelessNotifyView(View):
) )
# Perform our notification at this point # Perform our notification at this point
a_obj.notify( result = a_obj.notify(
content.get('body'), content.get('body'),
title=content.get('title', ''), title=content.get('title', ''),
notify_type=content.get('type', apprise.NotifyType.INFO), notify_type=content.get('type', apprise.NotifyType.INFO),
tag='all', tag='all',
) )
if not result:
# If at least one notification couldn't be sent; change up the
# response to a 424 error code
return HttpResponse(
_('One or more notification could not be sent.'),
status=ResponseCode.failed_dependency)
# Return our retrieved content # Return our retrieved content
return HttpResponse( return HttpResponse(
_('Notification(s) sent.'), _('Notification(s) sent.'),