| 1 | n/a | """Tests for distutils.command.upload.""" |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | import unittest |
|---|
| 4 | n/a | import unittest.mock as mock |
|---|
| 5 | n/a | from urllib.request import HTTPError |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from test.support import run_unittest |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | from distutils.command import upload as upload_mod |
|---|
| 10 | n/a | from distutils.command.upload import upload |
|---|
| 11 | n/a | from distutils.core import Distribution |
|---|
| 12 | n/a | from distutils.errors import DistutilsError |
|---|
| 13 | n/a | from distutils.log import ERROR, INFO |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | from distutils.tests.test_config import PYPIRC, BasePyPIRCCommandTestCase |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | PYPIRC_LONG_PASSWORD = """\ |
|---|
| 18 | n/a | [distutils] |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | index-servers = |
|---|
| 21 | n/a | server1 |
|---|
| 22 | n/a | server2 |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | [server1] |
|---|
| 25 | n/a | username:me |
|---|
| 26 | n/a | password:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | [server2] |
|---|
| 29 | n/a | username:meagain |
|---|
| 30 | n/a | password: secret |
|---|
| 31 | n/a | realm:acme |
|---|
| 32 | n/a | repository:http://another.pypi/ |
|---|
| 33 | n/a | """ |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | PYPIRC_NOPASSWORD = """\ |
|---|
| 37 | n/a | [distutils] |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | index-servers = |
|---|
| 40 | n/a | server1 |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | [server1] |
|---|
| 43 | n/a | username:me |
|---|
| 44 | n/a | """ |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | class FakeOpen(object): |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | def __init__(self, url, msg=None, code=None): |
|---|
| 49 | n/a | self.url = url |
|---|
| 50 | n/a | if not isinstance(url, str): |
|---|
| 51 | n/a | self.req = url |
|---|
| 52 | n/a | else: |
|---|
| 53 | n/a | self.req = None |
|---|
| 54 | n/a | self.msg = msg or 'OK' |
|---|
| 55 | n/a | self.code = code or 200 |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def getheader(self, name, default=None): |
|---|
| 58 | n/a | return { |
|---|
| 59 | n/a | 'content-type': 'text/plain; charset=utf-8', |
|---|
| 60 | n/a | }.get(name.lower(), default) |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | def read(self): |
|---|
| 63 | n/a | return b'xyzzy' |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def getcode(self): |
|---|
| 66 | n/a | return self.code |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | class uploadTestCase(BasePyPIRCCommandTestCase): |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def setUp(self): |
|---|
| 72 | n/a | super(uploadTestCase, self).setUp() |
|---|
| 73 | n/a | self.old_open = upload_mod.urlopen |
|---|
| 74 | n/a | upload_mod.urlopen = self._urlopen |
|---|
| 75 | n/a | self.last_open = None |
|---|
| 76 | n/a | self.next_msg = None |
|---|
| 77 | n/a | self.next_code = None |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def tearDown(self): |
|---|
| 80 | n/a | upload_mod.urlopen = self.old_open |
|---|
| 81 | n/a | super(uploadTestCase, self).tearDown() |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def _urlopen(self, url): |
|---|
| 84 | n/a | self.last_open = FakeOpen(url, msg=self.next_msg, code=self.next_code) |
|---|
| 85 | n/a | return self.last_open |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | def test_finalize_options(self): |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # new format |
|---|
| 90 | n/a | self.write_file(self.rc, PYPIRC) |
|---|
| 91 | n/a | dist = Distribution() |
|---|
| 92 | n/a | cmd = upload(dist) |
|---|
| 93 | n/a | cmd.finalize_options() |
|---|
| 94 | n/a | for attr, waited in (('username', 'me'), ('password', 'secret'), |
|---|
| 95 | n/a | ('realm', 'pypi'), |
|---|
| 96 | n/a | ('repository', 'https://upload.pypi.org/legacy/')): |
|---|
| 97 | n/a | self.assertEqual(getattr(cmd, attr), waited) |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | def test_saved_password(self): |
|---|
| 100 | n/a | # file with no password |
|---|
| 101 | n/a | self.write_file(self.rc, PYPIRC_NOPASSWORD) |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | # make sure it passes |
|---|
| 104 | n/a | dist = Distribution() |
|---|
| 105 | n/a | cmd = upload(dist) |
|---|
| 106 | n/a | cmd.finalize_options() |
|---|
| 107 | n/a | self.assertEqual(cmd.password, None) |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | # make sure we get it as well, if another command |
|---|
| 110 | n/a | # initialized it at the dist level |
|---|
| 111 | n/a | dist.password = 'xxx' |
|---|
| 112 | n/a | cmd = upload(dist) |
|---|
| 113 | n/a | cmd.finalize_options() |
|---|
| 114 | n/a | self.assertEqual(cmd.password, 'xxx') |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | def test_upload(self): |
|---|
| 117 | n/a | tmp = self.mkdtemp() |
|---|
| 118 | n/a | path = os.path.join(tmp, 'xxx') |
|---|
| 119 | n/a | self.write_file(path) |
|---|
| 120 | n/a | command, pyversion, filename = 'xxx', '2.6', path |
|---|
| 121 | n/a | dist_files = [(command, pyversion, filename)] |
|---|
| 122 | n/a | self.write_file(self.rc, PYPIRC_LONG_PASSWORD) |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | # lets run it |
|---|
| 125 | n/a | pkg_dir, dist = self.create_dist(dist_files=dist_files) |
|---|
| 126 | n/a | cmd = upload(dist) |
|---|
| 127 | n/a | cmd.show_response = 1 |
|---|
| 128 | n/a | cmd.ensure_finalized() |
|---|
| 129 | n/a | cmd.run() |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | # what did we send ? |
|---|
| 132 | n/a | headers = dict(self.last_open.req.headers) |
|---|
| 133 | n/a | self.assertEqual(headers['Content-length'], '2162') |
|---|
| 134 | n/a | content_type = headers['Content-type'] |
|---|
| 135 | n/a | self.assertTrue(content_type.startswith('multipart/form-data')) |
|---|
| 136 | n/a | self.assertEqual(self.last_open.req.get_method(), 'POST') |
|---|
| 137 | n/a | expected_url = 'https://upload.pypi.org/legacy/' |
|---|
| 138 | n/a | self.assertEqual(self.last_open.req.get_full_url(), expected_url) |
|---|
| 139 | n/a | self.assertTrue(b'xxx' in self.last_open.req.data) |
|---|
| 140 | n/a | self.assertIn(b'protocol_version', self.last_open.req.data) |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | # The PyPI response body was echoed |
|---|
| 143 | n/a | results = self.get_logs(INFO) |
|---|
| 144 | n/a | self.assertEqual(results[-1], 75 * '-' + '\nxyzzy\n' + 75 * '-') |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | def test_upload_fails(self): |
|---|
| 147 | n/a | self.next_msg = "Not Found" |
|---|
| 148 | n/a | self.next_code = 404 |
|---|
| 149 | n/a | self.assertRaises(DistutilsError, self.test_upload) |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | def test_wrong_exception_order(self): |
|---|
| 152 | n/a | tmp = self.mkdtemp() |
|---|
| 153 | n/a | path = os.path.join(tmp, 'xxx') |
|---|
| 154 | n/a | self.write_file(path) |
|---|
| 155 | n/a | dist_files = [('xxx', '2.6', path)] # command, pyversion, filename |
|---|
| 156 | n/a | self.write_file(self.rc, PYPIRC_LONG_PASSWORD) |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | pkg_dir, dist = self.create_dist(dist_files=dist_files) |
|---|
| 159 | n/a | tests = [ |
|---|
| 160 | n/a | (OSError('oserror'), 'oserror', OSError), |
|---|
| 161 | n/a | (HTTPError('url', 400, 'httperror', {}, None), |
|---|
| 162 | n/a | 'Upload failed (400): httperror', DistutilsError), |
|---|
| 163 | n/a | ] |
|---|
| 164 | n/a | for exception, expected, raised_exception in tests: |
|---|
| 165 | n/a | with self.subTest(exception=type(exception).__name__): |
|---|
| 166 | n/a | with mock.patch('distutils.command.upload.urlopen', |
|---|
| 167 | n/a | new=mock.Mock(side_effect=exception)): |
|---|
| 168 | n/a | with self.assertRaises(raised_exception): |
|---|
| 169 | n/a | cmd = upload(dist) |
|---|
| 170 | n/a | cmd.ensure_finalized() |
|---|
| 171 | n/a | cmd.run() |
|---|
| 172 | n/a | results = self.get_logs(ERROR) |
|---|
| 173 | n/a | self.assertIn(expected, results[-1]) |
|---|
| 174 | n/a | self.clear_logs() |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | def test_suite(): |
|---|
| 178 | n/a | return unittest.makeSuite(uploadTestCase) |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | if __name__ == "__main__": |
|---|
| 181 | n/a | run_unittest(test_suite()) |
|---|