|
2 | 2 |
|
3 | 3 | from mock import patch, call, Mock
|
4 | 4 |
|
5 |
| -from jupyterlab_git.git import Git |
| 5 | +from jupyterlab_git.git import Git, git_auth_input_wrapper |
6 | 6 |
|
7 | 7 |
|
8 | 8 | @patch('subprocess.Popen')
|
@@ -66,3 +66,109 @@ def test_git_clone_failure_from_git(mock_subproc_popen):
|
66 | 66 | call().communicate()
|
67 | 67 | ])
|
68 | 68 | assert {'code': 128, 'message': 'fatal: Not a git repository'} == actual_response
|
| 69 | + |
| 70 | +@patch('jupyterlab_git.git.git_auth_input_wrapper') |
| 71 | +@patch('os.environ', {'TEST': 'test'}) |
| 72 | +def test_git_clone_with_auth_success(mock_git_auth_input_wrapper): |
| 73 | + # Given |
| 74 | + process_mock = Mock() |
| 75 | + attrs = { |
| 76 | + 'communicate.return_value': '', |
| 77 | + 'returncode': 0 |
| 78 | + } |
| 79 | + process_mock.configure_mock(**attrs) |
| 80 | + mock_git_auth_input_wrapper.return_value = process_mock |
| 81 | + |
| 82 | + # When |
| 83 | + auth = { |
| 84 | + 'username' : 'asdf', |
| 85 | + 'password' : 'qwerty' |
| 86 | + } |
| 87 | + actual_response = Git(root_dir='/bin').clone(current_path='test_curr_path', repo_url='ghjkhjkl', auth=auth) |
| 88 | + |
| 89 | + # Then |
| 90 | + mock_git_auth_input_wrapper.assert_has_calls([ |
| 91 | + call( |
| 92 | + command = 'git clone ghjkhjkl -q', |
| 93 | + cwd = '/bin/test_curr_path', |
| 94 | + env={'TEST': 'test', 'GIT_TERMINAL_PROMPT': '1'}, |
| 95 | + username = 'asdf', |
| 96 | + password = 'qwerty' |
| 97 | + ), |
| 98 | + call().communicate() |
| 99 | + ]) |
| 100 | + assert {'code': 0} == actual_response |
| 101 | + |
| 102 | +@patch('jupyterlab_git.git.git_auth_input_wrapper') |
| 103 | +@patch('os.environ', {'TEST': 'test'}) |
| 104 | +def test_git_clone_with_auth_wrong_repo_url_failure_from_git(mock_git_auth_input_wrapper): |
| 105 | + """ |
| 106 | + Git internally will throw an error if it is an invalid URL, or if there is a permissions issue. We want to just |
| 107 | + relay it back to the user. |
| 108 | +
|
| 109 | + """ |
| 110 | + # Given |
| 111 | + process_mock = Mock() |
| 112 | + attrs = { |
| 113 | + 'communicate.return_value': "fatal: repository 'ghjkhjkl' does not exist".encode('utf-8'), |
| 114 | + 'returncode': 128 |
| 115 | + } |
| 116 | + process_mock.configure_mock(**attrs) |
| 117 | + mock_git_auth_input_wrapper.return_value = process_mock |
| 118 | + |
| 119 | + # When |
| 120 | + auth = { |
| 121 | + 'username' : 'asdf', |
| 122 | + 'password' : 'qwerty' |
| 123 | + } |
| 124 | + actual_response = Git(root_dir='/bin').clone(current_path='test_curr_path', repo_url='ghjkhjkl', auth=auth) |
| 125 | + |
| 126 | + # Then |
| 127 | + mock_git_auth_input_wrapper.assert_has_calls([ |
| 128 | + call( |
| 129 | + command = 'git clone ghjkhjkl -q', |
| 130 | + cwd = '/bin/test_curr_path', |
| 131 | + env={'TEST': 'test', 'GIT_TERMINAL_PROMPT': '1'}, |
| 132 | + username = 'asdf', |
| 133 | + password = 'qwerty' |
| 134 | + ), |
| 135 | + call().communicate() |
| 136 | + ]) |
| 137 | + assert {'code': 128, 'message': "fatal: repository 'ghjkhjkl' does not exist"} == actual_response |
| 138 | + |
| 139 | +@patch('jupyterlab_git.git.git_auth_input_wrapper') |
| 140 | +@patch('os.environ', {'TEST': 'test'}) |
| 141 | +def test_git_clone_with_auth_auth_failure_from_git(mock_git_auth_input_wrapper): |
| 142 | + """ |
| 143 | + Git internally will throw an error if it is an invalid URL, or if there is a permissions issue. We want to just |
| 144 | + relay it back to the user. |
| 145 | +
|
| 146 | + """ |
| 147 | + # Given |
| 148 | + process_mock = Mock() |
| 149 | + attrs = { |
| 150 | + 'communicate.return_value': "remote: Invalid username or password.\r\nfatal: Authentication failed for 'ghjkhjkl'".encode('utf-8'), |
| 151 | + 'returncode': 128 |
| 152 | + } |
| 153 | + process_mock.configure_mock(**attrs) |
| 154 | + mock_git_auth_input_wrapper.return_value = process_mock |
| 155 | + |
| 156 | + # When |
| 157 | + auth = { |
| 158 | + 'username' : 'asdf', |
| 159 | + 'password' : 'qwerty' |
| 160 | + } |
| 161 | + actual_response = Git(root_dir='/bin').clone(current_path='test_curr_path', repo_url='ghjkhjkl', auth=auth) |
| 162 | + |
| 163 | + # Then |
| 164 | + mock_git_auth_input_wrapper.assert_has_calls([ |
| 165 | + call( |
| 166 | + command = 'git clone ghjkhjkl -q', |
| 167 | + cwd = '/bin/test_curr_path', |
| 168 | + env={'TEST': 'test', 'GIT_TERMINAL_PROMPT': '1'}, |
| 169 | + username = 'asdf', |
| 170 | + password = 'qwerty' |
| 171 | + ), |
| 172 | + call().communicate() |
| 173 | + ]) |
| 174 | + assert {'code': 128, 'message': "remote: Invalid username or password.\r\nfatal: Authentication failed for 'ghjkhjkl'"} == actual_response |
0 commit comments