| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
from helpers import AppriseURLTester |
| 33 |
import pytest |
| 34 |
import requests |
| 35 |
|
| 36 |
from apprise.exception import AppriseImproperlyConfigured |
| 37 |
from apprise.plugins.flock import NotifyFlock |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
# Our Testing URLs |
| 42 |
apprise_url_tests = ( |
| 43 |
( |
| 44 |
"flock://", |
| 45 |
{ |
| 46 |
"instance": AppriseImproperlyConfigured, |
| 47 |
}, |
| 48 |
), |
| 49 |
# An invalid url |
| 50 |
( |
| 51 |
"flock://:@/", |
| 52 |
{ |
| 53 |
"instance": AppriseImproperlyConfigured, |
| 54 |
}, |
| 55 |
), |
| 56 |
# Provide a token |
| 57 |
( |
| 58 |
"flock://%s" % ("t" * 24), |
| 59 |
{ |
| 60 |
"instance": NotifyFlock, |
| 61 |
}, |
| 62 |
), |
| 63 |
# Image handling |
| 64 |
( |
| 65 |
"flock://%s?image=True" % ("t" * 24), |
| 66 |
{ |
| 67 |
"instance": NotifyFlock, |
| 68 |
# Our expected url(privacy=True) startswith() response: |
| 69 |
"privacy_url": "flock://t...t", |
| 70 |
}, |
| 71 |
), |
| 72 |
( |
| 73 |
"flock://%s?image=False" % ("t" * 24), |
| 74 |
{ |
| 75 |
"instance": NotifyFlock, |
| 76 |
}, |
| 77 |
), |
| 78 |
( |
| 79 |
"flock://%s?image=True" % ("t" * 24), |
| 80 |
{ |
| 81 |
"instance": NotifyFlock, |
| 82 |
# Run test when image is set to True, but one couldn't actually be |
| 83 |
# loaded from the Asset Object. |
| 84 |
"include_image": False, |
| 85 |
}, |
| 86 |
), |
| 87 |
# Test to= |
| 88 |
( |
| 89 |
"flock://{}?to=u:{}&format=markdown".format("i" * 24, "u" * 12), |
| 90 |
{ |
| 91 |
"instance": NotifyFlock, |
| 92 |
}, |
| 93 |
), |
| 94 |
# Provide markdown format |
| 95 |
( |
| 96 |
"flock://%s?format=markdown" % ("i" * 24), |
| 97 |
{ |
| 98 |
"instance": NotifyFlock, |
| 99 |
}, |
| 100 |
), |
| 101 |
# Provide text format |
| 102 |
( |
| 103 |
"flock://%s?format=text" % ("i" * 24), |
| 104 |
{ |
| 105 |
"instance": NotifyFlock, |
| 106 |
}, |
| 107 |
), |
| 108 |
# Native URL Support, take the slack URL and still build from it |
| 109 |
( |
| 110 |
"https://api.flock.com/hooks/sendMessage/{}/".format("i" * 24), |
| 111 |
{ |
| 112 |
"instance": NotifyFlock, |
| 113 |
}, |
| 114 |
), |
| 115 |
# Native URL Support with arguments |
| 116 |
( |
| 117 |
"https://api.flock.com/hooks/sendMessage/{}/?format=markdown".format( |
| 118 |
"i" * 24 |
| 119 |
), |
| 120 |
{ |
| 121 |
"instance": NotifyFlock, |
| 122 |
}, |
| 123 |
), |
| 124 |
# Bot API presumed if one or more targets are specified |
| 125 |
# Provide markdown format |
| 126 |
( |
| 127 |
"flock://{}/u:{}?format=markdown".format("i" * 24, "u" * 12), |
| 128 |
{ |
| 129 |
"instance": NotifyFlock, |
| 130 |
}, |
| 131 |
), |
| 132 |
# Bot API presumed if one or more targets are specified |
| 133 |
# Provide text format |
| 134 |
( |
| 135 |
"flock://{}/u:{}?format=html".format("i" * 24, "u" * 12), |
| 136 |
{ |
| 137 |
"instance": NotifyFlock, |
| 138 |
}, |
| 139 |
), |
| 140 |
# Bot API presumed if one or more targets are specified |
| 141 |
# u: is optional |
| 142 |
( |
| 143 |
"flock://{}/{}?format=text".format("i" * 24, "u" * 12), |
| 144 |
{ |
| 145 |
"instance": NotifyFlock, |
| 146 |
}, |
| 147 |
), |
| 148 |
# Bot API presumed if one or more targets are specified |
| 149 |
# Multi-entries |
| 150 |
( |
| 151 |
"flock://{}/g:{}/u:{}?format=text".format( |
| 152 |
"i" * 24, "g" * 12, "u" * 12 |
| 153 |
), |
| 154 |
{ |
| 155 |
"instance": NotifyFlock, |
| 156 |
}, |
| 157 |
), |
| 158 |
# Bot API presumed if one or more targets are specified |
| 159 |
# Multi-entries using @ for user and # for channel |
| 160 |
( |
| 161 |
"flock://{}/#{}/@{}?format=text".format("i" * 24, "g" * 12, "u" * 12), |
| 162 |
{ |
| 163 |
"instance": NotifyFlock, |
| 164 |
}, |
| 165 |
), |
| 166 |
# Bot API presumed if one or more targets are specified |
| 167 |
# has bad entry |
| 168 |
( |
| 169 |
"flock://{}/g:{}/u:{}?format=text".format( |
| 170 |
"i" * 24, "g" * 12, "u" * 10 |
| 171 |
), |
| 172 |
{ |
| 173 |
"instance": NotifyFlock, |
| 174 |
}, |
| 175 |
), |
| 176 |
# Invalid user/group defined |
| 177 |
( |
| 178 |
"flock://%s/g:/u:?format=text" % ("i" * 24), |
| 179 |
{ |
| 180 |
"instance": AppriseImproperlyConfigured, |
| 181 |
}, |
| 182 |
), |
| 183 |
# we don't focus on the invalid length of the user/group fields. |
| 184 |
# As a result, the following will load and pass the data upstream |
| 185 |
( |
| 186 |
"flock://{}/g:{}/u:{}?format=text".format( |
| 187 |
"i" * 24, "g" * 14, "u" * 10 |
| 188 |
), |
| 189 |
{ |
| 190 |
# We will still instantiate the object |
| 191 |
"instance": NotifyFlock, |
| 192 |
}, |
| 193 |
), |
| 194 |
# Error Testing |
| 195 |
( |
| 196 |
"flock://{}/g:{}/u:{}?format=text".format( |
| 197 |
"i" * 24, "g" * 12, "u" * 10 |
| 198 |
), |
| 199 |
{ |
| 200 |
"instance": NotifyFlock, |
| 201 |
# force a failure |
| 202 |
"response": False, |
| 203 |
"requests_response_code": requests.codes.internal_server_error, |
| 204 |
}, |
| 205 |
), |
| 206 |
( |
| 207 |
"flock://%s/" % ("t" * 24), |
| 208 |
{ |
| 209 |
"instance": NotifyFlock, |
| 210 |
# force a failure |
| 211 |
"response": False, |
| 212 |
"requests_response_code": requests.codes.internal_server_error, |
| 213 |
}, |
| 214 |
), |
| 215 |
( |
| 216 |
"flock://%s/" % ("t" * 24), |
| 217 |
{ |
| 218 |
"instance": NotifyFlock, |
| 219 |
# throw a bizarre code forcing us to fail to look it up |
| 220 |
"response": False, |
| 221 |
"requests_response_code": 999, |
| 222 |
}, |
| 223 |
), |
| 224 |
( |
| 225 |
"flock://%s/" % ("t" * 24), |
| 226 |
{ |
| 227 |
"instance": NotifyFlock, |
| 228 |
# Throws a series of i/o exceptions with this flag |
| 229 |
# is set and tests that we gracefully handle them |
| 230 |
"test_requests_exceptions": True, |
| 231 |
}, |
| 232 |
), |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
def test_plugin_flock_urls(): |
| 237 |
"""NotifyFlock() Apprise URLs.""" |
| 238 |
|
| 239 |
# Run our general tests |
| 240 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 241 |
|
| 242 |
|
| 243 |
@mock.patch("requests.get") |
| 244 |
@mock.patch("requests.post") |
| 245 |
def test_plugin_flock_edge_cases(mock_post, mock_get): |
| 246 |
"""NotifyFlock() Edge Cases.""" |
| 247 |
|
| 248 |
# Initializes the plugin with an invalid token |
| 249 |
with pytest.raises(AppriseImproperlyConfigured): |
| 250 |
NotifyFlock(token=None) |
| 251 |
# Whitespace also acts as an invalid token value |
| 252 |
with pytest.raises(AppriseImproperlyConfigured): |
| 253 |
NotifyFlock(token=" ") |