From 2f689df192f48ef0cee94543e23c0ebcc751edb4 Mon Sep 17 00:00:00 2001 From: bigtedde Date: Tue, 29 Aug 2023 17:07:28 -0700 Subject: [PATCH] test_ssh_copy_to_clipboard_action begun --- tests/unit/test_repo.py | 39 +++++++++++++++++++++++++++++++++++++++ tests/unit/test_utils.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_repo.py b/tests/unit/test_repo.py index 07f0f28b3..9a32f50b4 100644 --- a/tests/unit/test_repo.py +++ b/tests/unit/test_repo.py @@ -228,3 +228,42 @@ def test_repo_check_failed_response(qapp, qtbot, mocker, response): 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] + + +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 + text = mocker.patch.object(QMessageBox, "setText") + mocker.patch.object(QMessageBox, "show") # prevent QMessageBox from disrupting test + mocker.patch.object(qapp.clipboard(), "setText") # prevent actual clipboard data from being changed + + # create a new ssh key to be copied + qtbot.mouseClick(tab.bAddSSHKey, QtCore.Qt.MouseButton.LeftButton) + ssh_dialog, ssh_dir = tab._window, tmpdir + key_tmpfile, pub_tmpfile = ssh_dir.join("id_rsa-test"), ssh_dir.join("id_rsa-test.pub") # noqa: F841 + key_tmpfile_full = os.path.join(key_tmpfile.dirname, key_tmpfile.basename) + ssh_dialog.outputFileTextBox.setText(key_tmpfile_full) + ssh_dialog.generate_key() + qtbot.waitUntil(lambda: ssh_dialog.errors.text().startswith('New key was copied'), **pytest._wait_defaults) + assert len(ssh_dir.listdir()) == 2 + assert tab.sshComboBox.count() > 1 + + # no ssh key 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 + 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 + mocker.patch.object(os.path, "isfile", return_value=False) + assert tab.sshComboBox.currentIndex() == 1 + qtbot.mouseClick(tab.sshKeyToClipboardButton, QtCore.Qt.MouseButton.LeftButton) + message = "Could not find public key." + text.assert_called_with(message) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index cbb971b85..ea529c089 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -9,6 +9,7 @@ is_system_tray_available, normalize_path, pretty_bytes, + sort_sizes, ) @@ -21,6 +22,33 @@ def test_keyring(): assert keyring.get_password("vorta-repo", REPO) == UNICODE_PW +@pytest.mark.parametrize( + "input_sizes, expected_sorted", + [ + # Basic ordering + (["1.0 GB", "2.0 MB", "3.0 KB"], ["3.0 KB", "2.0 MB", "1.0 GB"]), + # Multiple same units + (["3.0 GB", "2.0 GB", "1.0 GB"], ["1.0 GB", "2.0 GB", "3.0 GB"]), + # Multiple different units + (["2.0 MB", "3.0 GB", "1.0 KB", "5.0 GB"], ["1.0 KB", "2.0 MB", "3.0 GB", "5.0 GB"]), + # Larger to smaller units + (["1.0 YB", "1.0 ZB", "1.0 EB", "1.0 PB"], ["1.0 PB", "1.0 EB", "1.0 ZB", "1.0 YB"]), + # Skipping non-numeric sizes + (["2x MB", "3.0 KB", "apple GB", "1.0 GB"], ["3.0 KB", "1.0 GB"]), + # Skipping invalid suffix + (["1.0 XX", "5.0 YY", "9.0 ZZ", "1.0 MB"], ["1.0 MB"]), + # Floats with decimals + (["2.5 GB", "2.3 GB", "1.1 MB"], ["1.1 MB", "2.3 GB", "2.5 GB"]), + # Checking the same sizes across different units + (["1.0 MB", "1000.0 KB"], ["1000.0 KB", "1.0 MB"]), + # Handle empty lists + ([], []), + ], +) +def test_sort_sizes(input_sizes, expected_sorted): + assert sort_sizes(input_sizes) == expected_sorted + + @pytest.mark.parametrize( "precision, expected_unit", [ @@ -60,7 +88,7 @@ def test_best_unit_for_sizes_nonmetric(sizes, expected_unit): ) def test_pretty_bytes_fixed_units(size, metric, precision, fixed_unit, expected_output): """ - test pretty bytes when specifying a fixed unit of measurement + Test pretty bytes when specifying a fixed unit of measurement """ output = pretty_bytes(size, metric=metric, precision=precision, fixed_unit=fixed_unit) assert output == expected_output @@ -131,7 +159,7 @@ def test_get_path_datasize(tmpdir): def test_is_system_tray_available(mocker): """ - sanity check to ensure proper behavior + Sanity check to ensure proper behavior """ mocker.patch('PyQt6.QtWidgets.QSystemTrayIcon.isSystemTrayAvailable', return_value=False) assert is_system_tray_available() is False