File: test_functional.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 (114 lines) | stat: -rw-r--r-- 3,866 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
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Functional, browser based tests for samba app.
"""

import random
import string
import subprocess
import urllib

import pytest

from plinth.tests import functional

pytestmark = [pytest.mark.apps, pytest.mark.samba]


class TestSambaApp(functional.BaseAppTests):
    app_name = 'samba'
    has_service = True
    has_web = False

    @pytest.fixture(scope='class', autouse=True)
    def fixture_setup(self, session_browser):
        """Setup the app."""
        functional.login(session_browser)
        functional.networks_set_firewall_zone(session_browser, 'internal')

    @pytest.mark.parametrize('share_type', ['open', 'group', 'home'])
    def test_enable_disable_samba_share(self, session_browser, share_type):
        """Test enabling and disabling Samba share."""
        _set_share(session_browser, share_type, status='enabled')

        _assert_share_is_writable(share_type)
        if share_type == 'open':
            _assert_share_is_writable(share_type, as_guest=True)
        else:
            _assert_share_is_not_accessible(share_type, as_guest=True)

        _set_share(session_browser, share_type, status='disabled')
        _assert_share_is_not_available(share_type)


def _set_share(browser, share_type, status='enabled'):
    """Enable or disable samba share."""
    disk_name = 'disk'
    share_row_id = 'samba-share-{0}-{1}'.format(disk_name, share_type)

    functional.nav_to_module(browser, 'samba')
    share = browser.find_by_id(share_row_id)
    share_btn = share.find_by_css('.share-status').find_by_tag('button').first

    if status == 'enabled' and share_btn['value'] == 'enable':
        functional.click_and_wait(browser, share_btn)
    elif status == 'disabled' and share_btn['value'] == 'disable':
        functional.click_and_wait(browser, share_btn)


def _write_to_share(share_type, as_guest=False):
    """Write to the samba share, return output messages as string."""
    disk_name = 'disk'
    default_url = functional.config['DEFAULT']['url']
    if share_type == 'open':
        share_name = disk_name
    else:
        share_name = '{0}_{1}'.format(disk_name, share_type)
    hostname = urllib.parse.urlparse(default_url).hostname
    servicename = '\\\\{0}\\{1}'.format(hostname, share_name)
    directory = '_plinth-test_{0}'.format(''.join(
        random.SystemRandom().choices(string.ascii_letters, k=8)))
    port = functional.config['DEFAULT']['samba_port']

    smb_command = ['smbclient', '-W', 'WORKGROUP', '-p', port]
    if as_guest:
        smb_command += ['-N']
    else:
        smb_command += [
            '-U', '{0}%{1}'.format(functional.config['DEFAULT']['username'],
                                   functional.config['DEFAULT']['password'])
        ]
    smb_command += [
        servicename, '-c', 'mkdir {0}; rmdir {0}'.format(directory)
    ]

    return subprocess.check_output(smb_command).decode()


def _assert_share_is_writable(share_type, as_guest=False):
    """Assert that samba share is writable."""
    output = _write_to_share(share_type, as_guest)

    assert not output, output


def _assert_share_is_not_accessible(share_type, as_guest=False):
    """Assert that samba share is not accessible."""
    try:
        _write_to_share(share_type, as_guest)
    except subprocess.CalledProcessError as err:
        err_output = err.output.decode()
        assert 'NT_STATUS_ACCESS_DENIED' in err_output, err_output
    else:
        assert False, 'Can access the share.'


def _assert_share_is_not_available(share_type):
    """Assert that samba share is not available."""
    try:
        _write_to_share(share_type)
    except subprocess.CalledProcessError as err:
        err_output = err.output.decode()
        assert 'NT_STATUS_BAD_NETWORK_NAME' in err_output, err_output
    else:
        assert False, 'Can access the share.'