| 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 |
from json import dumps |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
import os |
| 33 |
from unittest import mock |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import AppriseAttachment, NotifyType |
| 40 |
from apprise.plugins.pushsafer import NotifyPushSafer |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
|
| 45 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 46 |
|
| 47 |
|
| 48 |
apprise_url_tests = ( |
| 49 |
( |
| 50 |
"psafer://:@/", |
| 51 |
{ |
| 52 |
"instance": TypeError, |
| 53 |
}, |
| 54 |
), |
| 55 |
( |
| 56 |
"psafer://", |
| 57 |
{ |
| 58 |
"instance": TypeError, |
| 59 |
}, |
| 60 |
), |
| 61 |
( |
| 62 |
"psafers://", |
| 63 |
{ |
| 64 |
"instance": TypeError, |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
"psafer://{}".format("a" * 20), |
| 69 |
{ |
| 70 |
"instance": NotifyPushSafer, |
| 71 |
|
| 72 |
|
| 73 |
"notify_response": False, |
| 74 |
}, |
| 75 |
), |
| 76 |
( |
| 77 |
"psafer://{}".format("b" * 20), |
| 78 |
{ |
| 79 |
"instance": NotifyPushSafer, |
| 80 |
|
| 81 |
"requests_response_text": "{", |
| 82 |
"notify_response": False, |
| 83 |
}, |
| 84 |
), |
| 85 |
( |
| 86 |
"psafer://{}".format("c" * 20), |
| 87 |
{ |
| 88 |
"instance": NotifyPushSafer, |
| 89 |
|
| 90 |
|
| 91 |
"requests_response_text": {"status": 0, "error": "we failed"}, |
| 92 |
"notify_response": False, |
| 93 |
}, |
| 94 |
), |
| 95 |
( |
| 96 |
"psafers://{}".format("d" * 20), |
| 97 |
{ |
| 98 |
"instance": NotifyPushSafer, |
| 99 |
|
| 100 |
|
| 101 |
"requests_response_text": { |
| 102 |
"status": 0, |
| 103 |
}, |
| 104 |
"notify_response": False, |
| 105 |
}, |
| 106 |
), |
| 107 |
|
| 108 |
( |
| 109 |
"psafer://{}".format("e" * 20), |
| 110 |
{ |
| 111 |
"instance": NotifyPushSafer, |
| 112 |
|
| 113 |
"requests_response_text": { |
| 114 |
"status": 1, |
| 115 |
}, |
| 116 |
}, |
| 117 |
), |
| 118 |
|
| 119 |
( |
| 120 |
"psafer://{}/12/24/53".format("e" * 20), |
| 121 |
{ |
| 122 |
"instance": NotifyPushSafer, |
| 123 |
|
| 124 |
"requests_response_text": { |
| 125 |
"status": 1, |
| 126 |
}, |
| 127 |
}, |
| 128 |
), |
| 129 |
|
| 130 |
( |
| 131 |
"psafer://{}?to=12,24,53".format("e" * 20), |
| 132 |
{ |
| 133 |
"instance": NotifyPushSafer, |
| 134 |
|
| 135 |
"requests_response_text": { |
| 136 |
"status": 1, |
| 137 |
}, |
| 138 |
}, |
| 139 |
), |
| 140 |
|
| 141 |
( |
| 142 |
"psafer://{}?priority=emergency".format("f" * 20), |
| 143 |
{ |
| 144 |
"instance": NotifyPushSafer, |
| 145 |
"requests_response_text": { |
| 146 |
"status": 1, |
| 147 |
}, |
| 148 |
}, |
| 149 |
), |
| 150 |
|
| 151 |
( |
| 152 |
"psafer://{}?priority=-1".format("f" * 20), |
| 153 |
{ |
| 154 |
"instance": NotifyPushSafer, |
| 155 |
"requests_response_text": { |
| 156 |
"status": 1, |
| 157 |
}, |
| 158 |
"force_debug": True, |
| 159 |
}, |
| 160 |
), |
| 161 |
|
| 162 |
( |
| 163 |
"psafer://{}?priority=invalid".format("f" * 20), |
| 164 |
{ |
| 165 |
|
| 166 |
"instance": TypeError, |
| 167 |
}, |
| 168 |
), |
| 169 |
|
| 170 |
( |
| 171 |
"psafer://{}?priority=25".format("f" * 20), |
| 172 |
{ |
| 173 |
|
| 174 |
"instance": TypeError, |
| 175 |
}, |
| 176 |
), |
| 177 |
|
| 178 |
( |
| 179 |
"psafer://{}?sound=ok".format("g" * 20), |
| 180 |
{ |
| 181 |
"instance": NotifyPushSafer, |
| 182 |
"requests_response_text": { |
| 183 |
"status": 1, |
| 184 |
}, |
| 185 |
}, |
| 186 |
), |
| 187 |
|
| 188 |
( |
| 189 |
"psafers://{}?sound=14".format("g" * 20), |
| 190 |
{ |
| 191 |
"instance": NotifyPushSafer, |
| 192 |
"requests_response_text": { |
| 193 |
"status": 1, |
| 194 |
}, |
| 195 |
"privacy_url": "psafers://g...g", |
| 196 |
}, |
| 197 |
), |
| 198 |
|
| 199 |
( |
| 200 |
"psafer://{}?sound=invalid".format("h" * 20), |
| 201 |
{ |
| 202 |
|
| 203 |
"instance": TypeError, |
| 204 |
}, |
| 205 |
), |
| 206 |
( |
| 207 |
"psafer://{}?sound=94000".format("h" * 20), |
| 208 |
{ |
| 209 |
|
| 210 |
"instance": TypeError, |
| 211 |
}, |
| 212 |
), |
| 213 |
|
| 214 |
( |
| 215 |
"psafers://{}?vibration=1".format("h" * 20), |
| 216 |
{ |
| 217 |
"instance": NotifyPushSafer, |
| 218 |
"requests_response_text": { |
| 219 |
"status": 1, |
| 220 |
}, |
| 221 |
"privacy_url": "psafers://h...h", |
| 222 |
}, |
| 223 |
), |
| 224 |
|
| 225 |
( |
| 226 |
"psafer://{}?vibration=invalid".format("h" * 20), |
| 227 |
{ |
| 228 |
|
| 229 |
"instance": TypeError, |
| 230 |
}, |
| 231 |
), |
| 232 |
|
| 233 |
( |
| 234 |
"psafer://{}?vibration=25000".format("h" * 20), |
| 235 |
{ |
| 236 |
|
| 237 |
"instance": TypeError, |
| 238 |
}, |
| 239 |
), |
| 240 |
( |
| 241 |
"psafers://{}".format("d" * 20), |
| 242 |
{ |
| 243 |
"instance": NotifyPushSafer, |
| 244 |
|
| 245 |
|
| 246 |
"requests_response_text": { |
| 247 |
"status": 0, |
| 248 |
}, |
| 249 |
|
| 250 |
"response": False, |
| 251 |
"requests_response_code": requests.codes.internal_server_error, |
| 252 |
}, |
| 253 |
), |
| 254 |
( |
| 255 |
"psafer://{}".format("d" * 20), |
| 256 |
{ |
| 257 |
"instance": NotifyPushSafer, |
| 258 |
|
| 259 |
|
| 260 |
"requests_response_text": { |
| 261 |
"status": 0, |
| 262 |
}, |
| 263 |
|
| 264 |
"response": False, |
| 265 |
"requests_response_code": 999, |
| 266 |
}, |
| 267 |
), |
| 268 |
( |
| 269 |
"psafers://{}".format("d" * 20), |
| 270 |
{ |
| 271 |
"instance": NotifyPushSafer, |
| 272 |
|
| 273 |
|
| 274 |
"requests_response_text": { |
| 275 |
"status": 0, |
| 276 |
}, |
| 277 |
|
| 278 |
|
| 279 |
"test_requests_exceptions": True, |
| 280 |
}, |
| 281 |
), |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
def test_plugin_pushsafer_urls(): |
| 286 |
"""NotifyPushSafer() Apprise URLs.""" |
| 287 |
|
| 288 |
|
| 289 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 290 |
|
| 291 |
|
| 292 |
@mock.patch("requests.post") |
| 293 |
def test_plugin_pushsafer_general(mock_post): |
| 294 |
"""NotifyPushSafer() General Tests.""" |
| 295 |
|
| 296 |
|
| 297 |
privatekey = "abc123" |
| 298 |
|
| 299 |
|
| 300 |
mock_post.return_value = requests.Request() |
| 301 |
mock_post.return_value.status_code = requests.codes.ok |
| 302 |
mock_post.return_value.content = dumps( |
| 303 |
{ |
| 304 |
"status": 1, |
| 305 |
"success": "okay", |
| 306 |
} |
| 307 |
) |
| 308 |
|
| 309 |
|
| 310 |
with pytest.raises(TypeError): |
| 311 |
NotifyPushSafer(privatekey=None) |
| 312 |
|
| 313 |
|
| 314 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 315 |
attach = AppriseAttachment() |
| 316 |
for _ in range(0, 4): |
| 317 |
attach.add(path) |
| 318 |
|
| 319 |
obj = NotifyPushSafer(privatekey=privatekey) |
| 320 |
assert ( |
| 321 |
obj.notify( |
| 322 |
body="body", |
| 323 |
title="title", |
| 324 |
notify_type=NotifyType.INFO, |
| 325 |
attach=attach, |
| 326 |
) |
| 327 |
is True |
| 328 |
) |
| 329 |
|
| 330 |
|
| 331 |
with mock.patch("builtins.open", side_effect=OSError): |
| 332 |
obj.notify( |
| 333 |
body="body", |
| 334 |
title="title", |
| 335 |
notify_type=NotifyType.INFO, |
| 336 |
attach=attach, |
| 337 |
) |
| 338 |
|
| 339 |
|
| 340 |
attach = AppriseAttachment(path) |
| 341 |
|
| 342 |
attach[0]._mimetype = "application/octet-stream" |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
mock_post.reset_mock() |
| 347 |
assert ( |
| 348 |
obj.notify( |
| 349 |
body="body", |
| 350 |
title="title", |
| 351 |
notify_type=NotifyType.INFO, |
| 352 |
attach=attach, |
| 353 |
) |
| 354 |
is True |
| 355 |
) |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
assert "data" in mock_post.call_args[1] |
| 360 |
assert "p" not in mock_post.call_args[1]["data"] |
| 361 |
assert "p2" not in mock_post.call_args[1]["data"] |
| 362 |
assert "p3" not in mock_post.call_args[1]["data"] |
| 363 |
|
| 364 |
|
| 365 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 366 |
assert ( |
| 367 |
obj.notify( |
| 368 |
body="body", |
| 369 |
title="title", |
| 370 |
notify_type=NotifyType.INFO, |
| 371 |
attach=path, |
| 372 |
) |
| 373 |
is False |
| 374 |
) |