Debian Codegraph @

tests/test_plugin_guilded.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.exception import AppriseImproperlyConfigured
38 from apprise.plugins.guilded import NotifyGuilded
39
40 logging.disable(logging.CRITICAL)
41
42 # Attachment Directory
43 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
44
45 # Our Testing URLs
46 apprise_url_tests = (
47 (
48 "guilded://",
49 {
50 "instance": AppriseImproperlyConfigured,
51 },
52 ),
53 # An invalid url
54 (
55 "guilded://:@/",
56 {
57 "instance": AppriseImproperlyConfigured,
58 },
59 ),
60 # No webhook_token specified
61 (
62 "guilded://%s" % ("i" * 24),
63 {
64 "instance": AppriseImproperlyConfigured,
65 },
66 ),
67 # Provide both an webhook id and a webhook token
68 (
69 "guilded://{}/{}".format("i" * 24, "t" * 64),
70 {
71 "instance": NotifyGuilded,
72 "requests_response_code": requests.codes.no_content,
73 },
74 ),
75 # Provide a temporary username
76 (
77 "guilded://l2g@{}/{}".format("i" * 24, "t" * 64),
78 {
79 "instance": NotifyGuilded,
80 "requests_response_code": requests.codes.no_content,
81 },
82 ),
83 # test image= field
84 (
85 "guilded://{}/{}?format=markdown&footer=Yes&image=Yes".format(
86 "i" * 24, "t" * 64
87 ),
88 {
89 "instance": NotifyGuilded,
90 "requests_response_code": requests.codes.no_content,
91 # don't include an image by default
92 "include_image": False,
93 },
94 ),
95 (
96 "guilded://{}/{}?format=markdown&footer=Yes&image=No&fields=no".format(
97 "i" * 24, "t" * 64
98 ),
99 {
100 "instance": NotifyGuilded,
101 "requests_response_code": requests.codes.no_content,
102 },
103 ),
104 (
105 "guilded://{}/{}?format=markdown&footer=Yes&image=Yes".format(
106 "i" * 24, "t" * 64
107 ),
108 {
109 "instance": NotifyGuilded,
110 "requests_response_code": requests.codes.no_content,
111 },
112 ),
113 (
114 "https://media.guilded.gg/webhooks/{}/{}".format("0" * 10, "B" * 40),
115 {
116 # Native URL Support, support the provided guilded URL from their
117 # webpage.
118 "instance": NotifyGuilded,
119 "requests_response_code": requests.codes.no_content,
120 },
121 ),
122 (
123 "guilded://{}/{}?format=markdown&avatar=No&footer=No".format(
124 "i" * 24, "t" * 64
125 ),
126 {
127 "instance": NotifyGuilded,
128 "requests_response_code": requests.codes.no_content,
129 },
130 ),
131 # different format support
132 (
133 "guilded://{}/{}?format=markdown".format("i" * 24, "t" * 64),
134 {
135 "instance": NotifyGuilded,
136 "requests_response_code": requests.codes.no_content,
137 },
138 ),
139 (
140 "guilded://{}/{}?format=text".format("i" * 24, "t" * 64),
141 {
142 "instance": NotifyGuilded,
143 "requests_response_code": requests.codes.no_content,
144 },
145 ),
146 # Test with avatar URL
147 (
148 "guilded://{}/{}?avatar_url=http://localhost/test.jpg".format(
149 "i" * 24, "t" * 64
150 ),
151 {
152 "instance": NotifyGuilded,
153 "requests_response_code": requests.codes.no_content,
154 },
155 ),
156 # Test without image set
157 (
158 "guilded://{}/{}".format("i" * 24, "t" * 64),
159 {
160 "instance": NotifyGuilded,
161 "requests_response_code": requests.codes.no_content,
162 # don't include an image by default
163 "include_image": False,
164 },
165 ),
166 (
167 "guilded://{}/{}/".format("a" * 24, "b" * 64),
168 {
169 "instance": NotifyGuilded,
170 # force a failure
171 "response": False,
172 "requests_response_code": requests.codes.internal_server_error,
173 },
174 ),
175 (
176 "guilded://{}/{}/".format("a" * 24, "b" * 64),
177 {
178 "instance": NotifyGuilded,
179 # throw a bizarre code forcing us to fail to look it up
180 "response": False,
181 "requests_response_code": 999,
182 },
183 ),
184 (
185 "guilded://{}/{}/".format("a" * 24, "b" * 64),
186 {
187 "instance": NotifyGuilded,
188 # Throws a series of i/o exceptions with this flag
189 # is set and tests that we gracefully handle them
190 "test_requests_exceptions": True,
191 },
192 ),
193 )
194
195
196 def test_plugin_guilded_urls():
197 """NotifyGuilded() Apprise URLs."""
198
199 # Run our general tests
200 AppriseURLTester(tests=apprise_url_tests).run_all()
201
202
203 @mock.patch("requests.post")
204 def test_plugin_guilded_general(mock_post):
205 """NotifyGuilded() General Checks."""
206
207 # Initialize some generic (but valid) tokens
208 webhook_id = "A" * 24
209 webhook_token = "B" * 64
210
211 # Prepare Mock
212 mock_post.return_value = requests.Request()
213 mock_post.return_value.status_code = requests.codes.ok
214
215 # Invalid webhook id
216 with pytest.raises(AppriseImproperlyConfigured):
217 NotifyGuilded(webhook_id=None, webhook_token=webhook_token)
218 # Invalid webhook id (whitespace)
219 with pytest.raises(AppriseImproperlyConfigured):
220 NotifyGuilded(webhook_id=" ", webhook_token=webhook_token)
221
222 # Invalid webhook token
223 with pytest.raises(AppriseImproperlyConfigured):
224 NotifyGuilded(webhook_id=webhook_id, webhook_token=None)
225 # Invalid webhook token (whitespace)
226 with pytest.raises(AppriseImproperlyConfigured):
227 NotifyGuilded(webhook_id=webhook_id, webhook_token=" ")
228
229 obj = NotifyGuilded(
230 webhook_id=webhook_id,
231 webhook_token=webhook_token,
232 footer=True,
233 thumbnail=False,
234 )
235
236 # Test that we get a string response
237 assert isinstance(obj.url(), str)