Skip to content

Commit

Permalink
test_ssh_copy_to_clipboard_action begun
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtedde committed Aug 31, 2023
1 parent 605820c commit 2f689df
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
39 changes: 39 additions & 0 deletions tests/unit/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 30 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
is_system_tray_available,
normalize_path,
pretty_bytes,
sort_sizes,
)


Expand All @@ -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",
[
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2f689df

Please sign in to comment.