summaryrefslogtreecommitdiffstats
path: root/Lib/distutils/tests/test_upload.py
blob: 95e4ac3315335b562e41b870c3091459160136c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Tests for distutils.command.upload."""
import sys
import os
import unittest
import http.client as httpclient

from distutils.command.upload import upload
from distutils.core import Distribution

from distutils.tests import support
from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase

PYPIRC_NOPASSWORD = """\
[distutils]

index-servers =
    server1

[server1]
username:me
"""
class Response(object):
    def __init__(self, status=200, reason='OK'):
        self.status = status
        self.reason = reason

class FakeConnection(object):

    def __init__(self):
        self.requests = []
        self.headers = []
        self.body = ''

    def __call__(self, netloc):
        return self

    def connect(self):
        pass
    endheaders = connect

    def putrequest(self, method, url):
        self.requests.append((method, url))

    def putheader(self, name, value):
        self.headers.append((name, value))

    def send(self, body):
        self.body = body

    def getresponse(self):
        return Response()

class uploadTestCase(PyPIRCCommandTestCase):

    def setUp(self):
        super(uploadTestCase, self).setUp()
        self.old_class = httpclient.HTTPConnection
        self.conn = httpclient.HTTPConnection = FakeConnection()

    def tearDown(self):
        httpclient.HTTPConnection = self.old_class
        super(uploadTestCase, self).tearDown()

    def test_finalize_options(self):

        # new format
        self.write_file(self.rc, PYPIRC)
        dist = Distribution()
        cmd = upload(dist)
        cmd.finalize_options()
        for attr, waited in (('username', 'me'), ('password', 'secret'),
                             ('realm', 'pypi'),
                             ('repository', 'http://pypi.python.org/pypi')):
            self.assertEquals(getattr(cmd, attr), waited)

    def test_saved_password(self):
        # file with no password
        self.write_file(self.rc, PYPIRC_NOPASSWORD)

        # make sure it passes
        dist = Distribution()
        cmd = upload(dist)
        cmd.finalize_options()
        self.assertEquals(cmd.password, None)

        # 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()
        self.assertEquals(cmd.password, 'xxx')

    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)]
        self.write_file(self.rc, PYPIRC)

        # lets run it
        pkg_dir, dist = self.create_dist(dist_files=dist_files)
        cmd = upload(dist)
        cmd.ensure_finalized()
        cmd.run()

        # what did we send ?
        headers = dict(self.conn.headers)
        self.assertEquals(headers['Content-length'], '2087')
        self.assert_(headers['Content-type'].startswith('multipart/form-data'))

        self.assertEquals(self.conn.requests, [('POST', '/pypi')])
        self.assert_((b'xxx') in self.conn.body)

def test_suite():
    return unittest.makeSuite(uploadTestCase)

if __name__ == "__main__":
    unittest.main(defaultTest="test_suite")