Skip to content
Snippets Groups Projects
Commit 30497dc4 authored by Jean-Paul Calderone's avatar Jean-Paul Calderone
Browse files

Have and use a voucher store fixture

parent ed1f2016
No related branches found
No related tags found
1 merge request!66Retry redemption on failure
...@@ -20,6 +20,8 @@ from __future__ import ( ...@@ -20,6 +20,8 @@ from __future__ import (
absolute_import, absolute_import,
) )
import attr
from fixtures import ( from fixtures import (
Fixture, Fixture,
TempDir, TempDir,
...@@ -33,6 +35,11 @@ from allmydata.storage.server import ( ...@@ -33,6 +35,11 @@ from allmydata.storage.server import (
StorageServer, StorageServer,
) )
from ..model import (
VoucherStore,
memory_connect,
)
class AnonymousStorageServer(Fixture): class AnonymousStorageServer(Fixture):
""" """
Supply an instance of allmydata.storage.server.StorageServer which Supply an instance of allmydata.storage.server.StorageServer which
...@@ -50,3 +57,28 @@ class AnonymousStorageServer(Fixture): ...@@ -50,3 +57,28 @@ class AnonymousStorageServer(Fixture):
self.tempdir.asBytesMode().path, self.tempdir.asBytesMode().path,
b"x" * 20, b"x" * 20,
) )
@attr.s
class TemporaryVoucherStore(Fixture):
"""
Create a ``VoucherStore`` in a temporary directory associated with the
given test case.
:ivar get_config: A function like the one built by ``tahoe_configs``.
:ivar get_now: A no-argument callable that returns a datetime giving a
time to consider as "now".
:ivar store: A newly created temporary store.
"""
get_config = attr.ib()
get_now = attr.ib()
def _setUp(self):
self.tempdir = self.useFixture(TempDir())
self.config = self.get_config(self.tempdir.join(b"node"), b"tub.port")
self.store = VoucherStore.from_node_config(
self.config,
self.get_now,
memory_connect,
)
...@@ -50,10 +50,6 @@ from testtools.twistedsupport import ( ...@@ -50,10 +50,6 @@ from testtools.twistedsupport import (
failed, failed,
) )
from fixtures import (
TempDir,
)
from hypothesis import ( from hypothesis import (
given, given,
) )
...@@ -105,8 +101,6 @@ from ..controller import ( ...@@ -105,8 +101,6 @@ from ..controller import (
) )
from ..model import ( from ..model import (
memory_connect,
VoucherStore,
UnblindedToken, UnblindedToken,
Pending as model_Pending, Pending as model_Pending,
DoubleSpend as model_DoubleSpend, DoubleSpend as model_DoubleSpend,
...@@ -122,6 +116,10 @@ from .strategies import ( ...@@ -122,6 +116,10 @@ from .strategies import (
from .matchers import ( from .matchers import (
Provides, Provides,
) )
from .fixtures import (
TemporaryVoucherStore,
)
class PaymentControllerTests(TestCase): class PaymentControllerTests(TestCase):
""" """
...@@ -133,15 +131,7 @@ class PaymentControllerTests(TestCase): ...@@ -133,15 +131,7 @@ class PaymentControllerTests(TestCase):
A ``Voucher`` is not marked redeemed before ``IRedeemer.redeem`` A ``Voucher`` is not marked redeemed before ``IRedeemer.redeem``
completes. completes.
""" """
tempdir = self.useFixture(TempDir()) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store = VoucherStore.from_node_config(
get_config(
tempdir.join(b"node"),
b"tub.port",
),
now=lambda: now,
connect=memory_connect,
)
controller = PaymentController( controller = PaymentController(
store, store,
NonRedeemer(), NonRedeemer(),
...@@ -159,15 +149,7 @@ class PaymentControllerTests(TestCase): ...@@ -159,15 +149,7 @@ class PaymentControllerTests(TestCase):
""" """
A ``Voucher`` is marked as redeemed after ``IRedeemer.redeem`` succeeds. A ``Voucher`` is marked as redeemed after ``IRedeemer.redeem`` succeeds.
""" """
tempdir = self.useFixture(TempDir()) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store = VoucherStore.from_node_config(
get_config(
tempdir.join(b"node"),
b"tub.port",
),
now=lambda: now,
connect=memory_connect,
)
controller = PaymentController( controller = PaymentController(
store, store,
DummyRedeemer(), DummyRedeemer(),
...@@ -189,15 +171,7 @@ class PaymentControllerTests(TestCase): ...@@ -189,15 +171,7 @@ class PaymentControllerTests(TestCase):
A ``Voucher`` is marked as double-spent after ``IRedeemer.redeem`` fails A ``Voucher`` is marked as double-spent after ``IRedeemer.redeem`` fails
with ``AlreadySpent``. with ``AlreadySpent``.
""" """
tempdir = self.useFixture(TempDir()) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store = VoucherStore.from_node_config(
get_config(
tempdir.join(b"node"),
b"tub.port",
),
now=lambda: now,
connect=memory_connect,
)
controller = PaymentController( controller = PaymentController(
store, store,
DoubleSpendRedeemer(), DoubleSpendRedeemer(),
...@@ -220,15 +194,7 @@ class PaymentControllerTests(TestCase): ...@@ -220,15 +194,7 @@ class PaymentControllerTests(TestCase):
When ``PaymentController`` is created, any vouchers in the store in the When ``PaymentController`` is created, any vouchers in the store in the
pending state are redeemed. pending state are redeemed.
""" """
tempdir = self.useFixture(TempDir()) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store = VoucherStore.from_node_config(
get_config(
tempdir.join(b"node"),
b"tub.port",
),
now=lambda: now,
connect=memory_connect,
)
# Create the voucher state in the store with a redemption that will # Create the voucher state in the store with a redemption that will
# certainly fail. # certainly fail.
unpaid_controller = PaymentController( unpaid_controller = PaymentController(
......
...@@ -79,6 +79,9 @@ from .strategies import ( ...@@ -79,6 +79,9 @@ from .strategies import (
random_tokens, random_tokens,
unblinded_tokens, unblinded_tokens,
) )
from .fixtures import (
TemporaryVoucherStore,
)
class VoucherStoreTests(TestCase): class VoucherStoreTests(TestCase):
...@@ -107,7 +110,7 @@ class VoucherStoreTests(TestCase): ...@@ -107,7 +110,7 @@ class VoucherStoreTests(TestCase):
``VoucherStore.get`` raises ``KeyError`` when called with a ``VoucherStore.get`` raises ``KeyError`` when called with a
voucher not previously added to the store. voucher not previously added to the store.
""" """
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
self.assertThat( self.assertThat(
lambda: store.get(voucher), lambda: store.get(voucher),
raises(KeyError), raises(KeyError),
...@@ -119,7 +122,7 @@ class VoucherStoreTests(TestCase): ...@@ -119,7 +122,7 @@ class VoucherStoreTests(TestCase):
``VoucherStore.get`` returns a ``Voucher`` representing a voucher ``VoucherStore.get`` returns a ``Voucher`` representing a voucher
previously added to the store with ``VoucherStore.add``. previously added to the store with ``VoucherStore.add``.
""" """
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store.add(voucher, tokens) store.add(voucher, tokens)
self.assertThat( self.assertThat(
store.get(voucher), store.get(voucher),
...@@ -136,7 +139,7 @@ class VoucherStoreTests(TestCase): ...@@ -136,7 +139,7 @@ class VoucherStoreTests(TestCase):
More than one call to ``VoucherStore.add`` with the same argument results More than one call to ``VoucherStore.add`` with the same argument results
in the same state as a single call. in the same state as a single call.
""" """
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store.add(voucher, tokens) store.add(voucher, tokens)
store.add(voucher, []) store.add(voucher, [])
self.assertThat( self.assertThat(
...@@ -155,7 +158,7 @@ class VoucherStoreTests(TestCase): ...@@ -155,7 +158,7 @@ class VoucherStoreTests(TestCase):
``VoucherStore.list`` returns a ``list`` containing a ``Voucher`` object ``VoucherStore.list`` returns a ``list`` containing a ``Voucher`` object
for each voucher previously added. for each voucher previously added.
""" """
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
for voucher in vouchers: for voucher in vouchers:
store.add(voucher, []) store.add(voucher, [])
...@@ -259,7 +262,7 @@ class UnblindedTokenStoreTests(TestCase): ...@@ -259,7 +262,7 @@ class UnblindedTokenStoreTests(TestCase):
""" """
Unblinded tokens that are added to the store can later be retrieved. Unblinded tokens that are added to the store can later be retrieved.
""" """
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store.insert_unblinded_tokens_for_voucher(voucher_value, tokens) store.insert_unblinded_tokens_for_voucher(voucher_value, tokens)
retrieved_tokens = store.extract_unblinded_tokens(len(tokens)) retrieved_tokens = store.extract_unblinded_tokens(len(tokens))
self.expectThat(tokens, AfterPreprocessing(sorted, Equals(retrieved_tokens))) self.expectThat(tokens, AfterPreprocessing(sorted, Equals(retrieved_tokens)))
...@@ -297,7 +300,7 @@ class UnblindedTokenStoreTests(TestCase): ...@@ -297,7 +300,7 @@ class UnblindedTokenStoreTests(TestCase):
), ),
) )
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store.add(voucher_value, random) store.add(voucher_value, random)
store.insert_unblinded_tokens_for_voucher(voucher_value, unblinded) store.insert_unblinded_tokens_for_voucher(voucher_value, unblinded)
loaded_voucher = store.get(voucher_value) loaded_voucher = store.get(voucher_value)
...@@ -322,7 +325,7 @@ class UnblindedTokenStoreTests(TestCase): ...@@ -322,7 +325,7 @@ class UnblindedTokenStoreTests(TestCase):
A voucher which is reported as double-spent is marked in the database as A voucher which is reported as double-spent is marked in the database as
such. such.
""" """
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store.add(voucher_value, random_tokens) store.add(voucher_value, random_tokens)
store.mark_voucher_double_spent(voucher_value) store.mark_voucher_double_spent(voucher_value)
voucher = store.get(voucher_value) voucher = store.get(voucher_value)
...@@ -362,7 +365,7 @@ class UnblindedTokenStoreTests(TestCase): ...@@ -362,7 +365,7 @@ class UnblindedTokenStoreTests(TestCase):
unique=True, unique=True,
), ),
) )
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
store.add(voucher_value, random) store.add(voucher_value, random)
store.insert_unblinded_tokens_for_voucher(voucher_value, unblinded) store.insert_unblinded_tokens_for_voucher(voucher_value, unblinded)
try: try:
...@@ -383,7 +386,7 @@ class UnblindedTokenStoreTests(TestCase): ...@@ -383,7 +386,7 @@ class UnblindedTokenStoreTests(TestCase):
""" """
A voucher which is not known cannot be marked as double-spent. A voucher which is not known cannot be marked as double-spent.
""" """
store = store_for_test(self, get_config, lambda: now) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store
try: try:
result = store.mark_voucher_double_spent(voucher_value) result = store.mark_voucher_double_spent(voucher_value)
except ValueError: except ValueError:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment