File: test_ssh_remotes.py

package info (click to toggle)
freedombox 26.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,092 kB
  • sloc: python: 48,542; javascript: 1,730; xml: 481; makefile: 290; sh: 137; php: 32
file content (121 lines) | stat: -rw-r--r-- 3,805 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Tests for SSH remotes for backups.
"""

import datetime
import os
import pwd
import subprocess

import pytest
from django.forms import ValidationError

from plinth.utils import generate_password, random_string

from .. import forms

pytestmark = [
    pytest.mark.usefixtures('needs_root', 'load_cfg', 'has_ssh_key'),
    pytest.mark.django_db
]


@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()
    dir_name = os.path.join('/tmp', random_string())
    yield dir_name
    os.path.exists(dir_name) and subprocess.check_call(['rm', '-rf', dir_name])


@pytest.fixture(name='password', scope='module')
def fixture_password():
    return generate_password()


def get_hashed_password(password):
    res = subprocess.run(['mkpasswd', '--method=md5', password],
                         stdout=subprocess.PIPE, check=True)
    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', '-e',
        tomorrow.strftime('%Y-%m-%d'), '-p', hashed_password, username
    ])
    yield username
    subprocess.check_call(['sudo', 'pkill', '-u', username])
    subprocess.check_call(['sudo', 'userdel', username])


@pytest.fixture(name='has_ssh_key', scope='module', autouse=True)
def fixture_ssh_key(temp_home, temp_user, password, needs_root, needs_sudo):
    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
def test_user_setup(temp_home, temp_user):
    assert os.path.isdir(temp_home)
    assert pwd.getpwnam(temp_user)


@pytest.mark.skip
def test_add_repository_when_directory_is_missing(temp_user, temp_home,
                                                  password):
    remote_path = os.path.join(temp_home, 'non_existent_dir')
    data = {
        'repository': f'{temp_user}@localhost:{remote_path}',
        'ssh_password': password,
        'encryption': 'none'
    }
    # TODO test the view instead of the form
    form = forms.AddRemoteRepositoryForm(data=data)
    form.is_valid()
    assert os.path.isdir(remote_path)  # Directory gets created


@pytest.mark.skip
def test_add_repository_when_directory_exists_and_empty(
        temp_user, temp_home, password):
    remote_path = os.path.join(temp_home, 'empty_dir')
    os.makedirs(remote_path)
    data = {
        'repository': f'{temp_user}@localhost:{remote_path}',
        'ssh_password': password,
        'encryption': 'none'
    }
    # TODO test the view instead of the form
    form = forms.AddRemoteRepositoryForm(data=data)
    form.is_valid()


# TODO Fails only in unit test but not if manually tested!
@pytest.mark.skip
def test_add_repository_when_directory_exists_and_not_empty(
        temp_user, temp_home, password):
    remote_path = os.path.join(temp_home, 'non_empty_dir')
    os.makedirs(remote_path)
    open(os.path.join(remote_path, 'somefile.txt'), 'w',
         encoding='utf-8').close()
    data = {
        'repository': f'{temp_user}@localhost:{remote_path}',
        'ssh_password': password,
        'encryption': 'none'
    }
    form = forms.AddRemoteRepositoryForm(data=data)
    with pytest.raises(ValidationError):
        form.is_valid()