Commit 6f68c843 authored by Tres Seaver's avatar Tres Seaver

Remove module-scope imports from unit tests.

parent 64afeb22
import sys
import base64
import unittest import unittest
from urllib import quote_plus
from types import ListType, TupleType, StringType, UnicodeType
from StringIO import StringIO
from DateTime import DateTime
from ZPublisher.HTTPRequest import HTTPRequest, record, trusted_proxies
from ZPublisher.TaintedString import TaintedString
from ZPublisher.Converters import type_converters
TEST_LARGEFILE_DATA = ''' TEST_LARGEFILE_DATA = '''
--12345 --12345
...@@ -22,9 +12,11 @@ test %s ...@@ -22,9 +12,11 @@ test %s
class AuthCredentialsTests( unittest.TestCase ): class AuthCredentialsTests( unittest.TestCase ):
def _getTargetClass(self): def _getTargetClass(self):
from ZPublisher.HTTPRequest import HTTPRequest
return HTTPRequest return HTTPRequest
def _makeOne(self, stdin=None, environ=None, response=None, clean=1): def _makeOne(self, stdin=None, environ=None, response=None, clean=1):
from StringIO import StringIO
if stdin is None: if stdin is None:
stdin = StringIO() stdin = StringIO()
...@@ -46,6 +38,7 @@ class AuthCredentialsTests( unittest.TestCase ): ...@@ -46,6 +38,7 @@ class AuthCredentialsTests( unittest.TestCase ):
return self._getTargetClass()(stdin, environ, response, clean) return self._getTargetClass()(stdin, environ, response, clean)
def test__authUserPW_simple( self ): def test__authUserPW_simple( self ):
import base64
user_id = 'user' user_id = 'user'
password = 'password' password = 'password'
encoded = base64.encodestring( '%s:%s' % ( user_id, password ) ) encoded = base64.encodestring( '%s:%s' % ( user_id, password ) )
...@@ -61,6 +54,7 @@ class AuthCredentialsTests( unittest.TestCase ): ...@@ -61,6 +54,7 @@ class AuthCredentialsTests( unittest.TestCase ):
def test__authUserPW_with_embedded_colon( self ): def test__authUserPW_with_embedded_colon( self ):
# http://www.zope.org/Collectors/Zope/2039 # http://www.zope.org/Collectors/Zope/2039
import base64
user_id = 'user' user_id = 'user'
password = 'embedded:colon' password = 'embedded:colon'
encoded = base64.encodestring( '%s:%s' % ( user_id, password ) ) encoded = base64.encodestring( '%s:%s' % ( user_id, password ) )
...@@ -78,6 +72,7 @@ class AuthCredentialsTests( unittest.TestCase ): ...@@ -78,6 +72,7 @@ class AuthCredentialsTests( unittest.TestCase ):
class RecordTests(unittest.TestCase): class RecordTests(unittest.TestCase):
def test_repr(self): def test_repr(self):
from ZPublisher.HTTPRequest import record
rec = record() rec = record()
rec.a = 1 rec.a = 1
rec.b = 'foo' rec.b = 'foo'
...@@ -87,10 +82,16 @@ class RecordTests(unittest.TestCase): ...@@ -87,10 +82,16 @@ class RecordTests(unittest.TestCase):
class ProcessInputsTests(unittest.TestCase): class ProcessInputsTests(unittest.TestCase):
def _getHTTPRequest(self, env):
return HTTPRequest(None, env, None) def _getTargetClass(self):
from ZPublisher.HTTPRequest import HTTPRequest
return HTTPRequest
def _makeOne(self, environ):
return self._getTargetClass()(None, environ, None)
def _processInputs(self, inputs): def _processInputs(self, inputs):
from urllib import quote_plus
# Have the inputs processed, and return a HTTPRequest object holding the # Have the inputs processed, and return a HTTPRequest object holding the
# result. # result.
# inputs is expected to be a list of (key, value) tuples, no CGI # inputs is expected to be a list of (key, value) tuples, no CGI
...@@ -104,7 +105,7 @@ class ProcessInputsTests(unittest.TestCase): ...@@ -104,7 +105,7 @@ class ProcessInputsTests(unittest.TestCase):
env = {'SERVER_NAME': 'testingharnas', 'SERVER_PORT': '80'} env = {'SERVER_NAME': 'testingharnas', 'SERVER_PORT': '80'}
env['QUERY_STRING'] = query_string env['QUERY_STRING'] = query_string
req = self._getHTTPRequest(env) req = self._makeOne(env)
req.processInputs() req.processInputs()
self._noFormValuesInOther(req) self._noFormValuesInOther(req)
return req return req
...@@ -117,6 +118,8 @@ class ProcessInputsTests(unittest.TestCase): ...@@ -117,6 +118,8 @@ class ProcessInputsTests(unittest.TestCase):
# when one is found. # when one is found.
# Also raises an Assertion if a string which *should* have been # Also raises an Assertion if a string which *should* have been
# tainted is found, or when a tainted string is not deemed dangerous. # tainted is found, or when a tainted string is not deemed dangerous.
from ZPublisher.HTTPRequest import record
from ZPublisher.TaintedString import TaintedString
retval = 0 retval = 0
...@@ -132,12 +135,12 @@ class ProcessInputsTests(unittest.TestCase): ...@@ -132,12 +135,12 @@ class ProcessInputsTests(unittest.TestCase):
rval = self._valueIsOrHoldsTainted(value) rval = self._valueIsOrHoldsTainted(value)
if rval: retval = 1 if rval: retval = 1
elif type(val) in (ListType, TupleType): elif type(val) in (list, tuple):
for entry in val: for entry in val:
rval = self._valueIsOrHoldsTainted(entry) rval = self._valueIsOrHoldsTainted(entry)
if rval: retval = 1 if rval: retval = 1
elif type(val) in (StringType, UnicodeType): elif type(val) in (str, unicode):
self.failIf('<' in val, self.failIf('<' in val,
"'%s' is dangerous and should have been tainted." % val) "'%s' is dangerous and should have been tainted." % val)
...@@ -194,6 +197,7 @@ class ProcessInputsTests(unittest.TestCase): ...@@ -194,6 +197,7 @@ class ProcessInputsTests(unittest.TestCase):
self._onlyTaintedformHoldsTaintedStrings(req) self._onlyTaintedformHoldsTaintedStrings(req)
def testSimpleMarshalling(self): def testSimpleMarshalling(self):
from DateTime.DateTime import DateTime
inputs = ( inputs = (
('num:int', '42'), ('fract:float', '4.2'), ('bign:long', '45'), ('num:int', '42'), ('fract:float', '4.2'), ('bign:long', '45'),
('words:string', 'Some words'), ('2tokens:tokens', 'one two'), ('words:string', 'Some words'), ('2tokens:tokens', 'one two'),
...@@ -601,6 +605,8 @@ class ProcessInputsTests(unittest.TestCase): ...@@ -601,6 +605,8 @@ class ProcessInputsTests(unittest.TestCase):
def testNoTaintedExceptions(self): def testNoTaintedExceptions(self):
# Feed tainted garbage to the conversion methods, and any exception # Feed tainted garbage to the conversion methods, and any exception
# returned should be HTML safe # returned should be HTML safe
from DateTime.DateTime import DateTime
from ZPublisher.Converters import type_converters
for type, convert in type_converters.items(): for type, convert in type_converters.items():
try: try:
convert('<html garbage>') convert('<html garbage>')
...@@ -630,18 +636,18 @@ class ProcessInputsTests(unittest.TestCase): ...@@ -630,18 +636,18 @@ class ProcessInputsTests(unittest.TestCase):
env = {'SERVER_NAME': 'testingharnas', 'SERVER_PORT': '80'} env = {'SERVER_NAME': 'testingharnas', 'SERVER_PORT': '80'}
env['HTTP_COOKIE'] = 'foo=bar; baz=gee' env['HTTP_COOKIE'] = 'foo=bar; baz=gee'
req = self._getHTTPRequest(env) req = self._makeOne(env)
self.assertEquals(req.cookies['foo'], 'bar') self.assertEquals(req.cookies['foo'], 'bar')
self.assertEquals(req.cookies['baz'], 'gee') self.assertEquals(req.cookies['baz'], 'gee')
env['HTTP_COOKIE'] = 'foo=bar; baz="gee, like, e=mc^2"' env['HTTP_COOKIE'] = 'foo=bar; baz="gee, like, e=mc^2"'
req = self._getHTTPRequest(env) req = self._makeOne(env)
self.assertEquals(req.cookies['foo'], 'bar') self.assertEquals(req.cookies['foo'], 'bar')
self.assertEquals(req.cookies['baz'], 'gee, like, e=mc^2') self.assertEquals(req.cookies['baz'], 'gee, like, e=mc^2')
# Collector #1498: empty cookies # Collector #1498: empty cookies
env['HTTP_COOKIE'] = 'foo=bar; hmm; baz=gee' env['HTTP_COOKIE'] = 'foo=bar; hmm; baz=gee'
req = self._getHTTPRequest(env) req = self._makeOne(env)
self.assertEquals(req.cookies['foo'], 'bar') self.assertEquals(req.cookies['foo'], 'bar')
self.assertEquals(req.cookies['hmm'], '') self.assertEquals(req.cookies['hmm'], '')
self.assertEquals(req.cookies['baz'], 'gee') self.assertEquals(req.cookies['baz'], 'gee')
...@@ -651,7 +657,7 @@ class ProcessInputsTests(unittest.TestCase): ...@@ -651,7 +657,7 @@ class ProcessInputsTests(unittest.TestCase):
'quoted="cookie data with unquoted spaces"; ' \ 'quoted="cookie data with unquoted spaces"; ' \
'multi=cookie data with unquoted spaces; ' \ 'multi=cookie data with unquoted spaces; ' \
'multi2=cookie data with unquoted spaces' 'multi2=cookie data with unquoted spaces'
req = self._getHTTPRequest(env) req = self._makeOne(env)
self.assertEquals(req.cookies['single'], 'cookie data') self.assertEquals(req.cookies['single'], 'cookie data')
self.assertEquals(req.cookies['quoted'], self.assertEquals(req.cookies['quoted'],
'cookie data with unquoted spaces') 'cookie data with unquoted spaces')
...@@ -680,14 +686,23 @@ test ...@@ -680,14 +686,23 @@ test
class RequestTests( unittest.TestCase ): class RequestTests( unittest.TestCase ):
def _getTargetClass(self):
from ZPublisher.HTTPRequest import HTTPRequest
return HTTPRequest
def _makeOne(self, stdin, environ, response, clean=0):
return self._getTargetClass()(stdin, environ, response, clean)
def testRemoveStdinReferences(self): def testRemoveStdinReferences(self):
# Verifies that all references to the input stream go away on # Verifies that all references to the input stream go away on
# request.close(). Otherwise a tempfile may stick around. # request.close(). Otherwise a tempfile may stick around.
import sys
from StringIO import StringIO
s = StringIO(TEST_FILE_DATA) s = StringIO(TEST_FILE_DATA)
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
start_count = sys.getrefcount(s) start_count = sys.getrefcount(s)
req = HTTPRequest(s, env, None) req = self._makeOne(s, env, None)
req.processInputs() req.processInputs()
self.assertNotEqual(start_count, sys.getrefcount(s)) # Precondition self.assertNotEqual(start_count, sys.getrefcount(s)) # Precondition
req.close() req.close()
...@@ -695,10 +710,11 @@ class RequestTests( unittest.TestCase ): ...@@ -695,10 +710,11 @@ class RequestTests( unittest.TestCase ):
def testFileName(self): def testFileName(self):
# checks fileupload object supports the filename # checks fileupload object supports the filename
from StringIO import StringIO
s = StringIO(TEST_LARGEFILE_DATA) s = StringIO(TEST_LARGEFILE_DATA)
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
req = HTTPRequest(s, env, None) req = self._makeOne(s, env, None)
req.processInputs() req.processInputs()
f = req.form.get('file') f = req.form.get('file')
self.assert_(f.name) self.assert_(f.name)
...@@ -706,10 +722,11 @@ class RequestTests( unittest.TestCase ): ...@@ -706,10 +722,11 @@ class RequestTests( unittest.TestCase ):
def testFileIterator(self): def testFileIterator(self):
# checks fileupload object supports the iterator protocol # checks fileupload object supports the iterator protocol
# collector entry 1837 # collector entry 1837
from StringIO import StringIO
s = StringIO(TEST_FILE_DATA) s = StringIO(TEST_FILE_DATA)
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
req = HTTPRequest(s, env, None) req = self._makeOne(s, env, None)
req.processInputs() req.processInputs()
f=req.form.get('file') f=req.form.get('file')
self.assertEqual(list(f),['test\n']) self.assertEqual(list(f),['test\n'])
...@@ -719,17 +736,18 @@ class RequestTests( unittest.TestCase ): ...@@ -719,17 +736,18 @@ class RequestTests( unittest.TestCase ):
self.assertEqual(f.xreadlines(),f) self.assertEqual(f.xreadlines(),f)
def testDebug(self): def testDebug(self):
from zope.publisher.base import DebugFlags
from StringIO import StringIO
TEST_ENVIRON = { TEST_ENVIRON = {
'REQUEST_METHOD': 'GET', 'REQUEST_METHOD': 'GET',
'SERVER_NAME': 'localhost', 'SERVER_NAME': 'localhost',
'SERVER_PORT': '80', 'SERVER_PORT': '80',
} }
from zope.publisher.base import DebugFlags
s = StringIO('') s = StringIO('')
# when accessing request.debug we will see the DebugFlags instance # when accessing request.debug we will see the DebugFlags instance
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assert_(isinstance(request.debug, DebugFlags)) self.assert_(isinstance(request.debug, DebugFlags))
# It won't be available through dictonary lookup, though # It won't be available through dictonary lookup, though
self.assert_(request.get('debug') is None) self.assert_(request.get('debug') is None)
...@@ -738,7 +756,7 @@ class RequestTests( unittest.TestCase ): ...@@ -738,7 +756,7 @@ class RequestTests( unittest.TestCase ):
# if it exists # if it exists
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
env['QUERY_STRING'] = 'debug=1' env['QUERY_STRING'] = 'debug=1'
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
request.processInputs() request.processInputs()
self.assertEqual(request.debug, '1') self.assertEqual(request.debug, '1')
self.assertEqual(request.get('debug'), '1') self.assertEqual(request.get('debug'), '1')
...@@ -746,7 +764,7 @@ class RequestTests( unittest.TestCase ): ...@@ -746,7 +764,7 @@ class RequestTests( unittest.TestCase ):
# we can still override request.debug with a form variable or directly # we can still override request.debug with a form variable or directly
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
request.processInputs() request.processInputs()
self.assert_(isinstance(request.debug, DebugFlags)) self.assert_(isinstance(request.debug, DebugFlags))
request.form['debug'] = '1' request.form['debug'] = '1'
...@@ -777,7 +795,7 @@ class RequestTests( unittest.TestCase ): ...@@ -777,7 +795,7 @@ class RequestTests( unittest.TestCase ):
# is still a marker # is still a marker
from ZPublisher.HTTPRequest import _marker from ZPublisher.HTTPRequest import _marker
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assert_(request._locale is _marker) self.assert_(request._locale is _marker)
# when accessing request.locale we will see an ILocale # when accessing request.locale we will see an ILocale
self.assert_(ILocale.providedBy(request.locale)) self.assert_(ILocale.providedBy(request.locale))
...@@ -790,7 +808,7 @@ class RequestTests( unittest.TestCase ): ...@@ -790,7 +808,7 @@ class RequestTests( unittest.TestCase ):
# if it exists # if it exists
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
env['QUERY_STRING'] = 'locale=1' env['QUERY_STRING'] = 'locale=1'
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
request.processInputs() request.processInputs()
self.assertEqual(request.locale, '1') self.assertEqual(request.locale, '1')
self.assertEqual(request.get('locale'), '1') self.assertEqual(request.get('locale'), '1')
...@@ -798,7 +816,7 @@ class RequestTests( unittest.TestCase ): ...@@ -798,7 +816,7 @@ class RequestTests( unittest.TestCase ):
# we can still override request.locale with a form variable # we can still override request.locale with a form variable
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
request.processInputs() request.processInputs()
self.assert_(ILocale.providedBy(request.locale)) self.assert_(ILocale.providedBy(request.locale))
request.form['locale'] = '1' request.form['locale'] = '1'
...@@ -810,7 +828,7 @@ class RequestTests( unittest.TestCase ): ...@@ -810,7 +828,7 @@ class RequestTests( unittest.TestCase ):
for httplang in ('it', 'it-ch', 'it-CH', 'IT', 'IT-CH', 'IT-ch'): for httplang in ('it', 'it-ch', 'it-CH', 'IT', 'IT-CH', 'IT-ch'):
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
env['HTTP_ACCEPT_LANGUAGE'] = httplang env['HTTP_ACCEPT_LANGUAGE'] = httplang
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
locale = request.locale locale = request.locale
self.assert_(ILocale.providedBy(locale)) self.assert_(ILocale.providedBy(locale))
parts = httplang.split('-') parts = httplang.split('-')
...@@ -827,7 +845,7 @@ class RequestTests( unittest.TestCase ): ...@@ -827,7 +845,7 @@ class RequestTests( unittest.TestCase ):
# Now test for non-existant locale fallback # Now test for non-existant locale fallback
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
env['HTTP_ACCEPT_LANGUAGE'] = 'xx' env['HTTP_ACCEPT_LANGUAGE'] = 'xx'
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
locale = request.locale locale = request.locale
self.assert_(ILocale.providedBy(locale)) self.assert_(ILocale.providedBy(locale))
self.assert_(locale.id.language is None) self.assert_(locale.id.language is None)
...@@ -835,6 +853,7 @@ class RequestTests( unittest.TestCase ): ...@@ -835,6 +853,7 @@ class RequestTests( unittest.TestCase ):
self.assert_(locale.id.variant is None) self.assert_(locale.id.variant is None)
def testMethod(self): def testMethod(self):
from StringIO import StringIO
TEST_ENVIRON = { TEST_ENVIRON = {
'REQUEST_METHOD': 'GET', 'REQUEST_METHOD': 'GET',
'SERVER_NAME': 'localhost', 'SERVER_NAME': 'localhost',
...@@ -843,15 +862,17 @@ class RequestTests( unittest.TestCase ): ...@@ -843,15 +862,17 @@ class RequestTests( unittest.TestCase ):
s = StringIO('') s = StringIO('')
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assertEqual(request.method, 'GET') self.assertEqual(request.method, 'GET')
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
env['REQUEST_METHOD'] = 'post' env['REQUEST_METHOD'] = 'post'
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assertEqual(request.method, 'POST') self.assertEqual(request.method, 'POST')
def testTrustedProxies(self): def testTrustedProxies(self):
from StringIO import StringIO
from ZPublisher.HTTPRequest import trusted_proxies
TEST_ENVIRON = { TEST_ENVIRON = {
'REQUEST_METHOD': 'GET', 'REQUEST_METHOD': 'GET',
'SERVER_NAME': 'localhost', 'SERVER_NAME': 'localhost',
...@@ -862,28 +883,29 @@ class RequestTests( unittest.TestCase ): ...@@ -862,28 +883,29 @@ class RequestTests( unittest.TestCase ):
s = StringIO('') s = StringIO('')
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assertEqual(request.getClientAddr(), '127.0.0.1') self.assertEqual(request.getClientAddr(), '127.0.0.1')
trusted_proxies.append('127.0.0.1') trusted_proxies.append('127.0.0.1')
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assertEqual(request.getClientAddr(), '192.168.1.100') self.assertEqual(request.getClientAddr(), '192.168.1.100')
trusted_proxies[0] = '192.168.1.100' trusted_proxies[0] = '192.168.1.100'
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
env['REMOTE_ADDR'] = '192.168.1.100' env['REMOTE_ADDR'] = '192.168.1.100'
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assertEqual(request.getClientAddr(), '10.1.20.30') self.assertEqual(request.getClientAddr(), '10.1.20.30')
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
del env['REMOTE_ADDR'] del env['REMOTE_ADDR']
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assertEqual(request.getClientAddr(), '') self.assertEqual(request.getClientAddr(), '')
def testGetHeader(self): def testGetHeader(self):
from StringIO import StringIO
s = StringIO('') s = StringIO('')
env = TEST_ENVIRON.copy() env = TEST_ENVIRON.copy()
request = HTTPRequest(s, env, None) request = self._makeOne(s, env, None)
self.assertEqual(request.getHeader('Content-Type'), self.assertEqual(request.getHeader('Content-Type'),
'multipart/form-data; boundary=12345') 'multipart/form-data; boundary=12345')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment