| 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 |
from json import dumps |
| 30 |
import logging |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import requests |
| 35 |
|
| 36 |
from apprise import Apprise, AppriseAsset, NotifyType, PersistentStoreMode |
| 37 |
from apprise.exception import ( |
| 38 |
AppriseImproperlyConfigured, |
| 39 |
ApprisePluginException, |
| 40 |
) |
| 41 |
from apprise.plugins.nextcloud import ( |
| 42 |
NextcloudGroupDiscoveryException, |
| 43 |
NotifyNextcloud, |
| 44 |
) |
| 45 |
|
| 46 |
NEXTCLOUD_GOOD_RESPONSE = dumps( |
| 47 |
{ |
| 48 |
"ocs": { |
| 49 |
"meta": {"status": "ok", "statuscode": 100}, |
| 50 |
"data": {"users": ["user1", "user2"]}, |
| 51 |
} |
| 52 |
} |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def test_plugin_nextcloud_discovery_exception(): |
| 57 |
"""Nextcloud discovery failures use the common plugin exception.""" |
| 58 |
exc = NextcloudGroupDiscoveryException("discovery failed") |
| 59 |
assert isinstance(exc, ApprisePluginException) |
| 60 |
assert exc.error_code == 600 |
| 61 |
|
| 62 |
|
| 63 |
logging.disable(logging.CRITICAL) |
| 64 |
|
| 65 |
apprise_url_tests = ( |
| 66 |
################################## |
| 67 |
# NotifyNextcloud |
| 68 |
################################## |
| 69 |
( |
| 70 |
"ncloud://:@/", |
| 71 |
{ |
| 72 |
"instance": None, |
| 73 |
# Our response expected server response |
| 74 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
"ncloud://", |
| 79 |
{ |
| 80 |
"instance": None, |
| 81 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 82 |
}, |
| 83 |
), |
| 84 |
( |
| 85 |
"nclouds://", |
| 86 |
{ |
| 87 |
# No hostname |
| 88 |
"instance": None, |
| 89 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 90 |
}, |
| 91 |
), |
| 92 |
( |
| 93 |
"ncloud://localhost", |
| 94 |
{ |
| 95 |
# No user specified |
| 96 |
"instance": NotifyNextcloud, |
| 97 |
# Since there are no targets specified we expect a False return on |
| 98 |
# send() |
| 99 |
"notify_response": False, |
| 100 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 101 |
}, |
| 102 |
), |
| 103 |
( |
| 104 |
"ncloud://user@localhost?to=user1,user2&version=invalid", |
| 105 |
{ |
| 106 |
# An invalid version was specified |
| 107 |
"instance": AppriseImproperlyConfigured, |
| 108 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 109 |
}, |
| 110 |
), |
| 111 |
( |
| 112 |
"ncloud://user@localhost?to=user1,user2&version=0", |
| 113 |
{ |
| 114 |
# An invalid version was specified |
| 115 |
"instance": AppriseImproperlyConfigured, |
| 116 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 117 |
}, |
| 118 |
), |
| 119 |
( |
| 120 |
"ncloud://user@localhost?to=user1,user2&version=-23", |
| 121 |
{ |
| 122 |
# An invalid version was specified |
| 123 |
"instance": AppriseImproperlyConfigured, |
| 124 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 125 |
}, |
| 126 |
), |
| 127 |
( |
| 128 |
"ncloud://localhost/admin", |
| 129 |
{ |
| 130 |
"instance": NotifyNextcloud, |
| 131 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 132 |
}, |
| 133 |
), |
| 134 |
( |
| 135 |
"ncloud://user@localhost/admin", |
| 136 |
{ |
| 137 |
"instance": NotifyNextcloud, |
| 138 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 139 |
}, |
| 140 |
), |
| 141 |
( |
| 142 |
"ncloud://user@localhost?to=user1,user2", |
| 143 |
{ |
| 144 |
"instance": NotifyNextcloud, |
| 145 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 146 |
}, |
| 147 |
), |
| 148 |
( |
| 149 |
"ncloud://user@localhost?to=user1,user2&version=20", |
| 150 |
{ |
| 151 |
"instance": NotifyNextcloud, |
| 152 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 153 |
}, |
| 154 |
), |
| 155 |
( |
| 156 |
"ncloud://user@localhost?to=user1,user2&version=21", |
| 157 |
{ |
| 158 |
"instance": NotifyNextcloud, |
| 159 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 160 |
}, |
| 161 |
), |
| 162 |
( |
| 163 |
"ncloud://user@localhost?to=user1&version=20&url_prefix=/abcd", |
| 164 |
{ |
| 165 |
"instance": NotifyNextcloud, |
| 166 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 167 |
}, |
| 168 |
), |
| 169 |
( |
| 170 |
"ncloud://user@localhost?to=user1&version=21&url_prefix=/abcd", |
| 171 |
{ |
| 172 |
"instance": NotifyNextcloud, |
| 173 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 174 |
}, |
| 175 |
), |
| 176 |
( |
| 177 |
"ncloud://user:pass@localhost/user1/user2", |
| 178 |
{ |
| 179 |
"instance": NotifyNextcloud, |
| 180 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 181 |
# Our expected url(privacy=True) startswith() response: |
| 182 |
"privacy_url": "ncloud://user:****@localhost/@user1/@user2", |
| 183 |
}, |
| 184 |
), |
| 185 |
( |
| 186 |
"ncloud://user:pass@localhost/#group1/#group2/#group1", |
| 187 |
{ |
| 188 |
# Test groups, but also note a duplicate group provided |
| 189 |
"instance": NotifyNextcloud, |
| 190 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 191 |
"privacy_url": "ncloud://user:****@localhost/#group", |
| 192 |
}, |
| 193 |
), |
| 194 |
( |
| 195 |
"ncloud://user:pass@localhost:8080/admin", |
| 196 |
{ |
| 197 |
"instance": NotifyNextcloud, |
| 198 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 199 |
}, |
| 200 |
), |
| 201 |
( |
| 202 |
"nclouds://user:pass@localhost/admin", |
| 203 |
{ |
| 204 |
"instance": NotifyNextcloud, |
| 205 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 206 |
# Our expected url(privacy=True) startswith() response: |
| 207 |
"privacy_url": "nclouds://user:****@localhost/@admin", |
| 208 |
}, |
| 209 |
), |
| 210 |
( |
| 211 |
"nclouds://user:pass@localhost:8080/admin/", |
| 212 |
{ |
| 213 |
"instance": NotifyNextcloud, |
| 214 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 215 |
}, |
| 216 |
), |
| 217 |
( |
| 218 |
"nclouds://user:pass@localhost:8080/#group/", |
| 219 |
{ |
| 220 |
"instance": NotifyNextcloud, |
| 221 |
# Invalid JSON Response |
| 222 |
"requests_response_text": "{", |
| 223 |
# We will fail to make the notify() call due to our bad response |
| 224 |
"notify_response": False, |
| 225 |
}, |
| 226 |
), |
| 227 |
( |
| 228 |
"ncloud://localhost:8080/admin?+HeaderKey=HeaderValue", |
| 229 |
{ |
| 230 |
"instance": NotifyNextcloud, |
| 231 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 232 |
}, |
| 233 |
), |
| 234 |
( |
| 235 |
"ncloud://user:pass@localhost:8081/admin", |
| 236 |
{ |
| 237 |
"instance": NotifyNextcloud, |
| 238 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 239 |
# force a failure |
| 240 |
"response": False, |
| 241 |
"requests_response_code": requests.codes.internal_server_error, |
| 242 |
}, |
| 243 |
), |
| 244 |
( |
| 245 |
"ncloud://user:pass@localhost:8082/admin", |
| 246 |
{ |
| 247 |
"instance": NotifyNextcloud, |
| 248 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 249 |
# throw a bizarre code forcing us to fail to look it up |
| 250 |
"response": False, |
| 251 |
"requests_response_code": 999, |
| 252 |
}, |
| 253 |
), |
| 254 |
( |
| 255 |
"ncloud://user:pass@localhost:8083/user1/user2/user3", |
| 256 |
{ |
| 257 |
"instance": NotifyNextcloud, |
| 258 |
"requests_response_text": NEXTCLOUD_GOOD_RESPONSE, |
| 259 |
# Throws a series of i/o exceptions with this flag |
| 260 |
# is set and tests that we gracefully handle them |
| 261 |
"test_requests_exceptions": True, |
| 262 |
}, |
| 263 |
), |
| 264 |
) |
| 265 |
|
| 266 |
|
| 267 |
def test_plugin_nextcloud_urls(): |
| 268 |
"""NotifyNextcloud() Apprise URLs.""" |
| 269 |
|
| 270 |
# Run our general tests |
| 271 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 272 |
|
| 273 |
|
| 274 |
@mock.patch("requests.post") |
| 275 |
def test_plugin_nextcloud_edge_cases(mock_post): |
| 276 |
"""NotifyNextcloud() Edge Cases.""" |
| 277 |
|
| 278 |
# A response |
| 279 |
robj = mock.Mock() |
| 280 |
robj.content = "" |
| 281 |
robj.status_code = requests.codes.ok |
| 282 |
|
| 283 |
# Prepare Mock |
| 284 |
mock_post.return_value = robj |
| 285 |
|
| 286 |
# Variation Initializations |
| 287 |
obj = NotifyNextcloud( |
| 288 |
host="localhost", user="admin", password="pass", targets="user" |
| 289 |
) |
| 290 |
assert isinstance(obj, NotifyNextcloud) is True |
| 291 |
assert isinstance(obj.url(), str) is True |
| 292 |
|
| 293 |
# An empty body |
| 294 |
assert obj.send(body="") is True |
| 295 |
assert "data" in mock_post.call_args_list[0][1] |
| 296 |
assert "shortMessage" in mock_post.call_args_list[0][1]["data"] |
| 297 |
# The longMessage argument is not set |
| 298 |
assert "longMessage" not in mock_post.call_args_list[0][1]["data"] |
| 299 |
|
| 300 |
|
| 301 |
@mock.patch("requests.post") |
| 302 |
def test_plugin_nextcloud_url_prefix(mock_post): |
| 303 |
"""NotifyNextcloud() URL Prefix Testing.""" |
| 304 |
|
| 305 |
response = mock.Mock() |
| 306 |
response.content = "" |
| 307 |
response.status_code = requests.codes.ok |
| 308 |
|
| 309 |
# Prepare our mock object |
| 310 |
mock_post.return_value = response |
| 311 |
|
| 312 |
# instantiate our object (without a batch mode) |
| 313 |
obj = Apprise.instantiate( |
| 314 |
"ncloud://localhost/admin/?version=20&url_prefix=/abcd" |
| 315 |
) |
| 316 |
|
| 317 |
assert ( |
| 318 |
bool( |
| 319 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 320 |
) |
| 321 |
is True |
| 322 |
) |
| 323 |
|
| 324 |
# Not set to batch, so we send 2 different messages |
| 325 |
assert mock_post.call_count == 1 |
| 326 |
assert ( |
| 327 |
mock_post.call_args_list[0][0][0] |
| 328 |
== "http://localhost/abcd/ocs/v2.php/apps/" |
| 329 |
"admin_notifications/api/v1/notifications/admin" |
| 330 |
) |
| 331 |
|
| 332 |
|
| 333 |
@mock.patch("requests.post") |
| 334 |
@mock.patch("requests.get") |
| 335 |
def test_plugin_nextcloud_groups_and_all(mock_get, mock_post): |
| 336 |
"""NotifyNextcloud() Group and All user expansion.""" |
| 337 |
|
| 338 |
# Mock POST success |
| 339 |
post_resp = mock.Mock() |
| 340 |
post_resp.content = "" |
| 341 |
post_resp.status_code = requests.codes.ok |
| 342 |
mock_post.return_value = post_resp |
| 343 |
|
| 344 |
# Mock GET responses for group and users listing |
| 345 |
def get_side_effect(url, *args, **kwargs): |
| 346 |
resp = mock.Mock() |
| 347 |
if "/ocs/v1.php/cloud/groups/" in url: |
| 348 |
# Return JSON for group |
| 349 |
j = { |
| 350 |
"ocs": { |
| 351 |
"meta": {"status": "ok", "statuscode": 100}, |
| 352 |
"data": {"users": ["user1", "user2"]}, |
| 353 |
} |
| 354 |
} |
| 355 |
resp.status_code = requests.codes.ok |
| 356 |
resp.json = lambda: j |
| 357 |
resp.content = dumps(j).encode() |
| 358 |
return resp |
| 359 |
|
| 360 |
elif "/ocs/v1.php/cloud/users" in url: |
| 361 |
j = { |
| 362 |
"ocs": { |
| 363 |
"meta": {"status": "ok", "statuscode": 100}, |
| 364 |
"data": {"users": ["user1", "user3"]}, |
| 365 |
} |
| 366 |
} |
| 367 |
resp.status_code = requests.codes.ok |
| 368 |
resp.json = lambda: j |
| 369 |
resp.content = dumps(j).encode() |
| 370 |
return resp |
| 371 |
# default |
| 372 |
resp.status_code = requests.codes.ok |
| 373 |
resp.content = b"" |
| 374 |
resp.json = lambda: {} |
| 375 |
return resp |
| 376 |
|
| 377 |
mock_get.side_effect = get_side_effect |
| 378 |
|
| 379 |
# Instantiate with a mix of targets: group, all, and direct user |
| 380 |
obj = NotifyNextcloud( |
| 381 |
host="localhost", |
| 382 |
user="admin", |
| 383 |
password="pass", |
| 384 |
targets=["#devs", "all", "user4"], |
| 385 |
) |
| 386 |
|
| 387 |
assert isinstance(obj, NotifyNextcloud) |
| 388 |
|
| 389 |
# Send notification |
| 390 |
assert ( |
| 391 |
obj.send(body="body", title="title", notify_type=NotifyType.INFO) |
| 392 |
is True |
| 393 |
) |
| 394 |
|
| 395 |
# Expected resolved users (deduplicated): user1, user2, user3, user4 |
| 396 |
# Hence 4 POST calls |
| 397 |
assert mock_post.call_count == 4 |
| 398 |
|
| 399 |
# Validate calls were made to expected endpoints |
| 400 |
called_urls = [c[0][0] for c in mock_post.call_args_list] |
| 401 |
for u in ("user1", "user2", "user3", "user4"): |
| 402 |
assert any(u in url for url in called_urls) |
| 403 |
|
| 404 |
|
| 405 |
@mock.patch("requests.post") |
| 406 |
@mock.patch("requests.get") |
| 407 |
def test_plugin_nextcloud_persistent_storage(mock_get, mock_post, tmpdir): |
| 408 |
"""Testing persistent storage""" |
| 409 |
|
| 410 |
post_resp = mock.Mock() |
| 411 |
post_resp.content = "" |
| 412 |
post_resp.status_code = requests.codes.ok |
| 413 |
mock_post.return_value = post_resp |
| 414 |
|
| 415 |
# Set up persistent storage |
| 416 |
asset = AppriseAsset( |
| 417 |
storage_mode=PersistentStoreMode.FLUSH, |
| 418 |
storage_path=str(tmpdir), |
| 419 |
) |
| 420 |
|
| 421 |
def get_side_effect(url, *args, **kwargs): |
| 422 |
resp = mock.Mock() |
| 423 |
# Default json() to empty |
| 424 |
resp.json = lambda: {} |
| 425 |
|
| 426 |
if "/ocs/v1.php/cloud/groups" in url: |
| 427 |
resp.status_code = requests.codes.ok |
| 428 |
payload = {"ocs": {"data": {"users": ["u1"]}}} |
| 429 |
resp.json = lambda: payload |
| 430 |
resp.content = dumps(payload).encode() |
| 431 |
return resp |
| 432 |
|
| 433 |
elif "/ocs/v1.php/cloud/users" in url: |
| 434 |
resp.status_code = requests.codes.ok |
| 435 |
payload = {"ocs": {"data": {"users": ["u2"]}}} |
| 436 |
resp.json = lambda: payload |
| 437 |
resp.content = dumps(payload).encode() |
| 438 |
return resp |
| 439 |
|
| 440 |
resp.status_code = 500 |
| 441 |
resp.content = b"" |
| 442 |
return resp |
| 443 |
|
| 444 |
mock_get.side_effect = get_side_effect |
| 445 |
|
| 446 |
obj = NotifyNextcloud( |
| 447 |
host="localhost", |
| 448 |
user="admin", |
| 449 |
password="pass", |
| 450 |
targets=["#devs", "all"], |
| 451 |
asset=asset, |
| 452 |
) |
| 453 |
# We failed to get our list |
| 454 |
assert ( |
| 455 |
obj.send(body="body", title="title", notify_type=NotifyType.INFO) |
| 456 |
is True |
| 457 |
) |
| 458 |
|
| 459 |
# User and Group looked up |
| 460 |
assert mock_get.call_count == 2 |
| 461 |
|
| 462 |
# Expect users u1 (group) and u2 (all) |
| 463 |
assert mock_post.call_count == 2 |
| 464 |
called_urls = [c[0][0] for c in mock_post.call_args_list] |
| 465 |
for u in ("u1", "u2"): |
| 466 |
assert any(u in url for url in called_urls) |
| 467 |
|
| 468 |
mock_get.reset_mock() |
| 469 |
mock_post.reset_mock() |
| 470 |
|
| 471 |
obj = NotifyNextcloud( |
| 472 |
host="localhost", |
| 473 |
user="admin", |
| 474 |
password="pass", |
| 475 |
targets=["#devs"], |
| 476 |
asset=asset, |
| 477 |
) |
| 478 |
# We succeeded this time |
| 479 |
assert ( |
| 480 |
obj.send(body="body", title="title", notify_type=NotifyType.INFO) |
| 481 |
is True |
| 482 |
) |
| 483 |
|
| 484 |
# Expect users u1 (group) only and pulled from cache |
| 485 |
assert mock_get.call_count == 0 |
| 486 |
assert mock_post.call_count == 1 |
| 487 |
assert mock_post.call_args_list[0][0][0].endswith("/u1") |
| 488 |
|
| 489 |
|
| 490 |
@mock.patch("requests.post") |
| 491 |
@mock.patch("requests.get") |
| 492 |
def test_plugin_nextcloud_groups_errors_and_dedup(mock_get, mock_post): |
| 493 |
"""Non-200/exception paths return empty lists and dedup still applies.""" |
| 494 |
|
| 495 |
post_resp = mock.Mock() |
| 496 |
post_resp.content = "" |
| 497 |
post_resp.status_code = requests.codes.ok |
| 498 |
mock_post.return_value = post_resp |
| 499 |
|
| 500 |
# Non-200 for group and users; JSON invalid |
| 501 |
def get_side_effect(url, *args, **kwargs): |
| 502 |
resp = mock.Mock() |
| 503 |
resp.status_code = 401 |
| 504 |
resp.content = b"<ocs><data></data></ocs>" |
| 505 |
# Return empty/invalid JSON to drive empty path |
| 506 |
resp.json = lambda: {} |
| 507 |
return resp |
| 508 |
|
| 509 |
mock_get.side_effect = get_side_effect |
| 510 |
|
| 511 |
# Provide duplicates alongside failing expansions |
| 512 |
obj = NotifyNextcloud( |
| 513 |
host="localhost", |
| 514 |
user="admin", |
| 515 |
password="pass", |
| 516 |
targets=["#devs", "all", "user1", "user1", "user2"], |
| 517 |
) |
| 518 |
|
| 519 |
# We failed to hit the server for data |
| 520 |
assert obj.send(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 521 |
|
| 522 |
# we have no control over the order, but we know that on the first |
| 523 |
# GET call, we'd have gotten a 401 response; so we'd have stopped from |
| 524 |
# that point further |
| 525 |
assert mock_get.call_count == 1 |
| 526 |
|
| 527 |
# Nothing notified |
| 528 |
assert mock_post.call_count == 0 |
| 529 |
|
| 530 |
|
| 531 |
@mock.patch("requests.post") |
| 532 |
@mock.patch("requests.get") |
| 533 |
def test_plugin_nextcloud_req_exception_and_empty_targets(mock_get, mock_post): |
| 534 |
"""RequestException returns empty expansion; direct users send.""" |
| 535 |
|
| 536 |
post_resp = mock.Mock() |
| 537 |
post_resp.content = "" |
| 538 |
post_resp.status_code = requests.codes.ok |
| 539 |
mock_post.return_value = post_resp |
| 540 |
|
| 541 |
def get_side_effect(url, *args, **kwargs): |
| 542 |
raise requests.RequestException("boom") |
| 543 |
|
| 544 |
mock_get.side_effect = get_side_effect |
| 545 |
|
| 546 |
obj = NotifyNextcloud( |
| 547 |
host="localhost", |
| 548 |
user="admin", |
| 549 |
password="pass", |
| 550 |
targets=["", " ", "#DevTeam", "#", "userX"], |
| 551 |
) |
| 552 |
|
| 553 |
# Our Group inquiry failed to respond |
| 554 |
assert obj.send(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 555 |
assert mock_post.call_count == 0 |
| 556 |
assert mock_get.call_count == 1 |
| 557 |
assert ( |
| 558 |
mock_get.call_args_list[0][0][0] |
| 559 |
== "http://localhost/ocs/v1.php/cloud/groups/DevTeam" |
| 560 |
) |
| 561 |
assert mock_get.call_args_list[0][1]["params"].get("format") == "json" |
| 562 |
|
| 563 |
|
| 564 |
@mock.patch("requests.post") |
| 565 |
@mock.patch("requests.get") |
| 566 |
def test_plugin_nextcloud_json_empty_returns_empty(mock_get, mock_post): |
| 567 |
"""Invalid/empty JSON returns empty; direct users still send.""" |
| 568 |
|
| 569 |
post_resp = mock.Mock() |
| 570 |
post_resp.content = "" |
| 571 |
post_resp.status_code = requests.codes.ok |
| 572 |
mock_post.return_value = post_resp |
| 573 |
|
| 574 |
def get_side_effect(url, *args, **kwargs): |
| 575 |
resp = mock.Mock() |
| 576 |
resp.json = lambda: {} |
| 577 |
resp.status_code = requests.codes.ok |
| 578 |
resp.content = b"{}" |
| 579 |
return resp |
| 580 |
|
| 581 |
mock_get.side_effect = get_side_effect |
| 582 |
|
| 583 |
obj = NotifyNextcloud( |
| 584 |
host="localhost", |
| 585 |
user="admin", |
| 586 |
password="pass", |
| 587 |
targets=["#broken", "all", "userZ"], |
| 588 |
) |
| 589 |
|
| 590 |
# Our notification |
| 591 |
assert obj.send(body="x", title="y", notify_type=NotifyType.INFO) is True |
| 592 |
# Only direct userZ posts because both expansions return empty |
| 593 |
assert mock_get.call_count == 2 |
| 594 |
assert any( |
| 595 |
"/cloud/users" in call[0][0] for call in mock_get.call_args_list |
| 596 |
) |
| 597 |
assert any( |
| 598 |
"/cloud/groups/broken" in call[0][0] |
| 599 |
for call in mock_get.call_args_list |
| 600 |
) |
| 601 |
|
| 602 |
# userZ would get a notification |
| 603 |
assert mock_post.call_count == 1 |
| 604 |
assert mock_post.call_args_list[0][0][0].endswith("/userZ") |
| 605 |
|
| 606 |
|
| 607 |
@mock.patch("requests.post") |
| 608 |
@mock.patch("requests.get") |
| 609 |
def test_plugin_nextcloud_caching_group_and_all(mock_get, mock_post): |
| 610 |
"""Cache hits avoid repeat OCS lookups.""" |
| 611 |
|
| 612 |
# First round of GETs return users for group and all |
| 613 |
def get_side_effect(url, *args, **kwargs): |
| 614 |
resp = mock.Mock() |
| 615 |
resp.status_code = requests.codes.ok |
| 616 |
if "/ocs/v1.php/cloud/groups" in url: |
| 617 |
j = { |
| 618 |
"ocs": { |
| 619 |
"meta": {"status": "ok", "statuscode": 100}, |
| 620 |
"data": {"users": ["g1", "g2"]}, |
| 621 |
} |
| 622 |
} |
| 623 |
elif "/ocs/v1.php/cloud/users" in url: |
| 624 |
j = { |
| 625 |
"ocs": { |
| 626 |
"meta": {"status": "ok", "statuscode": 100}, |
| 627 |
"data": {"users": ["a1", "a2"]}, |
| 628 |
} |
| 629 |
} |
| 630 |
else: |
| 631 |
j = {"ocs": {"data": {"users": []}}} |
| 632 |
resp.json = lambda: j |
| 633 |
resp.content = dumps(j).encode() |
| 634 |
return resp |
| 635 |
|
| 636 |
mock_get.side_effect = get_side_effect |
| 637 |
|
| 638 |
post_resp = mock.Mock() |
| 639 |
post_resp.content = "" |
| 640 |
post_resp.status_code = requests.codes.ok |
| 641 |
mock_post.return_value = post_resp |
| 642 |
|
| 643 |
obj = NotifyNextcloud( |
| 644 |
host="localhost", |
| 645 |
user="admin", |
| 646 |
password="pass", |
| 647 |
targets=["#devs", "all", "@joe"], |
| 648 |
) |
| 649 |
|
| 650 |
# First send: resolves via OCS; expect 2 GETs (group + all) |
| 651 |
assert obj.send(body="b", title="t", notify_type=NotifyType.INFO) is True |
| 652 |
assert mock_get.call_count == 2 |
| 653 |
called = "".join(c[0][0] for c in mock_get.call_args_list) |
| 654 |
assert "/cloud/groups/" in called and "/cloud/users" in called |
| 655 |
|
| 656 |
# we sent 5 notifications |
| 657 |
assert mock_post.call_count == 5 |
| 658 |
expected_users = {"a1", "a2", "g1", "g2", "joe"} |
| 659 |
|
| 660 |
# Extract the user segment from the URL of each call |
| 661 |
actual_users = { |
| 662 |
call[0][0].split("/")[-1] for call in mock_post.call_args_list |
| 663 |
} |
| 664 |
|
| 665 |
# Assert that the set of actual users matches the set of expected users |
| 666 |
assert actual_users == expected_users |
| 667 |
|
| 668 |
# Reset our mock object |
| 669 |
mock_get.reset_mock() |
| 670 |
mock_post.reset_mock() |
| 671 |
|
| 672 |
assert obj.send(body="b2", title="t2", notify_type=NotifyType.INFO) is True |
| 673 |
# Cached responses were used to get our user information |
| 674 |
assert mock_get.call_count == 0 |
| 675 |
assert mock_post.call_count == 5 |
| 676 |
|
| 677 |
# We can re-verify our notifications went as expected: |
| 678 |
actual_users = { |
| 679 |
call[0][0].split("/")[-1] for call in mock_post.call_args_list |
| 680 |
} |
| 681 |
|
| 682 |
# Assert that the set of actual users matches the set of expected users |
| 683 |
assert actual_users == expected_users |