| 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 |
from unittest import mock |
| 31 |
|
| 32 |
from helpers import AppriseURLTester |
| 33 |
import pytest |
| 34 |
import requests |
| 35 |
|
| 36 |
from apprise import Apprise |
| 37 |
from apprise.exception import AppriseImproperlyConfigured |
| 38 |
from apprise.plugins.base import _delivery_tracker |
| 39 |
from apprise.plugins.humhub import NotifyHumHub |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
# Our Testing URLs |
| 44 |
apprise_url_tests = ( |
| 45 |
# No host |
| 46 |
( |
| 47 |
"humhub://", |
| 48 |
{ |
| 49 |
"instance": None, |
| 50 |
}, |
| 51 |
), |
| 52 |
# Host present but no user/token |
| 53 |
( |
| 54 |
"humhub://hostname", |
| 55 |
{ |
| 56 |
"instance": AppriseImproperlyConfigured, |
| 57 |
}, |
| 58 |
), |
| 59 |
# Token present but no container ID |
| 60 |
( |
| 61 |
"humhub://token@hostname", |
| 62 |
{ |
| 63 |
"instance": AppriseImproperlyConfigured, |
| 64 |
}, |
| 65 |
), |
| 66 |
# Invalid container ID (not numeric) |
| 67 |
( |
| 68 |
"humhubs://token@hostname/invalid", |
| 69 |
{ |
| 70 |
"instance": AppriseImproperlyConfigured, |
| 71 |
}, |
| 72 |
), |
| 73 |
# Negative container ID (not a positive integer) |
| 74 |
( |
| 75 |
"humhubs://token@hostname/-1", |
| 76 |
{ |
| 77 |
"instance": AppriseImproperlyConfigured, |
| 78 |
}, |
| 79 |
), |
| 80 |
# Zero container ID (not a positive integer) |
| 81 |
( |
| 82 |
"humhubs://token@hostname/0", |
| 83 |
{ |
| 84 |
"instance": AppriseImproperlyConfigured, |
| 85 |
}, |
| 86 |
), |
| 87 |
# Valid bearer token + container ID over HTTP (insecure) |
| 88 |
( |
| 89 |
"humhub://mytoken@hostname/1", |
| 90 |
{ |
| 91 |
"instance": NotifyHumHub, |
| 92 |
"privacy_url": "humhub://m...n@hostname/1/", |
| 93 |
"requests_response_text": '{"id": 1}', |
| 94 |
}, |
| 95 |
), |
| 96 |
# Valid bearer token + container ID over HTTPS |
| 97 |
( |
| 98 |
"humhubs://mytoken@hostname/1", |
| 99 |
{ |
| 100 |
"instance": NotifyHumHub, |
| 101 |
"privacy_url": "humhubs://m...n@hostname/1/", |
| 102 |
"requests_response_text": '{"id": 1}', |
| 103 |
}, |
| 104 |
), |
| 105 |
# Valid basic auth + container ID |
| 106 |
( |
| 107 |
"humhubs://user:pass@hostname/1", |
| 108 |
{ |
| 109 |
"instance": NotifyHumHub, |
| 110 |
"privacy_url": "humhubs://u...r:****@hostname/1/", |
| 111 |
"requests_response_text": '{"id": 1}', |
| 112 |
}, |
| 113 |
), |
| 114 |
# Multiple container IDs in the path |
| 115 |
( |
| 116 |
"humhubs://mytoken@hostname/1/2/3", |
| 117 |
{ |
| 118 |
"instance": NotifyHumHub, |
| 119 |
"requests_response_text": '{"id": 1}', |
| 120 |
}, |
| 121 |
), |
| 122 |
# Container IDs via ?to= parameter |
| 123 |
( |
| 124 |
"humhubs://mytoken@hostname/?to=1,2,3", |
| 125 |
{ |
| 126 |
"instance": NotifyHumHub, |
| 127 |
"requests_response_text": '{"id": 1}', |
| 128 |
}, |
| 129 |
), |
| 130 |
# Custom port |
| 131 |
( |
| 132 |
"humhubs://mytoken@hostname:8443/1", |
| 133 |
{ |
| 134 |
"instance": NotifyHumHub, |
| 135 |
"requests_response_text": '{"id": 1}', |
| 136 |
}, |
| 137 |
), |
| 138 |
# HTTP 500 response -- bearer auth |
| 139 |
( |
| 140 |
"humhubs://mytoken@hostname/1", |
| 141 |
{ |
| 142 |
"instance": NotifyHumHub, |
| 143 |
"response": False, |
| 144 |
"requests_response_code": requests.codes.internal_server_error, |
| 145 |
}, |
| 146 |
), |
| 147 |
# Unknown error code -- bearer auth |
| 148 |
( |
| 149 |
"humhubs://mytoken@hostname/1", |
| 150 |
{ |
| 151 |
"instance": NotifyHumHub, |
| 152 |
"response": False, |
| 153 |
"requests_response_code": 999, |
| 154 |
}, |
| 155 |
), |
| 156 |
# RequestException -- bearer auth |
| 157 |
( |
| 158 |
"humhubs://mytoken@hostname/1", |
| 159 |
{ |
| 160 |
"instance": NotifyHumHub, |
| 161 |
"test_requests_exceptions": True, |
| 162 |
}, |
| 163 |
), |
| 164 |
# HTTP 500 response -- basic auth |
| 165 |
( |
| 166 |
"humhubs://user:pass@hostname/1", |
| 167 |
{ |
| 168 |
"instance": NotifyHumHub, |
| 169 |
"response": False, |
| 170 |
"requests_response_code": requests.codes.internal_server_error, |
| 171 |
}, |
| 172 |
), |
| 173 |
# Unknown error code -- basic auth |
| 174 |
( |
| 175 |
"humhubs://user:pass@hostname/1", |
| 176 |
{ |
| 177 |
"instance": NotifyHumHub, |
| 178 |
"response": False, |
| 179 |
"requests_response_code": 999, |
| 180 |
}, |
| 181 |
), |
| 182 |
# RequestException -- basic auth |
| 183 |
( |
| 184 |
"humhubs://user:pass@hostname/1", |
| 185 |
{ |
| 186 |
"instance": NotifyHumHub, |
| 187 |
"test_requests_exceptions": True, |
| 188 |
}, |
| 189 |
), |
| 190 |
) |
| 191 |
|
| 192 |
|
| 193 |
def test_plugin_humhub_urls(): |
| 194 |
"""NotifyHumHub() Apprise URLs.""" |
| 195 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 196 |
|
| 197 |
|
| 198 |
def test_plugin_humhub_init(): |
| 199 |
"""NotifyHumHub() initialization edge cases.""" |
| 200 |
|
| 201 |
# No user/token at all |
| 202 |
with pytest.raises(AppriseImproperlyConfigured): |
| 203 |
NotifyHumHub(user=None, host="hostname", targets=["1"]) |
| 204 |
|
| 205 |
# Empty string user |
| 206 |
with pytest.raises(AppriseImproperlyConfigured): |
| 207 |
NotifyHumHub(user="", host="hostname", targets=["1"]) |
| 208 |
|
| 209 |
# No containers specified |
| 210 |
with pytest.raises(AppriseImproperlyConfigured): |
| 211 |
NotifyHumHub(user="token", host="hostname", targets=[]) |
| 212 |
|
| 213 |
# None targets |
| 214 |
with pytest.raises(AppriseImproperlyConfigured): |
| 215 |
NotifyHumHub(user="token", host="hostname", targets=None) |
| 216 |
|
| 217 |
# Only invalid targets |
| 218 |
with pytest.raises(AppriseImproperlyConfigured): |
| 219 |
NotifyHumHub(user="token", host="hostname", targets=["bad", "0", "-5"]) |
| 220 |
|
| 221 |
# Mixed valid and invalid -- should succeed, dropping the bad ones |
| 222 |
obj = NotifyHumHub( |
| 223 |
user="token", host="hostname", targets=["1", "bad", "2"] |
| 224 |
) |
| 225 |
assert len(obj.targets) == 2 |
| 226 |
assert "1" in obj.targets |
| 227 |
assert "2" in obj.targets |
| 228 |
assert len(obj._invalid_targets) == 1 |
| 229 |
assert "bad" in obj._invalid_targets |
| 230 |
|
| 231 |
# Valid bearer token |
| 232 |
obj = NotifyHumHub(user="bearertoken", host="hostname", targets=["42"]) |
| 233 |
assert obj.targets == ["42"] |
| 234 |
assert obj.password is None |
| 235 |
|
| 236 |
# Valid basic auth |
| 237 |
obj = NotifyHumHub( |
| 238 |
user="admin", password="secret", host="hostname", targets=["5"] |
| 239 |
) |
| 240 |
assert obj.targets == ["5"] |
| 241 |
assert obj.password == "secret" |
| 242 |
|
| 243 |
|
| 244 |
@mock.patch("requests.post") |
| 245 |
def test_plugin_humhub_bearer_send(mock_post): |
| 246 |
"""NotifyHumHub() bearer token send.""" |
| 247 |
|
| 248 |
# Successful response |
| 249 |
mock_post.return_value = requests.Request() |
| 250 |
mock_post.return_value.status_code = requests.codes.ok |
| 251 |
mock_post.return_value.content = b"" |
| 252 |
|
| 253 |
obj = NotifyHumHub(user="mytoken", host="localhost", targets=["1"]) |
| 254 |
assert bool(obj.notify(body="Test body")) is True |
| 255 |
|
| 256 |
# Confirm bearer token header was set |
| 257 |
assert mock_post.called |
| 258 |
_, kwargs = mock_post.call_args |
| 259 |
assert "Authorization" in kwargs.get("headers", {}) |
| 260 |
assert kwargs["headers"]["Authorization"] == "Bearer mytoken" |
| 261 |
assert kwargs.get("auth") is None |
| 262 |
|
| 263 |
|
| 264 |
@mock.patch("requests.post") |
| 265 |
def test_plugin_humhub_basic_send(mock_post): |
| 266 |
"""NotifyHumHub() basic auth send.""" |
| 267 |
|
| 268 |
# Successful response |
| 269 |
mock_post.return_value = requests.Request() |
| 270 |
mock_post.return_value.status_code = requests.codes.ok |
| 271 |
mock_post.return_value.content = b"" |
| 272 |
|
| 273 |
obj = NotifyHumHub( |
| 274 |
user="admin", password="secret", host="localhost", targets=["1"] |
| 275 |
) |
| 276 |
assert bool(obj.notify(body="Test body")) is True |
| 277 |
|
| 278 |
# Confirm basic auth tuple was passed and no Authorization header |
| 279 |
assert mock_post.called |
| 280 |
_, kwargs = mock_post.call_args |
| 281 |
assert kwargs.get("auth") == ("admin", "secret") |
| 282 |
assert "Authorization" not in kwargs.get("headers", {}) |
| 283 |
|
| 284 |
|
| 285 |
@mock.patch("requests.post") |
| 286 |
def test_plugin_humhub_multi_container(mock_post): |
| 287 |
"""NotifyHumHub() multiple container send.""" |
| 288 |
|
| 289 |
mock_post.return_value = requests.Request() |
| 290 |
mock_post.return_value.status_code = requests.codes.ok |
| 291 |
mock_post.return_value.content = b"" |
| 292 |
|
| 293 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1", "2", "3"]) |
| 294 |
assert bool(obj.notify(body="Hello")) is True |
| 295 |
|
| 296 |
# One POST per container |
| 297 |
assert mock_post.call_count == 3 |
| 298 |
|
| 299 |
|
| 300 |
@mock.patch("requests.post") |
| 301 |
def test_plugin_humhub_partial_failure(mock_post): |
| 302 |
"""NotifyHumHub() partial failure across multiple containers.""" |
| 303 |
|
| 304 |
def side_effect_fn(*args, **kwargs): |
| 305 |
# Fail on the second call (container "2"), succeed on others |
| 306 |
if mock_post.call_count == 2: |
| 307 |
r = requests.Request() |
| 308 |
r.status_code = requests.codes.internal_server_error |
| 309 |
r.content = b"error" |
| 310 |
return r |
| 311 |
r = requests.Request() |
| 312 |
r.status_code = requests.codes.ok |
| 313 |
r.content = b"" |
| 314 |
return r |
| 315 |
|
| 316 |
mock_post.side_effect = side_effect_fn |
| 317 |
|
| 318 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1", "2", "3"]) |
| 319 |
# Partial failure means overall False |
| 320 |
assert bool(obj.notify(body="msg")) is False |
| 321 |
assert mock_post.call_count == 3 |
| 322 |
|
| 323 |
|
| 324 |
@mock.patch("requests.post") |
| 325 |
def test_plugin_humhub_http_errors(mock_post): |
| 326 |
"""NotifyHumHub() HTTP error responses.""" |
| 327 |
|
| 328 |
# HTTP 401 Unauthorized |
| 329 |
mock_post.return_value = requests.Request() |
| 330 |
mock_post.return_value.status_code = 401 |
| 331 |
mock_post.return_value.content = b"Unauthorized" |
| 332 |
|
| 333 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 334 |
assert bool(obj.notify(body="msg")) is False |
| 335 |
|
| 336 |
# HTTP 500 Internal Server Error |
| 337 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 338 |
assert bool(obj.notify(body="msg")) is False |
| 339 |
|
| 340 |
# Unknown / unexpected code |
| 341 |
mock_post.return_value.status_code = 999 |
| 342 |
assert bool(obj.notify(body="msg")) is False |
| 343 |
|
| 344 |
|
| 345 |
@mock.patch("requests.post") |
| 346 |
def test_plugin_humhub_request_exception(mock_post): |
| 347 |
"""NotifyHumHub() RequestException handling.""" |
| 348 |
|
| 349 |
mock_post.side_effect = requests.RequestException("Connection refused") |
| 350 |
|
| 351 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1", "2"]) |
| 352 |
assert bool(obj.notify(body="msg")) is False |
| 353 |
# Both containers attempted |
| 354 |
assert mock_post.call_count == 2 |
| 355 |
|
| 356 |
|
| 357 |
def test_plugin_humhub_url_round_trip(): |
| 358 |
"""NotifyHumHub() URL round-trip invariant.""" |
| 359 |
|
| 360 |
# Bearer token, single container |
| 361 |
obj1 = NotifyHumHub(user="mytoken", host="myhost", targets=["7"]) |
| 362 |
result = NotifyHumHub.parse_url(obj1.url()) |
| 363 |
obj2 = NotifyHumHub(**result) |
| 364 |
assert obj1.url_identifier == obj2.url_identifier |
| 365 |
assert len(obj1.targets) == len(obj2.targets) |
| 366 |
|
| 367 |
# Basic auth, multiple containers |
| 368 |
obj1 = NotifyHumHub( |
| 369 |
user="admin", |
| 370 |
password="secret", |
| 371 |
host="myhost", |
| 372 |
targets=["1", "2", "3"], |
| 373 |
) |
| 374 |
result = NotifyHumHub.parse_url(obj1.url()) |
| 375 |
obj2 = NotifyHumHub(**result) |
| 376 |
assert obj1.url_identifier == obj2.url_identifier |
| 377 |
assert len(obj1.targets) == len(obj2.targets) |
| 378 |
|
| 379 |
# Custom port |
| 380 |
obj1 = NotifyHumHub(user="tok", host="myhost", port=9090, targets=["10"]) |
| 381 |
result = NotifyHumHub.parse_url(obj1.url()) |
| 382 |
obj2 = NotifyHumHub(**result) |
| 383 |
assert obj1.url_identifier == obj2.url_identifier |
| 384 |
|
| 385 |
# Invalid targets round-trip inline in path alongside valid ones |
| 386 |
obj1 = NotifyHumHub(user="tok", host="myhost", targets=["5", "bad", "0"]) |
| 387 |
assert len(obj1.targets) == 1 |
| 388 |
assert len(obj1._invalid_targets) == 2 |
| 389 |
# url() emits valid + invalid targets together in the path |
| 390 |
generated = obj1.url() |
| 391 |
assert "bad" in generated |
| 392 |
assert "0" in generated |
| 393 |
result = NotifyHumHub.parse_url(generated) |
| 394 |
obj2 = NotifyHumHub(**result) |
| 395 |
# obj2 re-validates: valid target "5" survives, invalid ones re-land in |
| 396 |
# _invalid_targets so the overall target count is the same |
| 397 |
assert len(obj2.targets) == 1 |
| 398 |
|
| 399 |
|
| 400 |
def test_plugin_humhub_url_parsing(): |
| 401 |
"""NotifyHumHub() parse_url edge cases.""" |
| 402 |
|
| 403 |
# Bearer token from path and ?to= combined |
| 404 |
result = NotifyHumHub.parse_url("humhubs://mytoken@hostname/1/?to=2,3") |
| 405 |
assert result is not None |
| 406 |
assert "1" in result["targets"] |
| 407 |
assert "2" in result["targets"] |
| 408 |
assert "3" in result["targets"] |
| 409 |
|
| 410 |
# Basic auth |
| 411 |
result = NotifyHumHub.parse_url("humhubs://user:pass@hostname/42") |
| 412 |
assert result is not None |
| 413 |
assert result["user"] == "user" |
| 414 |
assert result["password"] == "pass" |
| 415 |
assert "42" in result["targets"] |
| 416 |
|
| 417 |
# Empty / invalid URL returns None (no host) |
| 418 |
assert NotifyHumHub.parse_url("humhubs://") is None |
| 419 |
# Parsing accepts a host alone; initialization later requires a user. |
| 420 |
result = NotifyHumHub.parse_url("humhubs://hostname/1") |
| 421 |
assert result is not None |
| 422 |
|
| 423 |
|
| 424 |
def test_plugin_humhub_privacy_url(): |
| 425 |
"""NotifyHumHub() privacy URL masking.""" |
| 426 |
|
| 427 |
obj = NotifyHumHub(user="secrettoken", host="myhost", targets=["1"]) |
| 428 |
priv = obj.url(privacy=True) |
| 429 |
assert "secrettoken" not in priv |
| 430 |
assert "myhost" in priv |
| 431 |
|
| 432 |
obj2 = NotifyHumHub( |
| 433 |
user="admin", password="topsecret", host="myhost", targets=["1"] |
| 434 |
) |
| 435 |
priv2 = obj2.url(privacy=True) |
| 436 |
assert "topsecret" not in priv2 |
| 437 |
assert "admin" not in priv2 or "****" in priv2 |
| 438 |
|
| 439 |
|
| 440 |
@mock.patch("requests.post") |
| 441 |
def test_plugin_humhub_apprise_integration(mock_post): |
| 442 |
"""NotifyHumHub() Apprise integration.""" |
| 443 |
|
| 444 |
mock_post.return_value = requests.Request() |
| 445 |
mock_post.return_value.status_code = requests.codes.ok |
| 446 |
mock_post.return_value.content = b"" |
| 447 |
|
| 448 |
aobj = Apprise() |
| 449 |
assert aobj.add("humhubs://mytoken@localhost/1") |
| 450 |
assert bool(aobj.notify(title="Title", body="Body")) is True |
| 451 |
|
| 452 |
aobj2 = Apprise() |
| 453 |
assert aobj2.add("humhubs://user:pass@localhost/1") |
| 454 |
assert bool(aobj2.notify(title="Title", body="Body")) is True |
| 455 |
|
| 456 |
|
| 457 |
@mock.patch("requests.post") |
| 458 |
def test_plugin_humhub_attachment_success(mock_post): |
| 459 |
"""NotifyHumHub() attachment upload success.""" |
| 460 |
from io import BytesIO |
| 461 |
|
| 462 |
# First response: post creation with a post ID |
| 463 |
create_resp = requests.Request() |
| 464 |
create_resp.status_code = requests.codes.ok |
| 465 |
create_resp.content = b'{"id": 42}' |
| 466 |
|
| 467 |
# Second response: attachment upload success |
| 468 |
upload_resp = requests.Request() |
| 469 |
upload_resp.status_code = requests.codes.ok |
| 470 |
upload_resp.content = b"{}" |
| 471 |
|
| 472 |
mock_post.side_effect = [create_resp, upload_resp] |
| 473 |
|
| 474 |
# Build a mock accessible attachment |
| 475 |
attachment = mock.MagicMock() |
| 476 |
attachment.__bool__ = mock.MagicMock(return_value=True) |
| 477 |
attachment.name = "test.png" |
| 478 |
attachment.open = mock.MagicMock(return_value=BytesIO(b"image data")) |
| 479 |
|
| 480 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 481 |
assert obj.send(body="Test body", attach=[attachment]) is True |
| 482 |
# Post creation + file upload |
| 483 |
assert mock_post.call_count == 2 |
| 484 |
|
| 485 |
|
| 486 |
@mock.patch("requests.post") |
| 487 |
def test_plugin_humhub_attachment_multiple(mock_post): |
| 488 |
"""NotifyHumHub() multiple attachments all uploaded.""" |
| 489 |
from io import BytesIO |
| 490 |
|
| 491 |
# Post creation returns a post ID |
| 492 |
create_resp = requests.Request() |
| 493 |
create_resp.status_code = requests.codes.ok |
| 494 |
create_resp.content = b'{"id": 10}' |
| 495 |
|
| 496 |
# Three attachment upload responses |
| 497 |
upload_resp = requests.Request() |
| 498 |
upload_resp.status_code = requests.codes.ok |
| 499 |
upload_resp.content = b"{}" |
| 500 |
|
| 501 |
mock_post.side_effect = [ |
| 502 |
create_resp, |
| 503 |
upload_resp, |
| 504 |
upload_resp, |
| 505 |
upload_resp, |
| 506 |
] |
| 507 |
|
| 508 |
def make_attach(name): |
| 509 |
"""Build a mock accessible attachment.""" |
| 510 |
a = mock.MagicMock() |
| 511 |
a.__bool__ = mock.MagicMock(return_value=True) |
| 512 |
a.name = name |
| 513 |
a.open = mock.MagicMock(return_value=BytesIO(b"data")) |
| 514 |
return a |
| 515 |
|
| 516 |
attachments = [ |
| 517 |
make_attach("a.png"), |
| 518 |
make_attach("b.png"), |
| 519 |
make_attach("c.png"), |
| 520 |
] |
| 521 |
|
| 522 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 523 |
assert obj.send(body="msg", attach=attachments) is True |
| 524 |
# 1 creation + 3 uploads |
| 525 |
assert mock_post.call_count == 4 |
| 526 |
|
| 527 |
|
| 528 |
@mock.patch("requests.post") |
| 529 |
def test_plugin_humhub_attachment_inaccessible(mock_post): |
| 530 |
"""NotifyHumHub() inaccessible attachment marks failure.""" |
| 531 |
|
| 532 |
# Post creation succeeds with a post ID |
| 533 |
create_resp = requests.Request() |
| 534 |
create_resp.status_code = requests.codes.ok |
| 535 |
create_resp.content = b'{"id": 7}' |
| 536 |
mock_post.return_value = create_resp |
| 537 |
|
| 538 |
# Build a mock inaccessible attachment (falsy) |
| 539 |
attachment = mock.MagicMock() |
| 540 |
attachment.__bool__ = mock.MagicMock(return_value=False) |
| 541 |
attachment.url = mock.MagicMock(return_value="file:///tmp/missing") |
| 542 |
|
| 543 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 544 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 545 |
# Only the post creation call was made; upload skipped |
| 546 |
assert mock_post.call_count == 1 |
| 547 |
|
| 548 |
|
| 549 |
@mock.patch("requests.post") |
| 550 |
def test_plugin_humhub_attachment_no_post_id(mock_post): |
| 551 |
"""NotifyHumHub() handles missing post ID in creation response.""" |
| 552 |
|
| 553 |
# Post creation returns 200 but no "id" field in the response body |
| 554 |
create_resp = requests.Request() |
| 555 |
create_resp.status_code = requests.codes.ok |
| 556 |
create_resp.content = b"{}" |
| 557 |
mock_post.return_value = create_resp |
| 558 |
|
| 559 |
attachment = mock.MagicMock() |
| 560 |
attachment.__bool__ = mock.MagicMock(return_value=True) |
| 561 |
|
| 562 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 563 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 564 |
# Only the post creation call was made; no upload attempted |
| 565 |
assert mock_post.call_count == 1 |
| 566 |
|
| 567 |
|
| 568 |
@mock.patch("requests.post") |
| 569 |
def test_plugin_humhub_attachment_bad_json_response(mock_post): |
| 570 |
"""NotifyHumHub() handles unparseable post creation response.""" |
| 571 |
|
| 572 |
# Post creation returns 200 but a non-JSON body |
| 573 |
create_resp = requests.Request() |
| 574 |
create_resp.status_code = requests.codes.ok |
| 575 |
create_resp.content = b"not json" |
| 576 |
mock_post.return_value = create_resp |
| 577 |
|
| 578 |
attachment = mock.MagicMock() |
| 579 |
attachment.__bool__ = mock.MagicMock(return_value=True) |
| 580 |
|
| 581 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 582 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 583 |
assert mock_post.call_count == 1 |
| 584 |
|
| 585 |
|
| 586 |
@mock.patch("requests.post") |
| 587 |
def test_plugin_humhub_attachment_upload_failure(mock_post): |
| 588 |
"""NotifyHumHub() attachment upload HTTP failure.""" |
| 589 |
from io import BytesIO |
| 590 |
|
| 591 |
# Post creation succeeds |
| 592 |
create_resp = requests.Request() |
| 593 |
create_resp.status_code = requests.codes.ok |
| 594 |
create_resp.content = b'{"id": 3}' |
| 595 |
|
| 596 |
# Attachment upload returns HTTP 500 |
| 597 |
upload_resp = requests.Request() |
| 598 |
upload_resp.status_code = requests.codes.internal_server_error |
| 599 |
upload_resp.content = b"error" |
| 600 |
|
| 601 |
mock_post.side_effect = [create_resp, upload_resp] |
| 602 |
|
| 603 |
attachment = mock.MagicMock() |
| 604 |
attachment.__bool__ = mock.MagicMock(return_value=True) |
| 605 |
attachment.name = "report.pdf" |
| 606 |
attachment.open = mock.MagicMock(return_value=BytesIO(b"pdf data")) |
| 607 |
|
| 608 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 609 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 610 |
assert mock_post.call_count == 2 |
| 611 |
|
| 612 |
|
| 613 |
@mock.patch("requests.post") |
| 614 |
def test_plugin_humhub_attachment_failure_does_not_repeat_post(mock_post): |
| 615 |
"""An attachment failure does not create a duplicate post.""" |
| 616 |
from io import BytesIO |
| 617 |
|
| 618 |
create_resp = requests.Request() |
| 619 |
create_resp.status_code = requests.codes.ok |
| 620 |
create_resp.content = b'{"id": 3}' |
| 621 |
|
| 622 |
failed_upload = requests.Request() |
| 623 |
failed_upload.status_code = requests.codes.internal_server_error |
| 624 |
failed_upload.content = b"error" |
| 625 |
|
| 626 |
mock_post.side_effect = [create_resp, failed_upload] |
| 627 |
|
| 628 |
attachment = mock.MagicMock() |
| 629 |
attachment.__bool__ = mock.MagicMock(return_value=True) |
| 630 |
attachment.name = "report.pdf" |
| 631 |
attachment.open = mock.MagicMock(return_value=BytesIO(b"pdf data")) |
| 632 |
|
| 633 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 634 |
tracker_token = _delivery_tracker.set(set()) |
| 635 |
try: |
| 636 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 637 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 638 |
finally: |
| 639 |
_delivery_tracker.reset(tracker_token) |
| 640 |
|
| 641 |
create_calls = [ |
| 642 |
call |
| 643 |
for call in mock_post.call_args_list |
| 644 |
if "/api/v1/post/container/1" in call.args[0] |
| 645 |
] |
| 646 |
assert len(create_calls) == 1 |
| 647 |
|
| 648 |
|
| 649 |
@mock.patch("requests.post") |
| 650 |
def test_plugin_humhub_attachment_request_exception(mock_post): |
| 651 |
"""NotifyHumHub() RequestException during attachment upload.""" |
| 652 |
from io import BytesIO |
| 653 |
|
| 654 |
# Post creation succeeds |
| 655 |
create_resp = requests.Request() |
| 656 |
create_resp.status_code = requests.codes.ok |
| 657 |
create_resp.content = b'{"id": 5}' |
| 658 |
|
| 659 |
# Upload raises RequestException |
| 660 |
mock_post.side_effect = [ |
| 661 |
create_resp, |
| 662 |
requests.RequestException("Connection refused"), |
| 663 |
] |
| 664 |
|
| 665 |
attachment = mock.MagicMock() |
| 666 |
attachment.__bool__ = mock.MagicMock(return_value=True) |
| 667 |
attachment.name = "file.txt" |
| 668 |
attachment.open = mock.MagicMock(return_value=BytesIO(b"text data")) |
| 669 |
|
| 670 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 671 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 672 |
|
| 673 |
|
| 674 |
@mock.patch("requests.post") |
| 675 |
def test_plugin_humhub_attachment_oserror(mock_post): |
| 676 |
"""NotifyHumHub() OSError when opening attachment file.""" |
| 677 |
|
| 678 |
# Post creation succeeds |
| 679 |
create_resp = requests.Request() |
| 680 |
create_resp.status_code = requests.codes.ok |
| 681 |
create_resp.content = b'{"id": 8}' |
| 682 |
mock_post.return_value = create_resp |
| 683 |
|
| 684 |
# Attachment open() raises OSError before requests.post is reached |
| 685 |
attachment = mock.MagicMock() |
| 686 |
attachment.__bool__ = mock.MagicMock(return_value=True) |
| 687 |
attachment.name = "secret.key" |
| 688 |
attachment.open = mock.MagicMock(side_effect=OSError("Permission denied")) |
| 689 |
|
| 690 |
obj = NotifyHumHub(user="token", host="localhost", targets=["1"]) |
| 691 |
assert obj.send(body="msg", attach=[attachment]) is False |
| 692 |
# OSError fires inside _send() before requests.post; only creation hit |
| 693 |
assert mock_post.call_count == 1 |