Skip to content
This repository was archived by the owner on Oct 13, 2024. It is now read-only.

Commit e3da810

Browse files
testing tests
1 parent 1971431 commit e3da810

File tree

7 files changed

+166
-60
lines changed

7 files changed

+166
-60
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ cython_debug/
163163
Contents/Info.plist
164164

165165
# Remove plexhints files
166-
plexhints-temp
166+
plexhints/
167+
plexhints-temp/
167168
*cache.sqlite
168169

169170
# Remove python modules

Contents/Code/plex_api_helper.py

+52-51
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,58 @@ def get_plex_item(rating_key):
598598
return item
599599

600600

601+
def get_user_info(token):
602+
# type: (str) -> Optional[MyPlexAccount]
603+
"""
604+
Get the Plex user info.
605+
606+
Parameters
607+
----------
608+
token : str
609+
The Plex token.
610+
611+
Returns
612+
-------
613+
Optional[MyPlexAccount]
614+
The Plex user info.
615+
616+
Examples
617+
--------
618+
>>> get_user_info(token='...')
619+
...
620+
"""
621+
try:
622+
return MyPlexAccount(token=token)
623+
except Exception as e:
624+
Log.Error('Error getting user info: {}'.format(e))
625+
return None
626+
627+
628+
def is_server_owner(user):
629+
# type: (MyPlexAccount) -> bool
630+
"""
631+
Check if the user is the owner of the Plex server.
632+
633+
Parameters
634+
----------
635+
user : MyPlexAccount
636+
The Plex user info.
637+
638+
Returns
639+
-------
640+
py:class:`bool`
641+
True if the user is the owner of the Plex server, False otherwise.
642+
643+
Examples
644+
--------
645+
>>> is_server_owner(user=...)
646+
...
647+
"""
648+
plex = setup_plexapi()
649+
650+
return plex.account().username in {user.email, user.username}
651+
652+
601653
def process_queue():
602654
# type: () -> None
603655
"""
@@ -766,54 +818,3 @@ def scheduled_update():
766818
for item in all_items:
767819
if item.ratingKey not in q.queue:
768820
q.put(item=item.ratingKey)
769-
770-
771-
def get_user_info(token):
772-
# type: (str) -> Optional[MyPlexAccount]
773-
"""
774-
Get the Plex user info.
775-
776-
Parameters
777-
----------
778-
token : str
779-
The Plex token.
780-
781-
Returns
782-
-------
783-
Optional[MyPlexAccount]
784-
The Plex user info.
785-
786-
Examples
787-
--------
788-
>>> get_user_info(token='...')
789-
...
790-
"""
791-
try:
792-
return MyPlexAccount(token=token)
793-
except Exception:
794-
return None
795-
796-
797-
def is_server_owner(user):
798-
# type: (MyPlexAccount) -> bool
799-
"""
800-
Check if the user is the owner of the Plex server.
801-
802-
Parameters
803-
----------
804-
user : MyPlexAccount
805-
The Plex user info.
806-
807-
Returns
808-
-------
809-
py:class:`bool`
810-
True if the user is the owner of the Plex server, False otherwise.
811-
812-
Examples
813-
--------
814-
>>> is_server_owner(user=...)
815-
...
816-
"""
817-
plex = setup_plexapi()
818-
819-
return plex.account().username in {user.email, user.username}

Contents/Code/webapp.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
import os
1010
from threading import Lock, Thread
11+
from typing import Optional
1112
import uuid
1213

1314
# plex debugging
@@ -62,7 +63,7 @@ def render_template(*args, **kwargs):
6263
>>> render_template('home.html', title='Home', items=items)
6364
"""
6465
kwargs['Prefs'] = Prefs
65-
kwargs['is_logged_in'] = is_logged_in
66+
kwargs['is_logged_in'] = is_logged_in()
6667
return flask_render_template(*args, **kwargs)
6768

6869

@@ -574,8 +575,8 @@ def is_logged_in():
574575
return False
575576

