config: Set home page to FreedomBox for invalid values

- When attempting to set an invalid shortcut ID or invalid user's directory as
home page, set FreedomBox UI as home page.

- Simplify the tests somewhat and avoid failure first time and skipping the test
next time.

Tests:

- Run unit tests as 'root' and 'fbx' users.

- Set home page to apache default, FreedomBox, user home page and a shortcut.
The set value is retained. The change works when visiting / with browser. The
value is as expected in Apache configuration.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2025-09-24 16:22:41 -07:00 committed by Veiko Aasa
parent 647e72516c
commit 5566f05cad
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
2 changed files with 16 additions and 37 deletions

View File

@ -109,23 +109,23 @@ def home_page_url2scid(url: str | None):
def _home_page_scid2url(shortcut_id: str) -> str | None: def _home_page_scid2url(shortcut_id: str) -> str | None:
"""Return the url for the given home page shortcut ID.""" """Return the url for the given home page shortcut ID."""
url: str | None = '/plinth/'
if shortcut_id == 'plinth': if shortcut_id == 'plinth':
url = '/plinth/' pass
elif shortcut_id == 'apache-default': elif shortcut_id == 'apache-default':
url = None url = None
elif shortcut_id.startswith('uws-'): elif shortcut_id.startswith('uws-'):
user = shortcut_id[4:] user = shortcut_id[4:]
if user in get_users_with_website(): if user in get_users_with_website():
url = uws_url_of_user(user) url = uws_url_of_user(user)
else:
url = None
else: else:
shortcuts = frontpage.Shortcut.list() shortcuts = frontpage.Shortcut.list()
aux = [ aux = [
shortcut.url for shortcut in shortcuts shortcut.url for shortcut in shortcuts
if shortcut_id == shortcut.component_id if shortcut_id == shortcut.component_id
] ]
url = aux[0] if 1 == len(aux) else None if 1 == len(aux):
url = aux[0]
return url return url

View File

@ -4,11 +4,13 @@ Tests for config module.
""" """
import os import os
from unittest.mock import MagicMock, patch import pathlib
from unittest.mock import Mock, patch
import pytest import pytest
from plinth import __main__ as plinth_main from plinth import __main__ as plinth_main
from plinth import utils
from plinth.modules.apache import uws_directory_of_user, uws_url_of_user from plinth.modules.apache import uws_directory_of_user, uws_url_of_user
from plinth.modules.config import (_home_page_scid2url, change_home_page, from plinth.modules.config import (_home_page_scid2url, change_home_page,
get_home_page, home_page_url2scid) get_home_page, home_page_url2scid)
@ -61,23 +63,14 @@ def test_homepage_mapping_skip_ci():
# AC: Return None if it doesn't: # AC: Return None if it doesn't:
os.rmdir(uws_directory) os.rmdir(uws_directory)
assert _home_page_scid2url(uws_scid) is None assert _home_page_scid2url(uws_scid) == '/plinth/'
class Dict2Obj: @patch(
"""Mock object made out of any dict.""" 'plinth.frontpage.Shortcut.list',
Mock(return_value=[
def __init__(self, a_dict): Mock(url='url/for/' + id, component_id=id) for id in ('a', 'b')
self.__dict__ = a_dict ]))
@patch('plinth.frontpage.Shortcut.list',
MagicMock(return_value=[
Dict2Obj({
'url': 'url/for/' + id,
'component_id': id
}) for id in ('a', 'b')
]))
@pytest.mark.usefixtures('needs_root') @pytest.mark.usefixtures('needs_root')
def test_homepage_field(): def test_homepage_field():
"""Test homepage changes. """Test homepage changes.
@ -104,23 +97,14 @@ def test_homepage_field():
Currently they share the same test case. Currently they share the same test case.
- Search for another valid user apart from fbx. - Search for another valid user apart from fbx.
""" """
user = 'fbx' user = 'test_' + utils.random_string(size=12)
uws_directory = uws_directory_of_user(user) uws_directory = uws_directory_of_user(user)
uws_url = uws_url_of_user(user) uws_url = uws_url_of_user(user)
uws_scid = home_page_url2scid(uws_url) uws_scid = home_page_url2scid(uws_url)
default_home_page = 'plinth' default_home_page = 'plinth'
original_home_page = get_home_page() or default_home_page original_home_page = get_home_page() or default_home_page
change_home_page(default_home_page) # Set to known value explicitly
# Check test's preconditions:
if original_home_page not in (default_home_page, None):
reason = "Unexpected home page {}.".format(original_home_page)
pytest.skip(reason)
if os.path.exists(uws_directory):
# Don't blindly remove a pre-existing directory. Just skip the test.
reason = "UWS directory {} exists already.".format(uws_directory)
pytest.skip(reason)
# AC: invalid changes fall back to default: # AC: invalid changes fall back to default:
for scid in ('uws-unexisting', uws_scid, 'missing_app'): for scid in ('uws-unexisting', uws_scid, 'missing_app'):
@ -128,12 +112,7 @@ def test_homepage_field():
assert get_home_page() == default_home_page assert get_home_page() == default_home_page
# AC: valid changes actually happen: # AC: valid changes actually happen:
try: pathlib.Path(uws_directory).mkdir(parents=True)
os.mkdir(uws_directory)
except Exception:
reason = "Needs access to ~/ directory. " \
+ "CI sandboxed workspace doesn't provide it."
pytest.skip(reason)
for scid in ('b', 'a', uws_scid, 'apache-default', 'plinth'): for scid in ('b', 'a', uws_scid, 'apache-default', 'plinth'):
change_home_page(scid) change_home_page(scid)
assert get_home_page() == scid assert get_home_page() == scid