Fix (some) tests for modern flask and pep8

Somewhere along the way, WSME and flask/werkzeug have got out of sync
and tests have started failing. Since there aren't regular contributions
to WSME we don't catch these changes, so this may have happened months
or years ago.

I have adjusted tests to attempt to account for what I can, but one test
fails to work so I have marked it as an xfail. It works correctly with
werkzeug 1.13.x but not later. Since WSME is in something worse than
maintenance mode, I'm not inclined to fix this.

pep8/flake8 in python3 is more strict than python. The gate now
runs the pep8 jobs using python3 by default, so the local jobs
should as well. This changes the job and also fixes the new problems it
points out.

There are other failures, but they are present in master as well, so
leaving that for other changes.

Change-Id: I57ae0405e0d6ddba0bb1dac93020fb08a0fc7c89
This commit is contained in:
Chris Dent 2018-04-09 12:44:47 +01:00
parent 9f84e4c7c5
commit 1440eeb13b
15 changed files with 40 additions and 31 deletions

@ -146,17 +146,25 @@ class FlaskrTestCase(unittest.TestCase):
headers={'Accept': 'application/json'} headers={'Accept': 'application/json'}
) )
assert r.status_code == 403, r.status_code assert r.status_code == 403, r.status_code
assert json.loads(r.data)['faultstring'] == '403: Forbidden' assert '403 Forbidden:' in json.loads(r.data)['faultstring']
r = self.app.get( r = self.app.get(
'/models/test/secret', '/models/test/secret',
headers={'Accept': 'application/xml'} headers={'Accept': 'application/xml'}
) )
assert r.status_code == 403, r.status_code assert r.status_code == 403, r.status_code
assert r.data == (b'<error><faultcode>Client</faultcode>' assert r.data == (b"<error><faultcode>Client</faultcode>"
b'<faultstring>403: Forbidden</faultstring>' b"<faultstring>403 Forbidden: You don't have the "
b'<debuginfo /></error>') b"permission to access the requested resource. It "
b"is either read-protected or not readable by the "
b"server."
b"</faultstring><debuginfo /></error>")
# NOTE(cdent): For reasons unclear, 'r' here has no value on data
# even though it does earlier in the stack. If works with Werkzeug
# <0.14.0 but not after. As WSME does not have test-requirement, nor
# pinning, so not a lot to do here.
@unittest.expectedFailure
def test_custom_non_http_clientside_error(self): def test_custom_non_http_clientside_error(self):
r = self.app.get( r = self.app.get(
'/models/test/custom-error', '/models/test/custom-error',

@ -83,6 +83,7 @@ commands =
make clean ziphtml make clean ziphtml
[testenv:pep8] [testenv:pep8]
basepython = python3
deps = flake8 deps = flake8
commands = flake8 wsme wsmeext setup.py commands = flake8 wsme wsmeext setup.py

@ -191,7 +191,7 @@ class WSRoot(object):
try: try:
result = context.func(*args, **kw) result = context.func(*args, **kw)
txn.commit() txn.commit()
except: except Exception:
txn.abort() txn.abort()
raise raise
@ -352,7 +352,7 @@ class WSRoot(object):
try: try:
lexer = get_lexer_for_mimetype(ct) lexer = get_lexer_for_mimetype(ct)
break break
except: except Exception:
pass pass
if lexer is None: if lexer is None:

@ -144,14 +144,14 @@ Value should be one of:"))
class Loop(object): class Loop(object):
pass pass
l = Loop() ell = Loop()
for i in range(0, 21): for i in range(0, 21):
nl = Loop() nl = Loop()
nl.l = l nl.ell = ell
l = nl ell = nl
class MyRoot(WSRoot): class MyRoot(WSRoot):
loop = l loop = ell
r = MyRoot() r = MyRoot()

@ -6,7 +6,7 @@ import wsme.tests.protocol
try: try:
import simplejson as json import simplejson as json
except: except ImportError:
import json # noqa import json # noqa
from wsme.rest.json import fromjson, tojson, parse from wsme.rest.json import fromjson, tojson, parse

@ -13,7 +13,7 @@ from wsme.rest.xml import fromxml, toxml
try: try:
import xml.etree.ElementTree as et import xml.etree.ElementTree as et
except: except ImportError:
import cElementTree as et # noqa import cElementTree as et # noqa

@ -446,8 +446,8 @@ Value: 'v3'. Value should be one of: v., v.",
assert c.s == six.u('test') assert c.s == six.u('test')
def test_array_eq(self): def test_array_eq(self):
l = [types.ArrayType(str)] ell = [types.ArrayType(str)]
assert types.ArrayType(str) in l assert types.ArrayType(str) in ell
def test_array_sample(self): def test_array_sample(self):
s = types.ArrayType(str).sample() s = types.ArrayType(str).sample()

@ -6,7 +6,7 @@ from six.moves import builtins, http_client
try: try:
import dateutil.parser import dateutil.parser
except: except ImportError:
dateutil = None # noqa dateutil = None # noqa
date_re = r'(?P<year>-?\d{4,})-(?P<month>\d{2})-(?P<day>\d{2})' date_re = r'(?P<year>-?\d{4,})-(?P<month>\d{2})-(?P<day>\d{2})'

@ -126,7 +126,7 @@ def signature(*args, **kwargs):
'datatype': funcdef.return_type, 'datatype': funcdef.return_type,
'result': result 'result': result
} }
except: except Exception:
try: try:
exception_info = sys.exc_info() exception_info = sys.exc_info()
orig_exception = exception_info[1] orig_exception = exception_info[1]

