FreedomBox/plinth/tests/test_views.py
Sunil Mohan Adapa 168f662a17
*: Update URL base from /plinth to /freedombox
- Since we are going to be an OpenID Provider, we need to fix the URLs that
other apps will be configured with for authentication. So change now from
/plinth to /freedombox. If done later, it will be harder since all the
configuration files for all dependent apps will need to be updated.

Tests:

- App availability checking works. Request goes to /freedombox URL

- Favicon is served properly and through /favicon.ico URL

- Redirection happens from / to /freedombox directly

- UI is available on /freedombox and on /plinth

- Manual page show /freedombox as the URL in two places

- Static files are successfully served from /freedombox URLs. URLs inside page
start with /freedombox

- backup, bepasty, calibre, config, dynamicdns, ejabberd, featherwiki, gitweb,
ikiwiki, kiwix, miniflux, names, openvpn, shadowsocks, shadowsocksserver,
sharing, shapshot, tiddlywiki, users, wireguard, jsxc, matrixsynapse, first
wizard, storage, samba, tags functional tests work. Backup/restore test for
matrixsynapse fails due to an unrelated bug (server not restarted after
restore).

- Setting the home page works:

  - Having /plinth in the home page configuration works. Shows selection
    correctly.

  - Setting to app works. Shows selection correctly.

  - Setting to user home page (sets /freedombox). Shows selection correctly.

  - Setting to apache default works. Shows selection correctly.

  - Changing back to FreedomBox service works. Shows selection correctly.

- Unit tests work

- Configuration page shows /freedombox in description but not /plinth

- Diagnostics show /freedombox in tests

- Roundcube URL link in email app has /freedombox

- email loads the page /.well-known/autoconfig/mail/config-v1.1.xml correctly

- email app shows /freedombox/apps/roundcube for /roundcube if roundcube is not
installed.

- networks: router configuration page shows URL starting with /freedombox.

- snapshot: Shows URL starting with /freedombox on the app page

- js licenses page uses /freedombox prefix for JSXC.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2026-03-02 20:50:30 -05: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', [
'/freedombox/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,
'\\freedombox',
'///freedombox',
'https://example.com/freedombox/login/',
'https:///example.com',
'https:///freedombox/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)