Adds validate_allowed_to_pay(), which validates whether you’re allowed to pay for an invoice
This commit is contained in:
		
							parent
							
								
									0e80e0336c
								
							
						
					
					
						commit
						2fbe789090
					
				
					 4 changed files with 44 additions and 12 deletions
				
			
		|  | @ -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. ''' | ||||
| 
 | ||||
|  |  | |||
|  | @ -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( | ||||
|  |  | |||
|  | @ -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): | ||||
| 
 | ||||
|  |  | |||
|  | @ -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,6 +199,19 @@ 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 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Christopher Neugebauer
						Christopher Neugebauer