| 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 |
from json import loads |
| 30 |
import logging |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
import apprise |
| 38 |
from apprise import NotifyFormat |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.gotify import GotifyPriority, NotifyGotify |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# Our Testing URLs |
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"gotify://", |
| 48 |
{ |
| 49 |
"instance": None, |
| 50 |
}, |
| 51 |
), |
| 52 |
# No token specified |
| 53 |
( |
| 54 |
"gotify://hostname", |
| 55 |
{ |
| 56 |
"instance": AppriseImproperlyConfigured, |
| 57 |
}, |
| 58 |
), |
| 59 |
# Provide a hostname and token |
| 60 |
( |
| 61 |
"gotify://hostname/%s" % ("t" * 16), |
| 62 |
{ |
| 63 |
"instance": NotifyGotify, |
| 64 |
# Our expected url(privacy=True) startswith() response: |
| 65 |
"privacy_url": "gotify://hostname/t...t", |
| 66 |
}, |
| 67 |
), |
| 68 |
# Provide a hostname, path, and token |
| 69 |
( |
| 70 |
"gotify://hostname/a/path/ending/in/a/slash/%s" % ("u" * 16), |
| 71 |
{ |
| 72 |
"instance": NotifyGotify, |
| 73 |
# Our expected url(privacy=True) startswith() response: |
| 74 |
"privacy_url": "gotify://hostname/a/path/ending/in/a/slash/u...u/", |
| 75 |
}, |
| 76 |
), |
| 77 |
# Markdown test |
| 78 |
( |
| 79 |
"gotify://hostname/%s?format=markdown" % ("t" * 16), |
| 80 |
{ |
| 81 |
"instance": NotifyGotify, |
| 82 |
}, |
| 83 |
), |
| 84 |
# Provide a hostname, path, and token |
| 85 |
( |
| 86 |
"gotify://hostname/a/path/not/ending/in/a/slash/%s" % ("v" * 16), |
| 87 |
{ |
| 88 |
"instance": NotifyGotify, |
| 89 |
# Our expected url(privacy=True) startswith() response: |
| 90 |
"privacy_url": ( |
| 91 |
"gotify://hostname/a/path/not/ending/in/a/slash/v...v/" |
| 92 |
), |
| 93 |
}, |
| 94 |
), |
| 95 |
# Provide a priority |
| 96 |
( |
| 97 |
"gotify://hostname/%s?priority=high" % ("i" * 16), |
| 98 |
{ |
| 99 |
"instance": NotifyGotify, |
| 100 |
}, |
| 101 |
), |
| 102 |
# Provide an invalid priority |
| 103 |
( |
| 104 |
"gotify://hostname:8008/%s?priority=invalid" % ("i" * 16), |
| 105 |
{ |
| 106 |
"instance": NotifyGotify, |
| 107 |
}, |
| 108 |
), |
| 109 |
# An invalid url |
| 110 |
( |
| 111 |
"gotify://:@/", |
| 112 |
{ |
| 113 |
"instance": None, |
| 114 |
}, |
| 115 |
), |
| 116 |
( |
| 117 |
"gotify://hostname/%s/" % ("t" * 16), |
| 118 |
{ |
| 119 |
"instance": NotifyGotify, |
| 120 |
# force a failure |
| 121 |
"response": False, |
| 122 |
"requests_response_code": requests.codes.internal_server_error, |
| 123 |
}, |
| 124 |
), |
| 125 |
( |
| 126 |
"gotifys://localhost/%s/" % ("t" * 16), |
| 127 |
{ |
| 128 |
"instance": NotifyGotify, |
| 129 |
# throw a bizarre code forcing us to fail to look it up |
| 130 |
"response": False, |
| 131 |
"requests_response_code": 999, |
| 132 |
}, |
| 133 |
), |
| 134 |
( |
| 135 |
"gotify://localhost/%s/" % ("t" * 16), |
| 136 |
{ |
| 137 |
"instance": NotifyGotify, |
| 138 |
# Throws a series of i/o exceptions with this flag |
| 139 |
# is set and tests that we gracefully handle them |
| 140 |
"test_requests_exceptions": True, |
| 141 |
}, |
| 142 |
), |
| 143 |
) |
| 144 |
|
| 145 |
|
| 146 |
def test_plugin_gotify_urls(): |
| 147 |
"""NotifyGotify() Apprise URLs.""" |
| 148 |
|
| 149 |
# Run our general tests |
| 150 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 151 |
|
| 152 |
|
| 153 |
def test_plugin_gotify_edge_cases(): |
| 154 |
"""NotifyGotify() Edge Cases.""" |
| 155 |
# Initializes the plugin with an invalid token |
| 156 |
with pytest.raises(AppriseImproperlyConfigured): |
| 157 |
NotifyGotify(token=None) |
| 158 |
# Whitespace also acts as an invalid token value |
| 159 |
with pytest.raises(AppriseImproperlyConfigured): |
| 160 |
NotifyGotify(token=" ") |
| 161 |
|
| 162 |
|
| 163 |
@mock.patch("requests.post") |
| 164 |
def test_plugin_gotify_config_files(mock_post): |
| 165 |
"""NotifyGotify() Config File Cases.""" |
| 166 |
content = """ |
| 167 |
urls: |
| 168 |
- gotify://hostname/{}: |
| 169 |
- priority: 0 |
| 170 |
tag: gotify_int low |
| 171 |
- priority: "0" |
| 172 |
tag: gotify_str_int low |
| 173 |
# We want to make sure our '1' does not match the '10' entry |
| 174 |
- priority: "1" |
| 175 |
tag: gotify_str_int low |
| 176 |
- priority: low |
| 177 |
tag: gotify_str low |
| 178 |
|
| 179 |
# This will take on moderate (default) priority |
| 180 |
- priority: invalid |
| 181 |
tag: gotify_invalid |
| 182 |
|
| 183 |
- gotify://hostname/{}: |
| 184 |
- priority: 10 |
| 185 |
tag: gotify_int emerg |
| 186 |
- priority: "10" |
| 187 |
tag: gotify_str_int emerg |
| 188 |
- priority: emergency |
| 189 |
tag: gotify_str emerg |
| 190 |
""".format("a" * 16, "b" * 16) |
| 191 |
|
| 192 |
# Prepare Mock |
| 193 |
mock_post.return_value = requests.Request() |
| 194 |
mock_post.return_value.status_code = requests.codes.ok |
| 195 |
|
| 196 |
# Create ourselves a config object |
| 197 |
ac = apprise.AppriseConfig() |
| 198 |
assert ac.add_config(content=content) is True |
| 199 |
|
| 200 |
aobj = apprise.Apprise() |
| 201 |
|
| 202 |
# Add our configuration |
| 203 |
aobj.add(ac) |
| 204 |
|
| 205 |
# We should be able to read our 8 services from that |
| 206 |
# 4x low |
| 207 |
# 3x emerg |
| 208 |
# 1x invalid (so takes on normal priority) |
| 209 |
assert len(ac.services()) == 8 |
| 210 |
assert len(aobj) == 8 |
| 211 |
assert len(list(aobj.find(tag="low"))) == 4 |
| 212 |
for s in aobj.find(tag="low"): |
| 213 |
assert s.priority == GotifyPriority.LOW |
| 214 |
|
| 215 |
assert len(list(aobj.find(tag="emerg"))) == 3 |
| 216 |
for s in aobj.find(tag="emerg"): |
| 217 |
assert s.priority == GotifyPriority.EMERGENCY |
| 218 |
|
| 219 |
assert len(list(aobj.find(tag="gotify_str"))) == 2 |
| 220 |
assert len(list(aobj.find(tag="gotify_str_int"))) == 3 |
| 221 |
assert len(list(aobj.find(tag="gotify_int"))) == 2 |
| 222 |
|
| 223 |
assert len(list(aobj.find(tag="gotify_invalid"))) == 1 |
| 224 |
assert ( |
| 225 |
next(aobj.find(tag="gotify_invalid")).priority == GotifyPriority.NORMAL |
| 226 |
) |
| 227 |
|
| 228 |
|
| 229 |
@mock.patch("requests.post") |
| 230 |
def test_plugin_gotify_html_to_markdown_format(mock_post): |
| 231 |
"""NotifyGotify(): HTML body is converted to Markdown.""" |
| 232 |
|
| 233 |
# Prepare Mock |
| 234 |
mock_post.return_value = requests.Request() |
| 235 |
mock_post.return_value.status_code = requests.codes.ok |
| 236 |
|
| 237 |
# Instantiate a Gotify plugin in Markdown mode |
| 238 |
aobj = apprise.Apprise() |
| 239 |
assert aobj.add("gotify://hostname/{}?format=markdown".format("t" * 16)) |
| 240 |
|
| 241 |
# Notify with an HTML body; the framework should convert it |
| 242 |
# to Markdown before dispatching to Gotify |
| 243 |
assert ( |
| 244 |
bool( |
| 245 |
aobj.notify( |
| 246 |
body="<b>hello</b> <i>world</i>", |
| 247 |
body_format=NotifyFormat.HTML, |
| 248 |
) |
| 249 |
) |
| 250 |
is True |
| 251 |
) |
| 252 |
assert mock_post.call_count == 1 |
| 253 |
|
| 254 |
# The body must arrive as Markdown, not as stripped plain text |
| 255 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 256 |
assert payload["message"] == "**hello** *world*" |