From 3dfceda78587b3fd9bb0d11991c9e22f4f76895b Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 24 Jun 2020 00:30:40 -0700 Subject: [PATCH] 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 Reviewed-by: James Valleroy --- plinth/frontpage.py | 19 ++++-- plinth/tests/conftest.py | 41 ++++-------- plinth/tests/data/shortcuts/blank.json | 0 plinth/tests/data/shortcuts/dotd.json | 13 ++++ .../data/shortcuts/dotd.json.d/01_extra.json | 13 ++++ plinth/tests/data/shortcuts/empty.json | 1 + plinth/tests/data/shortcuts/nextcloud.json | 13 ++++ plinth/tests/test_custom_shortcuts.py | 65 +++++++++---------- plinth/tests/test_frontpage.py | 4 +- 9 files changed, 96 insertions(+), 73 deletions(-) create mode 100644 plinth/tests/data/shortcuts/blank.json create mode 100644 plinth/tests/data/shortcuts/dotd.json create mode 100644 plinth/tests/data/shortcuts/dotd.json.d/01_extra.json create mode 100644 plinth/tests/data/shortcuts/empty.json create mode 100644 plinth/tests/data/shortcuts/nextcloud.json diff --git a/plinth/frontpage.py b/plinth/frontpage.py index ae1f73f46..49d1b02dd 100644 --- a/plinth/frontpage.py +++ b/plinth/frontpage.py @@ -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 diff --git a/plinth/tests/conftest.py b/plinth/tests/conftest.py index 2afad9b4a..ee2bfb5e5 100644 --- a/plinth/tests/conftest.py +++ b/plinth/tests/conftest.py @@ -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 diff --git a/plinth/tests/data/shortcuts/blank.json b/plinth/tests/data/shortcuts/blank.json new file mode 100644 index 000000000..e69de29bb diff --git a/plinth/tests/data/shortcuts/dotd.json b/plinth/tests/data/shortcuts/dotd.json new file mode 100644 index 000000000..8bbe19a17 --- /dev/null +++ b/plinth/tests/data/shortcuts/dotd.json @@ -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" + }] + }] +}]} diff --git a/plinth/tests/data/shortcuts/dotd.json.d/01_extra.json b/plinth/tests/data/shortcuts/dotd.json.d/01_extra.json new file mode 100644 index 000000000..a18eb4686 --- /dev/null +++ b/plinth/tests/data/shortcuts/dotd.json.d/01_extra.json @@ -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" + }] + }] +}]} diff --git a/plinth/tests/data/shortcuts/empty.json b/plinth/tests/data/shortcuts/empty.json new file mode 100644 index 000000000..9f4656aed --- /dev/null +++ b/plinth/tests/data/shortcuts/empty.json @@ -0,0 +1 @@ +{'shortcuts': []} diff --git a/plinth/tests/data/shortcuts/nextcloud.json b/plinth/tests/data/shortcuts/nextcloud.json new file mode 100644 index 000000000..8bbe19a17 --- /dev/null +++ b/plinth/tests/data/shortcuts/nextcloud.json @@ -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" + }] + }] +}]} diff --git a/plinth/tests/test_custom_shortcuts.py b/plinth/tests/test_custom_shortcuts.py index 57a1b7505..cac2dc42c 100644 --- a/plinth/tests/test_custom_shortcuts.py +++ b/plinth/tests/test_custom_shortcuts.py @@ -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] diff --git a/plinth/tests/test_frontpage.py b/plinth/tests/test_frontpage.py index 86cad2072..95f16135d 100644 --- a/plinth/tests/test_frontpage.py +++ b/plinth/tests/test_frontpage.py @@ -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()