1+ import logging
12import os
23import uuid
34from typing import Any , Dict
1011import vorta .borg .borg_job
1112from vorta .keyring .abc import VortaKeyring
1213from vorta .store .models import ArchiveModel , EventLogModel , RepoModel
14+ from vorta .views .repo_add_dialog import AddRepoWindow
1315
1416LONG_PASSWORD = 'long-password-long'
1517SHORT_PASSWORD = 'hunter2'
@@ -28,7 +30,7 @@ def test_new_repo_password_validation(qapp, qtbot, borg_json_output, first_passw
2830 # Add new repo window
2931 main = qapp .main_window
3032 tab = main .repoTab
31- tab .new_repo ()
33+ tab .add_repo ()
3234 add_repo_window = tab ._window
3335 qtbot .addWidget (add_repo_window )
3436
@@ -41,15 +43,15 @@ def test_new_repo_password_validation(qapp, qtbot, borg_json_output, first_passw
4143@pytest .mark .parametrize (
4244 "repo_name, error_text" ,
4345 [
44- ('test_repo_name' , '' ), # valid repo name
45- ('a' * 64 , '' ), # also valid (<=64 characters)
46+ ('test_repo_name' , 'Checking repository \u2026 ' ), # valid repo name
47+ ('a' * 64 , 'Checking repository \u2026 ' ), # also valid (<=64 characters)
4648 ('a' * 65 , 'Repository name must be less than 65 characters.' ), # not valid (>64 characters)
4749 ],
4850)
4951def test_repo_add_name_validation (qapp , qtbot , borg_json_output , repo_name , error_text ):
5052 main = qapp .main_window
5153 tab = main .repoTab
52- tab .new_repo ()
54+ tab .add_repo ()
5355 add_repo_window = tab ._window
5456 test_repo_url = f'vorta-test-repo.{ uuid .uuid4 ()} .com:repo' # Random repo URL to avoid macOS keychain
5557 qtbot .addWidget (add_repo_window )
@@ -81,7 +83,7 @@ def test_repo_unlink(qapp, qtbot, monkeypatch):
8183def test_password_autofill (qapp , qtbot ):
8284 main = qapp .main_window
8385 tab = main .repoTab
84- tab .new_repo ()
86+ tab .add_repo ()
8587 add_repo_window = tab ._window
8688 test_repo_url = f'vorta-test-repo.{ uuid .uuid4 ()} .com:repo' # Random repo URL to avoid macOS keychain
8789
@@ -97,7 +99,7 @@ def test_password_autofill(qapp, qtbot):
9799def test_repo_add_failure (qapp , qtbot , borg_json_output ):
98100 main = qapp .main_window
99101 tab = main .repoTab
100- tab .new_repo ()
102+ tab .add_repo ()
101103 add_repo_window = tab ._window
102104 qtbot .addWidget (add_repo_window )
103105
@@ -110,7 +112,7 @@ def test_repo_add_failure(qapp, qtbot, borg_json_output):
110112def test_repo_add_success (qapp , qtbot , mocker , borg_json_output ):
111113 main = qapp .main_window
112114 tab = main .repoTab
113- tab .new_repo ()
115+ tab .add_repo ()
114116 add_repo_window = tab ._window
115117 test_repo_url = f'vorta-test-repo.{ uuid .uuid4 ()} .com:repo' # Random repo URL to avoid macOS keychain
116118 test_repo_name = 'Test Repo'
@@ -359,3 +361,112 @@ def test_handle_passphrase_change_result(qapp, qtbot, mocker, result, expected_t
359361 mock_instance .setWindowTitle .assert_called_once_with (tab .tr (expected_title ))
360362 mock_instance .setText .assert_called_once_with (tab .tr (expected_text ))
361363 mock_instance .show .assert_called_once ()
364+
365+
366+ @pytest .mark .parametrize (
367+ "error_message" ,
368+ [
369+ 'Repository /tmp/nonexistent-repo does not exist.' ,
370+ '/tmp/nonexistent-repo is not a valid repository. Check repo config.' ,
371+ ],
372+ )
373+ def test_add_repo_not_found_offers_init (qapp , qtbot , mocker , error_message ):
374+ main = qapp .main_window
375+ tab = main .repoTab
376+ tab .add_repo ()
377+ window = tab ._window
378+
379+ mocker .patch .object (
380+ QMessageBox ,
381+ 'question' ,
382+ return_value = QMessageBox .StandardButton .Yes ,
383+ )
384+ mock_init = mocker .patch .object (window , '_init_repo' )
385+
386+ qtbot .keyClicks (window .passwordInput .passwordLineEdit , 'long-password-long' )
387+ qtbot .keyClicks (window .passwordInput .confirmLineEdit , 'long-password-long' )
388+ window .repoURL .setText ('/tmp/nonexistent-repo' )
389+ window .repoName .setText ('Test Repo' )
390+ window .is_remote_repo = False
391+
392+ result = {
393+ 'returncode' : 2 ,
394+ 'cmd' : ['borg' , 'info' , '/tmp/nonexistent-repo' ],
395+ 'errors' : [(logging .ERROR , error_message )],
396+ 'params' : {'repo_url' : '/tmp/nonexistent-repo' , 'profile_name' : 'Default' },
397+ 'data' : '' ,
398+ }
399+ window ._probe_result (result )
400+
401+ QMessageBox .question .assert_called_once ()
402+ mock_init .assert_called_once ()
403+
404+
405+ def test_add_repo_not_found_user_declines (qapp , qtbot , mocker ):
406+ main = qapp .main_window
407+ tab = main .repoTab
408+ tab .add_repo ()
409+ window = tab ._window
410+
411+ mocker .patch .object (
412+ QMessageBox ,
413+ 'question' ,
414+ return_value = QMessageBox .StandardButton .No ,
415+ )
416+
417+ window .repoURL .setText ('/tmp/nonexistent-repo' )
418+ window .is_remote_repo = False
419+
420+ result = {
421+ 'returncode' : 2 ,
422+ 'cmd' : ['borg' , 'info' , '/tmp/nonexistent-repo' ],
423+ 'errors' : [(logging .ERROR , 'Repository /tmp/nonexistent-repo does not exist.' )],
424+ 'params' : {'repo_url' : '/tmp/nonexistent-repo' , 'profile_name' : 'Default' },
425+ 'data' : '' ,
426+ }
427+ window ._probe_result (result )
428+
429+ assert window .errorText .text () == 'Unable to add your repository.'
430+
431+
432+ def test_add_repo_other_error_no_init_offer (qapp , qtbot , mocker ):
433+ main = qapp .main_window
434+ tab = main .repoTab
435+ tab .add_repo ()
436+ window = tab ._window
437+
438+ mock_question = mocker .patch .object (QMessageBox , 'question' )
439+
440+ window .repoURL .setText ('host.example.com:repo' )
441+ window .is_remote_repo = True
442+
443+ result = {
444+ 'returncode' : 2 ,
445+ 'cmd' : ['borg' , 'info' , 'host.example.com:repo' ],
446+ 'errors' : [(logging .ERROR , 'Connection refused' )],
447+ 'params' : {'repo_url' : 'host.example.com:repo' , 'profile_name' : 'Default' },
448+ 'data' : '' ,
449+ }
450+ window ._probe_result (result )
451+
452+ mock_question .assert_not_called ()
453+ assert window .errorText .text () == 'Unable to add your repository.'
454+
455+
456+ def test_add_repo_probe_succeeds_connects (qapp , qtbot ):
457+ window = AddRepoWindow ()
458+
459+ signal_received = []
460+ window .added_repo .connect (lambda r : signal_received .append (r ))
461+
462+ result = {
463+ 'returncode' : 0 ,
464+ 'cmd' : ['borg' , 'info' , '/tmp/existing-repo' ],
465+ 'errors' : [],
466+ 'params' : {'repo_url' : '/tmp/existing-repo' , 'repo_name' : 'Test' },
467+ 'data' : {},
468+ }
469+ window ._probe_result (result )
470+
471+ assert len (signal_received ) == 1
472+ assert signal_received [0 ]['returncode' ] == 0
0 commit comments