Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: tests/unit/test_repo - Clean up, parametrize, and increase test coverage. #1771

Merged
merged 7 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/vorta/views/partials/password_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def validate(self) -> bool:
self.passwordLineEdit.error_state = True
self.confirmLineEdit.error_state = True
self.set_error_label(
translate('PasswordInput', "Passwords must be identical and atleast {0} characters long.").format(
translate('PasswordInput', "Passwords must be identical and at least {0} characters long.").format(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo corrected: 'atleast' -> 'at least'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

self._minimum_length
)
)
Expand All @@ -148,7 +148,7 @@ def validate(self) -> bool:
elif not pass_long:
self.passwordLineEdit.error_state = True
self.set_error_label(
translate('PasswordInput', "Passwords must be atleast {0} characters long.").format(
translate('PasswordInput', "Passwords must be at least {0} characters long.").format(
self._minimum_length
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/vorta/views/repo_add_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def validate(self):
return False

if len(self.values['repo_name']) > 64:
self._set_status(self.tr('Repository name must be less than 64 characters.'))
self._set_status(self.tr('Repository name must be less than 65 characters.'))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected wording, should say "less than 65 characters" or "no more than 64 characters". I chose the path of least change.

return False

if RepoModel.get_or_none(RepoModel.url == self.values['repo_url']) is not None:
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_password_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def test_password_input_validation(qapp, qtbot):
qtbot.keyClicks(password_input.confirmLineEdit, "123456789")

assert password_input.passwordLineEdit.error_state
assert password_input.validation_label.text() == "Passwords must be atleast 10 characters long."
assert password_input.validation_label.text() == "Passwords must be at least 10 characters long."

password_input.clear()
qtbot.keyClicks(password_input.passwordLineEdit, "123456789")
qtbot.keyClicks(password_input.confirmLineEdit, "test")

assert password_input.passwordLineEdit.error_state
assert password_input.confirmLineEdit.error_state
assert password_input.validation_label.text() == "Passwords must be identical and atleast 10 characters long."
assert password_input.validation_label.text() == "Passwords must be identical and at least 10 characters long."

password_input.clear()
qtbot.keyClicks(password_input.passwordLineEdit, "1234567890")
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_password_input_validation_disabled(qapp, qtbot):

assert password_input.passwordLineEdit.error_state
assert password_input.confirmLineEdit.error_state
assert password_input.validation_label.text() == "Passwords must be identical and atleast 9 characters long."
assert password_input.validation_label.text() == "Passwords must be identical and at least 9 characters long."

password_input.set_validation_enabled(False)
assert not password_input.passwordLineEdit.error_state
Expand Down
100 changes: 61 additions & 39 deletions tests/unit/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,56 @@
SHORT_PASSWORD = 'hunter2'


def test_repo_add_failures(qapp, qtbot, mocker, borg_json_output):
@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):
# Add new repo window
main = qapp.main_window
main.repoTab.new_repo()
add_repo_window = main.repoTab._window
tab = main.repoTab
tab.new_repo()
add_repo_window = tab._window
qtbot.addWidget(add_repo_window)

qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, LONG_PASSWORD)
qtbot.keyClicks(add_repo_window.passwordInput.confirmLineEdit, LONG_PASSWORD)
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')

add_repo_window.passwordInput.passwordLineEdit.clear()
add_repo_window.passwordInput.confirmLineEdit.clear()
qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, SHORT_PASSWORD)
qtbot.keyClicks(add_repo_window.passwordInput.confirmLineEdit, SHORT_PASSWORD)
qtbot.keyClicks(add_repo_window.repoURL, 'bbb.com:repo')
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.MouseButton.LeftButton)
assert add_repo_window.passwordInput.validation_label.text() == 'Passwords must be atleast 9 characters long.'

add_repo_window.passwordInput.passwordLineEdit.clear()
add_repo_window.passwordInput.confirmLineEdit.clear()
qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, SHORT_PASSWORD + "1")
qtbot.keyClicks(add_repo_window.passwordInput.confirmLineEdit, SHORT_PASSWORD)
qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, first_password)
qtbot.keyClicks(add_repo_window.passwordInput.confirmLineEdit, second_password)
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.MouseButton.LeftButton)
assert (
add_repo_window.passwordInput.validation_label.text()
== 'Passwords must be identical and atleast 9 characters long.'
)
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)

add_repo_window.passwordInput.passwordLineEdit.clear()
add_repo_window.passwordInput.confirmLineEdit.clear()
qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, LONG_PASSWORD)
qtbot.keyClicks(add_repo_window.passwordInput.confirmLineEdit, SHORT_PASSWORD)
qtbot.keyClicks(add_repo_window.repoURL, test_repo_url)
qtbot.keyClicks(add_repo_window.repoName, repo_name)
qtbot.mouseClick(add_repo_window.saveButton, QtCore.Qt.MouseButton.LeftButton)
assert add_repo_window.passwordInput.validation_label.text() == 'Passwords must be identical.'
assert add_repo_window.errorText.text() == error_text


def test_repo_unlink(qapp, qtbot, monkeypatch):
main = qapp.main_window
tab = main.repoTab
monkeypatch.setattr(QMessageBox, "show", lambda *args: True)

main.tabWidget.setCurrentIndex(0)
qtbot.mouseClick(tab.repoRemoveToolbutton, QtCore.Qt.MouseButton.LeftButton)
qtbot.waitUntil(lambda: tab.repoSelector.count() == 1, **pytest._wait_defaults)
assert RepoModel.select().count() == 0
Expand All @@ -69,8 +74,9 @@ def test_repo_unlink(qapp, qtbot, monkeypatch):

def test_password_autofill(qapp, qtbot):
main = qapp.main_window
main.repoTab.new_repo() # couldn't click menu
add_repo_window = main.repoTab._window
tab = main.repoTab
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency across the tests, I defined main and tab in each test that used them.

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

keyring = VortaKeyring.get_keyring()
Expand All @@ -82,14 +88,28 @@ def test_password_autofill(qapp, qtbot):
assert add_repo_window.passwordInput.passwordLineEdit.text() == password


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')


def test_repo_add_success(qapp, qtbot, mocker, borg_json_output):
# Add new repo window
main = qapp.main_window
main.repoTab.new_repo() # couldn't click menu
add_repo_window = main.repoTab._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
test_repo_name = 'Test Repo'

# Enter valid repo URL, name, and password
qtbot.keyClicks(add_repo_window.repoURL, test_repo_url)
qtbot.keyClicks(add_repo_window.repoName, test_repo_name)
qtbot.keyClicks(add_repo_window.passwordInput.passwordLineEdit, LONG_PASSWORD)
Expand All @@ -108,13 +128,15 @@ def test_repo_add_success(qapp, qtbot, mocker, borg_json_output):

keyring = VortaKeyring.get_keyring()
assert keyring.get_password("vorta-repo", RepoModel.get(id=2).url) == LONG_PASSWORD
assert main.repoTab.repoSelector.currentText() == f"{test_repo_name} - {test_repo_url}"
assert tab.repoSelector.currentText() == f"{test_repo_name} - {test_repo_url}"


def test_ssh_dialog(qapp, qtbot, tmpdir):
main = qapp.main_window
qtbot.mouseClick(main.repoTab.bAddSSHKey, QtCore.Qt.MouseButton.LeftButton)
ssh_dialog = main.repoTab._window
tab = main.repoTab

qtbot.mouseClick(tab.bAddSSHKey, QtCore.Qt.MouseButton.LeftButton)
ssh_dialog = tab._window

ssh_dir = tmpdir
key_tmpfile = ssh_dir.join("id_rsa-test")
Expand Down