| 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 datetime import datetime |
| 29 |
from json import dumps, loads |
| 30 |
|
| 31 |
|
| 32 |
import logging |
| 33 |
import os |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
from helpers import AppriseURLTester |
| 37 |
import pytest |
| 38 |
import requests |
| 39 |
|
| 40 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 41 |
from apprise.plugins.office365 import NotifyOffice365 |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
|
| 46 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 47 |
|
| 48 |
|
| 49 |
apprise_url_tests = ( |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
( |
| 54 |
"o365://", |
| 55 |
{ |
| 56 |
|
| 57 |
"instance": TypeError, |
| 58 |
}, |
| 59 |
), |
| 60 |
( |
| 61 |
"o365://:@/", |
| 62 |
{ |
| 63 |
|
| 64 |
"instance": TypeError, |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}".format( |
| 69 |
|
| 70 |
tenant=",", |
| 71 |
cid="ab-cd-ef-gh", |
| 72 |
aid="[email protected]", |
| 73 |
secret="abcd/123/3343/@jack/test", |
| 74 |
targets="/".join(["[email protected]"]), |
| 75 |
), |
| 76 |
{ |
| 77 |
|
| 78 |
"instance": TypeError, |
| 79 |
}, |
| 80 |
), |
| 81 |
( |
| 82 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}".format( |
| 83 |
tenant="tenant", |
| 84 |
|
| 85 |
cid="ab.", |
| 86 |
aid="[email protected]", |
| 87 |
secret="abcd/123/3343/@jack/test", |
| 88 |
targets="/".join(["[email protected]"]), |
| 89 |
), |
| 90 |
{ |
| 91 |
|
| 92 |
"instance": TypeError, |
| 93 |
}, |
| 94 |
), |
| 95 |
( |
| 96 |
"o365://{tenant}/{cid}/{secret}/{targets}".format( |
| 97 |
|
| 98 |
tenant="tenant", |
| 99 |
cid="ab-cd-ef-gh", |
| 100 |
secret="abcd/123/3343/@jack/test", |
| 101 |
targets="/".join(["[email protected]"]), |
| 102 |
), |
| 103 |
{ |
| 104 |
|
| 105 |
"instance": NotifyOffice365, |
| 106 |
|
| 107 |
"requests_response_text": { |
| 108 |
"expires_in": 2000, |
| 109 |
"access_token": "abcd1234", |
| 110 |
"mail": "[email protected]", |
| 111 |
}, |
| 112 |
}, |
| 113 |
), |
| 114 |
( |
| 115 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}".format( |
| 116 |
tenant="tenant", |
| 117 |
cid="ab-cd-ef-gh", |
| 118 |
aid="[email protected]", |
| 119 |
secret="abcd/123/3343/@jack/test", |
| 120 |
targets="/".join(["[email protected]"]), |
| 121 |
), |
| 122 |
{ |
| 123 |
|
| 124 |
"instance": NotifyOffice365, |
| 125 |
|
| 126 |
"requests_response_text": { |
| 127 |
"expires_in": 2000, |
| 128 |
"access_token": "abcd1234", |
| 129 |
|
| 130 |
"mail": "[email protected]", |
| 131 |
}, |
| 132 |
|
| 133 |
"privacy_url": ( |
| 134 |
"azure://[email protected]/t...t/a...h/****/[email protected]/" |
| 135 |
), |
| 136 |
"force_debug": True, |
| 137 |
}, |
| 138 |
), |
| 139 |
( |
| 140 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}".format( |
| 141 |
tenant="tenant", |
| 142 |
cid="ab-cd-ef-gh", |
| 143 |
|
| 144 |
aid="hg-fe-dc-ba", |
| 145 |
secret="abcd/123/3343/@jack/test", |
| 146 |
targets="/".join(["[email protected]"]), |
| 147 |
), |
| 148 |
{ |
| 149 |
|
| 150 |
"instance": NotifyOffice365, |
| 151 |
|
| 152 |
"requests_response_text": { |
| 153 |
"expires_in": 2000, |
| 154 |
"access_token": "abcd1234", |
| 155 |
"mail": "[email protected]", |
| 156 |
"displayName": "John", |
| 157 |
}, |
| 158 |
|
| 159 |
"privacy_url": ( |
| 160 |
"azure://hg-fe-dc-ba/t...t/a...h/****/[email protected]/" |
| 161 |
), |
| 162 |
}, |
| 163 |
), |
| 164 |
|
| 165 |
( |
| 166 |
"o365://{aid}/{tenant}/{cid}/{secret}/".format( |
| 167 |
tenant="tenant", |
| 168 |
cid="ab-cd-ef-gh", |
| 169 |
|
| 170 |
aid="hg-fe-dc-ba", |
| 171 |
secret="abcd/123/3343/@jack/test", |
| 172 |
), |
| 173 |
{ |
| 174 |
|
| 175 |
"instance": NotifyOffice365, |
| 176 |
|
| 177 |
"requests_response_text": { |
| 178 |
"expires_in": 2000, |
| 179 |
"access_token": "abcd1234", |
| 180 |
"mail": "[email protected]", |
| 181 |
}, |
| 182 |
|
| 183 |
"notify_response": False, |
| 184 |
|
| 185 |
"privacy_url": "azure://hg-fe-dc-ba/t...t/a...h/****", |
| 186 |
}, |
| 187 |
), |
| 188 |
|
| 189 |
( |
| 190 |
"o365://{aid}/{tenant}/{cid}/{secret}/".format( |
| 191 |
tenant="tenant", |
| 192 |
cid="ab-cd-ef-gh", |
| 193 |
|
| 194 |
aid="hg-fe-dc-ba", |
| 195 |
secret="abcd/123/3343/@jack/test", |
| 196 |
), |
| 197 |
{ |
| 198 |
|
| 199 |
"instance": NotifyOffice365, |
| 200 |
|
| 201 |
"requests_response_text": { |
| 202 |
"expires_in": 2000, |
| 203 |
"access_token": "abcd1234", |
| 204 |
"userPrincipalName": "[email protected]", |
| 205 |
}, |
| 206 |
|
| 207 |
"notify_response": False, |
| 208 |
|
| 209 |
"privacy_url": "azure://hg-fe-dc-ba/t...t/a...h/****", |
| 210 |
}, |
| 211 |
), |
| 212 |
|
| 213 |
( |
| 214 |
"o365://_/?oauth_id={cid}&oauth_secret={secret}&tenant={tenant}" |
| 215 |
"&to={targets}&from={aid}".format( |
| 216 |
tenant="tenant", |
| 217 |
cid="ab-cd-ef-gh", |
| 218 |
aid="[email protected]", |
| 219 |
secret="abcd/123/3343/@jack/test", |
| 220 |
targets="[email protected]", |
| 221 |
), |
| 222 |
{ |
| 223 |
|
| 224 |
"instance": NotifyOffice365, |
| 225 |
|
| 226 |
"requests_response_text": { |
| 227 |
"expires_in": 2000, |
| 228 |
"access_token": "abcd1234", |
| 229 |
"mail": "[email protected]", |
| 230 |
}, |
| 231 |
|
| 232 |
"privacy_url": ( |
| 233 |
"azure://[email protected]/t...t/a...h/****/[email protected]/" |
| 234 |
), |
| 235 |
}, |
| 236 |
), |
| 237 |
|
| 238 |
( |
| 239 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}".format( |
| 240 |
tenant="tenant", |
| 241 |
cid="ab-cd-ef-gh", |
| 242 |
aid="[email protected]", |
| 243 |
secret="abcd/123/3343/@jack/test", |
| 244 |
targets="/".join(["[email protected]"]), |
| 245 |
), |
| 246 |
{ |
| 247 |
|
| 248 |
"instance": NotifyOffice365, |
| 249 |
|
| 250 |
"requests_response_text": "{", |
| 251 |
"notify_response": False, |
| 252 |
}, |
| 253 |
), |
| 254 |
|
| 255 |
( |
| 256 |
"o365://{aid}/{tenant}/{cid}/{secret}".format( |
| 257 |
tenant="tenant", |
| 258 |
cid="ab-cd-ef-gh", |
| 259 |
aid="[email protected]", |
| 260 |
secret="abcd/123/3343/@jack/test", |
| 261 |
), |
| 262 |
{ |
| 263 |
|
| 264 |
"instance": NotifyOffice365, |
| 265 |
|
| 266 |
"requests_response_text": { |
| 267 |
"expires_in": 2000, |
| 268 |
"access_token": "abcd1234", |
| 269 |
"userPrincipalName": "[email protected]", |
| 270 |
}, |
| 271 |
}, |
| 272 |
), |
| 273 |
( |
| 274 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}".format( |
| 275 |
tenant="tenant", |
| 276 |
cid="zz-zz-zz-zz", |
| 277 |
aid="[email protected]", |
| 278 |
secret="abcd/abc/dcba/@john/test", |
| 279 |
targets="/".join(["[email protected]"]), |
| 280 |
), |
| 281 |
{ |
| 282 |
"instance": NotifyOffice365, |
| 283 |
|
| 284 |
"response": False, |
| 285 |
"requests_response_code": 999, |
| 286 |
}, |
| 287 |
), |
| 288 |
( |
| 289 |
"o365://{tenant}:{aid}/{cid}/{secret}/{targets}".format( |
| 290 |
tenant="tenant", |
| 291 |
cid="01-12-23-34", |
| 292 |
aid="[email protected]", |
| 293 |
secret="abcd/321/4321/@test/test", |
| 294 |
targets="/".join(["[email protected]"]), |
| 295 |
), |
| 296 |
{ |
| 297 |
"instance": NotifyOffice365, |
| 298 |
|
| 299 |
|
| 300 |
"test_requests_exceptions": True, |
| 301 |
}, |
| 302 |
), |
| 303 |
ss |
| 304 |
( |
| 305 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}" |
| 306 |
"[email protected]".format( |
| 307 |
tenant="tenant", |
| 308 |
cid="ab-cd-ef-gh", |
| 309 |
aid="[email protected]", |
| 310 |
secret="abcd/123/3343/@jack/test", |
| 311 |
targets="[email protected]", |
| 312 |
), |
| 313 |
{ |
| 314 |
"instance": NotifyOffice365, |
| 315 |
"requests_response_text": { |
| 316 |
"expires_in": 2000, |
| 317 |
"access_token": "abcd1234", |
| 318 |
"mail": "[email protected]", |
| 319 |
}, |
| 320 |
"privacy_url": ( |
| 321 |
"azure://[email protected]/t...t/a...h/****/[email protected]/" |
| 322 |
), |
| 323 |
}, |
| 324 |
), |
| 325 |
|
| 326 |
( |
| 327 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}?savesent=no".format( |
| 328 |
tenant="tenant", |
| 329 |
cid="ab-cd-ef-gh", |
| 330 |
aid="[email protected]", |
| 331 |
secret="abcd/123/3343/@jack/test", |
| 332 |
targets="[email protected]", |
| 333 |
), |
| 334 |
{ |
| 335 |
"instance": NotifyOffice365, |
| 336 |
"requests_response_text": { |
| 337 |
"expires_in": 2000, |
| 338 |
"access_token": "abcd1234", |
| 339 |
"mail": "[email protected]", |
| 340 |
}, |
| 341 |
"privacy_url": ( |
| 342 |
"azure://[email protected]/t...t/a...h/****/[email protected]/" |
| 343 |
), |
| 344 |
}, |
| 345 |
), |
| 346 |
|
| 347 |
( |
| 348 |
"o365://{aid}/{tenant}/{cid}/{secret}/{targets}" |
| 349 |
"[email protected]".format( |
| 350 |
tenant="tenant", |
| 351 |
cid="ab-cd-ef-gh", |
| 352 |
aid="[email protected]", |
| 353 |
secret="abcd/123/3343/@jack/test", |
| 354 |
targets="[email protected]", |
| 355 |
), |
| 356 |
{ |
| 357 |
"instance": NotifyOffice365, |
| 358 |
"requests_response_text": { |
| 359 |
"expires_in": 2000, |
| 360 |
"access_token": "abcd1234", |
| 361 |
"mail": "[email protected]", |
| 362 |
}, |
| 363 |
"privacy_url": ( |
| 364 |
"azure://[email protected]/t...t/a...h/****/[email protected]/" |
| 365 |
), |
| 366 |
}, |
| 367 |
), |
| 368 |
) |
| 369 |
|
| 370 |
|
| 371 |
def test_plugin_office365_urls(): |
| 372 |
"""NotifyOffice365() Apprise URLs.""" |
| 373 |
|
| 374 |
|
| 375 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 376 |
|
| 377 |
|
| 378 |
@mock.patch("requests.get") |
| 379 |
@mock.patch("requests.post") |
| 380 |
def test_plugin_office365_general(mock_get, mock_post): |
| 381 |
"""NotifyOffice365() General Testing.""" |
| 382 |
|
| 383 |
|
| 384 |
email = "[email protected]" |
| 385 |
tenant = "ff-gg-hh-ii-jj" |
| 386 |
client_id = "aa-bb-cc-dd-ee" |
| 387 |
secret = "abcd/1234/abcd@ajd@/test" |
| 388 |
targets = "[email protected]" |
| 389 |
|
| 390 |
|
| 391 |
payload = { |
| 392 |
"token_type": "Bearer", |
| 393 |
"expires_in": 6000, |
| 394 |
"access_token": "abcd1234", |
| 395 |
|
| 396 |
"mail": "[email protected]", |
| 397 |
|
| 398 |
"id": "draft-id-no", |
| 399 |
} |
| 400 |
response = mock.Mock() |
| 401 |
response.content = dumps(payload) |
| 402 |
response.status_code = requests.codes.ok |
| 403 |
mock_post.return_value = response |
| 404 |
mock_get.return_value = response |
| 405 |
|
| 406 |
|
| 407 |
obj = Apprise.instantiate(f"o365://{email}/{tenant}/{secret}/{targets}") |
| 408 |
|
| 409 |
assert isinstance(obj, NotifyOffice365) |
| 410 |
|
| 411 |
|
| 412 |
assert isinstance(obj.url(), str) |
| 413 |
|
| 414 |
|
| 415 |
assert obj.notify(title="title", body="test") is True |
| 416 |
|
| 417 |
|
| 418 |
obj = Apprise.instantiate( |
| 419 |
"o365://{email}/{tenant}/{client_id}/{secret}/{targets}" |
| 420 |
"?bcc={bcc}&cc={cc}".format( |
| 421 |
tenant=tenant, |
| 422 |
email=email, |
| 423 |
client_id=client_id, |
| 424 |
secret=secret, |
| 425 |
targets=targets, |
| 426 |
|
| 427 |
cc="Chuck Norris [email protected], [email protected], invalid@!", |
| 428 |
bcc="Bruce Willis [email protected], [email protected] invalid@!", |
| 429 |
) |
| 430 |
) |
| 431 |
|
| 432 |
assert isinstance(obj, NotifyOffice365) |
| 433 |
|
| 434 |
|
| 435 |
assert isinstance(obj.url(), str) |
| 436 |
|
| 437 |
|
| 438 |
assert obj.notify(title="title", body="test") is True |
| 439 |
|
| 440 |
with pytest.raises(TypeError): |
| 441 |
|
| 442 |
NotifyOffice365( |
| 443 |
email=email, |
| 444 |
client_id=client_id, |
| 445 |
tenant=tenant, |
| 446 |
secret=None, |
| 447 |
targets=None, |
| 448 |
) |
| 449 |
|
| 450 |
|
| 451 |
obj = NotifyOffice365( |
| 452 |
email=email, |
| 453 |
client_id=client_id, |
| 454 |
tenant=tenant, |
| 455 |
secret=secret, |
| 456 |
targets=("Management [email protected]", "garbage"), |
| 457 |
) |
| 458 |
|
| 459 |
assert obj.notify(title="title", body="test") is True |
| 460 |
|
| 461 |
|
| 462 |
obj = NotifyOffice365( |
| 463 |
email=email, |
| 464 |
client_id=client_id, |
| 465 |
tenant=tenant, |
| 466 |
secret=secret, |
| 467 |
targets=("invalid", "garbage"), |
| 468 |
) |
| 469 |
|
| 470 |
|
| 471 |
assert obj.notify(title="title", body="test") is False |
| 472 |
|
| 473 |
|
| 474 |
@mock.patch("requests.get") |
| 475 |
@mock.patch("requests.post") |
| 476 |
def test_plugin_office365_authentication(mock_get, mock_post): |
| 477 |
"""NotifyOffice365() Authentication Testing.""" |
| 478 |
|
| 479 |
|
| 480 |
tenant = "ff-gg-hh-ii-jj" |
| 481 |
email = "[email protected]" |
| 482 |
client_id = "aa-bb-cc-dd-ee" |
| 483 |
secret = "abcd/1234/abcd@ajd@/test" |
| 484 |
targets = "[email protected]" |
| 485 |
|
| 486 |
|
| 487 |
authentication_okay = { |
| 488 |
"token_type": "Bearer", |
| 489 |
"expires_in": 6000, |
| 490 |
"access_token": "abcd1234", |
| 491 |
} |
| 492 |
authentication_failure = { |
| 493 |
"error": "invalid_scope", |
| 494 |
"error_description": "AADSTS70011: Blah... Blah Blah... Blah", |
| 495 |
"error_codes": [70011], |
| 496 |
"timestamp": "2020-01-09 02:02:12Z", |
| 497 |
"trace_id": "255d1aef-8c98-452f-ac51-23d051240864", |
| 498 |
"correlation_id": "fb3d2015-bc17-4bb9-bb85-30c5cf1aaaa7", |
| 499 |
} |
| 500 |
response = mock.Mock() |
| 501 |
response.content = dumps(authentication_okay) |
| 502 |
response.status_code = requests.codes.ok |
| 503 |
mock_post.return_value = response |
| 504 |
mock_get.return_value = response |
| 505 |
|
| 506 |
|
| 507 |
obj = Apprise.instantiate( |
| 508 |
f"azure://{email}/{tenant}/{client_id}/{secret}/{targets}" |
| 509 |
) |
| 510 |
|
| 511 |
assert isinstance(obj, NotifyOffice365) |
| 512 |
|
| 513 |
|
| 514 |
assert obj.authenticate() is True |
| 515 |
|
| 516 |
|
| 517 |
assert obj.authenticate() is True |
| 518 |
|
| 519 |
|
| 520 |
obj.token_expiry = datetime.now() |
| 521 |
|
| 522 |
|
| 523 |
assert obj.authenticate() is True |
| 524 |
|
| 525 |
|
| 526 |
response.status_code = 400 |
| 527 |
|
| 528 |
|
| 529 |
assert obj.notify(title="title", body="test") is False |
| 530 |
|
| 531 |
|
| 532 |
obj.token_expiry = datetime.now() |
| 533 |
|
| 534 |
|
| 535 |
response.content = dumps(authentication_failure) |
| 536 |
|
| 537 |
|
| 538 |
assert obj.authenticate() is False |
| 539 |
|
| 540 |
|
| 541 |
assert obj.notify(title="title", body="test") is False |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
invalid_auth_entries = authentication_okay.copy() |
| 546 |
invalid_auth_entries["expires_in"] = "garbage" |
| 547 |
response.content = dumps(invalid_auth_entries) |
| 548 |
response.status_code = requests.codes.ok |
| 549 |
assert obj.authenticate() is False |
| 550 |
|
| 551 |
invalid_auth_entries["expires_in"] = None |
| 552 |
response.content = dumps(invalid_auth_entries) |
| 553 |
assert obj.authenticate() is False |
| 554 |
|
| 555 |
invalid_auth_entries["expires_in"] = "" |
| 556 |
response.content = dumps(invalid_auth_entries) |
| 557 |
assert obj.authenticate() is False |
| 558 |
|
| 559 |
del invalid_auth_entries["expires_in"] |
| 560 |
response.content = dumps(invalid_auth_entries) |
| 561 |
assert obj.authenticate() is False |
| 562 |
|
| 563 |
|
| 564 |
@mock.patch("requests.put") |
| 565 |
@mock.patch("requests.get") |
| 566 |
@mock.patch("requests.post") |
| 567 |
def test_plugin_office365_queries(mock_post, mock_get, mock_put): |
| 568 |
"""NotifyOffice365() General Queries.""" |
| 569 |
|
| 570 |
|
| 571 |
source = "abc-1234-object-id" |
| 572 |
tenant = "ff-gg-hh-ii-jj" |
| 573 |
client_id = "aa-bb-cc-dd-ee" |
| 574 |
secret = "abcd/1234/abcd@ajd@/test" |
| 575 |
targets = "[email protected]" |
| 576 |
|
| 577 |
|
| 578 |
payload = { |
| 579 |
"token_type": "Bearer", |
| 580 |
"expires_in": 6000, |
| 581 |
"access_token": "abcd1234", |
| 582 |
|
| 583 |
"mail": "[email protected]", |
| 584 |
|
| 585 |
"displayName": "John", |
| 586 |
|
| 587 |
"id": "draft-id-no", |
| 588 |
|
| 589 |
"uploadUrl": "https://my.url.path/", |
| 590 |
} |
| 591 |
|
| 592 |
okay_response = mock.Mock() |
| 593 |
okay_response.content = dumps(payload) |
| 594 |
okay_response.status_code = requests.codes.ok |
| 595 |
mock_post.return_value = okay_response |
| 596 |
mock_put.return_value = okay_response |
| 597 |
|
| 598 |
bad_response = mock.Mock() |
| 599 |
bad_response.content = dumps(payload) |
| 600 |
bad_response.status_code = requests.codes.forbidden |
| 601 |
|
| 602 |
|
| 603 |
mock_get.return_value = bad_response |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
guid_source = "e2a9db46-5714-44c2-87bf-c33bf236f42a" |
| 609 |
|
| 610 |
not_found_payload = { |
| 611 |
"error": { |
| 612 |
"code": "Request_ResourceNotFound", |
| 613 |
"message": "Resource not found.", |
| 614 |
} |
| 615 |
} |
| 616 |
not_found_response = mock.Mock() |
| 617 |
not_found_response.content = dumps(not_found_payload) |
| 618 |
not_found_response.status_code = requests.codes.not_found |
| 619 |
|
| 620 |
|
| 621 |
mock_get.return_value = not_found_response |
| 622 |
|
| 623 |
obj = Apprise.instantiate( |
| 624 |
f"azure://{guid_source}/{tenant}/{client_id}{secret}/{targets}" |
| 625 |
) |
| 626 |
assert isinstance(obj, NotifyOffice365) |
| 627 |
|
| 628 |
|
| 629 |
assert ( |
| 630 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 631 |
is False |
| 632 |
) |
| 633 |
|
| 634 |
|
| 635 |
assert mock_post.call_count == 1 |
| 636 |
|
| 637 |
assert mock_get.call_count == 1 |
| 638 |
|
| 639 |
mock_post.reset_mock() |
| 640 |
mock_get.reset_mock() |
| 641 |
|
| 642 |
|
| 643 |
mock_get.return_value = bad_response |
| 644 |
|
| 645 |
|
| 646 |
obj = Apprise.instantiate( |
| 647 |
f"azure://{source}/{tenant}/{client_id}{secret}/{targets}" |
| 648 |
) |
| 649 |
|
| 650 |
assert isinstance(obj, NotifyOffice365) |
| 651 |
|
| 652 |
|
| 653 |
assert ( |
| 654 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 655 |
is True |
| 656 |
) |
| 657 |
|
| 658 |
assert mock_post.call_count == 2 |
| 659 |
assert ( |
| 660 |
mock_post.call_args_list[0][0][0] |
| 661 |
== f"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token" |
| 662 |
) |
| 663 |
assert ( |
| 664 |
mock_post.call_args_list[1][0][0] |
| 665 |
== "https://graph.microsoft.com/v1.0/users/abc-1234-object-id/sendMail" |
| 666 |
) |
| 667 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 668 |
assert payload == { |
| 669 |
"message": { |
| 670 |
"subject": "title", |
| 671 |
"body": { |
| 672 |
"contentType": "HTML", |
| 673 |
"content": "body", |
| 674 |
}, |
| 675 |
"toRecipients": [ |
| 676 |
{"emailAddress": {"address": "[email protected]"}} |
| 677 |
], |
| 678 |
}, |
| 679 |
"saveToSentItems": "true", |
| 680 |
} |
| 681 |
mock_post.reset_mock() |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
temp_payload = { |
| 688 |
"token_type": "Bearer", |
| 689 |
"expires_in": 6000, |
| 690 |
"access_token": "abcd1234", |
| 691 |
|
| 692 |
"id": "draft-id-no", |
| 693 |
|
| 694 |
"uploadUrl": "https://my.url.path/", |
| 695 |
} |
| 696 |
|
| 697 |
bad_response.content = dumps(temp_payload) |
| 698 |
bad_response.status_code = requests.codes.okay |
| 699 |
mock_get.return_value = bad_response |
| 700 |
|
| 701 |
obj = Apprise.instantiate( |
| 702 |
f"azure://{source}/{tenant}/{client_id}{secret}/{targets}" |
| 703 |
) |
| 704 |
|
| 705 |
assert isinstance(obj, NotifyOffice365) |
| 706 |
|
| 707 |
|
| 708 |
assert ( |
| 709 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 710 |
is True |
| 711 |
) |
| 712 |
|
| 713 |
|
| 714 |
@mock.patch("requests.put") |
| 715 |
@mock.patch("requests.get") |
| 716 |
@mock.patch("requests.post") |
| 717 |
def test_plugin_office365_attachments(mock_post, mock_get, mock_put): |
| 718 |
"""NotifyOffice365() Attachments.""" |
| 719 |
|
| 720 |
|
| 721 |
source = "[email protected]" |
| 722 |
tenant = "ff-gg-hh-ii-jj" |
| 723 |
client_id = "aa-bb-cc-dd-ee" |
| 724 |
secret = "abcd/1234/abcd@ajd@/test" |
| 725 |
targets = "[email protected]" |
| 726 |
|
| 727 |
|
| 728 |
payload = { |
| 729 |
"token_type": "Bearer", |
| 730 |
"expires_in": 6000, |
| 731 |
"access_token": "abcd1234", |
| 732 |
|
| 733 |
"mail": "[email protected]", |
| 734 |
|
| 735 |
"id": "draft-id-no", |
| 736 |
|
| 737 |
"uploadUrl": "https://my.url.path/", |
| 738 |
} |
| 739 |
okay_response = mock.Mock() |
| 740 |
okay_response.content = dumps(payload) |
| 741 |
okay_response.status_code = requests.codes.ok |
| 742 |
mock_post.return_value = okay_response |
| 743 |
mock_get.return_value = okay_response |
| 744 |
mock_put.return_value = okay_response |
| 745 |
|
| 746 |
|
| 747 |
obj = Apprise.instantiate( |
| 748 |
f"azure://{source}/{tenant}/{client_id}{secret}/{targets}" |
| 749 |
) |
| 750 |
|
| 751 |
assert isinstance(obj, NotifyOffice365) |
| 752 |
|
| 753 |
|
| 754 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 755 |
attach = AppriseAttachment(path) |
| 756 |
assert ( |
| 757 |
obj.notify( |
| 758 |
body="body", |
| 759 |
title="title", |
| 760 |
notify_type=NotifyType.INFO, |
| 761 |
attach=attach, |
| 762 |
) |
| 763 |
is True |
| 764 |
) |
| 765 |
|
| 766 |
assert mock_post.call_count == 2 |
| 767 |
assert ( |
| 768 |
mock_post.call_args_list[0][0][0] |
| 769 |
== f"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token" |
| 770 |
) |
| 771 |
assert ( |
| 772 |
mock_post.call_args_list[0][1]["headers"].get("Content-Type") |
| 773 |
== "application/x-www-form-urlencoded" |
| 774 |
) |
| 775 |
assert ( |
| 776 |
mock_post.call_args_list[1][0][0] |
| 777 |
== f"https://graph.microsoft.com/v1.0/users/{source}/sendMail" |
| 778 |
) |
| 779 |
assert ( |
| 780 |
mock_post.call_args_list[1][1]["headers"].get("Content-Type") |
| 781 |
== "application/json" |
| 782 |
) |
| 783 |
mock_post.reset_mock() |
| 784 |
|
| 785 |
|
| 786 |
obj = Apprise.instantiate( |
| 787 |
"azure://{source}/{tenant}/{client_id}{secret}/{targets}".format( |
| 788 |
client_id=client_id, |
| 789 |
tenant=tenant, |
| 790 |
source="object-id-requiring-lookup", |
| 791 |
secret=secret, |
| 792 |
targets=targets, |
| 793 |
) |
| 794 |
) |
| 795 |
|
| 796 |
bad_response = mock.Mock() |
| 797 |
bad_response.content = dumps(payload) |
| 798 |
bad_response.status_code = requests.codes.forbidden |
| 799 |
mock_post.return_value = bad_response |
| 800 |
|
| 801 |
assert isinstance(obj, NotifyOffice365) |
| 802 |
|
| 803 |
assert ( |
| 804 |
obj.notify( |
| 805 |
body="auth-fail", title="title", notify_type=NotifyType.INFO |
| 806 |
) |
| 807 |
is False |
| 808 |
) |
| 809 |
assert mock_post.call_count == 1 |
| 810 |
assert ( |
| 811 |
mock_post.call_args_list[0][0][0] |
| 812 |
== "https://login.microsoftonline.com/ff-gg-hh-ii-jj/oauth2/v2.0/token" |
| 813 |
) |
| 814 |
mock_post.reset_mock() |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
obj = Apprise.instantiate( |
| 822 |
f"azure://{source}/{tenant}/{client_id}{secret}/{targets}" |
| 823 |
) |
| 824 |
|
| 825 |
assert isinstance(obj, NotifyOffice365) |
| 826 |
|
| 827 |
mock_post.return_value = okay_response |
| 828 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 829 |
assert ( |
| 830 |
obj.notify( |
| 831 |
body="body", |
| 832 |
title="title", |
| 833 |
notify_type=NotifyType.INFO, |
| 834 |
attach=path, |
| 835 |
) |
| 836 |
is False |
| 837 |
) |
| 838 |
assert mock_post.call_count == 0 |
| 839 |
mock_post.reset_mock() |
| 840 |
|
| 841 |
with mock.patch("base64.b64encode", side_effect=OSError()): |
| 842 |
|
| 843 |
assert ( |
| 844 |
obj.notify( |
| 845 |
body="body", |
| 846 |
title="title", |
| 847 |
notify_type=NotifyType.INFO, |
| 848 |
attach=attach, |
| 849 |
) |
| 850 |
is False |
| 851 |
) |
| 852 |
assert mock_post.call_count == 0 |
| 853 |
mock_post.reset_mock() |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
obj = Apprise.instantiate( |
| 859 |
f"azure://{source}/{tenant}/{client_id}{secret}/{targets}" |
| 860 |
) |
| 861 |
|
| 862 |
|
| 863 |
obj.outlook_attachment_inline_max = 50 |
| 864 |
|
| 865 |
assert isinstance(obj, NotifyOffice365) |
| 866 |
|
| 867 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 868 |
attach = AppriseAttachment(path) |
| 869 |
mock_post.return_value = bad_response |
| 870 |
assert obj.upload_attachment(attach[0], "id") is False |
| 871 |
assert mock_post.call_count == 1 |
| 872 |
assert ( |
| 873 |
mock_post.call_args_list[0][0][0] |
| 874 |
== "https://login.microsoftonline.com/ff-gg-hh-ii-jj/oauth2/v2.0/token" |
| 875 |
) |
| 876 |
|
| 877 |
mock_post.reset_mock() |
| 878 |
|
| 879 |
mock_post.side_effect = (okay_response, bad_response) |
| 880 |
mock_post.return_value = None |
| 881 |
assert obj.upload_attachment(attach[0], "id") is False |
| 882 |
assert mock_post.call_count == 2 |
| 883 |
assert ( |
| 884 |
mock_post.call_args_list[0][0][0] |
| 885 |
== "https://login.microsoftonline.com/ff-gg-hh-ii-jj/oauth2/v2.0/token" |
| 886 |
) |
| 887 |
assert ( |
| 888 |
mock_post.call_args_list[1][0][0] |
| 889 |
== f"https://graph.microsoft.com/v1.0/users/{source}/" |
| 890 |
+ "message/id/attachments/createUploadSession" |
| 891 |
) |
| 892 |
|
| 893 |
mock_post.reset_mock() |
| 894 |
|
| 895 |
mock_post.side_effect = None |
| 896 |
|
| 897 |
|
| 898 |
payload_no_upload_url = { |
| 899 |
"token_type": "Bearer", |
| 900 |
"expires_in": 6000, |
| 901 |
"access_token": "abcd1234", |
| 902 |
|
| 903 |
"mail": "[email protected]", |
| 904 |
|
| 905 |
"id": "draft-id-no", |
| 906 |
} |
| 907 |
tmp_response = mock.Mock() |
| 908 |
tmp_response.content = dumps(payload_no_upload_url) |
| 909 |
tmp_response.status_code = requests.codes.ok |
| 910 |
mock_post.return_value = tmp_response |
| 911 |
|
| 912 |
assert obj.upload_attachment(attach[0], "id") is False |
| 913 |
assert mock_post.call_count == 1 |
| 914 |
assert ( |
| 915 |
mock_post.call_args_list[0][0][0] |
| 916 |
== f"https://graph.microsoft.com/v1.0/users/{source}/" |
| 917 |
+ "message/id/attachments/createUploadSession" |
| 918 |
) |
| 919 |
|
| 920 |
mock_post.reset_mock() |
| 921 |
|
| 922 |
mock_post.side_effect = None |
| 923 |
mock_post.return_value = okay_response |
| 924 |
|
| 925 |
obj = Apprise.instantiate( |
| 926 |
f"azure://{source}/{tenant}/{client_id}{secret}/{targets}" |
| 927 |
) |
| 928 |
|
| 929 |
|
| 930 |
obj.outlook_attachment_inline_max = 50 |
| 931 |
|
| 932 |
assert isinstance(obj, NotifyOffice365) |
| 933 |
|
| 934 |
|
| 935 |
assert ( |
| 936 |
obj.notify( |
| 937 |
body="body", |
| 938 |
title="title-test", |
| 939 |
notify_type=NotifyType.INFO, |
| 940 |
attach=attach, |
| 941 |
) |
| 942 |
is True |
| 943 |
) |
| 944 |
|
| 945 |
|
| 946 |
assert mock_post.call_count == 4 |
| 947 |
assert ( |
| 948 |
mock_post.call_args_list[0][0][0] |
| 949 |
== "https://login.microsoftonline.com/ff-gg-hh-ii-jj/oauth2/v2.0/token" |
| 950 |
) |
| 951 |
assert ( |
| 952 |
mock_post.call_args_list[1][0][0] |
| 953 |
== f"https://graph.microsoft.com/v1.0/users/{source}/messages" |
| 954 |
) |
| 955 |
assert ( |
| 956 |
mock_post.call_args_list[2][0][0] |
| 957 |
== f"https://graph.microsoft.com/v1.0/users/{source}/" |
| 958 |
+ "message/draft-id-no/attachments/createUploadSession" |
| 959 |
) |
| 960 |
assert ( |
| 961 |
mock_post.call_args_list[3][0][0] |
| 962 |
== "https://graph.microsoft.com/v1.0/users/" |
| 963 |
+ f"{source}/messages/draft-id-no/send" |
| 964 |
) |
| 965 |
mock_post.reset_mock() |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 971 |
bad_attach = AppriseAttachment(path) |
| 972 |
assert obj.upload_attachment(bad_attach[0], "id") is False |
| 973 |
|
| 974 |
mock_post.reset_mock() |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
mock_post.return_value = None |
| 980 |
mock_post.side_effect = (okay_response, okay_response, bad_response) |
| 981 |
assert ( |
| 982 |
obj.notify( |
| 983 |
body="body", |
| 984 |
title="title-test", |
| 985 |
notify_type=NotifyType.INFO, |
| 986 |
attach=attach, |
| 987 |
) |
| 988 |
is False |
| 989 |
) |
| 990 |
|
| 991 |
assert mock_post.call_count == 3 |
| 992 |
assert ( |
| 993 |
mock_post.call_args_list[0][0][0] |
| 994 |
== f"https://graph.microsoft.com/v1.0/users/{source}/messages" |
| 995 |
) |
| 996 |
assert ( |
| 997 |
mock_post.call_args_list[1][0][0] |
| 998 |
== f"https://graph.microsoft.com/v1.0/users/{source}/" |
| 999 |
+ "message/draft-id-no/attachments/createUploadSession" |
| 1000 |
) |
| 1001 |
assert ( |
| 1002 |
mock_post.call_args_list[2][0][0] |
| 1003 |
== "https://graph.microsoft.com/v1.0/users/" |
| 1004 |
+ f"{source}/messages/draft-id-no/send" |
| 1005 |
) |
| 1006 |
mock_post.reset_mock() |
| 1007 |
mock_post.side_effect = None |
| 1008 |
mock_post.return_value = okay_response |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
mock_put.return_value = bad_response |
| 1014 |
|
| 1015 |
|
| 1016 |
assert ( |
| 1017 |
obj.notify( |
| 1018 |
body="body", |
| 1019 |
title="title-no-chunk", |
| 1020 |
notify_type=NotifyType.INFO, |
| 1021 |
attach=attach, |
| 1022 |
) |
| 1023 |
is False |
| 1024 |
) |
| 1025 |
|
| 1026 |
assert mock_post.call_count == 2 |
| 1027 |
assert ( |
| 1028 |
mock_post.call_args_list[0][0][0] |
| 1029 |
== f"https://graph.microsoft.com/v1.0/users/{source}/messages" |
| 1030 |
) |
| 1031 |
assert ( |
| 1032 |
mock_post.call_args_list[1][0][0] |
| 1033 |
== f"https://graph.microsoft.com/v1.0/users/{source}/" |
| 1034 |
+ "message/draft-id-no/attachments/createUploadSession" |
| 1035 |
) |
| 1036 |
|
| 1037 |
mock_put.return_value = okay_response |
| 1038 |
mock_post.reset_mock() |
| 1039 |
|
| 1040 |
|
| 1041 |
payload_missing_id = { |
| 1042 |
"token_type": "Bearer", |
| 1043 |
"expires_in": 6000, |
| 1044 |
"access_token": "abcd1234", |
| 1045 |
|
| 1046 |
"mail": "[email protected]", |
| 1047 |
|
| 1048 |
"uploadUrl": "https://my.url.path/", |
| 1049 |
} |
| 1050 |
temp_response = mock.Mock() |
| 1051 |
temp_response.content = dumps(payload_missing_id) |
| 1052 |
temp_response.status_code = requests.codes.ok |
| 1053 |
mock_post.return_value = temp_response |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
assert ( |
| 1058 |
obj.notify( |
| 1059 |
body="body", |
| 1060 |
title="title-test", |
| 1061 |
notify_type=NotifyType.INFO, |
| 1062 |
attach=attach, |
| 1063 |
) |
| 1064 |
is False |
| 1065 |
) |
| 1066 |
|
| 1067 |
|
| 1068 |
assert mock_post.call_count == 1 |
| 1069 |
assert ( |
| 1070 |
mock_post.call_args_list[0][0][0] |
| 1071 |
== "https://graph.microsoft.com/v1.0/users/[email protected]/messages" |
| 1072 |
) |
| 1073 |
|
| 1074 |
mock_post.reset_mock() |
| 1075 |
|
| 1076 |
|
| 1077 |
obj.outlook_attachment_inline_max = 50 * 1024 * 1024 |
| 1078 |
assert ( |
| 1079 |
obj.notify( |
| 1080 |
body="body", |
| 1081 |
title="title", |
| 1082 |
notify_type=NotifyType.INFO, |
| 1083 |
attach=attach, |
| 1084 |
) |
| 1085 |
is True |
| 1086 |
) |
| 1087 |
|
| 1088 |
|
| 1089 |
assert mock_post.call_count == 1 |
| 1090 |
assert ( |
| 1091 |
mock_post.call_args_list[0][0][0] |
| 1092 |
== f"https://graph.microsoft.com/v1.0/users/{source}/sendMail" |
| 1093 |
) |
| 1094 |
mock_post.reset_mock() |
| 1095 |
|
| 1096 |
|
| 1097 |
@mock.patch("requests.get") |
| 1098 |
@mock.patch("requests.post") |
| 1099 |
def test_plugin_office365_reply_to(mock_post, mock_get): |
| 1100 |
"""NotifyOffice365() reply_to parameter support.""" |
| 1101 |
|
| 1102 |
tenant = "ff-gg-hh-ii-jj" |
| 1103 |
email = "[email protected]" |
| 1104 |
client_id = "aa-bb-cc-dd-ee" |
| 1105 |
secret = "abcd/1234/abcd@ajd@/test" |
| 1106 |
targets = "[email protected]" |
| 1107 |
|
| 1108 |
payload = { |
| 1109 |
"token_type": "Bearer", |
| 1110 |
"expires_in": 6000, |
| 1111 |
"access_token": "abcd1234", |
| 1112 |
"mail": "[email protected]", |
| 1113 |
} |
| 1114 |
okay_response = mock.Mock() |
| 1115 |
okay_response.content = dumps(payload) |
| 1116 |
okay_response.status_code = requests.codes.ok |
| 1117 |
mock_post.return_value = okay_response |
| 1118 |
mock_get.return_value = okay_response |
| 1119 |
|
| 1120 |
|
| 1121 |
obj = NotifyOffice365( |
| 1122 |
source=email, |
| 1123 |
tenant=tenant, |
| 1124 |
client_id=client_id, |
| 1125 |
secret=secret, |
| 1126 |
targets=[targets], |
| 1127 |
reply_to=["Reply Person <[email protected]>"], |
| 1128 |
) |
| 1129 |
|
| 1130 |
assert isinstance(obj, NotifyOffice365) |
| 1131 |
assert len(obj.reply_to) == 1 |
| 1132 |
assert "[email protected]" in obj.reply_to |
| 1133 |
|
| 1134 |
|
| 1135 |
url = obj.url() |
| 1136 |
assert "reply_to=" in url |
| 1137 |
|
| 1138 |
|
| 1139 |
assert obj.notify(title="title", body="test") is True |
| 1140 |
assert mock_post.call_count == 2 |
| 1141 |
sent_payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1142 |
assert "replyTo" in sent_payload["message"] |
| 1143 |
assert ( |
| 1144 |
sent_payload["message"]["replyTo"][0]["emailAddress"]["address"] |
| 1145 |
== "[email protected]" |
| 1146 |
) |
| 1147 |
assert ( |
| 1148 |
sent_payload["message"]["replyTo"][0]["emailAddress"]["name"] |
| 1149 |
== "Reply Person" |
| 1150 |
) |
| 1151 |
mock_post.reset_mock() |
| 1152 |
|
| 1153 |
|
| 1154 |
obj2 = Apprise.instantiate( |
| 1155 |
"o365://{email}/{tenant}/{client_id}/{secret}/{targets}" |
| 1156 |
"[email protected]".format( |
| 1157 |
tenant=tenant, |
| 1158 |
email=email, |
| 1159 |
client_id=client_id, |
| 1160 |
secret=secret, |
| 1161 |
targets=targets, |
| 1162 |
) |
| 1163 |
) |
| 1164 |
|
| 1165 |
assert isinstance(obj2, NotifyOffice365) |
| 1166 |
assert obj2.notify(title="t", body="b") is True |
| 1167 |
sent_payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1168 |
assert "replyTo" in sent_payload["message"] |
| 1169 |
assert "name" not in sent_payload["message"]["replyTo"][0]["emailAddress"] |
| 1170 |
mock_post.reset_mock() |
| 1171 |
|
| 1172 |
|
| 1173 |
obj3 = NotifyOffice365( |
| 1174 |
source=email, |
| 1175 |
tenant=tenant, |
| 1176 |
client_id=client_id, |
| 1177 |
secret=secret, |
| 1178 |
targets=[targets], |
| 1179 |
reply_to=["not-a-valid-email"], |
| 1180 |
) |
| 1181 |
assert isinstance(obj3, NotifyOffice365) |
| 1182 |
assert len(obj3.reply_to) == 0 |
| 1183 |
|
| 1184 |
assert obj3.notify(title="t", body="b") is True |
| 1185 |
sent_payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1186 |
assert "replyTo" not in sent_payload["message"] |
| 1187 |
mock_post.reset_mock() |
| 1188 |
|
| 1189 |
|
| 1190 |
parsed = NotifyOffice365.parse_url( |
| 1191 |
"o365://{email}/{tenant}/{client_id}/{secret}/{targets}" |
| 1192 |
"[email protected]".format( |
| 1193 |
tenant=tenant, |
| 1194 |
email=email, |
| 1195 |
client_id=client_id, |
| 1196 |
secret=secret, |
| 1197 |
targets=targets, |
| 1198 |
) |
| 1199 |
) |
| 1200 |
assert parsed is not None |
| 1201 |
assert "reply_to" in parsed |
| 1202 |
assert "[email protected]" in parsed["reply_to"] |
| 1203 |
|
| 1204 |
|
| 1205 |
@mock.patch("requests.get") |
| 1206 |
@mock.patch("requests.post") |
| 1207 |
def test_plugin_office365_personal_mode(mock_post, mock_get): |
| 1208 |
"""NotifyOffice365() personal mode (refresh_token / consumer accounts).""" |
| 1209 |
from apprise.plugins.office365 import ( |
| 1210 |
Office365Mode, |
| 1211 |
) |
| 1212 |
|
| 1213 |
client_id = "aa-bb-cc-dd-ee" |
| 1214 |
seed_token = "seed-refresh-token-abc123" |
| 1215 |
new_token = "rotated-refresh-token-xyz789" |
| 1216 |
access_token = "access-token-live" |
| 1217 |
|
| 1218 |
auth_payload = { |
| 1219 |
"token_type": "Bearer", |
| 1220 |
"expires_in": 3600, |
| 1221 |
"access_token": access_token, |
| 1222 |
"refresh_token": new_token, |
| 1223 |
} |
| 1224 |
okay_response = mock.Mock() |
| 1225 |
okay_response.content = dumps(auth_payload) |
| 1226 |
okay_response.status_code = requests.codes.ok |
| 1227 |
bad_response = mock.Mock() |
| 1228 |
bad_response.content = dumps({"error": "bad"}) |
| 1229 |
bad_response.status_code = requests.codes.internal_server_error |
| 1230 |
|
| 1231 |
mock_post.return_value = okay_response |
| 1232 |
mock_get.return_value = okay_response |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
obj = NotifyOffice365( |
| 1238 |
source="[email protected]", |
| 1239 |
client_id=client_id, |
| 1240 |
secret=seed_token, |
| 1241 |
) |
| 1242 |
assert isinstance(obj, NotifyOffice365) |
| 1243 |
assert obj.mode == Office365Mode.PERSONAL |
| 1244 |
assert obj.tenant is None |
| 1245 |
assert obj.from_email == "[email protected]" |
| 1246 |
assert obj.source_is_object_id is False |
| 1247 |
|
| 1248 |
|
| 1249 |
for domain in ("hotmail.com", "outlook.com", "live.com", "msn.com"): |
| 1250 |
o = NotifyOffice365( |
| 1251 |
source=f"user@{domain}", |
| 1252 |
client_id=client_id, |
| 1253 |
secret=seed_token, |
| 1254 |
) |
| 1255 |
assert o.mode == Office365Mode.PERSONAL, ( |
| 1256 |
f"expected personal mode for {domain}" |
| 1257 |
) |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
obj_explicit = NotifyOffice365( |
| 1263 |
source="[email protected]", |
| 1264 |
client_id=client_id, |
| 1265 |
secret=seed_token, |
| 1266 |
mode=Office365Mode.PERSONAL, |
| 1267 |
) |
| 1268 |
assert obj_explicit.mode == Office365Mode.PERSONAL |
| 1269 |
|
| 1270 |
|
| 1271 |
il |
| 1272 |
|
| 1273 |
|
| 1274 |
with pytest.raises(TypeError): |
| 1275 |
|
| 1276 |
NotifyOffice365( |
| 1277 |
source="[email protected]", |
| 1278 |
client_id=client_id, |
| 1279 |
secret=seed_token, |
| 1280 |
mode=Office365Mode.ORG, |
| 1281 |
) |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
with pytest.raises(TypeError): |
| 1287 |
NotifyOffice365( |
| 1288 |
source="not-an-email", |
| 1289 |
client_id=client_id, |
| 1290 |
secret=seed_token, |
| 1291 |
mode=Office365Mode.PERSONAL, |
| 1292 |
) |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
with pytest.raises(TypeError): |
| 1298 |
NotifyOffice365( |
| 1299 |
source="[email protected]", |
| 1300 |
client_id=client_id, |
| 1301 |
secret=seed_token, |
| 1302 |
mode="invalid-mode", |
| 1303 |
) |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
obj = NotifyOffice365( |
| 1309 |
source="[email protected]", |
| 1310 |
client_id=client_id, |
| 1311 |
secret=seed_token, |
| 1312 |
targets=["[email protected]"], |
| 1313 |
) |
| 1314 |
assert obj.mode == Office365Mode.PERSONAL |
| 1315 |
assert obj.notify(title="title", body="body") is True |
| 1316 |
|
| 1317 |
|
| 1318 |
assert mock_post.call_count == 2 |
| 1319 |
assert ( |
| 1320 |
mock_post.call_args_list[0][0][0] |
| 1321 |
== "https://login.microsoftonline.com/consumers/oauth2/v2.0/token" |
| 1322 |
) |
| 1323 |
assert ( |
| 1324 |
mock_post.call_args_list[1][0][0] |
| 1325 |
== "https://graph.microsoft.com/v1.0/me/sendMail" |
| 1326 |
) |
| 1327 |
mock_post.reset_mock() |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
stored = obj.store.get("refresh_token") |
| 1333 |
assert stored == new_token |
| 1334 |
|
| 1335 |
generated_url = obj.url() |
| 1336 |
assert new_token in generated_url |
| 1337 |
assert seed_token not in generated_url |
| 1338 |
assert "mode=personal" in generated_url |
| 1339 |
|
| 1340 |
|
| 1341 |
private_url = obj.url(privacy=True) |
| 1342 |
assert new_token not in private_url |
| 1343 |
assert seed_token not in private_url |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
mock_post.return_value = bad_response |
| 1349 |
obj2 = NotifyOffice365( |
| 1350 |
source="[email protected]", |
| 1351 |
client_id=client_id, |
| 1352 |
secret=seed_token, |
| 1353 |
) |
| 1354 |
assert obj2.notify(title="t", body="b") is False |
| 1355 |
mock_post.reset_mock() |
| 1356 |
mock_post.return_value = okay_response |
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
broken_payload = {"access_token": access_token} |
| 1362 |
broken_response = mock.Mock() |
| 1363 |
broken_response.content = dumps(broken_payload) |
| 1364 |
broken_response.status_code = requests.codes.ok |
| 1365 |
mock_post.return_value = broken_response |
| 1366 |
obj3 = NotifyOffice365( |
| 1367 |
source="[email protected]", |
| 1368 |
client_id=client_id, |
| 1369 |
secret=seed_token, |
| 1370 |
) |
| 1371 |
assert obj3.notify(title="t", body="b") is False |
| 1372 |
mock_post.reset_mock() |
| 1373 |
mock_post.return_value = okay_response |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
parsed = NotifyOffice365.parse_url( |
| 1379 |
f"o365://[email protected]/{client_id}/{seed_token}/" |
| 1380 |
) |
| 1381 |
assert parsed is not None |
| 1382 |
assert parsed["mode"] == Office365Mode.PERSONAL |
| 1383 |
assert parsed["tenant"] is None |
| 1384 |
assert parsed["client_id"] == client_id |
| 1385 |
assert parsed["secret"] == seed_token |
| 1386 |
|
| 1387 |
|
| 1388 |
obj4 = Apprise.instantiate( |
| 1389 |
f"o365://[email protected]/{client_id}/{seed_token}/" |
| 1390 |
) |
| 1391 |
assert isinstance(obj4, NotifyOffice365) |
| 1392 |
assert obj4.mode == Office365Mode.PERSONAL |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
parsed2 = NotifyOffice365.parse_url( |
| 1398 |
f"o365://[email protected]/{client_id}/{seed_token}/?mode=personal" |
| 1399 |
) |
| 1400 |
assert parsed2 is not None |
| 1401 |
assert parsed2["mode"] == Office365Mode.PERSONAL |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
parsed3 = NotifyOffice365.parse_url( |
| 1407 |
f"o365://[email protected]/tenant-id/{client_id}/{seed_token}/?mode=org" |
| 1408 |
) |
| 1409 |
assert parsed3 is not None |
| 1410 |
assert parsed3["mode"] == Office365Mode.ORG |
| 1411 |
assert parsed3["tenant"] == "tenant-id" |
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
obj5 = NotifyOffice365( |
| 1417 |
source="[email protected]", |
| 1418 |
client_id=client_id, |
| 1419 |
secret=seed_token, |
| 1420 |
targets=["[email protected]"], |
| 1421 |
) |
| 1422 |
url = obj5.url() |
| 1423 |
assert "mode=personal" in url |
| 1424 |
assert "outlook.com" in url |
| 1425 |
assert client_id in url |
| 1426 |
|
| 1427 |
assert "tenant" not in url |
| 1428 |
|
| 1429 |
|
| 1430 |
parsed4 = NotifyOffice365.parse_url(url) |
| 1431 |
assert parsed4 is not None |
| 1432 |
assert parsed4["mode"] == Office365Mode.PERSONAL |
| 1433 |
obj6 = Apprise.instantiate(url) |
| 1434 |
assert isinstance(obj6, NotifyOffice365) |
| 1435 |
assert obj6.mode == Office365Mode.PERSONAL |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
parsed5 = NotifyOffice365.parse_url( |
| 1442 |
f"o365://[email protected]/{client_id}/{seed_token}/?mode=bogus-value" |
| 1443 |
) |
| 1444 |
assert parsed5 is not None |
| 1445 |
al |
| 1446 |
assert parsed5["mode"] == Office365Mode.PERSONAL |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
mock_post.reset_mock() |
| 1452 |
mock_post.return_value = okay_response |
| 1453 |
obj7 = NotifyOffice365( |
| 1454 |
source="[email protected]", |
| 1455 |
client_id=client_id, |
| 1456 |
secret=seed_token, |
| 1457 |
targets=["[email protected]"], |
| 1458 |
cc=["Carbon Copy <[email protected]>"], |
| 1459 |
bcc=["Blind Copy <[email protected]>"], |
| 1460 |
reply_to=["Reply Person <[email protected]>"], |
| 1461 |
) |
| 1462 |
assert isinstance(obj7, NotifyOffice365) |
| 1463 |
|
| 1464 |
personal_url = obj7.url() |
| 1465 |
assert "mode=personal" in personal_url |
| 1466 |
assert "cc=Carbon%20Copy%3Acc%40example.com" in personal_url |
| 1467 |
assert "bcc=Blind%20Copy%3Abcc%40example.com" in personal_url |
| 1468 |
assert "reply_to=Reply%20Person%3Areply%40example.org" in personal_url |
| 1469 |
|
| 1470 |
assert obj7.notify(title="title", body="body") is True |
| 1471 |
assert mock_post.call_count == 2 |
| 1472 |
assert ( |
| 1473 |
mock_post.call_args_list[1][0][0] |
| 1474 |
== "https://graph.microsoft.com/v1.0/me/sendMail" |
| 1475 |
) |
| 1476 |
sent_payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1477 |
assert sent_payload["message"]["ccRecipients"][0]["emailAddress"] == { |
| 1478 |
"address": "[email protected]", |
| 1479 |
"name": "Carbon Copy", |
| 1480 |
} |
| 1481 |
assert sent_payload["message"]["bccRecipients"][0]["emailAddress"] == { |
| 1482 |
"address": "[email protected]", |
| 1483 |
"name": "Blind Copy", |
| 1484 |
} |
| 1485 |
assert sent_payload["message"]["replyTo"][0]["emailAddress"] == { |
| 1486 |
"address": "[email protected]", |
| 1487 |
"name": "Reply Person", |
| 1488 |
} |
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
no_rotate_payload = { |
| 1495 |
"token_type": "Bearer", |
| 1496 |
"expires_in": 3600, |
| 1497 |
"access_token": access_token, |
| 1498 |
|
| 1499 |
} |
| 1500 |
no_rotate_response = mock.Mock() |
| 1501 |
no_rotate_response.content = dumps(no_rotate_payload) |
| 1502 |
no_rotate_response.status_code = requests.codes.ok |
| 1503 |
send_response = mock.Mock() |
| 1504 |
send_response.content = dumps({}) |
| 1505 |
send_response.status_code = requests.codes.accepted |
| 1506 |
|
| 1507 |
mock_post.side_effect = [no_rotate_response, send_response] |
| 1508 |
obj_nr = NotifyOffice365( |
| 1509 |
source="[email protected]", |
| 1510 |
client_id=client_id, |
| 1511 |
secret=seed_token, |
| 1512 |
targets=["[email protected]"], |
| 1513 |
) |
| 1514 |
assert obj_nr.notify(title="t", body="b") is True |
| 1515 |
() |
| 1516 |
assert obj_nr.store.get("refresh_token") is None |
| 1517 |
mock_post.reset_mock() |
| 1518 |
mock_post.side_effect = None |
| 1519 |
mock_post.return_value = okay_response |
| 1520 |
|
| 1521 |
|
| 1522 |
@mock.patch("requests.put") |
| 1523 |
@mock.patch("requests.get") |
| 1524 |
@mock.patch("requests.post") |
| 1525 |
def test_plugin_office365_personal_large_attachment( |
| 1526 |
mock_post, mock_get, mock_put |
| 1527 |
): |
| 1528 |
"""Personal mode large-attachment path (me/messages draft + send).""" |
| 1529 |
from apprise.plugins.office365 import Office365Mode |
| 1530 |
|
| 1531 |
client_id = "aa-bb-cc-dd-ee" |
| 1532 |
seed_token = "seed-refresh-token-abc123" |
| 1533 |
new_token = "rotated-refresh-token-xyz789" |
| 1534 |
access_token = "access-token-live" |
| 1535 |
|
| 1536 |
|
| 1537 |
payload = { |
| 1538 |
"token_type": "Bearer", |
| 1539 |
"expires_in": 3600, |
| 1540 |
"access_token": access_token, |
| 1541 |
"refresh_token": new_token, |
| 1542 |
"id": "draft-id-no", |
| 1543 |
"uploadUrl": "https://upload.url.example/session", |
| 1544 |
} |
| 1545 |
okay_response = mock.Mock() |
| 1546 |
okay_response.content = dumps(payload) |
| 1547 |
okay_response.status_code = requests.codes.ok |
| 1548 |
mock_post.return_value = okay_response |
| 1549 |
mock_get.return_value = okay_response |
| 1550 |
mock_put.return_value = okay_response |
| 1551 |
|
| 1552 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 1553 |
attach = AppriseAttachment(path) |
| 1554 |
|
| 1555 |
obj = NotifyOffice365( |
| 1556 |
source="[email protected]", |
| 1557 |
client_id=client_id, |
| 1558 |
secret=seed_token, |
| 1559 |
targets=["[email protected]"], |
| 1560 |
) |
| 1561 |
assert obj.mode == Office365Mode.PERSONAL |
| 1562 |
|
| 1563 |
|
| 1564 |
obj.outlook_attachment_inline_max = 50 |
| 1565 |
|
| 1566 |
assert ( |
| 1567 |
obj.notify( |
| 1568 |
body="body", |
| 1569 |
title="title", |
| 1570 |
notify_type=NotifyType.INFO, |
| 1571 |
attach=attach, |
| 1572 |
) |
| 1573 |
is True |
| 1574 |
) |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
assert mock_post.call_count == 4 |
| 1579 |
assert ( |
| 1580 |
mock_post.call_args_list[0][0][0] |
| 1581 |
== "https://login.microsoftonline.com/consumers/oauth2/v2.0/token" |
| 1582 |
) |
| 1583 |
assert ( |
| 1584 |
mock_post.call_args_list[1][0][0] |
| 1585 |
== "https://graph.microsoft.com/v1.0/me/messages" |
| 1586 |
) |
| 1587 |
assert ( |
| 1588 |
mock_post.call_args_list[2][0][0] |
| 1589 |
== "https://graph.microsoft.com/v1.0/me/" |
| 1590 |
"message/draft-id-no/attachments/createUploadSession" |
| 1591 |
) |
| 1592 |
assert ( |
| 1593 |
mock_post.call_args_list[3][0][0] == "https://graph.microsoft.com/v1.0" |
| 1594 |
"/me/messages/draft-id-no/send" |
| 1595 |
) |
| 1596 |
|
| 1597 |
|
| 1598 |
def test_plugin_office365_cc_bcc_invalid_branch(): |
| 1599 |
"""NotifyOffice365() CC/BCC/Reply-To invalid entries dropped.""" |
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
obj = NotifyOffice365( |
| 1609 |
client_id="aa-bb-cc", |
| 1610 |
secret="seed-refresh-token", |
| 1611 |
source="[email protected]", |
| 1612 |
targets=["[email protected]"], |
| 1613 |
cc=["[email protected]", "notanemail"], |
| 1614 |
bcc=["[email protected]", "alsonotanemail"], |
| 1615 |
reply_to=["[email protected]", "badreply"], |
| 1616 |
) |
| 1617 |
|
| 1618 |
|
| 1619 |
assert "[email protected]" in obj.cc |
| 1620 |
assert len(obj.cc) == 1 |
| 1621 |
|
| 1622 |
|
| 1623 |
assert "[email protected]" in obj.bcc |
| 1624 |
assert len(obj.bcc) == 1 |
| 1625 |
|
| 1626 |
|
| 1627 |
assert "[email protected]" in obj.reply_to |
| 1628 |
assert len(obj.reply_to) == 1 |