test_upload.py 7.0 KB

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