| 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 |
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.plugins.gotify import GotifyPriority, NotifyGotify |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"gotify://", |
| 47 |
{ |
| 48 |
"instance": None, |
| 49 |
}, |
| 50 |
), |
| 51 |
|
| 52 |
( |
| 53 |
"gotify://hostname", |
| 54 |
{ |
| 55 |
"instance": TypeError, |
| 56 |
}, |
| 57 |
), |
| 58 |
|
| 59 |
( |
| 60 |
"gotify://hostname/%s" % ("t" * 16), |
| 61 |
{ |
| 62 |
"instance": NotifyGotify, |
| 63 |
|
| 64 |
"privacy_url": "gotify://hostname/t...t", |
| 65 |
}, |
| 66 |
), |
| 67 |
|
| 68 |
( |
| 69 |
"gotify://hostname/a/path/ending/in/a/slash/%s" % ("u" * 16), |
| 70 |
{ |
| 71 |
"instance": NotifyGotify, |
| 72 |
|
| 73 |
"privacy_url": "gotify://hostname/a/path/ending/in/a/slash/u...u/", |
| 74 |
}, |
| 75 |
), |
| 76 |
|
| 77 |
( |
| 78 |
"gotify://hostname/%s?format=markdown" % ("t" * 16), |
| 79 |
{ |
| 80 |
"instance": NotifyGotify, |
| 81 |
}, |
| 82 |
), |
| 83 |
|
| 84 |
( |
| 85 |
"gotify://hostname/a/path/not/ending/in/a/slash/%s" % ("v" * 16), |
| 86 |
{ |
| 87 |
"instance": NotifyGotify, |
| 88 |
|
| 89 |
"privacy_url": ( |
| 90 |
"gotify://hostname/a/path/not/ending/in/a/slash/v...v/" |
| 91 |
), |
| 92 |
}, |
| 93 |
), |
| 94 |
|
| 95 |
( |
| 96 |
"gotify://hostname/%s?priority=high" % ("i" * 16), |
| 97 |
{ |
| 98 |
"instance": NotifyGotify, |
| 99 |
}, |
| 100 |
), |
| 101 |
|
| 102 |
( |
| 103 |
"gotify://hostname:8008/%s?priority=invalid" % ("i" * 16), |
| 104 |
{ |
| 105 |
"instance": NotifyGotify, |
| 106 |
}, |
| 107 |
), |
| 108 |
|
| 109 |
( |
| 110 |
"gotify://:@/", |
| 111 |
{ |
| 112 |
"instance": None, |
| 113 |
}, |
| 114 |
), |
| 115 |
( |
| 116 |
"gotify://hostname/%s/" % ("t" * 16), |
| 117 |
{ |
| 118 |
"instance": NotifyGotify, |
| 119 |
|
| 120 |
"response": False, |
| 121 |
"requests_response_code": requests.codes.internal_server_error, |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
"gotifys://localhost/%s/" % ("t" * 16), |
| 126 |
{ |
| 127 |
"instance": NotifyGotify, |
| 128 |
|
| 129 |
"response": False, |
| 130 |
"requests_response_code": 999, |
| 131 |
}, |
| 132 |
), |
| 133 |
( |
| 134 |
"gotify://localhost/%s/" % ("t" * 16), |
| 135 |
{ |
| 136 |
"instance": NotifyGotify, |
| 137 |
|
| 138 |
|
| 139 |
"test_requests_exceptions": True, |
| 140 |
}, |
| 141 |
), |
| 142 |
) |
| 143 |
|
| 144 |
|
| 145 |
def test_plugin_gotify_urls(): |
| 146 |
"""NotifyGotify() Apprise URLs.""" |
| 147 |
|
| 148 |
|
| 149 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 150 |
|
| 151 |
|
| 152 |
def test_plugin_gotify_edge_cases(): |
| 153 |
"""NotifyGotify() Edge Cases.""" |
| 154 |
|
| 155 |
with pytest.raises(TypeError): |
| 156 |
NotifyGotify(token=None) |
| 157 |
|
| 158 |
with pytest.raises(TypeError): |
| 159 |
NotifyGotify(token=" ") |
| 160 |
|
| 161 |
|
| 162 |
@mock.patch("requests.post") |
| 163 |
def test_plugin_gotify_config_files(mock_post): |
| 164 |
"""NotifyGotify() Config File Cases.""" |
| 165 |
content = """ |
| 166 |
urls: |
| 167 |
- gotify://hostname/{}: |
| 168 |
- priority: 0 |
| 169 |
tag: gotify_int low |
| 170 |
- priority: "0" |
| 171 |
tag: gotify_str_int low |
| 172 |
# We want to make sure our '1' does not match the '10' entry |
| 173 |
- priority: "1" |
| 174 |
tag: gotify_str_int low |
| 175 |
- priority: low |
| 176 |
tag: gotify_str low |
| 177 |
|
| 178 |
# This will take on moderate (default) priority |
| 179 |
- priority: invalid |
| 180 |
tag: gotify_invalid |
| 181 |
|
| 182 |
- gotify://hostname/{}: |
| 183 |
- priority: 10 |
| 184 |
tag: gotify_int emerg |
| 185 |
- priority: "10" |
| 186 |
tag: gotify_str_int emerg |
| 187 |
- priority: emergency |
| 188 |
tag: gotify_str emerg |
| 189 |
""".format("a" * 16, "b" * 16) |
| 190 |
|
| 191 |
|
| 192 |
mock_post.return_value = requests.Request() |
| 193 |
mock_post.return_value.status_code = requests.codes.ok |
| 194 |
|
| 195 |
|
| 196 |
ac = apprise.AppriseConfig() |
| 197 |
assert ac.add_config(content=content) is True |
| 198 |
|
| 199 |
aobj = apprise.Apprise() |
| 200 |
|
| 201 |
|
| 202 |
aobj.add(ac) |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
assert len(ac.servers()) == 8 |
| 209 |
assert len(aobj) == 8 |
| 210 |
assert len(list(aobj.find(tag="low"))) == 4 |
| 211 |
for s in aobj.find(tag="low"): |
| 212 |
assert s.priority == GotifyPriority.LOW |
| 213 |
|
| 214 |
assert len(list(aobj.find(tag="emerg"))) == 3 |
| 215 |
for s in aobj.find(tag="emerg"): |
| 216 |
assert s.priority == GotifyPriority.EMERGENCY |
| 217 |
|
| 218 |
assert len(list(aobj.find(tag="gotify_str"))) == 2 |
| 219 |
assert len(list(aobj.find(tag="gotify_str_int"))) == 3 |
| 220 |
assert len(list(aobj.find(tag="gotify_int"))) == 2 |
| 221 |
|
| 222 |
assert len(list(aobj.find(tag="gotify_invalid"))) == 1 |
| 223 |
assert ( |
| 224 |
next(aobj.find(tag="gotify_invalid")).priority == GotifyPriority.NORMAL |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
@mock.patch("requests.post") |
| 229 |
def test_plugin_gotify_html_to_markdown_format(mock_post): |
| 230 |
"""NotifyGotify(): HTML body is converted to Markdown.""" |
| 231 |
|
| 232 |
|
| 233 |
mock_post.return_value = requests.Request() |
| 234 |
mock_post.return_value.status_code = requests.codes.ok |
| 235 |
|
| 236 |
|
| 237 |
aobj = apprise.Apprise() |
| 238 |
assert aobj.add("gotify://hostname/{}?format=markdown".format("t" * 16)) |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
assert ( |
| 243 |
aobj.notify( |
| 244 |
body="<b>hello</b> <i>world</i>", |
| 245 |
body_format=NotifyFormat.HTML, |
| 246 |
) |
| 247 |
is True |
| 248 |
) |
| 249 |
assert mock_post.call_count == 1 |
| 250 |
|
| 251 |
|
| 252 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 253 |
assert payload["message"] == "**hello** *world*" |