Debian Codegraph @

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