From 7cc34199352229c94ef652ee4bb03a236fba7c8d Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Fri, 10 Sep 2021 14:58:22 -0400 Subject: [PATCH] backups: Convert functional tests to non-BDD python format Signed-off-by: James Valleroy [sunil: Add markers] Signed-off-by: Sunil Mohan Adapa Reviewed-by: Sunil Mohan Adapa --- plinth/modules/backups/tests/backups.feature | 36 --- .../modules/backups/tests/test_functional.py | 218 ++++++++++-------- 2 files changed, 117 insertions(+), 137 deletions(-) delete mode 100644 plinth/modules/backups/tests/backups.feature diff --git a/plinth/modules/backups/tests/backups.feature b/plinth/modules/backups/tests/backups.feature deleted file mode 100644 index d3695483f..000000000 --- a/plinth/modules/backups/tests/backups.feature +++ /dev/null @@ -1,36 +0,0 @@ -# SPDX-License-Identifier: AGPL-3.0-or-later - -@backups @system -Feature: Backups module - Create and restore backups. - -Background: - Given I'm a logged in user - Given the bind application is installed - -Scenario: Browser waits for redirect after restoring a backup - Given the bind application is enabled - When I create a backup of the bind app data with name test_backups - And I restore the bind app data backup with name test_backups - And I open the main page - And I wait for 5 seconds - Then the main page should be shown - -Scenario: Download, upload and restore a backup - Given the bind application is enabled - When I set bind forwarders to 1.1.1.1 - And I create a backup of the bind app data with name test_backups - And I set bind forwarders to 1.0.0.1 - And I download the app data backup with name test_backups - And I restore the downloaded app data backup - Then bind forwarders should be 1.1.1.1 - -Scenario: Set a schedule for a repository - Given the backup schedule is set to disable for 1 daily, 2 weekly and 3 monthly at 2:00 without app names - When I set the backup schedule to enable for 10 daily, 20 weekly and 30 monthly at 15:00 without app firewall - Then the schedule should be set to enable for 10 daily, 20 weekly and 30 monthly at 15:00 without app firewall - -Scenario: Disable schedule for a repository - Given the backup schedule is set to enable for 10 daily, 20 weekly and 30 monthly at 15:00 without app firewall - When I set the backup schedule to disable for 1 daily, 2 weekly and 3 monthly at 2:00 without app names - Then the schedule should be set to disable for 1 daily, 2 weekly and 3 monthly at 2:00 without app names diff --git a/plinth/modules/backups/tests/test_functional.py b/plinth/modules/backups/tests/test_functional.py index fd79a2c73..8f4d29840 100644 --- a/plinth/modules/backups/tests/test_functional.py +++ b/plinth/modules/backups/tests/test_functional.py @@ -5,42 +5,77 @@ Functional, browser based tests for backups app. import os import tempfile +import time import urllib.parse +import pytest import requests -from pytest import fixture -from pytest_bdd import given, parsers, scenarios, then, when - from plinth.tests import functional -scenarios('backups.feature') +pytestmark = [pytest.mark.system, pytest.mark.backups] -@fixture(scope='session') +@pytest.fixture(scope='module', autouse=True) +def fixture_background(session_browser): + """Login and install the app.""" + functional.login(session_browser) + functional.install(session_browser, 'bind') + functional.app_enable(session_browser, 'bind') + yield + functional.app_disable(session_browser, 'bind') + _backup_schedule_disable(session_browser) + + +@pytest.fixture(scope='session') def downloaded_file_info(): return dict() -@when(parsers.parse('I open the main page')) -def open_main_page(session_browser): +def test_browser_waits_after_restore(session_browser): + """Test that browser waits for redirect after restoring a backup.""" + functional.backup_create(session_browser, 'bind', 'test_backups') + functional.backup_restore(session_browser, 'bind', 'test_backups') + _open_main_page(session_browser) + time.sleep(5) + _assert_main_page_is_shown(session_browser) -@then(parsers.parse('the main page should be shown')) -def main_page_is_shown(session_browser): +def test_download_upload_restore(session_browser, downloaded_file_info): + """Test download, upload, and restore a backup.""" + functional.set_forwarders(session_browser, '1.1.1.1') + functional.backup_create(session_browser, 'bind', 'test_backups') + + functional.set_forwarders(session_browser, '1.0.0.1') + _backup_download(session_browser, downloaded_file_info, 'test_backups') + _backup_restore_from_upload(session_browser, 'bind', downloaded_file_info) + + assert functional.get_forwarders(session_browser) == '1.1.1.1' + + +def test_set_schedule(session_browser): + """Test set a schedule for a repository.""" + _backup_schedule_set(session_browser, enable=False, daily=1, weekly=2, + monthly=3, run_at=2, without_app='names') + + _backup_schedule_set(session_browser, enable=True, daily=10, weekly=20, + monthly=30, run_at=15, without_app='firewall') + + _backup_schedule_assert(session_browser, enable=True, daily=10, weekly=20, + monthly=30, run_at=15, without_app='firewall') + + +def _assert_main_page_is_shown(session_browser): assert (session_browser.url.endswith('/plinth/')) -@when( - parsers.parse('I download the app data backup with name {archive_name:w}')) -def backup_download(session_browser, downloaded_file_info, archive_name): +def _backup_download(session_browser, downloaded_file_info, archive_name): file_path = _download(session_browser, archive_name) downloaded_file_info['path'] = file_path -@when(parsers.parse('I restore the downloaded app data backup')) -def backup_restore_from_upload(session_browser, app_name, - downloaded_file_info): +def _backup_restore_from_upload(session_browser, app_name, + downloaded_file_info): path = downloaded_file_info["path"] try: _upload_and_restore(session_browser, app_name, path) @@ -50,34 +85,10 @@ def backup_restore_from_upload(session_browser, app_name, os.remove(path) -@given( - parsers.parse('the backup schedule is set to {enable:w} for {daily:d} ' - 'daily, {weekly:d} weekly and {monthly:d} monthly at ' - '{run_at:d}:00 without app {without_app:w}')) -def backup_schedule_set(session_browser, enable, daily, weekly, monthly, - run_at, without_app): - _backup_schedule_set(session_browser, enable == 'enable', daily, weekly, - monthly, run_at, without_app) - - -@when( - parsers.parse('I set the backup schedule to {enable:w} for {daily:d} ' - 'daily, {weekly:d} weekly and {monthly:d} monthly at ' - '{run_at:d}:00 without app {without_app:w}')) -def backup_schedule_set2(session_browser, enable, daily, weekly, monthly, - run_at, without_app): - _backup_schedule_set(session_browser, enable == 'enable', daily, weekly, - monthly, run_at, without_app) - - -@then( - parsers.parse('the schedule should be set to {enable:w} for {daily:d} ' - 'daily, {weekly:d} weekly and {monthly:d} monthly at ' - '{run_at:d}:00 without app {without_app:w}')) -def backup_schedule_assert(session_browser, enable, daily, weekly, monthly, - run_at, without_app): +def _backup_schedule_assert(session_browser, enable, daily, weekly, monthly, + run_at, without_app): schedule = _backup_schedule_get(session_browser) - assert schedule['enable'] == (enable == 'enable') + assert schedule['enable'] == enable assert schedule['daily'] == daily assert schedule['weekly'] == weekly assert schedule['monthly'] == monthly @@ -86,65 +97,9 @@ def backup_schedule_assert(session_browser, enable, daily, weekly, monthly, assert schedule['without_apps'][0] == without_app -def _open_main_page(browser): - with functional.wait_for_page_update(browser): - browser.find_link_by_href('/plinth/').first.click() - - -def _download_file_logged_in(browser, url, suffix=''): - """Download a file from Plinth, pretend being logged in via cookies""" - if not url.startswith("http"): - current_url = urllib.parse.urlparse(browser.url) - url = "%s://%s%s" % (current_url.scheme, current_url.netloc, url) - cookies = browser.driver.get_cookies() - cookies = {cookie["name"]: cookie["value"] for cookie in cookies} - response = requests.get(url, verify=False, cookies=cookies) - with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file: - for chunk in response.iter_content(chunk_size=128): - temp_file.write(chunk) - return temp_file.name - - -def _download(browser, archive_name=None): - functional.nav_to_module(browser, 'backups') - href = f'/plinth/sys/backups/root/download/{archive_name}/' - url = functional.base_url + href - file_path = _download_file_logged_in(browser, url, suffix='.tar.gz') - return file_path - - -def _upload_and_restore(browser, app_name, downloaded_file_path): - functional.nav_to_module(browser, 'backups') - browser.find_link_by_href('/plinth/sys/backups/upload/').first.click() - fileinput = browser.driver.find_element_by_id('id_backups-file') - fileinput.send_keys(downloaded_file_path) - # submit upload form - functional.submit(browser) - # submit restore form - with functional.wait_for_page_update(browser, - expected_url='/plinth/sys/backups/'): - functional.submit(browser) - - -def _backup_schedule_set(browser, enable, daily, weekly, monthly, run_at, - without_app): - """Set the schedule for root repository.""" - functional.nav_to_module(browser, 'backups') - browser.find_link_by_href( - '/plinth/sys/backups/root/schedule/').first.click() - if enable: - browser.find_by_name('backups_schedule-enabled').check() - else: - browser.find_by_name('backups_schedule-enabled').uncheck() - - browser.fill('backups_schedule-daily_to_keep', daily) - browser.fill('backups_schedule-weekly_to_keep', weekly) - browser.fill('backups_schedule-monthly_to_keep', monthly) - browser.fill('backups_schedule-run_at_hour', run_at) - functional.eventually(browser.find_by_css, args=['.select-all']) - browser.find_by_css('.select-all').first.check() - browser.find_by_css(f'input[value="{without_app}"]').first.uncheck() - functional.submit(browser) +def _backup_schedule_disable(session_browser): + """Disable schedule for the root repository.""" + _backup_schedule_set(session_browser, False, 1, 2, 3, 2, 'names') def _backup_schedule_get(browser): @@ -174,3 +129,64 @@ def _backup_schedule_get(browser): 'without_apps': without_apps } + + +def _backup_schedule_set(browser, enable, daily, weekly, monthly, run_at, + without_app): + """Set the schedule for root repository.""" + functional.nav_to_module(browser, 'backups') + browser.find_link_by_href( + '/plinth/sys/backups/root/schedule/').first.click() + if enable: + browser.find_by_name('backups_schedule-enabled').check() + else: + browser.find_by_name('backups_schedule-enabled').uncheck() + + browser.fill('backups_schedule-daily_to_keep', daily) + browser.fill('backups_schedule-weekly_to_keep', weekly) + browser.fill('backups_schedule-monthly_to_keep', monthly) + browser.fill('backups_schedule-run_at_hour', run_at) + functional.eventually(browser.find_by_css, args=['.select-all']) + browser.find_by_css('.select-all').first.check() + browser.find_by_css(f'input[value="{without_app}"]').first.uncheck() + functional.submit(browser) + + +def _download_file_logged_in(browser, url, suffix=''): + """Download a file from Plinth, pretend being logged in via cookies""" + if not url.startswith("http"): + current_url = urllib.parse.urlparse(browser.url) + url = "%s://%s%s" % (current_url.scheme, current_url.netloc, url) + cookies = browser.driver.get_cookies() + cookies = {cookie["name"]: cookie["value"] for cookie in cookies} + response = requests.get(url, verify=False, cookies=cookies) + with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file: + for chunk in response.iter_content(chunk_size=128): + temp_file.write(chunk) + return temp_file.name + + +def _download(browser, archive_name=None): + functional.nav_to_module(browser, 'backups') + href = f'/plinth/sys/backups/root/download/{archive_name}/' + url = functional.base_url + href + file_path = _download_file_logged_in(browser, url, suffix='.tar.gz') + return file_path + + +def _open_main_page(browser): + with functional.wait_for_page_update(browser): + browser.find_link_by_href('/plinth/').first.click() + + +def _upload_and_restore(browser, app_name, downloaded_file_path): + functional.nav_to_module(browser, 'backups') + browser.find_link_by_href('/plinth/sys/backups/upload/').first.click() + fileinput = browser.driver.find_element_by_id('id_backups-file') + fileinput.send_keys(downloaded_file_path) + # submit upload form + functional.submit(browser) + # submit restore form + with functional.wait_for_page_update(browser, + expected_url='/plinth/sys/backups/'): + functional.submit(browser)