|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | + |
| 6 | +try: |
| 7 | + from urllib.error import HTTPError |
| 8 | +except ImportError: |
| 9 | + from urllib2 import HTTPError |
| 10 | + |
| 11 | + |
| 12 | +try: |
| 13 | + from unittest import mock |
| 14 | +except ImportError: |
| 15 | + import mock |
| 16 | + |
| 17 | +from vcstool.clients import vcs_base |
| 18 | + |
| 19 | + |
| 20 | +class TestBase(unittest.TestCase): |
| 21 | + |
| 22 | + @mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) |
| 23 | + @mock.patch('vcstool.clients.vcs_base._netrc_open', autospec=True) |
| 24 | + def test_load_url_calls_urlopen(self, netrc_open_mock, urlopen_mock): |
| 25 | + urlopen_read_mock = urlopen_mock.return_value.read |
| 26 | + |
| 27 | + vcs_base.load_url('example.com', timeout=123) |
| 28 | + |
| 29 | + urlopen_mock.assert_called_once_with('example.com', timeout=123) |
| 30 | + urlopen_read_mock.assert_called_once_with() |
| 31 | + self.assertFalse(netrc_open_mock.mock_calls) |
| 32 | + |
| 33 | + @mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) |
| 34 | + @mock.patch('vcstool.clients.vcs_base._netrc_open', autospec=True) |
| 35 | + def test_load_url_calls_netrc_open(self, netrc_open_mock, urlopen_mock): |
| 36 | + for code in (401, 404): |
| 37 | + urlopen_mock.side_effect = [ |
| 38 | + HTTPError(None, code, None, None, None)] |
| 39 | + urlopen_read_mock = urlopen_mock.return_value.read |
| 40 | + |
| 41 | + vcs_base.load_url('example.com', timeout=123) |
| 42 | + |
| 43 | + urlopen_mock.assert_called_once_with('example.com', timeout=123) |
| 44 | + self.assertFalse(urlopen_read_mock.mock_calls) |
| 45 | + |
| 46 | + netrc_open_mock.assert_called_once_with('example.com', timeout=123) |
| 47 | + |
| 48 | + netrc_open_mock.reset_mock() |
| 49 | + urlopen_mock.reset_mock() |
| 50 | + |
| 51 | + def test_netrc_open_no_such_file(self): |
| 52 | + try: |
| 53 | + self.assertEqual(vcs_base._netrc_open( |
| 54 | + 'https://example.com', filename='/non-existent'), None) |
| 55 | + except Exception: |
| 56 | + self.fail( |
| 57 | + 'The lack of a .netrc file should not result in an exception') |
| 58 | + |
| 59 | + @mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) |
| 60 | + @mock.patch('vcstool.clients.vcs_base.build_opener', autospec=True) |
| 61 | + def test_netrc_open_basic_auth(self, build_opener_mock, urlopen_mock): |
| 62 | + open_mock = build_opener_mock.return_value.open |
| 63 | + |
| 64 | + tmpdir = tempfile.mkdtemp() |
| 65 | + netrc_file = os.path.join(tmpdir, 'netrc') |
| 66 | + machine = 'example.com' |
| 67 | + with open(netrc_file, 'w') as f: |
| 68 | + f.write('machine %s\n' % machine) |
| 69 | + f.write('login username\n') |
| 70 | + f.write('password password') |
| 71 | + |
| 72 | + url = 'https://%s/foo/bar' % machine |
| 73 | + try: |
| 74 | + vcs_base._netrc_open(url, filename=netrc_file, timeout=123) |
| 75 | + finally: |
| 76 | + shutil.rmtree(tmpdir) |
| 77 | + |
| 78 | + self.assertFalse(urlopen_mock.mock_calls) |
| 79 | + |
| 80 | + class _HTTPBasicAuthHandlerMatcher(object): |
| 81 | + def __init__(self, test): |
| 82 | + self.test = test |
| 83 | + |
| 84 | + def __eq__(self, other): |
| 85 | + manager = other.passwd |
| 86 | + self.test.assertEqual( |
| 87 | + manager.find_user_password(None, 'example.com'), |
| 88 | + ('username', 'password')) |
| 89 | + return True |
| 90 | + |
| 91 | + build_opener_mock.assert_called_once_with( |
| 92 | + _HTTPBasicAuthHandlerMatcher(self)) |
| 93 | + open_mock.assert_called_once_with(url, timeout=123) |
| 94 | + |
| 95 | + @mock.patch('vcstool.clients.vcs_base.urlopen', autospec=True) |
| 96 | + @mock.patch('vcstool.clients.vcs_base.build_opener', autospec=True) |
| 97 | + def test_netrc_open_token_auth(self, build_opener_mock, urlopen_mock): |
| 98 | + tmpdir = tempfile.mkdtemp() |
| 99 | + netrc_file = os.path.join(tmpdir, 'netrc') |
| 100 | + machine = 'example.com' |
| 101 | + with open(netrc_file, 'w') as f: |
| 102 | + f.write('machine %s\n' % machine) |
| 103 | + f.write('password password') |
| 104 | + |
| 105 | + url = 'https://%s/foo/bar' % machine |
| 106 | + try: |
| 107 | + vcs_base._netrc_open(url, filename=netrc_file, timeout=123) |
| 108 | + finally: |
| 109 | + shutil.rmtree(tmpdir) |
| 110 | + |
| 111 | + self.assertFalse(build_opener_mock.mock_calls) |
| 112 | + |
| 113 | + class _RequestMatcher(object): |
| 114 | + def __init__(self, test): |
| 115 | + self.test = test |
| 116 | + |
| 117 | + def __eq__(self, other): |
| 118 | + self.test.assertEqual(other.get_full_url(), url) |
| 119 | + self.test.assertEqual( |
| 120 | + other.get_header('Private-token'), 'password') |
| 121 | + return True |
| 122 | + |
| 123 | + urlopen_mock.assert_called_once_with( |
| 124 | + _RequestMatcher(self), timeout=123) |
0 commit comments