Skip to content

Commit 41dfb2c

Browse files
committed
test: Unit tests for git clone with auth on the backend
1 parent 16110c6 commit 41dfb2c

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

jupyterlab_git/tests/test_clone.py

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from mock import patch, call, Mock
44

5-
from jupyterlab_git.git import Git
5+
from jupyterlab_git.git import Git, git_auth_input_wrapper
66

77

88
@patch('subprocess.Popen')
@@ -66,3 +66,109 @@ def test_git_clone_failure_from_git(mock_subproc_popen):
6666
call().communicate()
6767
])
6868
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

Comments
 (0)