def setUp(self): self.client = Client() home_url = urlresolvers.reverse('catalog_home') self.checkout_url = urlresolvers.reverse('checkout') self.client.get(home_url) # need to create customer with a shopping cart first self.item = CartItem() product = Product.active.all()[0] self.item.product = product self.item.cart_id = self.client.session[cart.CART_ID_SESSION_KEY] self.item.quantity = 1 self.item.save()
class CheckoutTestCase(TestCase): """ tests checkout form page functionality """ def setUp(self): self.client = Client() home_url = urlresolvers.reverse('catalog_home') self.checkout_url = urlresolvers.reverse('checkout') self.client.get(home_url) # need to create customer with a shopping cart first self.item = CartItem() product = Product.active.all()[0] self.item.product = product self.item.cart_id = self.client.session[cart.CART_ID_SESSION_KEY] self.item.quantity = 1 self.item.save() def test_checkout_page_empty_cart(self): """ empty cart should be redirected to cart page """ client = Client() cart_url = urlresolvers.reverse('show_cart') response = client.get(self.checkout_url) self.assertRedirects(response, cart_url) def test_checkout_page(self): """ with at least one cart item, request for checkout page URL is successful """ response = self.client.get(self.checkout_url) self.assertEqual(response.status_code, httplib.OK) self.assertContains(response, "Checkout") url_entry = urlresolvers.resolve(self.checkout_url) template_name = url_entry[2]['template_name'] self.assertTemplateUsed(response, template_name) def test_submit_empty_form(self): """ empty order form raises 'required' error message for required order form fields """ form = CheckoutForm() response = self.client.post(self.checkout_url, form.initial) for name, field in form.fields.iteritems(): value = form.fields[name] if not value and form.fields[name].required: error_msg = form.fields[name].error_messages['required'] self.assertFormError(response, "form", name, [error_msg])
def add_to_cart(request, venue_id): """ function that takes a POST request and adds a product instance to the current customer's shopping cart """ postdata = request.POST.copy() # get product slug from post data, return blank if empty product_slug = postdata.get('product_slug','') # get quantity added, return 1 if empty quantity = postdata.get('quantity',1) # fetch the product or return a missing page error p = get_object_or_404(Product, slug=product_slug) #get products in cart cart_products = get_cart_items(request, venue_id) product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id == p.id: # update the quantity if found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: # create and save a new cart item ci = CartItem() ci.product = p ci.quantity = quantity ci.venue_id = venue_id ci.cart_id = _cart_id(request, venue_id) ci.user = None if request.user.is_authenticated(): ci.user = request.user else: ci.user = request.session['anonymous'] ci.xmpp_jid = request.session['jid_session'] ci.save()