|
25 | 25 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | 26 | # POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
|
28 | | -from json import dumps |
| 28 | +from json import dumps, loads |
29 | 29 |
|
30 | 30 | # Disable logging for a cleaner testing output |
31 | 31 | import logging |
32 | 32 | import os |
| 33 | +from unittest import mock |
33 | 34 |
|
34 | 35 | from helpers import AppriseURLTester |
35 | 36 | import requests |
36 | 37 |
|
| 38 | +from apprise import Apprise, NotifyType |
37 | 39 | from apprise.plugins.notificationapi import NotifyNotificationAPI |
38 | 40 |
|
39 | 41 | logging.disable(logging.CRITICAL) |
|
127 | 129 | "instance": NotifyNotificationAPI, |
128 | 130 | "requests_response_text": NOTIFICATIONAPI_GOOD_RESPONSE, |
129 | 131 | }), |
130 | | - ( "napi://user@client_id/cs7/id:[email protected]", { |
| 132 | + ( "napi://user@client_id/cs7/id:[email protected]", { |
131 | 133 | # An email with a designated to email |
132 | 134 | "instance": NotifyNotificationAPI, |
133 | 135 | "requests_response_text": NOTIFICATIONAPI_GOOD_RESPONSE, |
134 | | - }), |
| 136 | + }), |
135 | 137 | ( "napi://user@client_id/cs8/id:[email protected]" |
136 | 138 | "?to=id:Chris<[email protected]>", { |
137 | 139 | # An email with a full name in in To field |
@@ -221,3 +223,151 @@ def test_plugin_napi_urls(): |
221 | 223 |
|
222 | 224 | # Run our general tests |
223 | 225 | AppriseURLTester(tests=apprise_url_tests).run_all() |
| 226 | + |
| 227 | + |
| 228 | +@mock.patch("requests.post") |
| 229 | +def test_plugin_napi_sms_payloads(mock_post): |
| 230 | + """NotifyNotificationAPI() Testing SMS Payloads.""" |
| 231 | + |
| 232 | + okay_response = requests.Request() |
| 233 | + okay_response.status_code = requests.codes.ok |
| 234 | + okay_response.content = NOTIFICATIONAPI_GOOD_RESPONSE |
| 235 | + |
| 236 | + # Assign our mock object our return value |
| 237 | + mock_post.return_value = okay_response |
| 238 | + |
| 239 | + # Details |
| 240 | + client_id = "my_id" |
| 241 | + client_secret = "my_secret" |
| 242 | + message_type = "apprise-post" |
| 243 | + phone_no = "userid:+1-555-123-4567" |
| 244 | + |
| 245 | + obj = Apprise.instantiate( |
| 246 | + f"napi://{message_type}@{client_id}/{client_secret}/" |
| 247 | + f"{phone_no}") |
| 248 | + assert isinstance(obj, NotifyNotificationAPI) |
| 249 | + assert isinstance(obj.url(), str) |
| 250 | + |
| 251 | + # No calls made yet |
| 252 | + assert mock_post.call_count == 0 |
| 253 | + |
| 254 | + # Send our notification |
| 255 | + assert ( |
| 256 | + obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 257 | + is True |
| 258 | + ) |
| 259 | + |
| 260 | + # 2 calls were made, one to perform an email lookup, the second |
| 261 | + # was the notification itself |
| 262 | + assert mock_post.call_count == 1 |
| 263 | + assert ( |
| 264 | + mock_post.call_args_list[0][0][0] |
| 265 | + == f"https://api.notificationapi.com/{client_id}/sender" |
| 266 | + ) |
| 267 | + |
| 268 | + payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 269 | + assert payload == { |
| 270 | + "type": "apprise-post", |
| 271 | + "to": { |
| 272 | + "id": "userid", |
| 273 | + "number": "15551234567", |
| 274 | + }, |
| 275 | + "parameters": { |
| 276 | + "appBody": "body", |
| 277 | + "appTitle": "", |
| 278 | + "appType": "info", |
| 279 | + "appId": "Apprise", |
| 280 | + "appDescription": "Apprise Notifications", |
| 281 | + "appColor": "#3AA3E3", |
| 282 | + "appImageUrl": ( |
| 283 | + "https://github.com/caronc/apprise/raw/master/apprise" |
| 284 | + "/assets/themes/default/apprise-info-72x72.png"), |
| 285 | + "appUrl": "https://github.com/caronc/apprise"}, |
| 286 | + } |
| 287 | + headers = mock_post.call_args_list[0][1]["headers"] |
| 288 | + assert headers == { |
| 289 | + "User-Agent": "Apprise", |
| 290 | + "Content-Type": "application/json", |
| 291 | + "Authorization": "Basic bXlfaWQ6bXlfc2VjcmV0"} |
| 292 | + |
| 293 | + # Reset our mock object |
| 294 | + mock_post.reset_mock() |
| 295 | + |
| 296 | + |
| 297 | +@mock.patch("requests.post") |
| 298 | +def test_plugin_napi_email_payloads(mock_post): |
| 299 | + """NotifyNotificationAPI() Testing Email Payloads.""" |
| 300 | + |
| 301 | + okay_response = requests.Request() |
| 302 | + okay_response.status_code = requests.codes.ok |
| 303 | + okay_response.content = NOTIFICATIONAPI_GOOD_RESPONSE |
| 304 | + |
| 305 | + # Assign our mock object our return value |
| 306 | + mock_post.return_value = okay_response |
| 307 | + |
| 308 | + # Details |
| 309 | + client_id = "my_id_abc" |
| 310 | + client_secret = "my_secret" |
| 311 | + message_type = "apprise-post" |
| 312 | + email = "userid:[email protected]" |
| 313 | + |
| 314 | + obj = Apprise.instantiate( |
| 315 | + f"napi://{message_type}@{client_id}/{client_secret}/" |
| 316 | + |
| 317 | + f"[email protected]&:customToken=customValue") |
| 318 | + assert isinstance(obj, NotifyNotificationAPI) |
| 319 | + assert isinstance(obj.url(), str) |
| 320 | + |
| 321 | + # No calls made yet |
| 322 | + assert mock_post.call_count == 0 |
| 323 | + |
| 324 | + # Send our notification |
| 325 | + assert ( |
| 326 | + obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 327 | + is True |
| 328 | + ) |
| 329 | + |
| 330 | + # 2 calls were made, one to perform an email lookup, the second |
| 331 | + # was the notification itself |
| 332 | + assert mock_post.call_count == 1 |
| 333 | + assert ( |
| 334 | + mock_post.call_args_list[0][0][0] |
| 335 | + == f"https://api.notificationapi.com/{client_id}/sender" |
| 336 | + ) |
| 337 | + |
| 338 | + payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 339 | + assert payload == { |
| 340 | + "type": "apprise-post", |
| 341 | + "to": { |
| 342 | + "id": "userid", |
| 343 | + |
| 344 | + }, |
| 345 | + "options": { |
| 346 | + "email": { |
| 347 | + "fromAddress": "[email protected]", |
| 348 | + "fromName": "Chris", |
| 349 | + "ccAddresses": [ "[email protected]"], |
| 350 | + "bccAddresses": [ "[email protected]"]} |
| 351 | + }, |
| 352 | + "parameters": { |
| 353 | + "customToken": "customValue", |
| 354 | + "appBody": "body", |
| 355 | + "appTitle": "", |
| 356 | + "appType": "info", |
| 357 | + "appId": "Apprise", |
| 358 | + "appDescription": "Apprise Notifications", |
| 359 | + "appColor": "#3AA3E3", |
| 360 | + "appImageUrl": ( |
| 361 | + "https://github.com/caronc/apprise/raw/master/apprise/" |
| 362 | + "assets/themes/default/apprise-info-72x72.png"), |
| 363 | + "appUrl": "https://github.com/caronc/apprise" |
| 364 | + }, |
| 365 | + } |
| 366 | + headers = mock_post.call_args_list[0][1]["headers"] |
| 367 | + assert headers == { |
| 368 | + 'User-Agent': 'Apprise', |
| 369 | + 'Content-Type': 'application/json', |
| 370 | + 'Authorization': 'Basic bXlfaWRfYWJjOm15X3NlY3JldA=='} |
| 371 | + |
| 372 | + # Reset our mock object |
| 373 | + mock_post.reset_mock() |
0 commit comments