| 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 json import loads |
| 29 |
from unittest import mock |
| 30 |
|
| 31 |
import requests |
| 32 |
|
| 33 |
import apprise |
| 34 |
|
| 35 |
|
| 36 |
@mock.patch("requests.request") |
| 37 |
def test_apprise_interpret_escapes(mock_request): |
| 38 |
""" |
| 39 |
API: Apprise() interpret-escape tests |
| 40 |
""" |
| 41 |
|
| 42 |
|
| 43 |
mock_request.return_value = requests.Request() |
| 44 |
mock_request.return_value.status_code = requests.codes.ok |
| 45 |
|
| 46 |
|
| 47 |
asset = apprise.AppriseAsset() |
| 48 |
assert asset.interpret_escapes is False |
| 49 |
|
| 50 |
|
| 51 |
a = apprise.Apprise(asset=asset) |
| 52 |
|
| 53 |
|
| 54 |
assert a.add("json://localhost") is True |
| 55 |
|
| 56 |
|
| 57 |
assert a[0].asset.interpret_escapes is False |
| 58 |
|
| 59 |
|
| 60 |
assert a.notify("ab\\ncd") is True |
| 61 |
|
| 62 |
|
| 63 |
assert mock_request.call_count == 1 |
| 64 |
details = mock_request.call_args_list[0] |
| 65 |
assert details[0][0] == "POST" |
| 66 |
|
| 67 |
|
| 68 |
assert loads(details[1]["data"]).get("message", "") == "ab\\ncd" |
| 69 |
|
| 70 |
|
| 71 |
mock_request.reset_mock() |
| 72 |
|
| 73 |
|
| 74 |
assert a.notify("ab\\ncd", interpret_escapes=True) is True |
| 75 |
|
| 76 |
|
| 77 |
assert mock_request.call_count == 1 |
| 78 |
details = mock_request.call_args_list[0] |
| 79 |
assert details[0][0] == "POST" |
| 80 |
|
| 81 |
|
| 82 |
assert loads(details[1]["data"]).get("message", "") == "ab\ncd" |
| 83 |
|
| 84 |
|
| 85 |
mock_request.reset_mock() |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
asset = apprise.AppriseAsset(interpret_escapes=True) |
| 94 |
assert asset.interpret_escapes is True |
| 95 |
|
| 96 |
|
| 97 |
a = apprise.Apprise(asset=asset) |
| 98 |
|
| 99 |
|
| 100 |
assert a.add("json://localhost") is True |
| 101 |
|
| 102 |
|
| 103 |
assert a[0].asset.interpret_escapes is True |
| 104 |
|
| 105 |
|
| 106 |
assert a.notify("ab\\ncd") is True |
| 107 |
|
| 108 |
|
| 109 |
assert mock_request.call_count == 1 |
| 110 |
details = mock_request.call_args_list[0] |
| 111 |
assert details[0][0] == "POST" |
| 112 |
|
| 113 |
|
| 114 |
assert loads(details[1]["data"]).get("message", "") == "ab\ncd" |
| 115 |
|
| 116 |
|
| 117 |
mock_request.reset_mock() |
| 118 |
|
| 119 |
|
| 120 |
assert a.notify("ab\\ncd", interpret_escapes=False) is True |
| 121 |
|
| 122 |
|
| 123 |
assert mock_request.call_count == 1 |
| 124 |
details = mock_request.call_args_list[0] |
| 125 |
assert details[0][0] == "POST" |
| 126 |
|
| 127 |
|
| 128 |
assert loads(details[1]["data"]).get("message", "") == "ab\\ncd" |
| 129 |
|
| 130 |
|
| 131 |
@mock.patch("requests.request") |
| 132 |
def test_apprise_escaping(mock_request): |
| 133 |
""" |
| 134 |
API: Apprise() escaping tests |
| 135 |
|
| 136 |
""" |
| 137 |
a = apprise.Apprise() |
| 138 |
|
| 139 |
response = mock.Mock() |
| 140 |
response.content = "" |
| 141 |
response.status_code = requests.codes.ok |
| 142 |
mock_request.return_value = response |
| 143 |
|
| 144 |
|
| 145 |
a.add("json://localhost") |
| 146 |
|
| 147 |
|
| 148 |
assert a.notify( |
| 149 |
title="\\r\\ntitle\\r\\n", |
| 150 |
body="\\r\\nbody\\r\\n", |
| 151 |
interpret_escapes=True, |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
assert mock_request.call_count == 1 |
| 156 |
details = mock_request.call_args_list[0] |
| 157 |
assert details[0][0] == "POST" |
| 158 |
result = loads(details[1]["data"]) |
| 159 |
assert result["title"] == "title" |
| 160 |
assert result["message"] == "\r\nbody" |
| 161 |
|
| 162 |
|
| 163 |
mock_request.reset_mock() |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
assert a.notify( |
| 171 |
|
| 172 |
title="دعونا نجعل العالم مكانا أفضل.\\r\\t\\t\\n\\r\\n", |
| 173 |
|
| 174 |
body="Egy sor kódot egyszerre.\\r\\n\\r\\r\\n", |
| 175 |
|
| 176 |
interpret_escapes=True, |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
assert mock_request.call_count == 1 |
| 181 |
details = mock_request.call_args_list[0] |
| 182 |
assert details[0][0] == "POST" |
| 183 |
result = loads(details[1]["data"]) |
| 184 |
assert result["title"] == "دعونا نجعل العالم مكانا أفضل." |
| 185 |
assert result["message"] == "Egy sor kódot egyszerre." |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
assert a.notify(title=None, body=4, interpret_escapes=True) is False |
| 191 |
assert a.notify(title=4, body=None, interpret_escapes=True) is False |
| 192 |
assert ( |
| 193 |
a.notify(title=object(), body=False, interpret_escapes=True) is False |
| 194 |
) |
| 195 |
assert ( |
| 196 |
a.notify(title=False, body=object(), interpret_escapes=True) is False |
| 197 |
) |
| 198 |
|
| 199 |
|
| 200 |
assert ( |
| 201 |
a.notify( |
| 202 |
title=b"byte title", body=b"byte body", interpret_escapes=True |
| 203 |
) |
| 204 |
is True |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
ful title' |
| 211 |
cation' |
| 212 |
title = "כותרת נפלאה".encode("ISO-8859-8") |
| 213 |
body = "[_[זו הודעה](http://localhost)_".encode("ISO-8859-8") |
| 214 |
assert a.notify(title=title, body=body, interpret_escapes=True) is False |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
asset = apprise.AppriseAsset(encoding="ISO-8859-8") |
| 219 |
a = apprise.Apprise(asset=asset) |
| 220 |
|
| 221 |
a.add("json://localhost") |
| 222 |
assert a.notify(title=title, body=body, interpret_escapes=True) is True |
| 223 |
|
| 224 |
|
| 225 |
a = apprise.Apprise() |
| 226 |
a.add("json://localhost") |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
assert a.notify(title=None, body="valid", interpret_escapes=True) is True |
| 232 |
assert a.notify(title=4, body="valid", interpret_escapes=True) is False |
| 233 |
assert ( |
| 234 |
a.notify(title=object(), body="valid", interpret_escapes=True) is False |
| 235 |
) |
| 236 |
assert a.notify(title=False, body="valid", interpret_escapes=True) is True |
| 237 |
|
| 238 |
assert ( |
| 239 |
a.notify(title=b"byte title", body="valid", interpret_escapes=True) |
| 240 |
is True |
| 241 |
) |