samba: Add unit and functional tests

- Add functional tests
- Add unit tests for samba views
- New dependency for running functional tests: smbclient
- Make port configurable for the smbclient

Signed-off-by: Veiko Aasa <veiko17@disroot.org>
Reviewed-by: Joseph Nuthalapati <njoseph@riseup.net>
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
This commit is contained in:
Veiko Aasa authored and Joseph Nuthalapati committed 2020-01-17 02:47:06 +05:30
1 parent aa69585cb6
commit 3a202af843
11 files changed
+352 -3

No files matched your search

+1
View File
@@ -3,3 +3,4 @@ url = https://localhost:4430
username = tester
password = testingtesting
delete_root_backup_archives = true
samba_port = 4450
+33 -1
View File
@@ -15,12 +15,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
@apps @samba
@apps @samba @backups
Feature: Samba File Sharing
Configure samba file sharing service.
Background:
Given I'm a logged in user
Given the network device is in the internal firewall zone
Given the samba application is installed
Scenario: Enable samba application
@@ -33,3 +34,34 @@ Scenario: Disable samba application
When I disable the samba application
Then the samba service should not be running
Scenario: Enable open samba share
Given the samba application is enabled
When I enable the open samba share
Then I can write to the open samba share
And a guest user can write to the open samba share
Scenario: Enable group samba share
Given the samba application is enabled
When I enable the group samba share
Then I can write to the group samba share
And a guest user can't access the group samba share
Scenario: Enable home samba share
Given the samba application is enabled
When I enable the home samba share
Then I can write to the home samba share
And a guest user can't access the home samba share
Scenario: Disable open samba share
Given the samba application is enabled
When I disable the open samba share
Then the open samba share should not be available
Scenario: Backup and restore samba
Given the samba application is enabled
When I enable the home samba share
And I create a backup of the samba app data
And I disable the home samba share
And I restore the samba app data backup
Then the samba service should be running
And I can write to the home samba share
+1 -1
View File
@@ -5,7 +5,7 @@ IFS=$'\n\t'
echo "Installing requirements"
sudo apt-get install -yq --no-install-recommends \
python3-pytest python3-pytest-django \
python3-pip firefox \
python3-pip firefox smbclient\
xvfb
pip3 install wheel
pip3 install splinter pytest-splinter pytest-bdd pytest-xvfb
@@ -602,3 +602,31 @@ def gitweb_repo_privately_writable():
assert application.gitweb_repo_is_writable('Test-repo', with_auth=True)
assert application.gitweb_repo_is_writable('Test-repo', with_auth=True,
url_git_extension=True)
@when(parsers.parse('I {task:w} the {share_type:w} samba share'))
def samba_enable_share(browser, task, share_type):
if task == 'enable':
application.samba_set_share(browser, share_type, status='enabled')
elif task == 'disable':
application.samba_set_share(browser, share_type, status='disabled')
@then(parsers.parse('I can write to the {share_type:w} samba share'))
def samba_share_should_be_writable(share_type):
application.samba_assert_share_is_writable(share_type)
@then(parsers.parse('a guest user can write to the {share_type:w} samba share'))
def samba_share_should_be_writable_to_guest(share_type):
application.samba_assert_share_is_writable(share_type, as_guest=True)
@then(parsers.parse('a guest user can\'t access the {share_type:w} samba share'))
def samba_share_should_not_be_accessible_to_guest(share_type):
application.samba_assert_share_is_not_accessible(share_type, as_guest=True)
@then(parsers.parse('the {share_type:w} samba share should not be available'))
def samba_share_should_not_be_available(share_type):
application.samba_assert_share_is_not_available(share_type)
@@ -347,3 +347,8 @@ def open_main_page(browser):
@then(parsers.parse('the main page should be shown'))
def main_page_is_shown(browser):
assert (browser.url.endswith('/plinth/'))
@given(parsers.parse('the network device is in the {zone:w} firewall zone'))
def networks_set_firewall_zone(browser, zone):
system.networks_set_firewall_zone(browser, zone)
+2
View File
@@ -24,3 +24,5 @@ config.read(pathlib.Path(__file__).parent.with_name('config.ini'))
config['DEFAULT']['url'] = os.environ.get('FREEDOMBOX_URL',
config['DEFAULT']['url'])
config['DEFAULT']['samba_port'] = os.environ.get('FREEDOMBOX_SAMBA_PORT',
config['DEFAULT']['samba_port'])
+76
View File
@@ -17,9 +17,12 @@
import contextlib
import os
import random
import shutil
import string
import subprocess
import tempfile
import urllib
from time import sleep
import requests
@@ -664,6 +667,79 @@ def openvpn_download_profile(browser):
return _download_file(browser, url)
def samba_set_share(browser, share_type, status='enabled'):
"""Enable or disable samba share."""
disk_name = 'disk'
share_type_name = '{0}_share'.format(share_type)
interface.nav_to_module(browser, 'samba')
for elem in browser.find_by_tag('td'):
if elem.text == disk_name:
share_form = elem.find_by_xpath('(..//*)[2]/form').first
share_btn = share_form.find_by_name(share_type_name).first
if status == 'enabled' and share_btn['value'] == 'enable':
share_btn.click()
elif status == 'disabled' and share_btn['value'] == 'disable':
share_btn.click()
break
def _samba_write_to_share(share_type, as_guest=False):
"""Write to the samba share, return output messages as string."""
disk_name = 'disk'
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 = config['DEFAULT']['samba_port']
smb_command = ['smbclient', '-W', 'WORKGROUP', '-p', port]
if as_guest:
smb_command += ['-N']
else:
smb_command += [
'-U', '{0}%{1}'.format(config['DEFAULT']['username'],
config['DEFAULT']['password'])
]
smb_command += [
servicename, '-c', 'mkdir {0}; rmdir {0}'.format(directory)
]
return subprocess.check_output(smb_command).decode()
def samba_assert_share_is_writable(share_type, as_guest=False):
"""Assert that samba share is writable."""
output = _samba_write_to_share(share_type, as_guest=False)
assert not output, output
def samba_assert_share_is_not_accessible(share_type, as_guest=False):
"""Assert that samba share is not accessible."""
try:
_samba_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 samba_assert_share_is_not_available(share_type):
"""Assert that samba share is not accessible."""
try:
_samba_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.'
def searx_enable_public_access(browser):
"""Enable Public Access in SearX"""
interface.nav_to_module(browser, 'searx')
+13
View File
@@ -417,3 +417,16 @@ def monkeysphere_publish_key(browser, key_type, domain):
def open_main_page(browser):
with wait_for_page_update(browser):
browser.find_link_by_href('/plinth/').first.click()
def networks_set_firewall_zone(browser, zone):
""""Set the network device firewall zone as internal or external."""
nav_to_module(browser, 'networks')
device = browser.find_by_xpath('//span[contains(@class, "label-success") '
'and contains(@class, "connection-status-label")]/following::a').first
network_id = device['href'].split('/')[-3]
device.click()
edit_url = "/plinth/sys/networks/{}/edit/".format(network_id)
browser.find_link_by_href(edit_url).first.click()
browser.select('zone', zone)
browser.find_by_tag("form").first.find_by_tag('input')[-1].click()