~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/distutils/tests/test_upload.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for distutils.command.upload."""
 
2
import os
 
3
import unittest
 
4
from test.support import run_unittest
 
5
 
 
6
from distutils.command import upload as upload_mod
 
7
from distutils.command.upload import upload
 
8
from distutils.core import Distribution
 
9
 
 
10
from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
 
11
 
 
12
PYPIRC_LONG_PASSWORD = """\
 
13
[distutils]
 
14
 
 
15
index-servers =
 
16
    server1
 
17
    server2
 
18
 
 
19
[server1]
 
20
username:me
 
21
password:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
22
 
 
23
[server2]
 
24
username:meagain
 
25
password: secret
 
26
realm:acme
 
27
repository:http://another.pypi/
 
28
"""
 
29
 
 
30
 
 
31
PYPIRC_NOPASSWORD = """\
 
32
[distutils]
 
33
 
 
34
index-servers =
 
35
    server1
 
36
 
 
37
[server1]
 
38
username:me
 
39
"""
 
40
 
 
41
class FakeOpen(object):
 
42
 
 
43
    def __init__(self, url):
 
44
        self.url = url
 
45
        if not isinstance(url, str):
 
46
            self.req = url
 
47
        else:
 
48
            self.req = None
 
49
        self.msg = 'OK'
 
50
 
 
51
    def getcode(self):
 
52
        return 200
 
53
 
 
54
 
 
55
class uploadTestCase(PyPIRCCommandTestCase):
 
56
 
 
57
    def setUp(self):
 
58
        super(uploadTestCase, self).setUp()
 
59
        self.old_open = upload_mod.urlopen
 
60
        upload_mod.urlopen = self._urlopen
 
61
        self.last_open = None
 
62
 
 
63
    def tearDown(self):
 
64
        upload_mod.urlopen = self.old_open
 
65
        super(uploadTestCase, self).tearDown()
 
66
 
 
67
    def _urlopen(self, url):
 
68
        self.last_open = FakeOpen(url)
 
69
        return self.last_open
 
70
 
 
71
    def test_finalize_options(self):
 
72
 
 
73
        # new format
 
74
        self.write_file(self.rc, PYPIRC)
 
75
        dist = Distribution()
 
76
        cmd = upload(dist)
 
77
        cmd.finalize_options()
 
78
        for attr, waited in (('username', 'me'), ('password', 'secret'),
 
79
                             ('realm', 'pypi'),
 
80
                             ('repository', 'https://pypi.python.org/pypi')):
 
81
            self.assertEqual(getattr(cmd, attr), waited)
 
82
 
 
83
    def test_saved_password(self):
 
84
        # file with no password
 
85
        self.write_file(self.rc, PYPIRC_NOPASSWORD)
 
86
 
 
87
        # make sure it passes
 
88
        dist = Distribution()
 
89
        cmd = upload(dist)
 
90
        cmd.finalize_options()
 
91
        self.assertEqual(cmd.password, None)
 
92
 
 
93
        # make sure we get it as well, if another command
 
94
        # initialized it at the dist level
 
95
        dist.password = 'xxx'
 
96
        cmd = upload(dist)
 
97
        cmd.finalize_options()
 
98
        self.assertEqual(cmd.password, 'xxx')
 
99
 
 
100
    def test_upload(self):
 
101
        tmp = self.mkdtemp()
 
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)
 
107
 
 
108
        # lets run it
 
109
        pkg_dir, dist = self.create_dist(dist_files=dist_files)
 
110
        cmd = upload(dist)
 
111
        cmd.ensure_finalized()
 
112
        cmd.run()
 
113
 
 
114
         # what did we send ?
 
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)
 
123
 
 
124
def test_suite():
 
125
    return unittest.makeSuite(uploadTestCase)
 
126
 
 
127
if __name__ == "__main__":
 
128
    run_unittest(test_suite())