def test_create_order_insufficient_stock( request_checkout, customer_user, product_without_shipping ): variant = product_without_shipping.variants.get() add_variant_to_checkout(request_checkout, variant, 10, check_quantity=False) request_checkout.user = customer_user request_checkout.billing_address = customer_user.default_billing_address request_checkout.shipping_address = customer_user.default_billing_address request_checkout.save() with pytest.raises(InsufficientStock): prepare_order_data( checkout=request_checkout, tracking_code="tracking_code", discounts=None )
def test_create_order_with_gift_card_partial_use( checkout_with_item, gift_card_used, customer_user, shipping_method ): checkout = checkout_with_item checkout.user = customer_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() price_without_gift_card = calculations.checkout_total(checkout) gift_card_balance_before_order = gift_card_used.current_balance_amount checkout.gift_cards.add(gift_card_used) checkout.save() order = create_order( checkout=checkout, order_data=prepare_order_data( checkout=checkout, tracking_code="tracking_code", discounts=None ), user=customer_user, redirect_url="https://www.example.com", ) gift_card_used.refresh_from_db() expected_old_balance = ( price_without_gift_card.gross.amount + gift_card_used.current_balance_amount ) assert order.gift_cards.count() > 0 assert order.total == zero_taxed_money() assert gift_card_balance_before_order == expected_old_balance
def test_create_order_with_gift_card( checkout_with_gift_card, customer_user, shipping_method, is_anonymous_user ): checkout_user = None if is_anonymous_user else customer_user checkout = checkout_with_gift_card checkout.user = checkout_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() subtotal = calculations.checkout_subtotal(checkout) shipping_price = calculations.checkout_shipping_price(checkout) total_gross_without_gift_cards = ( subtotal.gross + shipping_price.gross - checkout.discount ) gift_cards_balance = checkout.get_total_gift_cards_balance() order = create_order( checkout=checkout, order_data=prepare_order_data( checkout=checkout, tracking_code="tracking_code", discounts=None ), user=customer_user if not is_anonymous_user else AnonymousUser(), redirect_url="https://www.example.com", ) assert order.gift_cards.count() == 1 assert order.gift_cards.first().current_balance.amount == 0 assert order.total.gross == (total_gross_without_gift_cards - gift_cards_balance)
def test_create_order_doesnt_duplicate_order( checkout_with_item, customer_user, shipping_method ): checkout = checkout_with_item checkout.user = customer_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() order_data = prepare_order_data(checkout=checkout, tracking_code="", discounts=None) order_1 = create_order( checkout=checkout, order_data=order_data, user=customer_user, redirect_url="https://www.example.com", ) assert order_1.checkout_token == checkout.token order_2 = create_order( checkout=checkout, order_data=order_data, user=customer_user, redirect_url="https://www.example.com", ) assert order_1.pk == order_2.pk
def test_create_order_with_gift_card_partial_use(checkout_with_item, gift_card_used, customer_user, shipping_method): checkout = checkout_with_item checkout.user = customer_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() price_without_gift_card = checkout.get_total() gift_card_balance_before_order = gift_card_used.current_balance checkout.gift_cards.add(gift_card_used) checkout.save() order = create_order( checkout=checkout, order_data=prepare_order_data(checkout=checkout, tracking_code="tracking_code", discounts=None, taxes=None), user=customer_user, ) gift_card_used.refresh_from_db() assert order.gift_cards.count() > 0 assert order.total == ZERO_TAXED_MONEY assert (gift_card_balance_before_order == ( price_without_gift_card + gift_card_used.current_balance).gross.amount)
def test_create_order_creates_expected_events(request_checkout_with_item, customer_user, shipping_method, is_anonymous_user): checkout = request_checkout_with_item checkout_user = None if is_anonymous_user else customer_user # Ensure not events are existing prior assert not OrderEvent.objects.exists() assert not CustomerEvent.objects.exists() # Prepare valid checkout checkout.user = checkout_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_shipping_address checkout.shipping_method = shipping_method checkout.save() # Place checkout order = create_order( checkout=checkout, order_data=prepare_order_data(checkout=checkout, tracking_code="tracking_code", discounts=None, taxes=None), user=customer_user if not is_anonymous_user else AnonymousUser(), ) # Ensure only two events were created, and retrieve them placement_event, email_sent_event = order.events.all() # type: OrderEvent # Ensure the correct order event was created assert placement_event.type == OrderEvents.PLACED # is the event the expected type assert placement_event.user == checkout_user # is the user anonymous/ the customer assert placement_event.order is order # is the associated backref order valid assert placement_event.date # ensure a date was set assert not placement_event.parameters # should not have any additional parameters # Ensure the correct email sent event was created assert email_sent_event.type == OrderEvents.EMAIL_SENT # should be email sent event assert email_sent_event.user == checkout_user # ensure the user is none or valid assert email_sent_event.order is order # ensure the mail event is related to order assert email_sent_event.date # ensure a date was set assert email_sent_event.parameters == { # ensure the correct parameters were set "email": order.get_user_current_email(), "email_type": OrderEventsEmails.ORDER, } # Check no event was created if the user was anonymous if is_anonymous_user: assert not CustomerEvent.objects.exists( ) # should not have created any event return # we are done testing as the user is anonymous # Ensure the correct customer event was created if the user was not anonymous placement_event = customer_user.events.get() # type: CustomerEvent assert placement_event.type == CustomerEvents.PLACED_ORDER # check the event type assert placement_event.user == customer_user # check the backref is valid assert placement_event.order == order # check the associated order is valid assert placement_event.date # ensure a date was set assert not placement_event.parameters # should not have any additional parameters
def test_create_order_with_gift_card(checkout_with_gift_card, customer_user, shipping_method, is_anonymous_user): checkout_user = None if is_anonymous_user else customer_user checkout = checkout_with_gift_card checkout.user = checkout_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() total_gross_without_gift_cards = (checkout.get_subtotal() + checkout.get_shipping_price(None) - checkout.discount_amount).gross gift_cards_balance = checkout.get_total_gift_cards_balance() order = create_order( checkout=checkout, order_data=prepare_order_data(checkout=checkout, tracking_code="tracking_code", discounts=None, taxes=None), user=customer_user if not is_anonymous_user else AnonymousUser(), ) assert order.gift_cards.count() > 0 assert order.gift_cards.first().current_balance.amount == 0 assert order.total.gross == (total_gross_without_gift_cards - gift_cards_balance)
def test_order_payment_flow(request_checkout_with_item, client, address, customer_user, shipping_zone, settings): request_checkout_with_item.shipping_address = address request_checkout_with_item.billing_address = address.get_copy() request_checkout_with_item.email = "*****@*****.**" request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first( ) request_checkout_with_item.save() order = create_order( checkout=request_checkout_with_item, order_data=prepare_order_data( checkout=request_checkout_with_item, tracking_code="tracking_code", discounts=None, taxes=None, ), user=customer_user, ) # Select payment url = reverse("order:payment", kwargs={"token": order.token}) data = {"gateway": settings.DUMMY} response = client.post(url, data, follow=True) assert len(response.redirect_chain) == 1 assert response.status_code == 200 redirect_url = reverse("order:payment", kwargs={ "token": order.token, "gateway": settings.DUMMY }) assert response.request["PATH_INFO"] == redirect_url # Go to payment details page, enter payment data data = { "gateway": settings.DUMMY, "is_active": True, "total": order.total.gross.amount, "currency": order.total.gross.currency, "charge_status": ChargeStatus.FULLY_CHARGED, } response = client.post(redirect_url, data) assert response.status_code == 302 redirect_url = reverse("order:payment-success", kwargs={"token": order.token}) assert get_redirect_location(response) == redirect_url # Assert that payment object was created and contains correct data payment = order.payments.all()[0] assert payment.total == order.total.gross.amount assert payment.currency == order.total.gross.currency assert payment.transactions.count() == 1 assert payment.transactions.last().kind == "capture"
def test_note_in_created_order(request_checkout_with_item, address, customer_user): request_checkout_with_item.shipping_address = address request_checkout_with_item.note = "test_note" request_checkout_with_item.save() order = create_order( checkout=request_checkout_with_item, order_data=prepare_order_data( checkout=request_checkout_with_item, tracking_code="tracking_code", discounts=None, ), user=customer_user, redirect_url="https://www.example.com", ) assert order.customer_note == request_checkout_with_item.note
def test_note_in_created_order(request_checkout_with_item, address, customer_user): request_checkout_with_item.address = address request_checkout_with_item.note = "test_note" request_checkout_with_item.save() order = create_order( checkout=request_checkout_with_item, order_data=prepare_order_data( checkout=request_checkout_with_item, tracking_code="tracking_code", discounts=None, ), user=customer_user, ) assert order.customer_note == request_checkout_with_item.note
def test_create_order_with_many_gift_cards( checkout_with_item, gift_card_created_by_staff, gift_card, customer_user, shipping_method, ): checkout = checkout_with_item checkout.user = customer_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() price_without_gift_card = calculations.checkout_total( checkout=checkout, lines=list(checkout) ) gift_cards_balance_before_order = ( gift_card_created_by_staff.current_balance.amount + gift_card.current_balance.amount ) checkout.gift_cards.add(gift_card_created_by_staff) checkout.gift_cards.add(gift_card) checkout.save() order = create_order( checkout=checkout, order_data=prepare_order_data( checkout=checkout, lines=list(checkout), tracking_code="tracking_code", discounts=None, ), user=customer_user, redirect_url="https://www.example.com", ) gift_card_created_by_staff.refresh_from_db() gift_card.refresh_from_db() zero_price = zero_money() assert order.gift_cards.count() > 0 assert gift_card_created_by_staff.current_balance == zero_price assert gift_card.current_balance == zero_price assert price_without_gift_card.gross.amount == ( gift_cards_balance_before_order + order.total.gross.amount )
def test_create_order_doesnt_duplicate_order(checkout_with_item, customer_user, shipping_method): checkout = checkout_with_item checkout.user = customer_user checkout.address = customer_user.default_address checkout.shipping_method = shipping_method checkout.save() order_data = prepare_order_data(checkout=checkout, tracking_code="", discounts=None) order_1 = create_order(checkout=checkout, order_data=order_data, user=customer_user) assert order_1.checkout_token == checkout.token order_2 = create_order(checkout=checkout, order_data=order_data, user=customer_user) assert order_1.pk == order_2.pk
def test_create_order_with_many_gift_cards( checkout_with_item, gift_card_created_by_staff, gift_card, customer_user, shipping_method, ): checkout = checkout_with_item checkout.user = customer_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() checkout_total = checkout.get_total() price_without_gift_card = TaxedMoney(net=checkout_total, gross=checkout_total) gift_cards_balance_befor_order = ( gift_card_created_by_staff.current_balance + gift_card.current_balance) checkout.gift_cards.add(gift_card_created_by_staff) checkout.gift_cards.add(gift_card) checkout.save() order = create_order( checkout=checkout, order_data=prepare_order_data(checkout=checkout, tracking_code="tracking_code", discounts=None), user=customer_user, ) gift_card_created_by_staff.refresh_from_db() gift_card.refresh_from_db() zero_price = zero_money() assert order.gift_cards.count() > 0 assert gift_card_created_by_staff.current_balance == zero_price assert gift_card.current_balance == zero_price assert price_without_gift_card.gross.amount == ( gift_cards_balance_befor_order + order.total.gross.amount)