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