Debian Codegraph @

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