-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`vcs_base.load_url()` currently doesn't support authentication. Add support for both basic and token-based authentication by parsing netrc files. Signed-off-by: Kyle Fazzari <[email protected]>
- Loading branch information
Showing
3 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
||
try: | ||
from urllib.error import HTTPError | ||
except ImportError: | ||
from urllib2 import HTTPError | ||
|
||
|
||
try: | ||
from unittest import mock | ||
except ImportError: | ||
import mock | ||
|
||
from vcstool.clients import vcs_base | ||
|
||
|
||
class TestBase(unittest.TestCase): | ||
|
||
@mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) | ||
@mock.patch('vcstool.clients.vcs_base._netrc_open', autospec=True) | ||
def test_load_url_calls_urlopen(self, netrc_open_mock, urlopen_mock): | ||
urlopen_read_mock = urlopen_mock.return_value.read | ||
|
||
vcs_base.load_url('example.com', timeout=123) | ||
|
||
urlopen_mock.assert_called_once_with('example.com', timeout=123) | ||
urlopen_read_mock.assert_called_once_with() | ||
self.assertFalse(netrc_open_mock.mock_calls) | ||
|
||
@mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) | ||
@mock.patch('vcstool.clients.vcs_base._netrc_open', autospec=True) | ||
def test_load_url_calls_netrc_open(self, netrc_open_mock, urlopen_mock): | ||
for code in (401, 404): | ||
urlopen_mock.side_effect = HTTPError(None, code, None, None, None) | ||
urlopen_read_mock = urlopen_mock.return_value.read | ||
|
||
vcs_base.load_url('example.com', timeout=123) | ||
|
||
urlopen_mock.assert_called_once_with('example.com', timeout=123) | ||
self.assertFalse(urlopen_read_mock.mock_calls) | ||
|
||
netrc_open_mock.assert_called_once_with('example.com', timeout=123) | ||
|
||
netrc_open_mock.reset_mock() | ||
urlopen_mock.reset_mock() | ||
|
||
def test_netrc_open_no_such_file(self): | ||
try: | ||
self.assertEqual(vcs_base._netrc_open( | ||
'https://example.com', filename='/non-existent'), None) | ||
except Exception: | ||
self.fail( | ||
'The lack of a .netrc file should not result in an exception') | ||
|
||
@mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) | ||
@mock.patch('vcstool.clients.vcs_base.build_opener', autospec=True) | ||
def test_netrc_open_basic_auth(self, build_opener_mock, urlopen_mock): | ||
open_mock = build_opener_mock.return_value.open | ||
|
||
tmpdir = tempfile.mkdtemp() | ||
netrc_file = os.path.join(tmpdir, 'netrc') | ||
machine = 'example.com' | ||
with open(netrc_file, 'w') as f: | ||
f.write('machine %s\n' % machine) | ||
f.write('login username\n') | ||
f.write('password password') | ||
|
||
url = 'https://%s/foo/bar' % machine | ||
try: | ||
vcs_base._netrc_open(url, filename=netrc_file, timeout=123) | ||
finally: | ||
shutil.rmtree(tmpdir) | ||
|
||
self.assertFalse(urlopen_mock.mock_calls) | ||
|
||
class _HTTPBasicAuthHandlerMatcher(object): | ||
def __init__(self, test): | ||
self.test = test | ||
|
||
def __eq__(self, other): | ||
manager = other.passwd | ||
self.test.assertEqual( | ||
manager.find_user_password(None, 'example.com'), | ||
('username', 'password')) | ||
return True | ||
|
||
build_opener_mock.assert_called_once_with( | ||
_HTTPBasicAuthHandlerMatcher(self)) | ||
open_mock.assert_called_once_with(url, timeout=123) | ||
|
||
@mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) | ||
@mock.patch('vcstool.clients.vcs_base.build_opener', autospec=True) | ||
def test_netrc_open_token_auth(self, build_opener_mock, urlopen_mock): | ||
tmpdir = tempfile.mkdtemp() | ||
netrc_file = os.path.join(tmpdir, 'netrc') | ||
machine = 'example.com' | ||
with open(netrc_file, 'w') as f: | ||
f.write('machine %s\n' % machine) | ||
f.write('password password') | ||
|
||
url = 'https://%s/foo/bar' % machine | ||
try: | ||
vcs_base._netrc_open(url, filename=netrc_file, timeout=123) | ||
finally: | ||
shutil.rmtree(tmpdir) | ||
|
||
self.assertFalse(build_opener_mock.mock_calls) | ||
|
||
class _RequestMatcher(object): | ||
def __init__(self, test): | ||
self.test = test | ||
|
||
def __eq__(self, other): | ||
self.test.assertEqual(other.get_full_url(), url) | ||
self.test.assertEqual( | ||
other.get_header('Private-token'), 'password') | ||
return True | ||
|
||
urlopen_mock.assert_called_once_with( | ||
_RequestMatcher(self), timeout=123) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters