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

Commit 05fe7a0

Browse files
testing tests
1 parent 1971431 commit 05fe7a0

File tree

6 files changed

+146
-58
lines changed

6 files changed

+146
-58
lines changed

Contents/Code/plex_api_helper.py

Lines changed: 52 additions & 51 deletions
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

Lines changed: 3 additions & 3 deletions
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

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
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 Prefs
1416
import pytest
1517
import requests
1618

@@ -87,6 +89,18 @@ def agent(request):
8789
return Themerr()
8890

8991

92+
@pytest.fixture(scope='function')
93+
def prefs():
94+
with patch('plexhints.prefs_kit.Prefs', autospec=True) as mock_prefs:
95+
def set_item(key, value):
96+
print("Setting {} to {}".format(key, value))
97+
mock_prefs[key] = value
98+
99+
mock_prefs.__setitem__.side_effect = set_item
100+
101+
yield mock_prefs
102+
103+
90104
@pytest.fixture(scope='function')
91105
def test_client():
92106
"""Create a test client for testing webapp endpoints"""
@@ -102,6 +116,13 @@ def test_client():
102116
yield test_client # this is where the testing happens!
103117

104118

119+
@pytest.fixture(scope='function')
120+
def test_client_disabled_login(test_client, prefs):
121+
"""Create a test client for testing webapp endpoints with login disabled"""
122+
prefs['bool_webapp_require_login'] = False
123+
return test_client
124+
125+
105126
# plex server fixtures
106127
@pytest.fixture(scope="session")
107128
def plugin_logs():
@@ -111,7 +132,6 @@ def plugin_logs():
111132
yield plugin_logs
112133

113134

114-
# plex server fixtures
115135
@pytest.fixture(scope="session")
116136
def plugin_log_file():
117137
# the primary plugin log file

tests/functional/test_webapp.py

Lines changed: 52 additions & 3 deletions
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

Lines changed: 17 additions & 0 deletions
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 plex_api_helper.is_server_owner(user={}) is None

0 commit comments

Comments
 (0)