Skip to content

Commit 80dde3c

Browse files
committed
improved test coverage + bugfixes
1 parent d933c1b commit 80dde3c

File tree

2 files changed

+158
-8
lines changed

2 files changed

+158
-8
lines changed

apprise/plugins/notificationapi.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ def url(self, privacy=False, *args, **kwargs):
501501
x["id"], x["number"]), safe="")
502502
for x in self.sms_targets],
503503
[
504-
NotifyNotificationAPI.quote(x, safe="")
505-
for x["id"] in self.id_targets],
504+
NotifyNotificationAPI.quote(x["id"], safe="")
505+
for x in self.id_targets],
506506
)),
507507
params=NotifyNotificationAPI.urlencode(params),
508508
)
@@ -553,8 +553,8 @@ def gen_payload(self, body, title="", notify_type=NotifyType.INFO,
553553
_payload.update({
554554
"options": {
555555
"email": {
556-
"fromAddress": self.names[self.from_addr],
557-
"fromName": self.from_addr}}})
556+
"fromAddress": self.from_addr,
557+
"fromName": self.names[self.from_addr]}}})
558558

559559
elif self.cc or self.bcc:
560560
# Set up shell
@@ -600,7 +600,7 @@ def send(self, body, title="", notify_type=NotifyType.INFO, **kwargs):
600600
# error tracking (used for function return)
601601
has_error = False
602602

