| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
import sys |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise import Apprise |
| 38 |
from apprise.exception import AppriseImproperlyConfigured |
| 39 |
from apprise.plugins import sogs |
| 40 |
from apprise.plugins.base import NotifyBase |
| 41 |
from apprise.plugins.sogs import ( |
| 42 |
NotifySessionOGS, |
| 43 |
_build_session_message, |
| 44 |
_encode_varint, |
| 45 |
_ld_field, |
| 46 |
) |
| 47 |
|
| 48 |
logging.disable(logging.CRITICAL) |
| 49 |
|
| 50 |
# A valid 64-hex-char Ed25519 seed used as the bot seed. |
| 51 |
SEED = "a" * 64 |
| 52 |
|
| 53 |
# A valid 64-hex-char server Curve25519 public key (from Session join URL). |
| 54 |
PUBLIC_KEY = "b" * 64 |
| 55 |
|
| 56 |
# An invalid key: 63 chars (too short). |
| 57 |
BAD_KEY = "c" * 63 |
| 58 |
|
| 59 |
# A valid room token. |
| 60 |
ROOM = "test-room" |
| 61 |
|
| 62 |
# Second room token for multi-room tests. |
| 63 |
ROOM2 = "another-room" |
| 64 |
|
| 65 |
# Canonical base URL used throughout the tests. |
| 66 |
BASE_URL = f"sessions://{PUBLIC_KEY}:{SEED}@open.getsession.org/{ROOM}" |
| 67 |
|
| 68 |
# Apprise URL tester entries; requires cryptography to run functionally. |
| 69 |
apprise_url_tests = ( |
| 70 |
# Missing public_key and seed (only hostname + room, no credentials) |
| 71 |
( |
| 72 |
f"sessions://host/{ROOM}", |
| 73 |
{ |
| 74 |
"instance": AppriseImproperlyConfigured, |
| 75 |
}, |
| 76 |
), |
| 77 |
# Missing seed (user field present, no password) |
| 78 |
( |
| 79 |
f"sessions://{PUBLIC_KEY}@host/{ROOM}", |
| 80 |
{ |
| 81 |
"instance": AppriseImproperlyConfigured, |
| 82 |
}, |
| 83 |
), |
| 84 |
# Missing public_key (bad key in user field, too short) |
| 85 |
( |
| 86 |
f"sessions://{BAD_KEY}:{SEED}@host/{ROOM}", |
| 87 |
{ |
| 88 |
"instance": AppriseImproperlyConfigured, |
| 89 |
}, |
| 90 |
), |
| 91 |
# Missing room token (no path segments) |
| 92 |
( |
| 93 |
f"sessions://{PUBLIC_KEY}:{SEED}@host", |
| 94 |
{ |
| 95 |
"instance": AppriseImproperlyConfigured, |
| 96 |
}, |
| 97 |
), |
| 98 |
# Invalid seed (too short, in password field) |
| 99 |
( |
| 100 |
f"sessions://{PUBLIC_KEY}:{BAD_KEY}@host/{ROOM}", |
| 101 |
{ |
| 102 |
"instance": AppriseImproperlyConfigured, |
| 103 |
}, |
| 104 |
), |
| 105 |
# Valid HTTPS URL - single room |
| 106 |
( |
| 107 |
BASE_URL, |
| 108 |
{ |
| 109 |
"instance": NotifySessionOGS, |
| 110 |
"privacy_url": ( |
| 111 |
f"sessions://{PUBLIC_KEY}:[email protected]/{ROOM}" |
| 112 |
), |
| 113 |
"requests_response_code": requests.codes.created, |
| 114 |
}, |
| 115 |
), |
| 116 |
# Valid HTTPS URL via ?to= alias (public_key:seed in authority field) |
| 117 |
( |
| 118 |
f"sessions://{PUBLIC_KEY}:{SEED}@open.getsession.org?to={ROOM}", |
| 119 |
{ |
| 120 |
"instance": NotifySessionOGS, |
| 121 |
"requests_response_code": requests.codes.created, |
| 122 |
}, |
| 123 |
), |
| 124 |
# Valid HTTPS URL with custom port |
| 125 |
( |
| 126 |
f"sessions://{PUBLIC_KEY}:{SEED}@open.getsession.org:8443/{ROOM}", |
| 127 |
{ |
| 128 |
"instance": NotifySessionOGS, |
| 129 |
"requests_response_code": requests.codes.created, |
| 130 |
}, |
| 131 |
), |
| 132 |
# Valid HTTP (insecure) URL |
| 133 |
( |
| 134 |
f"session://{PUBLIC_KEY}:{SEED}@open.getsession.org/{ROOM}", |
| 135 |
{ |
| 136 |
"instance": NotifySessionOGS, |
| 137 |
"requests_response_code": requests.codes.created, |
| 138 |
}, |
| 139 |
), |
| 140 |
# sogs:// alias for sessions:// (HTTPS) |
| 141 |
( |
| 142 |
f"sogs://{PUBLIC_KEY}:{SEED}@open.getsession.org/{ROOM}", |
| 143 |
{ |
| 144 |
"instance": NotifySessionOGS, |
| 145 |
"requests_response_code": requests.codes.created, |
| 146 |
}, |
| 147 |
), |
| 148 |
# Multiple rooms as path segments |
| 149 |
( |
| 150 |
f"sessions://{PUBLIC_KEY}:{SEED}@open.getsession.org/{ROOM}/{ROOM2}", |
| 151 |
{ |
| 152 |
"instance": NotifySessionOGS, |
| 153 |
"requests_response_code": requests.codes.created, |
| 154 |
}, |
| 155 |
), |
| 156 |
# Query-string form: ?key= and ?seed= instead of authority field |
| 157 |
( |
| 158 |
f"sessions://open.getsession.org/{ROOM}?key={PUBLIC_KEY}&seed={SEED}", |
| 159 |
{ |
| 160 |
"instance": NotifySessionOGS, |
| 161 |
"requests_response_code": requests.codes.created, |
| 162 |
}, |
| 163 |
), |
| 164 |
# Query-string form using Session-native ?public_key= alias |
| 165 |
( |
| 166 |
f"sessions://open.getsession.org/{ROOM}" |
| 167 |
f"?public_key={PUBLIC_KEY}&seed={SEED}", |
| 168 |
{ |
| 169 |
"instance": NotifySessionOGS, |
| 170 |
"requests_response_code": requests.codes.created, |
| 171 |
}, |
| 172 |
), |
| 173 |
# HTTP 403 Forbidden |
| 174 |
( |
| 175 |
BASE_URL, |
| 176 |
{ |
| 177 |
"instance": NotifySessionOGS, |
| 178 |
"response": False, |
| 179 |
"requests_response_code": requests.codes.forbidden, |
| 180 |
}, |
| 181 |
), |
| 182 |
# HTTP 500 Internal Server Error |
| 183 |
( |
| 184 |
BASE_URL, |
| 185 |
{ |
| 186 |
"instance": NotifySessionOGS, |
| 187 |
"response": False, |
| 188 |
"requests_response_code": requests.codes.internal_server_error, |
| 189 |
}, |
| 190 |
), |
| 191 |
# Unknown HTTP status code |
| 192 |
( |
| 193 |
BASE_URL, |
| 194 |
{ |
| 195 |
"instance": NotifySessionOGS, |
| 196 |
"response": False, |
| 197 |
"requests_response_code": 999, |
| 198 |
}, |
| 199 |
), |
| 200 |
# RequestException |
| 201 |
( |
| 202 |
BASE_URL, |
| 203 |
{ |
| 204 |
"instance": NotifySessionOGS, |
| 205 |
"test_requests_exceptions": True, |
| 206 |
}, |
| 207 |
), |
| 208 |
) |
| 209 |
|
| 210 |
|
| 211 |
@pytest.mark.skipif( |
| 212 |
"cryptography" not in sys.modules, |
| 213 |
reason="Requires cryptography", |
| 214 |
) |
| 215 |
def test_plugin_sogs_urls(): |
| 216 |
"""Run the Apprise URL test suite against SOGS URLs.""" |
| 217 |
helper = AppriseURLTester(tests=apprise_url_tests) |
| 218 |
helper.run_all() |
| 219 |
|
| 220 |
|
| 221 |
@pytest.mark.skipif( |
| 222 |
"cryptography" in sys.modules, |
| 223 |
reason="Requires that cryptography NOT be installed", |
| 224 |
) |
| 225 |
def test_plugin_sogs_no_cryptography(): |
| 226 |
"""Plugin is disabled when cryptography is not installed.""" |
| 227 |
# Apprise.instantiate() returns None for disabled plugins. |
| 228 |
obj = Apprise.instantiate(BASE_URL) |
| 229 |
assert obj is None |
| 230 |
|
| 231 |
# Direct construction remains safe, but sending is unavailable. |
| 232 |
direct = NotifySessionOGS( |
| 233 |
public_key=PUBLIC_KEY, |
| 234 |
seed=SEED, |
| 235 |
targets=[ROOM], |
| 236 |
) |
| 237 |
assert direct.enabled is False |
| 238 |
assert direct.send("test") is False |
| 239 |
|
| 240 |
|
| 241 |
def test_plugin_sogs_disabled_dependency(monkeypatch): |
| 242 |
"""Direct use fails safely when cryptography is unavailable.""" |
| 243 |
monkeypatch.setattr(sogs, "NOTIFY_SESSIONOGS_ENABLED", False) |
| 244 |
monkeypatch.setattr(NotifySessionOGS, "enabled", False) |
| 245 |
|
| 246 |
assert Apprise.instantiate(BASE_URL) is None |
| 247 |
|
| 248 |
direct = NotifySessionOGS( |
| 249 |
public_key=PUBLIC_KEY, |
| 250 |
seed=SEED, |
| 251 |
targets=[ROOM], |
| 252 |
) |
| 253 |
assert direct._signing_key is None |
| 254 |
assert direct._bot_pubkey_bytes is None |
| 255 |
|
| 256 |
with mock.patch("requests.post") as mock_post: |
| 257 |
assert direct.send("test") is False |
| 258 |
mock_post.assert_not_called() |
| 259 |
|
| 260 |
|
| 261 |
@pytest.mark.skipif( |
| 262 |
"cryptography" not in sys.modules, |
| 263 |
reason="Requires cryptography", |
| 264 |
) |
| 265 |
@mock.patch("requests.post") |
| 266 |
def test_plugin_sogs_skips_delivered_room(mock_post): |
| 267 |
"""A room that already has the message is left alone on a retry.""" |
| 268 |
|
| 269 |
obj = NotifySessionOGS( |
| 270 |
public_key=PUBLIC_KEY, |
| 271 |
seed=SEED, |
| 272 |
targets=[ROOM], |
| 273 |
host="open.getsession.org", |
| 274 |
) |
| 275 |
|
| 276 |
with mock.patch.object(NotifyBase, "is_delivered", return_value=True): |
| 277 |
assert obj.send(body="test", title="test") is True |
| 278 |
|
| 279 |
# Nothing was posted a second time |
| 280 |
assert mock_post.call_count == 0 |
| 281 |
|
| 282 |
|
| 283 |
@pytest.mark.skipif( |
| 284 |
"cryptography" not in sys.modules, |
| 285 |
reason="Requires cryptography", |
| 286 |
) |
| 287 |
@mock.patch("requests.post") |
| 288 |
def test_plugin_sogs_init(mock_post): |
| 289 |
"""Test basic initialization and URL round-trip.""" |
| 290 |
|
| 291 |
def _mk_resp(code=requests.codes.created): |
| 292 |
r = mock.Mock() |
| 293 |
r.status_code = code |
| 294 |
r.content = b'{"id": 1}' |
| 295 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 296 |
return r |
| 297 |
|
| 298 |
mock_post.return_value = _mk_resp() |
| 299 |
|
| 300 |
# Basic construction. |
| 301 |
obj = NotifySessionOGS( |
| 302 |
public_key=PUBLIC_KEY, |
| 303 |
seed=SEED, |
| 304 |
targets=[ROOM], |
| 305 |
host="open.getsession.org", |
| 306 |
) |
| 307 |
assert obj.rooms == [ROOM] |
| 308 |
assert obj.public_key == PUBLIC_KEY |
| 309 |
assert obj.seed == SEED |
| 310 |
|
| 311 |
# Round-trip invariant. |
| 312 |
url = obj.url() |
| 313 |
parsed = NotifySessionOGS.parse_url(url) |
| 314 |
obj2 = NotifySessionOGS(**parsed) |
| 315 |
assert obj.url_identifier == obj2.url_identifier |
| 316 |
assert len(obj.rooms) == len(obj2.rooms) |
| 317 |
|
| 318 |
|
| 319 |
@pytest.mark.skipif( |
| 320 |
"cryptography" not in sys.modules, |
| 321 |
reason="Requires cryptography", |
| 322 |
) |
| 323 |
@mock.patch("requests.post") |
| 324 |
def test_plugin_sogs_missing_public_key(mock_post): |
| 325 |
"""A missing or invalid public key is rejected.""" |
| 326 |
with pytest.raises(AppriseImproperlyConfigured): |
| 327 |
NotifySessionOGS( |
| 328 |
public_key=None, |
| 329 |
seed=SEED, |
| 330 |
targets=[ROOM], |
| 331 |
host="host", |
| 332 |
) |
| 333 |
with pytest.raises(AppriseImproperlyConfigured): |
| 334 |
NotifySessionOGS( |
| 335 |
public_key=BAD_KEY, |
| 336 |
seed=SEED, |
| 337 |
targets=[ROOM], |
| 338 |
host="host", |
| 339 |
) |
| 340 |
|
| 341 |
|
| 342 |
@pytest.mark.skipif( |
| 343 |
"cryptography" not in sys.modules, |
| 344 |
reason="Requires cryptography", |
| 345 |
) |
| 346 |
@mock.patch("requests.post") |
| 347 |
def test_plugin_sogs_missing_seed(mock_post): |
| 348 |
"""A missing or invalid seed is rejected.""" |
| 349 |
with pytest.raises(AppriseImproperlyConfigured): |
| 350 |
NotifySessionOGS( |
| 351 |
public_key=PUBLIC_KEY, |
| 352 |
seed=None, |
| 353 |
targets=[ROOM], |
| 354 |
host="host", |
| 355 |
) |
| 356 |
with pytest.raises(AppriseImproperlyConfigured): |
| 357 |
NotifySessionOGS( |
| 358 |
public_key=PUBLIC_KEY, |
| 359 |
seed=BAD_KEY, |
| 360 |
targets=[ROOM], |
| 361 |
host="host", |
| 362 |
) |
| 363 |
|
| 364 |
|
| 365 |
@pytest.mark.skipif( |
| 366 |
"cryptography" not in sys.modules, |
| 367 |
reason="Requires cryptography", |
| 368 |
) |
| 369 |
@mock.patch("requests.post") |
| 370 |
def test_plugin_sogs_missing_rooms(mock_post): |
| 371 |
"""At least one valid room token is required.""" |
| 372 |
with pytest.raises(AppriseImproperlyConfigured): |
| 373 |
NotifySessionOGS( |
| 374 |
public_key=PUBLIC_KEY, |
| 375 |
seed=SEED, |
| 376 |
targets=None, |
| 377 |
host="host", |
| 378 |
) |
| 379 |
with pytest.raises(AppriseImproperlyConfigured): |
| 380 |
NotifySessionOGS( |
| 381 |
public_key=PUBLIC_KEY, |
| 382 |
seed=SEED, |
| 383 |
targets=["!!invalid!!"], |
| 384 |
host="host", |
| 385 |
) |
| 386 |
|
| 387 |
|
| 388 |
@pytest.mark.skipif( |
| 389 |
"cryptography" not in sys.modules, |
| 390 |
reason="Requires cryptography", |
| 391 |
) |
| 392 |
@mock.patch("requests.post") |
| 393 |
def test_plugin_sogs_send_success(mock_post): |
| 394 |
"""A 201 Created response is treated as success.""" |
| 395 |
|
| 396 |
def _mk_resp(code=requests.codes.created): |
| 397 |
r = mock.Mock() |
| 398 |
r.status_code = code |
| 399 |
r.content = b'{"id": 1}' |
| 400 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 401 |
return r |
| 402 |
|
| 403 |
mock_post.return_value = _mk_resp() |
| 404 |
|
| 405 |
obj = NotifySessionOGS( |
| 406 |
public_key=PUBLIC_KEY, |
| 407 |
seed=SEED, |
| 408 |
targets=[ROOM], |
| 409 |
host="open.getsession.org", |
| 410 |
) |
| 411 |
assert bool(obj.notify(body="Hello SOGS")) is True |
| 412 |
assert mock_post.call_count == 1 |
| 413 |
|
| 414 |
# HTTP 200 (OK) is also treated as success. |
| 415 |
mock_post.return_value = _mk_resp(requests.codes.ok) |
| 416 |
assert bool(obj.notify(body="Hello SOGS")) is True |
| 417 |
|
| 418 |
|
| 419 |
@pytest.mark.skipif( |
| 420 |
"cryptography" not in sys.modules, |
| 421 |
reason="Requires cryptography", |
| 422 |
) |
| 423 |
@mock.patch("requests.post") |
| 424 |
def test_plugin_sogs_send_multi_room(mock_post): |
| 425 |
"""Sending to two rooms makes two POST requests.""" |
| 426 |
|
| 427 |
def _mk_resp(code=requests.codes.created): |
| 428 |
r = mock.Mock() |
| 429 |
r.status_code = code |
| 430 |
r.content = b'{"id": 1}' |
| 431 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 432 |
return r |
| 433 |
|
| 434 |
mock_post.return_value = _mk_resp() |
| 435 |
|
| 436 |
obj = NotifySessionOGS( |
| 437 |
public_key=PUBLIC_KEY, |
| 438 |
seed=SEED, |
| 439 |
targets=[ROOM, ROOM2], |
| 440 |
host="open.getsession.org", |
| 441 |
) |
| 442 |
assert bool(obj.notify(body="Multi")) is True |
| 443 |
assert mock_post.call_count == 2 |
| 444 |
|
| 445 |
|
| 446 |
@pytest.mark.skipif( |
| 447 |
"cryptography" not in sys.modules, |
| 448 |
reason="Requires cryptography", |
| 449 |
) |
| 450 |
@mock.patch("requests.post") |
| 451 |
def test_plugin_sogs_send_partial_failure(mock_post): |
| 452 |
"""Failure on any room causes the overall notify() to return False.""" |
| 453 |
|
| 454 |
def _mk_resp(code): |
| 455 |
r = mock.Mock() |
| 456 |
r.status_code = code |
| 457 |
r.content = b"" |
| 458 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 459 |
return r |
| 460 |
|
| 461 |
mock_post.side_effect = [ |
| 462 |
_mk_resp(requests.codes.created), |
| 463 |
_mk_resp(requests.codes.forbidden), |
| 464 |
] |
| 465 |
|
| 466 |
obj = NotifySessionOGS( |
| 467 |
public_key=PUBLIC_KEY, |
| 468 |
seed=SEED, |
| 469 |
targets=[ROOM, ROOM2], |
| 470 |
host="open.getsession.org", |
| 471 |
) |
| 472 |
assert bool(obj.notify(body="Partial")) is False |
| 473 |
assert mock_post.call_count == 2 |
| 474 |
|
| 475 |
|
| 476 |
@pytest.mark.skipif( |
| 477 |
"cryptography" not in sys.modules, |
| 478 |
reason="Requires cryptography", |
| 479 |
) |
| 480 |
@mock.patch("requests.post") |
| 481 |
def test_plugin_sogs_http_error(mock_post): |
| 482 |
"""Non-2xx responses cause send() to return False.""" |
| 483 |
|
| 484 |
def _mk_resp(code): |
| 485 |
r = mock.Mock() |
| 486 |
r.status_code = code |
| 487 |
r.content = b"" |
| 488 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 489 |
return r |
| 490 |
|
| 491 |
obj = NotifySessionOGS( |
| 492 |
public_key=PUBLIC_KEY, |
| 493 |
seed=SEED, |
| 494 |
targets=[ROOM], |
| 495 |
host="open.getsession.org", |
| 496 |
) |
| 497 |
|
| 498 |
for code in ( |
| 499 |
requests.codes.bad_request, |
| 500 |
requests.codes.unauthorized, |
| 501 |
requests.codes.forbidden, |
| 502 |
requests.codes.not_found, |
| 503 |
requests.codes.internal_server_error, |
| 504 |
999, |
| 505 |
): |
| 506 |
mock_post.reset_mock() |
| 507 |
mock_post.return_value = _mk_resp(code) |
| 508 |
assert bool(obj.notify(body="Error")) is False |
| 509 |
|
| 510 |
|
| 511 |
@pytest.mark.skipif( |
| 512 |
"cryptography" not in sys.modules, |
| 513 |
reason="Requires cryptography", |
| 514 |
) |
| 515 |
@mock.patch("requests.post") |
| 516 |
def test_plugin_sogs_request_exception(mock_post): |
| 517 |
"""requests.RequestException causes send() to return False.""" |
| 518 |
mock_post.side_effect = requests.RequestException("network error") |
| 519 |
|
| 520 |
obj = NotifySessionOGS( |
| 521 |
public_key=PUBLIC_KEY, |
| 522 |
seed=SEED, |
| 523 |
targets=[ROOM], |
| 524 |
host="open.getsession.org", |
| 525 |
) |
| 526 |
assert bool(obj.notify(body="Error")) is False |
| 527 |
|
| 528 |
|
| 529 |
@pytest.mark.skipif( |
| 530 |
"cryptography" not in sys.modules, |
| 531 |
reason="Requires cryptography", |
| 532 |
) |
| 533 |
@mock.patch("requests.post") |
| 534 |
def test_plugin_sogs_http_insecure(mock_post): |
| 535 |
"""session:// sends over plain HTTP.""" |
| 536 |
|
| 537 |
def _mk_resp(): |
| 538 |
r = mock.Mock() |
| 539 |
r.status_code = requests.codes.created |
| 540 |
r.content = b'{"id": 1}' |
| 541 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 542 |
return r |
| 543 |
|
| 544 |
mock_post.return_value = _mk_resp() |
| 545 |
|
| 546 |
obj = Apprise() |
| 547 |
assert obj.add(f"session://{PUBLIC_KEY}:{SEED}@open.getsession.org/{ROOM}") |
| 548 |
assert bool(obj.notify(body="Plain HTTP")) is True |
| 549 |
|
| 550 |
# Confirm the request was sent to http:// not https://. |
| 551 |
url_called = mock_post.call_args[0][0] |
| 552 |
assert url_called.startswith("http://") |
| 553 |
assert "https://" not in url_called |
| 554 |
|
| 555 |
|
| 556 |
@pytest.mark.skipif( |
| 557 |
"cryptography" not in sys.modules, |
| 558 |
reason="Requires cryptography", |
| 559 |
) |
| 560 |
@mock.patch("requests.post") |
| 561 |
def test_plugin_sogs_url_and_privacy(mock_post): |
| 562 |
"""url() round-trips and masks seed in privacy mode.""" |
| 563 |
|
| 564 |
def _mk_resp(): |
| 565 |
r = mock.Mock() |
| 566 |
r.status_code = requests.codes.created |
| 567 |
r.content = b'{"id": 1}' |
| 568 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 569 |
return r |
| 570 |
|
| 571 |
mock_post.return_value = _mk_resp() |
| 572 |
|
| 573 |
obj = NotifySessionOGS( |
| 574 |
public_key=PUBLIC_KEY, |
| 575 |
seed=SEED, |
| 576 |
targets=[ROOM], |
| 577 |
host="open.getsession.org", |
| 578 |
) |
| 579 |
|
| 580 |
full_url = obj.url(privacy=False) |
| 581 |
assert SEED in full_url |
| 582 |
assert PUBLIC_KEY in full_url |
| 583 |
|
| 584 |
priv_url = obj.url(privacy=True) |
| 585 |
assert SEED not in priv_url |
| 586 |
assert PUBLIC_KEY in priv_url |
| 587 |
|
| 588 |
# Round-trip. |
| 589 |
parsed = NotifySessionOGS.parse_url(full_url) |
| 590 |
obj2 = NotifySessionOGS(**parsed) |
| 591 |
assert obj.url_identifier == obj2.url_identifier |
| 592 |
|
| 593 |
|
| 594 |
@pytest.mark.skipif( |
| 595 |
"cryptography" not in sys.modules, |
| 596 |
reason="Requires cryptography", |
| 597 |
) |
| 598 |
@mock.patch("requests.post") |
| 599 |
def test_plugin_sogs_custom_port(mock_post): |
| 600 |
"""Custom port is encoded in the URL and used in requests.""" |
| 601 |
|
| 602 |
def _mk_resp(): |
| 603 |
r = mock.Mock() |
| 604 |
r.status_code = requests.codes.created |
| 605 |
r.content = b'{"id": 1}' |
| 606 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 607 |
return r |
| 608 |
|
| 609 |
mock_post.return_value = _mk_resp() |
| 610 |
|
| 611 |
obj = NotifySessionOGS( |
| 612 |
public_key=PUBLIC_KEY, |
| 613 |
seed=SEED, |
| 614 |
targets=[ROOM], |
| 615 |
host="open.getsession.org", |
| 616 |
port=8443, |
| 617 |
) |
| 618 |
assert ":8443" in obj.url() |
| 619 |
assert bool(obj.notify(body="custom port")) is True |
| 620 |
|
| 621 |
url_called = mock_post.call_args[0][0] |
| 622 |
assert ":8443" in url_called |
| 623 |
|
| 624 |
|
| 625 |
def test_plugin_sogs_sogs_alias(): |
| 626 |
"""sogs:// is accepted as an alias for sessions:// (HTTPS).""" |
| 627 |
|
| 628 |
# parse_url must recognise the sogs:// schema. |
| 629 |
url = f"sogs://{PUBLIC_KEY}:{SEED}@open.getsession.org/{ROOM}" |
| 630 |
results = NotifySessionOGS.parse_url(url) |
| 631 |
assert results is not None |
| 632 |
assert results["public_key"] == PUBLIC_KEY |
| 633 |
assert results["seed"] == SEED |
| 634 |
assert ROOM in results["targets"] |
| 635 |
|
| 636 |
# The parsed schema must be "sogs" (the alias used in the input URL). |
| 637 |
assert results["schema"] == "sogs" |
| 638 |
|
| 639 |
|
| 640 |
def test_plugin_sogs_parse_url(): |
| 641 |
"""parse_url extracts all expected fields.""" |
| 642 |
|
| 643 |
# Canonical format: public_key:seed in authority, rooms in path. |
| 644 |
url = f"sessions://{PUBLIC_KEY}:{SEED}@open.getsession.org/{ROOM}/{ROOM2}" |
| 645 |
results = NotifySessionOGS.parse_url(url) |
| 646 |
assert results["public_key"] == PUBLIC_KEY |
| 647 |
assert results["seed"] == SEED |
| 648 |
assert ROOM in results["targets"] |
| 649 |
assert ROOM2 in results["targets"] |
| 650 |
|
| 651 |
# ?key= query param provides the public key (no user in URL). |
| 652 |
url2 = ( |
| 653 |
f"sessions://open.getsession.org/{ROOM}?key={PUBLIC_KEY}&seed={SEED}" |
| 654 |
) |
| 655 |
results2 = NotifySessionOGS.parse_url(url2) |
| 656 |
assert results2["public_key"] == PUBLIC_KEY |
| 657 |
assert results2["seed"] == SEED |
| 658 |
|
| 659 |
# ?public_key= is the Session-native alias and overrides ?key= when both |
| 660 |
# are present. |
| 661 |
url2b = ( |
| 662 |
f"sessions://open.getsession.org/{ROOM}" |
| 663 |
f"?public_key={PUBLIC_KEY}&seed={SEED}" |
| 664 |
) |
| 665 |
results2b = NotifySessionOGS.parse_url(url2b) |
| 666 |
assert results2b["public_key"] == PUBLIC_KEY |
| 667 |
assert results2b["seed"] == SEED |
| 668 |
|
| 669 |
# ?to= alias populates targets (no room in path). |
| 670 |
url3 = f"sessions://{PUBLIC_KEY}:{SEED}@open.getsession.org?to={ROOM}" |
| 671 |
results3 = NotifySessionOGS.parse_url(url3) |
| 672 |
assert ROOM in results3["targets"] |
| 673 |
|
| 674 |
# No password field -> seed is None. |
| 675 |
url4 = f"sessions://{PUBLIC_KEY}@open.getsession.org/{ROOM}" |
| 676 |
results4 = NotifySessionOGS.parse_url(url4) |
| 677 |
assert results4["seed"] is None |
| 678 |
|
| 679 |
# Empty URL -> None (host is required; verify_host=True is the default). |
| 680 |
assert NotifySessionOGS.parse_url("sessions://") is None |
| 681 |
|
| 682 |
|
| 683 |
@pytest.mark.skipif( |
| 684 |
"cryptography" not in sys.modules, |
| 685 |
reason="Requires cryptography", |
| 686 |
) |
| 687 |
@mock.patch("requests.post") |
| 688 |
def test_plugin_sogs_invalid_room_token(mock_post): |
| 689 |
"""Invalid room tokens are stored in invalid_rooms and survive url().""" |
| 690 |
|
| 691 |
def _mk_resp(): |
| 692 |
r = mock.Mock() |
| 693 |
r.status_code = requests.codes.created |
| 694 |
r.content = b'{"id": 1}' |
| 695 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 696 |
return r |
| 697 |
|
| 698 |
mock_post.return_value = _mk_resp() |
| 699 |
|
| 700 |
# One valid + one invalid: must succeed with at least one valid room. |
| 701 |
obj = NotifySessionOGS( |
| 702 |
public_key=PUBLIC_KEY, |
| 703 |
seed=SEED, |
| 704 |
targets=[ROOM, "!!!bad!!!"], |
| 705 |
host="open.getsession.org", |
| 706 |
) |
| 707 |
assert obj.rooms == [ROOM] |
| 708 |
assert len(obj.invalid_rooms) == 1 |
| 709 |
|
| 710 |
# Invalid rooms survive url() round-trip (no silent data loss). |
| 711 |
full_url = obj.url() |
| 712 |
parsed = NotifySessionOGS.parse_url(full_url) |
| 713 |
obj2 = NotifySessionOGS(**parsed) |
| 714 |
assert obj.url_identifier == obj2.url_identifier |
| 715 |
|
| 716 |
|
| 717 |
@pytest.mark.skipif( |
| 718 |
"cryptography" not in sys.modules, |
| 719 |
reason="Requires cryptography", |
| 720 |
) |
| 721 |
@mock.patch("requests.post") |
| 722 |
def test_plugin_sogs_url_identifier(mock_post): |
| 723 |
"""url_identifier distinguishes connections by public_key and host.""" |
| 724 |
|
| 725 |
def _mk_obj(host, public_key=PUBLIC_KEY): |
| 726 |
return NotifySessionOGS( |
| 727 |
public_key=public_key, |
| 728 |
seed=SEED, |
| 729 |
targets=[ROOM], |
| 730 |
host=host, |
| 731 |
) |
| 732 |
|
| 733 |
obj_a = _mk_obj("server-a.example.com") |
| 734 |
obj_b = _mk_obj("server-b.example.com") |
| 735 |
obj_c = _mk_obj("server-a.example.com", public_key="d" * 64) |
| 736 |
|
| 737 |
# Same host + same public_key -> same identifier. |
| 738 |
obj_a2 = _mk_obj("server-a.example.com") |
| 739 |
assert obj_a.url_identifier == obj_a2.url_identifier |
| 740 |
|
| 741 |
# Different host -> different identifier. |
| 742 |
assert obj_a.url_identifier != obj_b.url_identifier |
| 743 |
|
| 744 |
# Same host, different public_key -> different identifier. |
| 745 |
assert obj_a.url_identifier != obj_c.url_identifier |
| 746 |
|
| 747 |
|
| 748 |
@pytest.mark.skipif( |
| 749 |
"cryptography" not in sys.modules, |
| 750 |
reason="Requires cryptography", |
| 751 |
) |
| 752 |
@mock.patch("requests.post") |
| 753 |
def test_plugin_sogs_auth_headers(mock_post): |
| 754 |
"""X-SOGS-* headers are present and well-formed on every request.""" |
| 755 |
|
| 756 |
def _mk_resp(): |
| 757 |
r = mock.Mock() |
| 758 |
r.status_code = requests.codes.created |
| 759 |
r.content = b'{"id": 1}' |
| 760 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 761 |
return r |
| 762 |
|
| 763 |
mock_post.return_value = _mk_resp() |
| 764 |
|
| 765 |
obj = NotifySessionOGS( |
| 766 |
public_key=PUBLIC_KEY, |
| 767 |
seed=SEED, |
| 768 |
targets=[ROOM], |
| 769 |
host="open.getsession.org", |
| 770 |
) |
| 771 |
assert bool(obj.notify(body="Auth test")) is True |
| 772 |
|
| 773 |
call_kwargs = mock_post.call_args[1] |
| 774 |
headers = call_kwargs.get("headers", {}) |
| 775 |
|
| 776 |
# All four X-SOGS-* headers must be present. |
| 777 |
assert "X-SOGS-Pubkey" in headers |
| 778 |
assert "X-SOGS-Nonce" in headers |
| 779 |
assert "X-SOGS-Timestamp" in headers |
| 780 |
assert "X-SOGS-Signature" in headers |
| 781 |
|
| 782 |
# Pubkey starts with "00" (unblinded) then 64 hex chars. |
| 783 |
pk = headers["X-SOGS-Pubkey"] |
| 784 |
assert pk.startswith("00") |
| 785 |
assert len(pk) == 66 # "00" + 32-byte pubkey hex = 2 + 64 |
| 786 |
|
| 787 |
# Timestamp is a digit string. |
| 788 |
assert headers["X-SOGS-Timestamp"].isdigit() |
| 789 |
|
| 790 |
# Verify the no-body branch of _sogs_auth_headers (body_bytes=None |
| 791 |
# skips the blake2b step; the returned dict must still be well-formed). |
| 792 |
obj2 = NotifySessionOGS( |
| 793 |
public_key=PUBLIC_KEY, |
| 794 |
seed=SEED, |
| 795 |
targets=[ROOM], |
| 796 |
host="open.getsession.org", |
| 797 |
) |
| 798 |
no_body_headers = obj2._sogs_auth_headers("GET", "/room/test") |
| 799 |
assert "X-SOGS-Pubkey" in no_body_headers |
| 800 |
assert "X-SOGS-Nonce" in no_body_headers |
| 801 |
assert "X-SOGS-Timestamp" in no_body_headers |
| 802 |
assert "X-SOGS-Signature" in no_body_headers |
| 803 |
|
| 804 |
|
| 805 |
@pytest.mark.skipif( |
| 806 |
"cryptography" not in sys.modules, |
| 807 |
reason="Requires cryptography", |
| 808 |
) |
| 809 |
@mock.patch("requests.post") |
| 810 |
def test_plugin_sogs_response_unparseable_json(mock_post): |
| 811 |
"""Unparseable response body is handled gracefully.""" |
| 812 |
r = mock.Mock() |
| 813 |
r.status_code = requests.codes.created |
| 814 |
r.content = b"not-json" |
| 815 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 816 |
mock_post.return_value = r |
| 817 |
|
| 818 |
obj = NotifySessionOGS( |
| 819 |
public_key=PUBLIC_KEY, |
| 820 |
seed=SEED, |
| 821 |
targets=[ROOM], |
| 822 |
host="open.getsession.org", |
| 823 |
) |
| 824 |
assert bool(obj.notify(body="Hello")) is True |
| 825 |
|
| 826 |
|
| 827 |
@pytest.mark.skipif( |
| 828 |
"cryptography" not in sys.modules, |
| 829 |
reason="Requires cryptography", |
| 830 |
) |
| 831 |
@mock.patch("requests.post") |
| 832 |
def test_plugin_sogs_oversized_response_rejected(mock_post): |
| 833 |
"""Reject a response whose body grows past the acknowledgement limit. |
| 834 |
|
| 835 |
The empty chunk confirms that keep-alive chunks are ignored. |
| 836 |
""" |
| 837 |
over_limit = b"x" * (NotifySessionOGS.max_response_size + 1) |
| 838 |
r = mock.Mock() |
| 839 |
r.status_code = requests.codes.created |
| 840 |
r.headers = {} |
| 841 |
r.iter_content = lambda chunk_size=None: iter([b"", over_limit]) |
| 842 |
mock_post.return_value = r |
| 843 |
|
| 844 |
obj = NotifySessionOGS( |
| 845 |
public_key=PUBLIC_KEY, |
| 846 |
seed=SEED, |
| 847 |
targets=[ROOM], |
| 848 |
host="open.getsession.org", |
| 849 |
) |
| 850 |
assert bool(obj.notify(body="Hello")) is False |
| 851 |
r.close.assert_called_once() |
| 852 |
|
| 853 |
|
| 854 |
@pytest.mark.skipif( |
| 855 |
"cryptography" not in sys.modules, |
| 856 |
reason="Requires cryptography", |
| 857 |
) |
| 858 |
@mock.patch("requests.post") |
| 859 |
def test_plugin_sogs_closes_connection_after_read(mock_post): |
| 860 |
"""The streamed connection is closed once the body is read.""" |
| 861 |
r = mock.Mock() |
| 862 |
r.status_code = requests.codes.created |
| 863 |
r.content = b'{"id": 1}' |
| 864 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 865 |
mock_post.return_value = r |
| 866 |
|
| 867 |
obj = NotifySessionOGS( |
| 868 |
public_key=PUBLIC_KEY, |
| 869 |
seed=SEED, |
| 870 |
targets=[ROOM], |
| 871 |
host="open.getsession.org", |
| 872 |
) |
| 873 |
assert bool(obj.notify(body="Hello")) is True |
| 874 |
r.close.assert_called_once() |
| 875 |
|
| 876 |
|
| 877 |
def test_plugin_sogs_build_session_message(): |
| 878 |
"""_build_session_message produces valid padded protobuf bytes.""" |
| 879 |
msg = _build_session_message("hello") |
| 880 |
|
| 881 |
# Must be bytes and non-empty. |
| 882 |
assert isinstance(msg, bytes) |
| 883 |
assert len(msg) > 0 |
| 884 |
|
| 885 |
# Ends with the Session padding marker 0x80. |
| 886 |
assert msg[-1] == 0x80 |
| 887 |
|
| 888 |
# Starts with protobuf tag 0x0A (field 1, wire type 2). |
| 889 |
assert msg[0] == 0x0A |
| 890 |
|
| 891 |
|
| 892 |
def test_plugin_sogs_encode_varint(): |
| 893 |
"""_encode_varint encodes small and multi-byte integers correctly.""" |
| 894 |
assert _encode_varint(0) == b"\x00" |
| 895 |
assert _encode_varint(1) == b"\x01" |
| 896 |
assert _encode_varint(127) == b"\x7f" |
| 897 |
assert _encode_varint(128) == b"\x80\x01" |
| 898 |
assert _encode_varint(300) == b"\xac\x02" |
| 899 |
|
| 900 |
|
| 901 |
def test_plugin_sogs_ld_field(): |
| 902 |
"""_ld_field encodes a length-delimited protobuf field.""" |
| 903 |
encoded = _ld_field(1, b"hi") |
| 904 |
assert encoded == b"\x0a\x02hi" |
| 905 |
|
| 906 |
|
| 907 |
def test_plugin_sogs_runtime_deps(): |
| 908 |
"""runtime_deps() returns the cryptography package name.""" |
| 909 |
deps = NotifySessionOGS.runtime_deps() |
| 910 |
assert "cryptography" in deps |
| 911 |
|
| 912 |
|
| 913 |
@pytest.mark.skipif( |
| 914 |
"cryptography" not in sys.modules, |
| 915 |
reason="Requires cryptography", |
| 916 |
) |
| 917 |
@mock.patch("requests.post") |
| 918 |
def test_plugin_sogs_apprise_integration(mock_post): |
| 919 |
"""Apprise.add() and Apprise.notify() work end-to-end.""" |
| 920 |
|
| 921 |
def _mk_resp(): |
| 922 |
r = mock.Mock() |
| 923 |
r.status_code = requests.codes.created |
| 924 |
r.content = b'{"id": 1}' |
| 925 |
r.iter_content = lambda chunk_size=None: iter([r.content]) |
| 926 |
return r |
| 927 |
|
| 928 |
mock_post.return_value = _mk_resp() |
| 929 |
|
| 930 |
app = Apprise() |
| 931 |
assert app.add(BASE_URL) |
| 932 |
assert bool(app.notify(title="Title", body="Body")) is True |
| 933 |
assert mock_post.call_count == 1 |