FreedomBox/plinth/modules/calibre/tests/test_privileged.py
Sunil Mohan Adapa d7a60b1aca
calibre: Use privileged decorator for actions
Tests:

- Unit and functional tests work.
- Creating a library works.
  - An error while creating library shows as proper message.
- Deleting a library works.
  - An error while deleting library shows as proper messages.
- Creating/deleting library reflects properly in the list of libraries.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2022-10-08 18:51:24 -04:00

65 lines
1.8 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Test module for calibre actions."""
import pathlib
from unittest.mock import call, patch
import pytest
from plinth.modules.calibre import privileged
pytestmark = pytest.mark.usefixtures('mock_privileged')
privileged_modules_to_mock = ['plinth.modules.calibre.privileged']
@pytest.fixture(autouse=True)
def fixture_libraries_path(tmpdir):
"""Set the libraries path in the actions module."""
privileged.LIBRARIES_PATH = pathlib.Path(str(tmpdir))
@pytest.fixture(autouse=True)
def fixture_patch():
"""Patch some underlying methods."""
def side_effect(*args, **_kwargs):
if args[0][0] != 'calibredb':
return
path = pathlib.Path(args[0][2]) / 'metadata.db'
path.touch()
with patch('subprocess.call') as subprocess_call, \
patch('shutil.chown'):
subprocess_call.side_effect = side_effect
yield
def test_list_libraries():
"""Test listing libraries."""
assert privileged.list_libraries() == []
privileged.create_library('TestLibrary')
assert privileged.list_libraries() == ['TestLibrary']
@patch('shutil.chown')
def test_create_library(chown):
"""Test creating a library."""
privileged.create_library('TestLibrary')
library = privileged.LIBRARIES_PATH / 'TestLibrary'
assert (library / 'metadata.db').exists()
assert library.stat().st_mode == 0o40755
assert privileged.list_libraries() == ['TestLibrary']
chown.assert_has_calls([call(library.parent.parent, 'root', 'root')])
def test_delete_library():
"""Test deleting a library."""
privileged.create_library('TestLibrary')
privileged.delete_library('TestLibrary')
assert privileged.list_libraries() == []
with pytest.raises(FileNotFoundError):
privileged.delete_library('TestLibrary')