mirror of https://github.com/caronc/apprise
code cleanup and test coverage fixed
parent
9bc937d585
commit
b0a006b261
|
@ -26,16 +26,15 @@
|
||||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
# To use this service you will need a Clickatell account to which you can get your
|
# To use this service you will need a Clickatell account to which you can get
|
||||||
# API_TOKEN at:
|
# your API_TOKEN at:
|
||||||
# https://www.clickatell.com/
|
# https://www.clickatell.com/
|
||||||
import requests
|
import requests
|
||||||
|
from itertools import chain
|
||||||
from .base import NotifyBase
|
from .base import NotifyBase
|
||||||
from ..common import NotifyType
|
from ..common import NotifyType
|
||||||
from ..locale import gettext_lazy as _
|
from ..locale import gettext_lazy as _
|
||||||
from ..url import PrivacyMode
|
from ..utils.parse import is_phone_no, validate_regex, parse_phone_no
|
||||||
from ..utils.parse import validate_regex, parse_phone_no
|
|
||||||
|
|
||||||
|
|
||||||
class NotifyClickatell(NotifyBase):
|
class NotifyClickatell(NotifyBase):
|
||||||
|
@ -50,18 +49,18 @@ class NotifyClickatell(NotifyBase):
|
||||||
notify_url = 'https://platform.clickatell.com/messages/http/send?apiKey={}'
|
notify_url = 'https://platform.clickatell.com/messages/http/send?apiKey={}'
|
||||||
|
|
||||||
templates = (
|
templates = (
|
||||||
'{schema}://{api_token}/{targets}',
|
'{schema}://{apikey}/{targets}',
|
||||||
'{schema}://{api_token}@{from_phone}/{targets}',
|
'{schema}://{source}@{apikey}/{targets}',
|
||||||
)
|
)
|
||||||
|
|
||||||
template_tokens = dict(NotifyBase.template_tokens, **{
|
template_tokens = dict(NotifyBase.template_tokens, **{
|
||||||
'api_token': {
|
'apikey': {
|
||||||
'name': _('API Token'),
|
'name': _('API Token'),
|
||||||
'type': 'string',
|
'type': 'string',
|
||||||
'private': True,
|
'private': True,
|
||||||
'required': True,
|
'required': True,
|
||||||
},
|
},
|
||||||
'from_phone': {
|
'source': {
|
||||||
'name': _('From Phone No'),
|
'name': _('From Phone No'),
|
||||||
'type': 'string',
|
'type': 'string',
|
||||||
'regex': (r'^[0-9\s)(+-]+$', 'i'),
|
'regex': (r'^[0-9\s)(+-]+$', 'i'),
|
||||||
|
@ -81,33 +80,72 @@ class NotifyClickatell(NotifyBase):
|
||||||
})
|
})
|
||||||
|
|
||||||
template_args = dict(NotifyBase.template_args, **{
|
template_args = dict(NotifyBase.template_args, **{
|
||||||
'token': {
|
'apikey': {
|
||||||
'alias_of': 'api_token'
|
'alias_of': 'apikey'
|
||||||
},
|
},
|
||||||
'to': {
|
'to': {
|
||||||
'alias_of': 'targets',
|
'alias_of': 'targets',
|
||||||
},
|
},
|
||||||
'from': {
|
'from': {
|
||||||
'alias_of': 'from_phone',
|
'alias_of': 'source',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
def __init__(self, api_token, from_phone, targets=None, **kwargs):
|
def __init__(self, apikey, source=None, targets=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Initialize Clickatell Object
|
Initialize Clickatell Object
|
||||||
"""
|
"""
|
||||||
|
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self.api_token = validate_regex(api_token)
|
self.apikey = validate_regex(apikey)
|
||||||
if not self.api_token:
|
if not self.apikey:
|
||||||
msg = 'An invalid Clickatell API Token ' \
|
msg = 'An invalid Clickatell API Token ' \
|
||||||
'({}) was specified.'.format(api_token)
|
'({}) was specified.'.format(apikey)
|
||||||
self.logger.warning(msg)
|
self.logger.warning(msg)
|
||||||
raise TypeError(msg)
|
raise TypeError(msg)
|
||||||
|
|
||||||
self.from_phone = validate_regex(from_phone)
|
self.source = None
|
||||||
self.targets = parse_phone_no(targets, prefix=True)
|
if source:
|
||||||
|
result = is_phone_no(source)
|
||||||
|
if not result:
|
||||||
|
msg = 'The Account (From) Phone # specified ' \
|
||||||
|
'({}) is invalid.'.format(source)
|
||||||
|
self.logger.warning(msg)
|
||||||
|
|
||||||
|
raise TypeError(msg)
|
||||||
|
|
||||||
|
# Tidy source
|
||||||
|
self.source = result['full']
|
||||||
|
|
||||||
|
# Used for URL generation afterwards only
|
||||||
|
self._invalid_targets = list()
|
||||||
|
|
||||||
|
# Parse our targets
|
||||||
|
self.targets = list()
|
||||||
|
|
||||||
|
for target in parse_phone_no(targets, prefix=True):
|
||||||
|
# Validate targets and drop bad ones:
|
||||||
|
result = is_phone_no(target)
|
||||||
|
if not result:
|
||||||
|
self.logger.warning(
|
||||||
|
'Dropped invalid phone # '
|
||||||
|
'({}) specified.'.format(target),
|
||||||
|
)
|
||||||
|
self._invalid_targets.append(target)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# store valid phone number
|
||||||
|
self.targets.append(result['full'])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url_identifier(self):
|
||||||
|
"""
|
||||||
|
Returns all of the identifiers that make this URL unique from
|
||||||
|
another simliar one. Targets or end points should never be identified
|
||||||
|
here.
|
||||||
|
"""
|
||||||
|
return (self.apikey, self.source)
|
||||||
|
|
||||||
def url(self, privacy=False, *args, **kwargs):
|
def url(self, privacy=False, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -116,12 +154,24 @@ class NotifyClickatell(NotifyBase):
|
||||||
|
|
||||||
params = self.url_parameters(privacy=privacy, *args, **kwargs)
|
params = self.url_parameters(privacy=privacy, *args, **kwargs)
|
||||||
|
|
||||||
return '{schema}://{apikey}/?{params}'.format(
|
return '{schema}://{source}{apikey}/{targets}/?{params}'.format(
|
||||||
schema=self.secure_protocol,
|
schema=self.secure_protocol,
|
||||||
apikey=self.quote(self.api_token, safe='/'),
|
source='{}@'.format(self.source) if self.source else '',
|
||||||
|
apikey=self.pprint(self.apikey, privacy, safe='='),
|
||||||
|
targets='/'.join(
|
||||||
|
[NotifyClickatell.quote(t, safe='')
|
||||||
|
for t in chain(self.targets, self._invalid_targets)]),
|
||||||
params=self.urlencode(params),
|
params=self.urlencode(params),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
"""
|
||||||
|
Returns the number of targets associated with this notification
|
||||||
|
|
||||||
|
Always return 1 at least
|
||||||
|
"""
|
||||||
|
return len(self.targets) if self.targets else 1
|
||||||
|
|
||||||
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
||||||
"""
|
"""
|
||||||
Perform Clickatell Notification
|
Perform Clickatell Notification
|
||||||
|
@ -137,9 +187,9 @@ class NotifyClickatell(NotifyBase):
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
}
|
}
|
||||||
|
|
||||||
url = self.notify_url.format(self.api_token)
|
url = self.notify_url.format(self.apikey)
|
||||||
if self.from_phone:
|
if self.source:
|
||||||
url += '&from={}'.format(self.from_phone)
|
url += '&from={}'.format(self.source)
|
||||||
url += '&to={}'.format(','.join(self.targets))
|
url += '&to={}'.format(','.join(self.targets))
|
||||||
url += '&content={}'.format(' '.join([title, body]))
|
url += '&content={}'.format(' '.join([title, body]))
|
||||||
|
|
||||||
|
@ -172,6 +222,7 @@ class NotifyClickatell(NotifyBase):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
self.logger.info('Sent Clickatell notification.')
|
self.logger.info('Sent Clickatell notification.')
|
||||||
|
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
'A Connection error occurred sending Clickatell '
|
'A Connection error occurred sending Clickatell '
|
||||||
|
@ -192,15 +243,22 @@ class NotifyClickatell(NotifyBase):
|
||||||
return results
|
return results
|
||||||
|
|
||||||
results['targets'] = NotifyClickatell.split_path(results['fullpath'])
|
results['targets'] = NotifyClickatell.split_path(results['fullpath'])
|
||||||
|
results['apikey'] = NotifyClickatell.unquote(results['host'])
|
||||||
if not results['targets']:
|
|
||||||
return results
|
|
||||||
|
|
||||||
if results['user']:
|
if results['user']:
|
||||||
results['api_token'] = NotifyClickatell.unquote(results['user'])
|
results['source'] = NotifyClickatell.unquote(results['user'])
|
||||||
results['from_phone'] = NotifyClickatell.unquote(results['host'])
|
|
||||||
else:
|
# Support the 'to' variable so that we can support targets this way too
|
||||||
results['api_token'] = NotifyClickatell.unquote(results['host'])
|
# The 'to' makes it easier to use yaml configuration
|
||||||
results['from_phone'] = ''
|
if 'to' in results['qsd'] and len(results['qsd']['to']):
|
||||||
|
results['targets'] += \
|
||||||
|
NotifyClickatell.parse_phone_no(results['qsd']['to'])
|
||||||
|
|
||||||
|
# Support the 'from' and 'source' variable so that we can support
|
||||||
|
# targets this way too.
|
||||||
|
# The 'from' makes it easier to use yaml configuration
|
||||||
|
if 'from' in results['qsd'] and len(results['qsd']['from']):
|
||||||
|
results['source'] = \
|
||||||
|
NotifyClickatell.unquote(results['qsd']['from'])
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
|
@ -45,64 +45,76 @@ apprise_url_tests = (
|
||||||
'instance': TypeError,
|
'instance': TypeError,
|
||||||
}),
|
}),
|
||||||
('clickatell:///', {
|
('clickatell:///', {
|
||||||
# invalid api_token
|
# invalid apikey
|
||||||
'instance': TypeError,
|
'instance': TypeError,
|
||||||
}),
|
}),
|
||||||
('clickatell://@/', {
|
('clickatell://@/', {
|
||||||
# invalid api_token
|
# invalid apikey
|
||||||
'instance': TypeError,
|
'instance': TypeError,
|
||||||
}),
|
}),
|
||||||
|
('clickatell://{}@/'.format('1' * 10), {
|
||||||
|
# no api key provided
|
||||||
|
'instance': TypeError,
|
||||||
|
}),
|
||||||
|
('clickatell://{}@{}/'.format('1' * 3, 'a' * 32), {
|
||||||
|
# invalid From/Source
|
||||||
|
'instance': TypeError
|
||||||
|
}),
|
||||||
('clickatell://{}/'.format('a' * 32), {
|
('clickatell://{}/'.format('a' * 32), {
|
||||||
# no targets provided
|
# no targets provided
|
||||||
'instance': TypeError,
|
'instance': NotifyClickatell,
|
||||||
|
# We have no one to notify
|
||||||
|
'notify_response': False,
|
||||||
}),
|
}),
|
||||||
('clickatell://{}@/'.format('a' * 32), {
|
('clickatell://{}@{}/'.format('1' * 10, 'a' * 32), {
|
||||||
# no targets provided
|
# no targets provided (no one to notify)
|
||||||
'instance': TypeError,
|
'instance': NotifyClickatell,
|
||||||
}),
|
# We have no one to notify
|
||||||
('clickatell://{}@{}/'.format('a' * 32, '1' * 9), {
|
'notify_response': False,
|
||||||
# no targets provided
|
|
||||||
'instance': TypeError,
|
|
||||||
}),
|
|
||||||
('clickatell://{}@{}'.format('a' * 32, 'b' * 32, '3' * 9), {
|
|
||||||
# no targets provided
|
|
||||||
'instance': TypeError,
|
|
||||||
}),
|
}),
|
||||||
('clickatell://{}@{}/123/{}/abcd'.format(
|
('clickatell://{}@{}/123/{}/abcd'.format(
|
||||||
'a' * 32, '1' * 6, '3' * 11), {
|
'1' * 10, 'a' * 32, '3' * 15), {
|
||||||
# valid everything but target numbers
|
# valid everything but target numbers
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
}),
|
# We have no one to notify
|
||||||
('clickatell://{}/{}'.format('a' * 32, '1' * 9), {
|
'notify_response': False,
|
||||||
|
}),
|
||||||
|
('clickatell://{}/{}'.format('1' * 10, 'a' * 32), {
|
||||||
|
# everything valid (no source defined)
|
||||||
|
'instance': NotifyClickatell,
|
||||||
|
# We have no one to notify
|
||||||
|
'notify_response': False,
|
||||||
|
}),
|
||||||
|
('clickatell://{}@{}/{}'.format('1' * 10, 'a' * 32, '1' * 10), {
|
||||||
# everything valid
|
# everything valid
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
}),
|
}),
|
||||||
('clickatell://{}@{}/{}'.format('a' * 32, '1' * 9, '1' * 9), {
|
('clickatell://{}/{}'.format('a' * 32, '1' * 10), {
|
||||||
# everything valid
|
# everything valid (no source)
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
}),
|
}),
|
||||||
('clickatell://_?token={}&from={}&to={},{}'.format(
|
('clickatell://_?apikey={}&from={}&to={},{}'.format(
|
||||||
'a' * 32, '1' * 9, '1' * 9, '1' * 9), {
|
'a' * 32, '1' * 10, '1' * 10, '1' * 10), {
|
||||||
# use get args to accomplish the same thing
|
# use get args to accomplish the same thing
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
}),
|
}),
|
||||||
('clickatell://_?token={}'.format('a' * 32), {
|
('clickatell://_?apikey={}'.format('a' * 32), {
|
||||||
# use get args
|
# use get args
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
'notify_response': False,
|
'notify_response': False,
|
||||||
}),
|
}),
|
||||||
('clickatell://_?token={}&from={}'.format('a' * 32, '1' * 9), {
|
('clickatell://_?apikey={}&from={}'.format('a' * 32, '1' * 10), {
|
||||||
# use get args
|
# use get args
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
'notify_response': False,
|
'notify_response': False,
|
||||||
}),
|
}),
|
||||||
('clickatell://{}/{}'.format('a' * 32, '1' * 9), {
|
('clickatell://{}@{}/{}'.format('1' * 10, 'a' * 32, '1' * 10), {
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
# throw a bizarre code forcing us to fail to look it up
|
# throw a bizarre code forcing us to fail to look it up
|
||||||
'response': False,
|
'response': False,
|
||||||
'requests_response_code': 999,
|
'requests_response_code': 999,
|
||||||
}),
|
}),
|
||||||
('clickatell://{}@{}/{}'.format('a' * 32, '1' * 9, '1' * 9), {
|
('clickatell://{}@{}/{}'.format('1' * 10, 'a' * 32, '1' * 10), {
|
||||||
'instance': NotifyClickatell,
|
'instance': NotifyClickatell,
|
||||||
# Throws a series of connection and transfer exceptions when this flag
|
# Throws a series of connection and transfer exceptions when this flag
|
||||||
# is set and tests that we gracefully handle them
|
# is set and tests that we gracefully handle them
|
||||||
|
@ -133,13 +145,13 @@ def test_plugin_clickatell_edge_cases(mock_post):
|
||||||
# Prepare Mock
|
# Prepare Mock
|
||||||
mock_post.return_value = response
|
mock_post.return_value = response
|
||||||
|
|
||||||
# Initialize some generic (but valid) tokens
|
# Initialize some generic (but valid) apikeys
|
||||||
api_token = 'b' * 32
|
apikey = 'b' * 32
|
||||||
from_phone = '+1 (555) 123-3456'
|
from_phone = '+1 (555) 123-3456'
|
||||||
|
|
||||||
# No api_token specified
|
# No apikey specified
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
NotifyClickatell(api_token=None, from_phone=from_phone)
|
NotifyClickatell(apikey=None, from_phone=from_phone)
|
||||||
|
|
||||||
# a error response
|
# a error response
|
||||||
response.status_code = 400
|
response.status_code = 400
|
||||||
|
@ -150,7 +162,7 @@ def test_plugin_clickatell_edge_cases(mock_post):
|
||||||
mock_post.return_value = response
|
mock_post.return_value = response
|
||||||
|
|
||||||
# Initialize our object
|
# Initialize our object
|
||||||
obj = NotifyClickatell(api_token=api_token, from_phone=from_phone)
|
obj = NotifyClickatell(apikey=apikey, from_phone=from_phone)
|
||||||
|
|
||||||
# We will fail with the above error code
|
# We will fail with the above error code
|
||||||
assert obj.notify('title', 'body', 'info') is False
|
assert obj.notify('title', 'body', 'info') is False
|
||||||
|
|
Loading…
Reference in New Issue