From 067fea7d17eb7f12cce481af4d8c337cd26c036c Mon Sep 17 00:00:00 2001 From: Michael Pimmer Date: Sun, 2 Dec 2018 23:45:50 +0000 Subject: [PATCH] Backups: functional tests update; restoring backup bugfix - updated functional tests to work with changed UI - fixed bug when restoring an uploaded file Reviewed-by: James Valleroy --- functional_tests/features/bind.feature | 4 ++-- functional_tests/step_definitions/system.py | 11 ++++------- functional_tests/support/system.py | 17 +++++++++++++---- plinth/modules/backups/__init__.py | 2 +- plinth/modules/backups/repository.py | 4 +++- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/functional_tests/features/bind.feature b/functional_tests/features/bind.feature index a735ef8d5..bf3f15c61 100644 --- a/functional_tests/features/bind.feature +++ b/functional_tests/features/bind.feature @@ -67,6 +67,6 @@ Scenario: Download, upload and restore a backup When I set bind forwarders to 1.1.1.1 And I create a backup of the bind app data And I set bind forwarders to 1.0.0.1 - And I download the bind app data backup - And I restore the downloaded bind app data backup + And I download the latest app data backup + And I restore the downloaded app data backup Then bind forwarders should be 1.1.1.1 diff --git a/functional_tests/step_definitions/system.py b/functional_tests/step_definitions/system.py index cc4405334..c3566c4b6 100644 --- a/functional_tests/step_definitions/system.py +++ b/functional_tests/step_definitions/system.py @@ -183,12 +183,9 @@ def backup_create(browser, app_name): system.backup_create(browser, app_name) -@when(parsers.parse('I download the {app_name:w} app data backup')) -def backup_download(browser, app_name, downloaded_file_info): - url = '/plinth/sys/backups/download/root/_functional_test_%s/' % \ - app_name - file_path = system.download_file_logged_in(browser, url, app_name, - suffix='.tar.gz') +@when(parsers.parse('I download the latest app data backup')) +def backup_download(browser, downloaded_file_info): + file_path = system.download_latest_backup(browser) downloaded_file_info['path'] = file_path @@ -197,7 +194,7 @@ def backup_restore(browser, app_name): system.backup_restore(browser, app_name) -@when(parsers.parse('I restore the downloaded {app_name:w} app data backup')) +@when(parsers.parse('I restore the downloaded app data backup')) def backup_restore_from_upload(browser, app_name, downloaded_file_info): path = downloaded_file_info["path"] try: diff --git a/functional_tests/support/system.py b/functional_tests/support/system.py index 0cb4bbd38..6048c31e5 100644 --- a/functional_tests/support/system.py +++ b/functional_tests/support/system.py @@ -203,7 +203,6 @@ def backup_create(browser, app_name): application.install(browser, 'backups') browser.find_link_by_href('/plinth/sys/backups/create/').first.click() - browser.find_by_id('id_backups-name').fill('_functional_test_' + app_name) for app in browser.find_by_css('input[type=checkbox]'): app.uncheck() @@ -236,10 +235,20 @@ def backup_upload_and_restore(browser, app_name, downloaded_file_path): submit(browser) -def download_file_logged_in(browser, url_path, app_names, suffix=''): +def download_latest_backup(browser): + nav_to_module(browser, 'backups') + path = "//a[starts-with(@href,'/plinth/sys/backups/download/root')]" + ele = browser.driver.find_elements_by_xpath(path)[0] + url = ele.get_attribute('href') + file_path = download_file_logged_in(browser, url, suffix='.tar.gz') + return file_path + + +def download_file_logged_in(browser, url, suffix=''): """Download a file from Plinth, pretend being logged in via cookies""" - current_url = urlparse(browser.url) - url = "%s://%s%s" % (current_url.scheme, current_url.netloc, url_path) + if not url.startswith("http"): + current_url = 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) diff --git a/plinth/modules/backups/__init__.py b/plinth/modules/backups/__init__.py index 73a6225d4..9c94303f6 100644 --- a/plinth/modules/backups/__init__.py +++ b/plinth/modules/backups/__init__.py @@ -96,7 +96,7 @@ def get_exported_archive_apps(path): return output.splitlines() -def _restore_exported_archive_handler(packet): +def _restore_exported_archive_handler(packet, encryption_passphrase=None): """Perform restore operation on packet.""" locations = {'directories': packet.directories, 'files': packet.files} locations_data = json.dumps(locations) diff --git a/plinth/modules/backups/repository.py b/plinth/modules/backups/repository.py index 861df2784..a2b586266 100644 --- a/plinth/modules/backups/repository.py +++ b/plinth/modules/backups/repository.py @@ -96,7 +96,9 @@ class BorgRepository(object): def list_archives(self): output = self.run(['list-repo', '--path', self.repo_path]) - return json.loads(output)['archives'] + archives = json.loads(output)['archives'] + return sorted(archives, key=lambda archive: archive['start'], + reverse=True) def get_view_content(self): """Get archives with additional information as needed by the view"""