ui: Handle and show most page load errors as alerts

- In addition to the OperationalError, also handle all generic exceptions during
page submit and page load. Redirect to the same page or parent using
breadcrumbs.

- Log exceptions handled by common error middleware so that they are also part
of the system logs.

- Update kiwix test as needed.

- Refactor some test code that is setting up the menu items.

Tests:

- When an error occurs during form POST, the same page is show but with an error
message.

- When an error occurs in an app page during GET, the browser is redirected to
the parent section.

- When an error occurs in apps page during GET, the browser is redirected to the
home page.

- When an error occurs in home page during GET, the error is not handled and
default 500 handle is triggered.

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-29 17:16:06 +02:00
1 parent 59a0a3b25f
commit 407fccba2f
7 files changed
+97 -44

No files matched your search

+1 -15
View File
@@ -5,27 +5,13 @@ Test module for custom context processors.
from unittest.mock import MagicMock, Mock, patch
import pytest
from django.urls import resolve
from plinth import context_processors as cp
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, rf):
def test_common(Notification, load_cfg, rf, test_menu):
"""Verify that the common() function returns the correct values."""
url = '/apps/testapp/create/'
request = rf.get(url)
+42 -6
View File
@@ -9,8 +9,9 @@ import pytest
from django.contrib.auth.models import AnonymousUser, Group, User
from django.core.exceptions import PermissionDenied
from django.db.utils import OperationalError
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.test.client import RequestFactory
from django.urls import resolve
from stronghold.decorators import public
from plinth import app as app_module
@@ -144,12 +145,14 @@ class TestSetupMiddleware:
resolve.return_value.namespaces = ['mockapp']
operation_manager.collect_results.return_value = [
Mock(translated_message='message1', exception=None),
Mock(translated_message='message2', exception='x-exception')
Mock(translated_message='message2',
exception=RuntimeError('x-exception'))
]
app.get_setup_state = lambda: app_module.App.SetupState.UP_TO_DATE
# Admin user can collect result
request = RequestFactory().get('/plinth/mockapp')
request.resolver_match = Mock()
user = User(username='adminuser')
user.save()
group = Group(name='admin')
@@ -162,7 +165,9 @@ class TestSetupMiddleware:
assert response is None
messages_success.assert_has_calls([call(request, 'message1')])
messages_error.assert_has_calls([call(request, 'message2')])
messages_error.assert_called_once()
assert messages_error.call_args.args[0] == request
assert messages_error.call_args.args[1].startswith('message2')
operation_manager.collect_results.assert_has_calls([call('mockapp')])
# Non-admin user can't collect result
@@ -265,7 +270,8 @@ class TestCommonErrorMiddleware:
@pytest.fixture(name='web_request')
def fixture_web_request():
"""Fixture for returning web request."""
web_request = RequestFactory().get('/plinth/mockapp')
web_request = RequestFactory().get('/apps/testapp/')
web_request.resolver_match = resolve('/apps/testapp/')
web_request.user = Mock()
return web_request
@@ -288,6 +294,36 @@ class TestCommonErrorMiddleware:
assert 'message' in response.context_data
@staticmethod
def test_other_error(middleware, web_request, other_error):
@patch('django.contrib.messages.error')
def test_other_error_get(messages_error, middleware, web_request,
other_error, test_menu):
response = middleware.process_exception(web_request, other_error)
assert response is None
assert isinstance(response, HttpResponseRedirect)
assert response.url == '/apps/'
messages_error.assert_called_once()
assert messages_error.call_args.args[0] == web_request
assert messages_error.call_args.args[1].startswith(
'Error loading page.')
@staticmethod
@patch('django.contrib.messages.error')
def test_other_error_post(messages_error, middleware, web_request,
other_error, test_menu):
web_request.method = 'POST'
response = middleware.process_exception(web_request, other_error)
assert isinstance(response, HttpResponseRedirect)
assert response.url == '/apps/testapp/'
messages_error.assert_called_once()
assert messages_error.call_args.args[0] == web_request
assert messages_error.call_args.args[1].startswith(
'Error running operation.')
@staticmethod
@patch('django.contrib.messages.error')
def test_other_error_index(messages_error, middleware, web_request,
other_error, test_menu):
web_request.path = '/'
web_request.resolver_match = resolve('/')
response = middleware.process_exception(web_request, other_error)
assert not response
messages_error.assert_not_called()
+1 -14
View File
@@ -6,23 +6,10 @@ Tests for common FreedomBox views.
import pytest
from django.urls import resolve
from plinth import menu as menu_module
from plinth.views import get_breadcrumbs, is_safe_url
@pytest.fixture(name='menu')
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')
def test_get_breadcrumbs(rf, menu):
def test_get_breadcrumbs(rf, test_menu):
"""Test that computing breadcrumbs works."""
def _crumb(name: str, is_active: bool = False, url_name: str | None = None,