FreedomBox/plinth/tests/test_context_processors.py
Sunil Mohan Adapa 407fccba2f
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>
2024-12-29 17:16:06 +02:00

32 lines
978 B
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Test module for custom context processors.
"""
from unittest.mock import MagicMock, Mock, patch
from django.urls import resolve
from plinth import context_processors as cp
@patch('plinth.notification.Notification')
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)
request.resolver_match = resolve(url)
request.user = Mock()
request.user.groups.filter().exists = Mock(return_value=True)
request.session = MagicMock()
response = cp.common(request)
assert response is not None
config = response['cfg']
assert config is not None
assert config.box_name == 'FreedomBox'
assert response['box_name'] == 'FreedomBox'
assert len(response['breadcrumbs']) == 4
assert response['active_section_url'] == '/apps/'
assert response['user_is_admin']