603-
if not (self.email_targets or self.sms_targets):
603+
if not (self.email_targets or self.sms_targets or self.id_targets):
604604
# There is no one to email or send an sms message to; we're done
605605
self.logger.warning(
606606
"There are no NotificationAPI recipients to notify"

tests/test_plugin_notificationapi.py

Lines changed: 153 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@
2525
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626
# POSSIBILITY OF SUCH DAMAGE.
2727

28-
from json import dumps
28+
from json import dumps, loads
2929

3030
# Disable logging for a cleaner testing output
3131
import logging
3232
import os
33+
from unittest import mock
3334

3435
from helpers import AppriseURLTester
3536
import requests
3637

38+
from apprise import Apprise, NotifyType
3739
from apprise.plugins.notificationapi import NotifyNotificationAPI
3840

3941
logging.disable(logging.CRITICAL)
@@ -127,11 +129,11 @@
127129
"instance": NotifyNotificationAPI,
128130
"requests_response_text": NOTIFICATIONAPI_GOOD_RESPONSE,
129131
}),
130-
("napi://user@client_id/cs7/id:[email protected]", {
132+
("napi://user@client_id/cs7/id:[email protected]", {
131133
# An email with a designated to email
132134
"instance": NotifyNotificationAPI,
133135
"requests_response_text": NOTIFICATIONAPI_GOOD_RESPONSE,
134-
}),
136+
}),
135137
("napi://user@client_id/cs8/id:[email protected]"
136138
"?to=id:Chris<[email protected]>", {
137139
# An email with a full name in in To field
@@ -221,3 +223,151 @@ def test_plugin_napi_urls():
221223

222224
# Run our general tests
223225
AppriseURLTester(tests=apprise_url_tests).run_all()
226+
227+
228+
@mock.patch("requests.post")
229+
def test_plugin_napi_sms_payloads(mock_post):
230+
"""NotifyNotificationAPI() Testing SMS Payloads."""
231+
232+
okay_response = requests.Request()
233+
okay_response.status_code = requests.codes.ok
234+
okay_response.content = NOTIFICATIONAPI_GOOD_RESPONSE
235+
236+
# Assign our mock object our return value
237+
mock_post.return_value = okay_response
238+
239+
# Details
240+
client_id = "my_id"
241+
client_secret = "my_secret"
242+
message_type = "apprise-post"
243+
phone_no = "userid:+1-555-123-4567"
244+
245+
obj = Apprise.instantiate(
246+
f"napi://{message_type}@{client_id}/{client_secret}/"
247+
f"{phone_no}")
248+
assert isinstance(obj, NotifyNotificationAPI)
249+
assert isinstance(obj.url(), str)
250+
251+
# No calls made yet
252+
assert mock_post.call_count == 0
253+
254+
# Send our notification
255+
assert (
256+
obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
257+
is True
258+
)
259+
260+
# 2 calls were made, one to perform an email lookup, the second
261+
# was the notification itself
262+
assert mock_post.call_count == 1
263+
assert (
264+
mock_post.call_args_list[0][0][0]
265+
== f"https://api.notificationapi.com/{client_id}/sender"
266+
)
267+
268+
payload = loads(mock_post.call_args_list[0][1]["data"])
269+
assert payload == {
270+
"type": "apprise-post",
271+
"to": {
272+
"id": "userid",
273+
"number": "15551234567",
274+
},
275+
"parameters": {
276+
"appBody": "body",
277+
"appTitle": "",
278+
"appType": "info",
279+
"appId": "Apprise",
280+
"appDescription": "Apprise Notifications",
281+
"appColor": "#3AA3E3",
282+
"appImageUrl": (
283+
"https://github.com/caronc/apprise/raw/master/apprise"
284+
"/assets/themes/default/apprise-info-72x72.png"),
285+
"appUrl": "https://github.com/caronc/apprise"},
286+
}
287+
headers = mock_post.call_args_list[0][1]["headers"]
288+
assert headers == {
289+
"User-Agent": "Apprise",
290+
"Content-Type": "application/json",
291+
"Authorization": "Basic bXlfaWQ6bXlfc2VjcmV0"}
292+
293+
# Reset our mock object
294+
mock_post.reset_mock()
295+
296+
297+
@mock.patch("requests.post")
298+
def test_plugin_napi_email_payloads(mock_post):
299+
"""NotifyNotificationAPI() Testing Email Payloads."""
300+
301+
okay_response = requests.Request()
302+
okay_response.status_code = requests.codes.ok
303+
okay_response.content = NOTIFICATIONAPI_GOOD_RESPONSE
304+
305+
# Assign our mock object our return value
306+
mock_post.return_value = okay_response
307+
308+
# Details
309+
client_id = "my_id_abc"
310+
client_secret = "my_secret"
311+
message_type = "apprise-post"
312+
email = "userid:[email protected]"
313+
314+
obj = Apprise.instantiate(
315+
f"napi://{message_type}@{client_id}/{client_secret}/"
316+
f"{email}?from=Chris<[email protected]>&[email protected]&"
317+
f"[email protected]&:customToken=customValue")
318+
assert isinstance(obj, NotifyNotificationAPI)
319+
assert isinstance(obj.url(), str)
320+
321+
# No calls made yet
322+
assert mock_post.call_count == 0
323+
324+
# Send our notification
325+
assert (
326+
obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
327+
is True
328+
)
329+
330+
# 2 calls were made, one to perform an email lookup, the second
331+
# was the notification itself
332+
assert mock_post.call_count == 1
333+
assert (
334+
mock_post.call_args_list[0][0][0]
335+
== f"https://api.notificationapi.com/{client_id}/sender"
336+
)
337+
338+
payload = loads(mock_post.call_args_list[0][1]["data"])
339+
assert payload == {
340+
"type": "apprise-post",
341+
"to": {
342+
"id": "userid",
343+
"email": "[email protected]",
344+
},
345+
"options": {
346+
"email": {
347+
"fromAddress": "[email protected]",
348+
"fromName": "Chris",
349+
"ccAddresses": ["[email protected]"],
350+
"bccAddresses": ["[email protected]"]}
351+
},
352+
"parameters": {
353+
"customToken": "customValue",
354+
"appBody": "body",
355+
"appTitle": "",
356+
"appType": "info",
357+
"appId": "Apprise",
358+
"appDescription": "Apprise Notifications",
359+
"appColor": "#3AA3E3",
360+
"appImageUrl": (
361+
"https://github.com/caronc/apprise/raw/master/apprise/"
362+
"assets/themes/default/apprise-info-72x72.png"),
363+
"appUrl": "https://github.com/caronc/apprise"
364+
},
365+
}
366+
headers = mock_post.call_args_list[0][1]["headers"]
367+
assert headers == {
368+
'User-Agent': 'Apprise',
369+
'Content-Type': 'application/json',
370+
'Authorization': 'Basic bXlfaWRfYWJjOm15X3NlY3JldA=='}
371+
372+
# Reset our mock object
373+
mock_post.reset_mock()

0 commit comments

Comments
 (0)