use Twisted API, and some hypothesis tests

This commit is contained in:
meejah 2019-09-05 16:48:08 -06:00
parent fc32d1e377
commit 4a226c7633
2 changed files with 35 additions and 10 deletions

View File

@ -9,10 +9,16 @@ from ...util.connection_status import ConnectionStatus
from allmydata.web.root import URIHandler from allmydata.web.root import URIHandler
from allmydata.web.common import WebError from allmydata.web.common import WebError
from hypothesis import given
from hypothesis.strategies import text
# remove nevow imports when we use twisted.web.Site instead of nevow
# for the base.
from nevow.inevow import IRequest from nevow.inevow import IRequest
from zope.interface import directlyProvides from zope.interface import directlyProvides
class FakeRoot(Root): class FakeRoot(Root):
def __init__(self): def __init__(self):
pass pass
@ -40,14 +46,19 @@ class FakeField(object):
class RenderSlashUri(unittest.TestCase): class RenderSlashUri(unittest.TestCase):
""" """
Ensure that URI's starting with /uri?uri= only accept valid Ensure that URIs starting with /uri?uri= only accept valid
capabilities capabilities
""" """
def setUp(self): def setUp(self):
self.request = DummyRequest("/uri") self.request = DummyRequest("/uri")
self.request.fields = {} self.request.fields = {}
self.request.prePathURL = lambda: "http://127.0.0.1.99999/{}".format("/".join(self.request.prepath))
def prepathURL():
return "http://127.0.0.1.99999/{}".format(
"/".join(self.request.prepath)
)
self.request.prePathURL = prepathURL
directlyProvides(self.request, IRequest) directlyProvides(self.request, IRequest)
self.client = Mock() self.client = Mock()
self.res = URIHandler(self.client) self.res = URIHandler(self.client)
@ -56,19 +67,33 @@ class RenderSlashUri(unittest.TestCase):
""" """
A valid capbility does not result in error A valid capbility does not result in error
""" """
self.request.fields["uri"] = FakeField( self.request.args[b"uri"] = [(
value=( b"URI:CHK:nt2xxmrccp7sursd6yh2thhcky:"
"URI:CHK:nt2xxmrccp7sursd6yh2thhcky:" b"mukesarwdjxiyqsjinbfiiro6q7kgmmekocxfjcngh23oxwyxtzq:2:5:5874882"
"mukesarwdjxiyqsjinbfiiro6q7kgmmekocxfjcngh23oxwyxtzq:2:5:5874882" )]
)
)
self.res.render_GET(self.request) self.res.render_GET(self.request)
def test_invalid(self): def test_invalid(self):
""" """
A (trivially) invalid capbility is an error A (trivially) invalid capbility is an error
""" """
self.request.fields["uri"] = FakeField(value="not a capability") self.request.args[b"uri"] = [b"not a capability"]
with self.assertRaises(WebError):
self.res.render_GET(self.request)
@given(
text()
)
def test_hypothesis_error_caps(self, cap):
"""
Let hypothesis try a bunch of invalid capabilities
"""
# existing code insists capabilities are type "str" .. which
# sounds like it'll definitely be wrong for python3? (what
# does twisted.web produce for stuff in 'fields' or the
# equivalent for a plain Request? also maybe I should use that
# already?)
self.request.args[b"uri"] = [cap.encode('utf8')]
with self.assertRaises(WebError): with self.assertRaises(WebError):
self.res.render_GET(self.request) self.res.render_GET(self.request)

View File

@ -57,7 +57,7 @@ class URIHandler(resource.Resource, object):
"GET /uri/<capability>" with any other query args "GET /uri/<capability>" with any other query args
preserved. New code should use "/uri/<cap>" preserved. New code should use "/uri/<cap>"
""" """
uri_arg = get_arg(req, "uri", None) uri_arg = req.args.get(b"uri", [None])[0]
if uri_arg is None: if uri_arg is None:
raise WebError("GET /uri requires uri=") raise WebError("GET /uri requires uri=")