2021-11-25 21:20:22 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-13 23:08:01 +02:00
|
|
|
# BSD 2-Clause License
|
2021-11-25 21:20:22 +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>
|
2021-11-25 21:20:22 +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:
|
2021-11-25 21:20:22 +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.
|
2021-11-25 21:20:22 +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.
|
2021-11-25 21:20:22 +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.
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import requests
|
2022-10-08 02:28:36 +02:00
|
|
|
from unittest import mock
|
2022-09-01 02:05:40 +02:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
from json import dumps
|
|
|
|
from random import choice
|
|
|
|
from string import ascii_uppercase as str_alpha
|
|
|
|
from string import digits as str_num
|
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
from apprise import NotifyBase
|
2024-08-23 03:44:14 +02:00
|
|
|
from apprise import PersistentStoreMode
|
2021-11-25 21:20:22 +01:00
|
|
|
from apprise import NotifyType
|
|
|
|
from apprise import Apprise
|
|
|
|
from apprise import AppriseAsset
|
|
|
|
from apprise import AppriseAttachment
|
|
|
|
from apprise.common import OverflowMode
|
|
|
|
|
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
|
|
|
|
|
2022-10-08 02:28:36 +02:00
|
|
|
class AppriseURLTester:
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
# Some exception handling we'll use
|
|
|
|
req_exceptions = (
|
|
|
|
requests.ConnectionError(
|
|
|
|
0, 'requests.ConnectionError() not handled'),
|
|
|
|
requests.RequestException(
|
|
|
|
0, 'requests.RequestException() not handled'),
|
|
|
|
requests.HTTPError(
|
|
|
|
0, 'requests.HTTPError() not handled'),
|
|
|
|
requests.ReadTimeout(
|
|
|
|
0, 'requests.ReadTimeout() not handled'),
|
|
|
|
requests.TooManyRedirects(
|
|
|
|
0, 'requests.TooManyRedirects() not handled'),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Attachment Testing Directory
|
|
|
|
__test_var_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.dirname(__file__)), 'var')
|
|
|
|
|
|
|
|
# Our URLs we'll test against
|
|
|
|
__tests = []
|
|
|
|
|
|
|
|
# Define how many characters exist per line
|
|
|
|
row = 80
|
|
|
|
|
|
|
|
# Some variables we use to control the data we work with
|
|
|
|
body_len = 1024
|
|
|
|
title_len = 1024
|
|
|
|
|
|
|
|
def __init__(self, tests=None, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Our initialization
|
|
|
|
"""
|
|
|
|
# Create a large body and title with random data
|
|
|
|
self.body = ''.join(
|
|
|
|
choice(str_alpha + str_num + ' ') for _ in range(self.body_len))
|
|
|
|
self.body = '\r\n'.join(
|
|
|
|
[self.body[i: i + self.row]
|
|
|
|
for i in range(0, len(self.body), self.row)])
|
|
|
|
|
|
|
|
# Create our title using random data
|
|
|
|
self.title = ''.join(
|
|
|
|
choice(str_alpha + str_num) for _ in range(self.title_len))
|
|
|
|
|
|
|
|
if tests:
|
|
|
|
self.__tests = tests
|
|
|
|
|
|
|
|
def add(self, url, meta):
|
|
|
|
"""
|
|
|
|
Adds a test suite to our object
|
|
|
|
"""
|
|
|
|
self.__tests.append({
|
|
|
|
'url': url,
|
|
|
|
'meta': meta,
|
|
|
|
})
|
|
|
|
|
2024-08-23 03:44:14 +02:00
|
|
|
def run_all(self, tmpdir=None):
|
2021-11-25 21:20:22 +01:00
|
|
|
"""
|
|
|
|
Run all of our tests
|
|
|
|
"""
|
|
|
|
# iterate over our dictionary and test it out
|
|
|
|
for (url, meta) in self.__tests:
|
2024-08-23 03:44:14 +02:00
|
|
|
self.run(url, meta, tmpdir)
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
@mock.patch('requests.get')
|
|
|
|
@mock.patch('requests.post')
|
2024-05-28 02:10:34 +02:00
|
|
|
@mock.patch('requests.request')
|
2024-08-23 03:44:14 +02:00
|
|
|
def run(self, url, meta, tmpdir, mock_request, mock_post, mock_get):
|
2021-11-25 21:20:22 +01:00
|
|
|
"""
|
|
|
|
Run a specific test
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Our expected instance
|
|
|
|
instance = meta.get('instance', None)
|
|
|
|
|
|
|
|
# Our expected server objects
|
|
|
|
_self = meta.get('self', None)
|
|
|
|
|
|
|
|
# Our expected privacy url
|
|
|
|
# Don't set this if don't need to check it's value
|
|
|
|
privacy_url = meta.get('privacy_url')
|
|
|
|
|
|
|
|
# Our regular expression
|
|
|
|
url_matches = meta.get('url_matches')
|
|
|
|
|
2024-08-23 03:44:14 +02:00
|
|
|
# Detect our storage path (used to set persistent storage
|
|
|
|
# mode
|
|
|
|
storage_path = \
|
|
|
|
tmpdir if tmpdir and isinstance(tmpdir, str) and \
|
|
|
|
os.path.isdir(tmpdir) else None
|
|
|
|
|
|
|
|
# Our storage mode to set
|
|
|
|
storage_mode = meta.get(
|
|
|
|
'storage_mode',
|
|
|
|
PersistentStoreMode.MEMORY
|
|
|
|
if not storage_path else PersistentStoreMode.AUTO)
|
|
|
|
|
|
|
|
# Debug Mode
|
|
|
|
pdb = meta.get('pdb', False)
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Whether or not we should include an image with our request; unless
|
|
|
|
# otherwise specified, we assume that images are to be included
|
|
|
|
include_image = meta.get('include_image', True)
|
|
|
|
if include_image:
|
|
|
|
# a default asset
|
2024-08-23 03:44:14 +02:00
|
|
|
asset = AppriseAsset(
|
|
|
|
storage_mode=storage_mode,
|
|
|
|
storage_path=storage_path,
|
|
|
|
)
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Disable images
|
2024-08-23 03:44:14 +02:00
|
|
|
asset = AppriseAsset(
|
|
|
|
image_path_mask=False, image_url_mask=False,
|
|
|
|
storage_mode=storage_mode,
|
|
|
|
storage_path=storage_path,
|
|
|
|
)
|
2021-11-25 21:20:22 +01:00
|
|
|
asset.image_url_logo = None
|
|
|
|
|
|
|
|
# Mock our request object
|
|
|
|
robj = mock.Mock()
|
|
|
|
robj.content = u''
|
|
|
|
mock_get.return_value = robj
|
|
|
|
mock_post.return_value = robj
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.return_value = robj
|
2021-11-25 21:20:22 +01:00
|
|
|
|
2024-08-23 03:44:14 +02:00
|
|
|
if pdb:
|
|
|
|
# Makes it easier to debug with this peice of code
|
|
|
|
# just add `pdb': True to the call that is failing
|
|
|
|
import pdb
|
|
|
|
pdb.set_trace()
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
try:
|
|
|
|
# We can now instantiate our object:
|
|
|
|
obj = Apprise.instantiate(
|
|
|
|
url, asset=asset, suppress_exceptions=False)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# Handle our exception
|
|
|
|
if instance is None:
|
|
|
|
print('%s %s' % (url, str(e)))
|
|
|
|
raise e
|
|
|
|
|
|
|
|
if not isinstance(e, instance):
|
|
|
|
print('%s %s' % (url, str(e)))
|
|
|
|
raise e
|
|
|
|
|
|
|
|
# We're okay if we get here
|
|
|
|
return
|
|
|
|
|
|
|
|
if obj is None:
|
|
|
|
if instance is not None:
|
|
|
|
# We're done (assuming this is what we were
|
|
|
|
# expecting)
|
|
|
|
print("{} didn't instantiate itself "
|
|
|
|
"(we expected it to be a {})".format(
|
|
|
|
url, instance))
|
|
|
|
assert False
|
|
|
|
# We're done because we got the results we expected
|
|
|
|
return
|
|
|
|
|
|
|
|
if instance is None:
|
|
|
|
# Expected None but didn't get it
|
|
|
|
print('%s instantiated %s (but expected None)' % (
|
2023-08-27 19:19:36 +02:00
|
|
|
url, type(obj)))
|
2021-11-25 21:20:22 +01:00
|
|
|
assert False
|
|
|
|
|
|
|
|
if not isinstance(obj, instance):
|
|
|
|
print('%s instantiated %s (but expected %s)' % (
|
2024-10-06 19:28:20 +02:00
|
|
|
url, type(obj), instance))
|
2021-11-25 21:20:22 +01:00
|
|
|
assert False
|
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
if isinstance(obj, NotifyBase):
|
2021-12-28 18:35:03 +01:00
|
|
|
# Ensure we are not performing any type of thorttling
|
|
|
|
obj.request_rate_per_sec = 0
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# We loaded okay; now lets make sure we can reverse
|
|
|
|
# this url
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(obj.url(), str) is True
|
2021-11-25 21:20:22 +01:00
|
|
|
|
2024-08-23 03:44:14 +02:00
|
|
|
# Test that we support a url identifier
|
|
|
|
url_id = obj.url_id()
|
|
|
|
|
|
|
|
# It can be either disabled or a string; nothing else
|
|
|
|
assert isinstance(url_id, str) or \
|
|
|
|
(url_id is None and obj.url_identifier is False)
|
|
|
|
|
2023-05-13 04:05:46 +02:00
|
|
|
# Verify we can acquire a target count as an integer
|
|
|
|
assert isinstance(len(obj), int)
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Test url() with privacy=True
|
|
|
|
assert isinstance(
|
2022-10-08 02:28:36 +02:00
|
|
|
obj.url(privacy=True), str) is True
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
# Some Simple Invalid Instance Testing
|
|
|
|
assert instance.parse_url(None) is None
|
|
|
|
assert instance.parse_url(object) is None
|
|
|
|
assert instance.parse_url(42) is None
|
|
|
|
|
|
|
|
if privacy_url:
|
|
|
|
# Assess that our privacy url is as expected
|
2021-12-06 23:03:30 +01:00
|
|
|
if not obj.url(privacy=True).startswith(privacy_url):
|
2022-11-11 22:54:07 +01:00
|
|
|
print('Provided %s' % url)
|
2021-12-06 23:03:30 +01:00
|
|
|
raise AssertionError(
|
|
|
|
"Privacy URL: '{}' != expected '{}'".format(
|
|
|
|
obj.url(privacy=True)[:len(privacy_url)],
|
|
|
|
privacy_url))
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
if url_matches:
|
|
|
|
# Assess that our URL matches a set regex
|
|
|
|
assert re.search(url_matches, obj.url())
|
|
|
|
|
|
|
|
# Instantiate the exact same object again using the URL
|
|
|
|
# from the one that was already created properly
|
|
|
|
obj_cmp = Apprise.instantiate(obj.url())
|
|
|
|
|
2024-08-23 03:44:14 +02:00
|
|
|
# Our new object should produce the same url identifier
|
|
|
|
if obj.url_identifier != obj_cmp.url_identifier:
|
|
|
|
print('Provided %s' % url)
|
|
|
|
raise AssertionError(
|
|
|
|
"URL Identifier: '{}' != expected '{}'".format(
|
|
|
|
obj_cmp.url_identifier, obj.url_identifier))
|
|
|
|
|
|
|
|
# Back our check up
|
|
|
|
if obj.url_id() != obj_cmp.url_id():
|
|
|
|
print('Provided %s' % url)
|
|
|
|
raise AssertionError(
|
|
|
|
"URL ID(): '{}' != expected '{}'".format(
|
|
|
|
obj_cmp.url_id(), obj.url_id()))
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Our object should be the same instance as what we had
|
|
|
|
# originally expected above.
|
2022-10-09 11:28:18 +02:00
|
|
|
if not isinstance(obj_cmp, NotifyBase):
|
2021-11-25 21:20:22 +01:00
|
|
|
# Assert messages are hard to trace back with the
|
|
|
|
# way these tests work. Just printing before
|
|
|
|
# throwing our assertion failure makes things
|
|
|
|
# easier to debug later on
|
|
|
|
print('TEST FAIL: {} regenerated as {}'.format(
|
|
|
|
url, obj.url()))
|
|
|
|
assert False
|
|
|
|
|
2023-05-13 04:05:46 +02:00
|
|
|
# Verify there is no change from the old and the new
|
|
|
|
if len(obj) != len(obj_cmp):
|
|
|
|
print('%d targets found in %s' % (
|
|
|
|
len(obj), obj.url(privacy=True)))
|
|
|
|
print('But %d targets found in %s' % (
|
|
|
|
len(obj_cmp), obj_cmp.url(privacy=True)))
|
|
|
|
raise AssertionError("Target miscount %d != %d")
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Tidy our object
|
|
|
|
del obj_cmp
|
2024-08-23 03:44:14 +02:00
|
|
|
del instance
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
if _self:
|
|
|
|
# Iterate over our expected entries inside of our
|
|
|
|
# object
|
|
|
|
for key, val in self.items():
|
|
|
|
# Test that our object has the desired key
|
|
|
|
assert hasattr(key, obj) is True
|
|
|
|
assert getattr(key, obj) == val
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.__notify(url, obj, meta, asset)
|
|
|
|
|
|
|
|
except AssertionError:
|
|
|
|
# Don't mess with these entries
|
|
|
|
print('%s AssertionError' % url)
|
|
|
|
raise
|
|
|
|
|
2022-10-08 02:28:36 +02:00
|
|
|
# Tidy our object and allow any possible defined destructors to
|
2021-11-25 21:20:22 +01:00
|
|
|
# be executed.
|
|
|
|
del obj
|
|
|
|
|
|
|
|
@mock.patch('requests.get')
|
|
|
|
@mock.patch('requests.post')
|
2022-01-04 17:30:16 +01:00
|
|
|
@mock.patch('requests.head')
|
|
|
|
@mock.patch('requests.put')
|
|
|
|
@mock.patch('requests.delete')
|
2023-02-17 22:29:22 +01:00
|
|
|
@mock.patch('requests.patch')
|
2024-05-28 02:10:34 +02:00
|
|
|
@mock.patch('requests.request')
|
|
|
|
def __notify(
|
|
|
|
self,
|
|
|
|
url,
|
|
|
|
obj,
|
|
|
|
meta,
|
|
|
|
asset,
|
|
|
|
mock_request,
|
|
|
|
mock_patch,
|
|
|
|
mock_del,
|
|
|
|
mock_put,
|
|
|
|
mock_head,
|
|
|
|
mock_post,
|
|
|
|
mock_get
|
|
|
|
):
|
2021-11-25 21:20:22 +01:00
|
|
|
"""
|
|
|
|
Perform notification testing against object specified
|
|
|
|
"""
|
|
|
|
#
|
|
|
|
# Prepare our options
|
|
|
|
#
|
|
|
|
|
|
|
|
# Allow notification type override, otherwise default to INFO
|
|
|
|
notify_type = meta.get('notify_type', NotifyType.INFO)
|
|
|
|
|
|
|
|
# Whether or not we're testing exceptions or not
|
|
|
|
test_requests_exceptions = meta.get('test_requests_exceptions', False)
|
|
|
|
|
|
|
|
# Our expected Query response (True, False, or exception type)
|
|
|
|
response = meta.get('response', True)
|
|
|
|
|
|
|
|
# Our expected Notify response (True or False)
|
|
|
|
notify_response = meta.get('notify_response', response)
|
|
|
|
|
|
|
|
# Our expected Notify Attachment response (True or False)
|
|
|
|
attach_response = meta.get('attach_response', notify_response)
|
|
|
|
|
|
|
|
# Test attachments
|
|
|
|
# Don't set this if don't need to check it's value
|
|
|
|
check_attachments = meta.get('check_attachments', True)
|
|
|
|
|
|
|
|
# Allow us to force the server response code to be something other then
|
|
|
|
# the defaults
|
|
|
|
requests_response_code = meta.get(
|
|
|
|
'requests_response_code',
|
|
|
|
requests.codes.ok if response else requests.codes.not_found,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Allow us to force the server response text to be something other then
|
|
|
|
# the defaults
|
|
|
|
requests_response_text = meta.get('requests_response_text')
|
2022-12-02 15:39:29 +01:00
|
|
|
requests_response_content = None
|
|
|
|
|
|
|
|
if isinstance(requests_response_text, str):
|
|
|
|
requests_response_content = requests_response_text.encode('utf-8')
|
|
|
|
|
|
|
|
elif isinstance(requests_response_text, bytes):
|
|
|
|
requests_response_content = requests_response_text
|
|
|
|
requests_response_text = requests_response_text.decode('utf-8')
|
|
|
|
|
|
|
|
elif not isinstance(requests_response_text, str):
|
2021-11-25 21:20:22 +01:00
|
|
|
# Convert to string
|
|
|
|
requests_response_text = dumps(requests_response_text)
|
2022-12-02 15:39:29 +01:00
|
|
|
requests_response_content = requests_response_text.encode('utf-8')
|
2021-11-25 21:20:22 +01:00
|
|
|
|
2023-07-09 23:36:41 +02:00
|
|
|
else:
|
|
|
|
requests_response_content = u''
|
|
|
|
requests_response_text = ''
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# A request
|
|
|
|
robj = mock.Mock()
|
|
|
|
robj.content = u''
|
2023-07-09 23:36:41 +02:00
|
|
|
robj.text = ''
|
2021-11-25 21:20:22 +01:00
|
|
|
mock_get.return_value = robj
|
|
|
|
mock_post.return_value = robj
|
2022-01-04 17:30:16 +01:00
|
|
|
mock_head.return_value = robj
|
2023-02-17 22:29:22 +01:00
|
|
|
mock_patch.return_value = robj
|
2022-01-04 17:30:16 +01:00
|
|
|
mock_del.return_value = robj
|
|
|
|
mock_put.return_value = robj
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.return_value = robj
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
if test_requests_exceptions is False:
|
|
|
|
# Handle our default response
|
2022-01-04 17:30:16 +01:00
|
|
|
mock_put.return_value.status_code = requests_response_code
|
|
|
|
mock_head.return_value.status_code = requests_response_code
|
|
|
|
mock_del.return_value.status_code = requests_response_code
|
2021-11-25 21:20:22 +01:00
|
|
|
mock_post.return_value.status_code = requests_response_code
|
|
|
|
mock_get.return_value.status_code = requests_response_code
|
2023-02-17 22:29:22 +01:00
|
|
|
mock_patch.return_value.status_code = requests_response_code
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.return_value.status_code = requests_response_code
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
# Handle our default text response
|
2022-12-02 15:39:29 +01:00
|
|
|
mock_get.return_value.content = requests_response_content
|
|
|
|
mock_post.return_value.content = requests_response_content
|
|
|
|
mock_del.return_value.content = requests_response_content
|
|
|
|
mock_put.return_value.content = requests_response_content
|
|
|
|
mock_head.return_value.content = requests_response_content
|
2023-02-17 22:29:22 +01:00
|
|
|
mock_patch.return_value.content = requests_response_content
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.return_value.content = requests_response_content
|
2022-01-04 17:30:16 +01:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
mock_get.return_value.text = requests_response_text
|
|
|
|
mock_post.return_value.text = requests_response_text
|
2022-01-04 17:30:16 +01:00
|
|
|
mock_put.return_value.text = requests_response_text
|
|
|
|
mock_del.return_value.text = requests_response_text
|
|
|
|
mock_head.return_value.text = requests_response_text
|
2023-02-17 22:29:22 +01:00
|
|
|
mock_patch.return_value.text = requests_response_text
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.return_value.text = requests_response_text
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
# Ensure there is no side effect set
|
|
|
|
mock_post.side_effect = None
|
2022-01-04 17:30:16 +01:00
|
|
|
mock_del.side_effect = None
|
|
|
|
mock_put.side_effect = None
|
|
|
|
mock_head.side_effect = None
|
2021-11-25 21:20:22 +01:00
|
|
|
mock_get.side_effect = None
|
2023-02-17 22:29:22 +01:00
|
|
|
mock_patch.side_effect = None
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.side_effect = None
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Handle exception testing; first we turn the boolean flag
|
|
|
|
# into a list of exceptions
|
|
|
|
test_requests_exceptions = self.req_exceptions
|
|
|
|
|
|
|
|
try:
|
|
|
|
if test_requests_exceptions is False:
|
|
|
|
|
2023-05-13 04:05:46 +02:00
|
|
|
# Verify we can acquire a target count as an integer
|
|
|
|
targets = len(obj)
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# check that we're as expected
|
2023-10-07 23:40:41 +02:00
|
|
|
_resp = obj.notify(
|
2021-11-25 21:20:22 +01:00
|
|
|
body=self.body, title=self.title,
|
2023-10-07 23:40:41 +02:00
|
|
|
notify_type=notify_type)
|
|
|
|
if _resp != notify_response:
|
|
|
|
print('%s notify() returned %s (but expected %s)' % (
|
|
|
|
url, _resp, notify_response))
|
|
|
|
assert False
|
2021-11-25 21:20:22 +01:00
|
|
|
|
2023-05-13 04:05:46 +02:00
|
|
|
if notify_response:
|
|
|
|
# If we successfully got a response, there must have been
|
|
|
|
# at least 1 target present
|
|
|
|
assert targets > 0
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# check that this doesn't change using different overflow
|
|
|
|
# methods
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type,
|
|
|
|
overflow=OverflowMode.UPSTREAM) == notify_response
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type,
|
|
|
|
overflow=OverflowMode.TRUNCATE) == notify_response
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type,
|
|
|
|
overflow=OverflowMode.SPLIT) == notify_response
|
|
|
|
|
|
|
|
#
|
|
|
|
# Handle varations of the Asset Object missing fields
|
|
|
|
#
|
|
|
|
|
|
|
|
# First make a backup
|
|
|
|
app_id = asset.app_id
|
|
|
|
app_desc = asset.app_desc
|
|
|
|
|
|
|
|
# now clear records
|
|
|
|
asset.app_id = None
|
|
|
|
asset.app_desc = None
|
|
|
|
|
|
|
|
# Notify should still work
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type) == notify_response
|
|
|
|
|
|
|
|
# App ID only
|
|
|
|
asset.app_id = app_id
|
|
|
|
asset.app_desc = None
|
|
|
|
|
|
|
|
# Notify should still work
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type) == notify_response
|
|
|
|
|
|
|
|
# App Desc only
|
|
|
|
asset.app_id = None
|
|
|
|
asset.app_desc = app_desc
|
|
|
|
|
|
|
|
# Notify should still work
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type) == notify_response
|
|
|
|
|
|
|
|
# Restore
|
|
|
|
asset.app_id = app_id
|
|
|
|
asset.app_desc = app_desc
|
|
|
|
|
|
|
|
if check_attachments:
|
|
|
|
# Test single attachment support; even if the service
|
|
|
|
# doesn't support attachments, it should still
|
|
|
|
# gracefully ignore the data
|
|
|
|
attach = os.path.join(
|
|
|
|
self.__test_var_dir, 'apprise-test.gif')
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type,
|
|
|
|
attach=attach) == attach_response
|
|
|
|
|
|
|
|
# Same results should apply to a list of attachments
|
|
|
|
attach = AppriseAttachment((
|
|
|
|
os.path.join(self.__test_var_dir, 'apprise-test.gif'),
|
|
|
|
os.path.join(self.__test_var_dir, 'apprise-test.png'),
|
|
|
|
os.path.join(self.__test_var_dir, 'apprise-test.jpeg'),
|
|
|
|
))
|
2023-05-13 04:05:46 +02:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type,
|
|
|
|
attach=attach) == attach_response
|
|
|
|
|
2023-08-12 22:32:07 +02:00
|
|
|
if obj.attachment_support:
|
|
|
|
#
|
|
|
|
# Services that support attachments should support
|
|
|
|
# sending a attachment (or more) without a body or
|
|
|
|
# title specified:
|
|
|
|
#
|
|
|
|
assert obj.notify(
|
|
|
|
body=None, title=None,
|
|
|
|
notify_type=notify_type,
|
|
|
|
attach=attach) == attach_response
|
|
|
|
|
|
|
|
# Turn off attachment support on the notifications
|
|
|
|
# that support it so we can test that any logic we
|
|
|
|
# have ot test against this flag is ran
|
|
|
|
obj.attachment_support = False
|
|
|
|
|
|
|
|
#
|
|
|
|
# Notifications should still transmit as normal if
|
|
|
|
# Attachment support is flipped off
|
|
|
|
#
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
|
|
|
notify_type=notify_type,
|
|
|
|
attach=attach) == notify_response
|
|
|
|
|
|
|
|
#
|
|
|
|
# We should not be able to send a message without a
|
|
|
|
# body or title in this circumstance
|
|
|
|
#
|
|
|
|
assert obj.notify(
|
|
|
|
body=None, title=None,
|
|
|
|
notify_type=notify_type,
|
|
|
|
attach=attach) is False
|
|
|
|
|
|
|
|
# Toggle Back
|
|
|
|
obj.attachment_support = True
|
|
|
|
|
|
|
|
else: # No Attachment support
|
|
|
|
#
|
|
|
|
# We should not be able to send a message without a
|
|
|
|
# body or title in this circumstance
|
|
|
|
#
|
|
|
|
assert obj.notify(
|
|
|
|
body=None, title=None,
|
|
|
|
notify_type=notify_type,
|
|
|
|
attach=attach) is False
|
2021-11-25 21:20:22 +01:00
|
|
|
else:
|
|
|
|
|
|
|
|
for _exception in self.req_exceptions:
|
|
|
|
mock_post.side_effect = _exception
|
2022-01-04 17:30:16 +01:00
|
|
|
mock_head.side_effect = _exception
|
|
|
|
mock_del.side_effect = _exception
|
|
|
|
mock_put.side_effect = _exception
|
2021-11-25 21:20:22 +01:00
|
|
|
mock_get.side_effect = _exception
|
2023-02-17 22:29:22 +01:00
|
|
|
mock_patch.side_effect = _exception
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.side_effect = _exception
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body, title=self.title,
|
2024-08-23 03:44:14 +02:00
|
|
|
notify_type=notify_type) is False
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
except AssertionError:
|
|
|
|
# Don't mess with these entries
|
|
|
|
raise
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
# We can't handle this exception type
|
|
|
|
raise
|
|
|
|
|
|
|
|
except AssertionError:
|
|
|
|
# Don't mess with these entries
|
|
|
|
raise
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# Check that we were expecting this exception to happen
|
|
|
|
try:
|
|
|
|
if not isinstance(e, response):
|
|
|
|
raise e
|
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
print('%s Unhandled response %s' % (url, type(e)))
|
|
|
|
raise e
|
|
|
|
|
|
|
|
#
|
|
|
|
# Do the test again but without a title defined
|
|
|
|
#
|
|
|
|
try:
|
|
|
|
if test_requests_exceptions is False:
|
|
|
|
# check that we're as expected
|
|
|
|
assert obj.notify(body='body', notify_type=notify_type) \
|
|
|
|
== notify_response
|
|
|
|
|
|
|
|
else:
|
|
|
|
for _exception in self.req_exceptions:
|
|
|
|
mock_post.side_effect = _exception
|
2022-01-04 17:30:16 +01:00
|
|
|
mock_del.side_effect = _exception
|
|
|
|
mock_put.side_effect = _exception
|
|
|
|
mock_head.side_effect = _exception
|
2021-11-25 21:20:22 +01:00
|
|
|
mock_get.side_effect = _exception
|
2023-02-17 22:29:22 +01:00
|
|
|
mock_patch.side_effect = _exception
|
2024-05-28 02:10:34 +02:00
|
|
|
mock_request.side_effect = _exception
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
assert obj.notify(
|
|
|
|
body=self.body,
|
2024-08-23 03:44:14 +02:00
|
|
|
notify_type=notify_type) is False
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
except AssertionError:
|
|
|
|
# Don't mess with these entries
|
|
|
|
raise
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
# We can't handle this exception type
|
|
|
|
raise
|
|
|
|
|
|
|
|
except AssertionError:
|
|
|
|
# Don't mess with these entries
|
|
|
|
raise
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# Check that we were expecting this exception to happen
|
|
|
|
if not isinstance(e, response):
|
|
|
|
raise e
|
|
|
|
|
|
|
|
return True
|