FreedomBox/plinth/tests/test_views.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

85 lines
2.5 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Tests for common FreedomBox views.
"""
import pytest
from django.urls import resolve
from plinth.views import get_breadcrumbs, is_safe_url
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,
is_active_section: bool = False):
crumb = {'name': name, 'is_active': is_active, 'url_name': url_name}
if is_active_section:
crumb['is_active_section'] = True
return crumb
def _get(path: str):
request = rf.get(path)
request.resolver_match = resolve(path)
return get_breadcrumbs(request)
def _compare(dict1: dict[str, dict[str, str | bool]],
dict2: dict[str, dict[str, str | bool]]):
"""Compare dictionaries with order."""
assert list(dict1.items()) == list(dict2.items())
_compare(_get('/'), {'/': _crumb('Home', True, 'index', True)})
_compare(
_get('/apps/'), {
'/apps/': _crumb('Apps', True, 'apps', True),
'/': _crumb('Home', False, 'index'),
})
_compare(
_get('/apps/testapp/'), {
'/apps/testapp/': _crumb('Test App', True, 'testapp:index'),
'/apps/': _crumb('Apps', False, 'apps', True),
'/': _crumb('Home', False, 'index'),
})
_compare(
_get('/apps/testapp/create/'), {
'/apps/testapp/create/': _crumb('Here', True, 'testapp:create'),
'/apps/testapp/': _crumb('Test App', False, 'testapp:index'),
'/apps/': _crumb('Apps', False, 'apps', True),
'/': _crumb('Home', False, 'index'),
})
_compare(
_get('/test/1/2/3/'), {
'/test/1/2/3/': _crumb('Here', True, 'test', True),
'/': _crumb('Home', False, 'index'),
})
@pytest.mark.parametrize('url', [
'/plinth/login/',
'/',
'safe',
])
def test_is_safe_url_valid_url(url):
"""Test valid URLs for safe URL checks."""
assert is_safe_url(url)
@pytest.mark.parametrize(
'url',
[
'',
None,
'\\plinth',
'///plinth',
'https://example.com/plinth/login/',
'https:///example.com',
'https:///plinth/login',
'ftp://example.com',
'https://[aabb::ccdd', # Invalid IPv6
])
def test_is_safe_url_invalid_url(url):
"""Test invalid URLs for safe URL checks."""
assert not is_safe_url(url)