Honor overflow=truncate when sending attachments (#1097)

This commit is contained in:
pomeloy 2024-04-08 23:19:14 +02:00 committed by GitHub
parent fcaa6f4925
commit 5df6c3a09b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 2 deletions

View File

@ -457,6 +457,19 @@ class NotifyBase(URLBase):
# Handle situations where the title is None
title = '' if not title else title
# Truncate flag set with attachments ensures that only 1
# attachment passes through. In the event there could be many
# services specified, we only want to do this logic once.
# The logic is only applicable if ther was more then 1 attachment
# specified
overflow = self.overflow_mode if overflow is None else overflow
if attach and len(attach) > 1 and overflow == OverflowMode.TRUNCATE:
# Save first attachment
_attach = AppriseAttachment(attach[0], asset=self.asset)
else:
# reference same attachment
_attach = attach
# Apply our overflow (if defined)
for chunk in self._apply_overflow(
body=body, title=title, overflow=overflow,
@ -465,7 +478,7 @@ class NotifyBase(URLBase):
# Send notification
yield dict(
body=chunk['body'], title=chunk['title'],
notify_type=notify_type, attach=attach,
notify_type=notify_type, attach=_attach,
body_format=body_format
)
@ -485,7 +498,7 @@ class NotifyBase(URLBase):
},
{
title: 'the title goes here',
body: 'the message body goes here',
body: 'the continued message body goes here',
},
]

View File

@ -27,10 +27,14 @@
# POSSIBILITY OF SUCH DAMAGE.
import pytest
import json
import requests
from unittest import mock
from os.path import getsize
from os.path import join
from os.path import dirname
from inspect import cleandoc
from apprise.Apprise import Apprise
from apprise.AttachmentManager import AttachmentManager
from apprise.AppriseAttachment import AppriseAttachment
from apprise.AppriseAsset import AppriseAsset
@ -261,6 +265,59 @@ def test_apprise_attachment():
assert aa.size() == 0
@mock.patch('requests.get')
def test_apprise_attachment_truncate(mock_get):
"""
API: AppriseAttachment when truncation in place
"""
# Prepare our response
response = requests.Request()
response.status_code = requests.codes.ok
# Prepare Mock
mock_get.return_value = response
# our Apprise Object
ap_obj = Apprise()
# Add ourselves an object set to truncate
ap_obj.add('json://localhost/?method=GET&overflow=truncate')
# Add ourselves a second object without truncate
ap_obj.add('json://localhost/?method=GET&overflow=upstream')
# Create ourselves an attachment object
aa = AppriseAttachment()
# There are no attachents loaded
assert len(aa) == 0
# Object can be directly checked as a boolean; response is False
# when there are no entries loaded
assert not aa
# Add 2 attachments
assert aa.add(join(TEST_VAR_DIR, 'apprise-test.gif'))
assert aa.add(join(TEST_VAR_DIR, 'apprise-test.png'))
assert mock_get.call_count == 0
assert ap_obj.notify(body='body', title='title', attach=aa)
assert mock_get.call_count == 2
# Our first item was truncated, so only 1 attachment
details = mock_get.call_args_list[0]
dataset = json.loads(details[1]['data'])
assert len(dataset['attachments']) == 1
# Our second item was not truncated, so all attachments
details = mock_get.call_args_list[1]
dataset = json.loads(details[1]['data'])
assert len(dataset['attachments']) == 2
def test_apprise_attachment_instantiate():
"""
API: AppriseAttachment.instantiate()