Debian Codegraph @

tests/test_plugin_ifttt.py

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