Debian Codegraph @

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