| 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 |
import logging |
| 30 |
import os |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 38 |
from apprise.plugins.resend import NotifyResend |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 44 |
|
| 45 |
|
| 46 |
UUID4 = "re_FmABCDEF_987654321zqbabc123abc8fw" |
| 47 |
|
| 48 |
|
| 49 |
apprise_url_tests = ( |
| 50 |
( |
| 51 |
"resend://", |
| 52 |
{ |
| 53 |
"instance": TypeError, |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"resend://:@/", |
| 58 |
{ |
| 59 |
"instance": TypeError, |
| 60 |
}, |
| 61 |
), |
| 62 |
( |
| 63 |
"resend://abcd", |
| 64 |
{ |
| 65 |
|
| 66 |
"instance": TypeError, |
| 67 |
}, |
| 68 |
), |
| 69 |
( |
| 70 |
"resend://abcd@host", |
| 71 |
{ |
| 72 |
|
| 73 |
"instance": TypeError, |
| 74 |
}, |
| 75 |
), |
| 76 |
( |
| 77 |
"resend://invalid-api-key+*-d:[email protected]", |
| 78 |
{ |
| 79 |
|
| 80 |
"instance": TypeError, |
| 81 |
}, |
| 82 |
), |
| 83 |
( |
| 84 |
"resend://abcd:[email protected]", |
| 85 |
{ |
| 86 |
|
| 87 |
|
| 88 |
"instance": NotifyResend, |
| 89 |
}, |
| 90 |
), |
| 91 |
( |
| 92 |
"resend://abcd:[email protected]/[email protected]", |
| 93 |
{ |
| 94 |
|
| 95 |
"instance": NotifyResend, |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
"resend://abcd:[email protected]/[email protected]?name=Jessica", |
| 100 |
{ |
| 101 |
|
| 102 |
"instance": NotifyResend, |
| 103 |
"privacy_url": "resend://a...d:[email protected]/[email protected]", |
| 104 |
"url_matches": r"name=Jessica", |
| 105 |
}, |
| 106 |
), |
| 107 |
( |
| 108 |
( |
| 109 |
"resend://abcd@newuser4%40example.com?name=Ralph" |
| 110 |
"&[email protected]" |
| 111 |
), |
| 112 |
{ |
| 113 |
|
| 114 |
"instance": NotifyResend, |
| 115 |
"privacy_url": "resend://a...d:[email protected]/", |
| 116 |
"url_matches": r"name=Ralph", |
| 117 |
"force_debug": True, |
| 118 |
}, |
| 119 |
), |
| 120 |
( |
| 121 |
( |
| 122 |
"resend://?apikey=abcd&from=Joe<[email protected]>" |
| 123 |
"&[email protected]" |
| 124 |
), |
| 125 |
{ |
| 126 |
|
| 127 |
"instance": NotifyResend, |
| 128 |
"privacy_url": "resend://a...d:[email protected]/[email protected]", |
| 129 |
"url_matches": r"name=Joe", |
| 130 |
}, |
| 131 |
), |
| 132 |
( |
| 133 |
( |
| 134 |
"resend://?apikey=abcd&from=Joe<[email protected]>" |
| 135 |
"&reply=John<[email protected]>" |
| 136 |
), |
| 137 |
{ |
| 138 |
|
| 139 |
"instance": NotifyResend, |
| 140 |
"privacy_url": "resend://a...d:[email protected]", |
| 141 |
"url_matches": r"reply=John", |
| 142 |
}, |
| 143 |
), |
| 144 |
( |
| 145 |
("resend://?apikey=abcd&from=Joe<[email protected]>&reply=garbage%"), |
| 146 |
{ |
| 147 |
|
| 148 |
"instance": NotifyResend, |
| 149 |
}, |
| 150 |
), |
| 151 |
( |
| 152 |
( |
| 153 |
"resend://abcd:[email protected]/[email protected]" |
| 154 |
"[email protected]" |
| 155 |
), |
| 156 |
{ |
| 157 |
|
| 158 |
"instance": NotifyResend, |
| 159 |
}, |
| 160 |
), |
| 161 |
( |
| 162 |
"resend://abcd:[email protected]/[email protected][email protected]", |
| 163 |
{ |
| 164 |
|
| 165 |
"instance": NotifyResend, |
| 166 |
}, |
| 167 |
), |
| 168 |
( |
| 169 |
( |
| 170 |
"resend://abcd:[email protected]/[email protected]?" |
| 171 |
"cc=Chris<[email protected]>" |
| 172 |
), |
| 173 |
{ |
| 174 |
|
| 175 |
"instance": NotifyResend, |
| 176 |
}, |
| 177 |
), |
| 178 |
( |
| 179 |
"resend://abcd:[email protected]/[email protected][email protected]", |
| 180 |
{ |
| 181 |
|
| 182 |
"instance": NotifyResend, |
| 183 |
}, |
| 184 |
), |
| 185 |
( |
| 186 |
"resend://abcd:[email protected]/[email protected]", |
| 187 |
{ |
| 188 |
"instance": NotifyResend, |
| 189 |
|
| 190 |
"response": False, |
| 191 |
"requests_response_code": requests.codes.internal_server_error, |
| 192 |
}, |
| 193 |
), |
| 194 |
( |
| 195 |
"resend://abcd:[email protected]/[email protected]", |
| 196 |
{ |
| 197 |
"instance": NotifyResend, |
| 198 |
|
| 199 |
"response": False, |
| 200 |
"requests_response_code": 999, |
| 201 |
}, |
| 202 |
), |
| 203 |
( |
| 204 |
"resend://abcd:[email protected]/[email protected]", |
| 205 |
{ |
| 206 |
"instance": NotifyResend, |
| 207 |
|
| 208 |
|
| 209 |
"test_requests_exceptions": True, |
| 210 |
}, |
| 211 |
), |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
def test_plugin_resend_urls(): |
| 216 |
"""NotifyResend() Apprise URLs.""" |
| 217 |
|
| 218 |
|
| 219 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 220 |
|
| 221 |
|
| 222 |
@mock.patch("requests.get") |
| 223 |
@mock.patch("requests.post") |
| 224 |
def test_plugin_resend_edge_cases(mock_post, mock_get): |
| 225 |
"""NotifyResend() Edge Cases.""" |
| 226 |
|
| 227 |
|
| 228 |
with pytest.raises(TypeError): |
| 229 |
NotifyResend(apikey=None, from_addr="[email protected]") |
| 230 |
|
| 231 |
|
| 232 |
with pytest.raises(TypeError): |
| 233 |
NotifyResend(apikey="abcd", from_addr="!invalid") |
| 234 |
|
| 235 |
|
| 236 |
with pytest.raises(TypeError): |
| 237 |
NotifyResend(apikey="abcd", from_addr=None) |
| 238 |
|
| 239 |
|
| 240 |
NotifyResend( |
| 241 |
apikey="abcd", from_addr="[email protected]", targets="!invalid" |
| 242 |
) |
| 243 |
|
| 244 |
|
| 245 |
assert isinstance( |
| 246 |
NotifyResend( |
| 247 |
apikey="abcd", |
| 248 |
from_addr="[email protected]", |
| 249 |
bcc=("[email protected]", "!invalid"), |
| 250 |
cc=("[email protected]", "!invalid"), |
| 251 |
), |
| 252 |
NotifyResend, |
| 253 |
) |
| 254 |
|
| 255 |
|
| 256 |
@mock.patch("requests.get") |
| 257 |
@mock.patch("requests.post") |
| 258 |
def test_plugin_resend_attachments(mock_post, mock_get): |
| 259 |
"""NotifyResend() Attachments.""" |
| 260 |
|
| 261 |
request = mock.Mock() |
| 262 |
request.status_code = requests.codes.ok |
| 263 |
|
| 264 |
|
| 265 |
mock_post.return_value = request |
| 266 |
mock_get.return_value = request |
| 267 |
|
| 268 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 269 |
attach = AppriseAttachment(path) |
| 270 |
obj = Apprise.instantiate("resend://abcd:[email protected]") |
| 271 |
assert isinstance(obj, NotifyResend) |
| 272 |
assert ( |
| 273 |
obj.notify( |
| 274 |
body="body", |
| 275 |
title="title", |
| 276 |
notify_type=NotifyType.INFO, |
| 277 |
attach=attach, |
| 278 |
) |
| 279 |
is True |
| 280 |
) |
| 281 |
|
| 282 |
mock_post.reset_mock() |
| 283 |
mock_get.reset_mock() |
| 284 |
|
| 285 |
|
| 286 |
with mock.patch("os.path.isfile", return_value=False): |
| 287 |
assert ( |
| 288 |
obj.notify( |
| 289 |
body="body", |
| 290 |
title="title", |
| 291 |
notify_type=NotifyType.INFO, |
| 292 |
attach=attach, |
| 293 |
) |
| 294 |
is False |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
with mock.patch("builtins.open", side_effect=OSError): |
| 299 |
assert ( |
| 300 |
obj.notify( |
| 301 |
body="body", |
| 302 |
title="title", |
| 303 |
notify_type=NotifyType.INFO, |
| 304 |
attach=attach, |
| 305 |
) |
| 306 |
is False |
| 307 |
) |