Signal API Attachment and Group Support Added (#580)

This commit is contained in:
Chris Caron
2022-05-11 11:11:51 -04:00
committed by GitHub
parent ca0c8460f1
commit 9c145a842e
4 changed files with 242 additions and 17 deletions

View File

@ -22,6 +22,8 @@
# 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.
import os
import sys
from json import loads
import mock
import pytest
@ -29,11 +31,16 @@ import requests
from apprise import plugins
from apprise import Apprise
from helpers import AppriseURLTester
from apprise import AppriseAttachment
from apprise import NotifyType
# Disable logging for a cleaner testing output
import logging
logging.disable(logging.CRITICAL)
# Attachment Directory
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), 'var')
# Our Testing URLs
apprise_url_tests = (
('signal://', {
@ -70,6 +77,18 @@ apprise_url_tests = (
'instance': plugins.NotifySignalAPI,
}),
('signal://localhost:8082/+{}/@group.abcd/'.format('2' * 11), {
# a valid group
'instance': plugins.NotifySignalAPI,
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'signal://localhost:8082/+{}/@abcd'.format('2' * 11),
}),
('signal://localhost:8080/+{}/group.abcd/'.format('1' * 11), {
# another valid group (without @ symbol)
'instance': plugins.NotifySignalAPI,
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'signal://localhost:8080/+{}/@abcd'.format('1' * 11),
}),
('signal://localhost:8080/?from={}&to={},{}'.format(
'1' * 11, '2' * 11, '3' * 11), {
# use get args to acomplish the same thing
@ -203,10 +222,13 @@ def test_plugin_signal_test_based_on_feedback(mock_post):
title = "My Title"
aobj = Apprise()
aobj.add('signal://10.0.0.112:8080/+12512222222/+12513333333')
aobj.add(
'signal://10.0.0.112:8080/+12512222222/+12513333333/'
'12514444444?batch=yes')
assert aobj.notify(title=title, body=body)
# If a batch, there is only 1 post
assert mock_post.call_count == 1
details = mock_post.call_args_list[0]
@ -214,4 +236,139 @@ def test_plugin_signal_test_based_on_feedback(mock_post):
payload = loads(details[1]['data'])
assert payload['message'] == 'My Title\r\ntest body'
assert payload['number'] == "+12512222222"
assert payload['recipients'] == ["+12513333333"]
assert len(payload['recipients']) == 2
assert "+12513333333" in payload['recipients']
# The + is appended
assert "+12514444444" in payload['recipients']
# Reset our test and turn batch mode off
mock_post.reset_mock()
aobj = Apprise()
aobj.add(
'signal://10.0.0.112:8080/+12512222222/+12513333333/'
'12514444444?batch=no')
assert aobj.notify(title=title, body=body)
# If a batch, there is only 1 post
assert mock_post.call_count == 2
details = mock_post.call_args_list[0]
assert details[0][0] == 'http://10.0.0.112:8080/v2/send'
payload = loads(details[1]['data'])
assert payload['message'] == 'My Title\r\ntest body'
assert payload['number'] == "+12512222222"
assert len(payload['recipients']) == 1
assert "+12513333333" in payload['recipients']
details = mock_post.call_args_list[1]
assert details[0][0] == 'http://10.0.0.112:8080/v2/send'
payload = loads(details[1]['data'])
assert payload['message'] == 'My Title\r\ntest body'
assert payload['number'] == "+12512222222"
assert len(payload['recipients']) == 1
# The + is appended
assert "+12514444444" in payload['recipients']
mock_post.reset_mock()
# Test group names
aobj = Apprise()
aobj.add(
'signal://10.0.0.112:8080/+12513333333/@group1/@group2/'
'12514444444?batch=yes')
assert aobj.notify(title=title, body=body)
# If a batch, there is only 1 post
assert mock_post.call_count == 1
details = mock_post.call_args_list[0]
assert details[0][0] == 'http://10.0.0.112:8080/v2/send'
payload = loads(details[1]['data'])
assert payload['message'] == 'My Title\r\ntest body'
assert payload['number'] == "+12513333333"
assert len(payload['recipients']) == 3
assert "+12514444444" in payload['recipients']
# our groups
assert "group.group1" in payload['recipients']
assert "group.group2" in payload['recipients']
# Groups are stored properly
assert '/@group1' in aobj[0].url()
assert '/@group2' in aobj[0].url()
# Our target phone number is also in the path
assert '/+12514444444' in aobj[0].url()
@mock.patch('requests.post')
def test_notify_signal_plugin_attachments(mock_post):
"""
NotifySignalAPI() Attachments
"""
# Disable Throttling to speed testing
plugins.NotifyBase.request_rate_per_sec = 0
okay_response = requests.Request()
okay_response.status_code = requests.codes.ok
okay_response.content = ""
# Assign our mock object our return value
mock_post.return_value = okay_response
obj = Apprise.instantiate(
'signal://10.0.0.112:8080/+12512222222/+12513333333/'
'12514444444?batch=no')
assert isinstance(obj, plugins.NotifySignalAPI)
# Test Valid Attachment
path = os.path.join(TEST_VAR_DIR, 'apprise-test.gif')
attach = AppriseAttachment(path)
assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO,
attach=attach) is True
# Test invalid attachment
path = os.path.join(TEST_VAR_DIR, '/invalid/path/to/an/invalid/file.jpg')
assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO,
attach=path) is False
# Get a appropriate "builtin" module name for pythons 2/3.
if sys.version_info.major >= 3:
builtin_open_function = 'builtins.open'
else:
builtin_open_function = '__builtin__.open'
# Test Valid Attachment (load 3)
path = (
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
)
attach = AppriseAttachment(path)
# Return our good configuration
mock_post.side_effect = None
mock_post.return_value = okay_response
with mock.patch(builtin_open_function, side_effect=OSError()):
# We can't send the message we can't open the attachment for reading
assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO,
attach=attach) is False
# test the handling of our batch modes
obj = Apprise.instantiate(
'signal://10.0.0.112:8080/+12512222222/+12513333333/'
'12514444444?batch=yes')
assert isinstance(obj, plugins.NotifySignalAPI)
# Now send an attachment normally without issues
mock_post.reset_mock()
assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO,
attach=attach) is True
assert mock_post.call_count == 1