@ -85,9 +85,9 @@ def signature(*args, **kw):
) )
res.mimetype = dataformat.content_type res.mimetype = dataformat.content_type
res.status_code = status_code res.status_code = status_code
except: except Exception:
try: try:
exception_info = sys.exc_info() exception_info = sys.exc_info() or None
orig_exception = exception_info[1] orig_exception = exception_info[1]
orig_code = getattr(orig_exception, 'code', None) orig_code = getattr(orig_exception, 'code', None)
data = wsme.api.format_exception(exception_info) data = wsme.api.format_exception(exception_info)
@ -95,10 +95,10 @@ def signature(*args, **kw):
del exception_info del exception_info
res = flask.make_response(dataformat.encode_error(None, data)) res = flask.make_response(dataformat.encode_error(None, data))
if data['faultcode'] == 'client': if orig_code and is_valid_code(orig_code):
res.status_code = 400
elif orig_code and is_valid_code(orig_code):
res.status_code = orig_code res.status_code = orig_code
elif data['faultcode'].lower() == 'client':
res.status_code = 400
else: else:
res.status_code = 500 res.status_code = 500
return res return res

@ -101,7 +101,7 @@ def wsexpose(*args, **kwargs):
result = result.obj result = result.obj
except: except Exception:
try: try:
exception_info = sys.exc_info() exception_info = sys.exc_info()
orig_exception = exception_info[1] orig_exception = exception_info[1]

@ -64,7 +64,7 @@ type_registry = {
} }
if not six.PY3: if not six.PY3:
type_registry[long] = "xs:long" type_registry[long] = "xs:long" # noqa
array_registry = { array_registry = {
wsme.types.text: "String_Array", wsme.types.text: "String_Array",
@ -75,7 +75,7 @@ array_registry = {
} }
if not six.PY3: if not six.PY3:
array_registry[long] = "Long_Array" array_registry[long] = "Long_Array" # noqa
def soap_array(datatype, ns): def soap_array(datatype, ns):
@ -462,7 +462,7 @@ class SoapProtocol(Protocol):
r = self.encoder.make_soap_element(datatype, 'value', value) r = self.encoder.make_soap_element(datatype, 'value', value)
if format: if format:
xml_indent(r) xml_indent(r)
return ('xml', unicode(r)) return ('xml', six.text_type(r))
def xml_indent(elem, level=0): def xml_indent(elem, level=0):

@ -104,7 +104,7 @@ class TestExtDirectProtocol(wsme.tests.protocol.ProtocolTestCase):
try: try:
func, funcdef, args = self.root._lookup_function(path) func, funcdef, args = self.root._lookup_function(path)
arguments = funcdef.arguments arguments = funcdef.arguments
except: except Exception:
arguments = [] arguments = []
if len(path) == 1: if len(path) == 1:
ns, action, fname = '', '', path[0] ns, action, fname = '', '', path[0]

@ -8,7 +8,7 @@ import wsme.tests.protocol
try: try:
import xml.etree.ElementTree as et import xml.etree.ElementTree as et
except: except ImportError:
import cElementTree as et # noqa import cElementTree as et # noqa
import suds.cache import suds.cache
@ -78,7 +78,7 @@ class SudsCache(suds.cache.Cache):
def purge(self, id): def purge(self, id):
try: try:
del self.d[id] del self.d[id]
except: except KeyError:
pass pass
def clear(self, id): def clear(self, id):
@ -148,7 +148,7 @@ array_types = {
} }
if not six.PY3: if not six.PY3:
array_types[long] = "Long_Array" array_types[long] = "Long_Array" # noqa
def tosoap(tag, value): def tosoap(tag, value):
@ -206,7 +206,7 @@ def read_bool(value):
soap_types = { soap_types = {
'xs:string': wsme.types.text, 'xs:string': wsme.types.text,
'xs:int': int, 'xs:int': int,
'xs:long': int if six.PY3 else long, 'xs:long': int if six.PY3 else long, # noqa
'xs:float': float, 'xs:float': float,
'xs:decimal': decimal.Decimal, 'xs:decimal': decimal.Decimal,
'xs:boolean': read_bool, 'xs:boolean': read_bool,

@ -61,7 +61,7 @@ def wsexpose(*args, **kwargs):
kwargs[funcdef.pass_request] = cherrypy.request kwargs[funcdef.pass_request] = cherrypy.request
try: try:
result = f(self, *args, **kwargs) result = f(self, *args, **kwargs)
except: except Exception:
try: try:
exception_info = sys.exc_info() exception_info = sys.exc_info()
orig_exception = exception_info[1] orig_exception = exception_info[1]