Debian Codegraph @

tests/test_plugin_emby.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 from json import dumps
29
30 # Disable logging for a cleaner testing output
31 import logging
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import requests
36
37 from apprise import Apprise
38 from apprise.plugins.emby import NotifyEmby
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 # Insecure Request; no hostname specified
45 (
46 "emby://",
47 {
48 "instance": None,
49 },
50 ),
51 # Secure Emby Request; no hostname specified
52 (
53 "embys://",
54 {
55 "instance": None,
56 },
57 ),
58 # No user specified
59 (
60 "emby://localhost",
61 {
62 # Missing a username
63 "instance": TypeError,
64 },
65 ),
66 (
67 "emby://:@/",
68 {
69 "instance": None,
70 },
71 ),
72 # Valid Authentication
73 (
74 "emby://l2g@localhost",
75 {
76 "instance": NotifyEmby,
77 # our response will be False because our authentication can't be
78 # tested very well using this matrix.
79 "response": False,
80 },
81 ),
82 (
83 "embys://l2g:password@localhost",
84 {
85 "instance": NotifyEmby,
86 # our response will be False because our authentication can't be
87 # tested very well using this matrix.
88 "response": False,
89 # Our expected url(privacy=True) startswith() response:
90 "privacy_url": "embys://l2g:****@localhost",
91 },
92 ),
93 )
94
95
96 def test_plugin_template_urls():
97 """NotifyTemplate() Apprise URLs."""
98
99 # Run our general tests
100 AppriseURLTester(tests=apprise_url_tests).run_all()
101
102
103 @mock.patch("apprise.plugins.emby.NotifyEmby.sessions")
104 @mock.patch("apprise.plugins.emby.NotifyEmby.login")
105 @mock.patch("apprise.plugins.emby.NotifyEmby.logout")
106 @mock.patch("requests.get")
107 @mock.patch("requests.post")
108 def test_plugin_emby_general(
109 mock_post, mock_get, mock_logout, mock_login, mock_sessions
110 ):
111 """NotifyEmby General Tests."""
112
113 req = requests.Request()
114 req.status_code = requests.codes.ok
115 req.content = ""
116 mock_get.return_value = req
117 mock_post.return_value = req
118
119 # This is done so we don't obstruct our access_token and user_id values
120 mock_login.return_value = True
121 mock_logout.return_value = True
122 mock_sessions.return_value = {"abcd": {}}
123
124 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost?modal=False")
125 assert isinstance(obj, NotifyEmby)
126 assert obj.notify("title", "body", "info") is True
127 obj.access_token = "abc"
128 obj.user_id = "123"
129
130 # Test Modal support
131 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost?modal=True")
132 assert isinstance(obj, NotifyEmby)
133 assert obj.notify("title", "body", "info") is True
134 obj.access_token = "abc"
135 obj.user_id = "123"
136
137 # Test our exception handling
138 for exception in AppriseURLTester.req_exceptions:
139 mock_post.side_effect = exception
140 mock_get.side_effect = exception
141 # We'll fail to log in each time
142 assert obj.notify("title", "body", "info") is False
143
144 # Disable Exceptions
145 mock_post.side_effect = None
146 mock_get.side_effect = None
147
148 # Our login flat out fails if we don't have proper parseable content
149 mock_post.return_value.content = ""
150 mock_get.return_value.content = mock_post.return_value.content
151
152 # KeyError handling
153 mock_post.return_value.status_code = 999
154 mock_get.return_value.status_code = 999
155 assert obj.notify("title", "body", "info") is False
156
157 # General Internal Server Error
158 mock_post.return_value.status_code = requests.codes.internal_server_error
159 mock_get.return_value.status_code = requests.codes.internal_server_error
160 assert obj.notify("title", "body", "info") is False
161
162 mock_post.return_value.status_code = requests.codes.ok
163 mock_get.return_value.status_code = requests.codes.ok
164 mock_get.return_value.content = mock_post.return_value.content
165
166 # Disable the port completely
167 obj.port = None
168 assert obj.notify("title", "body", "info") is True
169
170 # An Empty return set (no query is made, but notification will still
171 # succeed
172 mock_sessions.return_value = {}
173 assert obj.notify("title", "body", "info") is True
174
175 # Tidy our object
176 del obj
177
178
179 @mock.patch("requests.get")
180 @mock.patch("requests.post")
181 def test_plugin_emby_login(mock_post, mock_get):
182 """NotifyEmby() login()"""
183
184 # Prepare Mock
185 mock_get.return_value = requests.Request()
186 mock_post.return_value = requests.Request()
187
188 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost")
189 assert isinstance(obj, NotifyEmby)
190
191 # Test our exception handling
192 for exception in AppriseURLTester.req_exceptions:
193 mock_post.side_effect = exception
194 mock_get.side_effect = exception
195 # We'll fail to log in each time
196 assert obj.login() is False
197
198 # Disable Exceptions
199 mock_post.side_effect = None
200 mock_get.side_effect = None
201
202 # Our login flat out fails if we don't have proper parseable content
203 mock_post.return_value.content = ""
204 mock_get.return_value.content = mock_post.return_value.content
205
206 # KeyError handling
207 mock_post.return_value.status_code = 999
208 mock_get.return_value.status_code = 999
209 assert obj.login() is False
210
211 # General Internal Server Error
212 mock_post.return_value.status_code = requests.codes.internal_server_error
213 mock_get.return_value.status_code = requests.codes.internal_server_error
214 assert obj.login() is False
215
216 mock_post.return_value.status_code = requests.codes.ok
217 mock_get.return_value.status_code = requests.codes.ok
218
219 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost:1234")
220 # Set a different port (outside of default)
221 assert isinstance(obj, NotifyEmby)
222 assert obj.port == 1234
223
224 # The login will fail because '' is not a parseable JSON response
225 assert obj.login() is False
226
227 # Disable the port completely
228 obj.port = None
229 assert obj.login() is False
230
231 # Default port assignments
232 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost")
233 assert isinstance(obj, NotifyEmby)
234 assert obj.port == 8096
235
236 # The login will (still) fail because '' is not a parseable JSON response
237 assert obj.login() is False
238
239 # Our login flat out fails if we don't have proper parseable content
240 mock_post.return_value.content = dumps(
241 {
242 "AccessToken": "0000-0000-0000-0000",
243 }
244 )
245 mock_get.return_value.content = mock_post.return_value.content
246
247 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost")
248 assert isinstance(obj, NotifyEmby)
249
250 # The login will fail because the 'User' or 'Id' field wasn't parsed
251 assert obj.login() is False
252
253 # Our text content (we intentionally reverse the 2 locations
254 # that store the same thing; we do this so we can test which
255 # one it defaults to if both are present
256 mock_post.return_value.content = dumps(
257 {
258 "User": {
259 "Id": "abcd123",
260 },
261 "Id": "123abc",
262 "AccessToken": "0000-0000-0000-0000",
263 }
264 )
265 mock_get.return_value.content = mock_post.return_value.content
266
267 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost")
268 assert isinstance(obj, NotifyEmby)
269
270 # Login
271 assert obj.login() is True
272 assert obj.user_id == "123abc"
273 assert obj.access_token == "0000-0000-0000-0000"
274
275 # We're going to log in a second time which checks that we logout
276 # first before logging in again. But this time we'll scrap the
277 # 'Id' area and use the one found in the User area if detected
278 mock_post.return_value.content = dumps(
279 {
280 "User": {
281 "Id": "abcd123",
282 },
283 "AccessToken": "0000-0000-0000-0000",
284 }
285 )
286 mock_get.return_value.content = mock_post.return_value.content
287
288 # Login
289 assert obj.login() is True
290 assert obj.user_id == "abcd123"
291 assert obj.access_token == "0000-0000-0000-0000"
292
293
294 @mock.patch("apprise.plugins.emby.NotifyEmby.login")
295 @mock.patch("apprise.plugins.emby.NotifyEmby.logout")
296 @mock.patch("requests.get")
297 @mock.patch("requests.post")
298 def test_plugin_emby_sessions(mock_post, mock_get, mock_logout, mock_login):
299 """NotifyEmby() sessions()"""
300
301 # Prepare Mock
302 mock_get.return_value = requests.Request()
303 mock_post.return_value = requests.Request()
304
305 # This is done so we don't obstruct our access_token and user_id values
306 mock_login.return_value = True
307 mock_logout.return_value = True
308
309 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost")
310 assert isinstance(obj, NotifyEmby)
311 obj.access_token = "abc"
312 obj.user_id = "123"
313
314 # Test our exception handling
315 for exception in AppriseURLTester.req_exceptions:
316 mock_post.side_effect = exception
317 mock_get.side_effect = exception
318 # We'll fail to log in each time
319 sessions = obj.sessions()
320 assert isinstance(sessions, dict) is True
321 assert len(sessions) == 0
322
323 # Disable Exceptions
324 mock_post.side_effect = None
325 mock_get.side_effect = None
326
327 # Our login flat out fails if we don't have proper parseable content
328 mock_post.return_value.content = ""
329 mock_get.return_value.content = mock_post.return_value.content
330
331 # KeyError handling
332 mock_post.return_value.status_code = 999
333 mock_get.return_value.status_code = 999
334 sessions = obj.sessions()
335 assert isinstance(sessions, dict) is True
336 assert len(sessions) == 0
337
338 # General Internal Server Error
339 mock_post.return_value.status_code = requests.codes.internal_server_error
340 mock_get.return_value.status_code = requests.codes.internal_server_error
341 sessions = obj.sessions()
342 assert isinstance(sessions, dict) is True
343 assert len(sessions) == 0
344
345 mock_post.return_value.status_code = requests.codes.ok
346 mock_get.return_value.status_code = requests.codes.ok
347 mock_get.return_value.content = mock_post.return_value.content
348
349 # Disable the port completely
350 obj.port = None
351
352 sessions = obj.sessions()
353 assert isinstance(sessions, dict) is True
354 assert len(sessions) == 0
355
356 # Let's get some results
357 mock_post.return_value.content = dumps(
358 [
359 {
360 "Id": "abc123",
361 },
362 {
363 "Id": "def456",
364 },
365 {
366 "InvalidEntry": None,
367 },
368 ]
369 )
370 mock_get.return_value.content = mock_post.return_value.content
371
372 sessions = obj.sessions(user_controlled=True)
373 assert isinstance(sessions, dict) is True
374 assert len(sessions) == 2
375
376 # Test it without setting user-controlled sessions
377 sessions = obj.sessions(user_controlled=False)
378 assert isinstance(sessions, dict) is True
379 assert len(sessions) == 2
380
381 # Triggers an authentication failure
382 obj.user_id = None
383 mock_login.return_value = False
384 sessions = obj.sessions()
385 assert isinstance(sessions, dict) is True
386 assert len(sessions) == 0
387
388
389 @mock.patch("apprise.plugins.emby.NotifyEmby.login")
390 @mock.patch("requests.get")
391 @mock.patch("requests.post")
392 def test_plugin_emby_logout(mock_post, mock_get, mock_login):
393 """NotifyEmby() logout()"""
394
395 # Prepare Mock
396 mock_get.return_value = requests.Request()
397 mock_post.return_value = requests.Request()
398
399 # This is done so we don't obstruct our access_token and user_id values
400 mock_login.return_value = True
401
402 obj = Apprise.instantiate("emby://l2g:l2gpass@localhost")
403 assert isinstance(obj, NotifyEmby)
404 obj.access_token = "abc"
405 obj.user_id = "123"
406
407 # Test our exception handling
408 for exception in AppriseURLTester.req_exceptions:
409 mock_post.side_effect = exception
410 mock_get.side_effect = exception
411 # We'll fail to log in each time
412 obj.logout()
413 obj.access_token = "abc"
414 obj.user_id = "123"
415
416 # Disable Exceptions
417 mock_post.side_effect = None
418 mock_get.side_effect = None
419
420 # Our login flat out fails if we don't have proper parseable content
421 mock_post.return_value.content = ""
422 mock_get.return_value.content = mock_post.return_value.content
423
424 # KeyError handling
425 mock_post.return_value.status_code = 999
426 mock_get.return_value.status_code = 999
427 obj.logout()
428 obj.access_token = "abc"
429 obj.user_id = "123"
430
431 # General Internal Server Error
432 mock_post.return_value.status_code = requests.codes.internal_server_error
433 mock_get.return_value.status_code = requests.codes.internal_server_error
434 obj.logout()
435 obj.access_token = "abc"
436 obj.user_id = "123"
437
438 mock_post.return_value.status_code = requests.codes.ok
439 mock_get.return_value.status_code = requests.codes.ok
440 mock_get.return_value.content = mock_post.return_value.content
441
442 # Disable the port completely
443 obj.port = None
444
445 # Perform logout
446 obj.logout()
447
448 # Calling logout on an object already logged out
449 obj.logout()
450
451 # Test Python v3.5 LookupError Bug: https://bugs.python.org/issue29288
452 mock_post.side_effect = LookupError()
453 mock_get.side_effect = LookupError()
454 obj.access_token = "abc"
455 obj.user_id = "123"
456
457 # Tidy object
458 del obj