Newer
Older
# 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.
"""
from __future__ import (
absolute_import,
)
from fixtures import (
Fixture,
TempDir,
)
from twisted.python.filepath import (
FilePath,
)
Jean-Paul Calderone
committed
from twisted.internet.task import (
Clock,
)
from allmydata.storage.server import (
StorageServer,
)
from ..model import (
VoucherStore,
open_and_initialize,
from ..controller import (
PaymentController,
)
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.
"""
def _setUp(self):
self.tempdir = FilePath(self.useFixture(TempDir()).join(b"storage"))
self.storage_server = StorageServer(
self.tempdir.asBytesMode().path,
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,
)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
@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.
"""
redeemer = attr.ib()
get_now = attr.ib()
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,
Jean-Paul Calderone
committed
clock=Clock(),
).redeem(
voucher,
)