| 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 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import pytest |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise, NotifyFormat, NotifyType |
| 39 |
from apprise.plugins.google_chat import NotifyGoogleChat |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"gchat://", |
| 47 |
{ |
| 48 |
"instance": TypeError, |
| 49 |
}, |
| 50 |
), |
| 51 |
( |
| 52 |
"gchat://:@/", |
| 53 |
{ |
| 54 |
"instance": TypeError, |
| 55 |
}, |
| 56 |
), |
| 57 |
|
| 58 |
( |
| 59 |
"gchat://workspace", |
| 60 |
{ |
| 61 |
"instance": TypeError, |
| 62 |
}, |
| 63 |
), |
| 64 |
|
| 65 |
( |
| 66 |
"gchat://workspace/key/", |
| 67 |
{ |
| 68 |
"instance": TypeError, |
| 69 |
}, |
| 70 |
), |
| 71 |
|
| 72 |
( |
| 73 |
"gchat://workspace/key/token", |
| 74 |
{ |
| 75 |
"instance": NotifyGoogleChat, |
| 76 |
"privacy_url": "gchat://w...e/k...y/t...n", |
| 77 |
}, |
| 78 |
), |
| 79 |
|
| 80 |
( |
| 81 |
"gchat://?workspace=ws&key=mykey&token=mytoken", |
| 82 |
{ |
| 83 |
"instance": NotifyGoogleChat, |
| 84 |
"privacy_url": "gchat://w...s/m...y/m...n", |
| 85 |
}, |
| 86 |
), |
| 87 |
( |
| 88 |
"gchat://?workspace=ws&key=mykey&token=mytoken&thread=abc123", |
| 89 |
{ |
| 90 |
|
| 91 |
"instance": NotifyGoogleChat, |
| 92 |
"privacy_url": "gchat://w...s/m...y/m...n/a...3", |
| 93 |
}, |
| 94 |
), |
| 95 |
( |
| 96 |
"gchat://?workspace=ws&key=mykey&token=mytoken&threadKey=abc345", |
| 97 |
{ |
| 98 |
|
| 99 |
"instance": NotifyGoogleChat, |
| 100 |
"privacy_url": "gchat://w...s/m...y/m...n/a...5", |
| 101 |
}, |
| 102 |
), |
| 103 |
|
| 104 |
( |
| 105 |
( |
| 106 |
"https://chat.googleapis.com/v1/spaces/myworkspace/messages" |
| 107 |
"?key=mykey&token=mytoken" |
| 108 |
), |
| 109 |
{ |
| 110 |
"instance": NotifyGoogleChat, |
| 111 |
"privacy_url": "gchat://m...e/m...y/m...n", |
| 112 |
}, |
| 113 |
), |
| 114 |
( |
| 115 |
( |
| 116 |
"https://chat.googleapis.com/v1/spaces/myworkspace/messages" |
| 117 |
"?key=mykey&token=mytoken&threadKey=mythreadkey" |
| 118 |
), |
| 119 |
{ |
| 120 |
"instance": NotifyGoogleChat, |
| 121 |
"privacy_url": "gchat://m...e/m...y/m...n/m...y", |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
"gchat://workspace/key/token", |
| 126 |
{ |
| 127 |
"instance": NotifyGoogleChat, |
| 128 |
|
| 129 |
"response": False, |
| 130 |
"requests_response_code": requests.codes.internal_server_error, |
| 131 |
}, |
| 132 |
), |
| 133 |
( |
| 134 |
"gchat://workspace/key/token", |
| 135 |
{ |
| 136 |
"instance": NotifyGoogleChat, |
| 137 |
|
| 138 |
"response": False, |
| 139 |
"requests_response_code": 999, |
| 140 |
}, |
| 141 |
), |
| 142 |
( |
| 143 |
"gchat://workspace/key/token", |
| 144 |
{ |
| 145 |
"instance": NotifyGoogleChat, |
| 146 |
|
| 147 |
|
| 148 |
"test_requests_exceptions": True, |
| 149 |
}, |
| 150 |
), |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
def test_plugin_google_chat_urls(): |
| 155 |
"""NotifyGoogleChat() Apprise URLs.""" |
| 156 |
|
| 157 |
|
| 158 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 159 |
|
| 160 |
|
| 161 |
@mock.patch("requests.post") |
| 162 |
def test_plugin_google_chat_general(mock_post): |
| 163 |
"""NotifyGoogleChat() General Checks.""" |
| 164 |
|
| 165 |
|
| 166 |
workspace = "ws" |
| 167 |
key = "key" |
| 168 |
threadkey = "threadkey" |
| 169 |
token = "token" |
| 170 |
|
| 171 |
|
| 172 |
mock_post.return_value = requests.Request() |
| 173 |
mock_post.return_value.status_code = requests.codes.ok |
| 174 |
|
| 175 |
|
| 176 |
obj = Apprise.instantiate(f"gchat://{workspace}/{key}/{token}") |
| 177 |
assert isinstance(obj, NotifyGoogleChat) |
| 178 |
assert ( |
| 179 |
obj.notify( |
| 180 |
body="test body", title="title", notify_type=NotifyType.INFO |
| 181 |
) |
| 182 |
is True |
| 183 |
) |
| 184 |
|
| 185 |
|
| 186 |
assert mock_post.call_count == 1 |
| 187 |
assert ( |
| 188 |
mock_post.call_args_list[0][0][0] |
| 189 |
== "https://chat.googleapis.com/v1/spaces/ws/messages" |
| 190 |
) |
| 191 |
params = mock_post.call_args_list[0][1]["params"] |
| 192 |
assert params.get("token") == token |
| 193 |
assert params.get("key") == key |
| 194 |
assert "messageReplyOption" not in params |
| 195 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 196 |
assert "thread" not in payload |
| 197 |
assert payload["text"] == "title\r\ntest body" |
| 198 |
|
| 199 |
mock_post.reset_mock() |
| 200 |
|
| 201 |
|
| 202 |
obj = Apprise.instantiate(f"gchat://{workspace}/{key}/{token}/{threadkey}") |
| 203 |
assert isinstance(obj, NotifyGoogleChat) |
| 204 |
assert ( |
| 205 |
obj.notify( |
| 206 |
body="test body", title="title", notify_type=NotifyType.INFO |
| 207 |
) |
| 208 |
is True |
| 209 |
) |
| 210 |
|
| 211 |
|
| 212 |
assert mock_post.call_count == 1 |
| 213 |
assert ( |
| 214 |
mock_post.call_args_list[0][0][0] |
| 215 |
== "https://chat.googleapis.com/v1/spaces/ws/messages" |
| 216 |
) |
| 217 |
params = mock_post.call_args_list[0][1]["params"] |
| 218 |
assert params.get("token") == token |
| 219 |
assert params.get("key") == key |
| 220 |
assert ( |
| 221 |
params.get("messageReplyOption") |
| 222 |
== "REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD" |
| 223 |
) |
| 224 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 225 |
assert "thread" in payload |
| 226 |
assert payload["text"] == "title\r\ntest body" |
| 227 |
assert payload["thread"].get("thread_key") == threadkey |
| 228 |
|
| 229 |
|
| 230 |
def test_plugin_google_chat_edge_case(): |
| 231 |
"""NotifyGoogleChat() Edge Cases.""" |
| 232 |
with pytest.raises(TypeError): |
| 233 |
NotifyGoogleChat("workspace", "webhook", "token", thread_key=object()) |
| 234 |
|
| 235 |
|
| 236 |
@mock.patch("requests.post") |
| 237 |
def test_plugin_google_chat_html_to_markdown_hardening(mock_post): |
| 238 |
"""Test edge cases in the CommonMark-to-Google-Chat dialect |
| 239 |
adaptation.""" |
| 240 |
|
| 241 |
mock_post.return_value = requests.Request() |
| 242 |
mock_post.return_value.status_code = requests.codes.ok |
| 243 |
mock_post.return_value.content = b"{}" |
| 244 |
mock_post.return_value.text = "{}" |
| 245 |
|
| 246 |
def notify(body): |
| 247 |
aobj = Apprise() |
| 248 |
assert aobj.add("gchat://workspace/key/token") |
| 249 |
assert aobj.notify(body=body, body_format=NotifyFormat.HTML) is True |
| 250 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 251 |
mock_post.reset_mock() |
| 252 |
return payload["text"] |
| 253 |
|
| 254 |
|
| 255 |
assert notify("<b>hello</b> <i>world</i>") == "*hello* _world_" |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
assert ( |
| 260 |
notify('<a href="https://example.com/x">click here</a>') |
| 261 |
== "<https://example.com/x|click here>" |
| 262 |
) |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
assert notify("<p>2 < 3 & 4</p>") == "2 < 3 & 4" |
| 267 |
|
| 268 |
|
| 269 |
assert ( |
| 270 |
notify('<a href="https://e/x>TAIL">click</a>') |
| 271 |
== "<https://e/x>TAIL|click>" |
| 272 |
) |
| 273 |
|
| 274 |
|
| 275 |
assert ( |
| 276 |
notify('<a href="https://e/x|evil">click</a>') |
| 277 |
== "<https://e/x%7Cevil|click>" |
| 278 |
) |
| 279 |
|
| 280 |
|
| 281 |
assert notify("<code>a < b & c</code>") == "`a < b & c`" |
| 282 |
|
| 283 |
|
| 284 |
assert notify("<b><i>x</i></b>") == "*_x_*" |
| 285 |
|
| 286 |
|
| 287 |
assert notify("<b>bold <i>italic</i> still bold</b>") == ( |
| 288 |
"*bold _italic_ still bold*" |
| 289 |
) |
| 290 |
assert notify("<b>A</b><b>B</b>") == "*A**B*" |
| 291 |
|
| 292 |
|
| 293 |
aobj = Apprise() |
| 294 |
assert aobj.add("gchat://workspace/key/token") |
| 295 |
assert aobj.notify(body="*already* chat-bound markdown") is True |
| 296 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 297 |
assert payload["text"] == "*already* chat-bound markdown" |
| 298 |
mock_post.reset_mock() |
| 299 |
|
| 300 |
|
| 301 |
assert ( |
| 302 |
NotifyGoogleChat._commonmark_to_google_chat("text ``unterminated") |
| 303 |
== "text ``unterminated" |
| 304 |
) |
| 305 |
|
| 306 |
|
| 307 |
assert NotifyGoogleChat._commonmark_to_google_chat("****x") == "x" |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
f = NotifyGoogleChat._commonmark_to_google_chat |
| 312 |
assert f("\\>literal") == ">literal" |
| 313 |
|
| 314 |
|
| 315 |
assert f("\\!text") == "!text" |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
assert f("````\n``` nested\n indented\nlast\n````") == ( |
| 320 |
"````\n``` nested\n indented\nlast\n````" |
| 321 |
) |
| 322 |
|
| 323 |
|
| 324 |
assert f("\\*text\\*") == "\\*text\\*" |
| 325 |
assert f("\\_text\\_") == "\\_text\\_" |
| 326 |
assert f("\\~text\\~") == "\\~text\\~" |
| 327 |
assert f("\\`text\\`") == "\\`text\\`" |
| 328 |
|
| 329 |
|
| 330 |
assert f("[text](<https://example.com/unterminated") == ( |
| 331 |
"[text](<https://example.com/unterminated" |
| 332 |
) |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
assert f("**unterminated") == "*unterminated*" |
| 337 |
|
| 338 |
|
| 339 |
assert f("**") == "" |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
aobj = Apprise() |
| 345 |
assert aobj.add("gchat://workspace/key/token") |
| 346 |
assert ( |
| 347 |
aobj.notify( |
| 348 |
body="<b>hello</b>", |
| 349 |
title="My Title", |
| 350 |
body_format=NotifyFormat.HTML, |
| 351 |
) |
| 352 |
is True |
| 353 |
) |
| 354 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 355 |
assert payload["text"] == "# My Title\n*hello*" |
| 356 |
mock_post.reset_mock() |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
assert ( |
| 362 |
aobj.notify( |
| 363 |
body="<b>hello</b>", |
| 364 |
title=" - ", |
| 365 |
body_format=NotifyFormat.HTML, |
| 366 |
) |
| 367 |
is True |
| 368 |
) |
| 369 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 370 |
assert payload["text"] == "*hello*" |
| 371 |
mock_post.reset_mock() |