mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
custom_shortcuts: Fix issue with writing tests as different user
- Don't write to files in test data directory. Instead write to temporary directory patch the code to work with the temporary directory. This prevents a problem with running tests when the user owning the source directory is not the same as the user running the tests. - Use fixtures. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
parent
6a04500517
commit
9040b26b4e
@ -1 +0,0 @@
|
|||||||
{"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"}]}]}]}
|
|
||||||
@ -19,19 +19,11 @@ Test module for custom shortcuts.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from plinth import cfg
|
|
||||||
from plinth.modules.api.views import get_shortcuts_as_json
|
from plinth.modules.api.views import get_shortcuts_as_json
|
||||||
|
|
||||||
TEST_CONFIG_DIR = \
|
|
||||||
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
|
|
||||||
|
|
||||||
CUSTOM_SHORTCUTS_FILE = os.path.join(TEST_CONFIG_DIR,
|
|
||||||
'etc/plinth/custom-shortcuts.json')
|
|
||||||
|
|
||||||
NEXTCLOUD_SHORTCUT = {
|
NEXTCLOUD_SHORTCUT = {
|
||||||
'name':
|
'name':
|
||||||
'NextCloud',
|
'NextCloud',
|
||||||
@ -53,72 +45,68 @@ NEXTCLOUD_SHORTCUT = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def setup_module(module):
|
@pytest.fixture(name='custom_shortcuts_file')
|
||||||
"""Load test configuration."""
|
def fixture_custom_shortcuts_file(load_cfg, tmp_path):
|
||||||
root = os.path.dirname(os.path.realpath(__file__))
|
"""Fixture to set path for a custom shortcuts file."""
|
||||||
cfg_file = os.path.join(TEST_CONFIG_DIR, 'etc', 'plinth', 'plinth.config')
|
load_cfg.config_file = str(tmp_path / 'plinth.conf')
|
||||||
cfg.read(cfg_file, root)
|
return tmp_path / 'custom-shortcuts.json'
|
||||||
|
|
||||||
|
|
||||||
def teardown_module(module):
|
@pytest.fixture(name='no_custom_shortcuts_file')
|
||||||
"""Reset configuration."""
|
def fixture_no_custom_shortcuts_file(custom_shortcuts_file):
|
||||||
cfg.read()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def no_custom_shortcuts_file():
|
|
||||||
"""Delete the custom_shortcuts file."""
|
"""Delete the custom_shortcuts file."""
|
||||||
if os.path.exists(CUSTOM_SHORTCUTS_FILE):
|
if custom_shortcuts_file.exists():
|
||||||
os.remove(CUSTOM_SHORTCUTS_FILE)
|
custom_shortcuts_file.unlink()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture(name='blank_custom_shortcuts_file')
|
||||||
def blank_custom_shortcuts_file():
|
def fixture_blank_custom_shortcuts_file(custom_shortcuts_file):
|
||||||
"""Create a blank shortcuts file."""
|
"""Create a blank shortcuts file."""
|
||||||
open(CUSTOM_SHORTCUTS_FILE, 'w').close()
|
custom_shortcuts_file.write_text('')
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture(name='empty_custom_shortcuts')
|
||||||
def empty_custom_shortcuts():
|
def fixture_empty_custom_shortcuts(custom_shortcuts_file):
|
||||||
"""Create a custom_shortcuts file with an empty list of shortcuts."""
|
"""Create a custom_shortcuts file with an empty list of shortcuts."""
|
||||||
with open(CUSTOM_SHORTCUTS_FILE, 'w') as shortcuts_file:
|
custom_shortcuts_file.write_text(json.dumps({'shortcuts': []}))
|
||||||
shortcuts = {'shortcuts': []}
|
|
||||||
json.dump(shortcuts, shortcuts_file)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture(name='nextcloud_shortcut')
|
||||||
def nextcloud_shortcut():
|
def fixture_nextcloud_shortcut(custom_shortcuts_file):
|
||||||
with open(CUSTOM_SHORTCUTS_FILE, 'w') as shortcuts_file:
|
"""Create a custom_shortcuts file with NextCloud shortcut."""
|
||||||
shortcuts = {'shortcuts': [NEXTCLOUD_SHORTCUT]}
|
shortcuts = {'shortcuts': [NEXTCLOUD_SHORTCUT]}
|
||||||
json.dump(shortcuts, shortcuts_file)
|
custom_shortcuts_file.write_text(json.dumps(shortcuts))
|
||||||
|
|
||||||
|
|
||||||
def test_shortcuts_api_with_no_custom_shortcuts_file(no_custom_shortcuts_file):
|
@pytest.mark.usefixtures('no_custom_shortcuts_file')
|
||||||
|
def test_shortcuts_api_with_no_custom_shortcuts_file():
|
||||||
get_shortcuts_as_json()
|
get_shortcuts_as_json()
|
||||||
|
|
||||||
|
|
||||||
def test_shortcuts_api_with_blank_custom_shortcuts_file(
|
@pytest.mark.usefixtures('blank_custom_shortcuts_file')
|
||||||
blank_custom_shortcuts_file):
|
def test_shortcuts_api_with_blank_custom_shortcuts_file():
|
||||||
get_shortcuts_as_json()
|
get_shortcuts_as_json()
|
||||||
|
|
||||||
|
|
||||||
def test_shortcuts_api_with_empty_custom_shortcuts_list(
|
@pytest.mark.usefixtures('empty_custom_shortcuts')
|
||||||
empty_custom_shortcuts):
|
def test_shortcuts_api_with_empty_custom_shortcuts_list():
|
||||||
get_shortcuts_as_json()
|
get_shortcuts_as_json()
|
||||||
|
|
||||||
|
|
||||||
def test_shortcuts_api_with_custom_nextcloud_shortcut(nextcloud_shortcut):
|
@pytest.mark.usefixtures('nextcloud_shortcut')
|
||||||
|
def test_shortcuts_api_with_custom_nextcloud_shortcut():
|
||||||
shortcuts = get_shortcuts_as_json()
|
shortcuts = get_shortcuts_as_json()
|
||||||
assert len(shortcuts['shortcuts']) >= 1
|
assert len(shortcuts['shortcuts']) >= 1
|
||||||
assert any(
|
assert any(
|
||||||
shortcut['name'] == 'NextCloud' for shortcut in shortcuts['shortcuts'])
|
shortcut['name'] == 'NextCloud' for shortcut in shortcuts['shortcuts'])
|
||||||
|
|
||||||
|
|
||||||
def test_retrieved_custom_shortcut_from_api_is_correct(nextcloud_shortcut):
|
@pytest.mark.usefixtures('nextcloud_shortcut')
|
||||||
|
def test_retrieved_custom_shortcut_from_api_is_correct():
|
||||||
shortcuts = get_shortcuts_as_json()
|
shortcuts = get_shortcuts_as_json()
|
||||||
nextcloud_shortcut = [
|
shortcut = [
|
||||||
shortcut for shortcut in shortcuts['shortcuts']
|
current_shortcut for current_shortcut in shortcuts['shortcuts']
|
||||||
if shortcut['name'] == 'NextCloud'
|
if current_shortcut['name'] == 'NextCloud'
|
||||||
]
|
]
|
||||||
assert nextcloud_shortcut
|
assert shortcut
|
||||||
assert nextcloud_shortcut[0] == NEXTCLOUD_SHORTCUT
|
assert shortcut[0] == NEXTCLOUD_SHORTCUT
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user