Changeset - 77a7689de50a
[Not reviewed]
0 1 0
Christopher Neugebauer - 8 years ago 2016-09-15 01:28:14
chrisjrn@gmail.com
Fixes credit note tests that were broken with the old behaviour
1 file changed with 23 insertions and 12 deletions:
0 comments (0 inline, 0 general)
registrasion/tests/test_credit_note.py
Show inline comments
...
 
@@ -86,24 +86,30 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 
        # The total paid should be zero
 
        self.assertEqual(0, invoice.total_payments())
 
        self.assertTrue(invoice.invoice.is_refunded)
 

	
 
        # There should be a credit note generated out of the invoice.
 
        credit_notes = commerce.CreditNote.objects.filter(
 
            invoice=invoice.invoice,
 
        )
 
        self.assertEqual(1, credit_notes.count())
 
        self.assertEqual(to_pay, credit_notes[0].value)
 

	
 
    def test_apply_credit_note_pays_invoice(self):
 

	
 
        # Create a manual invoice (stops credit notes from being auto-applied)
 
        self._manual_invoice(1)
 

	
 
        # Begin the test
 

	
 
        invoice = self._invoice_containing_prod_1(1)
 

	
 
        to_pay = invoice.invoice.value
 
        invoice.pay("Reference", to_pay)
 
        self.assertTrue(invoice.invoice.is_paid)
 

	
 
        invoice.refund()
 

	
 
        # There should be one credit note generated out of the invoice.
 
        cn = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        # That credit note should be in the unclaimed pile
...
 
@@ -113,62 +119,65 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_1, 1)
 

	
 
        invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart))
 

	
 
        cn.apply_to_invoice(invoice2.invoice)
 
        self.assertTrue(invoice2.invoice.is_paid)
 

	
 
        # That invoice should not show up as unclaimed any more
 
        self.assertEquals(0, commerce.CreditNote.unclaimed().count())
 

	
 
    def test_apply_credit_note_generates_new_credit_note_if_overpaying(self):
 

	
 
        # Create and refund an invoice, generating a credit note.
 
        invoice = self._invoice_containing_prod_1(2)
 

	
 
        to_pay = invoice.invoice.value
 
        invoice.pay("Reference", to_pay)
 
        invoice.pay("Reference", invoice.invoice.value)
 
        self.assertTrue(invoice.invoice.is_paid)
 

	
 
        invoice.refund()
 

	
 
        # There should be one credit note generated out of the invoice.
 
        cn = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        self.assertEquals(1, commerce.CreditNote.unclaimed().count())
 

	
 
        # Create a new cart (of half value of inv 1) and get invoice
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_1, 1)
 

	
 
        invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart))
 

	
 
        cn.apply_to_invoice(invoice2.invoice)
 
        # Create a new invoice for a cart of half value of inv 1
 
        invoice2 = self._invoice_containing_prod_1(1)
 
        # Credit note is automatically applied by generating the new invoice
 
        self.assertTrue(invoice2.invoice.is_paid)
 

	
 
        # We generated a new credit note, and spent the old one,
 
        # unclaimed should still be 1.
 
        self.assertEquals(1, commerce.CreditNote.unclaimed().count())
 

	
 
        credit_note2 = commerce.CreditNote.objects.get(
 
            invoice=invoice2.invoice,
 
        )
 

	
 
        # The new credit note should be the residual of the cost of cart 1
 
        # minus the cost of cart 2.
 
        self.assertEquals(
 
            invoice.invoice.value - invoice2.invoice.value,
 
            credit_note2.value,
 
        )
 

	
 
    def test_cannot_apply_credit_note_on_invalid_invoices(self):
 

	
 
        # Disable auto-application of invoices.
 
        self._manual_invoice(1)
 

	
 
        # And now start the actual test.
 

	
 
        invoice = self._invoice_containing_prod_1(1)
 

	
 
        to_pay = invoice.invoice.value
 
        invoice.pay("Reference", to_pay)
 
        self.assertTrue(invoice.invoice.is_paid)
 

	
 
        invoice.refund()
 

	
 
        # There should be one credit note generated out of the invoice.
 
        cn = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        # Create a new cart with invoice, pay it
...
 
@@ -228,25 +237,27 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 

	
 
        invoice.refund()
 

	
 
        self.assertEquals(1, commerce.CreditNote.unclaimed().count())
 

	
 
        cn = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        # Create a new cart with invoice
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_1, 1)
 

	
 
        invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart))
 
        cn.apply_to_invoice(invoice_2.invoice)
 
        with self.assertRaises(ValidationError):
 
            # Creating `invoice_2` will automatically apply `cn`.
 
            cn.apply_to_invoice(invoice_2.invoice)
 

	
 
        self.assertEquals(0, commerce.CreditNote.unclaimed().count())
 

	
 
        # Cannot refund this credit note as it is already applied.
 
        with self.assertRaises(ValidationError):
 
            cn.refund()
 

	
 
    def test_money_into_void_invoice_generates_credit_note(self):
 
        invoice = self._invoice_containing_prod_1(1)
 
        invoice.void()
 

	
 
        val = invoice.invoice.value
...
 
@@ -292,27 +303,27 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 

	
 
        invoice = self._invoice_containing_prod_1(1)
 

	
 
        # Now get a credit note
 
        invoice.pay("Lol", invoice.invoice.value)
 
        invoice.refund()
 
        cn = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        # Create a cart of higher value than the credit note
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_1, 2)
 

	
 
        # Create a current invoice, and apply partial payments
 
        # Create a current invoice
 
        # This will automatically apply `cn` to the invoice
 
        invoice = TestingInvoiceController.for_cart(cart.cart)
 
        cn.apply_to_invoice(invoice.invoice)
 

	
 
        # Adding to cart will mean that the old invoice for this cart
 
        # will be invalidated. A new invoice should be generated.
 
        cart.add_to_cart(self.PROD_1, 1)
 
        invoice = TestingInvoiceController.for_id(invoice.invoice.id)
 
        invoice2 = TestingInvoiceController.for_cart(cart.cart)
 
        cn2 = self._credit_note_for_invoice(invoice.invoice)
 

	
 
        invoice._refresh()
 

	
 
        # The first invoice should be refunded
 
        self.assertEquals(
0 comments (0 inline, 0 general)