From 3a3695e48fbb68fb061f98563f2869128a9a5cbb Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Tue, 19 Nov 2019 10:26:48 -0500 Subject: [PATCH] Present vouchers correctly in the list-based interface --- src/_zkapauthorizer/resource.py | 3 +- .../tests/test_client_resource.py | 71 ++++++++++++++----- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/_zkapauthorizer/resource.py b/src/_zkapauthorizer/resource.py index 1601d70..fb7e61a 100644 --- a/src/_zkapauthorizer/resource.py +++ b/src/_zkapauthorizer/resource.py @@ -196,7 +196,7 @@ class _VoucherCollection(Resource): application_json(request) return dumps({ u"vouchers": list( - voucher.marshal() + self._controller.incorporate_transient_state(voucher).marshal() for voucher in self._store.list() ), @@ -211,7 +211,6 @@ class _VoucherCollection(Resource): voucher = self._store.get(voucher) except KeyError: return NoResource() - # TODO Apply the same treatment to the list result return VoucherView(self._controller.incorporate_transient_state(voucher)) diff --git a/src/_zkapauthorizer/tests/test_client_resource.py b/src/_zkapauthorizer/tests/test_client_resource.py index 22d2f54..6535744 100644 --- a/src/_zkapauthorizer/tests/test_client_resource.py +++ b/src/_zkapauthorizer/tests/test_client_resource.py @@ -776,6 +776,59 @@ class VoucherTests(TestCase): A ``GET`` to the ``VoucherCollection`` itself returns a list of existing vouchers. """ + return self._test_list_vouchers( + get_config, + now, + vouchers, + Equals({ + u"vouchers": list( + Voucher( + voucher, + created=now, + state=Redeemed( + finished=now, + # Value duplicated from + # PaymentController.redeem + # default. Should do this better. + token_count=100, + ), + ).marshal() + for voucher + in vouchers + ), + }), + ) + + @given( + tahoe_configs(client_unpaidredeemer_configurations()), + datetimes(), + lists(vouchers(), unique=True), + ) + def test_list_vouchers_transient_states(self, get_config, now, vouchers): + """ + A ``GET`` to the ``VoucherCollection`` itself returns a list of existing + vouchers including state information that reflects transient states. + """ + return self._test_list_vouchers( + get_config, + now, + vouchers, + Equals({ + u"vouchers": list( + Voucher( + voucher, + created=now, + state=Unpaid( + finished=now, + ), + ).marshal() + for voucher + in vouchers + ), + }), + ) + + def _test_list_vouchers(self, get_config, now, vouchers, match_response_object): # Hypothesis causes our test case instances to be re-used many times # between setUp and tearDown. Avoid re-using the same temporary # directory for every Hypothesis iteration because this test leaves @@ -817,23 +870,7 @@ class VoucherTests(TestCase): AfterPreprocessing( json_content, succeeded( - Equals({ - u"vouchers": list( - Voucher( - voucher, - created=now, - state=Redeemed( - finished=now, - # Value duplicated from - # PaymentController.redeem - # default. Should do this better. - token_count=100, - ), - ).marshal() - for voucher - in vouchers - ), - }), + match_response_object, ), ), ), -- GitLab