| 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 |
|
| 29 |
"""Shared responses, attachments, and recorders for delivery tests.""" |
| 30 |
|
| 31 |
import contextlib |
| 32 |
from json import dumps |
| 33 |
import os |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise.plugins.base import NotifyBase |
| 39 |
|
| 40 |
# Attachment Directory |
| 41 |
TEST_VAR_DIR = os.path.join( |
| 42 |
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "var" |
| 43 |
) |
| 44 |
ATTACHMENT = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 45 |
|
| 46 |
# Fields a service might look for in a good answer. It is deliberately |
| 47 |
# wide: one answer has to satisfy every service in the fleet, and a field |
| 48 |
# a service does not know about is simply ignored. |
| 49 |
OK_FIELDS = { |
| 50 |
"ok": True, |
| 51 |
"success": True, |
| 52 |
"status": "success", |
| 53 |
"id": "1", |
| 54 |
"uuid": "1", |
| 55 |
"sid": "1", |
| 56 |
"requestId": "1", |
| 57 |
"code": 0, |
| 58 |
"errcode": 0, |
| 59 |
"error": None, |
| 60 |
"channel": "C1", |
| 61 |
"access_token": "abc", |
| 62 |
"expires_in": 3600, |
| 63 |
"message_id": 1, |
| 64 |
"upload_url": "https://localhost/upload", |
| 65 |
"file_id": "F1", |
| 66 |
"file_name": "apprise-test.gif", |
| 67 |
"file_type": "image/gif", |
| 68 |
"file_url": "https://localhost/x", |
| 69 |
"media_id": 1, |
| 70 |
"media_id_string": "1", |
| 71 |
"content_uri": "mxc://localhost/1", |
| 72 |
"event_id": "e1", |
| 73 |
"room_id": "!r:localhost", |
| 74 |
"user_id": "@u:localhost", |
| 75 |
"url": "https://cdn.example/x", |
| 76 |
"post": {"id": 1}, |
| 77 |
"data": {"url": "https://cdn.example/x", "id": "1"}, |
| 78 |
"json": {"errors": []}, |
| 79 |
"result": {"message_id": 1, "id": "1", "status": "ok"}, |
| 80 |
"items": [{"id": "1"}], |
| 81 |
"messages": [{"status": "0", "message-id": "1"}], |
| 82 |
} |
| 83 |
|
| 84 |
# The same answer as a JSON document |
| 85 |
OK_BODY = dumps(OK_FIELDS) |
| 86 |
|
| 87 |
|
| 88 |
def ok_response(*args, **kwargs): |
| 89 |
"""Return a response a service will treat as a success.""" |
| 90 |
response = requests.Request() |
| 91 |
response.status_code = requests.codes.ok |
| 92 |
response.content = OK_BODY |
| 93 |
response.text = OK_BODY |
| 94 |
response.headers = {"Content-Type": "application/json"} |
| 95 |
|
| 96 |
def iter_content(chunk_size=None): |
| 97 |
# A bounded read pulls the body in chunks, the way requests does |
| 98 |
return iter([OK_BODY.encode("utf-8")]) |
| 99 |
|
| 100 |
def close(): |
| 101 |
# Nothing to release on a stand-in |
| 102 |
pass |
| 103 |
|
| 104 |
response.iter_content = iter_content |
| 105 |
response.close = close |
| 106 |
return response |
| 107 |
|
| 108 |
|
| 109 |
@contextlib.contextmanager |
| 110 |
def delivery_marks(): |
| 111 |
"""Collect every target a service records while the block runs.""" |
| 112 |
|
| 113 |
recorded = [] |
| 114 |
real = NotifyBase.mark_delivered |
| 115 |
|
| 116 |
def counted(self, key, per_message=False): |
| 117 |
recorded.append(key) |
| 118 |
return real(self, key, per_message) |
| 119 |
|
| 120 |
with mock.patch.object(NotifyBase, "mark_delivered", counted): |
| 121 |
yield recorded |