mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
more tests for the register command
This commit is contained in:
parent
023862890f
commit
ca2b8d283a
2 changed files with 89 additions and 42 deletions
|
@ -90,14 +90,14 @@ class register(PyPIRCCommand):
|
||||||
''' Fetch the list of classifiers from the server.
|
''' Fetch the list of classifiers from the server.
|
||||||
'''
|
'''
|
||||||
response = urllib2.urlopen(self.repository+'?:action=list_classifiers')
|
response = urllib2.urlopen(self.repository+'?:action=list_classifiers')
|
||||||
print response.read()
|
log.info(response.read())
|
||||||
|
|
||||||
def verify_metadata(self):
|
def verify_metadata(self):
|
||||||
''' Send the metadata to the package index server to be checked.
|
''' Send the metadata to the package index server to be checked.
|
||||||
'''
|
'''
|
||||||
# send the info to the server and report the result
|
# send the info to the server and report the result
|
||||||
(code, result) = self.post_to_server(self.build_post_data('verify'))
|
(code, result) = self.post_to_server(self.build_post_data('verify'))
|
||||||
print 'Server response (%s): %s'%(code, result)
|
log.info('Server response (%s): %s' % (code, result))
|
||||||
|
|
||||||
|
|
||||||
def send_metadata(self):
|
def send_metadata(self):
|
||||||
|
@ -210,17 +210,18 @@ Your selection [default 1]: ''', log.INFO)
|
||||||
data['email'] = raw_input(' EMail: ')
|
data['email'] = raw_input(' EMail: ')
|
||||||
code, result = self.post_to_server(data)
|
code, result = self.post_to_server(data)
|
||||||
if code != 200:
|
if code != 200:
|
||||||
print 'Server response (%s): %s'%(code, result)
|
log.info('Server response (%s): %s' % (code, result))
|
||||||
else:
|
else:
|
||||||
print 'You will receive an email shortly.'
|
log.info('You will receive an email shortly.')
|
||||||
print 'Follow the instructions in it to complete registration.'
|
log.info(('Follow the instructions in it to '
|
||||||
|
'complete registration.'))
|
||||||
elif choice == '3':
|
elif choice == '3':
|
||||||
data = {':action': 'password_reset'}
|
data = {':action': 'password_reset'}
|
||||||
data['email'] = ''
|
data['email'] = ''
|
||||||
while not data['email']:
|
while not data['email']:
|
||||||
data['email'] = raw_input('Your email address: ')
|
data['email'] = raw_input('Your email address: ')
|
||||||
code, result = self.post_to_server(data)
|
code, result = self.post_to_server(data)
|
||||||
print 'Server response (%s): %s'%(code, result)
|
log.info('Server response (%s): %s' % (code, result))
|
||||||
|
|
||||||
def build_post_data(self, action):
|
def build_post_data(self, action):
|
||||||
# figure the data to send - the metadata plus some additional
|
# figure the data to send - the metadata plus some additional
|
||||||
|
@ -253,8 +254,10 @@ Your selection [default 1]: ''', log.INFO)
|
||||||
def post_to_server(self, data, auth=None):
|
def post_to_server(self, data, auth=None):
|
||||||
''' Post a query to the server, and return a string response.
|
''' Post a query to the server, and return a string response.
|
||||||
'''
|
'''
|
||||||
self.announce('Registering %s to %s' % (data['name'],
|
if 'name' in data:
|
||||||
self.repository), log.INFO)
|
self.announce('Registering %s to %s' % (data['name'],
|
||||||
|
self.repository),
|
||||||
|
log.INFO)
|
||||||
# Build up the MIME payload for the urllib2 POST data
|
# Build up the MIME payload for the urllib2 POST data
|
||||||
boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
|
boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
|
||||||
sep_boundary = '\n--' + boundary
|
sep_boundary = '\n--' + boundary
|
||||||
|
@ -301,5 +304,7 @@ Your selection [default 1]: ''', log.INFO)
|
||||||
data = result.read()
|
data = result.read()
|
||||||
result = 200, 'OK'
|
result = 200, 'OK'
|
||||||
if self.show_response:
|
if self.show_response:
|
||||||
print '-'*75, data, '-'*75
|
dashes = '-' * 75
|
||||||
|
self.announce('%s%s%s' % (dashes, data, dashes))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -3,7 +3,9 @@ import sys
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
import getpass
|
import getpass
|
||||||
|
import urllib2
|
||||||
|
|
||||||
|
from distutils.command import register as register_module
|
||||||
from distutils.command.register import register
|
from distutils.command.register import register
|
||||||
from distutils.core import Distribution
|
from distutils.core import Distribution
|
||||||
|
|
||||||
|
@ -42,18 +44,20 @@ class RawInputs(object):
|
||||||
finally:
|
finally:
|
||||||
self.index += 1
|
self.index += 1
|
||||||
|
|
||||||
class FakeServer(object):
|
class FakeOpener(object):
|
||||||
"""Fakes a PyPI server"""
|
"""Fakes a PyPI server"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.calls = []
|
self.reqs = []
|
||||||
|
|
||||||
def __call__(self, *args):
|
def __call__(self, *args):
|
||||||
# we want to compare them, so let's store
|
return self
|
||||||
# something comparable
|
|
||||||
els = args[0].items()
|
def open(self, req):
|
||||||
els.sort()
|
self.reqs.append(req)
|
||||||
self.calls.append(tuple(els))
|
return self
|
||||||
return 200, 'OK'
|
|
||||||
|
def read(self):
|
||||||
|
return 'xxx'
|
||||||
|
|
||||||
class registerTestCase(PyPIRCCommandTestCase):
|
class registerTestCase(PyPIRCCommandTestCase):
|
||||||
|
|
||||||
|
@ -64,24 +68,27 @@ class registerTestCase(PyPIRCCommandTestCase):
|
||||||
def _getpass(prompt):
|
def _getpass(prompt):
|
||||||
return 'password'
|
return 'password'
|
||||||
getpass.getpass = _getpass
|
getpass.getpass = _getpass
|
||||||
|
self.old_opener = urllib2.build_opener
|
||||||
|
self.conn = urllib2.build_opener = FakeOpener()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
getpass.getpass = self._old_getpass
|
getpass.getpass = self._old_getpass
|
||||||
|
urllib2.build_opener = self.old_opener
|
||||||
PyPIRCCommandTestCase.tearDown(self)
|
PyPIRCCommandTestCase.tearDown(self)
|
||||||
|
|
||||||
|
def _get_cmd(self):
|
||||||
|
metadata = {'url': 'xxx', 'author': 'xxx',
|
||||||
|
'author_email': 'xxx',
|
||||||
|
'name': 'xxx', 'version': 'xxx'}
|
||||||
|
pkg_info, dist = self.create_dist(**metadata)
|
||||||
|
return register(dist)
|
||||||
|
|
||||||
def test_create_pypirc(self):
|
def test_create_pypirc(self):
|
||||||
# this test makes sure a .pypirc file
|
# this test makes sure a .pypirc file
|
||||||
# is created when requested.
|
# is created when requested.
|
||||||
|
|
||||||
# let's create a fake distribution
|
# let's create a register instance
|
||||||
# and a register instance
|
cmd = self._get_cmd()
|
||||||
dist = Distribution()
|
|
||||||
dist.metadata.url = 'xxx'
|
|
||||||
dist.metadata.author = 'xxx'
|
|
||||||
dist.metadata.author_email = 'xxx'
|
|
||||||
dist.metadata.name = 'xxx'
|
|
||||||
dist.metadata.version = 'xxx'
|
|
||||||
cmd = register(dist)
|
|
||||||
|
|
||||||
# we shouldn't have a .pypirc file yet
|
# we shouldn't have a .pypirc file yet
|
||||||
self.assert_(not os.path.exists(self.rc))
|
self.assert_(not os.path.exists(self.rc))
|
||||||
|
@ -95,13 +102,12 @@ class registerTestCase(PyPIRCCommandTestCase):
|
||||||
# Password : 'password'
|
# Password : 'password'
|
||||||
# Save your login (y/N)? : 'y'
|
# Save your login (y/N)? : 'y'
|
||||||
inputs = RawInputs('1', 'tarek', 'y')
|
inputs = RawInputs('1', 'tarek', 'y')
|
||||||
from distutils.command import register as register_module
|
|
||||||
register_module.raw_input = inputs.__call__
|
register_module.raw_input = inputs.__call__
|
||||||
|
|
||||||
cmd.post_to_server = pypi_server = FakeServer()
|
|
||||||
|
|
||||||
# let's run the command
|
# let's run the command
|
||||||
cmd.run()
|
try:
|
||||||
|
cmd.run()
|
||||||
|
finally:
|
||||||
|
del register_module.raw_input
|
||||||
|
|
||||||
# we should have a brand new .pypirc file
|
# we should have a brand new .pypirc file
|
||||||
self.assert_(os.path.exists(self.rc))
|
self.assert_(os.path.exists(self.rc))
|
||||||
|
@ -117,30 +123,66 @@ class registerTestCase(PyPIRCCommandTestCase):
|
||||||
raise AssertionError(prompt)
|
raise AssertionError(prompt)
|
||||||
register_module.raw_input = _no_way
|
register_module.raw_input = _no_way
|
||||||
|
|
||||||
|
cmd.show_response = 1
|
||||||
cmd.run()
|
cmd.run()
|
||||||
|
|
||||||
# let's see what the server received : we should
|
# let's see what the server received : we should
|
||||||
# have 2 similar requests
|
# have 2 similar requests
|
||||||
self.assert_(len(pypi_server.calls), 2)
|
self.assert_(self.conn.reqs, 2)
|
||||||
self.assert_(pypi_server.calls[0], pypi_server.calls[1])
|
req1 = dict(self.conn.reqs[0].headers)
|
||||||
|
req2 = dict(self.conn.reqs[1].headers)
|
||||||
|
|
||||||
|
self.assertEquals(req1['Content-length'], '1374')
|
||||||
|
self.assertEquals(req2['Content-length'], '1374')
|
||||||
|
self.assert_('xxx' in self.conn.reqs[1].data)
|
||||||
|
|
||||||
def test_password_not_in_file(self):
|
def test_password_not_in_file(self):
|
||||||
|
|
||||||
f = open(self.rc, 'w')
|
self.write_file(self.rc, PYPIRC_NOPASSWORD)
|
||||||
f.write(PYPIRC_NOPASSWORD)
|
cmd = self._get_cmd()
|
||||||
f.close()
|
|
||||||
|
|
||||||
dist = Distribution()
|
|
||||||
cmd = register(dist)
|
|
||||||
cmd.post_to_server = FakeServer()
|
|
||||||
|
|
||||||
cmd._set_config()
|
cmd._set_config()
|
||||||
cmd.finalize_options()
|
cmd.finalize_options()
|
||||||
cmd.send_metadata()
|
cmd.send_metadata()
|
||||||
|
|
||||||
# dist.password should be set
|
# dist.password should be set
|
||||||
# therefore used afterwards by other commands
|
# therefore used afterwards by other commands
|
||||||
self.assertEquals(dist.password, 'password')
|
self.assertEquals(cmd.distribution.password, 'password')
|
||||||
|
|
||||||
|
def test_registering(self):
|
||||||
|
# this test runs choice 2
|
||||||
|
cmd = self._get_cmd()
|
||||||
|
inputs = RawInputs('2', 'tarek', 'tarek@ziade.org')
|
||||||
|
register_module.raw_input = inputs.__call__
|
||||||
|
try:
|
||||||
|
# let's run the command
|
||||||
|
cmd.run()
|
||||||
|
finally:
|
||||||
|
del register_module.raw_input
|
||||||
|
|
||||||
|
# we should have send a request
|
||||||
|
self.assert_(self.conn.reqs, 1)
|
||||||
|
req = self.conn.reqs[0]
|
||||||
|
headers = dict(req.headers)
|
||||||
|
self.assertEquals(headers['Content-length'], '608')
|
||||||
|
self.assert_('tarek' in req.data)
|
||||||
|
|
||||||
|
def test_password_reset(self):
|
||||||
|
# this test runs choice 3
|
||||||
|
cmd = self._get_cmd()
|
||||||
|
inputs = RawInputs('3', 'tarek@ziade.org')
|
||||||
|
register_module.raw_input = inputs.__call__
|
||||||
|
try:
|
||||||
|
# let's run the command
|
||||||
|
cmd.run()
|
||||||
|
finally:
|
||||||
|
del register_module.raw_input
|
||||||
|
|
||||||
|
# we should have send a request
|
||||||
|
self.assert_(self.conn.reqs, 1)
|
||||||
|
req = self.conn.reqs[0]
|
||||||
|
headers = dict(req.headers)
|
||||||
|
self.assertEquals(headers['Content-length'], '290')
|
||||||
|
self.assert_('tarek' in req.data)
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
return unittest.makeSuite(registerTestCase)
|
return unittest.makeSuite(registerTestCase)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue