2018-11-30 00:40:18 +00:00
|
|
|
import os
|
2018-12-04 02:58:12 +00:00
|
|
|
import uuid
|
2023-10-01 08:19:39 +00:00
|
|
|
from typing import Any, Dict
|
2023-05-01 08:28:11 +00:00
|
|
|
|
2021-02-17 02:14:58 +00:00
|
|
|
import pytest
|
2023-04-17 10:17:01 +00:00
|
|
|
from PyQt6 import QtCore
|
|
|
|
from PyQt6.QtWidgets import QMessageBox
|
2024-10-18 11:08:59 +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
|
|
|
|
2023-08-17 10:08:03 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"first_password, second_password, validation_error",
|
|
|
|
[
|
|
|
|
(SHORT_PASSWORD, SHORT_PASSWORD, 'Passwords must be at least 9 characters long.'),
|
|
|
|
(LONG_PASSWORD, SHORT_PASSWORD, 'Passwords must be identical.'),
|
|
|
|
(SHORT_PASSWORD + "1", SHORT_PASSWORD, 'Passwords must be identical and at least 9 characters long.'),
|
|
|
|
(LONG_PASSWORD, LONG_PASSWORD, ''), # no error, password meets requirements.
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_new_repo_password_validation(qapp, qtbot, borg_json_output, first_password, second_password, validation_error):
|
2018-10-30 17:46:37 +00:00
|
|
|
# Add new repo window
|
2020-03-03 05:19:36 +00:00
|
|
|
main = qapp.main_window
|
2023-08-17 10:08:03 +00:00
|
|
|
tab = main.repoTab
|
|
|
|
tab.new_repo()
|
|
|
|
add_repo_window = tab._window
|
2018-12-04 02:58:12 +00:00
|
|
|
qtbot.addWidget(add_repo_window)
|
|
|
|
|
2023-08-17 10:08:03 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, first_password)
|
|
|
|
qtbot.keyClicks(add_repo_window.passwordInput.confirmLineEdit, second_password)
|
2023-04-17 10:17:01 +00:00
|
|
|
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.MouseButton.LeftButton)
|
2023-08-17 10:08:03 +00:00
|
|
|
assert add_repo_window.passwordInput.validation_label.text() == validation_error
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"repo_name, error_text",
|
|
|
|
[
|
|
|
|
('test_repo_name', ''), # valid repo name
|
|
|
|
('a' * 64, ''), # also valid (<=64 characters)
|
|
|
|
('a' * 65, 'Repository name must be less than 65 characters.'), # not valid (>64 characters)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_repo_add_name_validation(qapp, qtbot, borg_json_output, repo_name, error_text):
|
|
|
|
main = qapp.main_window
|
|
|
|
tab = main.repoTab
|
|
|
|
tab.new_repo()
|
|
|
|
add_repo_window = tab._window
|
|
|
|
test_repo_url = f'vorta-test-repo.{uuid.uuid4()}.com:repo' # Random repo URL to avoid macOS keychain
|
|
|
|
qtbot.addWidget(add_repo_window)
|
2021-01-18 07:52:07 +00:00
|
|
|
|
2023-08-17 10:08:03 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.repoURL, test_repo_url)
|
|
|
|
qtbot.keyClicks(add_repo_window.repoName, repo_name)
|
2023-04-17 10:17:01 +00:00
|
|
|
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.MouseButton.LeftButton)
|
2023-08-17 10:08:03 +00:00
|
|
|
assert add_repo_window.errorText.text() == error_text
|
2018-10-30 17:46:37 +00:00
|
|
|
|
2018-12-04 02:58:12 +00:00
|
|
|
|
2023-04-17 10:17:01 +00:00
|
|
|
def test_repo_unlink(qapp, qtbot, monkeypatch):
|
2020-03-03 05:19:36 +00:00
|
|
|
main = qapp.main_window
|
|
|
|
tab = main.repoTab
|
2023-04-17 10:17:01 +00:00
|
|
|
monkeypatch.setattr(QMessageBox, "show", lambda *args: True)
|
2020-03-03 05:19:36 +00:00
|
|
|
|
2023-04-17 10:17:01 +00:00
|
|
|
qtbot.mouseClick(tab.repoRemoveToolbutton, QtCore.Qt.MouseButton.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
|
|
|
|
|
2023-04-17 10:17:01 +00:00
|
|
|
qtbot.mouseClick(main.createStartBtn, QtCore.Qt.MouseButton.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
|
2023-08-17 10:08:03 +00:00
|
|
|
tab = main.repoTab
|
|
|
|
tab.new_repo()
|
|
|
|
add_repo_window = tab._window
|
2021-01-18 07:52:07 +00:00
|
|
|
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)
|
|
|
|
|
2023-07-28 07:39:10 +00:00
|
|
|
assert add_repo_window.passwordInput.passwordLineEdit.text() == password
|
2021-01-18 07:52:07 +00:00
|
|
|
|
|
|
|
|
2023-08-17 10:08:03 +00:00
|
|
|
def test_repo_add_failure(qapp, qtbot, borg_json_output):
|
|
|
|
main = qapp.main_window
|
|
|
|
tab = main.repoTab
|
|
|
|
tab.new_repo()
|
|
|
|
add_repo_window = tab._window
|
|
|
|
qtbot.addWidget(add_repo_window)
|
|
|
|
|
|
|
|
# Add repo with invalid URL
|
|
|
|
qtbot.keyClicks(add_repo_window.repoURL, 'aaa')
|
|
|
|
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.MouseButton.LeftButton)
|
|
|
|
assert add_repo_window.errorText.text().startswith('Please enter a valid repo URL')
|
|
|
|
|
|
|
|
|
2021-01-18 07:52:07 +00:00
|
|
|
def test_repo_add_success(qapp, qtbot, mocker, borg_json_output):
|
2020-03-03 05:19:36 +00:00
|
|
|
main = qapp.main_window
|
2023-08-17 10:08:03 +00:00
|
|
|
tab = main.repoTab
|
|
|
|
tab.new_repo()
|
|
|
|
add_repo_window = tab._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
|
2023-06-24 19:57:46 +00:00
|
|
|
test_repo_name = 'Test Repo'
|
2018-12-04 02:58:12 +00:00
|
|
|
|
2023-08-17 10:08:03 +00:00
|
|
|
# Enter valid repo URL, name, and password
|
2018-12-04 02:58:12 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.repoURL, test_repo_url)
|
2023-06-24 19:57:46 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.repoName, test_repo_name)
|
2023-07-28 07:39:10 +00:00
|
|
|
qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, LONG_PASSWORD)
|
|
|
|
qtbot.keyClicks(add_repo_window.passwordInput.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
|
2023-08-17 10:08:03 +00:00
|
|
|
assert tab.repoSelector.currentText() == f"{test_repo_name} - {test_repo_url}"
|
2020-11-20 00:46:09 +00:00
|
|
|
|
2018-12-14 08:03:26 +00:00
|
|
|
|
2023-09-26 13:22:54 +00:00
|
|
|
def test_ssh_dialog_success(qapp, qtbot, mocker, tmpdir):
|
2020-11-20 00:46:09 +00:00
|
|
|
main = qapp.main_window
|
2023-08-17 10:08:03 +00:00
|
|
|
tab = main.repoTab
|
|
|
|
|
|
|
|
qtbot.mouseClick(tab.bAddSSHKey, QtCore.Qt.MouseButton.LeftButton)
|
|
|
|
ssh_dialog = tab._window
|
2023-09-26 13:22:54 +00:00
|
|
|
ssh_dialog_closed = mocker.spy(ssh_dialog, 'reject')
|
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
|
|
|
|
2023-09-26 13:22:54 +00:00
|
|
|
# Ensure new key file was created
|
|
|
|
qtbot.waitUntil(lambda: ssh_dialog_closed.called, **pytest._wait_defaults)
|
2021-12-24 10:26:21 +00:00
|
|
|
assert len(ssh_dir.listdir()) == 2
|
2018-11-30 00:40:18 +00:00
|
|
|
|
2023-09-26 13:22:54 +00:00
|
|
|
# Ensure new key is populated in SSH combobox
|
|
|
|
mocker.patch('os.path.expanduser', return_value=str(tmpdir))
|
|
|
|
tab.init_ssh()
|
|
|
|
assert tab.sshComboBox.count() == 2
|
|
|
|
|
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')
|
|
|
|
|
2023-09-26 13:22:54 +00:00
|
|
|
|
|
|
|
def test_ssh_dialog_failure(qapp, qtbot, mocker, monkeypatch, tmpdir):
|
|
|
|
main = qapp.main_window
|
|
|
|
tab = main.repoTab
|
|
|
|
monkeypatch.setattr(QMessageBox, "show", lambda *args: True)
|
|
|
|
failure_message = mocker.spy(tab, "create_ssh_key_failure")
|
|
|
|
|
|
|
|
qtbot.mouseClick(tab.bAddSSHKey, QtCore.Qt.MouseButton.LeftButton)
|
|
|
|
ssh_dialog = tab._window
|
|
|
|
ssh_dir = tmpdir
|
|
|
|
key_tmpfile = ssh_dir.join("invalid///===for_testing")
|
|
|
|
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()
|
2023-09-26 13:22:54 +00:00
|
|
|
|
|
|
|
qtbot.waitUntil(lambda: failure_message.called, **pytest._wait_defaults)
|
|
|
|
failure_message.assert_called_once()
|
|
|
|
|
|
|
|
# Ensure no new ney file was created
|
|
|
|
assert len(ssh_dir.listdir()) == 0
|
|
|
|
|
|
|
|
# Ensure no new key file in combo box
|
|
|
|
mocker.patch('os.path.expanduser', return_value=str(tmpdir))
|
|
|
|
tab.init_ssh()
|
|
|
|
assert tab.sshComboBox.count() == 1
|
2018-11-30 00:40:18 +00:00
|
|
|
|
|
|
|
|
2023-10-01 08:19:39 +00:00
|
|
|
def test_ssh_copy_to_clipboard_action(qapp, qtbot, mocker, tmpdir):
|
|
|
|
"""Testing the proper QMessageBox dialogue appears depending on the copy action circumstances."""
|
|
|
|
tab = qapp.main_window.repoTab
|
|
|
|
|
|
|
|
# set mocks to test assertions and prevent test interruptions
|
|
|
|
text = mocker.patch.object(QMessageBox, "setText")
|
|
|
|
mocker.patch.object(QMessageBox, "show")
|
|
|
|
mocker.patch.object(qapp.clipboard(), "setText")
|
|
|
|
|
|
|
|
qtbot.mouseClick(tab.bAddSSHKey, QtCore.Qt.MouseButton.LeftButton)
|
|
|
|
ssh_dialog = tab._window
|
|
|
|
ssh_dialog_closed = mocker.spy(ssh_dialog, 'reject')
|
|
|
|
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)
|
|
|
|
ssh_dialog.generate_key()
|
|
|
|
|
|
|
|
# Ensure new key file was created
|
|
|
|
qtbot.waitUntil(lambda: ssh_dialog_closed.called, **pytest._wait_defaults)
|
|
|
|
assert len(ssh_dir.listdir()) == 2
|
|
|
|
# populate the ssh combobox with the ssh key we created in tmpdir
|
|
|
|
mock_expanduser = mocker.patch('os.path.expanduser', return_value=str(tmpdir))
|
|
|
|
tab.init_ssh()
|
|
|
|
assert tab.sshComboBox.count() == 2
|
|
|
|
|
|
|
|
# test when no ssh key is selected to copy
|
|
|
|
assert tab.sshComboBox.currentIndex() == 0
|
|
|
|
qtbot.mouseClick(tab.sshKeyToClipboardButton, QtCore.Qt.MouseButton.LeftButton)
|
|
|
|
message = "Select a public key from the dropdown first."
|
|
|
|
text.assert_called_with(message)
|
|
|
|
|
|
|
|
# Select a key and copy it
|
|
|
|
mock_expanduser.return_value = pub_tmpfile
|
|
|
|
tab.sshComboBox.setCurrentIndex(1)
|
|
|
|
assert tab.sshComboBox.currentIndex() == 1
|
|
|
|
qtbot.mouseClick(tab.sshKeyToClipboardButton, QtCore.Qt.MouseButton.LeftButton)
|
|
|
|
message = "The selected public SSH key was copied to the clipboard. Use it to set up remote repo permissions."
|
|
|
|
text.assert_called_with(message)
|
|
|
|
|
|
|
|
# handle ssh key file not found
|
|
|
|
mock_expanduser.return_value = "foobar"
|
|
|
|
assert tab.sshComboBox.currentIndex() == 1
|
|
|
|
qtbot.mouseClick(tab.sshKeyToClipboardButton, QtCore.Qt.MouseButton.LeftButton)
|
|
|
|
message = "Could not find public key."
|
|
|
|
text.assert_called_with(message)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-04-17 10:17:01 +00:00
|
|
|
qtbot.mouseClick(main.createStartBtn, QtCore.Qt.MouseButton.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
|
2024-07-19 14:21:58 +00:00
|
|
|
assert main.scheduleTab.logPage.logPage.rowCount() == 1
|
2023-10-01 08:19:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"response",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"return_code": 0, # no error
|
|
|
|
"error": "",
|
|
|
|
"icon": None,
|
|
|
|
"info": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"return_code": 1, # warning
|
|
|
|
"error": "Borg exited with warning status (rc 1).",
|
|
|
|
"icon": QMessageBox.Icon.Warning,
|
|
|
|
"info": "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"return_code": 2, # critical error
|
|
|
|
"error": "Repository data check for repo test_repo_url failed. Error code 2",
|
|
|
|
"icon": QMessageBox.Icon.Critical,
|
|
|
|
"info": "Consider repairing or recreating the repository soon to avoid missing data.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"return_code": 135, # 128 + n = kill signal n
|
|
|
|
"error": "killed by signal 7",
|
|
|
|
"icon": QMessageBox.Icon.Critical,
|
|
|
|
"info": "The process running the check job got a kill signal. Try again.",
|
|
|
|
},
|
|
|
|
{"return_code": 130, "error": "", "icon": None, "info": None}, # keyboard interrupt
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_repo_check_failed_response(qapp, qtbot, mocker, response):
|
|
|
|
"""Test the processing of the signal that a repo consistency check has failed."""
|
|
|
|
mock_result: Dict[str, Any] = {
|
|
|
|
'params': {'repo_url': 'test_repo_url'},
|
|
|
|
'returncode': response["return_code"],
|
|
|
|
'errors': [(0, 'test_error_message')] if response["return_code"] not in [0, 130] else None,
|
|
|
|
}
|
|
|
|
|
|
|
|
mock_exec = mocker.patch.object(QMessageBox, "exec")
|
|
|
|
mock_text = mocker.patch.object(QMessageBox, "setText")
|
|
|
|
mock_info = mocker.patch.object(QMessageBox, "setInformativeText")
|
|
|
|
mock_icon = mocker.patch.object(QMessageBox, "setIcon")
|
|
|
|
|
|
|
|
qapp.check_failed_response(mock_result)
|
|
|
|
|
|
|
|
# return codes 0 and 130 do not provide a message
|
|
|
|
# for all other return codes, assert the message is formatted correctly
|
|
|
|
if mock_exec.call_count != 0:
|
|
|
|
mock_icon.assert_called_with(response["icon"])
|
|
|
|
assert response["error"] in mock_text.call_args[0][0]
|
|
|
|
assert response["info"] in mock_info.call_args[0][0]
|