Skip to content
Snippets Groups Projects
fixtures.py 4.03 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jean-Paul Calderone's avatar
    Jean-Paul Calderone committed
    # Copyright 2019 PrivateStorage.io, LLC
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    """
    Common fixtures to let the test suite focus on application logic.
    """
    
    
    Tom Prince's avatar
    Tom Prince committed
    from base64 import b64encode
    
    Tom Prince's avatar
    Tom Prince committed
    from allmydata.storage.server import StorageServer
    from fixtures import Fixture, TempDir
    from twisted.internet.task import Clock
    from twisted.python.filepath import FilePath
    
    Tom Prince's avatar
    Tom Prince committed
    from ..controller import DummyRedeemer, PaymentController
    from ..model import VoucherStore, memory_connect, open_and_initialize
    
    Tom Prince's avatar
    Tom Prince committed
    
    
    class AnonymousStorageServer(Fixture):
        """
        Supply an instance of allmydata.storage.server.StorageServer which
        implements anonymous access to Tahoe-LAFS storage server functionality.
    
        :ivar FilePath tempdir: The path to the server's storage on the
            filesystem.
    
        :ivar allmydata.storage.server.StorageServer storage_server: The storage
            server.
    
    
        :ivar twisted.internet.task.Clock clock: The ``IReactorTime`` provider to
            supply to ``StorageServer`` for its time-checking needs.
    
    Tom Prince's avatar
    Tom Prince committed
    
    
            self.tempdir = FilePath(self.useFixture(TempDir()).join(u"storage"))
    
            self.storage_server = StorageServer(
    
                self.tempdir.path,
    
    
    
    @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.
        """
    
    Tom Prince's avatar
    Tom Prince committed
    
    
        get_config = attr.ib()
        get_now = attr.ib()
    
        def _setUp(self):
            self.tempdir = self.useFixture(TempDir())
    
            self.config = self.get_config(self.tempdir.join(u"node"), u"tub.port")
    
            self.store = VoucherStore.from_node_config(
                self.config,
                self.get_now,
                memory_connect,
            )
    
    
    
    @attr.s
    class ConfiglessMemoryVoucherStore(Fixture):
        """
        Create a ``VoucherStore`` backed by an in-memory database and with no
        associated Tahoe-LAFS configuration or node.
    
        This is like ``TemporaryVoucherStore`` but faster because it skips the
        Tahoe-LAFS parts.
        """
    
    Tom Prince's avatar
    Tom Prince committed
    
    
        _public_key = attr.ib(default=b64encode(b"A" * 32).decode("utf-8"))
        redeemer = attr.ib(default=None, init=False)
    
        def __attrs_post_init__(self):
            self.redeemer = DummyRedeemer(self._public_key)
    
    
        def _setUp(self):
            here = FilePath(u".")
            self.store = VoucherStore(
                pass_value=2 ** 15,
                database_path=here,
                now=self.get_now,
                connection=open_and_initialize(here, memory_connect),
            )
    
        def redeem(self, voucher, num_passes):
            """
            Redeem a voucher for some passes.
    
            :return: A ``Deferred`` that fires with the redemption result.
            """
            return PaymentController(
                self.store,
                self.redeemer,
                # Have to pass it here or to redeem, doesn't matter which.
                default_token_count=num_passes,
                # No value in splitting it into smaller groups in this case.
                # Doing so only complicates the test by imposing a different
                # minimum token count requirement (can't have fewer tokens
                # than groups).
                num_redemption_groups=1,
    
                allowed_public_keys={self._public_key},