| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
from inspect import cleandoc |
| 29 |
from json import loads |
| 30 |
|
| 31 |
# Disable logging for a cleaner testing output |
| 32 |
import logging |
| 33 |
import os |
| 34 |
|
| 35 |
import pytest |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise |
| 39 |
from apprise.config import ConfigBase |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
# Attachment Directory |
| 44 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 45 |
|
| 46 |
|
| 47 |
@pytest.fixture |
| 48 |
def request_mock(mocker): |
| 49 |
"""Prepare requests mock.""" |
| 50 |
resp = requests.Request() |
| 51 |
resp.status_code = requests.codes.ok |
| 52 |
resp.content = "" |
| 53 |
# json/form/xml plugins now use requests.request(); |
| 54 |
# telegram still uses requests.post() |
| 55 |
mock_request = mocker.patch("requests.request") |
| 56 |
mock_request.return_value = resp |
| 57 |
mock_post = mocker.patch("requests.post") |
| 58 |
mock_post.return_value = resp |
| 59 |
return mock_request, mock_post |
| 60 |
|
| 61 |
|
| 62 |
def test_plugin_title_maxlen(request_mock): |
| 63 |
"""Plugin title maxlen blending support.""" |
| 64 |
mock_request, mock_post = request_mock |
| 65 |
|
| 66 |
# Load our configuration |
| 67 |
result, _ = ConfigBase.config_parse_yaml( |
| 68 |
cleandoc(""" |
| 69 |
urls: |
| 70 |
|
| 71 |
# Our JSON plugin allows for a title definition; we enforce a html format |
| 72 |
- json://user:[email protected]?format=html |
| 73 |
# Telegram has a title_maxlen of 0 |
| 74 |
- tgram://123456789:AABCeFGhIJKLmnOPqrStUvWxYZ12345678U/987654321 |
| 75 |
""") |
| 76 |
) |
| 77 |
|
| 78 |
# Verify we loaded correctly |
| 79 |
assert isinstance(result, list) |
| 80 |
assert len(result) == 2 |
| 81 |
assert len(result[0].tags) == 0 |
| 82 |
|
| 83 |
aobj = Apprise() |
| 84 |
aobj.add(result) |
| 85 |
assert len(aobj) == 2 |
| 86 |
|
| 87 |
title = "Hello World" |
| 88 |
body = "Foo Bar" |
| 89 |
assert aobj.notify(title=title, body=body) |
| 90 |
|
| 91 |
# JSON uses requests.request, Telegram uses requests.post |
| 92 |
assert mock_request.call_count == 1 |
| 93 |
assert mock_post.call_count == 1 |
| 94 |
|
| 95 |
details = mock_request.call_args_list[0] |
| 96 |
assert details[0][0] == "POST" |
| 97 |
assert details[0][1] == "http://example.ca" |
| 98 |
payload = loads(details[1]["data"]) |
| 99 |
assert payload["message"] == body |
| 100 |
assert payload["title"] == "Hello World" |
| 101 |
|
| 102 |
# Telegram plugin: requests.post(url, data=...) -> url is [0][0] |
| 103 |
details = mock_post.call_args_list[0] |
| 104 |
assert ( |
| 105 |
details[0][0] == "https://api.telegram.org/bot123456789:" |
| 106 |
"AABCeFGhIJKLmnOPqrStUvWxYZ12345678U/sendMessage" |
| 107 |
) |
| 108 |
payload = loads(details[1]["data"]) |
| 109 |
# HTML in Title is escaped |
| 110 |
assert payload["text"] == "<b>Hello World</b>\r\nFoo Bar" |
| 111 |
|
| 112 |
# Reset our mock objects |
| 113 |
mock_request.reset_mock() |
| 114 |
mock_post.reset_mock() |
| 115 |
# |
| 116 |
# Reverse the configuration file and expect the same results |
| 117 |
# |
| 118 |
result, _config = ConfigBase.config_parse_yaml( |
| 119 |
cleandoc(""" |
| 120 |
urls: |
| 121 |
|
| 122 |
# Telegram has a title_maxlen of 0 |
| 123 |
- tgram://123456789:AABCeFGhIJKLmnOPqrStUvWxYZ12345678U/987654321 |
| 124 |
# Our JSON plugin allows for a title definition; we enforce a html format |
| 125 |
- json://user:[email protected]?format=html |
| 126 |
""") |
| 127 |
) |
| 128 |
|
| 129 |
# Verify we loaded correctly |
| 130 |
assert isinstance(result, list) |
| 131 |
assert len(result) == 2 |
| 132 |
assert len(result[0].tags) == 0 |
| 133 |
|
| 134 |
aobj = Apprise() |
| 135 |
aobj.add(result) |
| 136 |
assert len(aobj) == 2 |
| 137 |
|
| 138 |
title = "Hello World" |
| 139 |
body = "Foo Bar" |
| 140 |
assert aobj.notify(title=title, body=body) |
| 141 |
|
| 142 |
# JSON uses requests.request, Telegram uses requests.post |
| 143 |
assert mock_request.call_count == 1 |
| 144 |
assert mock_post.call_count == 1 |
| 145 |
|
| 146 |
details = mock_post.call_args_list[0] |
| 147 |
assert ( |
| 148 |
details[0][0] == "https://api.telegram.org/bot123456789:" |
| 149 |
"AABCeFGhIJKLmnOPqrStUvWxYZ12345678U/sendMessage" |
| 150 |
) |
| 151 |
payload = loads(details[1]["data"]) |
| 152 |
# HTML in Title is escaped |
| 153 |
assert payload["text"] == "<b>Hello World</b>\r\nFoo Bar" |
| 154 |
|
| 155 |
details = mock_request.call_args_list[0] |
| 156 |
assert details[0][0] == "POST" |
| 157 |
assert details[0][1] == "http://example.ca" |
| 158 |
payload = loads(details[1]["data"]) |
| 159 |
assert payload["message"] == body |
| 160 |
assert payload["title"] == "Hello World" |