diff --git a/docs/source/interface.rst b/docs/source/interface.rst index e48f900634ab6bb7d78bf6c48794aab4d4b020fc..909d05e134e68cb75c0640e28baf395700fd8b13 100644 --- a/docs/source/interface.rst +++ b/docs/source/interface.rst @@ -7,44 +7,44 @@ Client When enabled in a Tahoe-LAFS client node, SecureAccessTokenAuthorizer publishes an HTTP-based interface inside the main Tahoe-LAFS web interface. -``PUT /storage-plugins/privatestorageio-satauthz-v1/payment-reference-number`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``PUT /storage-plugins/privatestorageio-satauthz-v1/voucher`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -This endpoint allows an external agent which has submitted a payment to cause the plugin to redeem the payment reference for tokens. +This endpoint allows an external agent which has submitted a payment to cause the plugin to redeem the voucher for tokens. The request body for this endpoint must have the ``application/json`` content-type. -The request body contains a simple json object containing the payment reference number:: +The request body contains a simple json object containing the voucher:: - {"payment-reference-number": "<payment reference number>"} + {"voucher": "<voucher>"} -The endpoint responds to such a request with an **OK** HTTP response code if the payment reference number is accepted for processing. -If the payment reference number cannot be accepted at the time of the request then the response code will be anything other than **OK**. +The endpoint responds to such a request with an **OK** HTTP response code if the voucher is accepted for processing. +If the voucher cannot be accepted at the time of the request then the response code will be anything other than **OK**. If the response is **OK** then a repeated request with the same body will have no effect. If the response is not **OK** then a repeated request with the same body will try to accept the number again. -``GET /storage-plugins/privatestorageio-satauthz-v1/payment-reference-number/<payment reference number>`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``GET /storage-plugins/privatestorageio-satauthz-v1/voucher/<voucher>`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -This endpoint allows an external agent to monitor the status of the redemption of a payment reference number. +This endpoint allows an external agent to monitor the status of the redemption of a voucher. This endpoint accepts no request body. -If the payment reference number is not known then the response is **NOT FOUND**. -For any payment reference number which has previously been submitted, +If the voucher is not known then the response is **NOT FOUND**. +For any voucher which has previously been submitted, the response is **OK** with an ``application/json`` content-type response body like:: {"value": <string>} -The ``value`` property merely indicates the payment reference number which was requested. +The ``value`` property merely indicates the voucher which was requested. Further properties will be added to this response in the near future. -``GET /storage-plugins/privatestorageio-satauthz-v1/payment-reference-number`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``GET /storage-plugins/privatestorageio-satauthz-v1/voucher`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -This endpoint allows an external agent to retrieve the status of all payment reference numbers. +This endpoint allows an external agent to retrieve the status of all vouchers. This endpoint accepts no request body. The response is **OK** with ``application/json`` content-type response body like:: - {"payment-reference-numbers": [<payment reference status object>, ...]} + {"vouchers": [<voucher status object>, ...]} The elements of the list are objects like the one returned by issuing a **GET** to a child of this collection resource. diff --git a/src/_secureaccesstokenauthorizer/controller.py b/src/_secureaccesstokenauthorizer/controller.py index 5b3bea2b4492ac9c0578e0c474cdc6b93c6279c0..7e3302d7953be1cc1e462f7caab63253a4f0b4c7 100644 --- a/src/_secureaccesstokenauthorizer/controller.py +++ b/src/_secureaccesstokenauthorizer/controller.py @@ -23,5 +23,5 @@ import attr class PaymentController(object): store = attr.ib() - def redeem(self, prn): - self.store.add(prn) + def redeem(self, voucher): + self.store.add(voucher) diff --git a/src/_secureaccesstokenauthorizer/model.py b/src/_secureaccesstokenauthorizer/model.py index fb259f1e4e4377a43a70c740ff87909ebb3d977d..3027761be74ff61961e752071f6e8f8b293fdc02 100644 --- a/src/_secureaccesstokenauthorizer/model.py +++ b/src/_secureaccesstokenauthorizer/model.py @@ -53,7 +53,7 @@ CONFIG_DB_NAME = u"privatestorageio-satauthz-v1.sqlite3" def open_and_initialize(path, required_schema_version, connect=None): """ - Open a SQLite3 database for use as a payment reference store. + Open a SQLite3 database for use as a voucher store. Create the database and populate it with a schema, if it does not already exist. @@ -110,7 +110,7 @@ def open_and_initialize(path, required_schema_version, connect=None): cursor.execute( """ - CREATE TABLE IF NOT EXISTS [payment-references] ( + CREATE TABLE IF NOT EXISTS [vouchers] ( [number] text, PRIMARY KEY([number]) @@ -136,12 +136,12 @@ def memory_connect(path, *a, **kw): @attr.s(frozen=True) -class PaymentReferenceStore(object): +class VoucherStore(object): """ - This class implements persistence for payment references. + This class implements persistence for vouchers. :ivar allmydata.node._Config node_config: The Tahoe-LAFS node configuration object for - the node that owns the persisted payment preferences. + the node that owns the persisted vouchers. """ database_path = attr.ib(type=FilePath) _connection = attr.ib() @@ -160,50 +160,50 @@ class PaymentReferenceStore(object): ) @with_cursor - def get(self, cursor, prn): + def get(self, cursor, voucher): cursor.execute( """ SELECT ([number]) FROM - [payment-references] + [vouchers] WHERE [number] = ? """, - (prn,), + (voucher,), ) refs = cursor.fetchall() if len(refs) == 0: - raise KeyError(prn) - return PaymentReference(refs[0][0]) + raise KeyError(voucher) + return Voucher(refs[0][0]) @with_cursor - def add(self, cursor, prn): + def add(self, cursor, voucher): cursor.execute( """ - INSERT OR IGNORE INTO [payment-references] VALUES (?) + INSERT OR IGNORE INTO [vouchers] VALUES (?) """, - (prn,) + (voucher,) ) @with_cursor def list(self, cursor): cursor.execute( """ - SELECT ([number]) FROM [payment-references] + SELECT ([number]) FROM [vouchers] """, ) refs = cursor.fetchall() return list( - PaymentReference(number) + Voucher(number) for (number,) in refs ) @attr.s -class PaymentReference(object): +class Voucher(object): number = attr.ib() @classmethod diff --git a/src/_secureaccesstokenauthorizer/resource.py b/src/_secureaccesstokenauthorizer/resource.py index d8b3626e862d35dbb034345344af368ebbb947cf..97743a5bc35842ded2b4c3dcd1c05c38109189b0 100644 --- a/src/_secureaccesstokenauthorizer/resource.py +++ b/src/_secureaccesstokenauthorizer/resource.py @@ -15,7 +15,7 @@ """ This module implements views (in the MVC sense) for the web interface for the client side of the storage plugin. This interface allows users to redeem -payment codes for fresh tokens. +vouchers for fresh tokens. In the future it should also allow users to read statistics about token usage. """ @@ -38,7 +38,7 @@ from ._base64 import ( ) from .model import ( - PaymentReferenceStore, + VoucherStore, ) from .controller import ( PaymentController, @@ -57,19 +57,19 @@ def from_configuration(node_config, store=None): This is also used to read and write files in the private storage area of the node's persistent state location. - :param PaymentReferenceStore store: The store to use. If ``None`` a - sensible one is constructed. + :param VoucherStore store: The store to use. If ``None`` a sensible one + is constructed. :return IResource: The root of the resource hierarchy presented by the client side of the plugin. """ if store is None: - store = PaymentReferenceStore.from_node_config(node_config) + store = VoucherStore.from_node_config(node_config) controller = PaymentController(store) root = Resource() root.putChild( - b"payment-reference-number", - _PaymentReferenceNumberCollection( + b"voucher", + _VoucherCollection( store, controller, ), @@ -77,13 +77,12 @@ def from_configuration(node_config, store=None): return root -class _PaymentReferenceNumberCollection(Resource): +class _VoucherCollection(Resource): """ - This class implements redemption of payment reference numbers (PRNs). - Users **PUT** such numbers to this resource which delegates redemption - responsibilities to the redemption controller. Child resources of this - resource can also be retrieved to monitor the status of previously - submitted PRNs. + This class implements redemption of vouchers. Users **PUT** such numbers + to this resource which delegates redemption responsibilities to the + redemption controller. Child resources of this resource can also be + retrieved to monitor the status of previously submitted vouchers. """ def __init__(self, store, controller): self._store = store @@ -93,83 +92,82 @@ class _PaymentReferenceNumberCollection(Resource): def render_PUT(self, request): """ - Record a PRN and begin attempting to redeem it. + Record a voucher and begin attempting to redeem it. """ try: payload = loads(request.content.read()) except Exception: return bad_request().render(request) - if payload.keys() != [u"payment-reference-number"]: + if payload.keys() != [u"voucher"]: return bad_request().render(request) - prn = payload[u"payment-reference-number"] - if not is_syntactic_prn(prn): + voucher = payload[u"voucher"] + if not is_syntactic_voucher(voucher): return bad_request().render(request) - self._controller.redeem(prn) + self._controller.redeem(voucher) return b"" def render_GET(self, request): request.responseHeaders.setRawHeaders(u"content-type", [u"application/json"]) return dumps({ - u"payment-reference-numbers": list( - prn.marshal() - for prn + u"vouchers": list( + voucher.marshal() + for voucher in self._store.list() ), }) def getChild(self, segment, request): - prn = segment.decode("utf-8") - if not is_syntactic_prn(prn): + voucher = segment.decode("utf-8") + if not is_syntactic_voucher(voucher): return bad_request() try: - payment_reference = self._store.get(prn) + voucher = self._store.get(voucher) except KeyError: return NoResource() - return PaymentReferenceView(payment_reference) + return VoucherView(voucher) -def is_syntactic_prn(prn): +def is_syntactic_voucher(voucher): """ - :param prn: A candidate object to inspect. + :param voucher: A candidate object to inspect. - :return bool: ``True`` if and only if ``prn`` is a unicode string - containing a syntactically valid payment reference number. This says - **nothing** about the validity of the represented PRN itself. A - ``True`` result only means the unicode string can be **interpreted** - as a PRN. + :return bool: ``True`` if and only if ``voucher`` is a unicode string + containing a syntactically valid voucher. This says **nothing** about + the validity of the represented voucher itself. A ``True`` result + only means the unicode string can be **interpreted** as a voucher. """ - if not isinstance(prn, unicode): + if not isinstance(voucher, unicode): return False - if len(prn) != 44: + if len(voucher) != 44: # TODO. 44 is the length of 32 bytes base64 encoded. This model # information presumably belongs somewhere else. return False try: - urlsafe_b64decode(prn.encode("ascii")) + urlsafe_b64decode(voucher.encode("ascii")) except Exception: return False return True -class PaymentReferenceView(Resource): +class VoucherView(Resource): """ - This class implements a view for a ``PaymentReference`` instance. + This class implements a view for a ``Voucher`` instance. """ - def __init__(self, reference): + def __init__(self, voucher): """ - :param PaymentReference reference: The model object for which to provide a + :param Voucher reference: The model object for which to provide a view. """ - self._reference = reference + self._voucher = voucher Resource.__init__(self) def render_GET(self, request): request.responseHeaders.setRawHeaders(u"content-type", [u"application/json"]) - return self._reference.to_json() + return self._voucher.to_json() def bad_request(): diff --git a/src/_secureaccesstokenauthorizer/tests/strategies.py b/src/_secureaccesstokenauthorizer/tests/strategies.py index 4af061defb0f2d34a3912078e55dfd76e580d205..38f4afa502b565ee64799250d7aa22d30e36644e 100644 --- a/src/_secureaccesstokenauthorizer/tests/strategies.py +++ b/src/_secureaccesstokenauthorizer/tests/strategies.py @@ -161,9 +161,9 @@ def client_configurations(): return just({}) -def payment_reference_numbers(): +def vouchers(): """ - Build unicode strings in the format of payment reference numbers. + Build unicode strings in the format of vouchers. """ return binary( min_size=32, @@ -171,7 +171,7 @@ def payment_reference_numbers(): ).map( urlsafe_b64encode, ).map( - lambda prn: prn.decode("ascii"), + lambda voucher: voucher.decode("ascii"), ) diff --git a/src/_secureaccesstokenauthorizer/tests/test_client_resource.py b/src/_secureaccesstokenauthorizer/tests/test_client_resource.py index 3bd4812c4fc639e510eb0227b809f002f9647645..2fc1651bfbfeee07f9f0b1d47d27aca98ef2ff5a 100644 --- a/src/_secureaccesstokenauthorizer/tests/test_client_resource.py +++ b/src/_secureaccesstokenauthorizer/tests/test_client_resource.py @@ -92,7 +92,7 @@ from treq.testing import ( ) from ..model import ( - PaymentReferenceStore, + VoucherStore, memory_connect, ) from ..resource import ( @@ -102,7 +102,7 @@ from ..resource import ( from .strategies import ( tahoe_configs, client_configurations, - payment_reference_numbers, + vouchers, requests, ) from .matchers import ( @@ -136,9 +136,9 @@ def is_not_json(bytestring): return True return False -def not_payment_reference_numbers(): +def not_vouchers(): """ - Builds unicode strings which are not legal payment reference numbers. + Builds unicode strings which are not legal vouchers. """ return one_of( text().filter( @@ -146,11 +146,11 @@ def not_payment_reference_numbers(): not is_urlsafe_base64(t) ), ), - payment_reference_numbers().map( - # Turn a valid PRN into a PRN that is invalid only by containing a - # character from the base64 alphabet in place of one from the - # urlsafe-base64 alphabet. - lambda prn: u"/" + prn[1:], + vouchers().map( + # Turn a valid voucher into a voucher that is invalid only by + # containing a character from the base64 alphabet in place of one + # from the urlsafe-base64 alphabet. + lambda voucher: u"/" + voucher[1:], ), ) @@ -169,19 +169,18 @@ def is_urlsafe_base64(text): def invalid_bodies(): """ - Build byte strings that ``PUT /payment-reference-number`` considers - invalid. + Build byte strings that ``PUT /voucher`` considers invalid. """ return one_of( # The wrong key but the right kind of value. fixed_dictionaries({ - u"some-key": payment_reference_numbers(), + u"some-key": vouchers(), }).map(dumps), # The right key but the wrong kind of value. fixed_dictionaries({ - u"payment-reference-number": one_of( + u"voucher": one_of( integers(), - not_payment_reference_numbers(), + not_vouchers(), ), }).map(dumps), # Not even JSON @@ -199,29 +198,29 @@ def root_from_config(config): """ return from_configuration( config, - PaymentReferenceStore.from_node_config( + VoucherStore.from_node_config( config, memory_connect, ), ) -class PaymentReferenceNumberTests(TestCase): +class VoucherTests(TestCase): """ - Tests relating to ``/payment-reference-number`` as implemented by the + Tests relating to ``/voucher`` as implemented by the ``_secureaccesstokenauthorizer.resource`` module and its handling of - payment reference numbers (PRNs). + vouchers. """ def setUp(self): - super(PaymentReferenceNumberTests, self).setUp() + super(VoucherTests, self).setUp() self.useFixture(CaptureTwistedLogs()) - @given(tahoe_configs_with_client_config, requests(just([u"payment-reference-number"]))) + @given(tahoe_configs_with_client_config, requests(just([u"voucher"]))) def test_reachable(self, get_config, request): """ - A resource is reachable at the ``payment-reference-number`` child of a the - resource returned by ``from_configuration``. + A resource is reachable at the ``voucher`` child of a the resource + returned by ``from_configuration``. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"tahoe"), b"tub.port") @@ -232,24 +231,24 @@ class PaymentReferenceNumberTests(TestCase): ) - @given(tahoe_configs_with_client_config, payment_reference_numbers()) - def test_put_prn(self, get_config, prn): + @given(tahoe_configs_with_client_config, vouchers()) + def test_put_voucher(self, get_config, voucher): """ - When a PRN is sent in a ``PUT`` to ``PaymentReferenceNumberCollection`` it - is passed in to the PRN redemption model object for handling and an - ``OK`` response is returned. + When a voucher is ``PUT`` to ``VoucherCollection`` it is passed in to the + redemption model object for handling and an ``OK`` response is + returned. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"tahoe"), b"tub.port") root = root_from_config(config) agent = RequestTraversalAgent(root) producer = FileBodyProducer( - BytesIO(dumps({u"payment-reference-number": prn})), + BytesIO(dumps({u"voucher": voucher})), cooperator=uncooperator(), ) requesting = agent.request( b"PUT", - b"http://127.0.0.1/payment-reference-number", + b"http://127.0.0.1/voucher", bodyProducer=producer, ) self.addDetail( @@ -266,9 +265,9 @@ class PaymentReferenceNumberTests(TestCase): @given(tahoe_configs_with_client_config, invalid_bodies()) def test_put_invalid_body(self, get_config, body): """ - If the body of a ``PUT`` to ``PaymentReferenceNumberCollection`` does not - consist of an object with a single *payment-reference-number* property - then the response is *BAD REQUEST*. + If the body of a ``PUT`` to ``VoucherCollection`` does not consist of an + object with a single *voucher* property then the response is *BAD + REQUEST*. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"tahoe"), b"tub.port") @@ -280,7 +279,7 @@ class PaymentReferenceNumberTests(TestCase): ) requesting = agent.request( b"PUT", - b"http://127.0.0.1/payment-reference-number", + b"http://127.0.0.1/voucher", bodyProducer=producer, ) self.addDetail( @@ -294,19 +293,19 @@ class PaymentReferenceNumberTests(TestCase): ), ) - @given(tahoe_configs_with_client_config, not_payment_reference_numbers()) - def test_get_invalid_prn(self, get_config, not_prn): + @given(tahoe_configs_with_client_config, not_vouchers()) + def test_get_invalid_voucher(self, get_config, not_voucher): """ - When a syntactically invalid PRN is requested with a ``GET`` to a child of - ``PaymentReferenceNumberCollection`` the response is **BAD REQUEST**. + When a syntactically invalid voucher is requested with a ``GET`` to a + child of ``VoucherCollection`` the response is **BAD REQUEST**. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"tahoe"), b"tub.port") root = root_from_config(config) agent = RequestTraversalAgent(root) - url = u"http://127.0.0.1/payment-reference-number/{}".format( + url = u"http://127.0.0.1/voucher/{}".format( quote( - not_prn.encode("utf-8"), + not_voucher.encode("utf-8"), safe=b"", ).decode("utf-8"), ).encode("ascii") @@ -322,12 +321,12 @@ class PaymentReferenceNumberTests(TestCase): ) - @given(tahoe_configs_with_client_config, payment_reference_numbers()) - def test_get_unknown_prn(self, get_config, prn): + @given(tahoe_configs_with_client_config, vouchers()) + def test_get_unknown_voucher(self, get_config, voucher): """ - When a PRN is requested with a ``GET`` to a child of - ``PaymentReferenceNumberCollection`` the response is **NOT FOUND** if - the PRN hasn't previously been submitted with a ``PUT``. + When a voucher is requested with a ``GET`` to a child of + ``VoucherCollection`` the response is **NOT FOUND** if the voucher + hasn't previously been submitted with a ``PUT``. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"tahoe"), b"tub.port") @@ -335,7 +334,7 @@ class PaymentReferenceNumberTests(TestCase): agent = RequestTraversalAgent(root) requesting = agent.request( b"GET", - u"http://127.0.0.1/payment-reference-number/{}".format(prn).encode("ascii"), + u"http://127.0.0.1/voucher/{}".format(voucher).encode("ascii"), ) self.assertThat( requesting, @@ -345,12 +344,12 @@ class PaymentReferenceNumberTests(TestCase): ) - @given(tahoe_configs_with_client_config, payment_reference_numbers()) - def test_get_known_prn(self, get_config, prn): + @given(tahoe_configs_with_client_config, vouchers()) + def test_get_known_voucher(self, get_config, voucher): """ - When a PRN is first ``PUT`` and then later a ``GET`` is issued for the - same PRN then the response code is **OK** and details about the PRN - are included in a json-encoded response body. + When a voucher is first ``PUT`` and then later a ``GET`` is issued for the + same voucher then the response code is **OK** and details about the + voucher are included in a json-encoded response body. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"tahoe"), b"tub.port") @@ -358,12 +357,12 @@ class PaymentReferenceNumberTests(TestCase): agent = RequestTraversalAgent(root) producer = FileBodyProducer( - BytesIO(dumps({u"payment-reference-number": prn})), + BytesIO(dumps({u"voucher": voucher})), cooperator=uncooperator(), ) putting = agent.request( b"PUT", - b"http://127.0.0.1/payment-reference-number", + b"http://127.0.0.1/voucher", bodyProducer=producer, ) self.assertThat( @@ -375,9 +374,9 @@ class PaymentReferenceNumberTests(TestCase): getting = agent.request( b"GET", - u"http://127.0.0.1/payment-reference-number/{}".format( + u"http://127.0.0.1/voucher/{}".format( quote( - prn.encode("utf-8"), + voucher.encode("utf-8"), safe=b"", ).decode("utf-8"), ).encode("ascii"), @@ -393,7 +392,7 @@ class PaymentReferenceNumberTests(TestCase): succeeded( Equals({ u"version": 1, - u"number": prn, + u"number": voucher, }), ), ), @@ -401,11 +400,11 @@ class PaymentReferenceNumberTests(TestCase): ), ) - @given(tahoe_configs_with_client_config, lists(payment_reference_numbers(), unique=True)) - def test_list_prns(self, get_config, prns): + @given(tahoe_configs_with_client_config, lists(vouchers(), unique=True)) + def test_list_vouchers(self, get_config, vouchers): """ - A ``GET`` to the ``PaymentReferenceNumberCollection`` itself returns a - list of existing payment reference numbers. + A ``GET`` to the ``VoucherCollection`` itself returns a list of existing + vouchers. """ # Hypothesis causes our test case instances to be re-used many times # between setUp and tearDown. Avoid re-using the same temporary @@ -416,16 +415,16 @@ class PaymentReferenceNumberTests(TestCase): root = root_from_config(config) agent = RequestTraversalAgent(root) - note("{} PRNs".format(len(prns))) + note("{} vouchers".format(len(vouchers))) - for prn in prns: + for voucher in vouchers: producer = FileBodyProducer( - BytesIO(dumps({u"payment-reference-number": prn})), + BytesIO(dumps({u"voucher": voucher})), cooperator=uncooperator(), ) putting = agent.request( b"PUT", - b"http://127.0.0.1/payment-reference-number", + b"http://127.0.0.1/voucher", bodyProducer=producer, ) self.assertThat( @@ -437,7 +436,7 @@ class PaymentReferenceNumberTests(TestCase): getting = agent.request( b"GET", - b"http://127.0.0.1/payment-reference-number", + b"http://127.0.0.1/voucher", ) self.assertThat( @@ -449,10 +448,10 @@ class PaymentReferenceNumberTests(TestCase): json_content, succeeded( Equals({ - u"payment-reference-numbers": list( - {u"version": 1, u"number": prn} - for prn - in prns + u"vouchers": list( + {u"version": 1, u"number": voucher} + for voucher + in vouchers ), }), ), diff --git a/src/_secureaccesstokenauthorizer/tests/test_model.py b/src/_secureaccesstokenauthorizer/tests/test_model.py index 3ef54d46d16e960fc2728f2abf46309bf1c49fc9..d700078e3ef31afde38b8799ea17b7033728bf02 100644 --- a/src/_secureaccesstokenauthorizer/tests/test_model.py +++ b/src/_secureaccesstokenauthorizer/tests/test_model.py @@ -55,21 +55,21 @@ from twisted.python.filepath import ( from ..model import ( SchemaError, StoreOpenError, - PaymentReferenceStore, - PaymentReference, + VoucherStore, + Voucher, open_and_initialize, memory_connect, ) from .strategies import ( tahoe_configs, - payment_reference_numbers, + vouchers, ) -class PaymentReferenceStoreTests(TestCase): +class VoucherStoreTests(TestCase): """ - Tests for ``PaymentReferenceStore``. + Tests for ``VoucherStore``. """ def test_create_mismatched_schema(self): """ @@ -87,91 +87,87 @@ class PaymentReferenceStoreTests(TestCase): ) - @given(tahoe_configs(), payment_reference_numbers()) - def test_get_missing(self, get_config, prn): + @given(tahoe_configs(), vouchers()) + def test_get_missing(self, get_config, voucher): """ - ``PaymentReferenceStore.get`` raises ``KeyError`` when called with a - payment reference number not previously added to the store. + ``VoucherStore.get`` raises ``KeyError`` when called with a + voucher not previously added to the store. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"node"), b"tub.port") - store = PaymentReferenceStore.from_node_config( + store = VoucherStore.from_node_config( config, memory_connect, ) self.assertThat( - lambda: store.get(prn), + lambda: store.get(voucher), raises(KeyError), ) - @given(tahoe_configs(), payment_reference_numbers()) - def test_add(self, get_config, prn): + @given(tahoe_configs(), vouchers()) + def test_add(self, get_config, voucher): """ - ``PaymentReferenceStore.get`` returns a ``PaymentReference`` representing - a payment reference previously added to the store with - ``PaymentReferenceStore.add``. + ``VoucherStore.get`` returns a ``Voucher`` representing a voucher + previously added to the store with ``VoucherStore.add``. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"node"), b"tub.port") - store = PaymentReferenceStore.from_node_config( + store = VoucherStore.from_node_config( config, memory_connect, ) - store.add(prn) - payment_reference = store.get(prn) + store.add(voucher) self.assertThat( - payment_reference, + store.get(voucher), MatchesStructure( - number=Equals(prn), + number=Equals(voucher), ), ) - @given(tahoe_configs(), payment_reference_numbers()) - def test_add_idempotent(self, get_config, prn): + @given(tahoe_configs(), vouchers()) + def test_add_idempotent(self, get_config, voucher): """ - More than one call to ``PaymentReferenceStore.add`` with the same argument - results in the same state as a single call. + More than one call to ``VoucherStore.add`` with the same argument results + in the same state as a single call. """ tempdir = self.useFixture(TempDir()) config = get_config(tempdir.join(b"node"), b"tub.port") - store = PaymentReferenceStore.from_node_config( + store = VoucherStore.from_node_config( config, memory_connect, ) - store.add(prn) - store.add(prn) - payment_reference = store.get(prn) + store.add(voucher) + store.add(voucher) self.assertThat( - payment_reference, + store.get(voucher), MatchesStructure( - number=Equals(prn), + number=Equals(voucher), ), ) - @given(tahoe_configs(), lists(payment_reference_numbers())) - def test_list(self, get_config, prns): + @given(tahoe_configs(), lists(vouchers())) + def test_list(self, get_config, vouchers): """ - ``PaymentReferenceStore.list`` returns a ``list`` containing a - ``PaymentReference`` object for each payment reference number - previously added. + ``VoucherStore.list`` returns a ``list`` containing a ``Voucher`` object + for each voucher previously added. """ tempdir = self.useFixture(TempDir()) nodedir = tempdir.join(b"node") config = get_config(nodedir, b"tub.port") - store = PaymentReferenceStore.from_node_config( + store = VoucherStore.from_node_config( config, memory_connect, ) - for prn in prns: - store.add(prn) + for voucher in vouchers: + store.add(voucher) self.assertThat( store.list(), AfterPreprocessing( lambda refs: set(ref.number for ref in refs), - Equals(set(prns)), + Equals(set(vouchers)), ), ) @@ -180,8 +176,7 @@ class PaymentReferenceStoreTests(TestCase): def test_uncreateable_store_directory(self, get_config): """ If the underlying directory in the node configuration cannot be created - then ``PaymentReferenceStore.from_node_config`` raises - ``StoreOpenError``. + then ``VoucherStore.from_node_config`` raises ``StoreOpenError``. """ tempdir = self.useFixture(TempDir()) nodedir = tempdir.join(b"node") @@ -193,7 +188,7 @@ class PaymentReferenceStoreTests(TestCase): config = get_config(nodedir, b"tub.port") self.assertThat( - lambda: PaymentReferenceStore.from_node_config( + lambda: VoucherStore.from_node_config( config, memory_connect, ), @@ -220,7 +215,7 @@ class PaymentReferenceStoreTests(TestCase): def test_unopenable_store(self, get_config): """ If the underlying database file cannot be opened then - ``PaymentReferenceStore.from_node_config`` raises ``StoreOpenError``. + ``VoucherStore.from_node_config`` raises ``StoreOpenError``. """ tempdir = self.useFixture(TempDir()) nodedir = tempdir.join(b"node") @@ -228,30 +223,30 @@ class PaymentReferenceStoreTests(TestCase): config = get_config(nodedir, b"tub.port") # Create the underlying database file. - store = PaymentReferenceStore.from_node_config(config) + store = VoucherStore.from_node_config(config) # Prevent further access to it. store.database_path.chmod(0o000) self.assertThat( - lambda: PaymentReferenceStore.from_node_config( + lambda: VoucherStore.from_node_config( config, ), raises(StoreOpenError), ) -class PaymentReferenceTests(TestCase): +class VoucherTests(TestCase): """ - Tests for ``PaymentReference``. + Tests for ``Voucher``. """ - @given(payment_reference_numbers()) - def test_json_roundtrip(self, prn): + @given(vouchers()) + def test_json_roundtrip(self, voucher): """ - ``PaymentReference.to_json . PaymentReference.from_json → id`` + ``Voucher.to_json . Voucher.from_json → id`` """ - ref = PaymentReference(prn) + ref = Voucher(voucher) self.assertThat( - PaymentReference.from_json(ref.to_json()), + Voucher.from_json(ref.to_json()), Equals(ref), )