2018-11-30 00:40:18 +00:00
|
|
|
import os
|
2018-12-04 02:58:12 +00:00
|
|
|
import uuid
|
2021-02-17 02:14:58 +00:00
|
|
|
import pytest
|
2018-10-30 17:46:37 +00:00
|
|
|
from PyQt5 import QtCore
|
2021-10-04 11:31:41 +00:00
|
|
|
import vorta.borg.borg_job
|
2021-01-18 07:52:07 +00:00
|
|
|
from vorta.keyring.abc import VortaKeyring
|
2022-03-24 06:27:07 +00:00
|
|
|
from vorta.store.models import ArchiveModel, EventLogModel, RepoModel
|
2018-10-30 17:46:37 +00:00
|
|
|
|
2021-01-18 07:52:07 +00:00
|
|
|
LONG_PASSWORD = 'long-password-long'
|
|
|
|
SHORT_PASSWORD = 'hunter2'
|
|
|
|
|
2018-11-05 10:30:59 +00:00
|
|
|
|
2020-03-03 05:19:36 +00:00
|
|
|
def test_repo_add_failures(qapp, qtbot, mocker, borg_json_output):
|
2018-10-30 17:46:37 +00:00
|
|
|
# Add new repo window
|
2020-03-03 05:19:36 +00:00
|
|
|
main = qapp.main_window
|
2022-03-24 06:27:07 +00:00
|
|
|
main.repoTab.new_repo()
|
2020-11-20 00:46:09 +00:00
|
|
|
add_repo_window = main.repoTab._window
|
2018-12-04 02:58:12 +00:00
|
|
|
qtbot.addWidget(add_repo_window)
|
|
|
|
|
2021-01-18 07:52:07 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.passwordLineEdit, LONG_PASSWORD)
|
|
|
|
qtbot.keyClicks(add_repo_window.confirmLineEdit, LONG_PASSWORD)
|
2018-10-30 17:46:37 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.repoURL, 'aaa')
|
|
|
|
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.LeftButton)
|
2018-11-17 11:36:44 +00:00
|
|
|
assert add_repo_window.errorText.text().startswith('Please enter a valid')
|
2018-10-30 17:46:37 +00:00
|
|
|
|
2021-01-18 07:52:07 +00:00
|
|
|
add_repo_window.passwordLineEdit.clear()
|
|
|
|
add_repo_window.confirmLineEdit.clear()
|
|
|
|
qtbot.keyClicks(add_repo_window.passwordLineEdit, SHORT_PASSWORD)
|
|
|
|
qtbot.keyClicks(add_repo_window.confirmLineEdit, SHORT_PASSWORD)
|
2018-10-30 17:46:37 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.repoURL, 'bbb.com:repo')
|
|
|
|
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.LeftButton)
|
2021-01-18 07:52:07 +00:00
|
|
|
assert add_repo_window.passwordLabel.text() == 'Passwords must be greater than 8 characters long.'
|
|
|
|
|
|
|
|
add_repo_window.passwordLineEdit.clear()
|
|
|
|
add_repo_window.confirmLineEdit.clear()
|
|
|
|
qtbot.keyClicks(add_repo_window.passwordLineEdit, SHORT_PASSWORD + "1")
|
|
|
|
qtbot.keyClicks(add_repo_window.confirmLineEdit, SHORT_PASSWORD)
|
|
|
|
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.LeftButton)
|
|
|
|
assert add_repo_window.passwordLabel.text() == 'Passwords must be identical and greater than 8 characters long.'
|
|
|
|
|
|
|
|
add_repo_window.passwordLineEdit.clear()
|
|
|
|
add_repo_window.confirmLineEdit.clear()
|
|
|
|
qtbot.keyClicks(add_repo_window.passwordLineEdit, LONG_PASSWORD)
|
|
|
|
qtbot.keyClicks(add_repo_window.confirmLineEdit, SHORT_PASSWORD)
|
|
|
|
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.LeftButton)
|
|
|
|
assert add_repo_window.passwordLabel.text() == 'Passwords must be identical.'
|
2018-10-30 17:46:37 +00:00
|
|
|
|
2018-12-04 02:58:12 +00:00
|
|
|
|
2020-11-20 00:46:09 +00:00
|
|
|
def test_repo_unlink(qapp, qtbot):
|
2020-03-03 05:19:36 +00:00
|
|
|
main = qapp.main_window
|
|
|
|
tab = main.repoTab
|
|
|
|
|
2022-11-03 09:22:11 +00:00
|
|
|
main.tabWidget.setCurrentIndex(0)
|
2020-03-03 05:19:36 +00:00
|
|
|
qtbot.mouseClick(tab.repoRemoveToolbutton, QtCore.Qt.LeftButton)
|
2022-10-13 11:12:20 +00:00
|
|
|
qtbot.waitUntil(lambda: tab.repoSelector.count() == 1, **pytest._wait_defaults)
|
2020-03-03 05:19:36 +00:00
|
|
|
assert RepoModel.select().count() == 0
|
|
|
|
|
|
|
|
qtbot.mouseClick(main.createStartBtn, QtCore.Qt.LeftButton)
|
2021-10-04 11:31:41 +00:00
|
|
|
# -1 is the repo id in this test
|
2023-03-22 11:16:46 +00:00
|
|
|
qtbot.waitUntil(lambda: 'Select a backup repository first.' in main.progressText.text(), **pytest._wait_defaults)
|
|
|
|
assert 'Select a backup repository first.' in main.progressText.text()
|
2020-03-03 05:19:36 +00:00
|
|
|
|
|
|
|
|
2021-01-18 07:52:07 +00:00
|
|
|
def test_password_autofill(qapp, qtbot):
|
|
|
|
main = qapp.main_window
|
2022-03-24 06:27:07 +00:00
|
|
|
main.repoTab.new_repo() # couldn't click menu
|
2021-01-18 07:52:07 +00:00
|
|
|
add_repo_window = main.repoTab._window
|
|
|
|
test_repo_url = f'vorta-test-repo.{uuid.uuid4()}.com:repo' # Random repo URL to avoid macOS keychain
|
2020-03-03 05:19:36 +00:00
|
|
|
|
2021-01-18 07:52:07 +00:00
|
|
|
keyring = VortaKeyring.get_keyring()
|
|
|
|
password = str(uuid.uuid4())
|
|
|
|
keyring.set_password('vorta-repo', test_repo_url, password)
|
|
|
|
|
|
|
|
qtbot.keyClicks(add_repo_window.repoURL, test_repo_url)
|
|
|
|
|
2022-08-15 17:02:40 +00:00
|
|
|
assert add_repo_window.passwordLineEdit.text() == password
|
2021-01-18 07:52:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_repo_add_success(qapp, qtbot, mocker, borg_json_output):
|
2018-12-04 02:58:12 +00:00
|
|
|
# Add new repo window
|
2020-03-03 05:19:36 +00:00
|
|
|
main = qapp.main_window
|
2022-03-24 06:27:07 +00:00
|
|
|
main.repoTab.new_repo() # couldn't click menu
|
2020-11-20 00:46:09 +00:00
|
|
|
add_repo_window = main.repoTab._window
|
2018-12-04 02:58:12 +00:00
|
|
|
test_repo_url = f'vorta-test-repo.{uuid.uuid4()}.com:repo' # Random repo URL to avoid macOS keychain
|
|
|
|
|
|
|
|
qtbot.keyClicks(add_repo_window.repoURL, test_repo_url)
|
2018-12-14 08:03:26 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.passwordLineEdit, LONG_PASSWORD)
|
2021-01-18 07:52:07 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.confirmLineEdit, LONG_PASSWORD)
|
2018-10-30 17:46:37 +00:00
|
|
|
|
2018-11-06 06:47:04 +00:00
|
|
|
stdout, stderr = borg_json_output('info')
|
2018-11-22 00:43:37 +00:00
|
|
|
popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0)
|
2021-10-04 11:31:41 +00:00
|
|
|
mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result)
|
2018-10-30 17:46:37 +00:00
|
|
|
|
2021-02-18 07:01:32 +00:00
|
|
|
add_repo_window.run()
|
2022-08-15 17:02:40 +00:00
|
|
|
qtbot.waitUntil(
|
|
|
|
lambda: EventLogModel.select().where(EventLogModel.returncode == 0).count() == 2, **pytest._wait_defaults
|
|
|
|
)
|
2018-10-30 17:46:37 +00:00
|
|
|
|
2018-12-04 02:58:12 +00:00
|
|
|
assert RepoModel.get(id=2).url == test_repo_url
|
2018-11-03 08:55:38 +00:00
|
|
|
|
2021-01-18 07:52:07 +00:00
|
|
|
keyring = VortaKeyring.get_keyring()
|
2018-12-14 08:03:26 +00:00
|
|
|
assert keyring.get_password("vorta-repo", RepoModel.get(id=2).url) == LONG_PASSWORD
|
2020-11-20 00:46:09 +00:00
|
|
|
assert main.repoTab.repoSelector.currentText() == test_repo_url
|
|
|
|
|
2018-12-14 08:03:26 +00:00
|
|
|
|
2020-11-20 00:46:09 +00:00
|
|
|
def test_ssh_dialog(qapp, qtbot, tmpdir):
|
|
|
|
main = qapp.main_window
|
2022-03-24 06:27:07 +00:00
|
|
|
qtbot.mouseClick(main.repoTab.bAddSSHKey, QtCore.Qt.LeftButton)
|
2020-11-20 00:46:09 +00:00
|
|
|
ssh_dialog = main.repoTab._window
|
2018-11-22 00:43:37 +00:00
|
|
|
|
2018-11-30 00:40:18 +00:00
|
|
|
ssh_dir = tmpdir
|
|
|
|
key_tmpfile = ssh_dir.join("id_rsa-test")
|
|
|
|
pub_tmpfile = ssh_dir.join("id_rsa-test.pub")
|
|
|
|
key_tmpfile_full = os.path.join(key_tmpfile.dirname, key_tmpfile.basename)
|
|
|
|
ssh_dialog.outputFileTextBox.setText(key_tmpfile_full)
|
2021-02-18 01:44:10 +00:00
|
|
|
ssh_dialog.generate_key()
|
2018-11-30 00:40:18 +00:00
|
|
|
|
2021-12-24 10:26:21 +00:00
|
|
|
# Ensure new key files exist
|
|
|
|
qtbot.waitUntil(lambda: ssh_dialog.errors.text().startswith('New key was copied'), **pytest._wait_defaults)
|
|
|
|
assert len(ssh_dir.listdir()) == 2
|
2018-11-30 00:40:18 +00:00
|
|
|
|
2021-12-24 10:26:21 +00:00
|
|
|
# Ensure valid keys were created
|
2018-11-30 00:40:18 +00:00
|
|
|
key_tmpfile_content = key_tmpfile.read()
|
|
|
|
assert key_tmpfile_content.startswith('-----BEGIN OPENSSH PRIVATE KEY-----')
|
2021-12-24 10:26:21 +00:00
|
|
|
pub_tmpfile_content = pub_tmpfile.read()
|
2018-11-30 00:40:18 +00:00
|
|
|
assert pub_tmpfile_content.startswith('ssh-ed25519')
|
|
|
|
|
2021-02-18 01:44:10 +00:00
|
|
|
ssh_dialog.generate_key()
|
|
|
|
qtbot.waitUntil(lambda: ssh_dialog.errors.text().startswith('Key file already'), **pytest._wait_defaults)
|
2018-11-30 00:40:18 +00:00
|
|
|
|
|
|
|
|
2020-03-03 05:19:36 +00:00
|
|
|
def test_create(qapp, borg_json_output, mocker, qtbot):
|
|
|
|
main = qapp.main_window
|
2018-11-06 06:47:04 +00:00
|
|
|
stdout, stderr = borg_json_output('create')
|
2018-11-22 00:43:37 +00:00
|
|
|
popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0)
|
2021-10-04 11:31:41 +00:00
|
|
|
mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result)
|
2018-11-06 06:47:04 +00:00
|
|
|
|
|
|
|
qtbot.mouseClick(main.createStartBtn, QtCore.Qt.LeftButton)
|
2023-03-22 11:16:46 +00:00
|
|
|
qtbot.waitUntil(lambda: 'Backup finished.' in main.progressText.text(), **pytest._wait_defaults)
|
2021-02-17 02:14:58 +00:00
|
|
|
qtbot.waitUntil(lambda: main.createStartBtn.isEnabled(), **pytest._wait_defaults)
|
2018-11-06 06:47:04 +00:00
|
|
|
assert EventLogModel.select().count() == 1
|
2020-03-23 06:20:09 +00:00
|
|
|
assert ArchiveModel.select().count() == 3
|
2018-11-06 06:47:04 +00:00
|
|
|
assert RepoModel.get(id=1).unique_size == 15520474
|
|
|
|
assert main.createStartBtn.isEnabled()
|
2020-03-23 06:20:09 +00:00
|
|
|
assert main.archiveTab.archiveTable.rowCount() == 3
|
2018-11-06 06:47:04 +00:00
|
|
|
assert main.scheduleTab.logTableWidget.rowCount() == 1
|