context_processors: Use breadcrumbs to highlight current section

- We were using hacky logic of assuming that if a page using the URL
/plinth/sys/..., then it belongs to 'system' section based on the URL match.
This won't work when the URL does not follow this pattern for any reason.

- Instead use the breadcrumbs mechanism which uses menu items and URL names to
determine the section a page belongs to.

Tests:

- Visit page, apps page, system page, help pages, an app page in apps sections,
an app page in system section, backups -> create backup page and notice that the
correct section is highlighted.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa authored and Veiko Aasa committed 2024-12-25 12:24:29 +02:00
1 parent a29fb97dd9
commit 36c4bc30fb
3 files changed
+27 -44

No files matched your search

+13 -31
View File
@@ -6,7 +6,7 @@ Test module for custom context processors.
from unittest.mock import MagicMock, Mock, patch
import pytest
from django.http import HttpRequest
from django.urls import resolve
from plinth import context_processors as cp
from plinth import menu as menu_module
@@ -15,15 +15,21 @@ from plinth import menu as menu_module
@pytest.fixture(name='menu', autouse=True)
def fixture_menu():
"""Initialized menu module."""
menu_module.Menu._all_menus = set()
menu_module.init()
menu_module.Menu('home-id', name='Home', url_name='index')
menu_module.Menu('apps-id', name='Apps', url_name='apps',
parent_url_name='index')
menu_module.Menu('testapp-id', name='Test App', url_name='testapp:index',
parent_url_name='apps')
@patch('plinth.notification.Notification')
def test_common(Notification, load_cfg):
def test_common(Notification, load_cfg, rf):
"""Verify that the common() function returns the correct values."""
request = HttpRequest()
request.path = '/plinth/aaa/bbb/ccc/'
url = '/apps/testapp/create/'
request = rf.get(url)
request.resolver_match = resolve(url)
request.user = Mock()
request.user.groups.filter().exists = Mock(return_value=True)
request.session = MagicMock()
@@ -33,31 +39,7 @@ def test_common(Notification, load_cfg):
config = response['cfg']
assert config is not None
assert config.box_name == 'FreedomBox'
assert response['box_name'] == 'FreedomBox'
urls = response['active_menu_urls']
assert urls is not None
assert ['/plinth/aaa/', '/plinth/aaa/bbb/', '/plinth/aaa/bbb/ccc/'] == urls
assert len(response['breadcrumbs']) == 4
assert response['active_section_url'] == '/apps/'
assert response['user_is_admin']
@patch('plinth.notification.Notification')
def test_common_border_conditions(Notification):
"""Verify that the common() function works for border conditions."""
request = HttpRequest()
request.path = ''
request.user = Mock()
request.user.groups.filter().exists = Mock(return_value=True)
request.session = MagicMock()
response = cp.common(request)
assert response['active_menu_urls'] == []
request.path = '/plinth/'
response = cp.common(request)
assert response['active_menu_urls'] == []
request.path = '/plinth/aaa/bbb/ccc'
response = cp.common(request)
assert response['active_menu_urls'] == ['/plinth/aaa/', '/plinth/aaa/bbb/']