| 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 |
Unit Tests for the XMPP plugin. |
| 30 |
|
| 31 |
""" |
| 32 |
|
| 33 |
from __future__ import annotations |
| 34 |
|
| 35 |
import asyncio |
| 36 |
import contextlib |
| 37 |
import gc |
| 38 |
import logging |
| 39 |
import ssl |
| 40 |
import threading |
| 41 |
import time |
| 42 |
from types import SimpleNamespace |
| 43 |
from typing import Any, Optional |
| 44 |
|
| 45 |
import pytest |
| 46 |
|
| 47 |
from apprise import LOGGER_NAME, Apprise, NotifyType |
| 48 |
from apprise.plugins.xmpp import adapter as xmpp_adapter, base as xmpp_base |
| 49 |
from apprise.plugins.xmpp.base import NotifyXMPP |
| 50 |
|
| 51 |
|
| 52 |
def run_on_loop(loop: asyncio.AbstractEventLoop, coro: Any) -> Any: |
| 53 |
"""Run a coroutine on a specific event loop.""" |
| 54 |
asyncio.set_event_loop(loop) |
| 55 |
return loop.run_until_complete(coro) |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
try: |
| 63 |
|
| 64 |
SLIXMPP_AVAILABLE = xmpp_adapter.SlixmppAdapter.supported_version() |
| 65 |
|
| 66 |
except Exception: |
| 67 |
SLIXMPP_AVAILABLE = False |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
WORKER_THREAD_STARTUP_DELAY: float = 0.02 |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
class FakeClientXMPP: |
| 82 |
def __init__(self, jid: str, password: str) -> None: |
| 83 |
self.jid = jid |
| 84 |
self.password = password |
| 85 |
|
| 86 |
|
| 87 |
self.loop: Optional[asyncio.AbstractEventLoop] = None |
| 88 |
self.handlers: dict[str, Any] = {} |
| 89 |
self.auto_reconnect = True |
| 90 |
|
| 91 |
|
| 92 |
self.disconnected: Optional[asyncio.Future[bool]] = None |
| 93 |
|
| 94 |
|
| 95 |
self.enable_plaintext = True |
| 96 |
self.enable_starttls = True |
| 97 |
self.enable_direct_tls = False |
| 98 |
self.ssl_context = None |
| 99 |
|
| 100 |
|
| 101 |
self.registered_plugins: dict[str, Any] = {} |
| 102 |
|
| 103 |
def add_event_handler(self, name: str, handler: Any) -> None: |
| 104 |
self.handlers[name] = handler |
| 105 |
|
| 106 |
def send_presence(self) -> None: |
| 107 |
return None |
| 108 |
|
| 109 |
async def get_roster(self) -> None: |
| 110 |
return None |
| 111 |
|
| 112 |
def send_message(self, **kwargs: Any) -> None: |
| 113 |
return None |
| 114 |
|
| 115 |
def register_plugin(self, name: str, config: Optional[Any] = None) -> None: |
| 116 |
self.registered_plugins[name] = config |
| 117 |
return None |
| 118 |
|
| 119 |
def disconnect(self) -> None: |
| 120 |
|
| 121 |
if self.disconnected is not None and not self.disconnected.done(): |
| 122 |
self.disconnected.set_result(True) |
| 123 |
|
| 124 |
def connect(self, **kwargs: Any) -> asyncio.Future[bool]: |
| 125 |
""" |
| 126 |
Slixmpp >= 1.16.0 connect() returns a Future. Our adapter awaits it. |
| 127 |
|
| 128 |
This fake schedules session_start on the assigned loop so the adapter's |
| 129 |
loop.run_until_complete() drives it, and ensures disconnected resolves. |
| 130 |
""" |
| 131 |
loop = self.loop or asyncio.get_running_loop() |
| 132 |
|
| 133 |
|
| 134 |
if self.disconnected is None: |
| 135 |
self.disconnected = loop.create_future() |
| 136 |
|
| 137 |
fut: asyncio.Future[bool] = loop.create_future() |
| 138 |
fut.set_result(True) |
| 139 |
|
| 140 |
handler = self.handlers.get("session_start") |
| 141 |
if handler: |
| 142 |
|
| 143 |
def _fire_session_start() -> None: |
| 144 |
rv = handler() |
| 145 |
if asyncio.iscoroutine(rv): |
| 146 |
task = loop.create_task(rv) |
| 147 |
|
| 148 |
task.add_done_callback(lambda _: self.disconnect()) |
| 149 |
else: |
| 150 |
|
| 151 |
self.disconnect() |
| 152 |
|
| 153 |
|
| 154 |
loop.call_soon(_fire_session_start) |
| 155 |
|
| 156 |
return fut |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
def install_fake_slixmpp(monkeypatch: pytest.MonkeyPatch) -> None: |
| 165 |
monkeypatch.setattr(xmpp_adapter, "asyncio", asyncio, raising=False) |
| 166 |
monkeypatch.setattr( |
| 167 |
xmpp_adapter, |
| 168 |
"slixmpp", |
| 169 |
SimpleNamespace(ClientXMPP=FakeClientXMPP), |
| 170 |
raising=False, |
| 171 |
) |
| 172 |
monkeypatch.setattr( |
| 173 |
xmpp_adapter, "SLIXMPP_SUPPORT_AVAILABLE", True, raising=False |
| 174 |
) |
| 175 |
monkeypatch.setattr( |
| 176 |
xmpp_adapter.SlixmppAdapter, |
| 177 |
"_enabled", |
| 178 |
True, |
| 179 |
raising=False, |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
def _patch_threading( |
| 184 |
monkeypatch: pytest.MonkeyPatch, *, done_event_cls: type |
| 185 |
) -> None: |
| 186 |
"""Patch adapter threading to use a custom done-event class.""" |
| 187 |
import threading as _real_threading |
| 188 |
|
| 189 |
class _ThreadingProxy: |
| 190 |
Event = done_event_cls |
| 191 |
Thread = _real_threading.Thread |
| 192 |
|
| 193 |
def __getattr__(self, name: str) -> Any: |
| 194 |
return getattr(_real_threading, name) |
| 195 |
|
| 196 |
monkeypatch.setattr( |
| 197 |
xmpp_adapter, "threading", _ThreadingProxy(), raising=True |
| 198 |
) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
def _make_fake_done_event_cls( |
| 207 |
signal_evt: Optional[threading.Event] = None, |
| 208 |
) -> type: |
| 209 |
"""Return a deterministic threading.Event replacement for timeout tests. |
| 210 |
|
| 211 |
The adapter under test uses ``threading.Event()`` for *done*, then calls |
| 212 |
``done.wait(timeout=...)`` and later ``done.set()`` from the worker thread. |
| 213 |
|
| 214 |
These tests want ``wait()`` to return ``False`` deterministically (forcing |
| 215 |
the timeout branch), while still giving the worker thread a chance to |
| 216 |
reach a known checkpoint first (e.g. event loop created, client created). |
| 217 |
|
| 218 |
Capturing *signal_evt* in a closure rather than as a class-level attribute |
| 219 |
ensures that each test's event class is fully independent — parallel test |
| 220 |
runs cannot interfere with one another through shared mutable class state. |
| 221 |
|
| 222 |
Parameters |
| 223 |
---------- |
| 224 |
signal_evt: |
| 225 |
Optional ``threading.Event`` that the worker thread sets when it |
| 226 |
reaches a stable checkpoint. If provided, ``wait()`` pauses until |
| 227 |
that event is set before forcing the timeout branch. |
| 228 |
""" |
| 229 |
|
| 230 |
class _FakeDoneEvent: |
| 231 |
def __init__(self) -> None: |
| 232 |
self._set = False |
| 233 |
self._wait_calls = 0 |
| 234 |
|
| 235 |
def is_set(self) -> bool: |
| 236 |
return self._set |
| 237 |
|
| 238 |
def set(self) -> None: |
| 239 |
self._set = True |
| 240 |
|
| 241 |
def clear(self) -> None: |
| 242 |
self._set = False |
| 243 |
|
| 244 |
def wait(self, timeout: Optional[float] = None) -> bool: |
| 245 |
self._wait_calls += 1 |
| 246 |
if self._wait_calls == 1: |
| 247 |
if signal_evt is not None: |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
signal_evt.wait(timeout=1.0) |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
time.sleep(WORKER_THREAD_STARTUP_DELAY) |
| 256 |
return False |
| 257 |
return self._set |
| 258 |
|
| 259 |
return _FakeDoneEvent |
| 260 |
|
| 261 |
|
| 262 |
@pytest.mark.skipif(SLIXMPP_AVAILABLE, reason="Requires slixmpp NOT installed") |
| 263 |
def test_slixmpp_unavailable() -> None: |
| 264 |
obj = NotifyXMPP(host="example.com", user="[email protected]", password="x") |
| 265 |
assert obj.send("will fail") is False |
| 266 |
|
| 267 |
|
| 268 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 269 |
def test_xmpp_invalid_jid() -> None: |
| 270 |
with pytest.raises(TypeError): |
| 271 |
NotifyXMPP(host="example.com", user="bad jid", password="x") |
| 272 |
|
| 273 |
|
| 274 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 275 |
def test_xmpp_targets_filtered() -> None: |
| 276 |
n = NotifyXMPP( |
| 277 |
host="example.com", |
| 278 |
user="[email protected]", |
| 279 |
password="x", |
| 280 |
targets=["[email protected]", " user1 user2 ", "", "[email protected]"], |
| 281 |
) |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
assert sorted(n.targets) == [ |
| 287 |
("chat", "[email protected]"), |
| 288 |
("chat", "[email protected]"), |
| 289 |
("chat", "[email protected]"), |
| 290 |
("chat", "[email protected]"), |
| 291 |
] |
| 292 |
|
| 293 |
|
| 294 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 295 |
def test_xmpp_len() -> None: |
| 296 |
n1 = NotifyXMPP(host="example.com", user="[email protected]", password="x") |
| 297 |
assert len(n1) == 1 |
| 298 |
|
| 299 |
n2 = NotifyXMPP( |
| 300 |
host="example.com", |
| 301 |
user="[email protected]", |
| 302 |
password="x", |
| 303 |
targets=["[email protected]", "[email protected]"], |
| 304 |
) |
| 305 |
|
| 306 |
assert len(n2) == 1 |
| 307 |
|
| 308 |
|
| 309 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 310 |
def test_xmpp_url_privacy() -> None: |
| 311 |
n = NotifyXMPP( |
| 312 |
host="example.com", |
| 313 |
user="[email protected]", |
| 314 |
password="secret", |
| 315 |
targets=["[email protected]"], |
| 316 |
verify=False, |
| 317 |
) |
| 318 |
u = n.url(privacy=True) |
| 319 |
assert "secret" not in u |
| 320 |
assert "verify=" in u |
| 321 |
|
| 322 |
|
| 323 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 324 |
def test_xmpp_parse_url() -> None: |
| 325 |
r = NotifyXMPP.parse_url( |
| 326 |
"xmpps://me:[email protected]/[email protected]?verify=no" |
| 327 |
) |
| 328 |
assert r["verify"] is False |
| 329 |
assert r["targets"] == ["[email protected]"] |
| 330 |
|
| 331 |
|
| 332 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 333 |
def test_xmpp_parse_url_to() -> None: |
| 334 |
r = NotifyXMPP.parse_url( |
| 335 |
"xmpp://me:[email protected][email protected],[email protected]" |
| 336 |
) |
| 337 |
assert sorted(r["targets"]) == ["[email protected]", "[email protected]"] |
| 338 |
|
| 339 |
|
| 340 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 341 |
def test_xmpp_invalid_targets_logged( |
| 342 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 343 |
) -> None: |
| 344 |
"""Invalid XMPP targets are dropped with a warning.""" |
| 345 |
|
| 346 |
caplog.set_level(logging.WARNING, logger=LOGGER_NAME) |
| 347 |
caplog.set_level(logging.WARNING, logger=xmpp_base.__name__) |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
monkeypatch.setattr( |
| 353 |
xmpp_base, |
| 354 |
"parse_list", |
| 355 |
lambda _v: ["bad jid", "[email protected]"], |
| 356 |
raising=True, |
| 357 |
) |
| 358 |
|
| 359 |
n = NotifyXMPP( |
| 360 |
host="example.com", |
| 361 |
user="[email protected]", |
| 362 |
password="x", |
| 363 |
targets=["ignored"], |
| 364 |
) |
| 365 |
|
| 366 |
assert n.targets == [("chat", "[email protected]")] |
| 367 |
assert "Dropped invalid XMPP target" in caplog.text |
| 368 |
|
| 369 |
|
| 370 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 371 |
def test_xmpp_url_identifier_is_accessible() -> None: |
| 372 |
"""url_identifier property should be accessible and stable.""" |
| 373 |
n = NotifyXMPP( |
| 374 |
host="example.com", user="[email protected]", password="secret" |
| 375 |
) |
| 376 |
schema, host, xmpp_host, user, password, port = n.url_identifier |
| 377 |
assert schema == "xmpp" |
| 378 |
assert host == "example.com" |
| 379 |
assert xmpp_host == "" |
| 380 |
assert user == "[email protected]" |
| 381 |
assert password == "secret" |
| 382 |
assert port is None |
| 383 |
|
| 384 |
|
| 385 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 386 |
def test_xmpp_server_override_parse_url() -> None: |
| 387 |
"""?xmpp= is extracted into xmpp_host by parse_url().""" |
| 388 |
r = NotifyXMPP.parse_url( |
| 389 |
"xmpps://[email protected]/joe?xmpp=xmpp.example.com" |
| 390 |
) |
| 391 |
assert r is not None |
| 392 |
assert r["xmpp_host"] == "xmpp.example.com" |
| 393 |
|
| 394 |
assert r["host"] == "example.com" |
| 395 |
|
| 396 |
|
| 397 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 398 |
def test_xmpp_server_override_constructor_and_url() -> None: |
| 399 |
"""xmpp_host is stored and round-trips through url().""" |
| 400 |
n = NotifyXMPP( |
| 401 |
host="example.com", |
| 402 |
user="user", |
| 403 |
password="pass", |
| 404 |
xmpp_host="xmpp.example.com", |
| 405 |
) |
| 406 |
|
| 407 |
|
| 408 |
assert n.xmpp_host == "xmpp.example.com" |
| 409 |
|
| 410 |
|
| 411 |
assert n.jid == "[email protected]" |
| 412 |
|
| 413 |
|
| 414 |
u = n.url(privacy=False) |
| 415 |
assert "xmpp=xmpp.example.com" in u |
| 416 |
assert "example.com" in u |
| 417 |
|
| 418 |
|
| 419 |
n2 = NotifyXMPP( |
| 420 |
host="example.com", |
| 421 |
user="user", |
| 422 |
password="pass", |
| 423 |
xmpp_host="example.com", |
| 424 |
) |
| 425 |
u2 = n2.url(privacy=False) |
| 426 |
assert "xmpp=" not in u2 |
| 427 |
|
| 428 |
|
| 429 |
n3 = NotifyXMPP( |
| 430 |
host="example.com", |
| 431 |
user="user", |
| 432 |
password="pass", |
| 433 |
) |
| 434 |
u3 = n3.url(privacy=False) |
| 435 |
assert "xmpp=" not in u3 |
| 436 |
|
| 437 |
|
| 438 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 439 |
def test_xmpp_server_override_target_jids_use_host() -> None: |
| 440 |
"""Target JIDs are built from host, not xmpp_host.""" |
| 441 |
n = NotifyXMPP( |
| 442 |
host="example.com", |
| 443 |
user="user", |
| 444 |
password="pass", |
| 445 |
targets=["joe"], |
| 446 |
xmpp_host="xmpp.example.com", |
| 447 |
) |
| 448 |
|
| 449 |
assert n.targets == [("chat", "[email protected]")] |
| 450 |
|
| 451 |
|
| 452 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 453 |
def test_xmpp_server_override_url_identifier() -> None: |
| 454 |
"""url_identifier carries xmpp_host so deduplication is xmpp_host-aware.""" |
| 455 |
n = NotifyXMPP( |
| 456 |
host="example.com", |
| 457 |
user="user", |
| 458 |
password="pass", |
| 459 |
xmpp_host="xmpp.example.com", |
| 460 |
) |
| 461 |
_schema, host, xmpp_host, _user, _password, _port = n.url_identifier |
| 462 |
assert host == "example.com" |
| 463 |
assert xmpp_host == "xmpp.example.com" |
| 464 |
|
| 465 |
|
| 466 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 467 |
def test_xmpp_server_override_config_host( |
| 468 |
monkeypatch: pytest.MonkeyPatch, |
| 469 |
) -> None: |
| 470 |
"""send() passes xmpp_host (not host) to XMPPConfig when set.""" |
| 471 |
captured: dict = {} |
| 472 |
|
| 473 |
class _Adapter: |
| 474 |
def __init__(self, config: Any, **kwargs: Any) -> None: |
| 475 |
captured["config_host"] = config.host |
| 476 |
|
| 477 |
def process(self) -> bool: |
| 478 |
return True |
| 479 |
|
| 480 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _Adapter, raising=True) |
| 481 |
|
| 482 |
apobj = Apprise() |
| 483 |
apobj.add("xmpps://[email protected]/joe?xmpp=xmpp.example.com") |
| 484 |
apobj.notify("hello") |
| 485 |
|
| 486 |
assert captured["config_host"] == "xmpp.example.com" |
| 487 |
|
| 488 |
|
| 489 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 490 |
def test_xmpp_server_override_config_host_defaults_to_host( |
| 491 |
monkeypatch: pytest.MonkeyPatch, |
| 492 |
) -> None: |
| 493 |
"""send() passes host to XMPPConfig when xmpp_host is not set.""" |
| 494 |
captured: dict = {} |
| 495 |
|
| 496 |
class _Adapter: |
| 497 |
def __init__(self, config: Any, **kwargs: Any) -> None: |
| 498 |
captured["config_host"] = config.host |
| 499 |
|
| 500 |
def process(self) -> bool: |
| 501 |
return True |
| 502 |
|
| 503 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _Adapter, raising=True) |
| 504 |
|
| 505 |
apobj = Apprise() |
| 506 |
apobj.add("xmpps://[email protected]/joe") |
| 507 |
apobj.notify("hello") |
| 508 |
|
| 509 |
assert captured["config_host"] == "example.com" |
| 510 |
|
| 511 |
|
| 512 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 513 |
def test_xmpp_url_handling() -> None: |
| 514 |
"""url() must not append a path when there are no targets.""" |
| 515 |
apobj = Apprise() |
| 516 |
assert apobj.add("xmpp://me:[email protected]?verify=no") is True |
| 517 |
|
| 518 |
assert len(apobj) == 1 |
| 519 |
plugin = apobj[0] |
| 520 |
|
| 521 |
u = plugin.url(privacy=False) |
| 522 |
assert "example.com/?" in u |
| 523 |
assert "mode=none" in u |
| 524 |
|
| 525 |
apobj = Apprise() |
| 526 |
assert apobj.add("xmpps://me:[email protected]?mode=tls") is True |
| 527 |
|
| 528 |
assert len(apobj) == 1 |
| 529 |
plugin = apobj[0] |
| 530 |
|
| 531 |
u = plugin.url(privacy=False) |
| 532 |
assert "mode=tls" in u |
| 533 |
|
| 534 |
|
| 535 |
apobj = Apprise() |
| 536 |
assert apobj.add("xmpps://me:[email protected]?mode=invalid") is False |
| 537 |
|
| 538 |
|
| 539 |
apobj = Apprise() |
| 540 |
assert apobj.add("xmpps://me:[email protected]?mode=none") is True |
| 541 |
|
| 542 |
assert len(apobj) == 1 |
| 543 |
plugin = apobj[0] |
| 544 |
|
| 545 |
u = plugin.url(privacy=False) |
| 546 |
|
| 547 |
assert "mode=starttls" in u |
| 548 |
|
| 549 |
|
| 550 |
apobj = Apprise() |
| 551 |
assert apobj.add("xmpp://me:[email protected]?mode=tls") is True |
| 552 |
|
| 553 |
assert len(apobj) == 1 |
| 554 |
plugin = apobj[0] |
| 555 |
|
| 556 |
u = plugin.url(privacy=False) |
| 557 |
|
| 558 |
assert "mode=tls" in u |
| 559 |
|
| 560 |
apobj = Apprise() |
| 561 |
assert ( |
| 562 |
apobj.add("xmpp://me:[email protected]?subject=yes&roster=yes") |
| 563 |
is True |
| 564 |
) |
| 565 |
|
| 566 |
assert len(apobj) == 1 |
| 567 |
plugin = apobj[0] |
| 568 |
|
| 569 |
u = plugin.url(privacy=False) |
| 570 |
|
| 571 |
assert "mode=none" in u |
| 572 |
assert "roster=yes" in u |
| 573 |
assert "subject=yes" in u |
| 574 |
|
| 575 |
|
| 576 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 577 |
def test_xmpp_parse_url_returns_none(monkeypatch: pytest.MonkeyPatch) -> None: |
| 578 |
"""parse_url() returns None when NotifyBase.parse_url() fails.""" |
| 579 |
|
| 580 |
monkeypatch.setattr( |
| 581 |
xmpp_base.NotifyBase, |
| 582 |
"parse_url", |
| 583 |
lambda *_args, **_kwargs: None, |
| 584 |
raising=True, |
| 585 |
) |
| 586 |
assert NotifyXMPP.parse_url("xmpp://bad") is None |
| 587 |
|
| 588 |
|
| 589 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 590 |
def test_xmpp_finalize_loop_when_already_closed_does_not_close_again() -> None: |
| 591 |
loop = asyncio.new_event_loop() |
| 592 |
loop.close() |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
xmpp_adapter.SlixmppAdapter._finalize_loop(loop) |
| 598 |
|
| 599 |
assert loop.is_closed() |
| 600 |
|
| 601 |
|
| 602 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 603 |
def test_xmpp_apprise_notify_invokes_adapter( |
| 604 |
monkeypatch: pytest.MonkeyPatch, |
| 605 |
) -> None: |
| 606 |
"""Exercise the Apprise.notify() workflow while mocking network I/O.""" |
| 607 |
|
| 608 |
captured: dict[str, Any] = {} |
| 609 |
|
| 610 |
class _Adapter: |
| 611 |
def __init__( |
| 612 |
self, |
| 613 |
config: xmpp_adapter.XMPPConfig, |
| 614 |
targets: list[str], |
| 615 |
subject: str, |
| 616 |
body: str, |
| 617 |
timeout: float = 0.0, |
| 618 |
roster: bool = False, |
| 619 |
before_message: Any = None, |
| 620 |
logger: Optional[logging.Logger] = None, |
| 621 |
**kwargs: Any, |
| 622 |
) -> None: |
| 623 |
captured["targets"] = targets |
| 624 |
captured["body"] = body |
| 625 |
captured["subject"] = subject |
| 626 |
captured["roster"] = roster |
| 627 |
|
| 628 |
def process(self) -> bool: |
| 629 |
return True |
| 630 |
|
| 631 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _Adapter, raising=True) |
| 632 |
|
| 633 |
apobj = Apprise() |
| 634 |
apobj.add("xmpp://me:[email protected]/[email protected]") |
| 635 |
|
| 636 |
assert ( |
| 637 |
apobj.notify("hello", title="subject", notify_type=NotifyType.INFO) |
| 638 |
is True |
| 639 |
) |
| 640 |
assert captured["targets"] == [("chat", "[email protected]")] |
| 641 |
assert captured["subject"] == "" |
| 642 |
assert "subject\r\nhello" in captured["body"] |
| 643 |
|
| 644 |
apobj = Apprise() |
| 645 |
apobj.add("xmpp://me:[email protected]/[email protected]?subject=yes") |
| 646 |
|
| 647 |
assert ( |
| 648 |
apobj.notify("hello", title="subject", notify_type=NotifyType.INFO) |
| 649 |
is True |
| 650 |
) |
| 651 |
assert captured["targets"] == [("chat", "[email protected]")] |
| 652 |
assert captured["subject"] == "subject" |
| 653 |
assert "hello" in captured["body"] |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 662 |
def test_xmpp_ssl_context() -> None: |
| 663 |
ctx = xmpp_adapter.SlixmppAdapter._ssl_context(True) |
| 664 |
assert ctx.verify_mode != ssl.CERT_NONE |
| 665 |
|
| 666 |
ctx2 = xmpp_adapter.SlixmppAdapter._ssl_context(False) |
| 667 |
assert ctx2.verify_mode == ssl.CERT_NONE |
| 668 |
assert ctx2.check_hostname is False |
| 669 |
|
| 670 |
|
| 671 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 672 |
def test_xmpp_cancel_pending_empty(monkeypatch: pytest.MonkeyPatch) -> None: |
| 673 |
loop = asyncio.new_event_loop() |
| 674 |
try: |
| 675 |
monkeypatch.setattr( |
| 676 |
asyncio, "all_tasks", lambda _: set(), raising=True |
| 677 |
) |
| 678 |
xmpp_adapter.SlixmppAdapter._finalize_loop(loop) |
| 679 |
|
| 680 |
finally: |
| 681 |
with contextlib.suppress(Exception): |
| 682 |
asyncio.set_event_loop(None) |
| 683 |
|
| 684 |
loop.close() |
| 685 |
|
| 686 |
|
| 687 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 688 |
def test_xmpp_cancel_pending_tasks(monkeypatch: pytest.MonkeyPatch) -> None: |
| 689 |
loop = asyncio.new_event_loop() |
| 690 |
try: |
| 691 |
asyncio.set_event_loop(loop) |
| 692 |
|
| 693 |
async def _noop() -> None: |
| 694 |
await asyncio.sleep(0) |
| 695 |
|
| 696 |
t1 = loop.create_task(_noop()) |
| 697 |
t2 = loop.create_task(_noop()) |
| 698 |
|
| 699 |
monkeypatch.setattr( |
| 700 |
asyncio, "all_tasks", lambda _: {t1, t2}, raising=True |
| 701 |
) |
| 702 |
|
| 703 |
xmpp_adapter.SlixmppAdapter._finalize_loop(loop) |
| 704 |
assert t1.cancelled() |
| 705 |
assert t2.cancelled() |
| 706 |
|
| 707 |
finally: |
| 708 |
|
| 709 |
|
| 710 |
with contextlib.suppress(Exception): |
| 711 |
asyncio.set_event_loop(None) |
| 712 |
|
| 713 |
loop.close() |
| 714 |
|
| 715 |
|
| 716 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 717 |
def test_xmpp_process_disabled(monkeypatch: pytest.MonkeyPatch) -> None: |
| 718 |
install_fake_slixmpp(monkeypatch) |
| 719 |
config = xmpp_adapter.XMPPConfig( |
| 720 |
jid="[email protected]", |
| 721 |
password="x", |
| 722 |
host="example.com", |
| 723 |
port=5223, |
| 724 |
secure=xmpp_adapter.SecureXMPPMode.TLS, |
| 725 |
verify_certificate=True, |
| 726 |
) |
| 727 |
a = xmpp_adapter.SlixmppAdapter( |
| 728 |
config=config, |
| 729 |
targets=[], |
| 730 |
subject="s", |
| 731 |
body="b", |
| 732 |
timeout=1, |
| 733 |
) |
| 734 |
monkeypatch.setattr(a, "_enabled", False, raising=True) |
| 735 |
assert a.process() is False |
| 736 |
|
| 737 |
|
| 738 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 739 |
def test_xmpp_process_fail( |
| 740 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 741 |
) -> None: |
| 742 |
install_fake_slixmpp(monkeypatch) |
| 743 |
caplog.set_level(logging.WARNING) |
| 744 |
|
| 745 |
def bad_connect(self: FakeClientXMPP, **kw: Any) -> asyncio.Future[bool]: |
| 746 |
fut: asyncio.Future[bool] = self.loop.create_future() |
| 747 |
fut.set_result(False) |
| 748 |
return fut |
| 749 |
|
| 750 |
monkeypatch.setattr(FakeClientXMPP, "connect", bad_connect, raising=True) |
| 751 |
|
| 752 |
config = xmpp_adapter.XMPPConfig( |
| 753 |
jid="[email protected]", |
| 754 |
password="x", |
| 755 |
host="example.com", |
| 756 |
port=5223, |
| 757 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 758 |
verify_certificate=True, |
| 759 |
) |
| 760 |
a = xmpp_adapter.SlixmppAdapter( |
| 761 |
config=config, |
| 762 |
targets=[("chat", "[email protected]")], |
| 763 |
subject="s", |
| 764 |
body="b", |
| 765 |
timeout=1, |
| 766 |
) |
| 767 |
assert a.process() is False |
| 768 |
|
| 769 |
|
| 770 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 771 |
def test_xmpp_process_success(monkeypatch: pytest.MonkeyPatch) -> None: |
| 772 |
install_fake_slixmpp(monkeypatch) |
| 773 |
config = xmpp_adapter.XMPPConfig( |
| 774 |
jid="[email protected]", |
| 775 |
password="x", |
| 776 |
host="example.com", |
| 777 |
port=5223, |
| 778 |
secure=xmpp_adapter.SecureXMPPMode.TLS, |
| 779 |
verify_certificate=False, |
| 780 |
) |
| 781 |
|
| 782 |
a = xmpp_adapter.SlixmppAdapter( |
| 783 |
config=config, |
| 784 |
targets=[("chat", "[email protected]")], |
| 785 |
subject="s", |
| 786 |
body="b", |
| 787 |
timeout=1.0, |
| 788 |
) |
| 789 |
assert a.process() is True |
| 790 |
|
| 791 |
|
| 792 |
a = xmpp_adapter.SlixmppAdapter( |
| 793 |
config=config, |
| 794 |
targets=[("chat", "[email protected]")], |
| 795 |
subject="s", |
| 796 |
body="b", |
| 797 |
timeout=1.0, |
| 798 |
roster=True, |
| 799 |
) |
| 800 |
assert a.process() is True |
| 801 |
|
| 802 |
|
| 803 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 804 |
def test_xmpp_process_default_target(monkeypatch: pytest.MonkeyPatch) -> None: |
| 805 |
install_fake_slixmpp(monkeypatch) |
| 806 |
config = xmpp_adapter.XMPPConfig( |
| 807 |
jid="[email protected]", |
| 808 |
password="x", |
| 809 |
host="example.com", |
| 810 |
port=5223, |
| 811 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 812 |
verify_certificate=False, |
| 813 |
) |
| 814 |
|
| 815 |
a = xmpp_adapter.SlixmppAdapter( |
| 816 |
config=config, |
| 817 |
targets=[], |
| 818 |
subject="s", |
| 819 |
body="b", |
| 820 |
timeout=1.0, |
| 821 |
) |
| 822 |
assert a.process() is True |
| 823 |
|
| 824 |
|
| 825 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 826 |
def test_xmpp_process_exception( |
| 827 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 828 |
) -> None: |
| 829 |
install_fake_slixmpp(monkeypatch) |
| 830 |
caplog.set_level(logging.DEBUG) |
| 831 |
|
| 832 |
def explode(self: FakeClientXMPP, **kw: Any) -> bool: |
| 833 |
raise RuntimeError("boom") |
| 834 |
|
| 835 |
monkeypatch.setattr(FakeClientXMPP, "connect", explode, raising=True) |
| 836 |
|
| 837 |
config = xmpp_adapter.XMPPConfig( |
| 838 |
jid="[email protected]", |
| 839 |
password="x", |
| 840 |
host="example.com", |
| 841 |
port=5223, |
| 842 |
secure=xmpp_adapter.SecureXMPPMode.TLS, |
| 843 |
verify_certificate=True, |
| 844 |
) |
| 845 |
a = xmpp_adapter.SlixmppAdapter( |
| 846 |
config=config, |
| 847 |
targets=[("chat", "[email protected]")], |
| 848 |
subject="s", |
| 849 |
body="b", |
| 850 |
timeout=1, |
| 851 |
) |
| 852 |
assert a.process() is False |
| 853 |
|
| 854 |
|
| 855 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 856 |
def test_xmpp_process_before_message(monkeypatch: pytest.MonkeyPatch) -> None: |
| 857 |
"""Ensure before_message callback is invoked for each target.""" |
| 858 |
install_fake_slixmpp(monkeypatch) |
| 859 |
|
| 860 |
called: list[int] = [] |
| 861 |
|
| 862 |
def before_message() -> None: |
| 863 |
called.append(1) |
| 864 |
|
| 865 |
config = xmpp_adapter.XMPPConfig( |
| 866 |
jid="[email protected]", |
| 867 |
password="x", |
| 868 |
host="example.com", |
| 869 |
port=5223, |
| 870 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 871 |
verify_certificate=False, |
| 872 |
) |
| 873 |
a = xmpp_adapter.SlixmppAdapter( |
| 874 |
config=config, |
| 875 |
targets=[("chat", "[email protected]"), ("chat", "[email protected]")], |
| 876 |
subject="s", |
| 877 |
body="b", |
| 878 |
timeout=1, |
| 879 |
before_message=before_message, |
| 880 |
) |
| 881 |
assert a.process() is True |
| 882 |
assert len(called) == 2 |
| 883 |
|
| 884 |
|
| 885 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 886 |
def test_xmpp_process_failed_auth_disconnect( |
| 887 |
monkeypatch: pytest.MonkeyPatch, |
| 888 |
) -> None: |
| 889 |
"""Authentication failure path must still call disconnect().""" |
| 890 |
install_fake_slixmpp(monkeypatch) |
| 891 |
|
| 892 |
|
| 893 |
def connect_failed_auth( |
| 894 |
self: FakeClientXMPP, **kw: Any |
| 895 |
) -> asyncio.Future[bool]: |
| 896 |
loop = self.loop or asyncio.get_running_loop() |
| 897 |
if self.disconnected is None: |
| 898 |
self.disconnected = loop.create_future() |
| 899 |
|
| 900 |
fut: asyncio.Future[bool] = loop.create_future() |
| 901 |
fut.set_result(True) |
| 902 |
|
| 903 |
handler = self.handlers.get("failed_auth") |
| 904 |
if handler: |
| 905 |
loop.call_soon(handler) |
| 906 |
loop.call_soon(self.disconnect) |
| 907 |
|
| 908 |
return fut |
| 909 |
|
| 910 |
monkeypatch.setattr( |
| 911 |
FakeClientXMPP, "connect", connect_failed_auth, raising=True |
| 912 |
) |
| 913 |
|
| 914 |
disconnected = {"called": False} |
| 915 |
|
| 916 |
def disconnect(self: FakeClientXMPP) -> None: |
| 917 |
disconnected["called"] = True |
| 918 |
if not self.disconnected.done(): |
| 919 |
self.disconnected.set_result(True) |
| 920 |
|
| 921 |
monkeypatch.setattr(FakeClientXMPP, "disconnect", disconnect, raising=True) |
| 922 |
|
| 923 |
config = xmpp_adapter.XMPPConfig( |
| 924 |
jid="[email protected]", |
| 925 |
password="x", |
| 926 |
host="example.com", |
| 927 |
port=5223, |
| 928 |
secure=xmpp_adapter.SecureXMPPMode.TLS, |
| 929 |
verify_certificate=False, |
| 930 |
) |
| 931 |
a = xmpp_adapter.SlixmppAdapter( |
| 932 |
config=config, |
| 933 |
targets=[("chat", "[email protected]")], |
| 934 |
subject="s", |
| 935 |
body="b", |
| 936 |
timeout=1, |
| 937 |
) |
| 938 |
assert a.process() is False |
| 939 |
assert disconnected["called"] is True |
| 940 |
|
| 941 |
|
| 942 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 943 |
def test_xmpp_normalize_jid() -> None: |
| 944 |
"""Tests normalize_jid()""" |
| 945 |
assert NotifyXMPP.normalize_jid("user", "example.ca") == ( |
| 946 |
"[email protected]", |
| 947 |
False, |
| 948 |
) |
| 949 |
assert NotifyXMPP.normalize_jid("user/resource", "example.ca") == ( |
| 950 |
"[email protected]/resource", |
| 951 |
False, |
| 952 |
) |
| 953 |
assert NotifyXMPP.normalize_jid( |
| 954 |
"user/resource/extra/crap", "example.ca" |
| 955 |
) == ("[email protected]/resource", False) |
| 956 |
assert NotifyXMPP.normalize_jid("[email protected]/r1", "example.ca") == ( |
| 957 |
"[email protected]/r1", |
| 958 |
False, |
| 959 |
) |
| 960 |
assert NotifyXMPP.normalize_jid( |
| 961 |
"#[email protected]", "example.ca" |
| 962 |
) == ("[email protected]", True) |
| 963 |
|
| 964 |
with pytest.raises(ValueError): |
| 965 |
|
| 966 |
NotifyXMPP.normalize_jid("", "example.ca") |
| 967 |
|
| 968 |
|
| 969 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 970 |
def test_xmpp_bridge_logging_safe_lock( |
| 971 |
monkeypatch: pytest.MonkeyPatch, |
| 972 |
) -> None: |
| 973 |
"""tests bridge logging on multiple attempts""" |
| 974 |
|
| 975 |
monkeypatch.setattr(xmpp_adapter, "_LOG_BRIDGED", False, raising=True) |
| 976 |
|
| 977 |
|
| 978 |
xmpp_adapter._LOG_BRIDGE_LOCK.acquire() |
| 979 |
|
| 980 |
done = {"ok": False} |
| 981 |
|
| 982 |
def worker() -> None: |
| 983 |
xmpp_adapter.bridge_slixmpp_logging() |
| 984 |
done["ok"] = True |
| 985 |
|
| 986 |
t = threading.Thread(target=worker, daemon=True) |
| 987 |
t.start() |
| 988 |
|
| 989 |
|
| 990 |
time.sleep(0.02) |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
monkeypatch.setattr(xmpp_adapter, "_LOG_BRIDGED", True, raising=True) |
| 995 |
|
| 996 |
xmpp_adapter._LOG_BRIDGE_LOCK.release() |
| 997 |
t.join(timeout=1.0) |
| 998 |
|
| 999 |
assert done["ok"] is True |
| 1000 |
|
| 1001 |
|
| 1002 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1003 |
def test_xmpp_multi_logging_bridge_handling( |
| 1004 |
monkeypatch: pytest.MonkeyPatch, |
| 1005 |
) -> None: |
| 1006 |
"""Handles multiple logging attempts made by multiple xmpp instances""" |
| 1007 |
|
| 1008 |
|
| 1009 |
monkeypatch.setattr(xmpp_adapter, "_LOG_BRIDGED", False, raising=True) |
| 1010 |
|
| 1011 |
apprise_logger = logging.getLogger("apprise") |
| 1012 |
slix_logger = logging.getLogger("slixmpp") |
| 1013 |
|
| 1014 |
|
| 1015 |
old_apprise = list(apprise_logger.handlers) |
| 1016 |
old_slix = list(slix_logger.handlers) |
| 1017 |
|
| 1018 |
try: |
| 1019 |
handler = logging.NullHandler() |
| 1020 |
|
| 1021 |
|
| 1022 |
apprise_logger.handlers = [handler] |
| 1023 |
slix_logger.handlers = [handler] |
| 1024 |
|
| 1025 |
xmpp_adapter.bridge_slixmpp_logging() |
| 1026 |
|
| 1027 |
|
| 1028 |
assert slix_logger.handlers.count(handler) == 1 |
| 1029 |
|
| 1030 |
finally: |
| 1031 |
apprise_logger.handlers = old_apprise |
| 1032 |
slix_logger.handlers = old_slix |
| 1033 |
|
| 1034 |
|
| 1035 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1036 |
def test_xmpp_process_failure_cleanup(monkeypatch: pytest.MonkeyPatch) -> None: |
| 1037 |
"""Tests edge cases for failures that require cleanup""" |
| 1038 |
|
| 1039 |
install_fake_slixmpp(monkeypatch) |
| 1040 |
|
| 1041 |
|
| 1042 |
monkeypatch.setattr( |
| 1043 |
xmpp_adapter.SlixmppAdapter, "_enabled", True, raising=True |
| 1044 |
) |
| 1045 |
|
| 1046 |
|
| 1047 |
def raise_new_event_loop() -> Any: |
| 1048 |
raise RuntimeError("poof") |
| 1049 |
|
| 1050 |
monkeypatch.setattr( |
| 1051 |
xmpp_adapter.asyncio, |
| 1052 |
"new_event_loop", |
| 1053 |
raise_new_event_loop, |
| 1054 |
raising=True, |
| 1055 |
) |
| 1056 |
|
| 1057 |
config = xmpp_adapter.XMPPConfig( |
| 1058 |
jid="[email protected]", |
| 1059 |
password="x", |
| 1060 |
host="example.com", |
| 1061 |
port=5223, |
| 1062 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1063 |
verify_certificate=False, |
| 1064 |
) |
| 1065 |
|
| 1066 |
a = xmpp_adapter.SlixmppAdapter( |
| 1067 |
config=config, |
| 1068 |
targets=[("chat", "[email protected]")], |
| 1069 |
subject="s", |
| 1070 |
body="b", |
| 1071 |
timeout=5.0, |
| 1072 |
) |
| 1073 |
|
| 1074 |
|
| 1075 |
assert a.process() is False |
| 1076 |
|
| 1077 |
|
| 1078 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1079 |
def test_xmpp_timeout_cleanup_disconnect_exception_suppressed( |
| 1080 |
monkeypatch: pytest.MonkeyPatch, |
| 1081 |
) -> None: |
| 1082 |
"""Cover timeout cleanup: loop_obj != None""" |
| 1083 |
install_fake_slixmpp(monkeypatch) |
| 1084 |
|
| 1085 |
|
| 1086 |
monkeypatch.setattr( |
| 1087 |
xmpp_adapter.SlixmppAdapter, "_enabled", True, raising=True |
| 1088 |
) |
| 1089 |
|
| 1090 |
client_created = threading.Event() |
| 1091 |
|
| 1092 |
|
| 1093 |
orig_init = FakeClientXMPP.__init__ |
| 1094 |
|
| 1095 |
def init_signal( |
| 1096 |
self: FakeClientXMPP, |
| 1097 |
jid: str, |
| 1098 |
password: str, |
| 1099 |
*args: Any, |
| 1100 |
**kwargs: Any, |
| 1101 |
) -> None: |
| 1102 |
orig_init(self, jid, password, *args, **kwargs) |
| 1103 |
client_created.set() |
| 1104 |
|
| 1105 |
monkeypatch.setattr(FakeClientXMPP, "__init__", init_signal, raising=True) |
| 1106 |
|
| 1107 |
|
| 1108 |
def connect_blocks( |
| 1109 |
self: FakeClientXMPP, **kw: Any |
| 1110 |
) -> asyncio.Future[bool]: |
| 1111 |
fut: asyncio.Future[bool] = self.loop.create_future() |
| 1112 |
|
| 1113 |
return fut |
| 1114 |
|
| 1115 |
monkeypatch.setattr( |
| 1116 |
FakeClientXMPP, "connect", connect_blocks, raising=True |
| 1117 |
) |
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
_patch_threading( |
| 1122 |
monkeypatch, |
| 1123 |
done_event_cls=_make_fake_done_event_cls(client_created), |
| 1124 |
) |
| 1125 |
|
| 1126 |
|
| 1127 |
import asyncio as _real_asyncio |
| 1128 |
|
| 1129 |
orig_new_event_loop = _real_asyncio.new_event_loop |
| 1130 |
|
| 1131 |
calls: list[str] = [] |
| 1132 |
|
| 1133 |
def new_event_loop_wrapped() -> _real_asyncio.AbstractEventLoop: |
| 1134 |
loop = orig_new_event_loop() |
| 1135 |
orig_csts = loop.call_soon_threadsafe |
| 1136 |
|
| 1137 |
def csts(cb: Any, *a: Any) -> Any: |
| 1138 |
|
| 1139 |
name = getattr(cb, "__name__", repr(cb)) |
| 1140 |
calls.append(name) |
| 1141 |
|
| 1142 |
|
| 1143 |
if name == "disconnect": |
| 1144 |
raise RuntimeError("boom") |
| 1145 |
|
| 1146 |
return orig_csts(cb, *a) |
| 1147 |
|
| 1148 |
monkeypatch.setattr(loop, "call_soon_threadsafe", csts, raising=True) |
| 1149 |
return loop |
| 1150 |
|
| 1151 |
monkeypatch.setattr( |
| 1152 |
xmpp_adapter.asyncio, |
| 1153 |
"new_event_loop", |
| 1154 |
new_event_loop_wrapped, |
| 1155 |
raising=True, |
| 1156 |
) |
| 1157 |
|
| 1158 |
config = xmpp_adapter.XMPPConfig( |
| 1159 |
jid="[email protected]", |
| 1160 |
password="x", |
| 1161 |
host="example.com", |
| 1162 |
port=5223, |
| 1163 |
secure=xmpp_adapter.SecureXMPPMode.TLS, |
| 1164 |
verify_certificate=False, |
| 1165 |
) |
| 1166 |
|
| 1167 |
a = xmpp_adapter.SlixmppAdapter( |
| 1168 |
config=config, |
| 1169 |
targets=[("chat", "[email protected]")], |
| 1170 |
subject="s", |
| 1171 |
body="b", |
| 1172 |
timeout=5.0, |
| 1173 |
) |
| 1174 |
|
| 1175 |
assert a.process() is False |
| 1176 |
|
| 1177 |
|
| 1178 |
assert "disconnect" in calls |
| 1179 |
assert "stop" in calls |
| 1180 |
|
| 1181 |
|
| 1182 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1183 |
def test_xmpp_timeout_cleanup_no_client_stop_exception_suppressed( |
| 1184 |
monkeypatch: pytest.MonkeyPatch, |
| 1185 |
) -> None: |
| 1186 |
"""Cover timeout cleanup: loop_obj != None""" |
| 1187 |
install_fake_slixmpp(monkeypatch) |
| 1188 |
|
| 1189 |
monkeypatch.setattr( |
| 1190 |
xmpp_adapter.SlixmppAdapter, "_enabled", True, raising=True |
| 1191 |
) |
| 1192 |
|
| 1193 |
loop_created = threading.Event() |
| 1194 |
allow_client_create = threading.Event() |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
orig_init = FakeClientXMPP.__init__ |
| 1199 |
|
| 1200 |
def init_block( |
| 1201 |
self: FakeClientXMPP, |
| 1202 |
jid: str, |
| 1203 |
password: str, |
| 1204 |
*args: Any, |
| 1205 |
**kwargs: Any, |
| 1206 |
) -> None: |
| 1207 |
|
| 1208 |
allow_client_create.wait(timeout=1.0) |
| 1209 |
orig_init(self, jid, password, *args, **kwargs) |
| 1210 |
|
| 1211 |
monkeypatch.setattr(FakeClientXMPP, "__init__", init_block, raising=True) |
| 1212 |
|
| 1213 |
|
| 1214 |
_patch_threading( |
| 1215 |
monkeypatch, |
| 1216 |
done_event_cls=_make_fake_done_event_cls(loop_created), |
| 1217 |
) |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
import asyncio as _real_asyncio |
| 1222 |
|
| 1223 |
orig_new_event_loop = _real_asyncio.new_event_loop |
| 1224 |
|
| 1225 |
calls: list[str] = [] |
| 1226 |
|
| 1227 |
def new_event_loop_wrapped() -> _real_asyncio.AbstractEventLoop: |
| 1228 |
loop = orig_new_event_loop() |
| 1229 |
loop_created.set() |
| 1230 |
|
| 1231 |
def csts(cb: Any, *a: Any) -> Any: |
| 1232 |
name = getattr(cb, "__name__", repr(cb)) |
| 1233 |
calls.append(name) |
| 1234 |
if name == "stop": |
| 1235 |
raise RuntimeError("boom-stop") |
| 1236 |
return None |
| 1237 |
|
| 1238 |
monkeypatch.setattr(loop, "call_soon_threadsafe", csts, raising=True) |
| 1239 |
return loop |
| 1240 |
|
| 1241 |
monkeypatch.setattr( |
| 1242 |
xmpp_adapter.asyncio, |
| 1243 |
"new_event_loop", |
| 1244 |
new_event_loop_wrapped, |
| 1245 |
raising=True, |
| 1246 |
) |
| 1247 |
|
| 1248 |
config = xmpp_adapter.XMPPConfig( |
| 1249 |
jid="[email protected]", |
| 1250 |
password="x", |
| 1251 |
host="example.com", |
| 1252 |
port=5223, |
| 1253 |
secure=xmpp_adapter.SecureXMPPMode.NONE, |
| 1254 |
verify_certificate=False, |
| 1255 |
) |
| 1256 |
|
| 1257 |
a = xmpp_adapter.SlixmppAdapter( |
| 1258 |
config=config, |
| 1259 |
targets=[("chat", "[email protected]")], |
| 1260 |
subject="s", |
| 1261 |
body="b", |
| 1262 |
timeout=5.0, |
| 1263 |
) |
| 1264 |
|
| 1265 |
try: |
| 1266 |
assert a.process() is False |
| 1267 |
|
| 1268 |
assert "disconnect" not in calls |
| 1269 |
|
| 1270 |
assert "stop" in calls |
| 1271 |
finally: |
| 1272 |
|
| 1273 |
allow_client_create.set() |
| 1274 |
|
| 1275 |
|
| 1276 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1277 |
def test_xmpp_timeout_cleanup_loop_none_skips_disconnect_and_stop( |
| 1278 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 1279 |
) -> None: |
| 1280 |
"""Cover timeout clean-up branch where shared['loop'] is still None.""" |
| 1281 |
install_fake_slixmpp(monkeypatch) |
| 1282 |
|
| 1283 |
caplog.set_level(logging.WARNING, logger="apprise.xmpp") |
| 1284 |
|
| 1285 |
|
| 1286 |
monkeypatch.setattr( |
| 1287 |
xmpp_adapter.SlixmppAdapter, "_enabled", True, raising=True |
| 1288 |
) |
| 1289 |
|
| 1290 |
|
| 1291 |
gate = threading.Event() |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
worker_entered_gate = threading.Event() |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
import asyncio as _real_asyncio |
| 1303 |
|
| 1304 |
orig_new_event_loop = _real_asyncio.new_event_loop |
| 1305 |
|
| 1306 |
def new_event_loop_blocking() -> _real_asyncio.AbstractEventLoop: |
| 1307 |
|
| 1308 |
|
| 1309 |
worker_entered_gate.set() |
| 1310 |
gate.wait(timeout=2.0) |
| 1311 |
return orig_new_event_loop() |
| 1312 |
|
| 1313 |
monkeypatch.setattr( |
| 1314 |
xmpp_adapter.asyncio, |
| 1315 |
"new_event_loop", |
| 1316 |
new_event_loop_blocking, |
| 1317 |
raising=True, |
| 1318 |
) |
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
_patch_threading( |
| 1324 |
monkeypatch, |
| 1325 |
done_event_cls=_make_fake_done_event_cls(worker_entered_gate), |
| 1326 |
) |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
called = {"disconnect": 0, "stop": 0} |
| 1331 |
|
| 1332 |
|
| 1333 |
def connect_blocks( |
| 1334 |
self: FakeClientXMPP, **kw: Any |
| 1335 |
) -> asyncio.Future[bool]: |
| 1336 |
fut: asyncio.Future[bool] = self.loop.create_future() |
| 1337 |
|
| 1338 |
return fut |
| 1339 |
|
| 1340 |
monkeypatch.setattr( |
| 1341 |
FakeClientXMPP, "connect", connect_blocks, raising=True |
| 1342 |
) |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
def fake_disconnect(self: FakeClientXMPP) -> None: |
| 1347 |
called["disconnect"] += 1 |
| 1348 |
|
| 1349 |
monkeypatch.setattr( |
| 1350 |
FakeClientXMPP, "disconnect", fake_disconnect, raising=True |
| 1351 |
) |
| 1352 |
|
| 1353 |
config = xmpp_adapter.XMPPConfig( |
| 1354 |
jid="[email protected]", |
| 1355 |
password="x", |
| 1356 |
host="example.com", |
| 1357 |
port=5223, |
| 1358 |
secure=xmpp_adapter.SecureXMPPMode.TLS, |
| 1359 |
verify_certificate=False, |
| 1360 |
) |
| 1361 |
|
| 1362 |
a = xmpp_adapter.SlixmppAdapter( |
| 1363 |
config=config, |
| 1364 |
targets=[("chat", "[email protected]")], |
| 1365 |
subject="s", |
| 1366 |
body="b", |
| 1367 |
timeout=5.0, |
| 1368 |
) |
| 1369 |
|
| 1370 |
try: |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
assert a.process() is False |
| 1375 |
assert "XMPP send timed out" in caplog.text |
| 1376 |
|
| 1377 |
|
| 1378 |
assert called["disconnect"] == 0 |
| 1379 |
|
| 1380 |
finally: |
| 1381 |
|
| 1382 |
gate.set() |
| 1383 |
|
| 1384 |
|
| 1385 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1386 |
def test_xmpp_process_unsupported_secure_mode( |
| 1387 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 1388 |
) -> None: |
| 1389 |
"""Cover unsupported secure mode path (ValueError inside runner).""" |
| 1390 |
install_fake_slixmpp(monkeypatch) |
| 1391 |
caplog.set_level(logging.WARNING, logger="apprise.xmpp") |
| 1392 |
|
| 1393 |
config = xmpp_adapter.XMPPConfig( |
| 1394 |
jid="[email protected]", |
| 1395 |
password="x", |
| 1396 |
host="example.com", |
| 1397 |
port=5222, |
| 1398 |
secure="invalid", |
| 1399 |
verify_certificate=False, |
| 1400 |
) |
| 1401 |
|
| 1402 |
a = xmpp_adapter.SlixmppAdapter( |
| 1403 |
config=config, |
| 1404 |
targets=[("chat", "[email protected]")], |
| 1405 |
subject="s", |
| 1406 |
body="b", |
| 1407 |
timeout=5.0, |
| 1408 |
) |
| 1409 |
|
| 1410 |
assert a.process() is False |
| 1411 |
assert "XMPP send failed" in caplog.text |
| 1412 |
|
| 1413 |
|
| 1414 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1415 |
def test_xmpp_process_connect_timeout( |
| 1416 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 1417 |
) -> None: |
| 1418 |
"""Cover connect timeout branch (asyncio.TimeoutError).""" |
| 1419 |
install_fake_slixmpp(monkeypatch) |
| 1420 |
caplog.set_level(logging.WARNING, logger="apprise.xmpp") |
| 1421 |
|
| 1422 |
|
| 1423 |
connect_future: dict[str, Any] = {"fut": None} |
| 1424 |
|
| 1425 |
def connect_never(self: FakeClientXMPP, **kw: Any) -> asyncio.Future[bool]: |
| 1426 |
fut: asyncio.Future[bool] = self.loop.create_future() |
| 1427 |
connect_future["fut"] = fut |
| 1428 |
return fut |
| 1429 |
|
| 1430 |
monkeypatch.setattr(FakeClientXMPP, "connect", connect_never, raising=True) |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
real_wait_for = xmpp_adapter.asyncio.wait_for |
| 1435 |
|
| 1436 |
async def wait_for_patched( |
| 1437 |
aw: Any, timeout: Optional[float] = None |
| 1438 |
) -> Any: |
| 1439 |
if aw is connect_future["fut"]: |
| 1440 |
raise asyncio.TimeoutError() |
| 1441 |
return await real_wait_for(aw, timeout=timeout) |
| 1442 |
|
| 1443 |
monkeypatch.setattr( |
| 1444 |
xmpp_adapter.asyncio, "wait_for", wait_for_patched, raising=True |
| 1445 |
) |
| 1446 |
|
| 1447 |
config = xmpp_adapter.XMPPConfig( |
| 1448 |
jid="[email protected]", |
| 1449 |
password="x", |
| 1450 |
host="example.com", |
| 1451 |
port=5222, |
| 1452 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1453 |
verify_certificate=False, |
| 1454 |
) |
| 1455 |
|
| 1456 |
a = xmpp_adapter.SlixmppAdapter( |
| 1457 |
config=config, |
| 1458 |
targets=[("chat", "[email protected]")], |
| 1459 |
subject="s", |
| 1460 |
body="b", |
| 1461 |
timeout=5.0, |
| 1462 |
) |
| 1463 |
|
| 1464 |
assert a.process() is False |
| 1465 |
assert "XMPP connect timed out" in caplog.text |
| 1466 |
|
| 1467 |
|
| 1468 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1469 |
def test_xmpp_process_session_timeout( |
| 1470 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 1471 |
) -> None: |
| 1472 |
"""Cover session timeout branch when waiting on client.disconnected.""" |
| 1473 |
install_fake_slixmpp(monkeypatch) |
| 1474 |
caplog.set_level(logging.WARNING, logger="apprise.xmpp") |
| 1475 |
|
| 1476 |
|
| 1477 |
def disconnect_no_complete(self: FakeClientXMPP) -> None: |
| 1478 |
return None |
| 1479 |
|
| 1480 |
monkeypatch.setattr( |
| 1481 |
FakeClientXMPP, "disconnect", disconnect_no_complete, raising=True |
| 1482 |
) |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
real_connect = FakeClientXMPP.connect |
| 1488 |
disconnected_future: dict[str, Any] = {"fut": None} |
| 1489 |
|
| 1490 |
def connect_capture(self: FakeClientXMPP, **kwargs: Any) -> Any: |
| 1491 |
fut = real_connect(self, **kwargs) |
| 1492 |
disconnected_future["fut"] = self.disconnected |
| 1493 |
return fut |
| 1494 |
|
| 1495 |
monkeypatch.setattr( |
| 1496 |
FakeClientXMPP, "connect", connect_capture, raising=True |
| 1497 |
) |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
real_wait_for = xmpp_adapter.asyncio.wait_for |
| 1505 |
|
| 1506 |
def wait_for_patched(aw: Any, timeout: Optional[float] = None) -> Any: |
| 1507 |
if aw is disconnected_future["fut"]: |
| 1508 |
|
| 1509 |
with contextlib.suppress(Exception): |
| 1510 |
if hasattr(aw, "close"): |
| 1511 |
aw.close() |
| 1512 |
raise asyncio.TimeoutError() |
| 1513 |
return real_wait_for(aw, timeout=timeout) |
| 1514 |
|
| 1515 |
monkeypatch.setattr( |
| 1516 |
xmpp_adapter.asyncio, "wait_for", wait_for_patched, raising=True |
| 1517 |
) |
| 1518 |
|
| 1519 |
config = xmpp_adapter.XMPPConfig( |
| 1520 |
jid="[email protected]", |
| 1521 |
password="x", |
| 1522 |
host="example.com", |
| 1523 |
port=5222, |
| 1524 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1525 |
verify_certificate=False, |
| 1526 |
) |
| 1527 |
|
| 1528 |
a = xmpp_adapter.SlixmppAdapter( |
| 1529 |
config=config, |
| 1530 |
targets=[("chat", "[email protected]")], |
| 1531 |
subject="s", |
| 1532 |
body="b", |
| 1533 |
timeout=5.0, |
| 1534 |
) |
| 1535 |
|
| 1536 |
assert a.process() is False |
| 1537 |
assert "XMPP session timed out" in caplog.text |
| 1538 |
|
| 1539 |
|
| 1540 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1541 |
def test_xmpp_slixmpp_versioning(monkeypatch: pytest.MonkeyPatch) -> None: |
| 1542 |
""" |
| 1543 |
Simple checks to verify versioning works correctly |
| 1544 |
""" |
| 1545 |
|
| 1546 |
assert xmpp_adapter.SlixmppAdapter.supported_version("garbage") is False |
| 1547 |
|
| 1548 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.0.0") is False |
| 1549 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.10.0") is False |
| 1550 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.14.1") is False |
| 1551 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.15.0") is False |
| 1552 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.15.1") is False |
| 1553 |
|
| 1554 |
|
| 1555 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.16.0") is True |
| 1556 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.16") is True |
| 1557 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.16.1") is True |
| 1558 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.16.100") is True |
| 1559 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1.17.0") is True |
| 1560 |
|
| 1561 |
|
| 1562 |
assert xmpp_adapter.SlixmppAdapter.supported_version("1") is False |
| 1563 |
|
| 1564 |
|
| 1565 |
assert xmpp_adapter.SlixmppAdapter.supported_version("3") is True |
| 1566 |
assert xmpp_adapter.SlixmppAdapter.supported_version("3.2") is True |
| 1567 |
|
| 1568 |
monkeypatch.setattr( |
| 1569 |
xmpp_adapter, "SLIXMPP_SUPPORT_AVAILABLE", False, raising=False |
| 1570 |
) |
| 1571 |
monkeypatch.setattr( |
| 1572 |
xmpp_adapter.SlixmppAdapter, |
| 1573 |
"_enabled", |
| 1574 |
False, |
| 1575 |
raising=False, |
| 1576 |
) |
| 1577 |
assert xmpp_adapter.SlixmppAdapter.supported_version("3.2") is False |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1586 |
def test_xmpp_keepalive_url_and_parse() -> None: |
| 1587 |
n = NotifyXMPP( |
| 1588 |
host="example.com", |
| 1589 |
user="[email protected]", |
| 1590 |
password="x", |
| 1591 |
targets=["[email protected]"], |
| 1592 |
keepalive=True, |
| 1593 |
) |
| 1594 |
u = n.url(privacy=False) |
| 1595 |
assert "keepalive=yes" in u |
| 1596 |
|
| 1597 |
r = NotifyXMPP.parse_url( |
| 1598 |
"xmpp://me:[email protected]/[email protected]?keepalive=yes" |
| 1599 |
) |
| 1600 |
assert r["keepalive"] is True |
| 1601 |
|
| 1602 |
|
| 1603 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1604 |
def test_xmpp_keepalive_reuses_adapter_instance( |
| 1605 |
monkeypatch: pytest.MonkeyPatch, |
| 1606 |
) -> None: |
| 1607 |
created = {"count": 0} |
| 1608 |
calls = {"send_message": 0, "process": 0} |
| 1609 |
|
| 1610 |
class _Adapter: |
| 1611 |
def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 1612 |
created["count"] += 1 |
| 1613 |
self.keepalive = kwargs.get("keepalive", False) |
| 1614 |
|
| 1615 |
def process(self) -> bool: |
| 1616 |
calls["process"] += 1 |
| 1617 |
return True |
| 1618 |
|
| 1619 |
def send_message(self, **kwargs: Any) -> bool: |
| 1620 |
calls["send_message"] += 1 |
| 1621 |
return True |
| 1622 |
|
| 1623 |
def close(self) -> None: |
| 1624 |
return None |
| 1625 |
|
| 1626 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _Adapter, raising=True) |
| 1627 |
|
| 1628 |
|
| 1629 |
n = NotifyXMPP( |
| 1630 |
host="example.com", |
| 1631 |
user="[email protected]", |
| 1632 |
password="x", |
| 1633 |
targets=["[email protected]"], |
| 1634 |
keepalive=True, |
| 1635 |
) |
| 1636 |
assert n.send("body1", title="t1") is True |
| 1637 |
assert n.send("body2", title="t2") is True |
| 1638 |
|
| 1639 |
assert created["count"] == 1 |
| 1640 |
assert calls["send_message"] == 2 |
| 1641 |
assert calls["process"] == 0 |
| 1642 |
|
| 1643 |
|
| 1644 |
created["count"] = 0 |
| 1645 |
calls["send_message"] = 0 |
| 1646 |
calls["process"] = 0 |
| 1647 |
|
| 1648 |
n = NotifyXMPP( |
| 1649 |
host="example.com", |
| 1650 |
user="[email protected]", |
| 1651 |
password="x", |
| 1652 |
targets=["[email protected]"], |
| 1653 |
keepalive=False, |
| 1654 |
) |
| 1655 |
assert n.send("body", title="t") is True |
| 1656 |
assert created["count"] == 1 |
| 1657 |
assert calls["process"] == 1 |
| 1658 |
assert calls["send_message"] == 0 |
| 1659 |
|
| 1660 |
|
| 1661 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1662 |
def test_adapter_send_message_keepalive_false_calls_process( |
| 1663 |
monkeypatch: pytest.MonkeyPatch, |
| 1664 |
) -> None: |
| 1665 |
install_fake_slixmpp(monkeypatch) |
| 1666 |
config = xmpp_adapter.XMPPConfig( |
| 1667 |
jid="[email protected]", |
| 1668 |
password="x", |
| 1669 |
host="example.com", |
| 1670 |
port=5222, |
| 1671 |
secure=xmpp_adapter.SecureXMPPMode.NONE, |
| 1672 |
verify_certificate=False, |
| 1673 |
) |
| 1674 |
a = xmpp_adapter.SlixmppAdapter( |
| 1675 |
config=config, |
| 1676 |
targets=[("chat", "[email protected]")], |
| 1677 |
subject="s", |
| 1678 |
body="b", |
| 1679 |
timeout=5.0, |
| 1680 |
keepalive=False, |
| 1681 |
) |
| 1682 |
|
| 1683 |
called = {"process": 0} |
| 1684 |
|
| 1685 |
def process() -> bool: |
| 1686 |
called["process"] += 1 |
| 1687 |
return True |
| 1688 |
|
| 1689 |
monkeypatch.setattr(a, "process", process, raising=True) |
| 1690 |
|
| 1691 |
assert a.send_message(targets=["x"], subject="y", body="z") is True |
| 1692 |
assert called["process"] == 1 |
| 1693 |
assert a.targets == ["x"] |
| 1694 |
assert a.subject == "y" |
| 1695 |
assert a.body == "z" |
| 1696 |
|
| 1697 |
|
| 1698 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1699 |
def test_adapter_ensure_keepalive_worker_edge_cases( |
| 1700 |
monkeypatch: pytest.MonkeyPatch, |
| 1701 |
) -> None: |
| 1702 |
install_fake_slixmpp(monkeypatch) |
| 1703 |
config = xmpp_adapter.XMPPConfig( |
| 1704 |
jid="[email protected]", |
| 1705 |
password="x", |
| 1706 |
host="example.com", |
| 1707 |
port=5222, |
| 1708 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1709 |
verify_certificate=False, |
| 1710 |
) |
| 1711 |
|
| 1712 |
|
| 1713 |
a = xmpp_adapter.SlixmppAdapter( |
| 1714 |
config=config, |
| 1715 |
targets=[], |
| 1716 |
subject="s", |
| 1717 |
body="b", |
| 1718 |
keepalive=False, |
| 1719 |
) |
| 1720 |
assert a._ensure_keepalive_worker() is False |
| 1721 |
|
| 1722 |
|
| 1723 |
a = xmpp_adapter.SlixmppAdapter( |
| 1724 |
config=config, |
| 1725 |
targets=[], |
| 1726 |
subject="s", |
| 1727 |
body="b", |
| 1728 |
keepalive=True, |
| 1729 |
) |
| 1730 |
a._closing = True |
| 1731 |
assert a._ensure_keepalive_worker() is False |
| 1732 |
|
| 1733 |
|
| 1734 |
a = xmpp_adapter.SlixmppAdapter( |
| 1735 |
config=config, |
| 1736 |
targets=[], |
| 1737 |
subject="s", |
| 1738 |
body="b", |
| 1739 |
keepalive=True, |
| 1740 |
) |
| 1741 |
monkeypatch.setattr(a, "_enabled", False, raising=True) |
| 1742 |
assert a._ensure_keepalive_worker() is False |
| 1743 |
|
| 1744 |
|
| 1745 |
a = xmpp_adapter.SlixmppAdapter( |
| 1746 |
config=config, |
| 1747 |
targets=[], |
| 1748 |
subject="s", |
| 1749 |
body="b", |
| 1750 |
keepalive=True, |
| 1751 |
) |
| 1752 |
|
| 1753 |
class _AliveThread: |
| 1754 |
def is_alive(self) -> bool: |
| 1755 |
return True |
| 1756 |
|
| 1757 |
a._thread = _AliveThread() |
| 1758 |
assert a._ensure_keepalive_worker() is True |
| 1759 |
|
| 1760 |
|
| 1761 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1762 |
def test_adapter_ensure_keepalive_worker_wait_timeout( |
| 1763 |
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 1764 |
) -> None: |
| 1765 |
install_fake_slixmpp(monkeypatch) |
| 1766 |
caplog.set_level(logging.WARNING, logger="apprise.xmpp") |
| 1767 |
|
| 1768 |
config = xmpp_adapter.XMPPConfig( |
| 1769 |
jid="[email protected]", |
| 1770 |
password="x", |
| 1771 |
host="example.com", |
| 1772 |
port=5222, |
| 1773 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1774 |
verify_certificate=False, |
| 1775 |
) |
| 1776 |
a = xmpp_adapter.SlixmppAdapter( |
| 1777 |
config=config, |
| 1778 |
targets=[], |
| 1779 |
subject="s", |
| 1780 |
body="b", |
| 1781 |
timeout=0.1, |
| 1782 |
keepalive=True, |
| 1783 |
) |
| 1784 |
|
| 1785 |
|
| 1786 |
monkeypatch.setattr(a, "_keepalive_runner", lambda: None, raising=True) |
| 1787 |
|
| 1788 |
|
| 1789 |
monkeypatch.setattr( |
| 1790 |
a._loop_ready, "wait", lambda timeout=None: False, raising=True |
| 1791 |
) |
| 1792 |
|
| 1793 |
assert a._ensure_keepalive_worker() is False |
| 1794 |
assert "keepalive worker failed to start" in caplog.text |
| 1795 |
|
| 1796 |
|
| 1797 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1798 |
def test_adapter_connect_if_required_early_returns( |
| 1799 |
monkeypatch: pytest.MonkeyPatch, |
| 1800 |
) -> None: |
| 1801 |
install_fake_slixmpp(monkeypatch) |
| 1802 |
config = xmpp_adapter.XMPPConfig( |
| 1803 |
jid="[email protected]", |
| 1804 |
password="x", |
| 1805 |
host="example.com", |
| 1806 |
port=5222, |
| 1807 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1808 |
verify_certificate=False, |
| 1809 |
) |
| 1810 |
a = xmpp_adapter.SlixmppAdapter( |
| 1811 |
config=config, targets=[], subject="s", body="b", keepalive=True |
| 1812 |
) |
| 1813 |
|
| 1814 |
assert asyncio.run(a._connect_if_required()) is False |
| 1815 |
|
| 1816 |
loop = asyncio.new_event_loop() |
| 1817 |
try: |
| 1818 |
asyncio.set_event_loop(loop) |
| 1819 |
a._loop = loop |
| 1820 |
|
| 1821 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 1822 |
a._client = SimpleNamespace() |
| 1823 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 1824 |
a._connect_lock = asyncio.Lock() |
| 1825 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 1826 |
a._session_started = asyncio.Event() |
| 1827 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 1828 |
|
| 1829 |
finally: |
| 1830 |
with contextlib.suppress(Exception): |
| 1831 |
asyncio.set_event_loop(None) |
| 1832 |
|
| 1833 |
loop.close() |
| 1834 |
|
| 1835 |
|
| 1836 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1837 |
def test_adapter_connect_if_required_already_connected( |
| 1838 |
monkeypatch: pytest.MonkeyPatch, |
| 1839 |
) -> None: |
| 1840 |
install_fake_slixmpp(monkeypatch) |
| 1841 |
config = xmpp_adapter.XMPPConfig( |
| 1842 |
jid="[email protected]", |
| 1843 |
password="x", |
| 1844 |
host="example.com", |
| 1845 |
port=5222, |
| 1846 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1847 |
verify_certificate=False, |
| 1848 |
) |
| 1849 |
a = xmpp_adapter.SlixmppAdapter( |
| 1850 |
config=config, targets=[], subject="s", body="b", keepalive=True |
| 1851 |
) |
| 1852 |
|
| 1853 |
loop = asyncio.new_event_loop() |
| 1854 |
try: |
| 1855 |
asyncio.set_event_loop(loop) |
| 1856 |
a._loop = loop |
| 1857 |
a._connect_lock = asyncio.Lock() |
| 1858 |
a._session_started = asyncio.Event() |
| 1859 |
a._session_started.set() |
| 1860 |
|
| 1861 |
called = {"connect": 0} |
| 1862 |
|
| 1863 |
class _Client: |
| 1864 |
def connect(self, **kwargs: Any) -> Any: |
| 1865 |
called["connect"] += 1 |
| 1866 |
fut = loop.create_future() |
| 1867 |
fut.set_result(True) |
| 1868 |
return fut |
| 1869 |
|
| 1870 |
a._client = _Client() |
| 1871 |
assert run_on_loop(loop, a._connect_if_required()) is True |
| 1872 |
assert called["connect"] == 0 |
| 1873 |
|
| 1874 |
finally: |
| 1875 |
with contextlib.suppress(Exception): |
| 1876 |
asyncio.set_event_loop(None) |
| 1877 |
|
| 1878 |
loop.close() |
| 1879 |
|
| 1880 |
|
| 1881 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1882 |
def test_adapter_connect_if_required_connect_timeout( |
| 1883 |
monkeypatch: pytest.MonkeyPatch, |
| 1884 |
) -> None: |
| 1885 |
install_fake_slixmpp(monkeypatch) |
| 1886 |
config = xmpp_adapter.XMPPConfig( |
| 1887 |
jid="[email protected]", |
| 1888 |
password="x", |
| 1889 |
host="example.com", |
| 1890 |
port=5222, |
| 1891 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1892 |
verify_certificate=False, |
| 1893 |
) |
| 1894 |
a = xmpp_adapter.SlixmppAdapter( |
| 1895 |
config=config, |
| 1896 |
targets=[], |
| 1897 |
subject="s", |
| 1898 |
body="b", |
| 1899 |
timeout=5.0, |
| 1900 |
keepalive=True, |
| 1901 |
) |
| 1902 |
|
| 1903 |
loop = asyncio.new_event_loop() |
| 1904 |
try: |
| 1905 |
asyncio.set_event_loop(loop) |
| 1906 |
a._loop = loop |
| 1907 |
a._connect_lock = asyncio.Lock() |
| 1908 |
a._session_started = asyncio.Event() |
| 1909 |
|
| 1910 |
fut = loop.create_future() |
| 1911 |
|
| 1912 |
class _Client: |
| 1913 |
def connect(self, **kwargs: Any) -> Any: |
| 1914 |
return fut |
| 1915 |
|
| 1916 |
a._client = _Client() |
| 1917 |
|
| 1918 |
real_wait_for = xmpp_adapter.asyncio.wait_for |
| 1919 |
|
| 1920 |
def wait_for_patched(aw: Any, timeout: Optional[float] = None) -> Any: |
| 1921 |
if aw is fut: |
| 1922 |
raise asyncio.TimeoutError() |
| 1923 |
return real_wait_for(aw, timeout=timeout) |
| 1924 |
|
| 1925 |
monkeypatch.setattr( |
| 1926 |
xmpp_adapter.asyncio, "wait_for", wait_for_patched, raising=True |
| 1927 |
) |
| 1928 |
|
| 1929 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 1930 |
|
| 1931 |
finally: |
| 1932 |
with contextlib.suppress(Exception): |
| 1933 |
asyncio.set_event_loop(None) |
| 1934 |
|
| 1935 |
loop.close() |
| 1936 |
|
| 1937 |
|
| 1938 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1939 |
def test_adapter_connect_if_required_connect_exception( |
| 1940 |
monkeypatch: pytest.MonkeyPatch, |
| 1941 |
) -> None: |
| 1942 |
install_fake_slixmpp(monkeypatch) |
| 1943 |
config = xmpp_adapter.XMPPConfig( |
| 1944 |
jid="[email protected]", |
| 1945 |
password="x", |
| 1946 |
host="example.com", |
| 1947 |
port=5222, |
| 1948 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1949 |
verify_certificate=False, |
| 1950 |
) |
| 1951 |
a = xmpp_adapter.SlixmppAdapter( |
| 1952 |
config=config, targets=[], subject="s", body="b", keepalive=True |
| 1953 |
) |
| 1954 |
|
| 1955 |
loop = asyncio.new_event_loop() |
| 1956 |
try: |
| 1957 |
asyncio.set_event_loop(loop) |
| 1958 |
a._loop = loop |
| 1959 |
a._connect_lock = asyncio.Lock() |
| 1960 |
a._session_started = asyncio.Event() |
| 1961 |
|
| 1962 |
class _Client: |
| 1963 |
def connect(self, **kwargs: Any) -> Any: |
| 1964 |
raise RuntimeError("boom") |
| 1965 |
|
| 1966 |
a._client = _Client() |
| 1967 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 1968 |
|
| 1969 |
finally: |
| 1970 |
with contextlib.suppress(Exception): |
| 1971 |
asyncio.set_event_loop(None) |
| 1972 |
|
| 1973 |
loop.close() |
| 1974 |
|
| 1975 |
|
| 1976 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 1977 |
def test_adapter_connect_if_required_session_start_timeout( |
| 1978 |
monkeypatch: pytest.MonkeyPatch, |
| 1979 |
) -> None: |
| 1980 |
install_fake_slixmpp(monkeypatch) |
| 1981 |
config = xmpp_adapter.XMPPConfig( |
| 1982 |
jid="[email protected]", |
| 1983 |
password="x", |
| 1984 |
host="example.com", |
| 1985 |
port=5222, |
| 1986 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 1987 |
verify_certificate=False, |
| 1988 |
) |
| 1989 |
a = xmpp_adapter.SlixmppAdapter( |
| 1990 |
config=config, |
| 1991 |
targets=[], |
| 1992 |
subject="s", |
| 1993 |
body="b", |
| 1994 |
timeout=5.0, |
| 1995 |
keepalive=True, |
| 1996 |
) |
| 1997 |
|
| 1998 |
loop = asyncio.new_event_loop() |
| 1999 |
try: |
| 2000 |
asyncio.set_event_loop(loop) |
| 2001 |
a._loop = loop |
| 2002 |
a._connect_lock = asyncio.Lock() |
| 2003 |
a._session_started = asyncio.Event() |
| 2004 |
|
| 2005 |
fut = loop.create_future() |
| 2006 |
fut.set_result(True) |
| 2007 |
|
| 2008 |
class _Client: |
| 2009 |
def connect(self, **kwargs: Any) -> Any: |
| 2010 |
return fut |
| 2011 |
|
| 2012 |
a._client = _Client() |
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
real_wait_for = xmpp_adapter.asyncio.wait_for |
| 2017 |
calls = {"n": 0} |
| 2018 |
|
| 2019 |
async def wait_for_patched( |
| 2020 |
aw: Any, timeout: Optional[float] = None |
| 2021 |
) -> Any: |
| 2022 |
calls["n"] += 1 |
| 2023 |
if calls["n"] == 2: |
| 2024 |
|
| 2025 |
with contextlib.suppress(Exception): |
| 2026 |
if hasattr(aw, "close"): |
| 2027 |
aw.close() |
| 2028 |
|
| 2029 |
raise asyncio.TimeoutError() |
| 2030 |
return await real_wait_for(aw, timeout=timeout) |
| 2031 |
|
| 2032 |
monkeypatch.setattr( |
| 2033 |
xmpp_adapter.asyncio, "wait_for", wait_for_patched, raising=True |
| 2034 |
) |
| 2035 |
|
| 2036 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 2037 |
|
| 2038 |
finally: |
| 2039 |
with contextlib.suppress(Exception): |
| 2040 |
asyncio.set_event_loop(None) |
| 2041 |
loop.close() |
| 2042 |
|
| 2043 |
|
| 2044 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2045 |
def test_adapter_send_keepalive_async_default_target_and_send_error( |
| 2046 |
monkeypatch: pytest.MonkeyPatch, |
| 2047 |
) -> None: |
| 2048 |
install_fake_slixmpp(monkeypatch) |
| 2049 |
config = xmpp_adapter.XMPPConfig( |
| 2050 |
jid="[email protected]", |
| 2051 |
password="x", |
| 2052 |
host="example.com", |
| 2053 |
port=5222, |
| 2054 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2055 |
verify_certificate=False, |
| 2056 |
) |
| 2057 |
a = xmpp_adapter.SlixmppAdapter( |
| 2058 |
config=config, targets=[], subject="s", body="b", keepalive=True |
| 2059 |
) |
| 2060 |
|
| 2061 |
loop = asyncio.new_event_loop() |
| 2062 |
try: |
| 2063 |
asyncio.set_event_loop(loop) |
| 2064 |
a._loop = loop |
| 2065 |
a._connect_lock = asyncio.Lock() |
| 2066 |
a._session_started = asyncio.Event() |
| 2067 |
a._session_started.set() |
| 2068 |
|
| 2069 |
sent: list[str] = [] |
| 2070 |
|
| 2071 |
class _Client: |
| 2072 |
def send_message(self, **kwargs: Any) -> None: |
| 2073 |
sent.append(kwargs.get("mto")) |
| 2074 |
|
| 2075 |
a._client = _Client() |
| 2076 |
|
| 2077 |
async def ok_connect() -> bool: |
| 2078 |
return True |
| 2079 |
|
| 2080 |
monkeypatch.setattr( |
| 2081 |
a, "_connect_if_required", ok_connect, raising=True |
| 2082 |
) |
| 2083 |
|
| 2084 |
assert ( |
| 2085 |
run_on_loop( |
| 2086 |
loop, |
| 2087 |
a._send_keepalive_async(targets=[], subject="s", body="b"), |
| 2088 |
) |
| 2089 |
is True |
| 2090 |
) |
| 2091 |
assert sent == ["[email protected]"] |
| 2092 |
|
| 2093 |
|
| 2094 |
def boom_send_message(self: _Client, **kwargs: Any) -> None: |
| 2095 |
raise RuntimeError("boom-send") |
| 2096 |
|
| 2097 |
monkeypatch.setattr( |
| 2098 |
_Client, "send_message", boom_send_message, raising=True |
| 2099 |
) |
| 2100 |
|
| 2101 |
assert a._session_started.is_set() is True |
| 2102 |
|
| 2103 |
assert ( |
| 2104 |
run_on_loop( |
| 2105 |
loop, |
| 2106 |
a._send_keepalive_async(targets=[], subject="s", body="b"), |
| 2107 |
) |
| 2108 |
is False |
| 2109 |
) |
| 2110 |
assert a._session_started.is_set() is False |
| 2111 |
|
| 2112 |
finally: |
| 2113 |
with contextlib.suppress(Exception): |
| 2114 |
asyncio.set_event_loop(None) |
| 2115 |
loop.close() |
| 2116 |
|
| 2117 |
|
| 2118 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2119 |
def test_adapter_send_message_keepalive_timeout_and_exception( |
| 2120 |
monkeypatch: pytest.MonkeyPatch, |
| 2121 |
) -> None: |
| 2122 |
install_fake_slixmpp(monkeypatch) |
| 2123 |
config = xmpp_adapter.XMPPConfig( |
| 2124 |
jid="[email protected]", |
| 2125 |
password="x", |
| 2126 |
host="example.com", |
| 2127 |
port=5222, |
| 2128 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2129 |
verify_certificate=False, |
| 2130 |
) |
| 2131 |
a = xmpp_adapter.SlixmppAdapter( |
| 2132 |
config=config, |
| 2133 |
targets=[], |
| 2134 |
subject="s", |
| 2135 |
body="b", |
| 2136 |
timeout=5.0, |
| 2137 |
keepalive=True, |
| 2138 |
) |
| 2139 |
|
| 2140 |
|
| 2141 |
monkeypatch.setattr( |
| 2142 |
a, "_ensure_keepalive_worker", lambda: True, raising=True |
| 2143 |
) |
| 2144 |
|
| 2145 |
calls: list[str] = [] |
| 2146 |
|
| 2147 |
class _Loop: |
| 2148 |
def call_soon_threadsafe(self, cb: Any, *args: Any) -> None: |
| 2149 |
calls.append(getattr(cb, "__name__", "cb")) |
| 2150 |
cb(*args) |
| 2151 |
|
| 2152 |
a._loop = _Loop() |
| 2153 |
|
| 2154 |
cleared = {"n": 0} |
| 2155 |
|
| 2156 |
class _Evt: |
| 2157 |
def clear(self) -> None: |
| 2158 |
cleared["n"] += 1 |
| 2159 |
|
| 2160 |
a._session_started = _Evt() |
| 2161 |
|
| 2162 |
|
| 2163 |
class _FutureTimeout: |
| 2164 |
def result(self, timeout: Optional[float] = None) -> Any: |
| 2165 |
raise xmpp_adapter.FuturesTimeoutError() |
| 2166 |
|
| 2167 |
def run_coroutine_threadsafe_timeout(coro: Any, loop: Any) -> Any: |
| 2168 |
|
| 2169 |
with contextlib.suppress(Exception): |
| 2170 |
coro.close() |
| 2171 |
return _FutureTimeout() |
| 2172 |
|
| 2173 |
monkeypatch.setattr( |
| 2174 |
xmpp_adapter.asyncio, |
| 2175 |
"run_coroutine_threadsafe", |
| 2176 |
run_coroutine_threadsafe_timeout, |
| 2177 |
raising=True, |
| 2178 |
) |
| 2179 |
|
| 2180 |
assert a.send_message(targets=[], subject="s", body="b") is False |
| 2181 |
assert cleared["n"] == 1 |
| 2182 |
|
| 2183 |
|
| 2184 |
class _FutureError: |
| 2185 |
def result(self, timeout: Optional[float] = None) -> Any: |
| 2186 |
raise RuntimeError("boom") |
| 2187 |
|
| 2188 |
monkeypatch.setattr( |
| 2189 |
xmpp_adapter.asyncio, |
| 2190 |
"run_coroutine_threadsafe", |
| 2191 |
run_coroutine_threadsafe_timeout, |
| 2192 |
raising=True, |
| 2193 |
) |
| 2194 |
|
| 2195 |
assert a.send_message(targets=[], subject="s", body="b") is False |
| 2196 |
|
| 2197 |
|
| 2198 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2199 |
def test_adapter_del_suppresses_close_exception( |
| 2200 |
monkeypatch: pytest.MonkeyPatch, |
| 2201 |
) -> None: |
| 2202 |
""" |
| 2203 |
Cover SlixmppAdapter.__del__() exception suppression |
| 2204 |
""" |
| 2205 |
install_fake_slixmpp(monkeypatch) |
| 2206 |
|
| 2207 |
cfg = xmpp_adapter.XMPPConfig( |
| 2208 |
jid="[email protected]", |
| 2209 |
password="x", |
| 2210 |
host="example.com", |
| 2211 |
port=5222, |
| 2212 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2213 |
verify_certificate=False, |
| 2214 |
) |
| 2215 |
|
| 2216 |
a = xmpp_adapter.SlixmppAdapter( |
| 2217 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2218 |
) |
| 2219 |
|
| 2220 |
def boom_close() -> None: |
| 2221 |
raise RuntimeError("boom-close") |
| 2222 |
|
| 2223 |
monkeypatch.setattr(a, "close", boom_close, raising=True) |
| 2224 |
|
| 2225 |
|
| 2226 |
del a |
| 2227 |
gc.collect() |
| 2228 |
|
| 2229 |
|
| 2230 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2231 |
def test_adapter_close_shutdown_paths_and_state_reset( |
| 2232 |
monkeypatch: pytest.MonkeyPatch, |
| 2233 |
) -> None: |
| 2234 |
""" |
| 2235 |
Cover close(): _shutdown() branches and loop.call_soon_threadsafe exception |
| 2236 |
suppression, plus state reset |
| 2237 |
""" |
| 2238 |
install_fake_slixmpp(monkeypatch) |
| 2239 |
|
| 2240 |
cfg = xmpp_adapter.XMPPConfig( |
| 2241 |
jid="[email protected]", |
| 2242 |
password="x", |
| 2243 |
host="example.com", |
| 2244 |
port=5222, |
| 2245 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2246 |
verify_certificate=False, |
| 2247 |
) |
| 2248 |
|
| 2249 |
a = xmpp_adapter.SlixmppAdapter( |
| 2250 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2251 |
) |
| 2252 |
|
| 2253 |
class _Client: |
| 2254 |
def disconnect(self) -> None: |
| 2255 |
raise RuntimeError("disconnect boom") |
| 2256 |
|
| 2257 |
class _Loop: |
| 2258 |
def __init__(self) -> None: |
| 2259 |
self.called: list[str] = [] |
| 2260 |
|
| 2261 |
def stop(self) -> None: |
| 2262 |
self.called.append("stop") |
| 2263 |
raise RuntimeError("stop boom") |
| 2264 |
|
| 2265 |
def call_soon_threadsafe(self, cb: Any, *args: Any) -> None: |
| 2266 |
self.called.append("call_soon_threadsafe") |
| 2267 |
|
| 2268 |
cb(*args) |
| 2269 |
|
| 2270 |
class _Thread: |
| 2271 |
def join(self, timeout: Optional[float] = None) -> None: |
| 2272 |
return None |
| 2273 |
|
| 2274 |
|
| 2275 |
loop = _Loop() |
| 2276 |
a._loop = loop |
| 2277 |
a._client = _Client() |
| 2278 |
a._thread = _Thread() |
| 2279 |
|
| 2280 |
|
| 2281 |
a._connect_lock = asyncio.Lock() |
| 2282 |
a._session_started = asyncio.Event() |
| 2283 |
|
| 2284 |
a.close() |
| 2285 |
|
| 2286 |
|
| 2287 |
assert a._thread is None |
| 2288 |
assert a._loop is None |
| 2289 |
assert a._client is None |
| 2290 |
assert a._connect_lock is None |
| 2291 |
assert a._session_started is None |
| 2292 |
|
| 2293 |
|
| 2294 |
assert "call_soon_threadsafe" in loop.called |
| 2295 |
assert "stop" in loop.called |
| 2296 |
|
| 2297 |
|
| 2298 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2299 |
def test_adapter_close_call_soon_threadsafe_raises_still_resets( |
| 2300 |
monkeypatch: pytest.MonkeyPatch, |
| 2301 |
) -> None: |
| 2302 |
""" |
| 2303 |
Cover close(): loop.call_soon_threadsafe(_shutdown) raising and being |
| 2304 |
suppressed, while still joining and clearing state. |
| 2305 |
""" |
| 2306 |
install_fake_slixmpp(monkeypatch) |
| 2307 |
|
| 2308 |
cfg = xmpp_adapter.XMPPConfig( |
| 2309 |
jid="[email protected]", |
| 2310 |
password="x", |
| 2311 |
host="example.com", |
| 2312 |
port=5222, |
| 2313 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2314 |
verify_certificate=False, |
| 2315 |
) |
| 2316 |
|
| 2317 |
a = xmpp_adapter.SlixmppAdapter( |
| 2318 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2319 |
) |
| 2320 |
|
| 2321 |
class _Loop: |
| 2322 |
def call_soon_threadsafe(self, cb: Any, *args: Any) -> None: |
| 2323 |
raise RuntimeError("csts boom") |
| 2324 |
|
| 2325 |
def stop(self) -> None: |
| 2326 |
return None |
| 2327 |
|
| 2328 |
class _Thread: |
| 2329 |
def join(self, timeout: Optional[float] = None) -> None: |
| 2330 |
return None |
| 2331 |
|
| 2332 |
a._loop = _Loop() |
| 2333 |
a._client = SimpleNamespace(disconnect=lambda: None) |
| 2334 |
a._thread = _Thread() |
| 2335 |
a._connect_lock = asyncio.Lock() |
| 2336 |
a._session_started = asyncio.Event() |
| 2337 |
|
| 2338 |
a.close() |
| 2339 |
|
| 2340 |
assert a._thread is None |
| 2341 |
assert a._loop is None |
| 2342 |
assert a._client is None |
| 2343 |
assert a._connect_lock is None |
| 2344 |
assert a._session_started is None |
| 2345 |
|
| 2346 |
|
| 2347 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2348 |
def test_adapter_ensure_keepalive_worker_returns_true_at_end( |
| 2349 |
monkeypatch: pytest.MonkeyPatch, |
| 2350 |
) -> None: |
| 2351 |
""" |
| 2352 |
Hit _ensure_keepalive_worker() bottom return True |
| 2353 |
""" |
| 2354 |
install_fake_slixmpp(monkeypatch) |
| 2355 |
|
| 2356 |
cfg = xmpp_adapter.XMPPConfig( |
| 2357 |
jid="[email protected]", |
| 2358 |
password="x", |
| 2359 |
host="example.com", |
| 2360 |
port=5222, |
| 2361 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2362 |
verify_certificate=False, |
| 2363 |
) |
| 2364 |
|
| 2365 |
a = xmpp_adapter.SlixmppAdapter( |
| 2366 |
config=cfg, |
| 2367 |
targets=[], |
| 2368 |
subject="s", |
| 2369 |
body="b", |
| 2370 |
timeout=5.0, |
| 2371 |
keepalive=True, |
| 2372 |
) |
| 2373 |
|
| 2374 |
|
| 2375 |
def runner() -> None: |
| 2376 |
a._loop_ready.set() |
| 2377 |
return None |
| 2378 |
|
| 2379 |
monkeypatch.setattr(a, "_keepalive_runner", runner, raising=True) |
| 2380 |
|
| 2381 |
assert a._ensure_keepalive_worker() is True |
| 2382 |
|
| 2383 |
|
| 2384 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2385 |
def test_adapter_keepalive_runner_executes_and_register_plugin_suppressed( |
| 2386 |
monkeypatch: pytest.MonkeyPatch, |
| 2387 |
) -> None: |
| 2388 |
""" |
| 2389 |
Execute _keepalive_runner() including: |
| 2390 |
- secure mode config |
| 2391 |
- register_plugin() suppression |
| 2392 |
- loop.run_forever() exit |
| 2393 |
- finally: loop.stop/close |
| 2394 |
""" |
| 2395 |
install_fake_slixmpp(monkeypatch) |
| 2396 |
|
| 2397 |
|
| 2398 |
def boom_register_plugin( |
| 2399 |
self: FakeClientXMPP, name: str, config: Optional[Any] = None |
| 2400 |
) -> None: |
| 2401 |
raise RuntimeError("boom-plugin") |
| 2402 |
|
| 2403 |
monkeypatch.setattr( |
| 2404 |
FakeClientXMPP, "register_plugin", boom_register_plugin, raising=True |
| 2405 |
) |
| 2406 |
|
| 2407 |
|
| 2408 |
|
| 2409 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 2410 |
|
| 2411 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 2412 |
loop = real_new_event_loop() |
| 2413 |
|
| 2414 |
|
| 2415 |
monkeypatch.setattr(loop, "run_forever", lambda: None, raising=True) |
| 2416 |
|
| 2417 |
|
| 2418 |
return loop |
| 2419 |
|
| 2420 |
monkeypatch.setattr( |
| 2421 |
xmpp_adapter.asyncio, |
| 2422 |
"new_event_loop", |
| 2423 |
new_event_loop_wrapped, |
| 2424 |
raising=True, |
| 2425 |
) |
| 2426 |
|
| 2427 |
cfg = xmpp_adapter.XMPPConfig( |
| 2428 |
jid="[email protected]", |
| 2429 |
password="x", |
| 2430 |
host="example.com", |
| 2431 |
port=5222, |
| 2432 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2433 |
verify_certificate=False, |
| 2434 |
) |
| 2435 |
|
| 2436 |
a = xmpp_adapter.SlixmppAdapter( |
| 2437 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2438 |
) |
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
a._keepalive_runner() |
| 2443 |
|
| 2444 |
|
| 2445 |
assert a._loop is not None |
| 2446 |
assert a._client is not None |
| 2447 |
assert a._connect_lock is not None |
| 2448 |
assert a._session_started is not None |
| 2449 |
|
| 2450 |
|
| 2451 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2452 |
def test_adapter_connect_if_required_hits_bottom_return_true( |
| 2453 |
monkeypatch: pytest.MonkeyPatch, |
| 2454 |
) -> None: |
| 2455 |
""" |
| 2456 |
Cover _connect_if_required() success path reaching the final return True, |
| 2457 |
not the early already-connected return. |
| 2458 |
""" |
| 2459 |
install_fake_slixmpp(monkeypatch) |
| 2460 |
|
| 2461 |
cfg = xmpp_adapter.XMPPConfig( |
| 2462 |
jid="[email protected]", |
| 2463 |
password="x", |
| 2464 |
host="example.com", |
| 2465 |
port=5222, |
| 2466 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2467 |
verify_certificate=False, |
| 2468 |
) |
| 2469 |
|
| 2470 |
a = xmpp_adapter.SlixmppAdapter( |
| 2471 |
config=cfg, |
| 2472 |
targets=[], |
| 2473 |
subject="s", |
| 2474 |
body="b", |
| 2475 |
timeout=5.0, |
| 2476 |
keepalive=True, |
| 2477 |
) |
| 2478 |
|
| 2479 |
loop = asyncio.new_event_loop() |
| 2480 |
try: |
| 2481 |
asyncio.set_event_loop(loop) |
| 2482 |
a._loop = loop |
| 2483 |
a._connect_lock = asyncio.Lock() |
| 2484 |
a._session_started = asyncio.Event() |
| 2485 |
|
| 2486 |
fut = loop.create_future() |
| 2487 |
fut.set_result(True) |
| 2488 |
|
| 2489 |
class _Client: |
| 2490 |
def connect(self, **kwargs: Any) -> Any: |
| 2491 |
return fut |
| 2492 |
|
| 2493 |
a._client = _Client() |
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
real_wait_for = xmpp_adapter.asyncio.wait_for |
| 2498 |
calls = {"n": 0} |
| 2499 |
|
| 2500 |
async def wait_for_patched( |
| 2501 |
aw: Any, timeout: Optional[float] = None |
| 2502 |
) -> Any: |
| 2503 |
calls["n"] += 1 |
| 2504 |
if calls["n"] == 2: |
| 2505 |
assert a._session_started is not None |
| 2506 |
a._session_started.set() |
| 2507 |
return await real_wait_for(aw, timeout=timeout) |
| 2508 |
|
| 2509 |
monkeypatch.setattr( |
| 2510 |
xmpp_adapter.asyncio, "wait_for", wait_for_patched, raising=True |
| 2511 |
) |
| 2512 |
|
| 2513 |
assert run_on_loop(loop, a._connect_if_required()) is True |
| 2514 |
finally: |
| 2515 |
with contextlib.suppress(Exception): |
| 2516 |
asyncio.set_event_loop(None) |
| 2517 |
loop.close() |
| 2518 |
|
| 2519 |
|
| 2520 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2521 |
def test_adapter_send_keepalive_async_connect_fail_branch( |
| 2522 |
monkeypatch: pytest.MonkeyPatch, |
| 2523 |
) -> None: |
| 2524 |
""" |
| 2525 |
Cover _send_keepalive_async(): if not ok: return False |
| 2526 |
""" |
| 2527 |
install_fake_slixmpp(monkeypatch) |
| 2528 |
|
| 2529 |
cfg = xmpp_adapter.XMPPConfig( |
| 2530 |
jid="[email protected]", |
| 2531 |
password="x", |
| 2532 |
host="example.com", |
| 2533 |
port=5222, |
| 2534 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2535 |
verify_certificate=False, |
| 2536 |
) |
| 2537 |
|
| 2538 |
a = xmpp_adapter.SlixmppAdapter( |
| 2539 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2540 |
) |
| 2541 |
|
| 2542 |
|
| 2543 |
a._client = SimpleNamespace(send_message=lambda **kw: None) |
| 2544 |
|
| 2545 |
async def fail_connect() -> bool: |
| 2546 |
return False |
| 2547 |
|
| 2548 |
monkeypatch.setattr(a, "_connect_if_required", fail_connect, raising=True) |
| 2549 |
|
| 2550 |
assert ( |
| 2551 |
asyncio.run( |
| 2552 |
a._send_keepalive_async(targets=["x"], subject="s", body="b") |
| 2553 |
) |
| 2554 |
is False |
| 2555 |
) |
| 2556 |
|
| 2557 |
|
| 2558 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2559 |
def test_adapter_send_keepalive_async_exception_when_session_started_none( |
| 2560 |
monkeypatch: pytest.MonkeyPatch, |
| 2561 |
) -> None: |
| 2562 |
""" |
| 2563 |
Cover _send_keepalive_async() exception path where _session_started is None |
| 2564 |
so clear() is skipped |
| 2565 |
""" |
| 2566 |
install_fake_slixmpp(monkeypatch) |
| 2567 |
|
| 2568 |
cfg = xmpp_adapter.XMPPConfig( |
| 2569 |
jid="[email protected]", |
| 2570 |
password="x", |
| 2571 |
host="example.com", |
| 2572 |
port=5222, |
| 2573 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2574 |
verify_certificate=False, |
| 2575 |
) |
| 2576 |
|
| 2577 |
a = xmpp_adapter.SlixmppAdapter( |
| 2578 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2579 |
) |
| 2580 |
|
| 2581 |
async def ok_connect() -> bool: |
| 2582 |
return True |
| 2583 |
|
| 2584 |
monkeypatch.setattr(a, "_connect_if_required", ok_connect, raising=True) |
| 2585 |
|
| 2586 |
class _Client: |
| 2587 |
def send_message(self, **kwargs: Any) -> None: |
| 2588 |
raise RuntimeError("boom-send") |
| 2589 |
|
| 2590 |
a._client = _Client() |
| 2591 |
|
| 2592 |
|
| 2593 |
a._session_started = None |
| 2594 |
|
| 2595 |
assert ( |
| 2596 |
asyncio.run( |
| 2597 |
a._send_keepalive_async(targets=["x"], subject="s", body="b") |
| 2598 |
) |
| 2599 |
is False |
| 2600 |
) |
| 2601 |
|
| 2602 |
|
| 2603 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2604 |
def test_adapter_close_shutdown_client_none_branch( |
| 2605 |
monkeypatch: pytest.MonkeyPatch, |
| 2606 |
) -> None: |
| 2607 |
""" |
| 2608 |
Cover close(): _shutdown() path where client is None. |
| 2609 |
""" |
| 2610 |
install_fake_slixmpp(monkeypatch) |
| 2611 |
|
| 2612 |
cfg = xmpp_adapter.XMPPConfig( |
| 2613 |
jid="[email protected]", |
| 2614 |
password="x", |
| 2615 |
host="example.com", |
| 2616 |
port=5222, |
| 2617 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2618 |
verify_certificate=False, |
| 2619 |
) |
| 2620 |
a = xmpp_adapter.SlixmppAdapter( |
| 2621 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2622 |
) |
| 2623 |
|
| 2624 |
class _Loop: |
| 2625 |
def __init__(self) -> None: |
| 2626 |
self.stopped = False |
| 2627 |
self.called = False |
| 2628 |
|
| 2629 |
def stop(self) -> None: |
| 2630 |
self.stopped = True |
| 2631 |
|
| 2632 |
def call_soon_threadsafe(self, cb: Any, *args: Any) -> None: |
| 2633 |
self.called = True |
| 2634 |
cb(*args) |
| 2635 |
|
| 2636 |
class _Thread: |
| 2637 |
def join(self, timeout: Optional[float] = None) -> None: |
| 2638 |
return None |
| 2639 |
|
| 2640 |
loop = _Loop() |
| 2641 |
a._loop = loop |
| 2642 |
a._thread = _Thread() |
| 2643 |
|
| 2644 |
|
| 2645 |
a._client = None |
| 2646 |
|
| 2647 |
a.close() |
| 2648 |
|
| 2649 |
assert loop.called is True |
| 2650 |
assert loop.stopped is True |
| 2651 |
assert a._loop is None |
| 2652 |
assert a._thread is None |
| 2653 |
assert a._client is None |
| 2654 |
|
| 2655 |
|
| 2656 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2657 |
def test_adapter_close_thread_alive_returns_without_clearing_state( |
| 2658 |
monkeypatch: pytest.MonkeyPatch, |
| 2659 |
) -> None: |
| 2660 |
"""Cover close() branch where worker thread is still alive.""" |
| 2661 |
install_fake_slixmpp(monkeypatch) |
| 2662 |
|
| 2663 |
cfg = xmpp_adapter.XMPPConfig( |
| 2664 |
jid="[email protected]", |
| 2665 |
password="x", |
| 2666 |
host="example.com", |
| 2667 |
port=5222, |
| 2668 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2669 |
verify_certificate=False, |
| 2670 |
) |
| 2671 |
|
| 2672 |
a = xmpp_adapter.SlixmppAdapter( |
| 2673 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2674 |
) |
| 2675 |
|
| 2676 |
class _Client: |
| 2677 |
def disconnect(self) -> None: |
| 2678 |
return None |
| 2679 |
|
| 2680 |
class _Loop: |
| 2681 |
def __init__(self) -> None: |
| 2682 |
self.called: list[str] = [] |
| 2683 |
|
| 2684 |
def stop(self) -> None: |
| 2685 |
self.called.append("stop") |
| 2686 |
|
| 2687 |
def call_soon_threadsafe(self, cb: Any, *args: Any) -> None: |
| 2688 |
self.called.append("call_soon_threadsafe") |
| 2689 |
cb(*args) |
| 2690 |
|
| 2691 |
class _Thread: |
| 2692 |
def join(self, timeout: Optional[float] = None) -> None: |
| 2693 |
return None |
| 2694 |
|
| 2695 |
def is_alive(self) -> bool: |
| 2696 |
return True |
| 2697 |
|
| 2698 |
loop = _Loop() |
| 2699 |
thread = _Thread() |
| 2700 |
|
| 2701 |
a._loop = loop |
| 2702 |
a._client = _Client() |
| 2703 |
a._thread = thread |
| 2704 |
a._connect_lock = asyncio.Lock() |
| 2705 |
a._session_started = asyncio.Event() |
| 2706 |
|
| 2707 |
a.close() |
| 2708 |
|
| 2709 |
assert a._loop is loop |
| 2710 |
assert a._thread is thread |
| 2711 |
assert loop.called[0] == "call_soon_threadsafe" |
| 2712 |
|
| 2713 |
|
| 2714 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2715 |
def test_adapter_keepalive_runner_returns_if_closing_before_publish( |
| 2716 |
monkeypatch: pytest.MonkeyPatch, |
| 2717 |
) -> None: |
| 2718 |
"""Cover _keepalive_runner() state-lock early return when closing.""" |
| 2719 |
install_fake_slixmpp(monkeypatch) |
| 2720 |
|
| 2721 |
cfg = xmpp_adapter.XMPPConfig( |
| 2722 |
jid="[email protected]", |
| 2723 |
password="x", |
| 2724 |
host="example.com", |
| 2725 |
port=5222, |
| 2726 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2727 |
verify_certificate=False, |
| 2728 |
) |
| 2729 |
|
| 2730 |
a = xmpp_adapter.SlixmppAdapter( |
| 2731 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2732 |
) |
| 2733 |
|
| 2734 |
def _closing_register_plugin( |
| 2735 |
self: FakeClientXMPP, name: str, config: Optional[Any] = None |
| 2736 |
) -> None: |
| 2737 |
a._closing = True |
| 2738 |
return None |
| 2739 |
|
| 2740 |
monkeypatch.setattr( |
| 2741 |
FakeClientXMPP, |
| 2742 |
"register_plugin", |
| 2743 |
_closing_register_plugin, |
| 2744 |
raising=True, |
| 2745 |
) |
| 2746 |
|
| 2747 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 2748 |
|
| 2749 |
def _new_event_loop() -> asyncio.AbstractEventLoop: |
| 2750 |
loop = real_new_event_loop() |
| 2751 |
monkeypatch.setattr( |
| 2752 |
loop, |
| 2753 |
"run_forever", |
| 2754 |
lambda: (_ for _ in ()).throw(AssertionError("run_forever")), |
| 2755 |
raising=True, |
| 2756 |
) |
| 2757 |
return loop |
| 2758 |
|
| 2759 |
monkeypatch.setattr( |
| 2760 |
xmpp_adapter.asyncio, |
| 2761 |
"new_event_loop", |
| 2762 |
_new_event_loop, |
| 2763 |
raising=True, |
| 2764 |
) |
| 2765 |
|
| 2766 |
a._loop_ready.clear() |
| 2767 |
a._keepalive_runner() |
| 2768 |
|
| 2769 |
assert a._loop_ready.is_set() is False |
| 2770 |
assert a._loop is None |
| 2771 |
assert a._client is None |
| 2772 |
|
| 2773 |
|
| 2774 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2775 |
def test_adapter_keepalive_runner_finally_clears_state_when_closing( |
| 2776 |
monkeypatch: pytest.MonkeyPatch, |
| 2777 |
) -> None: |
| 2778 |
"""Cover _keepalive_runner() finally state-clear when closing.""" |
| 2779 |
install_fake_slixmpp(monkeypatch) |
| 2780 |
|
| 2781 |
cfg = xmpp_adapter.XMPPConfig( |
| 2782 |
jid="[email protected]", |
| 2783 |
password="x", |
| 2784 |
host="example.com", |
| 2785 |
port=5222, |
| 2786 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2787 |
verify_certificate=False, |
| 2788 |
) |
| 2789 |
|
| 2790 |
a = xmpp_adapter.SlixmppAdapter( |
| 2791 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 2792 |
) |
| 2793 |
|
| 2794 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 2795 |
created: list[asyncio.AbstractEventLoop] = [] |
| 2796 |
|
| 2797 |
def _new_event_loop() -> asyncio.AbstractEventLoop: |
| 2798 |
loop = real_new_event_loop() |
| 2799 |
created.append(loop) |
| 2800 |
|
| 2801 |
def _run_forever() -> None: |
| 2802 |
a._closing = True |
| 2803 |
return None |
| 2804 |
|
| 2805 |
monkeypatch.setattr(loop, "run_forever", _run_forever, raising=True) |
| 2806 |
return loop |
| 2807 |
|
| 2808 |
monkeypatch.setattr( |
| 2809 |
xmpp_adapter.asyncio, |
| 2810 |
"new_event_loop", |
| 2811 |
_new_event_loop, |
| 2812 |
raising=True, |
| 2813 |
) |
| 2814 |
|
| 2815 |
a._loop_ready.clear() |
| 2816 |
a._keepalive_runner() |
| 2817 |
|
| 2818 |
assert a._loop_ready.is_set() is True |
| 2819 |
assert a._loop is None |
| 2820 |
assert a._client is None |
| 2821 |
assert a._connect_lock is None |
| 2822 |
assert a._session_started is None |
| 2823 |
|
| 2824 |
assert created |
| 2825 |
assert created[0].is_closed() |
| 2826 |
|
| 2827 |
|
| 2828 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2829 |
def test_adapter_keepalive_runner_session_start_sets_event_even_on_exception( |
| 2830 |
monkeypatch: pytest.MonkeyPatch, |
| 2831 |
) -> None: |
| 2832 |
"""Cover keepalive _Client._session_start() exception handling.""" |
| 2833 |
|
| 2834 |
install_fake_slixmpp(monkeypatch) |
| 2835 |
|
| 2836 |
cfg = xmpp_adapter.XMPPConfig( |
| 2837 |
jid="[email protected]", |
| 2838 |
password="x", |
| 2839 |
host="ex.com", |
| 2840 |
port=5222, |
| 2841 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 2842 |
verify_certificate=False, |
| 2843 |
) |
| 2844 |
|
| 2845 |
a = xmpp_adapter.SlixmppAdapter( |
| 2846 |
config=cfg, |
| 2847 |
targets=[], |
| 2848 |
subject="s", |
| 2849 |
body="b", |
| 2850 |
keepalive=True, |
| 2851 |
roster=True, |
| 2852 |
timeout=5.0, |
| 2853 |
) |
| 2854 |
|
| 2855 |
loop = asyncio.new_event_loop() |
| 2856 |
|
| 2857 |
asyncio.set_event_loop(None) |
| 2858 |
|
| 2859 |
try: |
| 2860 |
|
| 2861 |
def run_forever_hook() -> None: |
| 2862 |
client = a._client |
| 2863 |
started = a._session_started |
| 2864 |
|
| 2865 |
monkeypatch.setattr( |
| 2866 |
client, |
| 2867 |
"send_presence", |
| 2868 |
lambda: (_ for _ in ()).throw(RuntimeError("fail")), |
| 2869 |
raising=True, |
| 2870 |
) |
| 2871 |
|
| 2872 |
client._on_session_start() |
| 2873 |
|
| 2874 |
async def wait_for_event(): |
| 2875 |
while not started.is_set(): |
| 2876 |
await asyncio.sleep(0.01) |
| 2877 |
|
| 2878 |
|
| 2879 |
coro = wait_for_event() |
| 2880 |
try: |
| 2881 |
wf = asyncio.wait_for(coro, timeout=1.0) |
| 2882 |
try: |
| 2883 |
loop.run_until_complete(wf) |
| 2884 |
finally: |
| 2885 |
with contextlib.suppress(Exception): |
| 2886 |
wf.close() |
| 2887 |
|
| 2888 |
finally: |
| 2889 |
|
| 2890 |
with contextlib.suppress(Exception): |
| 2891 |
coro.close() |
| 2892 |
|
| 2893 |
assert started.is_set() is True |
| 2894 |
return None |
| 2895 |
|
| 2896 |
monkeypatch.setattr( |
| 2897 |
xmpp_adapter.asyncio, |
| 2898 |
"new_event_loop", |
| 2899 |
lambda: loop, |
| 2900 |
) |
| 2901 |
monkeypatch.setattr(loop, "run_forever", run_forever_hook) |
| 2902 |
|
| 2903 |
a._keepalive_runner() |
| 2904 |
|
| 2905 |
finally: |
| 2906 |
if not loop.is_closed(): |
| 2907 |
|
| 2908 |
|
| 2909 |
for task in asyncio.all_tasks(loop): |
| 2910 |
task.cancel() |
| 2911 |
|
| 2912 |
with contextlib.suppress(Exception): |
| 2913 |
loop.run_until_complete(asyncio.sleep(0)) |
| 2914 |
|
| 2915 |
loop.close() |
| 2916 |
|
| 2917 |
|
| 2918 |
asyncio.set_event_loop(None) |
| 2919 |
|
| 2920 |
|
| 2921 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2922 |
def test_client_session_start_roster_timeout_closes_awaitable( |
| 2923 |
monkeypatch: pytest.MonkeyPatch, |
| 2924 |
) -> None: |
| 2925 |
"""Cover roster timeout close path in _session_start().""" |
| 2926 |
install_fake_slixmpp(monkeypatch) |
| 2927 |
|
| 2928 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 2929 |
|
| 2930 |
called: list[object] = [] |
| 2931 |
|
| 2932 |
orig_close = xmpp_adapter._close_awaitable |
| 2933 |
|
| 2934 |
def _spy_close(obj: object) -> None: |
| 2935 |
called.append(obj) |
| 2936 |
orig_close(obj) |
| 2937 |
|
| 2938 |
monkeypatch.setattr( |
| 2939 |
xmpp_adapter, "_close_awaitable", _spy_close, raising=True |
| 2940 |
) |
| 2941 |
|
| 2942 |
async def _raise_timeout(*args: object, **kwargs: object) -> None: |
| 2943 |
raise asyncio.TimeoutError() |
| 2944 |
|
| 2945 |
monkeypatch.setattr( |
| 2946 |
xmpp_adapter.asyncio, "wait_for", _raise_timeout, raising=True |
| 2947 |
) |
| 2948 |
|
| 2949 |
loop = asyncio.new_event_loop() |
| 2950 |
asyncio.set_event_loop(loop) |
| 2951 |
try: |
| 2952 |
evt = asyncio.Event() |
| 2953 |
client = client_cls( |
| 2954 |
jid="[email protected]", |
| 2955 |
password="pass", |
| 2956 |
oneshot=False, |
| 2957 |
want_roster=True, |
| 2958 |
roster_timeout=1.0, |
| 2959 |
session_started_evt=evt, |
| 2960 |
) |
| 2961 |
|
| 2962 |
loop.run_until_complete(client._session_start()) |
| 2963 |
assert len(called) == 1 |
| 2964 |
|
| 2965 |
finally: |
| 2966 |
asyncio.set_event_loop(None) |
| 2967 |
loop.close() |
| 2968 |
|
| 2969 |
|
| 2970 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 2971 |
def test_client_session_start_keepalive_sets_ready_event( |
| 2972 |
monkeypatch: pytest.MonkeyPatch, |
| 2973 |
) -> None: |
| 2974 |
"""Cover non-oneshot path in _session_start().""" |
| 2975 |
install_fake_slixmpp(monkeypatch) |
| 2976 |
|
| 2977 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 2978 |
|
| 2979 |
loop = asyncio.new_event_loop() |
| 2980 |
asyncio.set_event_loop(loop) |
| 2981 |
try: |
| 2982 |
evt = asyncio.Event() |
| 2983 |
|
| 2984 |
client = client_cls( |
| 2985 |
jid="[email protected]", |
| 2986 |
password="pass", |
| 2987 |
oneshot=False, |
| 2988 |
targets=["a", "b"], |
| 2989 |
subject="s", |
| 2990 |
body="b", |
| 2991 |
before_message=lambda: None, |
| 2992 |
want_roster=False, |
| 2993 |
roster_timeout=0.0, |
| 2994 |
session_started_evt=evt, |
| 2995 |
) |
| 2996 |
|
| 2997 |
monkeypatch.setattr( |
| 2998 |
client, |
| 2999 |
"send_message", |
| 3000 |
lambda **k: (_ for _ in ()).throw(AssertionError()), |
| 3001 |
raising=True, |
| 3002 |
) |
| 3003 |
monkeypatch.setattr( |
| 3004 |
client, |
| 3005 |
"disconnect", |
| 3006 |
lambda: (_ for _ in ()).throw(AssertionError()), |
| 3007 |
raising=True, |
| 3008 |
) |
| 3009 |
|
| 3010 |
loop.run_until_complete(client._session_start()) |
| 3011 |
assert evt.is_set() is True |
| 3012 |
|
| 3013 |
finally: |
| 3014 |
asyncio.set_event_loop(None) |
| 3015 |
loop.close() |
| 3016 |
|
| 3017 |
|
| 3018 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3019 |
def test_client_session_start_oneshot_sends_and_disconnects( |
| 3020 |
monkeypatch: pytest.MonkeyPatch, |
| 3021 |
) -> None: |
| 3022 |
"""Cover oneshot path in _session_start() and finally disconnect.""" |
| 3023 |
install_fake_slixmpp(monkeypatch) |
| 3024 |
|
| 3025 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 3026 |
|
| 3027 |
sent: list[dict[str, Any]] = [] |
| 3028 |
before: list[int] = [] |
| 3029 |
disconnected: list[bool] = [] |
| 3030 |
|
| 3031 |
def _before() -> None: |
| 3032 |
before.append(1) |
| 3033 |
|
| 3034 |
def _send_message(**kwargs: Any) -> None: |
| 3035 |
sent.append(kwargs) |
| 3036 |
|
| 3037 |
def _disconnect() -> None: |
| 3038 |
disconnected.append(True) |
| 3039 |
|
| 3040 |
loop = asyncio.new_event_loop() |
| 3041 |
asyncio.set_event_loop(loop) |
| 3042 |
try: |
| 3043 |
client = client_cls( |
| 3044 |
jid="[email protected]", |
| 3045 |
password="pass", |
| 3046 |
oneshot=True, |
| 3047 |
targets=[("chat", "[email protected]"), ("chat", "[email protected]")], |
| 3048 |
subject="sub", |
| 3049 |
body="body", |
| 3050 |
before_message=_before, |
| 3051 |
want_roster=False, |
| 3052 |
roster_timeout=0.0, |
| 3053 |
session_started_evt=None, |
| 3054 |
) |
| 3055 |
|
| 3056 |
monkeypatch.setattr( |
| 3057 |
client, "send_message", _send_message, raising=True |
| 3058 |
) |
| 3059 |
monkeypatch.setattr(client, "disconnect", _disconnect, raising=True) |
| 3060 |
|
| 3061 |
loop.run_until_complete(client._session_start()) |
| 3062 |
|
| 3063 |
assert len(before) == 2 |
| 3064 |
assert len(sent) == 2 |
| 3065 |
assert all(msg.get("mtype") == "chat" for msg in sent) |
| 3066 |
assert disconnected == [True] |
| 3067 |
|
| 3068 |
finally: |
| 3069 |
asyncio.set_event_loop(None) |
| 3070 |
loop.close() |
| 3071 |
|
| 3072 |
|
| 3073 |
def test_adapter_keepalive_runner_failed_handlers( |
| 3074 |
monkeypatch: pytest.MonkeyPatch, |
| 3075 |
) -> None: |
| 3076 |
""" |
| 3077 |
Cover keepalive _Client._failed_auth() and _disconnected(). |
| 3078 |
""" |
| 3079 |
install_fake_slixmpp(monkeypatch) |
| 3080 |
|
| 3081 |
cfg = xmpp_adapter.XMPPConfig( |
| 3082 |
jid="[email protected]", |
| 3083 |
password="x", |
| 3084 |
host="example.com", |
| 3085 |
port=5222, |
| 3086 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3087 |
verify_certificate=False, |
| 3088 |
) |
| 3089 |
|
| 3090 |
a = xmpp_adapter.SlixmppAdapter( |
| 3091 |
config=cfg, |
| 3092 |
targets=[], |
| 3093 |
subject="s", |
| 3094 |
body="b", |
| 3095 |
keepalive=True, |
| 3096 |
roster=False, |
| 3097 |
) |
| 3098 |
|
| 3099 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 3100 |
|
| 3101 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 3102 |
loop = real_new_event_loop() |
| 3103 |
|
| 3104 |
def run_forever_hook() -> None: |
| 3105 |
client = a._client |
| 3106 |
started = a._session_started |
| 3107 |
assert client is not None |
| 3108 |
assert started is not None |
| 3109 |
|
| 3110 |
disconnected = {"called": 0} |
| 3111 |
|
| 3112 |
def disconnect_spy() -> None: |
| 3113 |
disconnected["called"] += 1 |
| 3114 |
|
| 3115 |
monkeypatch.setattr( |
| 3116 |
client, "disconnect", disconnect_spy, raising=True |
| 3117 |
) |
| 3118 |
|
| 3119 |
|
| 3120 |
started.set() |
| 3121 |
client._failed_auth() |
| 3122 |
assert started.is_set() is False |
| 3123 |
assert disconnected["called"] == 1 |
| 3124 |
|
| 3125 |
|
| 3126 |
|
| 3127 |
started.set() |
| 3128 |
client._disconnected() |
| 3129 |
assert started.is_set() is False |
| 3130 |
assert disconnected["called"] == 1 |
| 3131 |
|
| 3132 |
return None |
| 3133 |
|
| 3134 |
monkeypatch.setattr( |
| 3135 |
loop, "run_forever", run_forever_hook, raising=True |
| 3136 |
) |
| 3137 |
return loop |
| 3138 |
|
| 3139 |
monkeypatch.setattr( |
| 3140 |
xmpp_adapter.asyncio, |
| 3141 |
"new_event_loop", |
| 3142 |
new_event_loop_wrapped, |
| 3143 |
raising=True, |
| 3144 |
) |
| 3145 |
|
| 3146 |
a._keepalive_runner() |
| 3147 |
|
| 3148 |
|
| 3149 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3150 |
def test_adapter_keepalive_runner_unsupported_secure_mode_hits_valueerror( |
| 3151 |
monkeypatch: pytest.MonkeyPatch, |
| 3152 |
) -> None: |
| 3153 |
""" |
| 3154 |
Cover keepalive runner unsupported secure mode ValueError. |
| 3155 |
The exception is caught internally, but the raise line is executed. |
| 3156 |
""" |
| 3157 |
install_fake_slixmpp(monkeypatch) |
| 3158 |
|
| 3159 |
cfg = xmpp_adapter.XMPPConfig( |
| 3160 |
jid="[email protected]", |
| 3161 |
password="x", |
| 3162 |
host="example.com", |
| 3163 |
port=5222, |
| 3164 |
secure="definitely-not-supported", |
| 3165 |
verify_certificate=False, |
| 3166 |
) |
| 3167 |
|
| 3168 |
a = xmpp_adapter.SlixmppAdapter( |
| 3169 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 3170 |
) |
| 3171 |
|
| 3172 |
|
| 3173 |
a._loop_ready.clear() |
| 3174 |
|
| 3175 |
|
| 3176 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 3177 |
|
| 3178 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 3179 |
loop = real_new_event_loop() |
| 3180 |
monkeypatch.setattr(loop, "run_forever", lambda: None, raising=True) |
| 3181 |
return loop |
| 3182 |
|
| 3183 |
monkeypatch.setattr( |
| 3184 |
xmpp_adapter.asyncio, |
| 3185 |
"new_event_loop", |
| 3186 |
new_event_loop_wrapped, |
| 3187 |
raising=True, |
| 3188 |
) |
| 3189 |
|
| 3190 |
a._keepalive_runner() |
| 3191 |
|
| 3192 |
|
| 3193 |
assert a._loop_ready.is_set() is False |
| 3194 |
assert a._loop is None |
| 3195 |
assert a._client is None |
| 3196 |
assert a._connect_lock is None |
| 3197 |
assert a._session_started is None |
| 3198 |
|
| 3199 |
|
| 3200 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3201 |
def test_adapter_keepalive_session_start_try_finally_and_roster( |
| 3202 |
monkeypatch: pytest.MonkeyPatch, |
| 3203 |
) -> None: |
| 3204 |
""" |
| 3205 |
Cover _session_start() try/finally and roster handling: |
| 3206 |
- try/finally always sets _session_started_evt |
| 3207 |
- roster path is taken and roster errors are suppressed |
| 3208 |
- send_presence exception still leads to finally:set() |
| 3209 |
""" |
| 3210 |
install_fake_slixmpp(monkeypatch) |
| 3211 |
|
| 3212 |
cfg = xmpp_adapter.XMPPConfig( |
| 3213 |
jid="[email protected]", |
| 3214 |
password="x", |
| 3215 |
host="example.com", |
| 3216 |
port=5222, |
| 3217 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3218 |
verify_certificate=False, |
| 3219 |
) |
| 3220 |
|
| 3221 |
a = xmpp_adapter.SlixmppAdapter( |
| 3222 |
config=cfg, |
| 3223 |
targets=[], |
| 3224 |
subject="s", |
| 3225 |
body="b", |
| 3226 |
keepalive=True, |
| 3227 |
roster=True, |
| 3228 |
timeout=5.0, |
| 3229 |
) |
| 3230 |
|
| 3231 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 3232 |
|
| 3233 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 3234 |
loop = real_new_event_loop() |
| 3235 |
|
| 3236 |
def run_forever_hook() -> None: |
| 3237 |
client = a._client |
| 3238 |
evt = a._session_started |
| 3239 |
assert client is not None |
| 3240 |
assert evt is not None |
| 3241 |
|
| 3242 |
|
| 3243 |
def send_presence_ok() -> None: |
| 3244 |
return None |
| 3245 |
|
| 3246 |
async def get_roster_boom() -> None: |
| 3247 |
raise RuntimeError("boom-roster") |
| 3248 |
|
| 3249 |
monkeypatch.setattr( |
| 3250 |
client, "send_presence", send_presence_ok, raising=True |
| 3251 |
) |
| 3252 |
monkeypatch.setattr( |
| 3253 |
client, "get_roster", get_roster_boom, raising=True |
| 3254 |
) |
| 3255 |
|
| 3256 |
evt.clear() |
| 3257 |
client._on_session_start() |
| 3258 |
|
| 3259 |
for _ in range(10): |
| 3260 |
fut = loop.create_future() |
| 3261 |
loop.call_soon(fut.set_result, None) |
| 3262 |
loop.run_until_complete(fut) |
| 3263 |
if evt.is_set(): |
| 3264 |
break |
| 3265 |
xmpp_adapter.SlixmppAdapter._finalize_loop(loop) |
| 3266 |
assert evt.is_set() is True |
| 3267 |
|
| 3268 |
|
| 3269 |
def send_presence_boom() -> None: |
| 3270 |
raise RuntimeError("boom-presence") |
| 3271 |
|
| 3272 |
monkeypatch.setattr( |
| 3273 |
client, "send_presence", send_presence_boom, raising=True |
| 3274 |
) |
| 3275 |
|
| 3276 |
evt.clear() |
| 3277 |
client._on_session_start() |
| 3278 |
|
| 3279 |
for _ in range(10): |
| 3280 |
fut = loop.create_future() |
| 3281 |
loop.call_soon(fut.set_result, None) |
| 3282 |
loop.run_until_complete(fut) |
| 3283 |
if evt.is_set(): |
| 3284 |
break |
| 3285 |
xmpp_adapter.SlixmppAdapter._finalize_loop(loop) |
| 3286 |
assert evt.is_set() is True |
| 3287 |
|
| 3288 |
return None |
| 3289 |
|
| 3290 |
monkeypatch.setattr( |
| 3291 |
loop, "run_forever", run_forever_hook, raising=True |
| 3292 |
) |
| 3293 |
return loop |
| 3294 |
|
| 3295 |
monkeypatch.setattr( |
| 3296 |
xmpp_adapter.asyncio, |
| 3297 |
"new_event_loop", |
| 3298 |
new_event_loop_wrapped, |
| 3299 |
raising=True, |
| 3300 |
) |
| 3301 |
|
| 3302 |
a._keepalive_runner() |
| 3303 |
|
| 3304 |
|
| 3305 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3306 |
def test_adapter_keepalive_runner_plaintext_mode_skips_ssl_context( |
| 3307 |
monkeypatch: pytest.MonkeyPatch, |
| 3308 |
) -> None: |
| 3309 |
""" |
| 3310 |
Cover adapter.py - ensuring enable_plaintext=True, |
| 3311 |
so 'if not client.enable_plaintext' is False and ssl_context is not set. |
| 3312 |
""" |
| 3313 |
install_fake_slixmpp(monkeypatch) |
| 3314 |
|
| 3315 |
cfg = xmpp_adapter.XMPPConfig( |
| 3316 |
jid="[email protected]", |
| 3317 |
password="x", |
| 3318 |
host="example.com", |
| 3319 |
port=5222, |
| 3320 |
secure=xmpp_adapter.SecureXMPPMode.NONE, |
| 3321 |
verify_certificate=False, |
| 3322 |
) |
| 3323 |
|
| 3324 |
a = xmpp_adapter.SlixmppAdapter( |
| 3325 |
config=cfg, |
| 3326 |
targets=[], |
| 3327 |
subject="s", |
| 3328 |
body="b", |
| 3329 |
keepalive=True, |
| 3330 |
timeout=5.0, |
| 3331 |
) |
| 3332 |
|
| 3333 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 3334 |
|
| 3335 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 3336 |
loop = real_new_event_loop() |
| 3337 |
|
| 3338 |
def run_forever_hook() -> None: |
| 3339 |
|
| 3340 |
assert a._client is not None |
| 3341 |
|
| 3342 |
|
| 3343 |
|
| 3344 |
assert getattr(a._client, "enable_plaintext", None) is True |
| 3345 |
assert getattr(a._client, "ssl_context", None) is None |
| 3346 |
return None |
| 3347 |
|
| 3348 |
monkeypatch.setattr( |
| 3349 |
loop, "run_forever", run_forever_hook, raising=True |
| 3350 |
) |
| 3351 |
return loop |
| 3352 |
|
| 3353 |
monkeypatch.setattr( |
| 3354 |
xmpp_adapter.asyncio, |
| 3355 |
"new_event_loop", |
| 3356 |
new_event_loop_wrapped, |
| 3357 |
raising=True, |
| 3358 |
) |
| 3359 |
|
| 3360 |
a._keepalive_runner() |
| 3361 |
|
| 3362 |
|
| 3363 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3364 |
def test_adapter_keepalive_runner_finally_loop_none( |
| 3365 |
monkeypatch: pytest.MonkeyPatch, |
| 3366 |
) -> None: |
| 3367 |
""" |
| 3368 |
Cover adapter.py when loop is None (new_event_loop fails), |
| 3369 |
so both 'if loop is not None' conditions are False. |
| 3370 |
""" |
| 3371 |
install_fake_slixmpp(monkeypatch) |
| 3372 |
|
| 3373 |
monkeypatch.setattr( |
| 3374 |
xmpp_adapter.asyncio, |
| 3375 |
"new_event_loop", |
| 3376 |
lambda: (_ for _ in ()).throw(RuntimeError("boom")), |
| 3377 |
raising=True, |
| 3378 |
) |
| 3379 |
|
| 3380 |
cfg = xmpp_adapter.XMPPConfig( |
| 3381 |
jid="[email protected]", |
| 3382 |
password="x", |
| 3383 |
host="example.com", |
| 3384 |
port=5222, |
| 3385 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3386 |
verify_certificate=False, |
| 3387 |
) |
| 3388 |
a = xmpp_adapter.SlixmppAdapter( |
| 3389 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 3390 |
) |
| 3391 |
|
| 3392 |
|
| 3393 |
a._keepalive_runner() |
| 3394 |
|
| 3395 |
|
| 3396 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3397 |
def test_adapter_runner_suppresses_cleanup_errors( |
| 3398 |
monkeypatch: pytest.MonkeyPatch, |
| 3399 |
) -> None: |
| 3400 |
"""Ensure event-loop cleanup errors do not escape the runner.""" |
| 3401 |
install_fake_slixmpp(monkeypatch) |
| 3402 |
|
| 3403 |
cfg = xmpp_adapter.XMPPConfig( |
| 3404 |
jid="[email protected]", |
| 3405 |
password="x", |
| 3406 |
host="ex.com", |
| 3407 |
port=5222, |
| 3408 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3409 |
verify_certificate=False, |
| 3410 |
) |
| 3411 |
|
| 3412 |
a = xmpp_adapter.SlixmppAdapter( |
| 3413 |
config=cfg, |
| 3414 |
targets=[], |
| 3415 |
subject="s", |
| 3416 |
body="b", |
| 3417 |
keepalive=True, |
| 3418 |
roster=True, |
| 3419 |
timeout=5.0, |
| 3420 |
) |
| 3421 |
|
| 3422 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 3423 |
|
| 3424 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 3425 |
loop = real_new_event_loop() |
| 3426 |
|
| 3427 |
|
| 3428 |
def run_forever_boom() -> None: |
| 3429 |
raise RuntimeError("boom-forever") |
| 3430 |
|
| 3431 |
monkeypatch.setattr( |
| 3432 |
loop, "run_forever", run_forever_boom, raising=True |
| 3433 |
) |
| 3434 |
|
| 3435 |
|
| 3436 |
real_stop = loop.stop |
| 3437 |
real_close = loop.close |
| 3438 |
|
| 3439 |
def stop_boom() -> None: |
| 3440 |
real_stop() |
| 3441 |
raise RuntimeError("boom-stop") |
| 3442 |
|
| 3443 |
def close_boom() -> None: |
| 3444 |
real_close() |
| 3445 |
raise RuntimeError("boom-close") |
| 3446 |
|
| 3447 |
monkeypatch.setattr(loop, "stop", stop_boom, raising=True) |
| 3448 |
monkeypatch.setattr(loop, "close", close_boom, raising=True) |
| 3449 |
|
| 3450 |
return loop |
| 3451 |
|
| 3452 |
monkeypatch.setattr( |
| 3453 |
xmpp_adapter.asyncio, |
| 3454 |
"new_event_loop", |
| 3455 |
new_event_loop_wrapped, |
| 3456 |
raising=True, |
| 3457 |
) |
| 3458 |
|
| 3459 |
cfg = xmpp_adapter.XMPPConfig( |
| 3460 |
jid="[email protected]", |
| 3461 |
password="x", |
| 3462 |
host="example.com", |
| 3463 |
port=5222, |
| 3464 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3465 |
verify_certificate=False, |
| 3466 |
) |
| 3467 |
a = xmpp_adapter.SlixmppAdapter( |
| 3468 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 3469 |
) |
| 3470 |
|
| 3471 |
|
| 3472 |
a._keepalive_runner() |
| 3473 |
|
| 3474 |
|
| 3475 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3476 |
def test_adapter_send_keepalive_async_returns_false_when_client_none( |
| 3477 |
monkeypatch: pytest.MonkeyPatch, |
| 3478 |
) -> None: |
| 3479 |
"""Cover adapter.py _client is None -> return False.""" |
| 3480 |
install_fake_slixmpp(monkeypatch) |
| 3481 |
|
| 3482 |
cfg = xmpp_adapter.XMPPConfig( |
| 3483 |
jid="[email protected]", |
| 3484 |
password="x", |
| 3485 |
host="example.com", |
| 3486 |
port=5222, |
| 3487 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3488 |
verify_certificate=False, |
| 3489 |
) |
| 3490 |
a = xmpp_adapter.SlixmppAdapter( |
| 3491 |
config=cfg, |
| 3492 |
targets=[], |
| 3493 |
subject="s", |
| 3494 |
body="b", |
| 3495 |
keepalive=True, |
| 3496 |
) |
| 3497 |
|
| 3498 |
a._client = None |
| 3499 |
|
| 3500 |
loop = asyncio.new_event_loop() |
| 3501 |
try: |
| 3502 |
asyncio.set_event_loop(loop) |
| 3503 |
result = loop.run_until_complete( |
| 3504 |
a._send_keepalive_async(["t"], "s", "b") |
| 3505 |
) |
| 3506 |
assert result is False |
| 3507 |
|
| 3508 |
finally: |
| 3509 |
with contextlib.suppress(Exception): |
| 3510 |
asyncio.set_event_loop(None) |
| 3511 |
|
| 3512 |
|
| 3513 |
if not loop.is_closed(): |
| 3514 |
with contextlib.suppress(Exception): |
| 3515 |
ag_coro = loop.shutdown_asyncgens() |
| 3516 |
try: |
| 3517 |
loop.run_until_complete(ag_coro) |
| 3518 |
except Exception: |
| 3519 |
|
| 3520 |
|
| 3521 |
if hasattr(ag_coro, "close"): |
| 3522 |
ag_coro.close() |
| 3523 |
|
| 3524 |
|
| 3525 |
with contextlib.suppress(Exception): |
| 3526 |
loop.close() |
| 3527 |
|
| 3528 |
|
| 3529 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3530 |
def test_adapter_send_message_keepalive_exception_close_failure_suppressed( |
| 3531 |
monkeypatch: pytest.MonkeyPatch, |
| 3532 |
) -> None: |
| 3533 |
install_fake_slixmpp(monkeypatch) |
| 3534 |
|
| 3535 |
config = xmpp_adapter.XMPPConfig( |
| 3536 |
jid="[email protected]", |
| 3537 |
password="x", |
| 3538 |
host="example.com", |
| 3539 |
port=5222, |
| 3540 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3541 |
verify_certificate=False, |
| 3542 |
) |
| 3543 |
a = xmpp_adapter.SlixmppAdapter( |
| 3544 |
config=config, |
| 3545 |
targets=[], |
| 3546 |
subject="s", |
| 3547 |
body="b", |
| 3548 |
timeout=5.0, |
| 3549 |
keepalive=True, |
| 3550 |
) |
| 3551 |
|
| 3552 |
monkeypatch.setattr( |
| 3553 |
a, "_ensure_keepalive_worker", lambda: True, raising=True |
| 3554 |
) |
| 3555 |
|
| 3556 |
class _Loop: |
| 3557 |
def call_soon_threadsafe(self, cb: Any, *args: Any) -> None: |
| 3558 |
cb(*args) |
| 3559 |
|
| 3560 |
a._loop = _Loop() |
| 3561 |
|
| 3562 |
class _BadCoro: |
| 3563 |
def close(self) -> None: |
| 3564 |
raise RuntimeError("close-failed") |
| 3565 |
|
| 3566 |
monkeypatch.setattr( |
| 3567 |
a, "_send_keepalive_async", lambda **kwargs: _BadCoro(), raising=True |
| 3568 |
) |
| 3569 |
|
| 3570 |
def run_coroutine_threadsafe_raises(coro: Any, loop: Any) -> Any: |
| 3571 |
raise RuntimeError("schedule-failed") |
| 3572 |
|
| 3573 |
monkeypatch.setattr( |
| 3574 |
xmpp_adapter.asyncio, |
| 3575 |
"run_coroutine_threadsafe", |
| 3576 |
run_coroutine_threadsafe_raises, |
| 3577 |
raising=True, |
| 3578 |
) |
| 3579 |
|
| 3580 |
assert a.send_message(targets=[], subject="s", body="b") is False |
| 3581 |
|
| 3582 |
|
| 3583 |
def test_adapter_send_message_keepalive_false_none_params_does_not_override( |
| 3584 |
monkeypatch: pytest.MonkeyPatch, |
| 3585 |
) -> None: |
| 3586 |
install_fake_slixmpp(monkeypatch) |
| 3587 |
|
| 3588 |
config = xmpp_adapter.XMPPConfig( |
| 3589 |
jid="[email protected]", |
| 3590 |
password="x", |
| 3591 |
host="example.com", |
| 3592 |
port=5222, |
| 3593 |
secure=xmpp_adapter.SecureXMPPMode.NONE, |
| 3594 |
verify_certificate=False, |
| 3595 |
) |
| 3596 |
|
| 3597 |
a = xmpp_adapter.SlixmppAdapter( |
| 3598 |
config=config, |
| 3599 |
targets=[("chat", "[email protected]")], |
| 3600 |
subject="orig-subj", |
| 3601 |
body="orig-body", |
| 3602 |
keepalive=False, |
| 3603 |
) |
| 3604 |
|
| 3605 |
called = {"process": 0} |
| 3606 |
|
| 3607 |
def process() -> bool: |
| 3608 |
called["process"] += 1 |
| 3609 |
return True |
| 3610 |
|
| 3611 |
monkeypatch.setattr(a, "process", process, raising=True) |
| 3612 |
|
| 3613 |
assert a.send_message() is True |
| 3614 |
assert called["process"] == 1 |
| 3615 |
assert a.targets == [("chat", "[email protected]")] |
| 3616 |
assert a.subject == "orig-subj" |
| 3617 |
assert a.body == "orig-body" |
| 3618 |
|
| 3619 |
|
| 3620 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3621 |
def test_adapter_send_message_keepalive_worker_failure_returns_false( |
| 3622 |
monkeypatch: pytest.MonkeyPatch, |
| 3623 |
) -> None: |
| 3624 |
install_fake_slixmpp(monkeypatch) |
| 3625 |
|
| 3626 |
config = xmpp_adapter.XMPPConfig( |
| 3627 |
jid="[email protected]", |
| 3628 |
password="x", |
| 3629 |
host="example.com", |
| 3630 |
port=5222, |
| 3631 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3632 |
verify_certificate=False, |
| 3633 |
) |
| 3634 |
|
| 3635 |
a = xmpp_adapter.SlixmppAdapter( |
| 3636 |
config=config, |
| 3637 |
targets=[("chat", "[email protected]")], |
| 3638 |
subject="s", |
| 3639 |
body="b", |
| 3640 |
keepalive=True, |
| 3641 |
) |
| 3642 |
|
| 3643 |
monkeypatch.setattr( |
| 3644 |
a, "_ensure_keepalive_worker", lambda: False, raising=True |
| 3645 |
) |
| 3646 |
|
| 3647 |
assert a.send_message() is False |
| 3648 |
|
| 3649 |
|
| 3650 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3651 |
def test_adapter_send_message_keepalive_timeout_with_no_session_started_event( |
| 3652 |
monkeypatch: pytest.MonkeyPatch, |
| 3653 |
caplog: pytest.LogCaptureFixture, |
| 3654 |
) -> None: |
| 3655 |
install_fake_slixmpp(monkeypatch) |
| 3656 |
caplog.set_level(logging.WARNING, logger="apprise.xmpp") |
| 3657 |
|
| 3658 |
config = xmpp_adapter.XMPPConfig( |
| 3659 |
jid="[email protected]", |
| 3660 |
password="x", |
| 3661 |
host="example.com", |
| 3662 |
port=5222, |
| 3663 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3664 |
verify_certificate=False, |
| 3665 |
) |
| 3666 |
|
| 3667 |
a = xmpp_adapter.SlixmppAdapter( |
| 3668 |
config=config, |
| 3669 |
targets=[("chat", "[email protected]")], |
| 3670 |
subject="s", |
| 3671 |
body="b", |
| 3672 |
timeout=5.0, |
| 3673 |
keepalive=True, |
| 3674 |
) |
| 3675 |
|
| 3676 |
monkeypatch.setattr( |
| 3677 |
a, "_ensure_keepalive_worker", lambda: True, raising=True |
| 3678 |
) |
| 3679 |
|
| 3680 |
class _Loop: |
| 3681 |
def call_soon_threadsafe(self, cb: Any, *args: Any) -> None: |
| 3682 |
raise AssertionError( |
| 3683 |
"should not be called when _session_started is None" |
| 3684 |
) |
| 3685 |
|
| 3686 |
a._loop = _Loop() |
| 3687 |
a._session_started = None |
| 3688 |
|
| 3689 |
class _FutureTimeout: |
| 3690 |
def result(self, timeout: Optional[float] = None) -> Any: |
| 3691 |
raise xmpp_adapter.FuturesTimeoutError() |
| 3692 |
|
| 3693 |
def run_coroutine_threadsafe_timeout(coro: Any, loop: Any) -> Any: |
| 3694 |
|
| 3695 |
with contextlib.suppress(Exception): |
| 3696 |
coro.close() |
| 3697 |
return _FutureTimeout() |
| 3698 |
|
| 3699 |
monkeypatch.setattr( |
| 3700 |
xmpp_adapter.asyncio, |
| 3701 |
"run_coroutine_threadsafe", |
| 3702 |
run_coroutine_threadsafe_timeout, |
| 3703 |
raising=True, |
| 3704 |
) |
| 3705 |
|
| 3706 |
assert a.send_message() is False |
| 3707 |
assert "XMPP keepalive send timed out" in caplog.text |
| 3708 |
|
| 3709 |
|
| 3710 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3711 |
def test_client_session_start_keepalive_evt_none_no_set( |
| 3712 |
monkeypatch: pytest.MonkeyPatch, |
| 3713 |
) -> None: |
| 3714 |
"""Cover _session_start() when session event is None.""" |
| 3715 |
install_fake_slixmpp(monkeypatch) |
| 3716 |
|
| 3717 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 3718 |
|
| 3719 |
loop = asyncio.new_event_loop() |
| 3720 |
try: |
| 3721 |
asyncio.set_event_loop(loop) |
| 3722 |
client = client_cls( |
| 3723 |
jid="[email protected]", |
| 3724 |
password="pass", |
| 3725 |
oneshot=False, |
| 3726 |
targets=[], |
| 3727 |
subject="s", |
| 3728 |
body="b", |
| 3729 |
before_message=None, |
| 3730 |
want_roster=False, |
| 3731 |
roster_timeout=0.0, |
| 3732 |
session_started_evt=None, |
| 3733 |
) |
| 3734 |
|
| 3735 |
loop.run_until_complete(client._session_start()) |
| 3736 |
|
| 3737 |
finally: |
| 3738 |
with contextlib.suppress(Exception): |
| 3739 |
asyncio.set_event_loop(None) |
| 3740 |
loop.close() |
| 3741 |
|
| 3742 |
|
| 3743 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3744 |
def test_adapter_general_function( |
| 3745 |
monkeypatch: pytest.MonkeyPatch, |
| 3746 |
caplog: pytest.LogCaptureFixture, |
| 3747 |
) -> None: |
| 3748 |
install_fake_slixmpp(monkeypatch) |
| 3749 |
|
| 3750 |
def _close(): |
| 3751 |
raise Exception("boom") |
| 3752 |
|
| 3753 |
arg = {"close": _close} |
| 3754 |
|
| 3755 |
|
| 3756 |
xmpp_adapter._close_awaitable(None) |
| 3757 |
xmpp_adapter._close_awaitable(arg) |
| 3758 |
|
| 3759 |
|
| 3760 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3761 |
def test_client_on_session_start_create_task_failure_closes_coro( |
| 3762 |
monkeypatch: pytest.MonkeyPatch, |
| 3763 |
) -> None: |
| 3764 |
"""Cover loop.create_task() failure that closes the awaitable.""" |
| 3765 |
install_fake_slixmpp(monkeypatch) |
| 3766 |
|
| 3767 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 3768 |
|
| 3769 |
loop = asyncio.new_event_loop() |
| 3770 |
try: |
| 3771 |
asyncio.set_event_loop(loop) |
| 3772 |
|
| 3773 |
client = client_cls( |
| 3774 |
jid="[email protected]", |
| 3775 |
password="pass", |
| 3776 |
oneshot=False, |
| 3777 |
targets=[], |
| 3778 |
subject="s", |
| 3779 |
body="b", |
| 3780 |
before_message=None, |
| 3781 |
want_roster=False, |
| 3782 |
roster_timeout=0.0, |
| 3783 |
session_started_evt=asyncio.Event(), |
| 3784 |
) |
| 3785 |
|
| 3786 |
class _FakeCoro: |
| 3787 |
def __init__(self) -> None: |
| 3788 |
self.closed = False |
| 3789 |
|
| 3790 |
def close(self) -> None: |
| 3791 |
self.closed = True |
| 3792 |
|
| 3793 |
fake_coro = _FakeCoro() |
| 3794 |
|
| 3795 |
def _session_start(*args: object, **kwargs: object) -> _FakeCoro: |
| 3796 |
return fake_coro |
| 3797 |
|
| 3798 |
monkeypatch.setattr( |
| 3799 |
client, "_session_start", _session_start, raising=True |
| 3800 |
) |
| 3801 |
|
| 3802 |
class _FakeLoop: |
| 3803 |
def is_running(self) -> bool: |
| 3804 |
return True |
| 3805 |
|
| 3806 |
def create_task(self, coro: object) -> object: |
| 3807 |
raise RuntimeError("boom") |
| 3808 |
|
| 3809 |
client.loop = _FakeLoop() |
| 3810 |
client._on_session_start() |
| 3811 |
assert fake_coro.closed is True |
| 3812 |
|
| 3813 |
finally: |
| 3814 |
with contextlib.suppress(Exception): |
| 3815 |
asyncio.set_event_loop(None) |
| 3816 |
loop.close() |
| 3817 |
|
| 3818 |
|
| 3819 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3820 |
def test_client_disconnected_evt_none_no_clear( |
| 3821 |
monkeypatch: pytest.MonkeyPatch, |
| 3822 |
) -> None: |
| 3823 |
"""Cover _disconnected() branch where session event is None.""" |
| 3824 |
install_fake_slixmpp(monkeypatch) |
| 3825 |
|
| 3826 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 3827 |
|
| 3828 |
loop = asyncio.new_event_loop() |
| 3829 |
try: |
| 3830 |
asyncio.set_event_loop(loop) |
| 3831 |
client = client_cls( |
| 3832 |
jid="[email protected]", |
| 3833 |
password="pass", |
| 3834 |
oneshot=False, |
| 3835 |
targets=[], |
| 3836 |
subject="s", |
| 3837 |
body="b", |
| 3838 |
before_message=None, |
| 3839 |
want_roster=False, |
| 3840 |
roster_timeout=0.0, |
| 3841 |
session_started_evt=None, |
| 3842 |
) |
| 3843 |
|
| 3844 |
client._disconnected() |
| 3845 |
|
| 3846 |
finally: |
| 3847 |
with contextlib.suppress(Exception): |
| 3848 |
asyncio.set_event_loop(None) |
| 3849 |
loop.close() |
| 3850 |
|
| 3851 |
|
| 3852 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3853 |
def test_adapter_send_message_keepalive_loop_none_returns_false( |
| 3854 |
monkeypatch: pytest.MonkeyPatch, |
| 3855 |
) -> None: |
| 3856 |
"""Cover send_message(): loop is None -> return False.""" |
| 3857 |
install_fake_slixmpp(monkeypatch) |
| 3858 |
|
| 3859 |
cfg = xmpp_adapter.XMPPConfig( |
| 3860 |
jid="[email protected]", |
| 3861 |
password="x", |
| 3862 |
host="example.com", |
| 3863 |
port=5222, |
| 3864 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3865 |
verify_certificate=False, |
| 3866 |
) |
| 3867 |
a = xmpp_adapter.SlixmppAdapter( |
| 3868 |
config=cfg, |
| 3869 |
targets=[("chat", "[email protected]")], |
| 3870 |
subject="s", |
| 3871 |
body="b", |
| 3872 |
keepalive=True, |
| 3873 |
) |
| 3874 |
|
| 3875 |
monkeypatch.setattr( |
| 3876 |
a, "_ensure_keepalive_worker", lambda: True, raising=True |
| 3877 |
) |
| 3878 |
a._loop = None |
| 3879 |
assert a.send_message() is False |
| 3880 |
|
| 3881 |
|
| 3882 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3883 |
def test_adapter_send_message_keepalive_exception_returns_false( |
| 3884 |
monkeypatch: pytest.MonkeyPatch, |
| 3885 |
) -> None: |
| 3886 |
"""Cover send_message(): exception path returns False.""" |
| 3887 |
install_fake_slixmpp(monkeypatch) |
| 3888 |
|
| 3889 |
cfg = xmpp_adapter.XMPPConfig( |
| 3890 |
jid="[email protected]", |
| 3891 |
password="x", |
| 3892 |
host="example.com", |
| 3893 |
port=5222, |
| 3894 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3895 |
verify_certificate=False, |
| 3896 |
) |
| 3897 |
a = xmpp_adapter.SlixmppAdapter( |
| 3898 |
config=cfg, |
| 3899 |
targets=[("chat", "[email protected]")], |
| 3900 |
subject="s", |
| 3901 |
body="b", |
| 3902 |
timeout=5.0, |
| 3903 |
keepalive=True, |
| 3904 |
) |
| 3905 |
|
| 3906 |
monkeypatch.setattr( |
| 3907 |
a, "_ensure_keepalive_worker", lambda: True, raising=True |
| 3908 |
) |
| 3909 |
|
| 3910 |
class _Loop: |
| 3911 |
pass |
| 3912 |
|
| 3913 |
a._loop = _Loop() |
| 3914 |
|
| 3915 |
def run_coroutine_threadsafe_boom(coro: Any, loop: Any) -> Any: |
| 3916 |
with contextlib.suppress(Exception): |
| 3917 |
coro.close() |
| 3918 |
raise RuntimeError("boom") |
| 3919 |
|
| 3920 |
monkeypatch.setattr( |
| 3921 |
xmpp_adapter.asyncio, |
| 3922 |
"run_coroutine_threadsafe", |
| 3923 |
run_coroutine_threadsafe_boom, |
| 3924 |
raising=True, |
| 3925 |
) |
| 3926 |
|
| 3927 |
assert a.send_message() is False |
| 3928 |
|
| 3929 |
|
| 3930 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3931 |
def test_adapter_send_keepalive_async_auth_failed_returns_false( |
| 3932 |
monkeypatch: pytest.MonkeyPatch, |
| 3933 |
) -> None: |
| 3934 |
"""Cover _send_keepalive_async(): auth_failed -> return False.""" |
| 3935 |
install_fake_slixmpp(monkeypatch) |
| 3936 |
|
| 3937 |
cfg = xmpp_adapter.XMPPConfig( |
| 3938 |
jid="[email protected]", |
| 3939 |
password="x", |
| 3940 |
host="example.com", |
| 3941 |
port=5222, |
| 3942 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3943 |
verify_certificate=False, |
| 3944 |
) |
| 3945 |
a = xmpp_adapter.SlixmppAdapter( |
| 3946 |
config=cfg, |
| 3947 |
targets=[], |
| 3948 |
subject="s", |
| 3949 |
body="b", |
| 3950 |
keepalive=True, |
| 3951 |
) |
| 3952 |
|
| 3953 |
class _Client: |
| 3954 |
_auth_failed = True |
| 3955 |
|
| 3956 |
def send_message(self, **kwargs: Any) -> None: |
| 3957 |
raise AssertionError("send must not occur") |
| 3958 |
|
| 3959 |
a._client = _Client() |
| 3960 |
|
| 3961 |
async def ok_connect() -> bool: |
| 3962 |
return True |
| 3963 |
|
| 3964 |
monkeypatch.setattr(a, "_connect_if_required", ok_connect, raising=True) |
| 3965 |
|
| 3966 |
assert ( |
| 3967 |
asyncio.run(a._send_keepalive_async(["[email protected]"], "s", "b")) |
| 3968 |
is False |
| 3969 |
) |
| 3970 |
|
| 3971 |
|
| 3972 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 3973 |
def test_adapter_connect_if_required_auth_failed_returns_false( |
| 3974 |
monkeypatch: pytest.MonkeyPatch, |
| 3975 |
) -> None: |
| 3976 |
"""Cover _connect_if_required(): auth_failed -> return False.""" |
| 3977 |
install_fake_slixmpp(monkeypatch) |
| 3978 |
|
| 3979 |
cfg = xmpp_adapter.XMPPConfig( |
| 3980 |
jid="[email protected]", |
| 3981 |
password="x", |
| 3982 |
host="example.com", |
| 3983 |
port=5222, |
| 3984 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 3985 |
verify_certificate=False, |
| 3986 |
) |
| 3987 |
a = xmpp_adapter.SlixmppAdapter( |
| 3988 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 3989 |
) |
| 3990 |
|
| 3991 |
loop = asyncio.new_event_loop() |
| 3992 |
try: |
| 3993 |
asyncio.set_event_loop(loop) |
| 3994 |
a._loop = loop |
| 3995 |
a._connect_lock = asyncio.Lock() |
| 3996 |
a._session_started = asyncio.Event() |
| 3997 |
|
| 3998 |
class _Client: |
| 3999 |
_auth_failed = True |
| 4000 |
|
| 4001 |
a._client = _Client() |
| 4002 |
|
| 4003 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 4004 |
|
| 4005 |
finally: |
| 4006 |
with contextlib.suppress(Exception): |
| 4007 |
asyncio.set_event_loop(None) |
| 4008 |
loop.close() |
| 4009 |
|
| 4010 |
|
| 4011 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4012 |
def test_adapter_connect_if_required_connect_ok_false_disconnects( |
| 4013 |
monkeypatch: pytest.MonkeyPatch, |
| 4014 |
caplog: pytest.LogCaptureFixture, |
| 4015 |
) -> None: |
| 4016 |
"""Cover _connect_if_required(): connect_ok False path.""" |
| 4017 |
install_fake_slixmpp(monkeypatch) |
| 4018 |
caplog.set_level(logging.WARNING, logger="apprise.xmpp") |
| 4019 |
|
| 4020 |
cfg = xmpp_adapter.XMPPConfig( |
| 4021 |
jid="[email protected]", |
| 4022 |
password="x", |
| 4023 |
host="example.com", |
| 4024 |
port=5222, |
| 4025 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 4026 |
verify_certificate=False, |
| 4027 |
) |
| 4028 |
a = xmpp_adapter.SlixmppAdapter( |
| 4029 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 4030 |
) |
| 4031 |
|
| 4032 |
loop = asyncio.new_event_loop() |
| 4033 |
try: |
| 4034 |
asyncio.set_event_loop(loop) |
| 4035 |
a._loop = loop |
| 4036 |
a._connect_lock = asyncio.Lock() |
| 4037 |
a._session_started = asyncio.Event() |
| 4038 |
|
| 4039 |
disconnected = {"n": 0} |
| 4040 |
|
| 4041 |
class _Client: |
| 4042 |
def connect(self, **kwargs: Any) -> Any: |
| 4043 |
fut = loop.create_future() |
| 4044 |
fut.set_result(False) |
| 4045 |
return fut |
| 4046 |
|
| 4047 |
def disconnect(self) -> None: |
| 4048 |
disconnected["n"] += 1 |
| 4049 |
|
| 4050 |
a._client = _Client() |
| 4051 |
|
| 4052 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 4053 |
assert disconnected["n"] == 1 |
| 4054 |
assert "XMPP connect failed" in caplog.text |
| 4055 |
|
| 4056 |
finally: |
| 4057 |
with contextlib.suppress(Exception): |
| 4058 |
asyncio.set_event_loop(None) |
| 4059 |
loop.close() |
| 4060 |
|
| 4061 |
|
| 4062 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4063 |
def test_adapter_connect_if_required_session_wait_exception_closes( |
| 4064 |
monkeypatch: pytest.MonkeyPatch, |
| 4065 |
) -> None: |
| 4066 |
"""Cover _connect_if_required(): session_wait Exception path.""" |
| 4067 |
install_fake_slixmpp(monkeypatch) |
| 4068 |
|
| 4069 |
cfg = xmpp_adapter.XMPPConfig( |
| 4070 |
jid="[email protected]", |
| 4071 |
password="x", |
| 4072 |
host="example.com", |
| 4073 |
port=5222, |
| 4074 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 4075 |
verify_certificate=False, |
| 4076 |
) |
| 4077 |
a = xmpp_adapter.SlixmppAdapter( |
| 4078 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 4079 |
) |
| 4080 |
|
| 4081 |
loop = asyncio.new_event_loop() |
| 4082 |
try: |
| 4083 |
asyncio.set_event_loop(loop) |
| 4084 |
a._loop = loop |
| 4085 |
a._connect_lock = asyncio.Lock() |
| 4086 |
a._session_started = asyncio.Event() |
| 4087 |
|
| 4088 |
fut = loop.create_future() |
| 4089 |
fut.set_result(True) |
| 4090 |
|
| 4091 |
class _Client: |
| 4092 |
def connect(self, **kwargs: Any) -> Any: |
| 4093 |
return fut |
| 4094 |
|
| 4095 |
a._client = _Client() |
| 4096 |
|
| 4097 |
called: list[object] = [] |
| 4098 |
real_close = xmpp_adapter._close_awaitable |
| 4099 |
|
| 4100 |
def close_spy(obj: object) -> None: |
| 4101 |
called.append(obj) |
| 4102 |
real_close(obj) |
| 4103 |
|
| 4104 |
monkeypatch.setattr( |
| 4105 |
xmpp_adapter, "_close_awaitable", close_spy, raising=True |
| 4106 |
) |
| 4107 |
|
| 4108 |
real_wait_for = xmpp_adapter.asyncio.wait_for |
| 4109 |
calls = {"n": 0} |
| 4110 |
|
| 4111 |
async def wait_for_patched( |
| 4112 |
aw: Any, timeout: Optional[float] = None |
| 4113 |
) -> Any: |
| 4114 |
calls["n"] += 1 |
| 4115 |
if calls["n"] == 2: |
| 4116 |
raise RuntimeError("boom") |
| 4117 |
return await real_wait_for(aw, timeout=timeout) |
| 4118 |
|
| 4119 |
monkeypatch.setattr( |
| 4120 |
xmpp_adapter.asyncio, "wait_for", wait_for_patched, raising=True |
| 4121 |
) |
| 4122 |
|
| 4123 |
assert run_on_loop(loop, a._connect_if_required()) is False |
| 4124 |
assert len(called) == 1 |
| 4125 |
|
| 4126 |
finally: |
| 4127 |
with contextlib.suppress(Exception): |
| 4128 |
asyncio.set_event_loop(None) |
| 4129 |
loop.close() |
| 4130 |
|
| 4131 |
|
| 4132 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4133 |
def test_client_on_session_start_add_done_callback_executes( |
| 4134 |
monkeypatch: pytest.MonkeyPatch, |
| 4135 |
) -> None: |
| 4136 |
""" |
| 4137 |
Cover adapter.py _on_session_start() keepalive path where we create a |
| 4138 |
task and attach a done callback that calls t.exception() when not |
| 4139 |
cancelled. |
| 4140 |
|
| 4141 |
This test avoids patching asyncio.Task (immutable on newer Pythons) by |
| 4142 |
using a fake loop and fake task object. |
| 4143 |
""" |
| 4144 |
install_fake_slixmpp(monkeypatch) |
| 4145 |
|
| 4146 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 4147 |
|
| 4148 |
loop = asyncio.new_event_loop() |
| 4149 |
try: |
| 4150 |
asyncio.set_event_loop(loop) |
| 4151 |
|
| 4152 |
evt = asyncio.Event() |
| 4153 |
|
| 4154 |
client = client_cls( |
| 4155 |
jid="[email protected]", |
| 4156 |
password="x", |
| 4157 |
oneshot=False, |
| 4158 |
targets=[], |
| 4159 |
subject="s", |
| 4160 |
body="b", |
| 4161 |
before_message=None, |
| 4162 |
want_roster=False, |
| 4163 |
roster_timeout=0.0, |
| 4164 |
session_started_evt=evt, |
| 4165 |
) |
| 4166 |
|
| 4167 |
class _FakeTask: |
| 4168 |
def __init__(self) -> None: |
| 4169 |
self.exception_called = False |
| 4170 |
self.callback_called = False |
| 4171 |
|
| 4172 |
def add_done_callback(self, cb: Any) -> None: |
| 4173 |
self.callback_called = True |
| 4174 |
cb(self) |
| 4175 |
|
| 4176 |
def cancelled(self) -> bool: |
| 4177 |
return False |
| 4178 |
|
| 4179 |
def exception(self) -> None: |
| 4180 |
self.exception_called = True |
| 4181 |
return None |
| 4182 |
|
| 4183 |
class _FakeLoop: |
| 4184 |
def __init__(self) -> None: |
| 4185 |
self.task = _FakeTask() |
| 4186 |
self.create_task_called = False |
| 4187 |
|
| 4188 |
def is_running(self) -> bool: |
| 4189 |
return True |
| 4190 |
|
| 4191 |
def create_task(self, coro: Any) -> _FakeTask: |
| 4192 |
self.create_task_called = True |
| 4193 |
return self.task |
| 4194 |
|
| 4195 |
fake_loop = _FakeLoop() |
| 4196 |
client.loop = fake_loop |
| 4197 |
|
| 4198 |
class _FakeCoro: |
| 4199 |
def close(self) -> None: |
| 4200 |
return None |
| 4201 |
|
| 4202 |
def _session_start(*args: object, **kwargs: object) -> _FakeCoro: |
| 4203 |
return _FakeCoro() |
| 4204 |
|
| 4205 |
monkeypatch.setattr( |
| 4206 |
client, "_session_start", _session_start, raising=True |
| 4207 |
) |
| 4208 |
|
| 4209 |
client._on_session_start() |
| 4210 |
|
| 4211 |
assert fake_loop.create_task_called is True |
| 4212 |
assert fake_loop.task.callback_called is True |
| 4213 |
assert fake_loop.task.exception_called is True |
| 4214 |
|
| 4215 |
finally: |
| 4216 |
with contextlib.suppress(Exception): |
| 4217 |
asyncio.set_event_loop(None) |
| 4218 |
with contextlib.suppress(Exception): |
| 4219 |
loop.close() |
| 4220 |
|
| 4221 |
|
| 4222 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4223 |
def test_client_on_session_start_done_callback_cancelled_returns( |
| 4224 |
monkeypatch: pytest.MonkeyPatch, |
| 4225 |
) -> None: |
| 4226 |
"""Cover _log_task() early return when task is cancelled.""" |
| 4227 |
install_fake_slixmpp(monkeypatch) |
| 4228 |
|
| 4229 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 4230 |
|
| 4231 |
loop = asyncio.new_event_loop() |
| 4232 |
try: |
| 4233 |
asyncio.set_event_loop(loop) |
| 4234 |
|
| 4235 |
evt = asyncio.Event() |
| 4236 |
client = client_cls( |
| 4237 |
jid="[email protected]", |
| 4238 |
password="x", |
| 4239 |
oneshot=False, |
| 4240 |
targets=[], |
| 4241 |
subject="s", |
| 4242 |
body="b", |
| 4243 |
before_message=None, |
| 4244 |
want_roster=False, |
| 4245 |
roster_timeout=0.0, |
| 4246 |
session_started_evt=evt, |
| 4247 |
) |
| 4248 |
|
| 4249 |
errors: list[str] = [] |
| 4250 |
|
| 4251 |
class _Logger: |
| 4252 |
def error(self, msg: str, *args: Any) -> None: |
| 4253 |
errors.append(msg % args if args else msg) |
| 4254 |
|
| 4255 |
client.logger = _Logger() |
| 4256 |
|
| 4257 |
class _FakeTask: |
| 4258 |
def __init__(self) -> None: |
| 4259 |
self.exception_called = False |
| 4260 |
|
| 4261 |
def add_done_callback(self, cb: Any) -> None: |
| 4262 |
cb(self) |
| 4263 |
|
| 4264 |
def cancelled(self) -> bool: |
| 4265 |
return True |
| 4266 |
|
| 4267 |
def exception(self) -> None: |
| 4268 |
self.exception_called = True |
| 4269 |
return RuntimeError("should-not-be-read") |
| 4270 |
|
| 4271 |
class _FakeLoop: |
| 4272 |
def is_running(self) -> bool: |
| 4273 |
return True |
| 4274 |
|
| 4275 |
def create_task(self, coro: Any) -> _FakeTask: |
| 4276 |
return _FakeTask() |
| 4277 |
|
| 4278 |
client.loop = _FakeLoop() |
| 4279 |
|
| 4280 |
class _FakeCoro: |
| 4281 |
def close(self) -> None: |
| 4282 |
return None |
| 4283 |
|
| 4284 |
monkeypatch.setattr( |
| 4285 |
client, |
| 4286 |
"_session_start", |
| 4287 |
lambda *_a, **_k: _FakeCoro(), |
| 4288 |
raising=True, |
| 4289 |
) |
| 4290 |
|
| 4291 |
client._on_session_start() |
| 4292 |
|
| 4293 |
assert errors == [] |
| 4294 |
|
| 4295 |
finally: |
| 4296 |
with contextlib.suppress(Exception): |
| 4297 |
asyncio.set_event_loop(None) |
| 4298 |
with contextlib.suppress(Exception): |
| 4299 |
loop.close() |
| 4300 |
|
| 4301 |
|
| 4302 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4303 |
def test_client_on_session_start_done_callback_logs_exception( |
| 4304 |
monkeypatch: pytest.MonkeyPatch, |
| 4305 |
) -> None: |
| 4306 |
"""Cover _log_task() error log when task.exception() is not None.""" |
| 4307 |
install_fake_slixmpp(monkeypatch) |
| 4308 |
|
| 4309 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 4310 |
|
| 4311 |
loop = asyncio.new_event_loop() |
| 4312 |
try: |
| 4313 |
asyncio.set_event_loop(loop) |
| 4314 |
|
| 4315 |
evt = asyncio.Event() |
| 4316 |
client = client_cls( |
| 4317 |
jid="[email protected]", |
| 4318 |
password="x", |
| 4319 |
oneshot=False, |
| 4320 |
targets=[], |
| 4321 |
subject="s", |
| 4322 |
body="b", |
| 4323 |
before_message=None, |
| 4324 |
want_roster=False, |
| 4325 |
roster_timeout=0.0, |
| 4326 |
session_started_evt=evt, |
| 4327 |
) |
| 4328 |
|
| 4329 |
errors: list[str] = [] |
| 4330 |
|
| 4331 |
class _Logger: |
| 4332 |
def error(self, msg: str, *args: Any) -> None: |
| 4333 |
errors.append(msg % args if args else msg) |
| 4334 |
|
| 4335 |
client.logger = _Logger() |
| 4336 |
|
| 4337 |
class _FakeTask: |
| 4338 |
def add_done_callback(self, cb: Any) -> None: |
| 4339 |
cb(self) |
| 4340 |
|
| 4341 |
def cancelled(self) -> bool: |
| 4342 |
return False |
| 4343 |
|
| 4344 |
def exception(self) -> Exception: |
| 4345 |
return RuntimeError("boom-task") |
| 4346 |
|
| 4347 |
class _FakeLoop: |
| 4348 |
def is_running(self) -> bool: |
| 4349 |
return True |
| 4350 |
|
| 4351 |
def create_task(self, coro: Any) -> _FakeTask: |
| 4352 |
return _FakeTask() |
| 4353 |
|
| 4354 |
client.loop = _FakeLoop() |
| 4355 |
|
| 4356 |
class _FakeCoro: |
| 4357 |
def close(self) -> None: |
| 4358 |
return None |
| 4359 |
|
| 4360 |
monkeypatch.setattr( |
| 4361 |
client, |
| 4362 |
"_session_start", |
| 4363 |
lambda *_a, **_k: _FakeCoro(), |
| 4364 |
raising=True, |
| 4365 |
) |
| 4366 |
|
| 4367 |
client._on_session_start() |
| 4368 |
|
| 4369 |
assert len(errors) == 1 |
| 4370 |
assert "XMPP task failed" in errors[0] |
| 4371 |
|
| 4372 |
finally: |
| 4373 |
with contextlib.suppress(Exception): |
| 4374 |
asyncio.set_event_loop(None) |
| 4375 |
with contextlib.suppress(Exception): |
| 4376 |
loop.close() |
| 4377 |
|
| 4378 |
|
| 4379 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4380 |
def test_xmpp_muc_url_parsing_and_reconstruction() -> None: |
| 4381 |
"""MUC targets parse from '#[email protected]' and round-trip via url().""" |
| 4382 |
n = NotifyXMPP( |
| 4383 |
host="example.com", |
| 4384 |
user="me", |
| 4385 |
password="secret", |
| 4386 |
targets=["#[email protected]", "[email protected]"], |
| 4387 |
) |
| 4388 |
|
| 4389 |
assert ("groupchat", "[email protected]") in n.targets |
| 4390 |
assert ("chat", "[email protected]") in n.targets |
| 4391 |
assert n.want_muc is True |
| 4392 |
|
| 4393 |
|
| 4394 |
u = n.url() |
| 4395 |
assert "%23room%40conference.example.com" in u |
| 4396 |
|
| 4397 |
results = NotifyXMPP.parse_url(u) |
| 4398 |
assert results is not None |
| 4399 |
n2 = NotifyXMPP(**results) |
| 4400 |
assert ("groupchat", "[email protected]") in n2.targets |
| 4401 |
assert ("chat", "[email protected]") in n2.targets |
| 4402 |
assert n2.want_muc is True |
| 4403 |
|
| 4404 |
|
| 4405 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4406 |
def test_xmpp_name_parameter() -> None: |
| 4407 |
"""name= is stored, emitted in url(), and round-trips via parse_url().""" |
| 4408 |
|
| 4409 |
n = NotifyXMPP( |
| 4410 |
host="example.com", |
| 4411 |
user="me", |
| 4412 |
password="secret", |
| 4413 |
name="my_bot", |
| 4414 |
) |
| 4415 |
assert n.name == "my_bot" |
| 4416 |
u = n.url() |
| 4417 |
assert "name=my_bot" in u |
| 4418 |
|
| 4419 |
|
| 4420 |
results = NotifyXMPP.parse_url(u) |
| 4421 |
assert results is not None |
| 4422 |
assert results.get("name") == "my_bot" |
| 4423 |
n2 = NotifyXMPP(**results) |
| 4424 |
assert n2.name == "my_bot" |
| 4425 |
|
| 4426 |
|
| 4427 |
n3 = NotifyXMPP( |
| 4428 |
host="example.com", |
| 4429 |
user="me", |
| 4430 |
password="secret", |
| 4431 |
name="bad name!", |
| 4432 |
) |
| 4433 |
assert n3.name == n3.user |
| 4434 |
|
| 4435 |
|
| 4436 |
|
| 4437 |
n4 = NotifyXMPP( |
| 4438 |
host="example.com", |
| 4439 |
user="me", |
| 4440 |
password="secret", |
| 4441 |
) |
| 4442 |
assert n4.name == n4.user |
| 4443 |
assert "name=" not in n4.url() |
| 4444 |
|
| 4445 |
|
| 4446 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4447 |
def test_xmpp_muc_only_no_regular_targets() -> None: |
| 4448 |
"""NotifyXMPP with only MUC targets sets want_muc=True.""" |
| 4449 |
n = NotifyXMPP( |
| 4450 |
host="example.com", |
| 4451 |
user="me", |
| 4452 |
password="secret", |
| 4453 |
targets=["#[email protected]"], |
| 4454 |
) |
| 4455 |
assert n.want_muc is True |
| 4456 |
assert len(n.targets) == 1 |
| 4457 |
assert n.targets[0] == ("groupchat", "[email protected]") |
| 4458 |
|
| 4459 |
|
| 4460 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4461 |
def test_client_session_start_oneshot_muc_join( |
| 4462 |
monkeypatch: pytest.MonkeyPatch, |
| 4463 |
) -> None: |
| 4464 |
"""Cover join_muc() in _session_start() for oneshot groupchat target.""" |
| 4465 |
install_fake_slixmpp(monkeypatch) |
| 4466 |
|
| 4467 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 4468 |
|
| 4469 |
sent: list[dict[str, Any]] = [] |
| 4470 |
join_calls: list[tuple[str, str]] = [] |
| 4471 |
|
| 4472 |
async def fake_join_muc(room: str, nick: str) -> None: |
| 4473 |
join_calls.append((room, nick)) |
| 4474 |
|
| 4475 |
class FakeMucPlugin: |
| 4476 |
def join_muc(self, room: str, nick: str, **kw: Any) -> Any: |
| 4477 |
return fake_join_muc(room, nick) |
| 4478 |
|
| 4479 |
loop = asyncio.new_event_loop() |
| 4480 |
asyncio.set_event_loop(loop) |
| 4481 |
try: |
| 4482 |
client = client_cls( |
| 4483 |
jid="[email protected]", |
| 4484 |
password="pass", |
| 4485 |
oneshot=True, |
| 4486 |
targets=[("groupchat", "[email protected]")], |
| 4487 |
subject="s", |
| 4488 |
body="b", |
| 4489 |
want_muc=True, |
| 4490 |
want_roster=False, |
| 4491 |
roster_timeout=0.0, |
| 4492 |
session_started_evt=None, |
| 4493 |
) |
| 4494 |
|
| 4495 |
|
| 4496 |
class _BoundJID: |
| 4497 |
user = "me" |
| 4498 |
|
| 4499 |
monkeypatch.setattr(client, "boundjid", _BoundJID(), raising=False) |
| 4500 |
monkeypatch.setattr( |
| 4501 |
client, "plugin", {"xep_0045": FakeMucPlugin()}, raising=False |
| 4502 |
) |
| 4503 |
monkeypatch.setattr( |
| 4504 |
client, "send_message", lambda **kw: sent.append(kw), raising=True |
| 4505 |
) |
| 4506 |
monkeypatch.setattr(client, "disconnect", lambda: None, raising=True) |
| 4507 |
|
| 4508 |
loop.run_until_complete(client._session_start()) |
| 4509 |
|
| 4510 |
assert len(join_calls) == 1 |
| 4511 |
assert join_calls[0][0] == "[email protected]" |
| 4512 |
assert join_calls[0][1] == "me" |
| 4513 |
assert len(sent) == 1 |
| 4514 |
assert sent[0]["mtype"] == "groupchat" |
| 4515 |
|
| 4516 |
finally: |
| 4517 |
asyncio.set_event_loop(None) |
| 4518 |
loop.close() |
| 4519 |
|
| 4520 |
|
| 4521 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4522 |
def test_client_session_start_oneshot_muc_join_error( |
| 4523 |
monkeypatch: pytest.MonkeyPatch, |
| 4524 |
) -> None: |
| 4525 |
"""Cover join_muc() error path: exception is caught, send still happens.""" |
| 4526 |
install_fake_slixmpp(monkeypatch) |
| 4527 |
|
| 4528 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 4529 |
|
| 4530 |
sent: list[dict[str, Any]] = [] |
| 4531 |
|
| 4532 |
async def join_muc_fail(room: str, nick: str) -> None: |
| 4533 |
raise asyncio.TimeoutError() |
| 4534 |
|
| 4535 |
class FakeMucPlugin: |
| 4536 |
def join_muc(self, room: str, nick: str, **kw: Any) -> Any: |
| 4537 |
return join_muc_fail(room, nick) |
| 4538 |
|
| 4539 |
loop = asyncio.new_event_loop() |
| 4540 |
asyncio.set_event_loop(loop) |
| 4541 |
try: |
| 4542 |
client = client_cls( |
| 4543 |
jid="[email protected]", |
| 4544 |
password="pass", |
| 4545 |
oneshot=True, |
| 4546 |
targets=[("groupchat", "[email protected]")], |
| 4547 |
subject="s", |
| 4548 |
body="b", |
| 4549 |
want_muc=True, |
| 4550 |
want_roster=False, |
| 4551 |
roster_timeout=0.0, |
| 4552 |
session_started_evt=None, |
| 4553 |
) |
| 4554 |
|
| 4555 |
class _BoundJID: |
| 4556 |
user = "me" |
| 4557 |
|
| 4558 |
monkeypatch.setattr(client, "boundjid", _BoundJID(), raising=False) |
| 4559 |
monkeypatch.setattr( |
| 4560 |
client, "plugin", {"xep_0045": FakeMucPlugin()}, raising=False |
| 4561 |
) |
| 4562 |
monkeypatch.setattr( |
| 4563 |
client, "send_message", lambda **kw: sent.append(kw), raising=True |
| 4564 |
) |
| 4565 |
monkeypatch.setattr(client, "disconnect", lambda: None, raising=True) |
| 4566 |
|
| 4567 |
loop.run_until_complete(client._session_start()) |
| 4568 |
|
| 4569 |
|
| 4570 |
assert len(sent) == 1 |
| 4571 |
assert sent[0]["mtype"] == "groupchat" |
| 4572 |
|
| 4573 |
finally: |
| 4574 |
asyncio.set_event_loop(None) |
| 4575 |
loop.close() |
| 4576 |
|
| 4577 |
|
| 4578 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4579 |
def test_send_keepalive_async_muc_join( |
| 4580 |
monkeypatch: pytest.MonkeyPatch, |
| 4581 |
) -> None: |
| 4582 |
"""Cover join_muc() in _send_keepalive_async() for groupchat target.""" |
| 4583 |
install_fake_slixmpp(monkeypatch) |
| 4584 |
|
| 4585 |
cfg = xmpp_adapter.XMPPConfig( |
| 4586 |
jid="[email protected]", |
| 4587 |
password="x", |
| 4588 |
host="example.com", |
| 4589 |
port=5222, |
| 4590 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 4591 |
verify_certificate=False, |
| 4592 |
) |
| 4593 |
a = xmpp_adapter.SlixmppAdapter( |
| 4594 |
config=cfg, |
| 4595 |
targets=[("groupchat", "[email protected]")], |
| 4596 |
subject="s", |
| 4597 |
body="b", |
| 4598 |
keepalive=True, |
| 4599 |
want_muc=True, |
| 4600 |
) |
| 4601 |
|
| 4602 |
join_calls: list[tuple[str, str]] = [] |
| 4603 |
sent: list[dict[str, Any]] = [] |
| 4604 |
|
| 4605 |
async def fake_join_muc(room: str, nick: str) -> None: |
| 4606 |
join_calls.append((room, nick)) |
| 4607 |
|
| 4608 |
class FakeBoundJID: |
| 4609 |
user = "me" |
| 4610 |
|
| 4611 |
class FakeMucPlugin: |
| 4612 |
def join_muc(self, room: str, nick: str, **kw: Any) -> Any: |
| 4613 |
return fake_join_muc(room, nick) |
| 4614 |
|
| 4615 |
class _Client: |
| 4616 |
_auth_failed = False |
| 4617 |
boundjid = FakeBoundJID() |
| 4618 |
plugin = {"xep_0045": FakeMucPlugin()} |
| 4619 |
|
| 4620 |
def send_message(self, **kwargs: Any) -> None: |
| 4621 |
sent.append(kwargs) |
| 4622 |
|
| 4623 |
a._client = _Client() |
| 4624 |
|
| 4625 |
async def ok_connect() -> bool: |
| 4626 |
return True |
| 4627 |
|
| 4628 |
monkeypatch.setattr(a, "_connect_if_required", ok_connect, raising=True) |
| 4629 |
|
| 4630 |
result = asyncio.run( |
| 4631 |
a._send_keepalive_async( |
| 4632 |
targets=[("groupchat", "[email protected]")], |
| 4633 |
subject="s", |
| 4634 |
body="b", |
| 4635 |
) |
| 4636 |
) |
| 4637 |
|
| 4638 |
assert result is True |
| 4639 |
assert len(join_calls) == 1 |
| 4640 |
assert join_calls[0][0] == "[email protected]" |
| 4641 |
|
| 4642 |
assert join_calls[0][1] == "me" |
| 4643 |
assert len(sent) == 1 |
| 4644 |
assert sent[0]["mtype"] == "groupchat" |
| 4645 |
|
| 4646 |
|
| 4647 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4648 |
def test_send_keepalive_async_muc_join_nick_fallback( |
| 4649 |
monkeypatch: pytest.MonkeyPatch, |
| 4650 |
) -> None: |
| 4651 |
"""When boundjid.user is empty, nick falls back to adapter nickname.""" |
| 4652 |
install_fake_slixmpp(monkeypatch) |
| 4653 |
|
| 4654 |
cfg = xmpp_adapter.XMPPConfig( |
| 4655 |
jid="[email protected]", |
| 4656 |
password="x", |
| 4657 |
host="example.com", |
| 4658 |
port=5222, |
| 4659 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 4660 |
verify_certificate=False, |
| 4661 |
) |
| 4662 |
a = xmpp_adapter.SlixmppAdapter( |
| 4663 |
config=cfg, |
| 4664 |
targets=[("groupchat", "[email protected]")], |
| 4665 |
subject="s", |
| 4666 |
body="b", |
| 4667 |
keepalive=True, |
| 4668 |
want_muc=True, |
| 4669 |
default_nickname="mybot", |
| 4670 |
) |
| 4671 |
|
| 4672 |
join_calls: list[tuple[str, str]] = [] |
| 4673 |
sent: list[dict[str, Any]] = [] |
| 4674 |
|
| 4675 |
async def fake_join_muc(room: str, nick: str) -> None: |
| 4676 |
join_calls.append((room, nick)) |
| 4677 |
|
| 4678 |
class FakeBoundJID: |
| 4679 |
user = "" |
| 4680 |
|
| 4681 |
class FakeMucPlugin: |
| 4682 |
def join_muc(self, room: str, nick: str, **kw: Any) -> Any: |
| 4683 |
return fake_join_muc(room, nick) |
| 4684 |
|
| 4685 |
class _Client: |
| 4686 |
_auth_failed = False |
| 4687 |
boundjid = FakeBoundJID() |
| 4688 |
plugin = {"xep_0045": FakeMucPlugin()} |
| 4689 |
|
| 4690 |
def send_message(self, **kwargs: Any) -> None: |
| 4691 |
sent.append(kwargs) |
| 4692 |
|
| 4693 |
a._client = _Client() |
| 4694 |
|
| 4695 |
async def ok_connect() -> bool: |
| 4696 |
return True |
| 4697 |
|
| 4698 |
monkeypatch.setattr(a, "_connect_if_required", ok_connect, raising=True) |
| 4699 |
|
| 4700 |
result = asyncio.run( |
| 4701 |
a._send_keepalive_async( |
| 4702 |
targets=[("groupchat", "[email protected]")], |
| 4703 |
subject="s", |
| 4704 |
body="b", |
| 4705 |
) |
| 4706 |
) |
| 4707 |
|
| 4708 |
assert result is True |
| 4709 |
assert len(join_calls) == 1 |
| 4710 |
assert join_calls[0][0] == "[email protected]" |
| 4711 |
|
| 4712 |
assert join_calls[0][1] == "mybot" |
| 4713 |
assert len(sent) == 1 |
| 4714 |
assert sent[0]["mtype"] == "groupchat" |
| 4715 |
|
| 4716 |
|
| 4717 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4718 |
def test_adapter_keepalive_runner_registers_xep_0045_when_want_muc( |
| 4719 |
monkeypatch: pytest.MonkeyPatch, |
| 4720 |
) -> None: |
| 4721 |
"""Cover _keepalive_runner(): register_plugin('xep_0045') for want_muc.""" |
| 4722 |
install_fake_slixmpp(monkeypatch) |
| 4723 |
|
| 4724 |
cfg = xmpp_adapter.XMPPConfig( |
| 4725 |
jid="[email protected]", |
| 4726 |
password="x", |
| 4727 |
host="example.com", |
| 4728 |
port=5222, |
| 4729 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 4730 |
verify_certificate=False, |
| 4731 |
) |
| 4732 |
a = xmpp_adapter.SlixmppAdapter( |
| 4733 |
config=cfg, |
| 4734 |
targets=[("groupchat", "[email protected]")], |
| 4735 |
subject="s", |
| 4736 |
body="b", |
| 4737 |
keepalive=True, |
| 4738 |
want_muc=True, |
| 4739 |
) |
| 4740 |
|
| 4741 |
registered: list[str] = [] |
| 4742 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 4743 |
|
| 4744 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 4745 |
loop = real_new_event_loop() |
| 4746 |
|
| 4747 |
def run_forever_hook() -> None: |
| 4748 |
client = a._client |
| 4749 |
assert client is not None |
| 4750 |
|
| 4751 |
registered.extend(client.registered_plugins.keys()) |
| 4752 |
|
| 4753 |
monkeypatch.setattr( |
| 4754 |
loop, "run_forever", run_forever_hook, raising=True |
| 4755 |
) |
| 4756 |
return loop |
| 4757 |
|
| 4758 |
monkeypatch.setattr( |
| 4759 |
xmpp_adapter.asyncio, |
| 4760 |
"new_event_loop", |
| 4761 |
new_event_loop_wrapped, |
| 4762 |
raising=True, |
| 4763 |
) |
| 4764 |
|
| 4765 |
a._keepalive_runner() |
| 4766 |
|
| 4767 |
assert "xep_0045" in registered |
| 4768 |
|
| 4769 |
|
| 4770 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4771 |
def test_send_keepalive_async_muc_join_error( |
| 4772 |
monkeypatch: pytest.MonkeyPatch, |
| 4773 |
) -> None: |
| 4774 |
"""Cover join_muc() exception path in _send_keepalive_async().""" |
| 4775 |
install_fake_slixmpp(monkeypatch) |
| 4776 |
|
| 4777 |
cfg = xmpp_adapter.XMPPConfig( |
| 4778 |
jid="[email protected]", |
| 4779 |
password="x", |
| 4780 |
host="example.com", |
| 4781 |
port=5222, |
| 4782 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 4783 |
verify_certificate=False, |
| 4784 |
) |
| 4785 |
a = xmpp_adapter.SlixmppAdapter( |
| 4786 |
config=cfg, |
| 4787 |
targets=[("groupchat", "[email protected]")], |
| 4788 |
subject="s", |
| 4789 |
body="b", |
| 4790 |
keepalive=True, |
| 4791 |
want_muc=True, |
| 4792 |
) |
| 4793 |
|
| 4794 |
sent: list[dict[str, Any]] = [] |
| 4795 |
|
| 4796 |
async def join_muc_fail(room: str, nick: str) -> None: |
| 4797 |
raise asyncio.TimeoutError() |
| 4798 |
|
| 4799 |
class FakeBoundJID: |
| 4800 |
user = "me" |
| 4801 |
|
| 4802 |
class FakeMucPlugin: |
| 4803 |
def join_muc(self, room: str, nick: str, **kw: Any) -> Any: |
| 4804 |
return join_muc_fail(room, nick) |
| 4805 |
|
| 4806 |
class _Client: |
| 4807 |
_auth_failed = False |
| 4808 |
boundjid = FakeBoundJID() |
| 4809 |
plugin = {"xep_0045": FakeMucPlugin()} |
| 4810 |
|
| 4811 |
def send_message(self, **kwargs: Any) -> None: |
| 4812 |
sent.append(kwargs) |
| 4813 |
|
| 4814 |
a._client = _Client() |
| 4815 |
|
| 4816 |
async def ok_connect() -> bool: |
| 4817 |
return True |
| 4818 |
|
| 4819 |
monkeypatch.setattr(a, "_connect_if_required", ok_connect, raising=True) |
| 4820 |
|
| 4821 |
result = asyncio.run( |
| 4822 |
a._send_keepalive_async( |
| 4823 |
targets=[("groupchat", "[email protected]")], |
| 4824 |
subject="s", |
| 4825 |
body="b", |
| 4826 |
) |
| 4827 |
) |
| 4828 |
|
| 4829 |
|
| 4830 |
assert result is True |
| 4831 |
assert len(sent) == 1 |
| 4832 |
assert sent[0]["mtype"] == "groupchat" |
| 4833 |
|
| 4834 |
|
| 4835 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4836 |
def test_xmpp_client_logger_presence_regression(monkeypatch): |
| 4837 |
""" |
| 4838 |
Regression test: Verify that the internal _Client class automatically |
| 4839 |
receives a 'logger' attribute so _log_task does not AttributeError. |
| 4840 |
""" |
| 4841 |
install_fake_slixmpp(monkeypatch) |
| 4842 |
|
| 4843 |
cfg = xmpp_adapter.XMPPConfig( |
| 4844 |
jid="[email protected]", password="x", host="ex.com", port=5222 |
| 4845 |
) |
| 4846 |
|
| 4847 |
|
| 4848 |
adapter = xmpp_adapter.SlixmppAdapter( |
| 4849 |
config=cfg, targets=[], subject="s", body="b" |
| 4850 |
) |
| 4851 |
|
| 4852 |
|
| 4853 |
client = xmpp_adapter._build_client( |
| 4854 |
jid="[email protected]", password="x", oneshot=True, logger=adapter.logger |
| 4855 |
) |
| 4856 |
|
| 4857 |
|
| 4858 |
assert hasattr(client, "logger") |
| 4859 |
assert client.logger == adapter.logger |
| 4860 |
|
| 4861 |
|
| 4862 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4863 |
def test_xmpp_log_task_crash_regression(monkeypatch): |
| 4864 |
""" |
| 4865 |
Regression test: Verify that _log_task can handle an exception |
| 4866 |
without crashing on a missing logger attribute. |
| 4867 |
""" |
| 4868 |
install_fake_slixmpp(monkeypatch) |
| 4869 |
|
| 4870 |
|
| 4871 |
client_cls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 4872 |
client = client_cls( |
| 4873 |
jid="[email protected]", |
| 4874 |
password="pass", |
| 4875 |
oneshot=False, |
| 4876 |
|
| 4877 |
logger=logging.getLogger("apprise"), |
| 4878 |
) |
| 4879 |
|
| 4880 |
|
| 4881 |
class MockTask: |
| 4882 |
def cancelled(self): |
| 4883 |
return False |
| 4884 |
|
| 4885 |
def exception(self): |
| 4886 |
return RuntimeError("Simulated XMPP Failure") |
| 4887 |
|
| 4888 |
|
| 4889 |
client.handlers.get("session_start") |
| 4890 |
|
| 4891 |
|
| 4892 |
client._on_session_start() |
| 4893 |
|
| 4894 |
|
| 4895 |
|
| 4896 |
|
| 4897 |
|
| 4898 |
|
| 4899 |
|
| 4900 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4901 |
def test_xmpp_scramplus_default_true() -> None: |
| 4902 |
"""scramplus defaults to True (SCRAM-PLUS enabled).""" |
| 4903 |
n = NotifyXMPP(host="example.com", user="[email protected]", password="x") |
| 4904 |
assert n.scramplus is True |
| 4905 |
|
| 4906 |
|
| 4907 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4908 |
def test_xmpp_scramplus_explicit_false() -> None: |
| 4909 |
"""scramplus=False is stored and reflected in url().""" |
| 4910 |
n = NotifyXMPP( |
| 4911 |
host="example.com", |
| 4912 |
user="[email protected]", |
| 4913 |
password="x", |
| 4914 |
scramplus=False, |
| 4915 |
) |
| 4916 |
assert n.scramplus is False |
| 4917 |
u = n.url() |
| 4918 |
assert "scramplus=no" in u |
| 4919 |
|
| 4920 |
|
| 4921 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4922 |
def test_xmpp_scramplus_url_round_trip() -> None: |
| 4923 |
"""scramplus round-trips through url() and parse_url().""" |
| 4924 |
n = NotifyXMPP( |
| 4925 |
host="example.com", |
| 4926 |
user="[email protected]", |
| 4927 |
password="secret", |
| 4928 |
scramplus=False, |
| 4929 |
) |
| 4930 |
u = n.url() |
| 4931 |
r = NotifyXMPP.parse_url(u) |
| 4932 |
assert r is not None |
| 4933 |
assert r["scramplus"] is False |
| 4934 |
|
| 4935 |
n2 = NotifyXMPP(**r) |
| 4936 |
assert n2.scramplus is False |
| 4937 |
|
| 4938 |
|
| 4939 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4940 |
def test_xmpp_scramplus_parse_url_no() -> None: |
| 4941 |
"""?scramplus=no is parsed and stored correctly.""" |
| 4942 |
r = NotifyXMPP.parse_url( |
| 4943 |
"xmpps://me:[email protected]/[email protected]?scramplus=no" |
| 4944 |
) |
| 4945 |
assert r is not None |
| 4946 |
assert r["scramplus"] is False |
| 4947 |
|
| 4948 |
|
| 4949 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4950 |
def test_xmpp_scramplus_parse_url_yes() -> None: |
| 4951 |
"""?scramplus=yes is parsed and stored correctly.""" |
| 4952 |
r = NotifyXMPP.parse_url( |
| 4953 |
"xmpps://me:[email protected]/[email protected]?scramplus=yes" |
| 4954 |
) |
| 4955 |
assert r is not None |
| 4956 |
assert r["scramplus"] is True |
| 4957 |
|
| 4958 |
|
| 4959 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4960 |
def test_xmpp_scramplus_passes_to_config( |
| 4961 |
monkeypatch: pytest.MonkeyPatch, |
| 4962 |
) -> None: |
| 4963 |
"""scramplus=False propagates use_channel_binding=False to XMPPConfig.""" |
| 4964 |
captured: dict = {} |
| 4965 |
|
| 4966 |
class _Adapter: |
| 4967 |
def __init__(self, config: Any, **kwargs: Any) -> None: |
| 4968 |
captured["use_channel_binding"] = config.use_channel_binding |
| 4969 |
|
| 4970 |
def process(self) -> bool: |
| 4971 |
return True |
| 4972 |
|
| 4973 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _Adapter, raising=True) |
| 4974 |
|
| 4975 |
apobj = Apprise() |
| 4976 |
apobj.add("xmpps://user:[email protected]/joe?scramplus=no") |
| 4977 |
apobj.notify("hello") |
| 4978 |
|
| 4979 |
assert captured["use_channel_binding"] is False |
| 4980 |
|
| 4981 |
|
| 4982 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 4983 |
def test_xmpp_scramplus_true_passes_to_config( |
| 4984 |
monkeypatch: pytest.MonkeyPatch, |
| 4985 |
) -> None: |
| 4986 |
"""Default scramplus=True propagates use_channel_binding=True.""" |
| 4987 |
captured: dict = {} |
| 4988 |
|
| 4989 |
class _Adapter: |
| 4990 |
def __init__(self, config: Any, **kwargs: Any) -> None: |
| 4991 |
captured["use_channel_binding"] = config.use_channel_binding |
| 4992 |
|
| 4993 |
def process(self) -> bool: |
| 4994 |
return True |
| 4995 |
|
| 4996 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _Adapter, raising=True) |
| 4997 |
|
| 4998 |
apobj = Apprise() |
| 4999 |
apobj.add("xmpps://user:[email protected]/joe") |
| 5000 |
apobj.notify("hello") |
| 5001 |
|
| 5002 |
assert captured["use_channel_binding"] is True |
| 5003 |
|
| 5004 |
|
| 5005 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5006 |
def test_disable_channel_binding_filters_plus_mechs() -> None: |
| 5007 |
"""_disable_channel_binding patches _send_auth to strip -PLUS mechs.""" |
| 5008 |
from types import SimpleNamespace |
| 5009 |
|
| 5010 |
captured: list[set] = [] |
| 5011 |
|
| 5012 |
plugin = SimpleNamespace( |
| 5013 |
mech_list={"SCRAM-SHA-256-PLUS", "SCRAM-SHA-256", "SCRAM-SHA-1"}, |
| 5014 |
) |
| 5015 |
|
| 5016 |
def orig_send_auth() -> None: |
| 5017 |
captured.append(set(plugin.mech_list)) |
| 5018 |
|
| 5019 |
plugin._send_auth = orig_send_auth |
| 5020 |
|
| 5021 |
class _Client: |
| 5022 |
def __getitem__(self, key: str) -> Any: |
| 5023 |
if key == "feature_mechanisms": |
| 5024 |
return plugin |
| 5025 |
raise KeyError(key) |
| 5026 |
|
| 5027 |
xmpp_adapter._disable_channel_binding(_Client()) |
| 5028 |
|
| 5029 |
|
| 5030 |
plugin._send_auth() |
| 5031 |
|
| 5032 |
assert len(captured) == 1 |
| 5033 |
assert "SCRAM-SHA-256-PLUS" not in captured[0] |
| 5034 |
assert "SCRAM-SHA-256" in captured[0] |
| 5035 |
assert "SCRAM-SHA-1" in captured[0] |
| 5036 |
|
| 5037 |
|
| 5038 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5039 |
def test_disable_channel_binding_safe_when_no_plugin() -> None: |
| 5040 |
"""_disable_channel_binding no-ops when feature_mechanisms is absent.""" |
| 5041 |
|
| 5042 |
class _BadClient: |
| 5043 |
def __getitem__(self, key: str) -> Any: |
| 5044 |
raise KeyError(key) |
| 5045 |
|
| 5046 |
|
| 5047 |
xmpp_adapter._disable_channel_binding(_BadClient()) |
| 5048 |
|
| 5049 |
|
| 5050 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5051 |
def test_xmpp_scramplus_disabled_process( |
| 5052 |
monkeypatch: pytest.MonkeyPatch, |
| 5053 |
) -> None: |
| 5054 |
"""process() calls _disable_channel_binding when binding disabled.""" |
| 5055 |
install_fake_slixmpp(monkeypatch) |
| 5056 |
|
| 5057 |
patched: list[bool] = [] |
| 5058 |
orig_disable = xmpp_adapter._disable_channel_binding |
| 5059 |
|
| 5060 |
def spy_disable(client: Any) -> None: |
| 5061 |
patched.append(True) |
| 5062 |
orig_disable(client) |
| 5063 |
|
| 5064 |
monkeypatch.setattr( |
| 5065 |
xmpp_adapter, "_disable_channel_binding", spy_disable, raising=True |
| 5066 |
) |
| 5067 |
|
| 5068 |
cfg = xmpp_adapter.XMPPConfig( |
| 5069 |
jid="[email protected]", |
| 5070 |
password="x", |
| 5071 |
host="example.com", |
| 5072 |
port=5222, |
| 5073 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 5074 |
verify_certificate=False, |
| 5075 |
use_channel_binding=False, |
| 5076 |
) |
| 5077 |
a = xmpp_adapter.SlixmppAdapter( |
| 5078 |
config=cfg, |
| 5079 |
targets=[("chat", "[email protected]")], |
| 5080 |
subject="s", |
| 5081 |
body="b", |
| 5082 |
timeout=1.0, |
| 5083 |
) |
| 5084 |
assert a.process() is True |
| 5085 |
assert len(patched) == 1 |
| 5086 |
|
| 5087 |
|
| 5088 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5089 |
def test_xmpp_scramplus_enabled_no_patch( |
| 5090 |
monkeypatch: pytest.MonkeyPatch, |
| 5091 |
) -> None: |
| 5092 |
"""process() skips _disable_channel_binding when binding enabled.""" |
| 5093 |
install_fake_slixmpp(monkeypatch) |
| 5094 |
|
| 5095 |
patched: list[bool] = [] |
| 5096 |
orig_disable = xmpp_adapter._disable_channel_binding |
| 5097 |
|
| 5098 |
def spy_disable(client: Any) -> None: |
| 5099 |
patched.append(True) |
| 5100 |
orig_disable(client) |
| 5101 |
|
| 5102 |
monkeypatch.setattr( |
| 5103 |
xmpp_adapter, "_disable_channel_binding", spy_disable, raising=True |
| 5104 |
) |
| 5105 |
|
| 5106 |
cfg = xmpp_adapter.XMPPConfig( |
| 5107 |
jid="[email protected]", |
| 5108 |
password="x", |
| 5109 |
host="example.com", |
| 5110 |
port=5222, |
| 5111 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 5112 |
verify_certificate=False, |
| 5113 |
use_channel_binding=True, |
| 5114 |
) |
| 5115 |
a = xmpp_adapter.SlixmppAdapter( |
| 5116 |
config=cfg, |
| 5117 |
targets=[("chat", "[email protected]")], |
| 5118 |
subject="s", |
| 5119 |
body="b", |
| 5120 |
timeout=1.0, |
| 5121 |
) |
| 5122 |
assert a.process() is True |
| 5123 |
|
| 5124 |
assert len(patched) == 0 |
| 5125 |
|
| 5126 |
|
| 5127 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5128 |
def test_xmpp_scramplus_disabled_keepalive_runner( |
| 5129 |
monkeypatch: pytest.MonkeyPatch, |
| 5130 |
) -> None: |
| 5131 |
"""_keepalive_runner calls _disable_channel_binding when disabled.""" |
| 5132 |
install_fake_slixmpp(monkeypatch) |
| 5133 |
|
| 5134 |
patched: list[bool] = [] |
| 5135 |
orig_disable = xmpp_adapter._disable_channel_binding |
| 5136 |
|
| 5137 |
def spy_disable(client: Any) -> None: |
| 5138 |
patched.append(True) |
| 5139 |
orig_disable(client) |
| 5140 |
|
| 5141 |
monkeypatch.setattr( |
| 5142 |
xmpp_adapter, "_disable_channel_binding", spy_disable, raising=True |
| 5143 |
) |
| 5144 |
|
| 5145 |
cfg = xmpp_adapter.XMPPConfig( |
| 5146 |
jid="[email protected]", |
| 5147 |
password="x", |
| 5148 |
host="example.com", |
| 5149 |
port=5222, |
| 5150 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 5151 |
verify_certificate=False, |
| 5152 |
use_channel_binding=False, |
| 5153 |
) |
| 5154 |
a = xmpp_adapter.SlixmppAdapter( |
| 5155 |
config=cfg, |
| 5156 |
targets=[("chat", "[email protected]")], |
| 5157 |
subject="s", |
| 5158 |
body="b", |
| 5159 |
timeout=1.0, |
| 5160 |
keepalive=True, |
| 5161 |
) |
| 5162 |
|
| 5163 |
real_new_event_loop = xmpp_adapter.asyncio.new_event_loop |
| 5164 |
|
| 5165 |
def new_event_loop_wrapped() -> asyncio.AbstractEventLoop: |
| 5166 |
loop = real_new_event_loop() |
| 5167 |
|
| 5168 |
def run_forever_hook() -> None: |
| 5169 |
pass |
| 5170 |
|
| 5171 |
monkeypatch.setattr( |
| 5172 |
loop, "run_forever", run_forever_hook, raising=True |
| 5173 |
) |
| 5174 |
return loop |
| 5175 |
|
| 5176 |
monkeypatch.setattr( |
| 5177 |
xmpp_adapter.asyncio, |
| 5178 |
"new_event_loop", |
| 5179 |
new_event_loop_wrapped, |
| 5180 |
raising=True, |
| 5181 |
) |
| 5182 |
|
| 5183 |
a._keepalive_runner() |
| 5184 |
|
| 5185 |
assert len(patched) == 1 |
| 5186 |
|
| 5187 |
|
| 5188 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5189 |
def test_xmpp_failed_auth_stanza_sets_channel_binding_flag( |
| 5190 |
monkeypatch: pytest.MonkeyPatch, |
| 5191 |
) -> None: |
| 5192 |
"""_failed_auth sets _channel_binding_failed when phrase detected.""" |
| 5193 |
install_fake_slixmpp(monkeypatch) |
| 5194 |
|
| 5195 |
|
| 5196 |
xmpp_adapter._CLIENT_SUBCLASS_CACHE.clear() |
| 5197 |
|
| 5198 |
|
| 5199 |
_ClientCls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 5200 |
client = _ClientCls( |
| 5201 |
jid="[email protected]", |
| 5202 |
password="x", |
| 5203 |
oneshot=True, |
| 5204 |
logger=logging.getLogger("test"), |
| 5205 |
) |
| 5206 |
|
| 5207 |
|
| 5208 |
assert client._auth_failed is False |
| 5209 |
assert client._channel_binding_failed is False |
| 5210 |
|
| 5211 |
|
| 5212 |
fake_stanza = ( |
| 5213 |
"<failure><not-authorized/>" |
| 5214 |
"<text>Invalid channel binding</text></failure>" |
| 5215 |
) |
| 5216 |
client._failed_auth(fake_stanza) |
| 5217 |
|
| 5218 |
assert client._auth_failed is True |
| 5219 |
assert client._channel_binding_failed is True |
| 5220 |
|
| 5221 |
|
| 5222 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5223 |
def test_xmpp_failed_auth_plain_does_not_set_channel_binding_flag( |
| 5224 |
monkeypatch: pytest.MonkeyPatch, |
| 5225 |
) -> None: |
| 5226 |
"""_failed_auth skips _channel_binding_failed for plain auth failures.""" |
| 5227 |
install_fake_slixmpp(monkeypatch) |
| 5228 |
|
| 5229 |
xmpp_adapter._CLIENT_SUBCLASS_CACHE.clear() |
| 5230 |
|
| 5231 |
_ClientCls = xmpp_adapter._get_client_subclass(FakeClientXMPP) |
| 5232 |
client = _ClientCls( |
| 5233 |
jid="[email protected]", |
| 5234 |
password="x", |
| 5235 |
oneshot=True, |
| 5236 |
logger=logging.getLogger("test"), |
| 5237 |
) |
| 5238 |
|
| 5239 |
|
| 5240 |
fake_stanza = "<failure><not-authorized/></failure>" |
| 5241 |
client._failed_auth(fake_stanza) |
| 5242 |
|
| 5243 |
assert client._auth_failed is True |
| 5244 |
assert client._channel_binding_failed is False |
| 5245 |
|
| 5246 |
|
| 5247 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5248 |
def test_xmpp_process_raises_channel_binding_error( |
| 5249 |
monkeypatch: pytest.MonkeyPatch, |
| 5250 |
) -> None: |
| 5251 |
"""process() raises XMPPChannelBindingError when channel binding fails.""" |
| 5252 |
install_fake_slixmpp(monkeypatch) |
| 5253 |
|
| 5254 |
xmpp_adapter._CLIENT_SUBCLASS_CACHE.clear() |
| 5255 |
|
| 5256 |
|
| 5257 |
|
| 5258 |
orig_connect = FakeClientXMPP.connect |
| 5259 |
|
| 5260 |
def connect_cb_fail(self, **kwargs: Any) -> asyncio.Future[bool]: |
| 5261 |
loop = self.loop or asyncio.get_running_loop() |
| 5262 |
|
| 5263 |
if self.disconnected is None: |
| 5264 |
self.disconnected = loop.create_future() |
| 5265 |
|
| 5266 |
fut: asyncio.Future[bool] = loop.create_future() |
| 5267 |
fut.set_result(True) |
| 5268 |
|
| 5269 |
def _fire_failed_auth() -> None: |
| 5270 |
|
| 5271 |
|
| 5272 |
failed_handler = self.handlers.get("failed_auth") |
| 5273 |
if failed_handler: |
| 5274 |
fake_stanza = ( |
| 5275 |
"<failure><not-authorized/>" |
| 5276 |
"<text>Invalid channel binding</text></failure>" |
| 5277 |
) |
| 5278 |
failed_handler(fake_stanza) |
| 5279 |
|
| 5280 |
if self.disconnected is not None and not self.disconnected.done(): |
| 5281 |
self.disconnected.set_result(True) |
| 5282 |
|
| 5283 |
loop.call_soon(_fire_failed_auth) |
| 5284 |
return fut |
| 5285 |
|
| 5286 |
monkeypatch.setattr(FakeClientXMPP, "connect", connect_cb_fail) |
| 5287 |
|
| 5288 |
cfg = xmpp_adapter.XMPPConfig( |
| 5289 |
jid="[email protected]", |
| 5290 |
password="x", |
| 5291 |
host="example.com", |
| 5292 |
port=5222, |
| 5293 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 5294 |
verify_certificate=False, |
| 5295 |
) |
| 5296 |
a = xmpp_adapter.SlixmppAdapter( |
| 5297 |
config=cfg, |
| 5298 |
targets=[("chat", "[email protected]")], |
| 5299 |
subject="s", |
| 5300 |
body="b", |
| 5301 |
timeout=5.0, |
| 5302 |
) |
| 5303 |
|
| 5304 |
with pytest.raises(xmpp_adapter.XMPPChannelBindingError): |
| 5305 |
a.process() |
| 5306 |
|
| 5307 |
monkeypatch.setattr(FakeClientXMPP, "connect", orig_connect) |
| 5308 |
|
| 5309 |
|
| 5310 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5311 |
def test_xmpp_send_catches_channel_binding_error_and_logs( |
| 5312 |
monkeypatch: pytest.MonkeyPatch, |
| 5313 |
caplog: pytest.LogCaptureFixture, |
| 5314 |
) -> None: |
| 5315 |
"""base.py send() catches XMPPChannelBindingError and logs the hint.""" |
| 5316 |
|
| 5317 |
class _RaisingAdapter: |
| 5318 |
def __init__(self, **kwargs: Any) -> None: |
| 5319 |
pass |
| 5320 |
|
| 5321 |
def process(self) -> bool: |
| 5322 |
raise xmpp_adapter.XMPPChannelBindingError() |
| 5323 |
|
| 5324 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _RaisingAdapter) |
| 5325 |
monkeypatch.setattr( |
| 5326 |
xmpp_base, "SLIXMPP_SUPPORT_AVAILABLE", True, raising=False |
| 5327 |
) |
| 5328 |
monkeypatch.setattr(xmpp_base.NotifyXMPP, "enabled", True, raising=False) |
| 5329 |
|
| 5330 |
n = NotifyXMPP(host="example.com", user="me", password="x") |
| 5331 |
|
| 5332 |
with caplog.at_level(logging.WARNING, logger="apprise"): |
| 5333 |
result = n.send(body="hi") |
| 5334 |
|
| 5335 |
assert result is False |
| 5336 |
assert "scramplus=no" in caplog.text |
| 5337 |
|
| 5338 |
|
| 5339 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5340 |
def test_xmpp_send_channel_binding_error_scramplus_already_disabled( |
| 5341 |
monkeypatch: pytest.MonkeyPatch, |
| 5342 |
caplog: pytest.LogCaptureFixture, |
| 5343 |
) -> None: |
| 5344 |
"""No ?scramplus=no hint when scramplus is already False.""" |
| 5345 |
|
| 5346 |
class _RaisingAdapter: |
| 5347 |
def __init__(self, **kwargs: Any) -> None: |
| 5348 |
pass |
| 5349 |
|
| 5350 |
def process(self) -> bool: |
| 5351 |
raise xmpp_adapter.XMPPChannelBindingError() |
| 5352 |
|
| 5353 |
monkeypatch.setattr(xmpp_base, "SlixmppAdapter", _RaisingAdapter) |
| 5354 |
monkeypatch.setattr( |
| 5355 |
xmpp_base, "SLIXMPP_SUPPORT_AVAILABLE", True, raising=False |
| 5356 |
) |
| 5357 |
monkeypatch.setattr(xmpp_base.NotifyXMPP, "enabled", True, raising=False) |
| 5358 |
|
| 5359 |
|
| 5360 |
n = NotifyXMPP( |
| 5361 |
host="example.com", user="me", password="x", scramplus=False |
| 5362 |
) |
| 5363 |
|
| 5364 |
with caplog.at_level(logging.WARNING, logger="apprise"): |
| 5365 |
result = n.send(body="hi") |
| 5366 |
|
| 5367 |
assert result is False |
| 5368 |
|
| 5369 |
assert "Add ?scramplus=no" not in caplog.text |
| 5370 |
|
| 5371 |
assert "compatibility" in caplog.text |
| 5372 |
|
| 5373 |
|
| 5374 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5375 |
def test_connect_if_required_channel_binding_early_raises( |
| 5376 |
monkeypatch: pytest.MonkeyPatch, |
| 5377 |
) -> None: |
| 5378 |
"""_connect_if_required() raises early when _channel_binding_failed.""" |
| 5379 |
install_fake_slixmpp(monkeypatch) |
| 5380 |
|
| 5381 |
cfg = xmpp_adapter.XMPPConfig( |
| 5382 |
jid="[email protected]", |
| 5383 |
password="x", |
| 5384 |
host="example.com", |
| 5385 |
port=5222, |
| 5386 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 5387 |
verify_certificate=False, |
| 5388 |
) |
| 5389 |
a = xmpp_adapter.SlixmppAdapter( |
| 5390 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 5391 |
) |
| 5392 |
|
| 5393 |
loop = asyncio.new_event_loop() |
| 5394 |
try: |
| 5395 |
asyncio.set_event_loop(loop) |
| 5396 |
a._loop = loop |
| 5397 |
|
| 5398 |
a._client = SimpleNamespace( |
| 5399 |
_channel_binding_failed=True, _auth_failed=False |
| 5400 |
) |
| 5401 |
a._connect_lock = asyncio.Lock() |
| 5402 |
a._session_started = asyncio.Event() |
| 5403 |
|
| 5404 |
with pytest.raises(xmpp_adapter.XMPPChannelBindingError): |
| 5405 |
run_on_loop(loop, a._connect_if_required()) |
| 5406 |
finally: |
| 5407 |
with contextlib.suppress(Exception): |
| 5408 |
asyncio.set_event_loop(None) |
| 5409 |
loop.close() |
| 5410 |
|
| 5411 |
|
| 5412 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5413 |
def test_connect_if_required_channel_binding_bottom_raises( |
| 5414 |
monkeypatch: pytest.MonkeyPatch, |
| 5415 |
) -> None: |
| 5416 |
"""_connect_if_required() raises XMPPChannelBindingError check.""" |
| 5417 |
install_fake_slixmpp(monkeypatch) |
| 5418 |
|
| 5419 |
cfg = xmpp_adapter.XMPPConfig( |
| 5420 |
jid="[email protected]", |
| 5421 |
password="x", |
| 5422 |
host="example.com", |
| 5423 |
port=5222, |
| 5424 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 5425 |
verify_certificate=False, |
| 5426 |
) |
| 5427 |
a = xmpp_adapter.SlixmppAdapter( |
| 5428 |
config=cfg, targets=[], subject="s", body="b", keepalive=True |
| 5429 |
) |
| 5430 |
|
| 5431 |
loop = asyncio.new_event_loop() |
| 5432 |
try: |
| 5433 |
asyncio.set_event_loop(loop) |
| 5434 |
a._loop = loop |
| 5435 |
a._connect_lock = asyncio.Lock() |
| 5436 |
a._session_started = asyncio.Event() |
| 5437 |
|
| 5438 |
fut = loop.create_future() |
| 5439 |
fut.set_result(True) |
| 5440 |
|
| 5441 |
class _Client: |
| 5442 |
def __init__(self) -> None: |
| 5443 |
self._channel_binding_failed = False |
| 5444 |
self._auth_failed = False |
| 5445 |
|
| 5446 |
def connect(self, **kwargs: Any) -> Any: |
| 5447 |
return fut |
| 5448 |
|
| 5449 |
a._client = _Client() |
| 5450 |
|
| 5451 |
|
| 5452 |
real_wait_for = xmpp_adapter.asyncio.wait_for |
| 5453 |
calls: dict[str, int] = {"n": 0} |
| 5454 |
|
| 5455 |
async def wait_for_patched( |
| 5456 |
aw: Any, timeout: Optional[float] = None |
| 5457 |
) -> Any: |
| 5458 |
calls["n"] += 1 |
| 5459 |
if calls["n"] == 2: |
| 5460 |
assert a._session_started is not None |
| 5461 |
a._session_started.set() |
| 5462 |
assert isinstance(a._client, _Client) |
| 5463 |
a._client._channel_binding_failed = True |
| 5464 |
return await real_wait_for(aw, timeout=timeout) |
| 5465 |
|
| 5466 |
monkeypatch.setattr( |
| 5467 |
xmpp_adapter.asyncio, "wait_for", wait_for_patched, raising=True |
| 5468 |
) |
| 5469 |
|
| 5470 |
with pytest.raises(xmpp_adapter.XMPPChannelBindingError): |
| 5471 |
run_on_loop(loop, a._connect_if_required()) |
| 5472 |
|
| 5473 |
finally: |
| 5474 |
with contextlib.suppress(Exception): |
| 5475 |
asyncio.set_event_loop(None) |
| 5476 |
loop.close() |
| 5477 |
|
| 5478 |
|
| 5479 |
@pytest.mark.skipif(not SLIXMPP_AVAILABLE, reason="Requires slixmpp") |
| 5480 |
def test_send_message_keepalive_reraises_channel_binding_error( |
| 5481 |
monkeypatch: pytest.MonkeyPatch, |
| 5482 |
) -> None: |
| 5483 |
"""send_message() keepalive path re-raises XMPPChannelBindingError.""" |
| 5484 |
install_fake_slixmpp(monkeypatch) |
| 5485 |
|
| 5486 |
cfg = xmpp_adapter.XMPPConfig( |
| 5487 |
jid="[email protected]", |
| 5488 |
password="x", |
| 5489 |
host="example.com", |
| 5490 |
port=5222, |
| 5491 |
secure=xmpp_adapter.SecureXMPPMode.STARTTLS, |
| 5492 |
verify_certificate=False, |
| 5493 |
) |
| 5494 |
a = xmpp_adapter.SlixmppAdapter( |
| 5495 |
config=cfg, |
| 5496 |
targets=[("chat", "[email protected]")], |
| 5497 |
subject="s", |
| 5498 |
body="b", |
| 5499 |
timeout=5.0, |
| 5500 |
keepalive=True, |
| 5501 |
) |
| 5502 |
|
| 5503 |
monkeypatch.setattr( |
| 5504 |
a, "_ensure_keepalive_worker", lambda: True, raising=True |
| 5505 |
) |
| 5506 |
|
| 5507 |
class _Loop: |
| 5508 |
pass |
| 5509 |
|
| 5510 |
a._loop = _Loop() |
| 5511 |
|
| 5512 |
def run_coroutine_threadsafe_cb_fail(coro: Any, loop: Any) -> Any: |
| 5513 |
|
| 5514 |
with contextlib.suppress(Exception): |
| 5515 |
coro.close() |
| 5516 |
|
| 5517 |
class _Fut: |
| 5518 |
def result(self, timeout: Optional[float] = None) -> bool: |
| 5519 |
raise xmpp_adapter.XMPPChannelBindingError() |
| 5520 |
|
| 5521 |
return _Fut() |
| 5522 |
|
| 5523 |
monkeypatch.setattr( |
| 5524 |
xmpp_adapter.asyncio, |
| 5525 |
"run_coroutine_threadsafe", |
| 5526 |
run_coroutine_threadsafe_cb_fail, |
| 5527 |
raising=True, |
| 5528 |
) |
| 5529 |
|
| 5530 |
with pytest.raises(xmpp_adapter.XMPPChannelBindingError): |
| 5531 |
a.send_message() |