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

Commit a2dbcf8

Browse files
testing tests
1 parent 1971431 commit a2dbcf8

File tree

5 files changed

+132
-57
lines changed

5 files changed

+132
-57
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def render_template(*args, **kwargs):
6262
>>> render_template('home.html', title='Home', items=items)
6363
"""
6464
kwargs['Prefs'] = Prefs
65-
kwargs['is_logged_in'] = is_logged_in
65+
kwargs['is_logged_in'] = is_logged_in()
6666
return flask_render_template(*args, **kwargs)
6767

6868

@@ -574,8 +574,8 @@ def is_logged_in():
574574
return False
575575

576576
token = session["token"]
577-
user = get_user_info(token)
578-
logged_in = user and is_server_owner(user)
577+
user = get_user_info(token=token)
578+
logged_in = user and is_server_owner(user=user)
579579
return logged_in
580580

581581

tests/conftest.py

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from plexapi.exceptions import NotFound
1212
from plexapi.server import PlexServer
1313
from plexhints.agent_kit import Agent
14+
from plexhints.prefs_kit import Prefs
1415
import pytest
1516
import requests
1617

@@ -102,6 +103,13 @@ def test_client():
102103
yield test_client # this is where the testing happens!
103104

104105

106+
@pytest.fixture(scope='function')
107+
def test_client_disabled_login(test_client):
108+
"""Create a test client for testing webapp endpoints with login disabled"""
109+
Prefs['bool_webapp_require_login'] = False
110+
return test_client
111+
112+
105113
# plex server fixtures
106114
@pytest.fixture(scope="session")
107115
def plugin_logs():

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):
30+
assert plex_api_helper.get_user_info(token=plex._token)
31+
32+
33+
def test_get_user_info_bad_token():
34+
assert not plex_api_helper.get_user_info(token='bad_token')
35+
36+
37+
def test_is_server_owner(plex):
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_server_owner_bad_user():
43+
assert not plex_api_helper.is_server_owner(user={})

0 commit comments

Comments
 (0)