From 5566f05caddbf56af94a047a809c9cdf0806b33b Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 24 Sep 2025 16:22:41 -0700 Subject: [PATCH] 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 Reviewed-by: Veiko Aasa --- plinth/modules/config/__init__.py | 8 ++-- plinth/modules/config/tests/test_config.py | 45 ++++++---------------- 2 files changed, 16 insertions(+), 37 deletions(-) diff --git a/plinth/modules/config/__init__.py b/plinth/modules/config/__init__.py index 75e1c6b1b..c0b82f459 100644 --- a/plinth/modules/config/__init__.py +++ b/plinth/modules/config/__init__.py @@ -109,23 +109,23 @@ def home_page_url2scid(url: str | None): def _home_page_scid2url(shortcut_id: str) -> str | None: """Return the url for the given home page shortcut ID.""" + url: str | None = '/plinth/' if shortcut_id == 'plinth': - url = '/plinth/' + pass elif shortcut_id == 'apache-default': url = None elif shortcut_id.startswith('uws-'): user = shortcut_id[4:] if user in get_users_with_website(): url = uws_url_of_user(user) - else: - url = None else: shortcuts = frontpage.Shortcut.list() aux = [ shortcut.url for shortcut in shortcuts if shortcut_id == shortcut.component_id ] - url = aux[0] if 1 == len(aux) else None + if 1 == len(aux): + url = aux[0] return url diff --git a/plinth/modules/config/tests/test_config.py b/plinth/modules/config/tests/test_config.py index dd7d192d1..5453fed1a 100644 --- a/plinth/modules/config/tests/test_config.py +++ b/plinth/modules/config/tests/test_config.py @@ -4,11 +4,13 @@ Tests for config module. """ import os -from unittest.mock import MagicMock, patch +import pathlib +from unittest.mock import Mock, patch import pytest 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.config import (_home_page_scid2url, change_home_page, get_home_page, home_page_url2scid) @@ -61,23 +63,14 @@ def test_homepage_mapping_skip_ci(): # AC: Return None if it doesn't: os.rmdir(uws_directory) - assert _home_page_scid2url(uws_scid) is None + assert _home_page_scid2url(uws_scid) == '/plinth/' -class Dict2Obj: - """Mock object made out of any dict.""" - - def __init__(self, a_dict): - 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') - ])) +@patch( + 'plinth.frontpage.Shortcut.list', + Mock(return_value=[ + Mock(url='url/for/' + id, component_id=id) for id in ('a', 'b') + ])) @pytest.mark.usefixtures('needs_root') def test_homepage_field(): """Test homepage changes. @@ -104,23 +97,14 @@ def test_homepage_field(): Currently they share the same test case. - 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_url = uws_url_of_user(user) uws_scid = home_page_url2scid(uws_url) default_home_page = 'plinth' original_home_page = get_home_page() or default_home_page - - # 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) + change_home_page(default_home_page) # Set to known value explicitly # AC: invalid changes fall back to default: for scid in ('uws-unexisting', uws_scid, 'missing_app'): @@ -128,12 +112,7 @@ def test_homepage_field(): assert get_home_page() == default_home_page # AC: valid changes actually happen: - try: - os.mkdir(uws_directory) - except Exception: - reason = "Needs access to ~/ directory. " \ - + "CI sandboxed workspace doesn't provide it." - pytest.skip(reason) + pathlib.Path(uws_directory).mkdir(parents=True) for scid in ('b', 'a', uws_scid, 'apache-default', 'plinth'): change_home_page(scid) assert get_home_page() == scid