From 234638ff44ec4ac6a7d4081e85646240e1240e39 Mon Sep 17 00:00:00 2001 From: VaticanCameos Date: Tue, 21 Apr 2015 14:49:27 +0530 Subject: [PATCH] Cart line products are checked for inventory before payment #7588 --- checkout.py | 11 +++++++++ tests/test_payment.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/checkout.py b/checkout.py index f319c46..67e69c0 100644 --- a/checkout.py +++ b/checkout.py @@ -746,6 +746,17 @@ def payment_method(cls): if not cart.sale.shipment_address: return redirect(url_for('nereid.checkout.shipping_address')) + # Check if any goods product is present in cart sale lines which + # is out of stock + if any( + not line.product.can_buy + for line in cart.sale.lines + ): + flash(_( + "Some items in your cart are out of stock. Please remove them." + )) + abort(redirect(url_for('nereid.cart.view_cart'))) + payment_form = cls.get_payment_form() credit_card_form = cls.get_credit_card_form() diff --git a/tests/test_payment.py b/tests/test_payment.py index 30a947e..f563950 100644 --- a/tests/test_payment.py +++ b/tests/test_payment.py @@ -1467,6 +1467,58 @@ def test_3340_regd_new_invalid_credit_card_details(self): self.assertEqual(rv.status_code, 302) self.assertTrue('/payment' in rv.location) + def test_4000_line_cannot_buy_product(self): + """ + Test that payment will fail if a product becomes out of stock in the + middle of payment. + """ + with Transaction().start(DB_NAME, USER, context=CONTEXT): + self.setup_defaults() + app = self.get_app() + + country = self.Country(self.available_countries[0]) + subdivision = country.subdivisions[0] + + with app.test_client() as c: + c.post( + '/cart/add', data={ + 'product': self.product1.id, 'quantity': 5 + } + ) + + # Sign-in + rv = c.post( + '/checkout/sign-in', data={ + 'email': 'new@example.com', + 'checkout_mode': 'guest', + } + ) + rv = c.post( + '/checkout/shipping-address', + data={ + 'name': 'Sharoon Thomas', + 'street': 'Biscayne Boulevard', + 'streetbis': 'Apt. 1906, Biscayne Park', + 'zip': 'FL33137', + 'city': 'Miami', + 'country': country.id, + 'subdivision': subdivision.id, + } + ) + + # Set min_warehouse_quantity to some positive number + self.product1.min_warehouse_quantity = 5 + self.product1.save() + + # Post to payment delivery-address with same flag + rv = c.post( + '/checkout/payment', + data={'use_shipment_address': 'True'} + ) + # Redirect back to cart + self.assertEqual(rv.status_code, 302) + self.assertTrue(rv.location.endswith('/cart')) + def suite(): "Checkout test suite"