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

View File

@ -146,17 +146,25 @@ class FlaskrTestCase(unittest.TestCase):
headers={'Accept': 'application/json'}
)
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(
'/models/test/secret',
headers={'Accept': 'application/xml'}
)
assert r.status_code == 403, r.status_code
assert r.data == (b'<error><faultcode>Client</faultcode>'
b'<faultstring>403: Forbidden</faultstring>'
b'<debuginfo /></error>')
assert r.data == (b"<error><faultcode>Client</faultcode>"
b"<faultstring>403 Forbidden: You don't have the "
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):
r = self.app.get(
'/models/test/custom-error',

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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