1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
# -*- coding: utf-8 -*-
# BSD 2-Clause License
#
# Apprise - Push Notification Library.
# Copyright (c) 2025, Chris Caron <lead2gold@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 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.
#
# 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.
import os
from unittest import mock
import requests
import pytest
from json import dumps
from apprise.plugins.pushover import PushoverPriority, NotifyPushover
import apprise
from helpers import AppriseURLTester
# 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 = (
('pover://', {
'instance': TypeError,
}),
# bad url
('pover://:@/', {
'instance': TypeError,
}),
# APIkey; no user
('pover://%s' % ('a' * 30), {
'instance': TypeError,
}),
# API Key + custom sound setting
('pover://%s@%s?sound=mysound' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + valid alternate sound picked
('pover://%s@%s?sound=spacealarm' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + valid url_title with url
('pover://%s@%s?url=my-url&url_title=title' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + Valid User
('pover://%s@%s' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
# don't include an image by default
'include_image': False,
}),
# API Key + Valid User + 1 Device
('pover://%s@%s/DEVICE' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + Valid User + 1 Device (via to=)
('pover://%s@%s?to=DEVICE' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + Valid User + 2 Devices
('pover://%s@%s/DEVICE1/Device-with-dash/' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'pover://u...u@a...a',
}),
# API Key + Valid User + invalid device
('pover://%s@%s/%s/' % ('u' * 30, 'a' * 30, 'd' * 30), {
'instance': NotifyPushover,
# Notify will return False since there is a bad device in our list
'response': False,
}),
# API Key + Valid User + device + invalid device
('pover://%s@%s/DEVICE1/%s/' % ('u' * 30, 'a' * 30, 'd' * 30), {
'instance': NotifyPushover,
# Notify will return False since there is a bad device in our list
'response': False,
}),
# API Key + priority setting
('pover://%s@%s?priority=high' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + priority setting + html mode
('pover://%s@%s?priority=high&format=html' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + priority setting + markdown mode
('pover://%s@%s?priority=high&format=markdown' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + invalid priority setting
('pover://%s@%s?priority=invalid' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + emergency(2) priority setting
('pover://%s@%s?priority=emergency' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + emergency(2) priority setting (via numeric value
('pover://%s@%s?priority=2' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
# API Key + emergency priority setting with retry and expire
('pover://%s@%s?priority=emergency&%s&%s' % ('u' * 30,
'a' * 30,
'retry=30',
'expire=300'), {
'instance': NotifyPushover,
}),
# API Key + emergency priority setting with text retry
('pover://%s@%s?priority=emergency&%s&%s' % ('u' * 30,
'a' * 30,
'retry=invalid',
'expire=300'), {
'instance': NotifyPushover,
}),
# API Key + emergency priority setting with text expire
('pover://%s@%s?priority=emergency&%s&%s' % ('u' * 30,
'a' * 30,
'retry=30',
'expire=invalid'), {
'instance': NotifyPushover,
}),
# API Key + emergency priority setting with invalid expire
('pover://%s@%s?priority=emergency&%s' % ('u' * 30,
'a' * 30,
'expire=100000'), {
'instance': TypeError,
}),
# API Key + emergency priority setting with invalid retry
('pover://%s@%s?priority=emergency&%s' % ('u' * 30,
'a' * 30,
'retry=15'), {
'instance': TypeError,
}),
# API Key + priority setting (empty)
('pover://%s@%s?priority=' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
}),
('pover://%s@%s' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
# force a failure
'response': False,
'requests_response_code': requests.codes.internal_server_error,
}),
('pover://%s@%s' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
# throw a bizzare code forcing us to fail to look it up
'response': False,
'requests_response_code': 999,
}),
('pover://%s@%s' % ('u' * 30, 'a' * 30), {
'instance': NotifyPushover,
# 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_pushover_urls():
"""
NotifyPushover() Apprise URLs
"""
# Run our general tests
AppriseURLTester(tests=apprise_url_tests).run_all()
@mock.patch('requests.post')
def test_plugin_pushover_attachments(mock_post, tmpdir):
"""
NotifyPushover() Attachment Checks
"""
# Initialize some generic (but valid) tokens
user_key = 'u' * 30
api_token = 'a' * 30
# Prepare a good response
response = mock.Mock()
response.content = dumps(
{"status": 1, "request": "647d2300-702c-4b38-8b2f-d56326ae460b"})
response.status_code = requests.codes.ok
# Prepare a bad response
bad_response = mock.Mock()
response.content = dumps(
{"status": 1, "request": "647d2300-702c-4b38-8b2f-d56326ae460b"})
bad_response.status_code = requests.codes.internal_server_error
# Assign our good response
mock_post.return_value = response
# prepare our attachment
attach = apprise.AppriseAttachment(
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
# Instantiate our object
obj = apprise.Apprise.instantiate(
'pover://{}@{}/'.format(user_key, api_token))
assert isinstance(obj, NotifyPushover)
# Test our attachment
assert obj.notify(body="test", attach=attach) is True
# Test our call count
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'https://api.pushover.net/1/messages.json'
# Reset our mock object for multiple tests
mock_post.reset_mock()
# Test multiple attachments
assert attach.add(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
assert obj.notify(body="test", attach=attach) is True
# Test our call count
assert mock_post.call_count == 2
assert mock_post.call_args_list[0][0][0] == \
'https://api.pushover.net/1/messages.json'
assert mock_post.call_args_list[1][0][0] == \
'https://api.pushover.net/1/messages.json'
# Reset our mock object for multiple tests
mock_post.reset_mock()
image = tmpdir.mkdir("pover_image").join("test.jpg")
image.write('a' * NotifyPushover.attach_max_size_bytes)
attach = apprise.AppriseAttachment.instantiate(str(image))
assert obj.notify(body="test", attach=attach) is True
# Test our call count
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'https://api.pushover.net/1/messages.json'
# Reset our mock object for multiple tests
mock_post.reset_mock()
# Add 1 more byte to the file (putting it over the limit)
image.write(
'a' * (NotifyPushover.attach_max_size_bytes + 1))
attach = apprise.AppriseAttachment.instantiate(str(image))
assert obj.notify(body="test", attach=attach) is False
# Test our call count
assert mock_post.call_count == 0
# Test case when file is missing
attach = apprise.AppriseAttachment.instantiate(
'file://{}?cache=False'.format(str(image)))
os.unlink(str(image))
assert obj.notify(
body='body', title='title', attach=attach) is False
# Test our call count
assert mock_post.call_count == 0
# Test unsuported files:
image = tmpdir.mkdir("pover_unsupported").join("test.doc")
image.write('a' * 256)
attach = apprise.AppriseAttachment.instantiate(str(image))
# Content is silently ignored
assert obj.notify(body="test", attach=attach) is True
# prepare our attachment
attach = apprise.AppriseAttachment(
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
# Throw an exception on the first call to requests.post()
for side_effect in (requests.RequestException(), OSError(), bad_response):
mock_post.side_effect = [side_effect, side_effect]
# We'll fail now because of our error handling
assert obj.send(body="test", attach=attach) is False
# Same case without an attachment
assert obj.send(body="test") is False
@mock.patch('requests.post')
def test_plugin_pushover_edge_cases(mock_post):
"""
NotifyPushover() Edge Cases
"""
# No token
with pytest.raises(TypeError):
NotifyPushover(token=None)
# Initialize some generic (but valid) tokens
token = 'a' * 30
user_key = 'u' * 30
invalid_device = 'd' * 35
# Support strings
devices = 'device1,device2,,,,%s' % invalid_device
# Prepare Mock
mock_post.return_value = requests.Request()
mock_post.return_value.status_code = requests.codes.ok
# No webhook id specified
with pytest.raises(TypeError):
NotifyPushover(user_key=user_key, webhook_id=None)
obj = NotifyPushover(
user_key=user_key, token=token, targets=devices)
assert isinstance(obj, NotifyPushover)
# Our invalid device is ignored
assert len(obj.targets) == 2
# We notify the 2 devices loaded
assert obj.notify(
body='body', title='title',
notify_type=apprise.NotifyType.INFO) is True
obj = NotifyPushover(user_key=user_key, token=token)
assert isinstance(obj, NotifyPushover)
# Default is to send to all devices, so there will be a
# device defined here
assert len(obj.targets) == 1
# This call succeeds because all of the devices are valid
assert obj.notify(
body='body', title='title',
notify_type=apprise.NotifyType.INFO) is True
obj = NotifyPushover(
user_key=user_key, token=token, targets=set())
assert isinstance(obj, NotifyPushover)
# Default is to send to all devices, so there will be a
# device defined here
assert len(obj.targets) == 1
# No User Key specified
with pytest.raises(TypeError):
NotifyPushover(user_key=None, token="abcd")
# No Access Token specified
with pytest.raises(TypeError):
NotifyPushover(user_key="abcd", token=None)
with pytest.raises(TypeError):
NotifyPushover(user_key="abcd", token=" ")
@mock.patch('requests.post')
def test_plugin_pushover_config_files(mock_post):
"""
NotifyPushover() Config File Cases
"""
content = """
urls:
- pover://USER@TOKEN:
- priority: -2
tag: pushover_int low
- priority: "-2"
tag: pushover_str_int low
- priority: low
tag: pushover_str low
# This will take on normal (default) priority
- priority: invalid
tag: pushover_invalid
- pover://USER2@TOKEN2:
- priority: 2
tag: pushover_int emerg
- priority: "2"
tag: pushover_str_int emerg
- priority: emergency
tag: pushover_str emerg
"""
# Prepare Mock
mock_post.return_value = requests.Request()
mock_post.return_value.status_code = requests.codes.ok
# Create ourselves a config object
ac = apprise.AppriseConfig()
assert ac.add_config(content=content) is True
aobj = apprise.Apprise()
# Add our configuration
aobj.add(ac)
# We should be able to read our 7 servers from that
# 3x low
# 3x emerg
# 1x invalid (so takes on normal priority)
assert len(ac.servers()) == 7
assert len(aobj) == 7
assert len([x for x in aobj.find(tag='low')]) == 3
for s in aobj.find(tag='low'):
assert s.priority == PushoverPriority.LOW
assert len([x for x in aobj.find(tag='emerg')]) == 3
for s in aobj.find(tag='emerg'):
assert s.priority == PushoverPriority.EMERGENCY
assert len([x for x in aobj.find(tag='pushover_str')]) == 2
assert len([x for x in aobj.find(tag='pushover_str_int')]) == 2
assert len([x for x in aobj.find(tag='pushover_int')]) == 2
assert len([x for x in aobj.find(tag='pushover_invalid')]) == 1
assert next(aobj.find(tag='pushover_invalid')).priority == \
PushoverPriority.NORMAL
# Notifications work
# We test 'pushover_str_int' and 'low' which only matches 1 end point
assert aobj.notify(
title="title", body="body", tag=[('pushover_str_int', 'low')]) is True
# Notify everything loaded
assert aobj.notify(title="title", body="body") is True
|