test_upload.py 3.74 KB
Newer Older
1 2 3
"""Tests for distutils.command.upload."""
import os
import unittest
4
from test.support import run_unittest
5

6
from distutils.command import upload as upload_mod
7 8
from distutils.command.upload import upload
from distutils.core import Distribution
9
from distutils.log import INFO
10 11 12

from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
PYPIRC_LONG_PASSWORD = """\
[distutils]

index-servers =
    server1
    server2

[server1]
username:me
password:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

[server2]
username:meagain
password: secret
realm:acme
repository:http://another.pypi/
"""


32 33 34 35 36 37 38 39 40 41
PYPIRC_NOPASSWORD = """\
[distutils]

index-servers =
    server1

[server1]
username:me
"""

42
class FakeOpen(object):
43

44 45 46 47 48 49 50
    def __init__(self, url):
        self.url = url
        if not isinstance(url, str):
            self.req = url
        else:
            self.req = None
        self.msg = 'OK'
51

52 53 54 55 56 57 58 59
    def getheader(self, name, default=None):
        return {
            'content-type': 'text/plain; charset=utf-8',
            }.get(name.lower(), default)

    def read(self):
        return b'xyzzy'

60 61
    def getcode(self):
        return 200
62

63

64 65
class uploadTestCase(PyPIRCCommandTestCase):

66 67
    def setUp(self):
        super(uploadTestCase, self).setUp()
68 69 70
        self.old_open = upload_mod.urlopen
        upload_mod.urlopen = self._urlopen
        self.last_open = None
71 72

    def tearDown(self):
73
        upload_mod.urlopen = self.old_open
74 75
        super(uploadTestCase, self).tearDown()

76 77 78
    def _urlopen(self, url):
        self.last_open = FakeOpen(url)
        return self.last_open
79

80 81 82
    def test_finalize_options(self):

        # new format
83
        self.write_file(self.rc, PYPIRC)
84 85 86 87 88
        dist = Distribution()
        cmd = upload(dist)
        cmd.finalize_options()
        for attr, waited in (('username', 'me'), ('password', 'secret'),
                             ('realm', 'pypi'),
89
                             ('repository', 'https://pypi.python.org/pypi')):
90
            self.assertEqual(getattr(cmd, attr), waited)
91

92 93
    def test_saved_password(self):
        # file with no password
94
        self.write_file(self.rc, PYPIRC_NOPASSWORD)
95 96 97 98 99

        # make sure it passes
        dist = Distribution()
        cmd = upload(dist)
        cmd.finalize_options()
100
        self.assertEqual(cmd.password, None)
101 102 103 104 105 106

        # make sure we get it as well, if another command
        # initialized it at the dist level
        dist.password = 'xxx'
        cmd = upload(dist)
        cmd.finalize_options()
107
        self.assertEqual(cmd.password, 'xxx')
108

109 110 111 112 113 114
    def test_upload(self):
        tmp = self.mkdtemp()
        path = os.path.join(tmp, 'xxx')
        self.write_file(path)
        command, pyversion, filename = 'xxx', '2.6', path
        dist_files = [(command, pyversion, filename)]
115
        self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
116 117 118 119

        # lets run it
        pkg_dir, dist = self.create_dist(dist_files=dist_files)
        cmd = upload(dist)
120
        cmd.show_response = 1
121 122 123
        cmd.ensure_finalized()
        cmd.run()

124
        # what did we send ?
125
        headers = dict(self.last_open.req.headers)
126
        self.assertEqual(headers['Content-length'], '2087')
Jason R. Coombs's avatar
Jason R. Coombs committed
127 128
        content_type = headers['Content-type']
        self.assertTrue(content_type.startswith('multipart/form-data'))
129 130 131
        self.assertEqual(self.last_open.req.get_method(), 'POST')
        expected_url = 'https://pypi.python.org/pypi'
        self.assertEqual(self.last_open.req.get_full_url(), expected_url)
132
        self.assertTrue(b'xxx' in self.last_open.req.data)
133

134 135 136 137 138
        # The PyPI response body was echoed
        results = self.get_logs(INFO)
        self.assertIn('xyzzy\n', results[-1])


139 140 141 142
def test_suite():
    return unittest.makeSuite(uploadTestCase)

if __name__ == "__main__":
143
    run_unittest(test_suite())