1
"""Tests for distutils.command.upload."""
4
from test.support import run_unittest
6
from distutils.command import upload as upload_mod
7
from distutils.command.upload import upload
8
from distutils.core import Distribution
10
from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
12
PYPIRC_LONG_PASSWORD = """\
21
password:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
27
repository:http://another.pypi/
31
PYPIRC_NOPASSWORD = """\
41
class FakeOpen(object):
43
def __init__(self, url):
45
if not isinstance(url, str):
55
class uploadTestCase(PyPIRCCommandTestCase):
58
super(uploadTestCase, self).setUp()
59
self.old_open = upload_mod.urlopen
60
upload_mod.urlopen = self._urlopen
64
upload_mod.urlopen = self.old_open
65
super(uploadTestCase, self).tearDown()
67
def _urlopen(self, url):
68
self.last_open = FakeOpen(url)
71
def test_finalize_options(self):
74
self.write_file(self.rc, PYPIRC)
77
cmd.finalize_options()
78
for attr, waited in (('username', 'me'), ('password', 'secret'),
80
('repository', 'https://pypi.python.org/pypi')):
81
self.assertEqual(getattr(cmd, attr), waited)
83
def test_saved_password(self):
84
# file with no password
85
self.write_file(self.rc, PYPIRC_NOPASSWORD)
90
cmd.finalize_options()
91
self.assertEqual(cmd.password, None)
93
# make sure we get it as well, if another command
94
# initialized it at the dist level
97
cmd.finalize_options()
98
self.assertEqual(cmd.password, 'xxx')
100
def test_upload(self):
102
path = os.path.join(tmp, 'xxx')
103
self.write_file(path)
104
command, pyversion, filename = 'xxx', '2.6', path
105
dist_files = [(command, pyversion, filename)]
106
self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
109
pkg_dir, dist = self.create_dist(dist_files=dist_files)
111
cmd.ensure_finalized()
115
headers = dict(self.last_open.req.headers)
116
self.assertEqual(headers['Content-length'], '2087')
117
content_type = headers['Content-type']
118
self.assertTrue(content_type.startswith('multipart/form-data'))
119
self.assertEqual(self.last_open.req.get_method(), 'POST')
120
expected_url = 'https://pypi.python.org/pypi'
121
self.assertEqual(self.last_open.req.get_full_url(), expected_url)
122
self.assertTrue(b'xxx' in self.last_open.req.data)
125
return unittest.makeSuite(uploadTestCase)
127
if __name__ == "__main__":
128
run_unittest(test_suite())