| 1 | n/a | """Tests for packaging.command.upload.""" |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | from packaging.command.upload import upload |
|---|
| 5 | n/a | from packaging.dist import Distribution |
|---|
| 6 | n/a | from packaging.errors import PackagingOptionError |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | from packaging.tests import unittest, support |
|---|
| 9 | n/a | try: |
|---|
| 10 | n/a | import threading |
|---|
| 11 | n/a | from packaging.tests.pypi_server import PyPIServerTestCase |
|---|
| 12 | n/a | except ImportError: |
|---|
| 13 | n/a | threading = None |
|---|
| 14 | n/a | PyPIServerTestCase = unittest.TestCase |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | PYPIRC_NOPASSWORD = """\ |
|---|
| 18 | n/a | [distutils] |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | index-servers = |
|---|
| 21 | n/a | server1 |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | [server1] |
|---|
| 24 | n/a | username:me |
|---|
| 25 | n/a | """ |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | PYPIRC = """\ |
|---|
| 28 | n/a | [distutils] |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | index-servers = |
|---|
| 31 | n/a | server1 |
|---|
| 32 | n/a | server2 |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | [server1] |
|---|
| 35 | n/a | username:me |
|---|
| 36 | n/a | password:secret |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | [server2] |
|---|
| 39 | n/a | username:meagain |
|---|
| 40 | n/a | password: secret |
|---|
| 41 | n/a | realm:acme |
|---|
| 42 | n/a | repository:http://another.pypi/ |
|---|
| 43 | n/a | """ |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | @unittest.skipIf(threading is None, 'needs threading') |
|---|
| 47 | n/a | class UploadTestCase(support.TempdirManager, support.EnvironRestorer, |
|---|
| 48 | n/a | support.LoggingCatcher, PyPIServerTestCase): |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | restore_environ = ['HOME'] |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def setUp(self): |
|---|
| 53 | n/a | super(UploadTestCase, self).setUp() |
|---|
| 54 | n/a | self.tmp_dir = self.mkdtemp() |
|---|
| 55 | n/a | self.rc = os.path.join(self.tmp_dir, '.pypirc') |
|---|
| 56 | n/a | os.environ['HOME'] = self.tmp_dir |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def test_finalize_options(self): |
|---|
| 59 | n/a | # new format |
|---|
| 60 | n/a | self.write_file(self.rc, PYPIRC) |
|---|
| 61 | n/a | dist = Distribution() |
|---|
| 62 | n/a | cmd = upload(dist) |
|---|
| 63 | n/a | cmd.finalize_options() |
|---|
| 64 | n/a | for attr, expected in (('username', 'me'), ('password', 'secret'), |
|---|
| 65 | n/a | ('realm', 'pypi'), |
|---|
| 66 | n/a | ('repository', 'http://pypi.python.org/pypi')): |
|---|
| 67 | n/a | self.assertEqual(getattr(cmd, attr), expected) |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def test_finalize_options_unsigned_identity_raises_exception(self): |
|---|
| 70 | n/a | self.write_file(self.rc, PYPIRC) |
|---|
| 71 | n/a | dist = Distribution() |
|---|
| 72 | n/a | cmd = upload(dist) |
|---|
| 73 | n/a | cmd.identity = True |
|---|
| 74 | n/a | cmd.sign = False |
|---|
| 75 | n/a | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | def test_saved_password(self): |
|---|
| 78 | n/a | # file with no password |
|---|
| 79 | n/a | self.write_file(self.rc, PYPIRC_NOPASSWORD) |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | # make sure it passes |
|---|
| 82 | n/a | dist = Distribution() |
|---|
| 83 | n/a | cmd = upload(dist) |
|---|
| 84 | n/a | cmd.ensure_finalized() |
|---|
| 85 | n/a | self.assertEqual(cmd.password, None) |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | # make sure we get it as well, if another command |
|---|
| 88 | n/a | # initialized it at the dist level |
|---|
| 89 | n/a | dist.password = 'xxx' |
|---|
| 90 | n/a | cmd = upload(dist) |
|---|
| 91 | n/a | cmd.finalize_options() |
|---|
| 92 | n/a | self.assertEqual(cmd.password, 'xxx') |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | def test_upload_without_files_raises_exception(self): |
|---|
| 95 | n/a | dist = Distribution() |
|---|
| 96 | n/a | cmd = upload(dist) |
|---|
| 97 | n/a | self.assertRaises(PackagingOptionError, cmd.run) |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | def test_upload(self): |
|---|
| 100 | n/a | path = os.path.join(self.tmp_dir, 'xxx') |
|---|
| 101 | n/a | self.write_file(path) |
|---|
| 102 | n/a | command, pyversion, filename = 'xxx', '3.3', path |
|---|
| 103 | n/a | dist_files = [(command, pyversion, filename)] |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | # let's run it |
|---|
| 106 | n/a | dist = self.create_dist(dist_files=dist_files, author='dédé')[1] |
|---|
| 107 | n/a | cmd = upload(dist) |
|---|
| 108 | n/a | cmd.ensure_finalized() |
|---|
| 109 | n/a | cmd.repository = self.pypi.full_address |
|---|
| 110 | n/a | cmd.run() |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | # what did we send? |
|---|
| 113 | n/a | handler, request_data = self.pypi.requests[-1] |
|---|
| 114 | n/a | headers = handler.headers |
|---|
| 115 | n/a | self.assertIn('dédé'.encode('utf-8'), request_data) |
|---|
| 116 | n/a | self.assertIn(b'xxx', request_data) |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | self.assertEqual(int(headers['content-length']), len(request_data)) |
|---|
| 119 | n/a | self.assertLess(int(headers['content-length']), 2500) |
|---|
| 120 | n/a | self.assertTrue(headers['content-type'].startswith( |
|---|
| 121 | n/a | 'multipart/form-data')) |
|---|
| 122 | n/a | self.assertEqual(handler.command, 'POST') |
|---|
| 123 | n/a | self.assertNotIn('\n', headers['authorization']) |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | def test_upload_docs(self): |
|---|
| 126 | n/a | path = os.path.join(self.tmp_dir, 'xxx') |
|---|
| 127 | n/a | self.write_file(path) |
|---|
| 128 | n/a | command, pyversion, filename = 'xxx', '3.3', path |
|---|
| 129 | n/a | dist_files = [(command, pyversion, filename)] |
|---|
| 130 | n/a | docs_path = os.path.join(self.tmp_dir, "build", "docs") |
|---|
| 131 | n/a | os.makedirs(docs_path) |
|---|
| 132 | n/a | self.write_file((docs_path, "index.html"), "yellow") |
|---|
| 133 | n/a | self.write_file(self.rc, PYPIRC) |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | # let's run it |
|---|
| 136 | n/a | dist = self.create_dist(dist_files=dist_files, author='dédé')[1] |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | cmd = upload(dist) |
|---|
| 139 | n/a | cmd.get_finalized_command("build").run() |
|---|
| 140 | n/a | cmd.upload_docs = True |
|---|
| 141 | n/a | cmd.ensure_finalized() |
|---|
| 142 | n/a | cmd.repository = self.pypi.full_address |
|---|
| 143 | n/a | os.chdir(self.tmp_dir) |
|---|
| 144 | n/a | cmd.run() |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | handler, request_data = self.pypi.requests[-1] |
|---|
| 147 | n/a | action, name, content = request_data.split( |
|---|
| 148 | n/a | "----------------GHSKFJDLGDS7543FJKLFHRE75642756743254" |
|---|
| 149 | n/a | .encode())[1:4] |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | self.assertIn(b'name=":action"', action) |
|---|
| 152 | n/a | self.assertIn(b'doc_upload', action) |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | def test_suite(): |
|---|
| 156 | n/a | return unittest.makeSuite(UploadTestCase) |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | if __name__ == "__main__": |
|---|
| 159 | n/a | unittest.main(defaultTest="test_suite") |
|---|