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

Commit

Permalink
testing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Jul 21, 2024
1 parent 1971431 commit c23f6c8
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 58 deletions.
103 changes: 52 additions & 51 deletions Contents/Code/plex_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,58 @@ def get_plex_item(rating_key):
return item


def get_user_info(token):
# type: (str) -> Optional[MyPlexAccount]
"""
Get the Plex user info.
Parameters
----------
token : str
The Plex token.
Returns
-------
Optional[MyPlexAccount]
The Plex user info.
Examples
--------
>>> get_user_info(token='...')
...
"""
try:
return MyPlexAccount(token=token)
except Exception as e:
Log.Error('Error getting user info: {}'.format(e))
return None


def is_server_owner(user):
# type: (MyPlexAccount) -> bool
"""
Check if the user is the owner of the Plex server.
Parameters
----------
user : MyPlexAccount
The Plex user info.
Returns
-------
py:class:`bool`
True if the user is the owner of the Plex server, False otherwise.
Examples
--------
>>> is_server_owner(user=...)
...
"""
plex = setup_plexapi()

return plex.account().username in {user.email, user.username}


def process_queue():
# type: () -> None
"""
Expand Down Expand Up @@ -766,54 +818,3 @@ def scheduled_update():
for item in all_items:
if item.ratingKey not in q.queue:
q.put(item=item.ratingKey)


def get_user_info(token):
# type: (str) -> Optional[MyPlexAccount]
"""
Get the Plex user info.
Parameters
----------
token : str
The Plex token.
Returns
-------
Optional[MyPlexAccount]
The Plex user info.
Examples
--------
>>> get_user_info(token='...')
...
"""
try:
return MyPlexAccount(token=token)
except Exception:
return None


def is_server_owner(user):
# type: (MyPlexAccount) -> bool
"""
Check if the user is the owner of the Plex server.
Parameters
----------
user : MyPlexAccount
The Plex user info.
Returns
-------
py:class:`bool`
True if the user is the owner of the Plex server, False otherwise.
Examples
--------
>>> is_server_owner(user=...)
...
"""
plex = setup_plexapi()

return plex.account().username in {user.email, user.username}
6 changes: 3 additions & 3 deletions Contents/Code/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def render_template(*args, **kwargs):
>>> render_template('home.html', title='Home', items=items)
"""
kwargs['Prefs'] = Prefs
kwargs['is_logged_in'] = is_logged_in
kwargs['is_logged_in'] = is_logged_in()
return flask_render_template(*args, **kwargs)

Check warning on line 66 in Contents/Code/webapp.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/webapp.py#L64-L66

Added lines #L64 - L66 were not covered by tests


Expand Down Expand Up @@ -574,8 +574,8 @@ def is_logged_in():
return False

token = session["token"]
user = get_user_info(token)
logged_in = user and is_server_owner(user)
user = get_user_info(token=token)
logged_in = user and is_server_owner(user=user)
return logged_in

Check warning on line 579 in Contents/Code/webapp.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/webapp.py#L576-L579

Added lines #L576 - L579 were not covered by tests


Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# development environment requirements, these should not be distributed
flake8==3.9.2;python_version<"3"
mock==3.0.5;python_version<"3"
m2r2==0.3.2;python_version<"3"
numpydoc==0.9.2;python_version<"3"
plexhints==2023.1211.160853 # type hinting library for plex development
Expand Down
22 changes: 21 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
import time
from mock import patch

# lib imports
import plexapi
Expand All @@ -21,6 +22,7 @@
sys.path.append(contents_dir)

# local imports
import Code # noqa F401 # import to ensure plexhints gets properly setup
from Code import constants
from Code import Themerr, ThemerrMovies, ThemerrTvShows
from Code import themerr_db_helper
Expand Down Expand Up @@ -87,6 +89,18 @@ def agent(request):
return Themerr()


@pytest.fixture(scope='function')
def prefs():
with patch('plexhints.prefs_kit.Prefs', autospec=True) as mock_prefs:
def set_item(key, value):
print("Setting {} to {}".format(key, value))
mock_prefs[key] = value

