mirror of
https://github.com/caronc/apprise-api.git
synced 2025-08-09 22:57:33 +02:00
Support 'attach' keyword (alias of 'attachment')
This commit is contained in:
@ -847,6 +847,22 @@ class NotifyTests(SimpleTestCase):
|
|||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert mock_notify.call_count == 0
|
assert mock_notify.call_count == 0
|
||||||
|
|
||||||
|
# Reset our mock object
|
||||||
|
mock_notify.reset_mock()
|
||||||
|
|
||||||
|
# Preare our form data
|
||||||
|
form_data = {
|
||||||
|
'body': 'test notifiction',
|
||||||
|
'attach': 'https://localhost/invalid/path/to/image.png',
|
||||||
|
}
|
||||||
|
|
||||||
|
# Send our notification
|
||||||
|
response = self.client.post(
|
||||||
|
'/notify/{}'.format(key), form_data)
|
||||||
|
# We fail because we couldn't retrieve our attachment
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert mock_notify.call_count == 0
|
||||||
|
|
||||||
@mock.patch('apprise.Apprise.notify')
|
@mock.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):
|
||||||
"""
|
"""
|
||||||
@ -998,9 +1014,38 @@ 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
|
||||||
|
|
||||||
|
|
||||||
# Reset our mock object
|
# Reset our mock object
|
||||||
mock_notify.reset_mock()
|
mock_notify.reset_mock()
|
||||||
|
|
||||||
|
# If an empty format is specified, it is accepted and
|
||||||
|
# no imput format is specified
|
||||||
|
json_data = {
|
||||||
|
'body': 'test message',
|
||||||
|
'format': None,
|
||||||
|
'attach': 'https://localhost/invalid/path/to/image.png',
|
||||||
|
}
|
||||||
|
|
||||||
|
# Test case with format changed
|
||||||
|
response = self.client.post(
|
||||||
|
'/notify/{}'.format(key),
|
||||||
|
data=json.dumps(json_data),
|
||||||
|
content_type='application/json',
|
||||||
|
)
|
||||||
|
|
||||||
|
# We failed to send notification because we couldn't fetch the
|
||||||
|
# attachment
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert mock_notify.call_count == 0
|
||||||
|
|
||||||
|
# Reset our mock object
|
||||||
|
mock_notify.reset_mock()
|
||||||
|
|
||||||
|
json_data = {
|
||||||
|
'body': 'test message',
|
||||||
|
'format': None,
|
||||||
|
}
|
||||||
|
|
||||||
# Same results for any empty string:
|
# Same results for any empty string:
|
||||||
json_data['format'] = ''
|
json_data['format'] = ''
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
|
@ -250,6 +250,50 @@ class StatelessNotifyTests(SimpleTestCase):
|
|||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert mock_notify.call_count == 0
|
assert mock_notify.call_count == 0
|
||||||
|
|
||||||
|
# Reset our mock object
|
||||||
|
mock_notify.reset_mock()
|
||||||
|
|
||||||
|
# Preare our form data (support attach keyword)
|
||||||
|
form_data = {
|
||||||
|
'body': 'test notifiction',
|
||||||
|
'urls': ', '.join([
|
||||||
|
'mailto://user:pass@hotmail.com',
|
||||||
|
'mailto://user:pass@gmail.com',
|
||||||
|
]),
|
||||||
|
'attach': 'https://localhost/invalid/path/to/image.png',
|
||||||
|
}
|
||||||
|
|
||||||
|
# Send our notification
|
||||||
|
response = self.client.post('/notify', form_data)
|
||||||
|
# We fail because we couldn't retrieve our attachment
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert mock_notify.call_count == 0
|
||||||
|
|
||||||
|
# Reset our mock object
|
||||||
|
mock_notify.reset_mock()
|
||||||
|
|
||||||
|
# Preare our json data (and support attach keyword as alias)
|
||||||
|
json_data = {
|
||||||
|
'body': 'test notifiction',
|
||||||
|
'urls': ', '.join([
|
||||||
|
'mailto://user:pass@hotmail.com',
|
||||||
|
'mailto://user:pass@gmail.com',
|
||||||
|
]),
|
||||||
|
'attach': 'https://localhost/invalid/path/to/image.png',
|
||||||
|
}
|
||||||
|
|
||||||
|
# Same results
|
||||||
|
response = self.client.post(
|
||||||
|
'/notify/',
|
||||||
|
data=json.dumps(json_data),
|
||||||
|
content_type='application/json',
|
||||||
|
)
|
||||||
|
|
||||||
|
# We fail because we couldn't retrieve our attachment
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert mock_notify.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
@override_settings(APPRISE_RECURSION_MAX=1)
|
@override_settings(APPRISE_RECURSION_MAX=1)
|
||||||
@mock.patch('apprise.Apprise.notify')
|
@mock.patch('apprise.Apprise.notify')
|
||||||
def test_stateless_notify_recursion(self, mock_notify):
|
def test_stateless_notify_recursion(self, mock_notify):
|
||||||
|
@ -610,10 +610,20 @@ class NotifyView(View):
|
|||||||
|
|
||||||
# Handle Attachments
|
# Handle Attachments
|
||||||
attach = None
|
attach = None
|
||||||
if not content.get('attachment') and 'attachment' in request.POST:
|
if not content.get('attachment'):
|
||||||
|
if 'attachment' in request.POST:
|
||||||
# Acquire attachments to work with them
|
# Acquire attachments to work with them
|
||||||
content['attachment'] = request.POST.getlist('attachment')
|
content['attachment'] = request.POST.getlist('attachment')
|
||||||
|
|
||||||
|
elif 'attach' in request.POST:
|
||||||
|
# Acquire kw (alias) attach to work with them
|
||||||
|
content['attachment'] = request.POST.getlist('attach')
|
||||||
|
|
||||||
|
elif content.get('attach'):
|
||||||
|
# Acquire kw (alias) attach from payload to work with
|
||||||
|
content['attachment'] = content['attach']
|
||||||
|
del content['attach']
|
||||||
|
|
||||||
if 'attachment' in content or request.FILES:
|
if 'attachment' in content or request.FILES:
|
||||||
try:
|
try:
|
||||||
attach = parse_attachments(
|
attach = parse_attachments(
|
||||||
@ -1148,16 +1158,31 @@ class StatelessNotifyView(View):
|
|||||||
|
|
||||||
# Handle Attachments
|
# Handle Attachments
|
||||||
attach = None
|
attach = None
|
||||||
if not content.get('attachment') and 'attachment' in request.POST:
|
if not content.get('attachment'):
|
||||||
|
if 'attachment' in request.POST:
|
||||||
# Acquire attachments to work with them
|
# Acquire attachments to work with them
|
||||||
content['attachment'] = request.POST.getlist('attachment')
|
content['attachment'] = request.POST.getlist('attachment')
|
||||||
|
|
||||||
|
elif 'attach' in request.POST:
|
||||||
|
# Acquire kw (alias) attach to work with them
|
||||||
|
content['attachment'] = request.POST.getlist('attach')
|
||||||
|
|
||||||
|
elif content.get('attach'):
|
||||||
|
# Acquire kw (alias) attach from payload to work with
|
||||||
|
content['attachment'] = content['attach']
|
||||||
|
del content['attach']
|
||||||
|
|
||||||
if 'attachment' in content or request.FILES:
|
if 'attachment' in content or request.FILES:
|
||||||
try:
|
try:
|
||||||
attach = parse_attachments(
|
attach = parse_attachments(
|
||||||
content.get('attachment'), request.FILES)
|
content.get('attachment'), request.FILES)
|
||||||
|
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
|
# Invalid entry found in list
|
||||||
|
logger.warning(
|
||||||
|
'NOTIFY - %s - Bad attachment specified',
|
||||||
|
request.META['REMOTE_ADDR'])
|
||||||
|
|
||||||
return HttpResponse(
|
return HttpResponse(
|
||||||
_('Bad attachment'),
|
_('Bad attachment'),
|
||||||
status=ResponseCode.bad_request)
|
status=ResponseCode.bad_request)
|
||||||
|
Reference in New Issue
Block a user