Switch to using a fixture.

This commit is contained in:
Itamar Turner-Trauring 2021-12-21 11:10:53 -05:00
parent 1721865b20
commit 2bda2a0127

View File

@ -17,28 +17,34 @@ if PY2:
from unittest import SkipTest from unittest import SkipTest
from base64 import b64encode from base64 import b64encode
from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks from twisted.internet.defer import inlineCallbacks
from fixtures import Fixture, TempDir
from treq.testing import StubTreq from treq.testing import StubTreq
from klein import Klein from klein import Klein
from hyperlink import DecodedURL from hyperlink import DecodedURL
from .common import AsyncTestCase, SyncTestCase
from ..storage.server import StorageServer from ..storage.server import StorageServer
from ..storage.http_server import ( from ..storage.http_server import (
HTTPServer, _extract_secrets, Secrets, ClientSecretsException, HTTPServer,
_extract_secrets,
Secrets,
ClientSecretsException,
_authorized_route, _authorized_route,
) )
from ..storage.http_client import StorageClient, ClientException from ..storage.http_client import StorageClient, ClientException
class ExtractSecretsTests(TestCase): class ExtractSecretsTests(SyncTestCase):
""" """
Tests for ``_extract_secrets``. Tests for ``_extract_secrets``.
""" """
def setUp(self): def setUp(self):
if PY2: if PY2:
raise SkipTest("Not going to bother supporting Python 2") raise SkipTest("Not going to bother supporting Python 2")
super(ExtractSecretsTests, self).setUp()
def test_extract_secrets(self): def test_extract_secrets(self):
""" """
@ -55,16 +61,16 @@ class ExtractSecretsTests(TestCase):
# One secret: # One secret:
self.assertEqual( self.assertEqual(
_extract_secrets([lease_secret], _extract_secrets([lease_secret], {Secrets.LEASE_RENEW}),
{Secrets.LEASE_RENEW}), {Secrets.LEASE_RENEW: secret1},
{Secrets.LEASE_RENEW: secret1}
) )
# Two secrets: # Two secrets:
self.assertEqual( self.assertEqual(
_extract_secrets([upload_secret, lease_secret], _extract_secrets(
{Secrets.LEASE_RENEW, Secrets.UPLOAD}), [upload_secret, lease_secret], {Secrets.LEASE_RENEW, Secrets.UPLOAD}
{Secrets.LEASE_RENEW: secret1, Secrets.UPLOAD: secret2} ),
{Secrets.LEASE_RENEW: secret1, Secrets.UPLOAD: secret2},
) )
def test_wrong_number_of_secrets(self): def test_wrong_number_of_secrets(self):
@ -129,19 +135,23 @@ class TestApp(object):
return "BAD: {}".format(authorization) return "BAD: {}".format(authorization)
class RoutingTests(TestCase): class RoutingTests(AsyncTestCase):
""" """
Tests for the HTTP routing infrastructure. Tests for the HTTP routing infrastructure.
""" """
def setUp(self): def setUp(self):
if PY2: if PY2:
raise SkipTest("Not going to bother supporting Python 2") raise SkipTest("Not going to bother supporting Python 2")
super(RoutingTests, self).setUp()
# Could be a fixture, but will only be used in this test class so not
# going to bother:
self._http_server = TestApp() self._http_server = TestApp()
self.client = StorageClient( self.client = StorageClient(
DecodedURL.from_text("http://127.0.0.1"), DecodedURL.from_text("http://127.0.0.1"),
SWISSNUM_FOR_TEST, SWISSNUM_FOR_TEST,
treq=StubTreq(self._http_server._app.resource()), treq=StubTreq(self._http_server._app.resource()),
) )
@inlineCallbacks @inlineCallbacks
def test_authorization_enforcement(self): def test_authorization_enforcement(self):
@ -163,30 +173,35 @@ class RoutingTests(TestCase):
self.assertEqual((yield response.content()), b"GOOD SECRET") self.assertEqual((yield response.content()), b"GOOD SECRET")
def setup_http_test(self): class HttpTestFixture(Fixture):
""" """
Setup HTTP tests; call from ``setUp``. Setup HTTP tests' infrastructure, the storage server and corresponding
client.
""" """
if PY2:
raise SkipTest("Not going to bother supporting Python 2") def _setUp(self):
self.storage_server = StorageServer(self.mktemp(), b"\x00" * 20) self.tempdir = self.useFixture(TempDir())
# TODO what should the swissnum _actually_ be? self.storage_server = StorageServer(self.tempdir.path, b"\x00" * 20)
self._http_server = HTTPServer(self.storage_server, SWISSNUM_FOR_TEST) # TODO what should the swissnum _actually_ be?
self.client = StorageClient( self.http_server = HTTPServer(self.storage_server, SWISSNUM_FOR_TEST)
DecodedURL.from_text("http://127.0.0.1"), self.client = StorageClient(
SWISSNUM_FOR_TEST, DecodedURL.from_text("http://127.0.0.1"),
treq=StubTreq(self._http_server.get_resource()), SWISSNUM_FOR_TEST,
) treq=StubTreq(self.http_server.get_resource()),
)
class GenericHTTPAPITests(TestCase): class GenericHTTPAPITests(AsyncTestCase):
""" """
Tests of HTTP client talking to the HTTP server, for generic HTTP API Tests of HTTP client talking to the HTTP server, for generic HTTP API
endpoints and concerns. endpoints and concerns.
""" """
def setUp(self): def setUp(self):
setup_http_test(self) if PY2:
raise SkipTest("Not going to bother supporting Python 2")
super(GenericHTTPAPITests, self).setUp()
self.http = self.useFixture(HttpTestFixture())
@inlineCallbacks @inlineCallbacks
def test_bad_authentication(self): def test_bad_authentication(self):
@ -197,7 +212,7 @@ class GenericHTTPAPITests(TestCase):
client = StorageClient( client = StorageClient(
DecodedURL.from_text("http://127.0.0.1"), DecodedURL.from_text("http://127.0.0.1"),
b"something wrong", b"something wrong",
treq=StubTreq(self._http_server.get_resource()), treq=StubTreq(self.http.http_server.get_resource()),
) )
with self.assertRaises(ClientException) as e: with self.assertRaises(ClientException) as e:
yield client.get_version() yield client.get_version()
@ -211,14 +226,14 @@ class GenericHTTPAPITests(TestCase):
We ignore available disk space and max immutable share size, since that We ignore available disk space and max immutable share size, since that
might change across calls. might change across calls.
""" """
version = yield self.client.get_version() version = yield self.http.client.get_version()
version[b"http://allmydata.org/tahoe/protocols/storage/v1"].pop( version[b"http://allmydata.org/tahoe/protocols/storage/v1"].pop(
b"available-space" b"available-space"
) )
version[b"http://allmydata.org/tahoe/protocols/storage/v1"].pop( version[b"http://allmydata.org/tahoe/protocols/storage/v1"].pop(
b"maximum-immutable-share-size" b"maximum-immutable-share-size"
) )
expected_version = self.storage_server.get_version() expected_version = self.http.storage_server.get_version()
expected_version[b"http://allmydata.org/tahoe/protocols/storage/v1"].pop( expected_version[b"http://allmydata.org/tahoe/protocols/storage/v1"].pop(
b"available-space" b"available-space"
) )