From f85e7829b9ad6eba8abcca6cf7c8a77b2e8c8683 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Thu, 23 May 2019 19:31:53 +0530 Subject: [PATCH] backups: tests: Fix issue with usage of fixture 'needs_root' Fixtures cannot be currently included into other fixtures by using @pytest.mark.fixtures('fixture_name') They have to be included as parameters instead. See bug: https://github.com/pytest-dev/pytest/issues/3664 Also increase the scope of needs_root to the highest, i.e. session, so that it can be used by any kind of fixture. Signed-off-by: Joseph Nuthalapati --- conftest.py | 2 +- .../modules/backups/tests/test_ssh_remotes.py | 90 ++++++++++++------- 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/conftest.py b/conftest.py index f2cf048a5..3d0f57bd8 100644 --- a/conftest.py +++ b/conftest.py @@ -64,7 +64,7 @@ def fixture_develop_mode(load_cfg): load_cfg.develop = False -@pytest.fixture(name='needs_root') +@pytest.fixture(name='needs_root', scope='session') def fixture_needs_root(): """Skip test if not running in root mode.""" if os.geteuid() != 0: diff --git a/plinth/modules/backups/tests/test_ssh_remotes.py b/plinth/modules/backups/tests/test_ssh_remotes.py index 3c0b5818b..44e01e240 100644 --- a/plinth/modules/backups/tests/test_ssh_remotes.py +++ b/plinth/modules/backups/tests/test_ssh_remotes.py @@ -26,77 +26,90 @@ import subprocess import tempfile import pytest +from django.forms import ValidationError +from django.urls import reverse +from plinth.modules.backups import network_storage from plinth.utils import generate_password, random_string -from .. import forms +from .. import forms, views -pytestmark = pytest.mark.usefixtures('needs_root') +pytestmark = [ + pytest.mark.usefixtures('needs_root', 'load_cfg', 'has_ssh_key'), + pytest.mark.django_db +] -@pytest.fixture(name='temp_home', scope='module', autouse=True) -def fixture_temp_home_directory(): +@pytest.fixture(name='temp_home', scope='module') +def fixture_temp_home_directory(needs_root): """Create a new temporary directory to act as a home directory. """ # TODO Try to get this working with tempfile.TemporaryDirectory() - tempfile.TemporaryDirectory() - dir_name = f'/tmp/{random_string()}' - os.mkdir(dir_name) + dir_name = os.path.join('/tmp', random_string()) yield dir_name - subprocess.check_call(['rm', '-rf', dir_name]) + os.path.exists(dir_name) and subprocess.check_call(['rm', '-rf', dir_name]) -@pytest.fixture(name='password', scope='module', autouse=True) +@pytest.fixture(name='password', scope='module') def fixture_password(): return generate_password() -@pytest.fixture(name='temp_user', scope='module', autouse=True) -def fixture_create_temp_user(temp_home, password): +def get_hashed_password(password): + res = subprocess.run(['mkpasswd', '--method=md5', password], + stdout=subprocess.PIPE) + return res.stdout.decode().strip() + + +@pytest.fixture(name='temp_user', scope='module') +def fixture_create_temp_user(temp_home, password, needs_root): """Create a temporary user. """ username = random_string() + hashed_password = get_hashed_password(password) # User account expires tomorrow tomorrow = datetime.date.today() + datetime.timedelta(days=1) subprocess.check_call([ - 'useradd', '-d', temp_home, '-m', '-p', password, username, '-e', - tomorrow.strftime('%Y-%m-%d') + 'useradd', '-d', temp_home, '-m', '-e', + tomorrow.strftime('%Y-%m-%d'), '-p', hashed_password, username ]) - subprocess.check_call(['chown', username, temp_home]) yield username + subprocess.check_call(['pkill', '-u', username]) subprocess.check_call(['userdel', username]) @pytest.mark.usefixtures('needs_sudo') @pytest.fixture(name='has_ssh_key', scope='module', autouse=True) -def fixture_ssh_key(temp_home, temp_user, password): +def fixture_ssh_key(temp_home, temp_user, password, needs_root): subprocess.check_call([ 'sudo', '-n', '-u', temp_user, 'ssh-keygen', '-t', 'rsa', '-b', '1024', '-N', '', '-f', f'{temp_home}/.ssh/id_rsa', '-q' ]) -@pytest.mark.skip -@pytest.mark.usefixtures('has_ssh_key') def test_user_setup(temp_home, temp_user): assert os.path.isdir(temp_home) assert pwd.getpwnam(temp_user) -# Tests -# forms.AddRepositoryForm -# * Create empty directory if not exists -# * Check if the directory is empty -# - if not empty, check if it's an existing backup repository -# - else throw an error - - -@pytest.mark.skip -@pytest.mark.django_db -@pytest.mark.usefixtures('has_ssh_key') -def test_add_repository_when_directory_is_missing(temp_home, temp_user, +def test_add_repository_when_directory_is_missing(temp_user, temp_home, password): - repo_path = os.path.join(temp_home, 'backups') + repo_path = os.path.join(temp_home, 'non_existent_dir') + data = { + 'repository': f'{temp_user}@localhost:{repo_path}', + 'ssh_password': password, + 'encryption': 'none' + } + # TODO test the view instead of the form + form = forms.AddRepositoryForm(data=data) + form.is_valid() + assert os.path.isdir(repo_path) # Directory gets created + + +def test_add_repository_when_directory_exists_and_empty( + temp_user, temp_home, password): + repo_path = os.path.join(temp_home, 'empty_dir') + os.makedirs(repo_path) data = { 'repository': f'{temp_user}@localhost:{repo_path}', 'ssh_password': password, @@ -104,5 +117,18 @@ def test_add_repository_when_directory_is_missing(temp_home, temp_user, } form = forms.AddRepositoryForm(data=data) form.is_valid() - form.clean() - assert os.path.isdir(repo_path) + + +def test_add_repository_when_directory_exists_and_not_empty( + temp_user, temp_home, password): + repo_path = os.path.join(temp_home, 'non_empty_dir') + os.makedirs(repo_path) + open(os.path.join(repo_path, 'somefile.txt'), 'w').close() + data = { + 'repository': f'{temp_user}@localhost:{repo_path}', + 'ssh_password': password, + 'encryption': 'none' + } + form = forms.AddRepositoryForm(data=data) + with pytest.raises(ValidationError): + form.is_valid()