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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
|
# -*- coding: utf-8 -*-
# BSD 2-Clause License
#
# Apprise - Push Notification Library.
# Copyright (c) 2025, Chris Caron <lead2gold@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from unittest import mock
import json
import requests
import pytest
from apprise import Apprise
from apprise import AppriseConfig
from apprise import NotifyType
from apprise.plugins.msteams import NotifyMSTeams
from helpers import AppriseURLTester
# Disable logging for a cleaner testing output
import logging
logging.disable(logging.CRITICAL)
# a test UUID we can use
UUID4 = '8b799edf-6f98-4d3a-9be7-2862fb4e5752'
# Our Testing URLs
apprise_url_tests = (
##################################
# NotifyMSTeams
##################################
('msteams://', {
# First API Token not specified
'instance': TypeError,
}),
('msteams://:@/', {
# We don't have strict host checking on for msteams, so this URL
# actually becomes parseable and :@ becomes a hostname.
# The below errors because a second token wasn't found
'instance': TypeError,
}),
('msteams://{}'.format(UUID4), {
# Just half of one token 1 provided
'instance': TypeError,
}),
('msteams://{}@{}/'.format(UUID4, UUID4), {
# Just 1 tokens provided
'instance': TypeError,
}),
('msteams://{}@{}/{}'.format(UUID4, UUID4, 'a' * 32), {
# Just 2 tokens provided
'instance': TypeError,
}),
('msteams://{}@{}/{}/{}?t1'.format(UUID4, UUID4, 'b' * 32, UUID4), {
# All tokens provided - we're good
'instance': NotifyMSTeams,
}),
# Support native URLs
('https://outlook.office.com/webhook/{}@{}/IncomingWebhook/{}/{}'
.format(UUID4, UUID4, 'k' * 32, UUID4), {
# All tokens provided - we're good
'instance': NotifyMSTeams,
# Our expected url(privacy=True) startswith() response (v1 format)
'privacy_url': 'msteams://8...2/k...k/8...2/'}),
# Support New Native URLs
('https://myteam.webhook.office.com/webhookb2/{}@{}/IncomingWebhook/{}/{}'
.format(UUID4, UUID4, 'm' * 32, UUID4), {
# All tokens provided - we're good
'instance': NotifyMSTeams,
# Our expected url(privacy=True) startswith() response (v2 format):
'privacy_url': 'msteams://myteam/8...2/m...m/8...2/'}),
# Support Newer Native URLs with 4 tokens, introduced in 2025
('https://myteam.webhook.office.com/webhookb2/{}@{}/IncomingWebhook/{}/{}'
'/{}'
.format(UUID4, UUID4, 'm' * 32, UUID4, 'V2-_' + 'n' * 43), {
# All tokens provided - we're good
'instance': NotifyMSTeams,
# Our expected url(privacy=True) startswith() response (v2 format):
'privacy_url': 'msteams://myteam/8...2/m...m/8...2/V...n'}),
# Legacy URL Formatting
('msteams://{}@{}/{}/{}?t2'.format(UUID4, UUID4, 'c' * 32, UUID4), {
# All tokens provided - we're good
'instance': NotifyMSTeams,
# don't include an image by default
'include_image': False,
}),
# Legacy URL Formatting
('msteams://{}@{}/{}/{}?image=No'.format(UUID4, UUID4, 'd' * 32, UUID4), {
# All tokens provided - we're good no image
'instance': NotifyMSTeams,
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'msteams://8...2/d...d/8...2/',
}),
# New 2021 URL formatting
('msteams://apprise/{}@{}/{}/{}'.format(
UUID4, UUID4, 'e' * 32, UUID4), {
# All tokens provided - we're good no image
'instance': NotifyMSTeams,
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'msteams://apprise/8...2/e...e/8...2/',
}),
# New 2021 URL formatting; support team= argument
('msteams://{}@{}/{}/{}?team=teamname'.format(
UUID4, UUID4, 'f' * 32, UUID4), {
# All tokens provided - we're good no image
'instance': NotifyMSTeams,
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'msteams://teamname/8...2/f...f/8...2/',
}),
# New 2021 URL formatting (forcing v1)
('msteams://apprise/{}@{}/{}/{}?version=1'.format(
UUID4, UUID4, 'e' * 32, UUID4), {
# All tokens provided - we're good
'instance': NotifyMSTeams,
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'msteams://8...2/e...e/8...2/',
}),
# Invalid versioning
('msteams://apprise/{}@{}/{}/{}?version=999'.format(
UUID4, UUID4, 'e' * 32, UUID4), {
# invalid version
'instance': TypeError,
}),
('msteams://apprise/{}@{}/{}/{}?version=invalid'.format(
UUID4, UUID4, 'e' * 32, UUID4), {
# invalid version
'instance': TypeError,
}),
('msteams://{}@{}/{}/{}?tx'.format(UUID4, UUID4, 'x' * 32, UUID4), {
'instance': NotifyMSTeams,
# force a failure
'response': False,
'requests_response_code': requests.codes.internal_server_error,
}),
('msteams://{}@{}/{}/{}?ty'.format(UUID4, UUID4, 'y' * 32, UUID4), {
'instance': NotifyMSTeams,
# throw a bizzare code forcing us to fail to look it up
'response': False,
'requests_response_code': 999,
}),
('msteams://{}@{}/{}/{}?tz'.format(UUID4, UUID4, 'z' * 32, UUID4), {
'instance': NotifyMSTeams,
# Throws a series of connection and transfer exceptions when this flag
# is set and tests that we gracfully handle them
'test_requests_exceptions': True,
}),
)
def test_plugin_msteams_urls():
"""
NotifyMSTeams() Apprise URLs
"""
# Run our general tests
AppriseURLTester(tests=apprise_url_tests).run_all()
@pytest.fixture
def msteams_url():
return 'msteams://{}@{}/{}/{}'.format(UUID4, UUID4, 'a' * 32, UUID4)
@pytest.fixture
def request_mock(mocker):
"""
Prepare requests mock.
"""
mock_post = mocker.patch("requests.post")
mock_post.return_value = requests.Request()
mock_post.return_value.status_code = requests.codes.ok
return mock_post
@pytest.fixture
def simple_template(tmpdir):
template = tmpdir.join("simple.json")
template.write("""
{
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"summary": "{{name}}",
"themeColor": "{{app_color}}",
"sections": [
{
"activityImage": null,
"activityTitle": "{{title}}",
"text": "{{body}}"
}
]
}
""")
return template
def test_plugin_msteams_templating_basic_success(
request_mock, msteams_url, tmpdir):
"""
NotifyMSTeams() Templating - success.
Test cases where URL and JSON is valid.
"""
template = tmpdir.join("simple.json")
template.write("""
{
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"summary": "{{app_id}}",
"themeColor": "{{app_color}}",
"sections": [
{
"activityImage": null,
"activityTitle": "{{app_title}}",
"text": "{{app_body}}"
}
]
}
""")
# Instantiate our URL
obj = Apprise.instantiate('{url}/?template={template}&{kwargs}'.format(
url=msteams_url,
template=str(template),
kwargs=':key1=token&:key2=token',
))
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is True
assert request_mock.called is True
assert request_mock.call_args_list[0][0][0].startswith(
'https://outlook.office.com/webhook/')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'summary' in posted_json
assert posted_json['summary'] == 'Apprise'
assert posted_json['themeColor'] == '#3AA3E3'
assert posted_json['sections'][0]['activityTitle'] == 'title'
assert posted_json['sections'][0]['text'] == 'body'
def test_plugin_msteams_templating_invalid_json(
request_mock, msteams_url, tmpdir):
"""
NotifyMSTeams() Templating - invalid JSON.
"""
template = tmpdir.join("invalid.json")
template.write("}")
# Instantiate our URL
obj = Apprise.instantiate('{url}/?template={template}&{kwargs}'.format(
url=msteams_url,
template=str(template),
kwargs=':key1=token&:key2=token',
))
assert isinstance(obj, NotifyMSTeams)
# We will fail to preform our notifcation because the JSON is bad
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is False
def test_plugin_msteams_templating_json_missing_type(
request_mock, msteams_url, tmpdir):
"""
NotifyMSTeams() Templating - invalid JSON.
Test case where we're missing the @type part of the URL.
"""
template = tmpdir.join("missing_type.json")
template.write("""
{
"@context": "https://schema.org/extensions",
"summary": "{{app_id}}",
"themeColor": "{{app_color}}",
"sections": [
{
"activityImage": null,
"activityTitle": "{{app_title}}",
"text": "{{app_body}}"
}
]
}
""")
# Instantiate our URL
obj = Apprise.instantiate('{url}/?template={template}&{kwargs}'.format(
url=msteams_url,
template=str(template),
kwargs=':key1=token&:key2=token',
))
assert isinstance(obj, NotifyMSTeams)
# We can not load the file because we're missing the @type entry
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is False
def test_plugin_msteams_templating_json_missing_context(
request_mock, msteams_url, tmpdir):
"""
NotifyMSTeams() Templating - invalid JSON.
Test cases where we're missing the @context part of the URL.
"""
template = tmpdir.join("missing_context.json")
template.write("""
{
"@type": "MessageCard",
"summary": "{{app_id}}",
"themeColor": "{{app_color}}",
"sections": [
{
"activityImage": null,
"activityTitle": "{{app_title}}",
"text": "{{app_body}}"
}
]
}
""")
# Instantiate our URL
obj = Apprise.instantiate('{url}/?template={template}&{kwargs}'.format(
url=msteams_url,
template=str(template),
kwargs=':key1=token&:key2=token',
))
assert isinstance(obj, NotifyMSTeams)
# We can not load the file because we're missing the @context entry
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is False
def test_plugin_msteams_templating_load_json_failure(
request_mock, msteams_url, tmpdir):
"""
NotifyMSTeams() Templating - template loading failure.
Test a case where we can not access the file.
"""
template = tmpdir.join("empty.json")
template.write("")
obj = Apprise.instantiate('{url}/?template={template}'.format(
url=msteams_url,
template=str(template),
))
with mock.patch('json.loads', side_effect=OSError):
# we fail, but this time it's because we couldn't
# access the cached file contents for reading
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is False
def test_plugin_msteams_templating_target_success(
request_mock, msteams_url, tmpdir):
"""
NotifyMSTeams() Templating - success with target.
A more complicated example; uses a target.
"""
template = tmpdir.join("more_complicated_example.json")
template.write("""
{
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"summary": "{{app_desc}}",
"themeColor": "{{app_color}}",
"sections": [
{
"activityImage": null,
"activityTitle": "{{app_title}}",
"text": "{{app_body}}"
}
],
"potentialAction": [{
"@type": "ActionCard",
"name": "Add a comment",
"inputs": [{
"@type": "TextInput",
"id": "comment",
"isMultiline": false,
"title": "Add a comment here for this task."
}],
"actions": [{
"@type": "HttpPOST",
"name": "Add Comment",
"target": "{{ target }}"
}]
}]
}
""")
# Instantiate our URL
obj = Apprise.instantiate('{url}/?template={template}&{kwargs}'.format(
url=msteams_url,
template=str(template),
kwargs=':key1=token&:key2=token&:target=http://localhost',
))
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is True
assert request_mock.called is True
assert request_mock.call_args_list[0][0][0].startswith(
'https://outlook.office.com/webhook/')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'summary' in posted_json
assert posted_json['summary'] == 'Apprise Notifications'
assert posted_json['themeColor'] == '#3AA3E3'
assert posted_json['sections'][0]['activityTitle'] == 'title'
assert posted_json['sections'][0]['text'] == 'body'
# We even parsed our entry out of the URL
assert posted_json['potentialAction'][0]['actions'][0]['target'] \
== 'http://localhost'
def test_msteams_yaml_config_invalid_template_filename(
request_mock, msteams_url, simple_template, tmpdir):
"""
NotifyMSTeams() YAML Configuration Entries - invalid template filename.
"""
config = tmpdir.join("msteams01.yml")
config.write("""
urls:
- {url}:
- tag: 'msteams'
template: {template}.missing
:name: 'Template.Missing'
:body: 'test body'
:title: 'test title'
""".format(url=msteams_url, template=str(simple_template)))
cfg = AppriseConfig()
cfg.add(str(config))
assert len(cfg) == 1
assert len(cfg[0]) == 1
obj = cfg[0][0]
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is False
assert request_mock.called is False
def test_msteams_yaml_config_token_identifiers(
request_mock, msteams_url, simple_template, tmpdir):
"""
NotifyMSTeams() YAML Configuration Entries - test token identifiers.
"""
config = tmpdir.join("msteams01.yml")
config.write("""
urls:
- {url}:
- tag: 'msteams'
template: {template}
:name: 'Testing'
:body: 'test body'
:title: 'test title'
""".format(url=msteams_url, template=str(simple_template)))
cfg = AppriseConfig()
cfg.add(str(config))
assert len(cfg) == 1
assert len(cfg[0]) == 1
obj = cfg[0][0]
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is True
assert request_mock.called is True
assert request_mock.call_args_list[0][0][0].startswith(
'https://outlook.office.com/webhook/')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'summary' in posted_json
assert posted_json['summary'] == 'Testing'
assert posted_json['themeColor'] == '#3AA3E3'
assert posted_json['sections'][0]['activityTitle'] == 'test title'
assert posted_json['sections'][0]['text'] == 'test body'
def test_msteams_yaml_config_no_bullet_under_url_1(
request_mock, msteams_url, simple_template, tmpdir):
"""
NotifyMSTeams() YAML Configuration Entries - no bullet 1.
Now again but without a bullet under the url definition.
"""
config = tmpdir.join("msteams02.yml")
config.write("""
urls:
- {url}:
tag: 'msteams'
template: {template}
:name: 'Testing2'
:body: 'test body2'
:title: 'test title2'
""".format(url=msteams_url, template=str(simple_template)))
cfg = AppriseConfig()
cfg.add(str(config))
assert len(cfg) == 1
assert len(cfg[0]) == 1
obj = cfg[0][0]
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is True
assert request_mock.called is True
assert request_mock.call_args_list[0][0][0].startswith(
'https://outlook.office.com/webhook/')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'summary' in posted_json
assert posted_json['summary'] == 'Testing2'
assert posted_json['themeColor'] == '#3AA3E3'
assert posted_json['sections'][0]['activityTitle'] == 'test title2'
assert posted_json['sections'][0]['text'] == 'test body2'
def test_msteams_yaml_config_dictionary_file(
request_mock, msteams_url, simple_template, tmpdir):
"""
NotifyMSTeams() YAML Configuration Entries.
Try again but store the content as a dictionary in the configuration file.
"""
config = tmpdir.join("msteams03.yml")
config.write("""
urls:
- {url}:
- tag: 'msteams'
template: {template}
tokens:
name: 'Testing3'
body: 'test body3'
title: 'test title3'
""".format(url=msteams_url, template=str(simple_template)))
cfg = AppriseConfig()
cfg.add(str(config))
assert len(cfg) == 1
assert len(cfg[0]) == 1
obj = cfg[0][0]
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is True
assert request_mock.called is True
assert request_mock.call_args_list[0][0][0].startswith(
'https://outlook.office.com/webhook/')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'summary' in posted_json
assert posted_json['summary'] == 'Testing3'
assert posted_json['themeColor'] == '#3AA3E3'
assert posted_json['sections'][0]['activityTitle'] == 'test title3'
assert posted_json['sections'][0]['text'] == 'test body3'
def test_msteams_yaml_config_no_bullet_under_url_2(
request_mock, msteams_url, simple_template, tmpdir):
"""
NotifyMSTeams() YAML Configuration Entries - no bullet 2.
Now again but without a bullet under the url definition.
"""
config = tmpdir.join("msteams04.yml")
config.write("""
urls:
- {url}:
tag: 'msteams'
template: {template}
tokens:
name: 'Testing4'
body: 'test body4'
title: 'test title4'
""".format(url=msteams_url, template=str(simple_template)))
cfg = AppriseConfig()
cfg.add(str(config))
assert len(cfg) == 1
assert len(cfg[0]) == 1
obj = cfg[0][0]
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is True
assert request_mock.called is True
assert request_mock.call_args_list[0][0][0].startswith(
'https://outlook.office.com/webhook/')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'summary' in posted_json
assert posted_json['summary'] == 'Testing4'
assert posted_json['themeColor'] == '#3AA3E3'
assert posted_json['sections'][0]['activityTitle'] == 'test title4'
assert posted_json['sections'][0]['text'] == 'test body4'
def test_msteams_yaml_config_combined(
request_mock, msteams_url, simple_template, tmpdir):
"""
NotifyMSTeams() YAML Configuration Entries.
Now let's do a combination of the two.
"""
config = tmpdir.join("msteams05.yml")
config.write("""
urls:
- {url}:
- tag: 'msteams'
template: {template}
tokens:
body: 'test body5'
title: 'test title5'
:name: 'Testing5'
""".format(url=msteams_url, template=str(simple_template)))
cfg = AppriseConfig()
cfg.add(str(config))
assert len(cfg) == 1
assert len(cfg[0]) == 1
obj = cfg[0][0]
assert isinstance(obj, NotifyMSTeams)
assert obj.notify(
body="body", title='title',
notify_type=NotifyType.INFO) is True
assert request_mock.called is True
assert request_mock.call_args_list[0][0][0].startswith(
'https://outlook.office.com/webhook/')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'summary' in posted_json
assert posted_json['summary'] == 'Testing5'
assert posted_json['themeColor'] == '#3AA3E3'
assert posted_json['sections'][0]['activityTitle'] == 'test title5'
assert posted_json['sections'][0]['text'] == 'test body5'
def test_msteams_yaml_config_token_mismatch(
request_mock, msteams_url, simple_template, tmpdir):
"""
NotifyMSTeams() YAML Configuration Entries.
Now let's do a test where our tokens is not the
expected dictionary we want to see.
"""
config = tmpdir.join("msteams06.yml")
config.write("""
urls:
- {url}:
- tag: 'msteams'
template: {template}
# Not a dictionary
tokens:
body
""".format(url=msteams_url, template=str(simple_template)))
cfg = AppriseConfig()
cfg.add(str(config))
assert len(cfg) == 1
# It could not load because of invalid tokens
assert len(cfg[0]) == 0
def test_plugin_msteams_edge_cases():
"""
NotifyMSTeams() Edge Cases
"""
# Initializes the plugin with an invalid token
with pytest.raises(TypeError):
NotifyMSTeams(token_a=None, token_b='abcd', token_c='abcd')
# Whitespace also acts as an invalid token value
with pytest.raises(TypeError):
NotifyMSTeams(token_a=' ', token_b='abcd', token_c='abcd')
with pytest.raises(TypeError):
NotifyMSTeams(token_a='abcd', token_b=None, token_c='abcd')
# Whitespace also acts as an invalid token value
with pytest.raises(TypeError):
NotifyMSTeams(token_a='abcd', token_b=' ', token_c='abcd')
with pytest.raises(TypeError):
NotifyMSTeams(token_a='abcd', token_b='abcd', token_c=None)
# Whitespace also acts as an invalid token value
with pytest.raises(TypeError):
NotifyMSTeams(token_a='abcd', token_b='abcd', token_c=' ')
uuid4 = '8b799edf-6f98-4d3a-9be7-2862fb4e5752'
token_a = '{}@{}'.format(uuid4, uuid4)
token_b = 'A' * 32
# test case where no tokens are specified
obj = NotifyMSTeams(
token_a=token_a, token_b=token_b, token_c=uuid4)
assert isinstance(obj, NotifyMSTeams)
|