frontpage: Read from .d files too

Read from following paths:

/etc/freedombox/custom-shortcuts.json
/etc/freedombox/custom-shortcuts.json.d/*.json
/etc/plinth/custom-shortcuts.json
/etc/plinth/custom-shortcuts.json.d/*.json
/var/lib/freedombox/custom-shortcuts.json
/var/lib/freedombox/custom-shortcuts.json.d/*.json
/usr/share/freedombox/custom-shortcuts.json
/usr/share/freedombox/custom-shortcuts.json.d/*.json

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-06-24 00:30:40 -07:00 committed by James Valleroy
parent 4ea2e755db
commit 3dfceda785
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
9 changed files with 96 additions and 73 deletions

View File

@ -158,16 +158,21 @@ def _extract_web_app_url(custom_shortcut):
return None
def get_custom_shortcuts_paths():
"""Return the list of custom shortcut file paths."""
file_paths = [
'/etc/freedombox/custom-shortcuts.json',
'/etc/plinth/custom-shortcuts.json',
'/var/lib/freedombox/custom-shortcuts.json',
'/usr/share/freedombox/custom-shortcuts.json',
]
return cfg.expand_to_dot_d_paths(file_paths)
def get_custom_shortcuts():
"""Return a merged dictionary of all custom shortcuts."""
dirs = [cfg.config_dir, cfg.data_dir, cfg.file_root]
file_paths = [
pathlib.Path(dir_) / 'custom-shortcuts.json' for dir_ in dirs
]
file_paths = cfg.expand_to_dot_d_paths(file_paths)
shortcuts = {'shortcuts': []}
for file_path in file_paths:
for file_path in get_custom_shortcuts_paths():
file_path = pathlib.Path(file_path)
if not file_path.is_file() or not file_path.stat().st_size:
continue

View File

@ -3,40 +3,21 @@
pytest configuration for all tests in the plinth/tests/ directory.
"""
import json
import pathlib
from unittest.mock import patch
import pytest
NEXTCLOUD_SHORTCUT = {
'name':
'NextCloud',
'short_description':
'File Hosting Service',
'description': [
'Nextcloud is a suite of client-server software for creating '
'and using file hosting services.'
],
'icon_url':
'/plinth/custom/static/themes/default/icons/nextcloud.png',
'clients': [{
'name': 'nextcloud',
'platforms': [{
'type': 'web',
'url': '/nextcloud'
}]
}]
}
from plinth import cfg
@pytest.fixture(name='custom_shortcuts_file')
def fixture_custom_shortcuts_file(load_cfg, tmp_path):
"""Fixture to set path for a custom shortcuts file."""
load_cfg.config_dir = str(tmp_path)
return tmp_path / 'custom-shortcuts.json'
@pytest.fixture(name='shortcuts_file')
def fixture_shortcuts_file():
with patch('plinth.frontpage.get_custom_shortcuts_paths') as func:
def setter(file_name):
path = pathlib.Path(__file__).parent / 'data' / 'shortcuts'
path /= file_name
func.return_value = cfg.expand_to_dot_d_paths([path])
@pytest.fixture(name='nextcloud_shortcut')
def fixture_nextcloud_shortcut(custom_shortcuts_file):
"""Create a custom_shortcuts file with NextCloud shortcut."""
shortcuts = {'shortcuts': [NEXTCLOUD_SHORTCUT]}
custom_shortcuts_file.write_text(json.dumps(shortcuts))
yield setter

View File

View File

@ -0,0 +1,13 @@
{"shortcuts": [{
"name": "NextCloud",
"short_description": "File Hosting Service",
"description": [ "Nextcloud is a suite of client-server software for creating and using file hosting services." ],
"icon_url": "/plinth/custom/static/themes/default/icons/nextcloud.png",
"clients": [{
"name": "nextcloud",
"platforms": [{
"type": "web",
"url": "/nextcloud"
}]
}]
}]}

View File

@ -0,0 +1,13 @@
{"shortcuts": [{
"name": "NextCloud2",
"short_description": "File Hosting Service",
"description": [ "Nextcloud is a suite of client-server software for creating and using file hosting services." ],
"icon_url": "/plinth/custom/static/themes/default/icons/nextcloud.png",
"clients": [{
"name": "nextcloud",
"platforms": [{
"type": "web",
"url": "/nextcloud"
}]
}]
}]}

View File

@ -0,0 +1 @@
{'shortcuts': []}

View File

@ -0,0 +1,13 @@
{"shortcuts": [{
"name": "NextCloud",
"short_description": "File Hosting Service",
"description": [ "Nextcloud is a suite of client-server software for creating and using file hosting services." ],
"icon_url": "/plinth/custom/static/themes/default/icons/nextcloud.png",
"clients": [{
"name": "nextcloud",
"platforms": [{
"type": "web",
"url": "/nextcloud"
}]
}]
}]}

View File

@ -4,62 +4,59 @@ Test module for custom shortcuts.
"""
import json
import pytest
import pathlib
from plinth.modules.api.views import get_shortcuts_as_json
from .conftest import NEXTCLOUD_SHORTCUT
@pytest.fixture(name='no_custom_shortcuts_file')
def fixture_no_custom_shortcuts_file(custom_shortcuts_file):
"""Delete the custom_shortcuts file."""
if custom_shortcuts_file.exists():
custom_shortcuts_file.unlink()
@pytest.fixture(name='blank_custom_shortcuts_file')
def fixture_blank_custom_shortcuts_file(custom_shortcuts_file):
"""Create a blank shortcuts file."""
custom_shortcuts_file.write_text('')
@pytest.fixture(name='empty_custom_shortcuts')
def fixture_empty_custom_shortcuts(custom_shortcuts_file):
"""Create a custom_shortcuts file with an empty list of shortcuts."""
custom_shortcuts_file.write_text(json.dumps({'shortcuts': []}))
@pytest.mark.usefixtures('no_custom_shortcuts_file')
def test_shortcuts_api_with_no_custom_shortcuts_file():
def test_non_existent_custom_shortcuts_file(shortcuts_file):
"""Test loading a non-existent shortcuts file."""
shortcuts_file('x-non-existant.json')
get_shortcuts_as_json()
@pytest.mark.usefixtures('blank_custom_shortcuts_file')
def test_shortcuts_api_with_blank_custom_shortcuts_file():
def test_blank_custom_shortcuts_file(shortcuts_file):
"""Test loading a shortcuts file that is blank."""
shortcuts_file('blank.json')
get_shortcuts_as_json()
@pytest.mark.usefixtures('empty_custom_shortcuts')
def test_shortcuts_api_with_empty_custom_shortcuts_list():
def test_empty_custom_shortcuts_list(shortcuts_file):
"""Test loading a shortcuts file that has zero shortcuts."""
shortcuts_file('empty.json')
get_shortcuts_as_json()
@pytest.mark.usefixtures('nextcloud_shortcut')
def test_shortcuts_api_with_custom_nextcloud_shortcut():
def test_dotd_shortcuts_files(shortcuts_file):
"""Test loading a shortcuts file that has more files in .d directory."""
shortcuts_file('dotd.json')
shortcuts = get_shortcuts_as_json()
assert len(shortcuts['shortcuts']) >= 2
assert any(shortcut['name'] == 'NextCloud'
for shortcut in shortcuts['shortcuts'])
assert any(shortcut['name'] == 'NextCloud2'
for shortcut in shortcuts['shortcuts'])
def test_custom_nextcloud_shortcut(shortcuts_file):
"""Test loading a shortcuts file that has nextcloud shortcut."""
shortcuts_file('nextcloud.json')
shortcuts = get_shortcuts_as_json()
assert len(shortcuts['shortcuts']) >= 1
assert any(shortcut['name'] == 'NextCloud'
for shortcut in shortcuts['shortcuts'])
@pytest.mark.usefixtures('nextcloud_shortcut')
def test_retrieved_custom_shortcut_from_api_is_correct():
def test_retrieved_custom_shortcut(shortcuts_file):
"""Test the value of loaded nextcloud shortcut."""
shortcuts_file('nextcloud.json')
shortcuts = get_shortcuts_as_json()
shortcut = [
current_shortcut for current_shortcut in shortcuts['shortcuts']
if current_shortcut['name'] == 'NextCloud'
]
assert shortcut
assert shortcut[0] == NEXTCLOUD_SHORTCUT
path = pathlib.Path(__file__).parent / 'data' / 'shortcuts'
path /= 'nextcloud.json'
assert shortcut[0] == json.loads(path.read_bytes())['shortcuts'][0]

View File

@ -138,7 +138,7 @@ def test_shortcut_list_with_username(superuser_run, common_shortcuts):
assert return_list == [cuts[0], cuts[3], cut]
@pytest.mark.usefixtures('nextcloud_shortcut')
def test_add_custom_shortcuts():
def test_add_custom_shortcuts(shortcuts_file):
"""Test that adding custom shortcuts succeeds."""
shortcuts_file('nextcloud.json')
add_custom_shortcuts()