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 867b7cbc21768908de493898749229499f17db4c..3027761be74ff61961e752071f6e8f8b293fdc02 100644 --- a/src/_secureaccesstokenauthorizer/model.py +++ b/src/_secureaccesstokenauthorizer/model.py @@ -160,7 +160,7 @@ class VoucherStore(object): ) @with_cursor - def get(self, cursor, prn): + def get(self, cursor, voucher): cursor.execute( """ SELECT @@ -170,20 +170,20 @@ class VoucherStore(object): WHERE [number] = ? """, - (prn,), + (voucher,), ) refs = cursor.fetchall() if len(refs) == 0: - raise KeyError(prn) + 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 [vouchers] VALUES (?) """, - (prn,) + (voucher,) ) @with_cursor diff --git a/src/_secureaccesstokenauthorizer/resource.py b/src/_secureaccesstokenauthorizer/resource.py index d75666843cf0ae376177eccdbdf05339fd88c088..97743a5bc35842ded2b4c3dcd1c05c38109189b0 100644 --- a/src/_secureaccesstokenauthorizer/resource.py +++ b/src/_secureaccesstokenauthorizer/resource.py @@ -100,11 +100,11 @@ class _VoucherCollection(Resource): return bad_request().render(request) if payload.keys() != [u"voucher"]: return bad_request().render(request) - prn = payload[u"voucher"] - 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"" @@ -112,42 +112,41 @@ class _VoucherCollection(Resource): request.responseHeaders.setRawHeaders(u"content-type", [u"application/json"]) return dumps({ u"vouchers": list( - prn.marshal() - for prn + 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: - voucher = self._store.get(prn) + voucher = self._store.get(voucher) except KeyError: return NoResource() 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 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. + :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 diff --git a/src/_secureaccesstokenauthorizer/tests/strategies.py b/src/_secureaccesstokenauthorizer/tests/strategies.py index c56b8cecef8a8ee4f417d30e63b86956b0c52315..38f4afa502b565ee64799250d7aa22d30e36644e 100644 --- a/src/_secureaccesstokenauthorizer/tests/strategies.py +++ b/src/_secureaccesstokenauthorizer/tests/strategies.py @@ -171,7 +171,7 @@ def vouchers(): ).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 89af191bcfca56f01b5719b9cebf18adab191f74..2fc1651bfbfeee07f9f0b1d47d27aca98ef2ff5a 100644 --- a/src/_secureaccesstokenauthorizer/tests/test_client_resource.py +++ b/src/_secureaccesstokenauthorizer/tests/test_client_resource.py @@ -150,7 +150,7 @@ def not_vouchers(): # 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 prn: u"/" + prn[1:], + lambda voucher: u"/" + voucher[1:], ), ) @@ -232,7 +232,7 @@ class VoucherTests(TestCase): @given(tahoe_configs_with_client_config, vouchers()) - def test_put_prn(self, get_config, prn): + def test_put_voucher(self, get_config, voucher): """ When a voucher is ``PUT`` to ``VoucherCollection`` it is passed in to the redemption model object for handling and an ``OK`` response is @@ -243,7 +243,7 @@ class VoucherTests(TestCase): root = root_from_config(config) agent = RequestTraversalAgent(root) producer = FileBodyProducer( - BytesIO(dumps({u"voucher": prn})), + BytesIO(dumps({u"voucher": voucher})), cooperator=uncooperator(), ) requesting = agent.request( @@ -294,7 +294,7 @@ class VoucherTests(TestCase): ) @given(tahoe_configs_with_client_config, not_vouchers()) - def test_get_invalid_prn(self, get_config, not_prn): + def test_get_invalid_voucher(self, get_config, not_voucher): """ When a syntactically invalid voucher is requested with a ``GET`` to a child of ``VoucherCollection`` the response is **BAD REQUEST**. @@ -305,7 +305,7 @@ class VoucherTests(TestCase): agent = RequestTraversalAgent(root) 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,7 +322,7 @@ class VoucherTests(TestCase): @given(tahoe_configs_with_client_config, vouchers()) - def test_get_unknown_prn(self, get_config, prn): + def test_get_unknown_voucher(self, get_config, voucher): """ When a voucher is requested with a ``GET`` to a child of ``VoucherCollection`` the response is **NOT FOUND** if the voucher @@ -334,7 +334,7 @@ class VoucherTests(TestCase): agent = RequestTraversalAgent(root) requesting = agent.request( b"GET", - u"http://127.0.0.1/voucher/{}".format(prn).encode("ascii"), + u"http://127.0.0.1/voucher/{}".format(voucher).encode("ascii"), ) self.assertThat( requesting, @@ -345,7 +345,7 @@ class VoucherTests(TestCase): @given(tahoe_configs_with_client_config, vouchers()) - def test_get_known_prn(self, get_config, prn): + def test_get_known_voucher(self, get_config, voucher): """ 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 @@ -357,7 +357,7 @@ class VoucherTests(TestCase): agent = RequestTraversalAgent(root) producer = FileBodyProducer( - BytesIO(dumps({u"voucher": prn})), + BytesIO(dumps({u"voucher": voucher})), cooperator=uncooperator(), ) putting = agent.request( @@ -376,7 +376,7 @@ class VoucherTests(TestCase): b"GET", u"http://127.0.0.1/voucher/{}".format( quote( - prn.encode("utf-8"), + voucher.encode("utf-8"), safe=b"", ).decode("utf-8"), ).encode("ascii"), @@ -392,7 +392,7 @@ class VoucherTests(TestCase): succeeded( Equals({ u"version": 1, - u"number": prn, + u"number": voucher, }), ), ), @@ -401,7 +401,7 @@ class VoucherTests(TestCase): ) @given(tahoe_configs_with_client_config, lists(vouchers(), unique=True)) - def test_list_prns(self, get_config, prns): + def test_list_vouchers(self, get_config, vouchers): """ A ``GET`` to the ``VoucherCollection`` itself returns a list of existing vouchers. @@ -415,11 +415,11 @@ class VoucherTests(TestCase): root = root_from_config(config) agent = RequestTraversalAgent(root) - note("{} vouchers".format(len(prns))) + note("{} vouchers".format(len(vouchers))) - for prn in prns: + for voucher in vouchers: producer = FileBodyProducer( - BytesIO(dumps({u"voucher": prn})), + BytesIO(dumps({u"voucher": voucher})), cooperator=uncooperator(), ) putting = agent.request( @@ -449,9 +449,9 @@ class VoucherTests(TestCase): succeeded( Equals({ u"vouchers": list( - {u"version": 1, u"number": prn} - for prn - in prns + {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 f8af40d578e1f8c257898539e0915b34e460a5b3..20f835c3ecfeaeda230da37f3c25542e15213f9b 100644 --- a/src/_secureaccesstokenauthorizer/tests/test_model.py +++ b/src/_secureaccesstokenauthorizer/tests/test_model.py @@ -88,7 +88,7 @@ class VoucherStoreTests(TestCase): @given(tahoe_configs(), vouchers()) - def test_get_missing(self, get_config, prn): + def test_get_missing(self, get_config, voucher): """ ``VoucherStore.get`` raises ``KeyError`` when called with a voucher not previously added to the store. @@ -100,12 +100,12 @@ class VoucherStoreTests(TestCase): memory_connect, ) self.assertThat( - lambda: store.get(prn), + lambda: store.get(voucher), raises(KeyError), ) @given(tahoe_configs(), vouchers()) - def test_add(self, get_config, prn): + def test_add(self, get_config, voucher): """ ``VoucherStore.get`` returns a ``Voucher`` representing a voucher previously added to the store with ``VoucherStore.add``. @@ -116,17 +116,17 @@ class VoucherStoreTests(TestCase): config, memory_connect, ) - store.add(prn) - voucher = store.get(prn) + store.add(voucher) + voucher = store.get(voucher) self.assertThat( voucher, MatchesStructure( - number=Equals(prn), + number=Equals(voucher), ), ) @given(tahoe_configs(), vouchers()) - def test_add_idempotent(self, get_config, prn): + def test_add_idempotent(self, get_config, voucher): """ More than one call to ``VoucherStore.add`` with the same argument results in the same state as a single call. @@ -137,19 +137,19 @@ class VoucherStoreTests(TestCase): config, memory_connect, ) - store.add(prn) - store.add(prn) - voucher = store.get(prn) + store.add(voucher) + store.add(voucher) + voucher = store.get(voucher) self.assertThat( voucher, MatchesStructure( - number=Equals(prn), + number=Equals(voucher), ), ) @given(tahoe_configs(), lists(vouchers())) - def test_list(self, get_config, prns): + def test_list(self, get_config, vouchers): """ ``VoucherStore.list`` returns a ``list`` containing a ``Voucher`` object for each voucher previously added. @@ -162,14 +162,14 @@ class VoucherStoreTests(TestCase): 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)), ), ) @@ -243,11 +243,11 @@ class VoucherTests(TestCase): Tests for ``Voucher``. """ @given(vouchers()) - def test_json_roundtrip(self, prn): + def test_json_roundtrip(self, voucher): """ ``Voucher.to_json . Voucher.from_json → id`` """ - ref = Voucher(prn) + ref = Voucher(voucher) self.assertThat( Voucher.from_json(ref.to_json()), Equals(ref),