2019-09-05 16:29:25 -06:00
|
|
|
from mock import Mock
|
|
|
|
|
2016-08-26 17:55:52 -07:00
|
|
|
from twisted.trial import unittest
|
2019-09-05 16:29:25 -06:00
|
|
|
from twisted.web.test.requesthelper import DummyRequest
|
2016-08-26 17:55:52 -07:00
|
|
|
|
|
|
|
from ...storage_client import NativeStorageServer
|
|
|
|
from ...web.root import Root
|
2016-12-08 15:15:49 -08:00
|
|
|
from ...util.connection_status import ConnectionStatus
|
2019-09-05 16:29:25 -06:00
|
|
|
from allmydata.web.root import URIHandler
|
2019-09-05 16:30:43 -06:00
|
|
|
from allmydata.web.common import WebError
|
2019-09-05 16:29:25 -06:00
|
|
|
|
|
|
|
from nevow.inevow import IRequest
|
|
|
|
|
|
|
|
from zope.interface import directlyProvides
|
2016-08-26 17:55:52 -07:00
|
|
|
|
|
|
|
class FakeRoot(Root):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def now_fn(self):
|
|
|
|
return 0
|
|
|
|
|
2019-09-05 16:07:09 -06:00
|
|
|
|
2019-05-15 08:17:44 +02:00
|
|
|
class FakeContext(object):
|
2016-08-26 17:55:52 -07:00
|
|
|
def __init__(self):
|
|
|
|
self.slots = {}
|
|
|
|
self.tag = self
|
|
|
|
def fillSlots(self, slotname, contents):
|
|
|
|
self.slots[slotname] = contents
|
|
|
|
|
2019-09-05 16:07:09 -06:00
|
|
|
|
2019-09-05 16:29:25 -06:00
|
|
|
class FakeField(object):
|
|
|
|
"""
|
|
|
|
Without using Nevow code directly, provide a false IRequest.fields
|
|
|
|
implementation on top of twisted.web's DummyRequest
|
|
|
|
"""
|
|
|
|
def __init__(self, **kw):
|
|
|
|
for k, v in kw.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
|
|
|
|
|
|
|
class RenderSlashUri(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Ensure that URI's starting with /uri?uri= only accept valid
|
|
|
|
capabilities
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.request = DummyRequest("/uri")
|
|
|
|
self.request.fields = {}
|
|
|
|
self.request.prePathURL = lambda: "http://127.0.0.1.99999/{}".format("/".join(self.request.prepath))
|
|
|
|
directlyProvides(self.request, IRequest)
|
|
|
|
self.client = Mock()
|
|
|
|
self.res = URIHandler(self.client)
|
|
|
|
|
|
|
|
def test_valid(self):
|
|
|
|
"""
|
|
|
|
A valid capbility does not result in error
|
|
|
|
"""
|
|
|
|
self.request.fields["uri"] = FakeField(
|
|
|
|
value=(
|
|
|
|
"URI:CHK:nt2xxmrccp7sursd6yh2thhcky:"
|
|
|
|
"mukesarwdjxiyqsjinbfiiro6q7kgmmekocxfjcngh23oxwyxtzq:2:5:5874882"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.res.render_GET(self.request)
|
2019-09-05 16:30:43 -06:00
|
|
|
|
|
|
|
def test_invalid(self):
|
|
|
|
"""
|
|
|
|
A (trivially) invalid capbility is an error
|
|
|
|
"""
|
|
|
|
self.request.fields["uri"] = FakeField(value="not a capability")
|
|
|
|
with self.assertRaises(WebError):
|
|
|
|
self.res.render_GET(self.request)
|
2019-09-05 16:29:25 -06:00
|
|
|
|
|
|
|
|
2016-08-26 17:55:52 -07:00
|
|
|
class RenderServiceRow(unittest.TestCase):
|
|
|
|
def test_missing(self):
|
2019-09-05 16:07:09 -06:00
|
|
|
"""
|
|
|
|
minimally-defined static servers just need anonymous-storage-FURL
|
|
|
|
and permutation-seed-base32. The WUI used to have problems
|
|
|
|
rendering servers that lacked nickname and version. This tests that
|
|
|
|
we can render such minimal servers.
|
|
|
|
"""
|
2016-08-26 17:55:52 -07:00
|
|
|
ann = {"anonymous-storage-FURL": "pb://w2hqnbaa25yw4qgcvghl5psa3srpfgw3@tcp:127.0.0.1:51309/vucto2z4fxment3vfxbqecblbf6zyp6x",
|
|
|
|
"permutation-seed-base32": "w2hqnbaa25yw4qgcvghl5psa3srpfgw3",
|
|
|
|
}
|
2016-08-27 16:53:31 -07:00
|
|
|
s = NativeStorageServer("server_id", ann, None, {})
|
2016-12-09 16:35:46 -08:00
|
|
|
cs = ConnectionStatus(False, "summary", {}, 0, 0)
|
2016-12-08 15:15:49 -08:00
|
|
|
s.get_connection_status = lambda: cs
|
2016-08-26 17:55:52 -07:00
|
|
|
|
|
|
|
r = FakeRoot()
|
|
|
|
ctx = FakeContext()
|
|
|
|
res = r.render_service_row(ctx, s)
|
|
|
|
self.assertIdentical(res, ctx)
|
|
|
|
self.assertEqual(ctx.slots["version"], "")
|
|
|
|
self.assertEqual(ctx.slots["nickname"], "")
|