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 
     11 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
     12 
     13 PYPIRC_LONG_PASSWORD = """\
     14 [distutils]
     15 
     16 index-servers =
     17     server1
     18     server2
     19 
     20 [server1]
     21 username:me
     22 password:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
     23 
     24 [server2]
     25 username:meagain
     26 password: secret
     27 realm:acme
     28 repository:http://another.pypi/
     29 """
     30 
     31 
     32 PYPIRC_NOPASSWORD = """\
     33 [distutils]
     34 
     35 index-servers =
     36     server1
     37 
     38 [server1]
     39 username:me
     40 """
     41 
     42 class FakeOpen(object):
     43 
     44     def __init__(self, url):
     45         self.url = url
     46         if not isinstance(url, str):
     47             self.req = url
     48         else:
     49             self.req = None
     50         self.msg = 'OK'
     51 
     52     def getcode(self):
     53         return 200
     54 
     55 
     56 class uploadTestCase(PyPIRCCommandTestCase):
     57 
     58     def setUp(self):
     59         super(uploadTestCase, self).setUp()
     60         self.old_open = upload_mod.urlopen
     61         upload_mod.urlopen = self._urlopen
     62         self.last_open = None
     63 
     64     def tearDown(self):
     65         upload_mod.urlopen = self.old_open
     66         super(uploadTestCase, self).tearDown()
     67 
     68     def _urlopen(self, url):
     69         self.last_open = FakeOpen(url)
     70         return self.last_open
     71 
     72     def test_finalize_options(self):
     73 
     74         # new format

     75         self.write_file(self.rc, PYPIRC)
     76         dist = Distribution()
     77         cmd = upload(dist)
     78         cmd.finalize_options()
     79         for attr, waited in (('username', 'me'), ('password', 'secret'),
     80                              ('realm', 'pypi'),
     81                              ('repository', 'http://pypi.python.org/pypi')):
     82             self.assertEqual(getattr(cmd, attr), waited)
     83 
     84     def test_saved_password(self):
     85         # file with no password

     86         self.write_file(self.rc, PYPIRC_NOPASSWORD)
     87 
     88         # make sure it passes

     89         dist = Distribution()
     90         cmd = upload(dist)
     91         cmd.finalize_options()
     92         self.assertEqual(cmd.password, None)
     93 
     94         # make sure we get it as well, if another command

     95         # initialized it at the dist level

     96         dist.password = 'xxx'
     97         cmd = upload(dist)
     98         cmd.finalize_options()
     99         self.assertEqual(cmd.password, 'xxx')
    100 
    101     def test_upload(self):
    102         tmp = self.mkdtemp()
    103         path = os.path.join(tmp, 'xxx')
    104         self.write_file(path)
    105         command, pyversion, filename = 'xxx', '2.6', path
    106         dist_files = [(command, pyversion, filename)]
    107         self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
    108 
    109         # lets run it

    110         pkg_dir, dist = self.create_dist(dist_files=dist_files, author=u'dd')
    111         cmd = upload(dist)
    112         cmd.ensure_finalized()
    113         cmd.run()
    114 
    115         # what did we send ?

    116         self.assertIn('dd', self.last_open.req.data)
    117         headers = dict(self.last_open.req.headers)
    118         self.assertEqual(headers['Content-length'], '2085')
    119         self.assertTrue(headers['Content-type'].startswith('multipart/form-data'))
    120         self.assertEqual(self.last_open.req.get_method(), 'POST')
    121         self.assertEqual(self.last_open.req.get_full_url(),
    122                          'http://pypi.python.org/pypi')
    123         self.assertTrue('xxx' in self.last_open.req.data)
    124         auth = self.last_open.req.headers['Authorization']
    125         self.assertFalse('\n' in auth)
    126 
    127 def test_suite():
    128     return unittest.makeSuite(uploadTestCase)
    129 
    130 if __name__ == "__main__":
    131     run_unittest(test_suite())
    132