From 2fbe789090861dabf5c7c0481995c378fbaf4e8b Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 7 Apr 2016 18:26:31 +1000 Subject: [PATCH] =?UTF-8?q?Adds=20validate=5Fallowed=5Fto=5Fpay(),=20which?= =?UTF-8?q?=20validates=20whether=20you=E2=80=99re=20allowed=20to=20pay=20?= =?UTF-8?q?for=20an=20invoice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- registrasion/controllers/invoice.py | 24 ++++++++++++++++++++++++ registrasion/tests/controller_helpers.py | 11 +---------- registrasion/tests/test_cart.py | 4 ++++ registrasion/tests/test_invoice.py | 17 +++++++++++++++-- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/registrasion/controllers/invoice.py b/registrasion/controllers/invoice.py index f7ae13e5..c5a527ca 100644 --- a/registrasion/controllers/invoice.py +++ b/registrasion/controllers/invoice.py @@ -126,6 +126,30 @@ class InvoiceController(object): return invoice + def _refresh(self): + ''' Refreshes the underlying invoice and cart objects. ''' + self.invoice.refresh_from_db() + if self.invoice.cart: + self.invoice.cart.refresh_from_db() + + def validate_allowed_to_pay(self): + ''' Passes cleanly if we're allowed to pay, otherwise raise + a ValidationError. ''' + + self._refresh() + + if not self.invoice.is_unpaid: + raise ValidationError("You can only pay for unpaid invoices.") + + if not self.invoice.cart: + return + + if not self._invoice_matches_cart(): + raise ValidationError("The registration has been amended since " + "generating this invoice.") + + CartController(self.invoice.cart).validate_cart() + def total_payments(self): ''' Returns the total amount paid towards this invoice. ''' diff --git a/registrasion/tests/controller_helpers.py b/registrasion/tests/controller_helpers.py index 60cf2346..476351dd 100644 --- a/registrasion/tests/controller_helpers.py +++ b/registrasion/tests/controller_helpers.py @@ -37,17 +37,8 @@ class TestingInvoiceController(InvoiceController): def pay(self, reference, amount): ''' Testing method for simulating an invoice paymenht by the given amount. ''' - if self.invoice.cart: - cart = CartController(self.invoice.cart) - cart.validate_cart() # Raises ValidationError if invalid - status = self.invoice.status - if status == rego.Invoice.STATUS_VOID: - raise ValidationError("Void invoices cannot be paid") - elif status == rego.Invoice.STATUS_PAID: - raise ValidationError("Paid invoices cannot be paid again") - elif status == rego.Invoice.STATUS_REFUNDED: - raise ValidationError("Refunded invoices cannot be paid") + self.validate_allowed_to_pay() ''' Adds a payment ''' payment = rego.ManualPayment.objects.create( diff --git a/registrasion/tests/test_cart.py b/registrasion/tests/test_cart.py index e87ef37e..f8a82c21 100644 --- a/registrasion/tests/test_cart.py +++ b/registrasion/tests/test_cart.py @@ -148,6 +148,10 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase): voucher.save() return voucher + @classmethod + def reget(cls, object): + return type(object).objects.get(id=object.id) + class BasicCartTests(RegistrationCartTestCase): diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index 645dfb80..b121bfa1 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -180,7 +180,7 @@ class InvoiceTestCase(RegistrationCartTestCase): invoice_1.void() with self.assertRaises(ValidationError): - invoice_1.pay("Reference", invoice_1.invoice.value) + invoice_1.validate_allowed_to_pay() def test_cannot_void_paid_invoice(self): current_cart = TestingCartController.for_user(self.USER_1) @@ -199,9 +199,22 @@ class InvoiceTestCase(RegistrationCartTestCase): with self.assertRaises(ValidationError): invoice_1 = TestingInvoiceController.for_cart(current_cart.cart) + def test_cannot_pay_implicitly_void_invoice(self): + cart = TestingCartController.for_user(self.USER_1) + cart.add_to_cart(self.PROD_1, 1) + invoice = TestingInvoiceController.for_cart(self.reget(cart.cart)) + + # Implicitly void the invoice + cart.add_to_cart(self.PROD_1, 1) + + with self.assertRaises(ValidationError): + invoice.validate_allowed_to_pay() + + + # TODO: test partially paid invoice cannot be void until payments # are refunded # TODO: test overpaid invoice results in credit note - # TODO: test credit note generation more generally + # TODO: test credit note generation more generally