1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
import os
from pathlib import Path
import pytest
from PyQt6 import QtCore
from PyQt6.QtWidgets import QDialogButtonBox, QFileDialog, QMessageBox
from vorta.profile_export import VersionException
from vorta.store.models import BackupProfileModel, SourceFileModel
from vorta.views.import_window import ImportWindow
VALID_IMPORT_FILE = Path(__file__).parent / 'profile_exports' / 'valid.json'
def test_import_success(qapp, qtbot, rootdir, monkeypatch):
monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args: [VALID_IMPORT_FILE])
monkeypatch.setattr(QMessageBox, 'information', lambda *args: None)
main = qapp.main_window
main.profile_import_action()
import_dialog: ImportWindow = main.window
import_dialog.overwriteExistingSettings.setChecked(True)
qtbot.mouseClick(
import_dialog.buttonBox.button(QDialogButtonBox.StandardButton.Ok), QtCore.Qt.MouseButton.LeftButton
)
qtbot.waitSignal(import_dialog.profile_imported, **pytest._wait_defaults)
restored_profile = BackupProfileModel.get_or_none(name="Test Profile Restoration")
assert restored_profile is not None
restored_repo = restored_profile.repo
assert restored_repo is not None
assert len(SourceFileModel.select().where(SourceFileModel.profile == restored_profile)) == 3
@pytest.mark.parametrize(
"exception, error_message",
[
(AttributeError, "Schema upgrade failure"),
(VersionException, "Newer profile_export export files cannot be used on older versions"),
(PermissionError, "Cannot read profile_export export file due to permission error"),
(FileNotFoundError, "Profile export file not found"),
],
)
def test_import_exceptions(qapp, qtbot, rootdir, monkeypatch, mocker, exception, error_message):
monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args: [VALID_IMPORT_FILE])
monkeypatch.setattr(QMessageBox, 'information', lambda *args: None)
main = qapp.main_window
main.profile_import_action()
import_dialog: ImportWindow = main.window
import_dialog.overwriteExistingSettings.setChecked(True)
def raise_exception(*args, **kwargs):
raise exception
# force an exception and mock the error QMessageBox
monkeypatch.setattr(import_dialog.profile_export, 'to_db', raise_exception)
mock_messagebox = mocker.patch.object(QMessageBox, "critical")
qtbot.mouseClick(
import_dialog.buttonBox.button(QDialogButtonBox.StandardButton.Ok), QtCore.Qt.MouseButton.LeftButton
)
# assert the correct error appears, and the profile does not get added
mock_messagebox.assert_called_once()
assert error_message in mock_messagebox.call_args[0][2]
assert BackupProfileModel.get_or_none(name="Test Profile Restoration") is None
def test_import_bootstrap_success(qapp, mocker):
mocked_unlink = mocker.MagicMock()
mocker.patch.object(Path, 'unlink', mocked_unlink)
qapp.bootstrap_profile(Path(VALID_IMPORT_FILE))
assert mocked_unlink.called
restored_profile = BackupProfileModel.get_or_none(name="Test Profile Restoration")
assert restored_profile is not None
restored_repo = restored_profile.repo
assert restored_repo is not None
assert len(SourceFileModel.select().where(SourceFileModel.profile == restored_profile)) == 3
assert BackupProfileModel.select().count() == 2
def test_import_fail_not_json(qapp, rootdir, monkeypatch):
BAD_FILE = os.path.join(rootdir, 'profile_exports', 'invalid_no_json.json')
def getOpenFileName(*args, **kwargs):
return [BAD_FILE]
monkeypatch.setattr(QFileDialog, "getOpenFileName", getOpenFileName)
alert_message = None
def critical(widget, title, message):
nonlocal alert_message
alert_message = message
monkeypatch.setattr(QMessageBox, "critical", critical)
main = qapp.main_window
main.profile_import_action()
# assert somehow that an alert is shown
assert alert_message == 'This file does not contain valid JSON: Expecting value: line 1 column 1 (char 0)'
def test_export_success(qapp, qtbot, tmpdir, monkeypatch):
FILE_PATH = os.path.join(tmpdir, "testresult.json")
def getSaveFileName(*args, **kwargs):
return [FILE_PATH]
monkeypatch.setattr(QFileDialog, "getSaveFileName", getSaveFileName)
main = qapp.main_window
main.profile_export_action()
export_dialog = main.window
qtbot.mouseClick(
export_dialog.buttonBox.button(QDialogButtonBox.StandardButton.Save), QtCore.Qt.MouseButton.LeftButton
)
qtbot.waitUntil(lambda: os.path.isfile(FILE_PATH))
assert os.path.isfile(FILE_PATH)
|