def test_post_checkout_billing(self): """ Test using an alternate shipping address in the checkout view """ country = CountryFactory() request = RequestFactory().post( reverse('longclaw_checkout_view'), { 'shipping-name': 'bob', 'shipping-line_1': 'blah blah', 'shipping-postcode': 'ytxx 23x', 'shipping-city': 'London', 'shipping-country': country.pk, 'billing-name': 'john', 'billing-line_1': 'somewhere', 'billing-postcode': 'lmewrewr', 'billing-city': 'London', 'billing-country': country.pk, 'email': '*****@*****.**', 'different_billing_address': True }) request.session = {} bid = basket_id(request) BasketItemFactory(basket_id=bid) response = CheckoutView.as_view()(request) self.assertEqual(response.status_code, 302)
def setUp(self): """Create a basket with things in it """ request = RequestFactory().get('/') request.session = {} bid = basket_id(request) self.item = BasketItemFactory(basket_id=bid) BasketItemFactory(basket_id=bid)
def item_count(self, request, variant_id=None): """ Get quantity of a single item in the basket """ bid = utils.basket_id(request) item = ProductVariant.objects.get(id=variant_id) try: count = BasketItem.objects.get(basket_id=bid, variant=item).quantity except BasketItem.DoesNotExist: count = 0 return Response(data={"quantity": count}, status=status.HTTP_200_OK)
def get_item_count(request): ''' Get quantity of a single item in the basket ''' bid = utils.basket_id(request) item = ProductVariant.objects.get(id=request.GET["variant_id"]) try: count = BasketItem.objects.get(basket_id=bid, variant=item).quantity except BasketItem.DoesNotExist: count = 0 return Response(data={"quantity": count}, status=status.HTTP_200_OK)
def test_post_checkout_invalid(self): """ Test posting an invalid form. This should return a 200 response - rerendering the form page with the errors """ request = RequestFactory().post(reverse('longclaw_checkout_view')) request.session = {} bid = basket_id(request) BasketItemFactory(basket_id=bid) response = CheckoutView.as_view()(request) self.assertEqual(response.status_code, 200)
def destroy(self, request, variant_id=None): """ Remove an item from the basket """ variant = ProductVariant.objects.get(id=variant_id) quantity = int(request.data.get("quantity", 1)) try: item = BasketItem.objects.get( basket_id=utils.basket_id(request), variant=variant) item.decrease_quantity(quantity) except BasketItem.DoesNotExist: pass serializer = BasketItemSerializer(self.get_queryset(request), many=True) return Response(data=serializer.data, status=status.HTTP_200_OK)
def setUp(self): self.addresses = { 'shipping_name': '', 'shipping_address_line1': '', 'shipping_address_city': '', 'shipping_address_zip': '', 'shipping_address_country': '', 'billing_name': '', 'billing_address_line1': '', 'billing_address_city': '', 'billing_address_zip': '', 'billing_address_country': '' } self.email = "*****@*****.**" request = RequestFactory().get('/') request.session = {} self.basket_id = basket_id(request)
def test_post_checkout(self): """ Test correctly posting to the checkout view """ country = CountryFactory() request = RequestFactory().post( reverse('longclaw_checkout_view'), { 'shipping-name': 'bob', 'shipping-line_1': 'blah blah', 'shipping-postcode': 'ytxx 23x', 'shipping-city': 'London', 'shipping-country': country.pk, 'email': '*****@*****.**' }) request.session = {} bid = basket_id(request) BasketItemFactory(basket_id=bid) response = CheckoutView.as_view()(request) self.assertEqual(response.status_code, 302)
def remove_from_basket(request): ''' Remove an item from the basket ''' print(request.data["variant_id"]) variant = ProductVariant.objects.get(id=request.data["variant_id"]) quantity = request.data.get("quantity", 1) try: item = BasketItem.objects.get(basket_id=utils.basket_id(request), variant=variant) except BasketItem.DoesNotExist: return Response(data={"message": "Item does not exist in cart"}, status=status.HTTP_400_BAD_REQUEST) if quantity >= item.quantity: item.delete() else: item.decrease_quantity(quantity) items, _ = utils.get_basket_items(request) serializer = BasketItemSerializer(items, many=True) return Response(data=serializer.data, status=status.HTTP_201_CREATED)
def setUp(self): request = RequestFactory().get('/') request.session = {} bid = basket_id(request) BasketItemFactory(basket_id=bid)