Debian Codegraph @

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