Debian Codegraph @

tests/test_plugin_ifttt.py

raw
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 NotifyType
37 from apprise.exception import AppriseImproperlyConfigured
38 from apprise.plugins.ifttt import NotifyIFTTT
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "ifttt://",
46 {
47 "instance": AppriseImproperlyConfigured,
48 },
49 ),
50 (
51 "ifttt://:@/",
52 {
53 "instance": AppriseImproperlyConfigured,
54 },
55 ),
56 # No User
57 (
58 "ifttt://EventID/",
59 {
60 "instance": AppriseImproperlyConfigured,
61 },
62 ),
63 # A nicely formed ifttt url with 1 event and a new key/value store
64 (
65 "ifttt://WebHookID@EventID/?+TemplateKey=TemplateVal",
66 {
67 "instance": NotifyIFTTT,
68 # Our expected url(privacy=True) startswith() response:
69 "privacy_url": "ifttt://W...D",
70 },
71 ),
72 # Test to= in which case we set the host to the webhook id
73 (
74 "ifttt://WebHookID?to=EventID,EventID2",
75 {
76 "instance": NotifyIFTTT,
77 },
78 ),
79 # Removing certain keys:
80 (
81 "ifttt://WebHookID@EventID/?-Value1=&-Value2",
82 {
83 "instance": NotifyIFTTT,
84 },
85 ),
86 # A nicely formed ifttt url with 2 events defined:
87 (
88 "ifttt://WebHookID@EventID/EventID2/",
89 {
90 "instance": NotifyIFTTT,
91 },
92 ),
93 # Support Native URL references
94 (
95 "https://maker.ifttt.com/use/WebHookID/",
96 {
97 # No EventID specified
98 "instance": AppriseImproperlyConfigured,
99 },
100 ),
101 (
102 "https://maker.ifttt.com/use/WebHookID/EventID/",
103 {
104 "instance": NotifyIFTTT,
105 },
106 ),
107 # Native URL with arguments
108 (
109 "https://maker.ifttt.com/use/WebHookID/EventID/?-Value1=",
110 {
111 "instance": NotifyIFTTT,
112 },
113 ),
114 # Test website connection failures
115 (
116 "ifttt://WebHookID@EventID",
117 {
118 "instance": NotifyIFTTT,
119 # force a failure
120 "response": False,
121 "requests_response_code": requests.codes.internal_server_error,
122 },
123 ),
124 (
125 "ifttt://WebHookID@EventID",
126 {
127 "instance": NotifyIFTTT,
128 # throw a bizarre code forcing us to fail to look it up
129 "response": False,
130 "requests_response_code": 999,
131 },
132 ),
133 (
134 "ifttt://WebHookID@EventID",
135 {
136 "instance": NotifyIFTTT,
137 # Throws a series of i/o exceptions with this flag
138 # is set and tests that we gracefully handle them
139 "test_requests_exceptions": True,
140 },
141 ),
142 )
143
144
145 def test_plugin_ifttt_urls():
146 """NotifyIFTTT() Apprise URLs."""
147
148 # Run our general tests
149 AppriseURLTester(tests=apprise_url_tests).run_all()
150
151
152 @mock.patch("requests.get")
153 @mock.patch("requests.post")
154 def test_plugin_ifttt_edge_cases(mock_post, mock_get):
155 """NotifyIFTTT() Edge Cases."""
156
157 # Initialize some generic (but valid) tokens
158 webhook_id = "webhook_id"
159 events = ["event1", "event2"]
160
161 # Prepare Mock
162 mock_get.return_value = requests.Request()
163 mock_post.return_value = requests.Request()
164 mock_post.return_value.status_code = requests.codes.ok
165 mock_get.return_value.status_code = requests.codes.ok
166 mock_get.return_value.content = "{}"
167 mock_post.return_value.content = "{}"
168
169 # No webhook_id specified
170 with pytest.raises(AppriseImproperlyConfigured):
171 NotifyIFTTT(webhook_id=None, events=None)
172
173 # Initializes the plugin with an invalid webhook id
174 with pytest.raises(AppriseImproperlyConfigured):
175 NotifyIFTTT(webhook_id=None, events=events)
176
177 # Whitespace also acts as an invalid webhook id
178 with pytest.raises(AppriseImproperlyConfigured):
179 NotifyIFTTT(webhook_id=" ", events=events)
180
181 # No events specified
182 with pytest.raises(AppriseImproperlyConfigured):
183 NotifyIFTTT(webhook_id=webhook_id, events=None)
184
185 obj = NotifyIFTTT(webhook_id=webhook_id, events=events)
186 assert isinstance(obj, NotifyIFTTT)
187
188 assert (
189 bool(
190 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
191 )
192 is True
193 )
194
195 # Validate outbound payload does not leak NotifyType Enum
196 assert mock_post.call_count >= 1
197 call = mock_post.call_args_list[-1]
198 payload = call[1].get("json") or call[1].get("data") or ""
199 payload_text = payload if isinstance(payload, str) else str(payload)
200
201 assert "NotifyType." not in payload_text
202 # If JSON dict is used:
203 if isinstance(payload, dict):
204 assert NotifyType.INFO.value in payload_text
205
206 # Test the addition of tokens
207 obj = NotifyIFTTT(
208 webhook_id=webhook_id,
209 events=events,
210 add_tokens={"Test": "ValueA", "Test2": "ValueB"},
211 )
212
213 assert isinstance(obj, NotifyIFTTT)
214
215 assert (
216 bool(
217 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
218 )
219 is True
220 )
221
222 # Invalid del_tokens entry
223 with pytest.raises(AppriseImproperlyConfigured):
224 NotifyIFTTT(
225 webhook_id=webhook_id,
226 events=events,
227 del_tokens=NotifyIFTTT.ifttt_default_title_key,
228 )
229
230 assert isinstance(obj, NotifyIFTTT)
231
232 assert (
233 bool(
234 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
235 )
236 is True
237 )
238
239 # Test removal of tokens by a list
240 obj = NotifyIFTTT(
241 webhook_id=webhook_id,
242 events=events,
243 add_tokens={"MyKey": "MyValue"},
244 del_tokens=(
245 NotifyIFTTT.ifttt_default_title_key,
246 NotifyIFTTT.ifttt_default_body_key,
247 NotifyIFTTT.ifttt_default_type_key,
248 ),
249 )
250
251 assert isinstance(obj, NotifyIFTTT)
252
253 assert (
254 bool(
255 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
256 )
257 is True
258 )
259
260 # Test removal of tokens as dict
261 obj = NotifyIFTTT(
262 webhook_id=webhook_id,
263 events=events,
264 add_tokens={"MyKey": "MyValue"},
265 del_tokens={
266 NotifyIFTTT.ifttt_default_title_key: None,
267 NotifyIFTTT.ifttt_default_body_key: None,
268 NotifyIFTTT.ifttt_default_type_key: None,
269 },
270 )
271
272 assert isinstance(obj, NotifyIFTTT)