2017-12-03 08:00:23 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-13 23:08:01 +02:00
|
|
|
# BSD 2-Clause License
|
2017-12-03 08:00:23 +01: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>
|
2017-12-03 08:00:23 +01: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:
|
2017-12-03 08:00:23 +01: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.
|
2017-12-03 08:00:23 +01: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.
|
2017-12-03 08:00:23 +01: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.
|
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
import pytest
|
2019-02-17 22:35:09 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2024-04-27 19:02:27 +02:00
|
|
|
from apprise.plugins import NotifyBase
|
2017-12-03 08:00:23 +01:00
|
|
|
from apprise import NotifyType
|
|
|
|
from apprise import NotifyImageSize
|
|
|
|
from timeit import default_timer
|
2019-02-25 07:02:29 +01:00
|
|
|
|
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.CRITICAL)
|
2017-12-03 08:00:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_notify_base():
|
|
|
|
"""
|
|
|
|
API: NotifyBase() object
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# invalid types throw exceptions
|
2019-10-09 18:39:31 +02:00
|
|
|
with pytest.raises(TypeError):
|
2019-02-25 07:02:29 +01:00
|
|
|
NotifyBase(**{'format': 'invalid'})
|
2017-12-03 08:00:23 +01:00
|
|
|
|
2019-02-18 04:21:59 +01:00
|
|
|
# invalid types throw exceptions
|
2019-10-09 18:39:31 +02:00
|
|
|
with pytest.raises(TypeError):
|
2019-02-25 07:02:29 +01:00
|
|
|
NotifyBase(**{'overflow': 'invalid'})
|
2019-02-18 04:21:59 +01:00
|
|
|
|
2017-12-03 08:00:23 +01:00
|
|
|
# Bad port information
|
|
|
|
nb = NotifyBase(port='invalid')
|
|
|
|
assert nb.port is None
|
|
|
|
|
|
|
|
nb = NotifyBase(port=10)
|
|
|
|
assert nb.port == 10
|
|
|
|
|
2023-10-08 20:15:58 +02:00
|
|
|
assert isinstance(nb.url(), str)
|
|
|
|
assert str(nb) == nb.url()
|
2019-02-13 23:48:09 +01:00
|
|
|
|
2024-04-21 01:00:31 +02:00
|
|
|
with pytest.raises(NotImplementedError):
|
2019-02-18 04:21:59 +01:00
|
|
|
# Each sub-module is that inherits this as a parent is required to
|
|
|
|
# over-ride this function. So direct calls to this throws a not
|
|
|
|
# implemented error intentionally
|
2024-04-21 01:00:31 +02:00
|
|
|
nb.send('test message')
|
2019-02-18 04:21:59 +01:00
|
|
|
|
2017-12-03 08:00:23 +01:00
|
|
|
# Throttle overrides..
|
2018-03-31 04:29:36 +02:00
|
|
|
nb = NotifyBase()
|
2019-02-17 22:35:09 +01:00
|
|
|
nb.request_rate_per_sec = 0.0
|
2017-12-03 08:00:23 +01:00
|
|
|
start_time = default_timer()
|
|
|
|
nb.throttle()
|
|
|
|
elapsed = default_timer() - start_time
|
|
|
|
# Should be a very fast response time since we set it to zero but we'll
|
|
|
|
# check for less then 500 to be fair as some testing systems may be slower
|
|
|
|
# then other
|
|
|
|
assert elapsed < 0.5
|
|
|
|
|
2019-02-17 22:35:09 +01:00
|
|
|
# Concurrent calls should achieve the same response
|
|
|
|
start_time = default_timer()
|
|
|
|
nb.throttle()
|
|
|
|
elapsed = default_timer() - start_time
|
|
|
|
assert elapsed < 0.5
|
|
|
|
|
|
|
|
nb = NotifyBase()
|
|
|
|
nb.request_rate_per_sec = 1.0
|
|
|
|
|
|
|
|
# Set our time to now
|
|
|
|
start_time = default_timer()
|
|
|
|
nb.throttle()
|
|
|
|
elapsed = default_timer() - start_time
|
|
|
|
# A first call to throttle (Without telling it a time previously ran) does
|
|
|
|
# not block for any length of time; it just merely sets us up for
|
|
|
|
# concurrent calls to block
|
|
|
|
assert elapsed < 0.5
|
|
|
|
|
|
|
|
# Concurrent calls could take up to the rate_per_sec though...
|
2017-12-03 08:00:23 +01:00
|
|
|
start_time = default_timer()
|
2019-02-17 22:35:09 +01:00
|
|
|
nb.throttle(last_io=datetime.now())
|
|
|
|
elapsed = default_timer() - start_time
|
2023-08-07 19:59:12 +02:00
|
|
|
assert elapsed > 0.48 and elapsed < 1.5
|
2019-02-17 22:35:09 +01:00
|
|
|
|
|
|
|
nb = NotifyBase()
|
|
|
|
nb.request_rate_per_sec = 1.0
|
|
|
|
|
|
|
|
# Set our time to now
|
|
|
|
start_time = default_timer()
|
|
|
|
nb.throttle(last_io=datetime.now())
|
|
|
|
elapsed = default_timer() - start_time
|
|
|
|
# because we told it that we had already done a previous action (now)
|
|
|
|
# the throttle holds out until the right time has passed
|
2023-08-07 19:59:12 +02:00
|
|
|
assert elapsed > 0.48 and elapsed < 1.5
|
2019-02-17 22:35:09 +01:00
|
|
|
|
|
|
|
# Concurrent calls could take up to the rate_per_sec though...
|
|
|
|
start_time = default_timer()
|
|
|
|
nb.throttle(last_io=datetime.now())
|
|
|
|
elapsed = default_timer() - start_time
|
2023-08-07 19:59:12 +02:00
|
|
|
assert elapsed > 0.48 and elapsed < 1.5
|
2019-02-17 22:35:09 +01:00
|
|
|
|
|
|
|
nb = NotifyBase()
|
|
|
|
start_time = default_timer()
|
|
|
|
nb.request_rate_per_sec = 1.0
|
|
|
|
# Force a time in the past
|
|
|
|
nb.throttle(last_io=(datetime.now() - timedelta(seconds=20)))
|
2017-12-03 08:00:23 +01:00
|
|
|
elapsed = default_timer() - start_time
|
|
|
|
# Should be a very fast response time since we set it to zero but we'll
|
|
|
|
# check for less then 500 to be fair as some testing systems may be slower
|
|
|
|
# then other
|
2019-02-17 22:35:09 +01:00
|
|
|
assert elapsed < 0.5
|
2017-12-03 08:00:23 +01:00
|
|
|
|
2019-03-10 00:35:15 +01:00
|
|
|
# Force a throttle time
|
|
|
|
start_time = default_timer()
|
|
|
|
nb.throttle(wait=0.5)
|
|
|
|
elapsed = default_timer() - start_time
|
2023-08-07 19:59:12 +02:00
|
|
|
assert elapsed > 0.48 and elapsed < 1.5
|
2019-03-10 00:35:15 +01:00
|
|
|
|
2017-12-03 08:00:23 +01:00
|
|
|
# our NotifyBase wasn't initialized with an ImageSize so this will fail
|
|
|
|
assert nb.image_url(notify_type=NotifyType.INFO) is None
|
|
|
|
assert nb.image_path(notify_type=NotifyType.INFO) is None
|
|
|
|
assert nb.image_raw(notify_type=NotifyType.INFO) is None
|
|
|
|
|
2018-02-26 01:59:37 +01:00
|
|
|
# Color handling
|
|
|
|
assert nb.color(notify_type='invalid') is None
|
2019-02-25 07:02:29 +01:00
|
|
|
assert isinstance(
|
|
|
|
nb.color(notify_type=NotifyType.INFO, color_type=None),
|
2022-10-08 02:28:36 +02:00
|
|
|
str)
|
2018-02-26 01:59:37 +01:00
|
|
|
assert isinstance(
|
|
|
|
nb.color(notify_type=NotifyType.INFO, color_type=int), int)
|
|
|
|
assert isinstance(
|
|
|
|
nb.color(notify_type=NotifyType.INFO, color_type=tuple), tuple)
|
|
|
|
|
2024-05-19 21:07:45 +02:00
|
|
|
# Ascii Handling
|
|
|
|
assert nb.ascii(notify_type='invalid') is None
|
|
|
|
assert nb.ascii(NotifyType.INFO) == '[i]'
|
|
|
|
assert nb.ascii(NotifyType.SUCCESS) == '[+]'
|
|
|
|
assert nb.ascii(NotifyType.WARNING) == '[~]'
|
|
|
|
assert nb.ascii(NotifyType.FAILURE) == '[!]'
|
|
|
|
|
2018-03-31 04:29:36 +02:00
|
|
|
# Create an object
|
|
|
|
nb = NotifyBase()
|
|
|
|
# Force an image size since the default doesn't have one
|
|
|
|
nb.image_size = NotifyImageSize.XY_256
|
2017-12-03 08:00:23 +01:00
|
|
|
|
2017-12-11 03:28:00 +01:00
|
|
|
# We'll get an object this time around
|
2017-12-03 08:00:23 +01:00
|
|
|
assert nb.image_url(notify_type=NotifyType.INFO) is not None
|
|
|
|
assert nb.image_path(notify_type=NotifyType.INFO) is not None
|
|
|
|
assert nb.image_raw(notify_type=NotifyType.INFO) is not None
|
|
|
|
|
|
|
|
# But we will not get a response with an invalid notification type
|
|
|
|
assert nb.image_url(notify_type='invalid') is None
|
|
|
|
assert nb.image_path(notify_type='invalid') is None
|
|
|
|
assert nb.image_raw(notify_type='invalid') is None
|
|
|
|
|
|
|
|
# Static function testing
|
|
|
|
assert NotifyBase.escape_html("<content>'\t \n</content>") == \
|
|
|
|
'<content>'  \n</content>'
|
|
|
|
|
|
|
|
assert NotifyBase.escape_html(
|
|
|
|
"<content>'\t \n</content>", convert_new_lines=True) == \
|
2022-07-01 15:31:06 +02:00
|
|
|
'<content>'  <br/></content>'
|
2017-12-03 08:00:23 +01:00
|
|
|
|
2019-03-27 04:06:20 +01:00
|
|
|
# Test invalid data
|
|
|
|
assert NotifyBase.split_path(None) == []
|
|
|
|
assert NotifyBase.split_path(object()) == []
|
|
|
|
assert NotifyBase.split_path(42) == []
|
|
|
|
|
2017-12-03 08:00:23 +01:00
|
|
|
assert NotifyBase.split_path(
|
|
|
|
'/path/?name=Dr%20Disrespect', unquote=False) == \
|
|
|
|
['path', '?name=Dr%20Disrespect']
|
|
|
|
|
|
|
|
assert NotifyBase.split_path(
|
|
|
|
'/path/?name=Dr%20Disrespect', unquote=True) == \
|
2019-03-27 04:06:20 +01:00
|
|
|
['path', '?name=Dr Disrespect']
|
|
|
|
|
|
|
|
# a slash found inside the path, if escaped properly will not be broken
|
|
|
|
# by split_path while additional concatinated slashes are ignored
|
|
|
|
# FYI: %2F = /
|
|
|
|
assert NotifyBase.split_path(
|
|
|
|
'/%2F///%2F%2F////%2F%2F%2F////', unquote=True) == \
|
|
|
|
['/', '//', '///']
|
|
|
|
|
|
|
|
# Test invalid data
|
|
|
|
assert NotifyBase.parse_list(None) == []
|
2021-07-28 16:32:10 +02:00
|
|
|
assert NotifyBase.parse_list(object()) == []
|
2019-10-09 18:39:31 +02:00
|
|
|
assert NotifyBase.parse_list(42) == []
|
2019-03-27 04:06:20 +01:00
|
|
|
|
|
|
|
result = NotifyBase.parse_list(
|
|
|
|
',path,?name=Dr%20Disrespect', unquote=False)
|
2024-04-21 01:00:31 +02:00
|
|
|
assert isinstance(result, list)
|
2019-03-27 04:06:20 +01:00
|
|
|
assert len(result) == 2
|
|
|
|
assert 'path' in result
|
|
|
|
assert '?name=Dr%20Disrespect' in result
|
|
|
|
|
|
|
|
result = NotifyBase.parse_list(',path,?name=Dr%20Disrespect', unquote=True)
|
2024-04-21 01:00:31 +02:00
|
|
|
assert isinstance(result, list)
|
2019-03-27 04:06:20 +01:00
|
|
|
assert len(result) == 2
|
|
|
|
assert 'path' in result
|
|
|
|
assert '?name=Dr Disrespect' in result
|
|
|
|
|
|
|
|
# by parse_list while additional concatinated slashes are ignored
|
|
|
|
# FYI: %2F = /
|
|
|
|
# In this lit there are actually 4 entries, however parse_list
|
|
|
|
# eliminates duplicates in addition to unquoting content by default
|
|
|
|
result = NotifyBase.parse_list(
|
|
|
|
',%2F,%2F%2F, , , ,%2F%2F%2F, %2F', unquote=True)
|
2024-04-21 01:00:31 +02:00
|
|
|
assert isinstance(result, list)
|
2019-03-27 04:06:20 +01:00
|
|
|
assert len(result) == 3
|
|
|
|
assert '/' in result
|
|
|
|
assert '//' in result
|
|
|
|
assert '///' in result
|
2017-12-03 08:00:23 +01:00
|
|
|
|
2021-07-28 16:32:10 +02:00
|
|
|
# Phone number parsing
|
|
|
|
assert NotifyBase.parse_phone_no(None) == []
|
|
|
|
assert NotifyBase.parse_phone_no(object()) == []
|
|
|
|
assert NotifyBase.parse_phone_no(42) == []
|
|
|
|
|
|
|
|
result = NotifyBase.parse_phone_no(
|
|
|
|
'+1-800-123-1234,(800) 123-4567', unquote=False)
|
2024-04-21 01:00:31 +02:00
|
|
|
assert isinstance(result, list)
|
2021-07-28 16:32:10 +02:00
|
|
|
assert len(result) == 2
|
|
|
|
assert '+1-800-123-1234' in result
|
|
|
|
assert '(800) 123-4567' in result
|
|
|
|
|
|
|
|
# %2B == +
|
|
|
|
result = NotifyBase.parse_phone_no(
|
|
|
|
'%2B1-800-123-1234,%2B1%20800%20123%204567', unquote=True)
|
2024-04-21 01:00:31 +02:00
|
|
|
assert isinstance(result, list)
|
2021-07-28 16:32:10 +02:00
|
|
|
assert len(result) == 2
|
|
|
|
assert '+1-800-123-1234' in result
|
|
|
|
assert '+1 800 123 4567' in result
|
|
|
|
|
2019-03-10 17:24:57 +01:00
|
|
|
# Give nothing, get nothing
|
|
|
|
assert NotifyBase.escape_html("") == ""
|
|
|
|
assert NotifyBase.escape_html(None) == ""
|
|
|
|
assert NotifyBase.escape_html(object()) == ""
|
|
|
|
|
2017-12-14 03:35:59 +01:00
|
|
|
# Test quote
|
|
|
|
assert NotifyBase.unquote('%20') == ' '
|
|
|
|
assert NotifyBase.quote(' ') == '%20'
|
|
|
|
assert NotifyBase.unquote(None) == ''
|
|
|
|
assert NotifyBase.quote(None) == ''
|
|
|
|
|
2017-12-03 08:00:23 +01:00
|
|
|
|
|
|
|
def test_notify_base_urls():
|
|
|
|
"""
|
|
|
|
API: NotifyBase() URLs
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Test verify switch whih is used as part of the SSL Verification
|
|
|
|
# by default all SSL sites are verified unless this flag is set to
|
|
|
|
# something like 'No', 'False', 'Disabled', etc. Boolean values are
|
|
|
|
# pretty forgiving.
|
|
|
|
results = NotifyBase.parse_url('https://localhost:8080/?verify=No')
|
|
|
|
assert 'verify' in results
|
|
|
|
assert results['verify'] is False
|
|
|
|
|
|
|
|
results = NotifyBase.parse_url('https://localhost:8080/?verify=Yes')
|
|
|
|
assert 'verify' in results
|
|
|
|
assert results['verify'] is True
|
|
|
|
|
|
|
|
# The default is to verify
|
|
|
|
results = NotifyBase.parse_url('https://localhost:8080')
|
|
|
|
assert 'verify' in results
|
|
|
|
assert results['verify'] is True
|
|
|
|
|
|
|
|
# Password Handling
|
|
|
|
|
|
|
|
# pass keyword over-rides default password
|
|
|
|
results = NotifyBase.parse_url('https://user:pass@localhost')
|
|
|
|
assert 'password' in results
|
|
|
|
assert results['password'] == "pass"
|
|
|
|
|
|
|
|
# pass keyword over-rides default password
|
|
|
|
results = NotifyBase.parse_url(
|
|
|
|
'https://user:pass@localhost?pass=newpassword')
|
|
|
|
assert 'password' in results
|
|
|
|
assert results['password'] == "newpassword"
|
|
|
|
|
2021-03-04 16:23:49 +01:00
|
|
|
# password keyword can also optionally be used
|
|
|
|
results = NotifyBase.parse_url(
|
|
|
|
'https://user:pass@localhost?password=passwd')
|
|
|
|
assert 'password' in results
|
|
|
|
assert results['password'] == "passwd"
|
|
|
|
|
|
|
|
# pass= override password=
|
|
|
|
# password keyword can also optionally be used
|
|
|
|
results = NotifyBase.parse_url(
|
|
|
|
'https://user:pass@localhost?pass=pw1&password=pw2')
|
|
|
|
assert 'password' in results
|
|
|
|
assert results['password'] == "pw1"
|
|
|
|
|
2019-02-18 04:21:59 +01:00
|
|
|
# Options
|
|
|
|
results = NotifyBase.parse_url('https://localhost?format=invalid')
|
|
|
|
assert 'format' not in results
|
|
|
|
results = NotifyBase.parse_url('https://localhost?format=text')
|
|
|
|
assert 'format' in results
|
|
|
|
assert results['format'] == 'text'
|
|
|
|
results = NotifyBase.parse_url('https://localhost?format=markdown')
|
|
|
|
assert 'format' in results
|
|
|
|
assert results['format'] == 'markdown'
|
|
|
|
results = NotifyBase.parse_url('https://localhost?format=html')
|
|
|
|
assert 'format' in results
|
|
|
|
assert results['format'] == 'html'
|
|
|
|
|
|
|
|
results = NotifyBase.parse_url('https://localhost?overflow=invalid')
|
|
|
|
assert 'overflow' not in results
|
|
|
|
results = NotifyBase.parse_url('https://localhost?overflow=upstream')
|
|
|
|
assert 'overflow' in results
|
|
|
|
assert results['overflow'] == 'upstream'
|
|
|
|
results = NotifyBase.parse_url('https://localhost?overflow=split')
|
|
|
|
assert 'overflow' in results
|
|
|
|
assert results['overflow'] == 'split'
|
|
|
|
results = NotifyBase.parse_url('https://localhost?overflow=truncate')
|
|
|
|
assert 'overflow' in results
|
|
|
|
assert results['overflow'] == 'truncate'
|
|
|
|
|
2017-12-03 08:00:23 +01:00
|
|
|
# User Handling
|
|
|
|
|
|
|
|
# user keyword over-rides default password
|
|
|
|
results = NotifyBase.parse_url('https://user:pass@localhost')
|
|
|
|
assert 'user' in results
|
|
|
|
assert results['user'] == "user"
|
|
|
|
|
|
|
|
# user keyword over-rides default password
|
|
|
|
results = NotifyBase.parse_url(
|
|
|
|
'https://user:pass@localhost?user=newuser')
|
|
|
|
assert 'user' in results
|
|
|
|
assert results['user'] == "newuser"
|
2017-12-06 06:35:03 +01:00
|
|
|
|
|
|
|
# Test invalid urls
|
|
|
|
assert NotifyBase.parse_url('https://:@/') is None
|
|
|
|
assert NotifyBase.parse_url('http://:@') is None
|
|
|
|
assert NotifyBase.parse_url('http://@') is None
|
|
|
|
assert NotifyBase.parse_url('http:///') is None
|
|
|
|
assert NotifyBase.parse_url('http://:test/') is None
|
|
|
|
assert NotifyBase.parse_url('http://pass:test/') is None
|