Debian Codegraph @

tests/test_plugin_nextcloudtalk.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 from unittest import mock
31
32 from helpers import AppriseURLTester
33 import requests
34
35 from apprise import Apprise, NotifyType
36 from apprise.plugins.nextcloudtalk import NotifyNextcloudTalk
37
38 logging.disable(logging.CRITICAL)
39
40 apprise_url_tests = (
41 ##################################
42 # NotifyNextcloudTalk
43 ##################################
44 (
45 "nctalk://:@/",
46 {
47 "instance": None,
48 },
49 ),
50 (
51 "nctalk://",
52 {
53 "instance": None,
54 },
55 ),
56 (
57 "nctalks://",
58 {
59 # No hostname
60 "instance": None,
61 },
62 ),
63 (
64 "nctalk://localhost",
65 {
66 # No user and password and roomid specified
67 "instance": TypeError,
68 },
69 ),
70 (
71 "nctalk://localhost/roomid",
72 {
73 # No user and password specified
74 "instance": TypeError,
75 },
76 ),
77 (
78 "nctalk://user@localhost/roomid",
79 {
80 # No password specified
81 "instance": TypeError,
82 },
83 ),
84 (
85 "nctalk://user:pass@localhost",
86 {
87 # No roomid specified
88 "instance": NotifyNextcloudTalk,
89 # Since there are no targets specified we expect a False return on
90 # send()
91 "notify_response": False,
92 },
93 ),
94 (
95 "nctalk://user:pass@localhost/roomid1/roomid2",
96 {
97 "instance": NotifyNextcloudTalk,
98 "requests_response_code": requests.codes.created,
99 # Our expected url(privacy=True) startswith() response:
100 "privacy_url": "nctalk://user:****@localhost/roomid1/roomid2",
101 },
102 ),
103 (
104 "nctalk://user:pass@localhost:8080/roomid",
105 {
106 "instance": NotifyNextcloudTalk,
107 "requests_response_code": requests.codes.created,
108 },
109 ),
110 (
111 "nctalk://user:pass@localhost:8080/roomid?url_prefix=/prefix",
112 {
113 "instance": NotifyNextcloudTalk,
114 "requests_response_code": requests.codes.created,
115 },
116 ),
117 (
118 "nctalks://user:pass@localhost/roomid",
119 {
120 "instance": NotifyNextcloudTalk,
121 "requests_response_code": requests.codes.created,
122 # Our expected url(privacy=True) startswith() response:
123 "privacy_url": "nctalks://user:****@localhost/roomid",
124 },
125 ),
126 (
127 "nctalks://user:pass@localhost:8080/roomid/",
128 {
129 "instance": NotifyNextcloudTalk,
130 "requests_response_code": requests.codes.created,
131 },
132 ),
133 (
134 "nctalk://user:pass@localhost:8080/roomid?+HeaderKey=HeaderValue",
135 {
136 "instance": NotifyNextcloudTalk,
137 "requests_response_code": requests.codes.created,
138 },
139 ),
140 (
141 "nctalk://user:pass@localhost:8081/roomid",
142 {
143 "instance": NotifyNextcloudTalk,
144 # force a failure
145 "response": False,
146 "requests_response_code": requests.codes.internal_server_error,
147 },
148 ),
149 (
150 "nctalk://user:pass@localhost:8082/roomid",
151 {
152 "instance": NotifyNextcloudTalk,
153 # throw a bizarre code forcing us to fail to look it up
154 "response": False,
155 "requests_response_code": 999,
156 },
157 ),
158 (
159 "nctalk://user:pass@localhost:8083/roomid1/roomid2/roomid3",
160 {
161 "instance": NotifyNextcloudTalk,
162 # Throws a series of i/o exceptions with this flag
163 # is set and tests that we gracefully handle them
164 "test_requests_exceptions": True,
165 },
166 ),
167 )
168
169
170 def test_plugin_nextcloudtalk_urls():
171 """NotifyNextcloudTalk() Apprise URLs."""
172
173 # Run our general tests
174 AppriseURLTester(tests=apprise_url_tests).run_all()
175
176
177 @mock.patch("requests.post")
178 def test_plugin_nextcloudtalk_edge_cases(mock_post):
179 """NotifyNextcloudTalk() Edge Cases."""
180
181 # A response
182 robj = mock.Mock()
183 robj.content = ""
184 robj.status_code = requests.codes.created
185
186 # Prepare Mock
187 mock_post.return_value = robj
188
189 # Variation Initializations
190 obj = NotifyNextcloudTalk(
191 host="localhost", user="admin", password="pass", targets="roomid"
192 )
193 assert isinstance(obj, NotifyNextcloudTalk) is True
194 assert isinstance(obj.url(), str) is True
195
196 # An empty body
197 assert obj.send(body="") is True
198 assert "data" in mock_post.call_args_list[0][1]
199 assert "message" in mock_post.call_args_list[0][1]["data"]
200
201
202 @mock.patch("requests.post")
203 def test_plugin_nextcloud_talk_url_prefix(mock_post):
204 """NotifyNextcloudTalk() URL Prefix Testing."""
205
206 response = mock.Mock()
207 response.content = ""
208 response.status_code = requests.codes.created
209
210 # Prepare our mock object
211 mock_post.return_value = response
212
213 # instantiate our object (without a batch mode)
214 obj = Apprise.instantiate(
215 "nctalk://user:pass@localhost/admin/?url_prefix=/abcd"
216 )
217
218 assert (
219 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
220 is True
221 )
222
223 # Not set to batch, so we send 2 different messages
224 assert mock_post.call_count == 1
225 assert (
226 mock_post.call_args_list[0][0][0]
227 == "http://localhost/abcd/ocs/v2.php/apps/spreed/api/v1/chat/admin"
228 )
229
230 mock_post.reset_mock()
231
232 # instantiate our object (without a batch mode)
233 obj = Apprise.instantiate(
234 "nctalk://user:pass@localhost/admin/?url_prefix=a/longer/path/abcd/"
235 )
236
237 assert (
238 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
239 is True
240 )
241
242 # Not set to batch, so we send 2 different messages
243 assert mock_post.call_count == 1
244 assert (
245 mock_post.call_args_list[0][0][0]
246 == "http://localhost/a/longer/path/abcd/"
247 "ocs/v2.php/apps/spreed/api/v1/chat/admin"
248 )