more tests for the register command

This commit is contained in:
Tarek Ziadé 2009-03-31 20:53:13 +00:00
parent 023862890f
commit ca2b8d283a
2 changed files with 89 additions and 42 deletions

View File

@ -90,14 +90,14 @@ class register(PyPIRCCommand):
''' Fetch the list of classifiers from the server.
'''
response = urllib2.urlopen(self.repository+'?:action=list_classifiers')
print response.read()
log.info(response.read())
def verify_metadata(self):
''' Send the metadata to the package index server to be checked.
'''
# send the info to the server and report the result
(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):
@ -210,17 +210,18 @@ Your selection [default 1]: ''', log.INFO)
data['email'] = raw_input(' EMail: ')
code, result = self.post_to_server(data)
if code != 200:
print 'Server response (%s): %s'%(code, result)
log.info('Server response (%s): %s' % (code, result))
else:
print 'You will receive an email shortly.'
print 'Follow the instructions in it to complete registration.'
log.info('You will receive an email shortly.')
log.info(('Follow the instructions in it to '
'complete registration.'))
elif choice == '3':
data = {':action': 'password_reset'}
data['email'] = ''
while not data['email']:
data['email'] = raw_input('Your email address: ')
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):
# 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):
''' Post a query to the server, and return a string response.
'''
self.announce('Registering %s to %s' % (data['name'],
self.repository), log.INFO)
if 'name' in data:
self.announce('Registering %s to %s' % (data['name'],
self.repository),
log.INFO)
# Build up the MIME payload for the urllib2 POST data
boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
sep_boundary = '\n--' + boundary
@ -301,5 +304,7 @@ Your selection [default 1]: ''', log.INFO)
data = result.read()
result = 200, 'OK'
if self.show_response:
print '-'*75, data, '-'*75
dashes = '-' * 75
self.announce('%s%s%s' % (dashes, data, dashes))
return result

View File

@ -3,7 +3,9 @@ import sys
import os
import unittest
import getpass
import urllib2
from distutils.command import register as register_module
from distutils.command.register import register
from distutils.core import Distribution
@ -42,18 +44,20 @@ class RawInputs(object):
finally:
self.index += 1
class FakeServer(object):
class FakeOpener(object):
"""Fakes a PyPI server"""
def __init__(self):
self.calls = []
self.reqs = []
def __call__(self, *args):
# we want to compare them, so let's store
# something comparable
els = args[0].items()
els.sort()
self.calls.append(tuple(els))
return 200, 'OK'
return self
def open(self, req):
self.reqs.append(req)
return self
def read(self):
return 'xxx'
class registerTestCase(PyPIRCCommandTestCase):
@ -64,24 +68,27 @@ class registerTestCase(PyPIRCCommandTestCase):
def _getpass(prompt):
return 'password'
getpass.getpass = _getpass
self.old_opener = urllib2.build_opener
self.conn = urllib2.build_opener = FakeOpener()
def tearDown(self):
getpass.getpass = self._old_getpass
urllib2.build_opener = self.old_opener
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):
# this test makes sure a .pypirc file
# is created when requested.
# let's create a fake distribution
# and a register instance
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)
# let's create a register instance
cmd = self._get_cmd()
# we shouldn't have a .pypirc file yet
self.assert_(not os.path.exists(self.rc))
@ -95,13 +102,12 @@ class registerTestCase(PyPIRCCommandTestCase):
# Password : 'password'
# Save your login (y/N)? : 'y'
inputs = RawInputs('1', 'tarek', 'y')
from distutils.command import register as register_module
register_module.raw_input = inputs.__call__
cmd.post_to_server = pypi_server = FakeServer()
# let's run the command
cmd.run()
try:
cmd.run()
finally:
del register_module.raw_input
# we should have a brand new .pypirc file
self.assert_(os.path.exists(self.rc))
@ -117,30 +123,66 @@ class registerTestCase(PyPIRCCommandTestCase):
raise AssertionError(prompt)
register_module.raw_input = _no_way
cmd.show_response = 1
cmd.run()
# let's see what the server received : we should
# have 2 similar requests
self.assert_(len(pypi_server.calls), 2)
self.assert_(pypi_server.calls[0], pypi_server.calls[1])
self.assert_(self.conn.reqs, 2)
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):
f = open(self.rc, 'w')
f.write(PYPIRC_NOPASSWORD)
f.close()
dist = Distribution()
cmd = register(dist)
cmd.post_to_server = FakeServer()
self.write_file(self.rc, PYPIRC_NOPASSWORD)
cmd = self._get_cmd()
cmd._set_config()
cmd.finalize_options()
cmd.send_metadata()
# dist.password should be set
# 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():
return unittest.makeSuite(registerTestCase)