Home | History | Annotate | Download | only in tests
      1 # -*- encoding: utf8 -*-
      2 """Tests for distutils.command.upload."""
      3 import os
      4 import unittest
      5 from test.test_support import run_unittest
      6 
      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 
     12 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
     13 
     14 PYPIRC_LONG_PASSWORD = """\
     15 [distutils]
     16 
     17 index-servers =
     18     server1
     19     server2
     20 
     21 [server1]
     22 username:me
     23 password:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
     24 
     25 [server2]
     26 username:meagain
     27 password: secret
     28 realm:acme
     29 repository:http://another.pypi/
     30 """
     31 
     32 
     33 PYPIRC_NOPASSWORD = """\
     34 [distutils]
     35 
     36 index-servers =
     37     server1
     38 
     39 [server1]
     40 username:me
     41 """
     42 
     43 class FakeOpen(object):
     44 
     45     def __init__(self, url, msg=None, code=None):
     46         self.url = url
     47         if not isinstance(url, str):
     48             self.req = url
     49         else:
     50             self.req = None
     51         self.msg = msg or 'OK'
     52         self.code = code or 200
     53 
     54     def getcode(self):
     55         return self.code
     56 
     57 
     58 class uploadTestCase(PyPIRCCommandTestCase):
     59 
     60     def setUp(self):
     61         super(uploadTestCase, self).setUp()
     62         self.old_open = upload_mod.urlopen
     63         upload_mod.urlopen = self._urlopen
     64         self.last_open = None
     65         self.next_msg = None
     66         self.next_code = None
     67 
     68     def tearDown(self):
     69         upload_mod.urlopen = self.old_open
     70         super(uploadTestCase, self).tearDown()
     71 
     72     def _urlopen(self, url):
     73         self.last_open = FakeOpen(url, msg=self.next_msg, code=self.next_code)
     74         return self.last_open
     75 
     76     def test_finalize_options(self):
     77 
     78         # new format
     79         self.write_file(self.rc, PYPIRC)
     80         dist = Distribution()
     81         cmd = upload(dist)
     82         cmd.finalize_options()
     83         for attr, waited in (('username', 'me'), ('password', 'secret'),
     84                              ('realm', 'pypi'),
     85                              ('repository', 'https://upload.pypi.org/legacy/')):
     86             self.assertEqual(getattr(cmd, attr), waited)
     87 
     88     def test_saved_password(self):
     89         # file with no password
     90         self.write_file(self.rc, PYPIRC_NOPASSWORD)
     91 
     92         # make sure it passes
     93         dist = Distribution()
     94         cmd = upload(dist)
     95         cmd.finalize_options()
     96         self.assertEqual(cmd.password, None)
     97 
     98         # make sure we get it as well, if another command
     99         # initialized it at the dist level
    100         dist.password = 'xxx'
    101         cmd = upload(dist)
    102         cmd.finalize_options()
    103         self.assertEqual(cmd.password, 'xxx')
    104 
    105     def test_upload(self):
    106         tmp = self.mkdtemp()
    107         path = os.path.join(tmp, 'xxx')
    108         self.write_file(path)
    109         command, pyversion, filename = 'xxx', '2.6', path
    110         dist_files = [(command, pyversion, filename)]
    111         self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
    112 
    113         # lets run it
    114         pkg_dir, dist = self.create_dist(dist_files=dist_files, author=u'dd')
    115         cmd = upload(dist)
    116         cmd.ensure_finalized()
    117         cmd.run()
    118 
    119         # what did we send ?
    120         self.assertIn('dd', self.last_open.req.data)
    121         headers = dict(self.last_open.req.headers)
    122         self.assertEqual(headers['Content-length'], '2159')
    123         self.assertTrue(headers['Content-type'].startswith('multipart/form-data'))
    124         self.assertEqual(self.last_open.req.get_method(), 'POST')
    125         self.assertEqual(self.last_open.req.get_full_url(),
    126                          'https://upload.pypi.org/legacy/')
    127         self.assertIn('xxx', self.last_open.req.data)
    128         auth = self.last_open.req.headers['Authorization']
    129         self.assertNotIn('\n', auth)
    130 
    131     def test_upload_fails(self):
    132         self.next_msg = "Not Found"
    133         self.next_code = 404
    134         self.assertRaises(DistutilsError, self.test_upload)
    135 
    136 def test_suite():
    137     return unittest.makeSuite(uploadTestCase)
    138 
    139 if __name__ == "__main__":
    140     run_unittest(test_suite())
    141