mock_prefs.__setitem__.side_effect = set_item

yield mock_prefs


@pytest.fixture(scope='function')
def test_client():
"""Create a test client for testing webapp endpoints"""
Expand All @@ -102,6 +116,13 @@ def test_client():
yield test_client # this is where the testing happens!


@pytest.fixture(scope='function')
def test_client_disabled_login(test_client, prefs):
"""Create a test client for testing webapp endpoints with login disabled"""
prefs['bool_webapp_require_login'] = False
return test_client


# plex server fixtures
@pytest.fixture(scope="session")
def plugin_logs():
Expand All @@ -111,7 +132,6 @@ def plugin_logs():
yield plugin_logs


# plex server fixtures
@pytest.fixture(scope="session")
def plugin_log_file():
# the primary plugin log file
Expand Down
55 changes: 52 additions & 3 deletions tests/functional/test_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def remove_themerr_db_cache_file():
def test_home(test_client):
"""
WHEN the '/' page is requested (GET)
THEN check that the response is valid
THEN check that the response is a redirect
Repeat for '/home'
"""
Expand All @@ -34,9 +34,27 @@ def test_home(test_client):
except AttributeError:
pytest.skip("cannot access Plex token/server")
else:
assert response.status_code == 200
assert response.status_code == 302

response = test_client.get('/home')
assert response.status_code == 302


def test_home_no_login(test_client_disabled_login):
"""
WHEN the '/' page is requested (GET)
THEN check that the response is valid
Repeat for '/home'
"""
try:
response = test_client_disabled_login.get('/')
except AttributeError:
pytest.skip("cannot access Plex token/server")
else:
assert response.status_code == 200

response = test_client_disabled_login.get('/home')
assert response.status_code == 200

assert 'id="section_' in response.data.decode('utf-8')
Expand All @@ -45,12 +63,25 @@ def test_home(test_client):
def test_home_without_cache(remove_themerr_db_cache_file, test_client):
"""
WHEN the '/' page is requested (GET)
THEN check that the response is valid
THEN check that the response is a redirect
"""
try:
response = test_client.get('/')
except AttributeError:
pytest.skip("cannot access Plex token/server")
else:
assert response.status_code == 302


def test_home_without_cache_no_login(remove_themerr_db_cache_file, test_client_disabled_login):
"""
WHEN the '/' page is requested (GET)
THEN check that the response is valid
"""
try:
response = test_client_disabled_login.get('/')
except AttributeError:
pytest.skip("cannot access Plex token/server")
else:
assert response.status_code == 200

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


def test_image_no_login(test_client_disabled_login):
"""
WHEN the '/favicon.ico' file is requested (GET)
THEN check that the response is a redirect
"""
response = test_client_disabled_login.get('favicon.ico')
assert response.status_code == 302


def test_status(test_client):
"""
WHEN the '/status' page is requested (GET)
Expand All @@ -76,3 +116,12 @@ def test_status(test_client):
response = test_client.get('/status')
assert response.status_code == 200
assert response.content_type == 'application/json'


def test_status_no_login(test_client_disabled_login):
"""
WHEN the '/status' page is requested (GET)
THEN check that the response is a redirect
"""
response = test_client_disabled_login.get('/status')
assert response.status_code == 302
17 changes: 17 additions & 0 deletions tests/unit/test_plex_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ def test_change_lock_status(section, lock):
change_status = plex_api_helper.change_lock_status(item, field=field, lock=lock)
assert change_status, 'change_lock_status did not return True'
assert item.isLocked(field=field) == lock, 'Failed to change lock status to {}'.format(lock)


def test_get_user_info(plex):
assert plex_api_helper.get_user_info(token=plex._token)


def test_get_user_info_bad_token():
assert not plex_api_helper.get_user_info(token='bad_token')


def test_is_server_owner(plex):
user = plex_api_helper.get_user_info(token=plex._token)
assert plex_api_helper.is_server_owner(user=user)


def test_is_server_owner_bad_user():
assert plex_api_helper.is_server_owner(user={}) is None

0 comments on commit c23f6c8

Please sign in to comment.