576577
token = session["token"]
577-
user = get_user_info(token)
578-
logged_in = user and is_server_owner(user)
578+
user = get_user_info(token=token)
579+
logged_in = user and is_server_owner(user=user)
579580
return logged_in
580581

581582

@@ -600,7 +601,7 @@ def logout():
600601

601602
@app.before_request
602603
def check_login_status():
603-
# type: () -> None
604+
# type: () -> Optional[flask.redirect]
604605
"""
605606
Check if the user is logged in.
606607

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# development environment requirements, these should not be distributed
22
flake8==3.9.2;python_version<"3"
3+
mock==3.0.5;python_version<"3"
34
m2r2==0.3.2;python_version<"3"
45
numpydoc==0.9.2;python_version<"3"
56
plexhints==2023.1211.160853 # type hinting library for plex development

tests/conftest.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
import os
66
import sys
77
import time
8+
from mock import patch
89

910
# lib imports
1011
import plexapi
1112
from plexapi.exceptions import NotFound
1213
from plexapi.server import PlexServer
1314
from plexhints.agent_kit import Agent
15+
from plexhints.prefs_kit import _PreferenceSet
1416
import pytest
1517
import requests
1618

19+
# create a fake directory for plexhints
20+
if not os.path.exists('plexhints'):
21+
os.mkdir('plexhints')
22+
1723
# add Contents directory to the system path
1824
pytest.root_dir = root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1925
pytest.contents_dir = contents_dir = os.path.join(root_dir, 'Contents')
@@ -87,6 +93,24 @@ def agent(request):
8793
return Themerr()
8894

8995

96+
# extend the _PreferenceSet class, and give it a setter
97+
class PreferenceSet(_PreferenceSet):
98+
def __setitem__(self, key, value):
99+
self.update_user_values(**{key: value})
100+
101+
102+
@pytest.fixture(scope='function')
103+
def prefs_webapp_patch():
104+
with patch('plexhints.prefs_kit._PreferenceSet', autospec=True) as mock_prefs:
105+
mock_prefs = PreferenceSet
106+
yield mock_prefs
107+
108+
109+
@pytest.fixture(scope='function')
110+
def webapp_prefs():
111+
return webapp.Prefs
112+
113+
90114
@pytest.fixture(scope='function')
91115
def test_client():
92116
"""Create a test client for testing webapp endpoints"""
@@ -102,6 +126,14 @@ def test_client():
102126
yield test_client # this is where the testing happens!
103127

104128

129+
@pytest.fixture(scope='function')
130+
def test_client_disabled_login(test_client, prefs_webapp_patch, webapp_prefs):
131+
"""Create a test client for testing webapp endpoints with login disabled"""
132+
webapp_prefs['bool_webapp_require_login'] = False
133+
print('debug: {}'.format(webapp.Prefs['bool_webapp_require_login']))
134+
return test_client
135+
136+
105137
# plex server fixtures
106138
@pytest.fixture(scope="session")
107139
def plugin_logs():
@@ -111,7 +143,6 @@ def plugin_logs():
111143
yield plugin_logs
112144

113145

114-
# plex server fixtures
115146
@pytest.fixture(scope="session")
116147
def plugin_log_file():
117148
# the primary plugin log file
@@ -136,6 +167,11 @@ def plex(request, sess):
136167
return PlexServer(SERVER_BASEURL, SERVER_TOKEN, session=sess)
137168

138169

170+
@pytest.fixture(scope="session")
171+
def plex_token():
172+
return SERVER_TOKEN
173+
174+
139175
@pytest.fixture(params=MOVIE_SECTIONS, scope="session")
140176
def movie_section_names(plex, request):
141177
library_section = request.param

tests/functional/test_webapp.py

+52-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def remove_themerr_db_cache_file():
2525
def test_home(test_client):
2626
"""
2727
WHEN the '/' page is requested (GET)
28-
THEN check that the response is valid
28+
THEN check that the response is a redirect
2929
3030
Repeat for '/home'
3131
"""
@@ -34,9 +34,27 @@ def test_home(test_client):
3434
except AttributeError:
3535
pytest.skip("cannot access Plex token/server")
3636
else:
37-
assert response.status_code == 200
37+
assert response.status_code == 302
3838

3939
response = test_client.get('/home')
40+
assert response.status_code == 302
41+
42+
43+
def test_home_no_login(test_client_disabled_login):
44+
"""
45+
WHEN the '/' page is requested (GET)
46+
THEN check that the response is valid
47+
48+
Repeat for '/home'
49+
"""
50+
try:
51+
response = test_client_disabled_login.get('/')
52+
except AttributeError:
53+
pytest.skip("cannot access Plex token/server")
54+
else:
55+
assert response.status_code == 200
56+
57+
response = test_client_disabled_login.get('/home')
4058
assert response.status_code == 200
4159

4260
assert 'id="section_' in response.data.decode('utf-8')
@@ -45,12 +63,25 @@ def test_home(test_client):
4563
def test_home_without_cache(remove_themerr_db_cache_file, test_client):
4664
"""
4765
WHEN the '/' page is requested (GET)
48-
THEN check that the response is valid
66+
THEN check that the response is a redirect
4967
"""
5068
try:
5169
response = test_client.get('/')
5270
except AttributeError:
5371
pytest.skip("cannot access Plex token/server")
72+
else:
73+
assert response.status_code == 302
74+
75+
76+
def test_home_without_cache_no_login(remove_themerr_db_cache_file, test_client_disabled_login):
77+
"""
78+
WHEN the '/' page is requested (GET)
79+
THEN check that the response is valid
80+
"""
81+
try:
82+
response = test_client_disabled_login.get('/')
83+
except AttributeError:
84+
pytest.skip("cannot access Plex token/server")
5485
else:
5586
assert response.status_code == 200
5687

@@ -68,6 +99,15 @@ def test_image(test_client):
6899
assert response.content_type == 'image/vnd.microsoft.icon'
69100

70101

102+
def test_image_no_login(test_client_disabled_login):
103+
"""
104+
WHEN the '/favicon.ico' file is requested (GET)
105+
THEN check that the response is a redirect
106+
"""
107+
response = test_client_disabled_login.get('favicon.ico')
108+
assert response.status_code == 302
109+
110+
71111
def test_status(test_client):
72112
"""
73113
WHEN the '/status' page is requested (GET)
@@ -76,3 +116,12 @@ def test_status(test_client):
76116
response = test_client.get('/status')
77117
assert response.status_code == 200
78118
assert response.content_type == 'application/json'
119+
120+
121+
def test_status_no_login(test_client_disabled_login):
122+
"""
123+
WHEN the '/status' page is requested (GET)
124+
THEN check that the response is a redirect
125+
"""
126+
response = test_client_disabled_login.get('/status')
127+
assert response.status_code == 302

tests/unit/test_plex_api_helper.py

+17
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,20 @@ def test_change_lock_status(section, lock):
2424
change_status = plex_api_helper.change_lock_status(item, field=field, lock=lock)
2525
assert change_status, 'change_lock_status did not return True'
2626
assert item.isLocked(field=field) == lock, 'Failed to change lock status to {}'.format(lock)
27+
28+
29+
def test_get_user_info(plex_token):
30+
assert plex_api_helper.get_user_info(token=plex_token)
31+
32+
33+
def test_get_user_info_invalid_token():
34+
assert not plex_api_helper.get_user_info(token='invalid_token')
35+
36+
37+
def test_is_server_owner(plex_token):
38+
user = plex_api_helper.get_user_info(token=plex_token)
39+
assert plex_api_helper.is_server_owner(user=user)
40+
41+
42+
def test_is_not_server_owner():
43+
assert plex_api_helper.is_server_owner(user={}) is None

0 commit comments

Comments
 (0)