Debian Codegraph @

tests/test_plugin_sendgrid.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 import os
31 from unittest import mock
32
33 from helpers import AppriseURLTester
34 import pytest
35 import requests
36
37 from apprise import Apprise, AppriseAttachment, NotifyType
38 from apprise.exception import AppriseImproperlyConfigured
39 from apprise.plugins.sendgrid import NotifySendGrid
40
41 logging.disable(logging.CRITICAL)
42
43 # Attachment Directory
44 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
45
46 # a test UUID we can use
47 UUID4 = "8b799edf-6f98-4d3a-9be7-2862fb4e5752"
48
49 # Our Testing URLs
50 apprise_url_tests = (
51 (
52 "sendgrid://",
53 {
54 "instance": None,
55 },
56 ),
57 (
58 "sendgrid://:@/",
59 {
60 "instance": None,
61 },
62 ),
63 (
64 "sendgrid://abcd",
65 {
66 # Just an broken email (no api key or email)
67 "instance": None,
68 },
69 ),
70 (
71 "sendgrid://abcd@host",
72 {
73 # Just an Email specified, no API Key
74 "instance": None,
75 },
76 ),
77 (
78 "sendgrid://invalid-api-key+*-d:[email protected]",
79 {
80 # An invalid API Key
81 "instance": AppriseImproperlyConfigured,
82 },
83 ),
84 (
85 "sendgrid://abcd:[email protected]",
86 {
87 # No To/Target Address(es) specified; so we sub in the same From
88 # address
89 "instance": NotifySendGrid,
90 },
91 ),
92 (
93 "sendgrid://abcd:[email protected]/[email protected]",
94 {
95 # A good email
96 "instance": NotifySendGrid,
97 },
98 ),
99 (
100 "sendgrid://abcd:[email protected]/bademailaddress",
101 {
102 # won't be able to send email
103 "instance": NotifySendGrid,
104 "notify_response": False,
105 },
106 ),
107 (
108 (
109 "sendgrid://abcd:[email protected]/[email protected]"
110 "[email protected]"
111 ),
112 {
113 # A good email with Blind Carbon Copy
114 "instance": NotifySendGrid,
115 "force_debug": True,
116 },
117 ),
118 (
119 (
120 "sendgrid://abcd:[email protected]/[email protected]"
121 "[email protected]"
122 ),
123 {
124 # A good email with Carbon Copy
125 "instance": NotifySendGrid,
126 },
127 ),
128 (
129 (
130 "sendgrid://abcd:[email protected]/[email protected]"
131 "[email protected]"
132 ),
133 {
134 # A good email with Carbon Copy
135 "instance": NotifySendGrid,
136 },
137 ),
138 (
139 (
140 "sendgrid://abcd:[email protected]/[email protected]"
141 f"?template={UUID4}"
142 ),
143 {
144 # A good email with a template + no substitutions
145 "instance": NotifySendGrid,
146 },
147 ),
148 (
149 (
150 "sendgrid://abcd:[email protected]/[email protected]"
151 f"?template={UUID4}&+sub=value&+sub2=value2"
152 ),
153 {
154 # A good email with a template + substitutions
155 "instance": NotifySendGrid,
156 # Our expected url(privacy=True) startswith() response:
157 "privacy_url": "sendgrid://a...d:[email protected]/",
158 },
159 ),
160 (
161 "sendgrid://abcd:[email protected]/[email protected]",
162 {
163 "instance": NotifySendGrid,
164 # force a failure
165 "response": False,
166 "requests_response_code": requests.codes.internal_server_error,
167 },
168 ),
169 (
170 "sendgrid://abcd:[email protected]/[email protected]",
171 {
172 "instance": NotifySendGrid,
173 # throw a bizarre code forcing us to fail to look it up
174 "response": False,
175 "requests_response_code": 999,
176 },
177 ),
178 (
179 "sendgrid://abcd:[email protected]/[email protected]",
180 {
181 "instance": NotifySendGrid,
182 # Throws a series of i/o exceptions with this flag
183 # is set and tests that we gracefully handle them
184 "test_requests_exceptions": True,
185 },
186 ),
187 )
188
189
190 def test_plugin_sendgrid_urls():
191 """NotifySendGrid() Apprise URLs."""
192
193 # Run our general tests
194 AppriseURLTester(tests=apprise_url_tests).run_all()
195
196
197 @mock.patch("requests.get")
198 @mock.patch("requests.post")
199 def test_plugin_sendgrid_edge_cases(mock_post, mock_get):
200 """NotifySendGrid() Edge Cases."""
201
202 # no apikey
203 with pytest.raises(AppriseImproperlyConfigured):
204 NotifySendGrid(apikey=None, from_email="[email protected]")
205
206 # invalid from email
207 with pytest.raises(AppriseImproperlyConfigured):
208 NotifySendGrid(apikey="abcd", from_email="!invalid")
209
210 # no email
211 with pytest.raises(AppriseImproperlyConfigured):
212 NotifySendGrid(apikey="abcd", from_email=None)
213
214 # Invalid To email address
215 NotifySendGrid(
216 apikey="abcd", from_email="[email protected]", targets="!invalid"
217 )
218
219 # Test invalid bcc/cc entries mixed with good ones
220 assert isinstance(
221 NotifySendGrid(
222 apikey="abcd",
223 from_email="[email protected]",
224 bcc=("[email protected]", "!invalid"),
225 cc=("[email protected]", "!invalid"),
226 ),
227 NotifySendGrid,
228 )
229
230
231 @mock.patch("requests.get")
232 @mock.patch("requests.post")
233 def test_plugin_sendgrid_attachments(mock_post, mock_get):
234 """NotifySendGrid() Attachments."""
235
236 request = mock.Mock()
237 request.status_code = requests.codes.ok
238
239 # Prepare Mock
240 mock_post.return_value = request
241 mock_get.return_value = request
242
243 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
244 attach = AppriseAttachment(path)
245 obj = Apprise.instantiate("sendgrid://abcd:[email protected]")
246 assert isinstance(obj, NotifySendGrid)
247 assert (
248 bool(
249 obj.notify(
250 body="body",
251 title="title",
252 notify_type=NotifyType.INFO,
253 attach=attach,
254 )
255 )
256 is True
257 )
258
259 mock_post.reset_mock()
260 mock_get.reset_mock()
261
262 # Try again in a use case where we can't access the file
263 with mock.patch("os.path.isfile", return_value=False):
264 assert (
265 bool(
266 obj.notify(
267 body="body",
268 title="title",
269 notify_type=NotifyType.INFO,
270 attach=attach,
271 )
272 )
273 is False
274 )
275
276 # Try again in a use case where we can't access the file
277 with mock.patch("builtins.open", side_effect=OSError):
278 assert (
279 bool(
280 obj.notify(
281 body="body",
282 title="title",
283 notify_type=NotifyType.INFO,
284 attach=attach,
285 )
286 )
287 is False
288 )