2022-10-08 05:32:34 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-13 23:08:01 +02:00
|
|
|
# BSD 2-Clause License
|
2022-10-08 05:32:34 +02:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Apprise - Push Notification Library.
|
2024-01-27 21:35:11 +01:00
|
|
|
# Copyright (c) 2024, Chris Caron <lead2gold@gmail.com>
|
2022-10-08 05:32:34 +02:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2022-10-08 05:32:34 +02:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2022-10-08 05:32:34 +02:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2022-10-08 05:32:34 +02:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2022-10-09 13:49:36 +02:00
|
|
|
from unittest import mock
|
2022-10-08 05:32:34 +02:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from json import loads
|
|
|
|
from apprise import Apprise
|
2024-04-27 19:02:27 +02:00
|
|
|
from apprise.plugins.bulksms import NotifyBulkSMS
|
2022-10-08 05:32:34 +02:00
|
|
|
from helpers import AppriseURLTester
|
|
|
|
from apprise import NotifyType
|
|
|
|
|
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
|
|
|
|
# Our Testing URLs
|
|
|
|
apprise_url_tests = (
|
|
|
|
('bulksms://', {
|
|
|
|
# Instantiated but no auth, so no otification can happen
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
# Expected notify() response because we have no one to notify
|
|
|
|
'notify_response': False,
|
|
|
|
}),
|
|
|
|
('bulksms://:@/', {
|
|
|
|
# invalid auth
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
# Expected notify() response because we have no one to notify
|
|
|
|
'notify_response': False,
|
|
|
|
}),
|
|
|
|
('bulksms://{}@12345678'.format('a' * 10), {
|
|
|
|
# Just user provided (no password)
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
# Expected notify() response because we have no one to notify
|
|
|
|
'notify_response': False,
|
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@{}'.format('a' * 10, 'b' * 10, '3' * 5), {
|
|
|
|
# invalid nubmer provided
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
# Expected notify() response because we have no one to notify
|
|
|
|
'notify_response': False,
|
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@123/{}/abcd/'.format(
|
|
|
|
'a' * 5, 'b' * 10, '3' * 11), {
|
|
|
|
# included group and phone, short number (123) dropped
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
'privacy_url': 'bulksms://a...a:****@+33333333333/@abcd'
|
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@{}?batch=y&unicode=n'.format(
|
|
|
|
'b' * 5, 'c' * 10, '4' * 11), {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
|
|
|
|
# Our expected url(privacy=True) startswith() response:
|
|
|
|
'privacy_url': 'bulksms://b...b:****@+4444444444',
|
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@123456/{}'.format('a' * 10, 'b' * 10, '4' * 11), {
|
|
|
|
# using short-code (6 characters)
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@{}'.format('a' * 10, 'b' * 10, '5' * 11), {
|
|
|
|
# using phone no with no target - we text ourselves in
|
|
|
|
# this case
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
}),
|
|
|
|
# Test route group
|
|
|
|
('bulksms://{}:{}@admin?route=premium'.format('a' * 10, 'b' * 10), {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@admin?route=invalid'.format('a' * 10, 'b' * 10), {
|
|
|
|
# invalid route
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('bulksms://_?user={}&password={}&from={}'.format(
|
|
|
|
'a' * 10, 'b' * 10, '5' * 11), {
|
|
|
|
# use get args to acomplish the same thing
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
}),
|
|
|
|
('bulksms://_?user={}&password={}&from={}'.format(
|
|
|
|
'a' * 10, 'b' * 10, '5' * 3), {
|
|
|
|
# use get args to acomplish the same thing
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('bulksms://_?user={}&password={}&from={}&to={}'.format(
|
|
|
|
'a' * 10, 'b' * 10, '5' * 11, '7' * 13), {
|
|
|
|
# use to=
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@{}'.format('a' * 10, 'b' * 10, 'a' * 3), {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
# throw a bizzare code forcing us to fail to look it up
|
|
|
|
'response': False,
|
|
|
|
'requests_response_code': 999,
|
|
|
|
}),
|
|
|
|
('bulksms://{}:{}@{}'.format('a' * 10, 'b' * 10, '6' * 11), {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyBulkSMS,
|
2022-10-08 05:32:34 +02:00
|
|
|
# Throws a series of connection and transfer exceptions when this flag
|
|
|
|
# is set and tests that we gracfully handle them
|
|
|
|
'test_requests_exceptions': True,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_plugin_bulksms_urls():
|
|
|
|
"""
|
|
|
|
NotifyTemplate() Apprise URLs
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Run our general tests
|
|
|
|
AppriseURLTester(tests=apprise_url_tests).run_all()
|
|
|
|
|
|
|
|
|
|
|
|
@mock.patch('requests.post')
|
2022-10-16 16:48:44 +02:00
|
|
|
def test_plugin_bulksms_edge_cases(mock_post):
|
2022-10-08 05:32:34 +02:00
|
|
|
"""
|
|
|
|
NotifyBulkSMS() Edge Cases
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Initialize some generic (but valid) tokens
|
|
|
|
user = 'abcd'
|
|
|
|
pwd = 'mypass123'
|
|
|
|
targets = [
|
|
|
|
'+1(555) 123-1234',
|
|
|
|
'1555 5555555',
|
|
|
|
'group',
|
|
|
|
# A garbage entry
|
|
|
|
'12',
|
|
|
|
# NOw a valid one because a group was implicit
|
|
|
|
'@12',
|
|
|
|
]
|
|
|
|
|
|
|
|
# Prepare our response
|
|
|
|
response = requests.Request()
|
|
|
|
response.status_code = requests.codes.ok
|
|
|
|
|
|
|
|
# Prepare Mock
|
|
|
|
mock_post.return_value = response
|
|
|
|
|
2023-12-17 18:03:29 +01:00
|
|
|
# Instantiate our object
|
2022-10-08 05:32:34 +02:00
|
|
|
obj = Apprise.instantiate(
|
|
|
|
'bulksms://{}:{}@{}?batch=n'.format(user, pwd, '/'.join(targets)))
|
|
|
|
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO) is True
|
|
|
|
|
2023-05-13 04:05:46 +02:00
|
|
|
# We know there are 4 targets
|
|
|
|
assert len(obj) == 4
|
|
|
|
|
2022-10-08 05:32:34 +02:00
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 4
|
|
|
|
|
|
|
|
# Test
|
|
|
|
details = mock_post.call_args_list[0]
|
|
|
|
payload = loads(details[1]['data'])
|
|
|
|
assert payload['to'] == '+15551231234'
|
|
|
|
assert payload['body'] == 'title\r\nbody'
|
|
|
|
|
|
|
|
details = mock_post.call_args_list[1]
|
|
|
|
payload = loads(details[1]['data'])
|
|
|
|
assert payload['to'] == '+15555555555'
|
|
|
|
assert payload['body'] == 'title\r\nbody'
|
|
|
|
|
|
|
|
details = mock_post.call_args_list[2]
|
|
|
|
payload = loads(details[1]['data'])
|
|
|
|
assert isinstance(payload['to'], dict)
|
|
|
|
assert payload['to']['name'] == 'group'
|
|
|
|
assert payload['body'] == 'title\r\nbody'
|
|
|
|
|
|
|
|
details = mock_post.call_args_list[3]
|
|
|
|
payload = loads(details[1]['data'])
|
|
|
|
assert isinstance(payload['to'], dict)
|
|
|
|
assert payload['to']['name'] == '12'
|
|
|
|
assert payload['body'] == 'title\r\nbody'
|
|
|
|
|
|
|
|
# Verify our URL looks good
|
|
|
|
assert obj.url().startswith(
|
|
|
|
'bulksms://{}:{}@{}'.format(user, pwd, '/'.join(
|
|
|
|
['+15551231234', '+15555555555', '@group', '@12'])))
|
|
|
|
|
|
|
|
assert 'batch=no' in obj.url()
|
2023-05-13 04:05:46 +02:00
|
|
|
|
|
|
|
# With our batch in place, our calculations are different
|
|
|
|
obj = Apprise.instantiate(
|
|
|
|
'bulksms://{}:{}@{}?batch=y'.format(user, pwd, '/'.join(targets)))
|
|
|
|
# 2 groups and 2 phones are lumped together
|
|
|
|
assert len(obj) == 3
|