Debian Codegraph @

tests/test_plugin_lauther.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.mock import Mock, patch
31
32 from helpers import AppriseURLTester
33 import requests
34
35 from apprise import Apprise, NotifyType
36 from apprise.exception import AppriseImproperlyConfigured
37 from apprise.plugins.lauther import LautherPriority, NotifyLauther
38
39 logging.disable(logging.CRITICAL)
40
41 # Our Testing URLs
42 apprise_url_tests = (
43 (
44 "lauther://",
45 {
46 # No token specified
47 "instance": AppriseImproperlyConfigured,
48 },
49 ),
50 (
51 "lauther://:@/",
52 {
53 "instance": AppriseImproperlyConfigured,
54 },
55 ),
56 (
57 "lauther://%badtoken%",
58 {
59 # Not a valid token
60 "instance": AppriseImproperlyConfigured,
61 },
62 ),
63 (
64 "lauther://abc123",
65 {
66 # Tokens must carry the lpt_ prefix
67 "instance": AppriseImproperlyConfigured,
68 },
69 ),
70 (
71 "lauther://lpt_abc123",
72 {
73 # A valid token
74 "instance": NotifyLauther,
75 # Our expected url(privacy=True) startswith() response:
76 "privacy_url": "lauther://l...3/",
77 },
78 ),
79 (
80 "lauther://?token=lpt_abc123",
81 {
82 # Token provided as an argument
83 "instance": NotifyLauther,
84 },
85 ),
86 (
87 "lauther://lpt_abc123?priority=high",
88 {
89 # Priority by name
90 "instance": NotifyLauther,
91 },
92 ),
93 (
94 "lauther://lpt_abc123?priority=2",
95 {
96 # Priority by value
97 "instance": NotifyLauther,
98 },
99 ),
100 (
101 "lauther://lpt_abc123?priority=invalid",
102 {
103 # An invalid priority falls back to the default
104 "instance": NotifyLauther,
105 },
106 ),
107 (
108 "lauther://lpt_abc123?priority=99",
109 {
110 # An out-of-range priority is not acceptable
111 "instance": AppriseImproperlyConfigured,
112 },
113 ),
114 (
115 "lauther://lpt_abc123?sound=default&click=https://example.ca",
116 {
117 # Sound and click-through URL
118 "instance": NotifyLauther,
119 },
120 ),
121 (
122 "lauther://lpt_abc123"
123 "?icon=https://example.ca/icon.png&color=%23D9EF00"
124 "&group=orders&route=/orders/123",
125 {
126 # Icon, color, group, and paired-site route overrides
127 "instance": NotifyLauther,
128 },
129 ),
130 (
131 "lauther://lpt_abc123",
132 {
133 "instance": NotifyLauther,
134 # force a failure
135 "response": False,
136 "requests_response_code": requests.codes.internal_server_error,
137 },
138 ),
139 (
140 "lauther://lpt_abc123",
141 {
142 "instance": NotifyLauther,
143 # throw a bizarre code forcing us to fail to look it up
144 "response": False,
145 "requests_response_code": 999,
146 },
147 ),
148 (
149 "lauther://lpt_abc123",
150 {
151 "instance": NotifyLauther,
152 # Throws a series of i/o exceptions with this flag
153 # is set and tests that we gracefully handle them
154 "test_requests_exceptions": True,
155 },
156 ),
157 )
158
159
160 def test_plugin_lauther_urls():
161 """NotifyLauther() Apprise URLs."""
162
163 # Run our general tests
164 AppriseURLTester(tests=apprise_url_tests).run_all()
165
166
167 @patch("requests.post")
168 def test_plugin_lauther_general(mock_post):
169 """NotifyLauther() General Checks."""
170
171 response = Mock()
172 response.status_code = requests.codes.ok
173 mock_post.return_value = response
174
175 # Prepare our object
176 obj = Apprise.instantiate("lauther://lpt_abc123")
177 assert isinstance(obj, NotifyLauther)
178 assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
179
180 assert mock_post.call_count == 1
181 details = mock_post.call_args_list[0]
182 assert details[0][0] == "https://api.lauther.id/v1/push"
183
184 # The token travels in the Authorization header, never the payload
185 assert details[1]["headers"]["Authorization"] == "Bearer lpt_abc123"
186 assert "token" not in details[1]["json"]
187
188 # Verify our url() call is reversible back into the same object
189 assert isinstance(obj.url(), str)
190 obj_b = Apprise.instantiate(obj.url())
191 assert isinstance(obj_b, NotifyLauther)
192 assert obj.url_identifier == obj_b.url_identifier
193
194 # The token is masked when privacy is requested
195 assert "lpt_abc123" not in obj.url(privacy=True)
196
197
198 @patch("requests.post")
199 def test_plugin_lauther_priority(mock_post):
200 """NotifyLauther() Priority Handling."""
201
202 response = Mock()
203 response.status_code = requests.codes.ok
204 mock_post.return_value = response
205
206 # Named priorities resolve to their integer equivalent
207 obj = Apprise.instantiate("lauther://lpt_abc123?priority=emergency")
208 assert isinstance(obj, NotifyLauther)
209 assert obj.priority == LautherPriority.EMERGENCY
210
211 # As do numeric ones
212 obj = Apprise.instantiate("lauther://lpt_abc123?priority=-2")
213 assert isinstance(obj, NotifyLauther)
214 assert obj.priority == LautherPriority.LOWEST
215
216 # No priority at all lands on the default
217 obj = Apprise.instantiate("lauther://lpt_abc123")
218 assert isinstance(obj, NotifyLauther)
219 assert obj.priority == LautherPriority.NORMAL
220
221 # "low" and "lowest" share a prefix; each must resolve to itself. A
222 # shortest-key-first scan silently mapped "low" onto LOWEST.
223 obj = Apprise.instantiate("lauther://lpt_abc123?priority=low")
224 assert isinstance(obj, NotifyLauther)
225 assert obj.priority == LautherPriority.LOW
226
227 obj = Apprise.instantiate("lauther://lpt_abc123?priority=lowest")
228 assert isinstance(obj, NotifyLauther)
229 assert obj.priority == LautherPriority.LOWEST