Debian Codegraph @

tests/test_plugin_simplepush.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 import json
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import sys
33 from unittest import mock
34
35 from helpers import AppriseURLTester
36 import pytest
37 import requests
38
39 from apprise import Apprise
40 from apprise.exception import AppriseImproperlyConfigured
41 from apprise.plugins.simplepush import NotifySimplePush
42
43 logging.disable(logging.CRITICAL)
44
45 # Our Testing URLs
46 apprise_url_tests = (
47 (
48 "spush://",
49 {
50 # No api key
51 "instance": AppriseImproperlyConfigured,
52 },
53 ),
54 (
55 "spush://{}".format("A" * 14),
56 {
57 # API Key specified however expected server response
58 # didn't have 'OK' in JSON response
59 "instance": NotifySimplePush,
60 # Expected notify() response
61 "notify_response": False,
62 },
63 ),
64 (
65 "spush://{}".format("Y" * 14),
66 {
67 # API Key valid and expected response was valid
68 "instance": NotifySimplePush,
69 # Set our response to OK
70 "requests_response_text": {
71 "status": "OK",
72 },
73 # Our expected url(privacy=True) startswith() response:
74 "privacy_url": "spush://Y...Y/",
75 },
76 ),
77 (
78 "spush://{}?event=Not%20So%20Good".format("X" * 14),
79 {
80 # API Key valid and expected response was valid
81 "instance": NotifySimplePush,
82 # Set our response to something that is not okay
83 "requests_response_text": {
84 "status": "NOT-OK",
85 },
86 # Expected notify() response
87 "notify_response": False,
88 },
89 ),
90 (
91 "spush://salt:pass@{}".format("X" * 14),
92 {
93 # Now we'll test encrypted messages with new salt
94 "instance": NotifySimplePush,
95 # Set our response to OK
96 "requests_response_text": {
97 "status": "OK",
98 },
99 # Our expected url(privacy=True) startswith() response:
100 "privacy_url": "spush://****:****@X...X/",
101 },
102 ),
103 (
104 "spush://{}".format("Y" * 14),
105 {
106 "instance": NotifySimplePush,
107 # throw a bizarre code forcing us to fail to look it up
108 "response": False,
109 "requests_response_code": 999,
110 # Set a failing message too
111 "requests_response_text": {
112 "status": "BadRequest",
113 "message": "Title or message too long",
114 },
115 },
116 ),
117 (
118 "spush://{}".format("Z" * 14),
119 {
120 "instance": NotifySimplePush,
121 # Throws a series of i/o exceptions with this flag
122 # is set and tests that we gracefully handle them
123 "test_requests_exceptions": True,
124 },
125 ),
126 )
127
128
129 @pytest.mark.skipif(
130 "cryptography" not in sys.modules, reason="Requires cryptography"
131 )
132 def test_plugin_simplepush_urls():
133 """NotifySimplePush() Apprise URLs."""
134
135 # Run our general tests
136 AppriseURLTester(tests=apprise_url_tests).run_all()
137
138
139 @pytest.mark.skipif(
140 "cryptography" in sys.modules,
141 reason="Requires that cryptography NOT be installed",
142 )
143 def test_plugin_simpepush_cryptography_import_error():
144 """
145 NotifySimplePush() Cryptography loading failure
146 """
147
148 # Attempt to instantiate our object
149 obj = Apprise.instantiate("spush://{}".format("Y" * 14))
150
151 # It's not possible because our cryptography depedancy is missing
152 assert obj is None
153
154
155 @pytest.mark.skipif(
156 "cryptography" not in sys.modules, reason="Requires cryptography"
157 )
158 def test_plugin_simplepush_edge_cases():
159 """
160 NotifySimplePush() Edge Cases
161
162 """
163
164 # No token
165 with pytest.raises(AppriseImproperlyConfigured):
166 NotifySimplePush(apikey=None)
167
168 with pytest.raises(AppriseImproperlyConfigured):
169 NotifySimplePush(apikey=" ")
170
171 # Bad event
172 with pytest.raises(AppriseImproperlyConfigured):
173 NotifySimplePush(apikey="abc", event=object)
174
175 with pytest.raises(AppriseImproperlyConfigured):
176 NotifySimplePush(apikey="abc", event=" ")
177
178
179 @pytest.mark.skipif(
180 "cryptography" not in sys.modules, reason="Requires cryptography"
181 )
182 @mock.patch("requests.post")
183 def test_plugin_simplepush_general(mock_post):
184 """NotifySimplePush() General Tests."""
185
186 # Prepare a good response
187 response = mock.Mock()
188 response.content = json.dumps(
189 {
190 "status": "OK",
191 }
192 )
193 response.status_code = requests.codes.ok
194 mock_post.return_value = response
195
196 obj = Apprise.instantiate("spush://{}".format("Y" * 14))
197
198 # Verify our content works as expected
199 assert bool(obj.notify(title="test", body="test")) is True