Skip to content

Commit 74d5ad2

Browse files
authored
Apprise API payload bugfix (#1428)
1 parent fe000d0 commit 74d5ad2

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

apprise/plugins/apprise_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ def send(
328328
# Apprise API Payload
329329
"title": title,
330330
"body": body,
331-
"type": notify_type,
332-
"format": self.notify_format,
331+
"type": notify_type.value,
332+
"format": self.notify_format.value,
333333
}
334334

335335
if self.method == AppriseAPIMethod.JSON:

tests/test_plugin_apprise_api.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# POSSIBILITY OF SUCH DAMAGE.
2727

2828
# Disable logging for a cleaner testing output
29+
from json import loads
2930
import logging
3031
import os
3132
from unittest import mock
@@ -273,13 +274,95 @@
273274
)
274275

275276

276-
def test_plugin_apprise_urls():
277+
def test_plugin_apprise_api_urls():
277278
"""NotifyAppriseAPI() General Checks."""
278279

279280
# Run our general tests
280281
AppriseURLTester(tests=apprise_url_tests).run_all()
281282

282283

284+
285+
@mock.patch("requests.post")
286+
def test_notify_apprise_api_payload_check(mock_post):
287+
"""NotifyAppriseAPI() payload checks"""
288+
289+
okay_response = requests.Request()
290+
291+
okay_response.status_code = requests.codes.ok
292+
okay_response.content = ""
293+
294+
# Assign our mock object our return value
295+
mock_post.return_value = okay_response
296+
297+
obj = Apprise.instantiate(
298+
"apprise://user@localhost/mytoken1/?method=form"
299+
)
300+
assert isinstance(obj, NotifyAppriseAPI)
301+
302+
# Test URL with Attachment
303+
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
304+
attach = AppriseAttachment(path)
305+
assert (
306+
obj.notify(
307+
body="body",
308+
title="title",
309+
notify_type=NotifyType.INFO,
310+
attach=attach,
311+
)
312+
is True
313+
)
314+
315+
details = mock_post.call_args_list[0]
316+
assert details[0][0] == "http://localhost/notify/mytoken1"
317+
assert details[1]["data"] == {
318+
"title": "title",
319+
"body": "body",
320+
"type": "info",
321+
"format": "text",
322+
}
323+
assert "X-Apprise-ID" in details[1]["headers"]
324+
assert details[1]["headers"].get("User-Agent") == "Apprise"
325+
assert details[1]["headers"].get("Accept") == "application/json"
326+
assert details[1]["headers"].get("X-Apprise-Recursion-Count") == "1"
327+
328+
mock_post.reset_mock()
329+
330+
obj = Apprise.instantiate(
331+
"apprise://user@localhost/mytoken1/?method=json"
332+
)
333+
assert isinstance(obj, NotifyAppriseAPI)
334+
335+
assert (
336+
obj.notify(
337+
body="body",
338+
title="title",
339+
notify_type=NotifyType.INFO,
340+
attach=attach,
341+
)
342+
is True
343+
)
344+
345+
details = mock_post.call_args_list[0]
346+
assert details[0][0] == "http://localhost/notify/mytoken1"
347+
data = loads(details[1]["data"])
348+
assert "attachments" in data
349+
assert isinstance(data["attachments"], list)
350+
assert len(data["attachments"]) == 1
351+
352+
# Remove our attachment to make the next assert easier
353+
del data["attachments"]
354+
assert data == {
355+
"title": "title",
356+
"body": "body",
357+
"type": "info",
358+
"format": "text",
359+
}
360+
assert "X-Apprise-ID" in details[1]["headers"]
361+
assert details[1]["headers"].get("User-Agent") == "Apprise"
362+
assert details[1]["headers"].get("Accept") == "application/json"
363+
assert details[1]["headers"].get("X-Apprise-Recursion-Count") == "1"
364+
365+
283366
@mock.patch("requests.post")
284367
def test_notify_apprise_api_attachments(mock_post):
285368
"""NotifyAppriseAPI() Attachments."""

0 commit comments

Comments
 (0)