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