def test_checkout_where_there_is_not_enough_stock(self): ''' test check out where there is not enough stock ''' # create a user user = User.objects.create_user('username', '*****@*****.**', 'password') # login user self.client.login(username='******', password='******') # create a product item = Product(name="PRODUCT NAME ", available_stock=100, content="PRODUCT CONTENT", price=30, image="img.jpg", num_of_ratings=1, average_rating=5) # save product item.save() # add the quantity needed to the product id in the cart self.client.post("/cart/add/{0}".format(item.id), data={'quantity': '20'}, follow=True) # change the available stock to less than quantity in cart item.available_stock = 10 # save the product item.save() # set the strip_id to the publishable stripe id stripe_id = settings.STRIPE_PUBLISHABLE # post the customer details and credit card details # to the checkout form page = self.client.post("/checkout/", { 'full_name': 'name', 'phone_number': '123', 'street_address1': 'my', 'street_address2': 'address is', 'town_or_city': 'kk', 'county': 'ireland', 'country': 'ireland', 'postcode': 'eircode', 'credit_card_number': '4242424242424242', 'cvv': '111', 'expiry_month': '2', 'expiry_year': '2019', 'stripe_id': stripe_id }, follow=True) # check the status code is 200 self.assertEqual(page.status_code, 200) # check Template Used is cart.html page self.assertTemplateUsed(page, "cart.html") # check the message stored is equal to the expected message messages = list(get_messages(page.wsgi_request)) self.assertEqual( str(messages[0]), 'We have limited stock available we have ' 'amended your cart to the maximum available ' 'at this time. <br> Some Items may have been ' 'removed due to no stock availability <br> ' 'Please check your cart and checkout again ' 'once you are happy to do so')