2019-09-05 16:29:25 -06:00
|
|
|
from mock import Mock
|
|
|
|
|
2020-05-04 09:45:36 -04:00
|
|
|
import time
|
|
|
|
|
2020-09-23 08:32:19 -04:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2016-08-26 17:55:52 -07:00
|
|
|
from twisted.trial import unittest
|
2020-04-28 19:07:57 -04:00
|
|
|
from twisted.web.template import Tag
|
2019-09-05 16:29:25 -06:00
|
|
|
from twisted.web.test.requesthelper import DummyRequest
|
2020-04-28 19:07:57 -04:00
|
|
|
from twisted.application import service
|
2016-08-26 17:55:52 -07:00
|
|
|
|
2020-04-28 19:07:57 -04:00
|
|
|
from ...storage_client import (
|
|
|
|
NativeStorageServer,
|
|
|
|
StorageFarmBroker,
|
|
|
|
)
|
2020-04-28 19:09:00 -04:00
|
|
|
from ...web.root import RootElement
|
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
|
2020-04-28 19:07:57 -04:00
|
|
|
from allmydata.client import _Client
|
2019-09-05 16:29:25 -06:00
|
|
|
|
2019-09-05 16:48:08 -06:00
|
|
|
from hypothesis import given
|
|
|
|
from hypothesis.strategies import text
|
|
|
|
|
2020-09-23 08:32:19 -04:00
|
|
|
from .common import (
|
|
|
|
assert_soup_has_tag_with_content,
|
|
|
|
)
|
2016-08-26 17:55:52 -07:00
|
|
|
|
2019-08-19 16:09:26 -04:00
|
|
|
from ..common import (
|
|
|
|
EMPTY_CLIENT_CONFIG,
|
|
|
|
)
|
|
|
|
|
2019-09-05 16:29:25 -06:00
|
|
|
class RenderSlashUri(unittest.TestCase):
|
|
|
|
"""
|
2019-09-05 16:48:08 -06:00
|
|
|
Ensure that URIs starting with /uri?uri= only accept valid
|
2019-09-05 16:29:25 -06:00
|
|
|
capabilities
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-09-27 11:52:27 -06:00
|
|
|
self.request = DummyRequest(b"/uri")
|
2019-09-05 16:29:25 -06:00
|
|
|
self.request.fields = {}
|
2019-09-05 16:48:08 -06:00
|
|
|
|
|
|
|
def prepathURL():
|
2019-09-27 11:52:27 -06:00
|
|
|
return b"http://127.0.0.1.99999/" + b"/".join(self.request.prepath)
|
|
|
|
|
2019-09-05 16:48:08 -06:00
|
|
|
self.request.prePathURL = prepathURL
|
2019-09-05 16:29:25 -06:00
|
|
|
self.client = Mock()
|
|
|
|
self.res = URIHandler(self.client)
|
|
|
|
|
|
|
|
def test_valid(self):
|
|
|
|
"""
|
|
|
|
A valid capbility does not result in error
|
|
|
|
"""
|
2019-09-05 16:48:08 -06:00
|
|
|
self.request.args[b"uri"] = [(
|
|
|
|
b"URI:CHK:nt2xxmrccp7sursd6yh2thhcky:"
|
|
|
|
b"mukesarwdjxiyqsjinbfiiro6q7kgmmekocxfjcngh23oxwyxtzq:2:5:5874882"
|
|
|
|
)]
|
2019-09-05 16:29:25 -06:00
|
|
|
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
|
|
|
|
"""
|
2019-09-05 16:48:08 -06:00
|
|
|
self.request.args[b"uri"] = [b"not a capability"]
|
2020-09-21 18:34:53 -04:00
|
|
|
response_body = self.res.render_GET(self.request)
|
|
|
|
|
2020-09-23 08:32:19 -04:00
|
|
|
soup = BeautifulSoup(response_body, 'html5lib')
|
|
|
|
|
|
|
|
assert_soup_has_tag_with_content(
|
|
|
|
self, soup, "title", "400 - Error",
|
2020-09-21 18:34:53 -04:00
|
|
|
)
|
2020-09-23 08:32:19 -04:00
|
|
|
assert_soup_has_tag_with_content(
|
|
|
|
self, soup, "h1", "Error",
|
2020-09-21 18:34:53 -04:00
|
|
|
)
|
2020-09-23 08:32:19 -04:00
|
|
|
assert_soup_has_tag_with_content(
|
|
|
|
self, soup, "p", "Invalid capability",
|
2020-09-21 18:34:53 -04:00
|
|
|
)
|
2019-09-05 16:48:08 -06:00
|
|
|
|
|
|
|
@given(
|
|
|
|
text()
|
|
|
|
)
|
|
|
|
def test_hypothesis_error_caps(self, cap):
|
|
|
|
"""
|
|
|
|
Let hypothesis try a bunch of invalid capabilities
|
|
|
|
"""
|
|
|
|
self.request.args[b"uri"] = [cap.encode('utf8')]
|
2020-09-21 18:34:53 -04:00
|
|
|
response_body = self.res.render_GET(self.request)
|
|
|
|
|
2020-09-23 08:32:19 -04:00
|
|
|
soup = BeautifulSoup(response_body, 'html5lib')
|
|
|
|
|
|
|
|
assert_soup_has_tag_with_content(
|
|
|
|
self, soup, "title", "400 - Error",
|
2020-09-21 18:34:53 -04:00
|
|
|
)
|
2020-09-23 08:32:19 -04:00
|
|
|
assert_soup_has_tag_with_content(
|
|
|
|
self, soup, "h1", "Error",
|
2020-09-21 18:34:53 -04:00
|
|
|
)
|
2020-09-23 08:32:19 -04:00
|
|
|
assert_soup_has_tag_with_content(
|
|
|
|
self, soup, "p", "Invalid capability",
|
2020-09-21 18:34:53 -04:00
|
|
|
)
|
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",
|
|
|
|
}
|
2020-05-04 09:45:36 -04:00
|
|
|
srv = NativeStorageServer("server_id", ann, None, {}, EMPTY_CLIENT_CONFIG)
|
|
|
|
srv.get_connection_status = lambda: ConnectionStatus(False, "summary", {}, 0, 0)
|
2016-08-26 17:55:52 -07:00
|
|
|
|
2020-04-28 19:07:57 -04:00
|
|
|
class FakeClient(_Client):
|
|
|
|
def __init__(self):
|
|
|
|
service.MultiService.__init__(self)
|
|
|
|
self.storage_broker = StorageFarmBroker(
|
|
|
|
permute_peers=True,
|
|
|
|
tub_maker=None,
|
|
|
|
node_config=EMPTY_CLIENT_CONFIG,
|
|
|
|
)
|
2020-05-04 09:45:36 -04:00
|
|
|
self.storage_broker.test_add_server("test-srv", srv)
|
2020-04-28 19:07:57 -04:00
|
|
|
|
2020-05-04 09:45:36 -04:00
|
|
|
root = RootElement(FakeClient(), time.time)
|
2020-04-28 19:07:57 -04:00
|
|
|
req = DummyRequest(b"")
|
2020-05-04 09:45:36 -04:00
|
|
|
tag = Tag(b"")
|
|
|
|
|
|
|
|
# Pick all items from services table.
|
|
|
|
items = root.services_table(req, tag).item(req, tag)
|
2020-04-28 19:07:57 -04:00
|
|
|
|
2020-05-04 09:45:36 -04:00
|
|
|
# Coerce `items` to list and pick the first item from it.
|
|
|
|
item = list(items)[0]
|
2020-04-28 19:07:57 -04:00
|
|
|
|
2020-05-04 09:45:36 -04:00
|
|
|
self.assertEqual(item.slotData.get("version"), "")
|
|
|
|
self.assertEqual(item.slotData.get("nickname"), "")
|