Skip to content

Commit

Permalink
Cart line products are checked for inventory before payment #7588
Browse files Browse the repository at this point in the history
  • Loading branch information
PritishC committed Apr 21, 2015
1 parent 1347b6d commit 234638f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
52 changes: 52 additions & 0 deletions tests/test_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '[email protected]',
'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"
Expand Down

0 comments on commit 234638f

Please sign in to comment.