示例#1
0
def test_order_payment_flow(
        request_cart_with_item, client, address, shipping_method):
    request_cart_with_item.shipping_address = address
    request_cart_with_item.billing_address = address.get_copy()
    request_cart_with_item.email = '*****@*****.**'
    request_cart_with_item.shipping_method = (
        shipping_method.price_per_country.first())
    request_cart_with_item.save()

    order = create_order(
        request_cart_with_item, 'tracking_code', discounts=None, taxes=None)

    # Select payment method
    url = reverse('order:payment', kwargs={'token': order.token})
    data = {'method': 'default'}
    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, 'variant': 'default'})
    assert response.request['PATH_INFO'] == redirect_url

    # Go to payment details page, enter payment data
    data = {
        'status': PaymentStatus.PREAUTH,
        'fraud_status': FraudStatus.UNKNOWN,
        'gateway_response': '3ds-disabled',
        'verification_result': 'waiting'}

    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

    # Complete payment, go to checkout success page
    data = {'status': 'ok'}
    response = client.post(redirect_url, data)
    assert response.status_code == 302
    redirect_url = reverse(
        'order:checkout-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.tax == order.total.tax.amount
    assert payment.currency == order.total.currency
    assert payment.delivery == order.shipping_price.net.amount
    assert len(payment.get_purchased_items()) == len(order.lines.all())
示例#2
0
def test_view_fulfill_order_lines_with_empty_quantity(admin_client, order_with_lines):
    url = reverse(
        "dashboard:fulfill-order-lines", kwargs={"order_pk": order_with_lines.pk}
    )
    data = {
        "csrfmiddlewaretoken": "hello",
        "form-INITIAL_FORMS": "0",
        "form-MAX_NUM_FORMS": "1000",
        "form-MIN_NUM_FORMS": "0",
        "form-TOTAL_FORMS": order_with_lines.lines.count(),
        "send_mail": "on",
        "tracking_number": "",
    }
    for i, line in enumerate(order_with_lines):
        data["form-{}-order_line".format(i)] = line.pk
        data["form-{}-quantity".format(i)] = line.quantity_unfulfilled

    # Set first order line's fulfill quantity to 0
    data["form-0-quantity"] = 0

    response = admin_client.post(url, data)
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        "dashboard:order-details", kwargs={"order_pk": order_with_lines.pk}
    )
    order_with_lines.refresh_from_db()
    assert not order_with_lines.lines.all()[0].quantity_unfulfilled == 0
    for line in order_with_lines.lines.all()[1:]:
        assert line.quantity_unfulfilled == 0
示例#3
0
def test_view_product_type_edit_to_no_variants_valid(admin_client, product):
    product_type = ProductType.objects.create(
        name="New product type", has_variants=True
    )
    product.product_type = product_type
    product.save()

    url = reverse("dashboard:product-type-update", kwargs={"pk": product_type.pk})
    # When all products have only one variant you can change
    # has_variants to false
    data = {
        "name": product_type.name,
        "product_attributes": product_type.product_attributes.values_list(
            "pk", flat=True
        ),
        "variant_attributes": product_type.variant_attributes.values_list(
            "pk", flat=True
        ),
        "has_variants": False,
        "weight": ["3.47"],
    }

    response = admin_client.post(url, data)

    assert response.status_code == 302
    assert get_redirect_location(response) == url
    product_type.refresh_from_db()
    assert not product_type.has_variants
    assert product.variants.count() == 1
示例#4
0
def test_view_cancel_order_line(admin_client, draft_order):
    lines_before = draft_order.lines.all()
    lines_before_count = lines_before.count()
    line = lines_before.first()
    line_quantity = line.quantity
    quantity_allocated_before = line.variant.quantity_allocated

    url = reverse(
        'dashboard:orderline-cancel', kwargs={
            'order_pk': draft_order.pk,
            'line_pk': line.pk})

    response = admin_client.get(url)
    assert response.status_code == 200
    response = admin_client.post(url, {'csrfmiddlewaretoken': 'hello'})
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        'dashboard:order-details', args=[draft_order.pk])
    # check ordered item removal
    lines_after = Order.objects.get().lines.all()
    assert lines_before_count - 1 == lines_after.count()
    # check stock deallocation
    line.variant.refresh_from_db()
    assert line.variant.quantity_allocated == (
        quantity_allocated_before - line_quantity)
    url = reverse(
        'dashboard:orderline-cancel', kwargs={
            'order_pk': draft_order.pk,
            'line_pk': OrderLine.objects.get().pk})
    response = admin_client.post(
        url, {'csrfmiddlewaretoken': 'hello'}, follow=True)
    assert Order.objects.get().lines.all().count() == 0
    # check success messages after redirect
    assert response.context['messages']
示例#5
0
def test_add_order_note_view(order, authorized_client, customer_user):
    order.user_email = customer_user.email
    order.save()
    url = reverse("order:details", kwargs={"token": order.token})
    customer_note = "bla-bla note"
    data = {"customer_note": customer_note}

    response = authorized_client.post(url, data)

    redirect_url = reverse("order:details", kwargs={"token": order.token})
    assert get_redirect_location(response) == redirect_url
    order.refresh_from_db()
    assert order.customer_note == customer_note

    # Ensure an order event was triggered
    note_event = order_events.OrderEvent.objects.get()  # type: order_events.OrderEvent
    assert note_event.type == order_events.OrderEvents.NOTE_ADDED
    assert note_event.user == customer_user
    assert note_event.order == order
    assert note_event.parameters == {"message": customer_note}

    # Ensure a customer event was triggered
    note_event = account_events.CustomerEvent.objects.get()
    assert note_event.type == account_events.CustomerEvents.NOTE_ADDED_TO_ORDER
    assert note_event.user == customer_user
    assert note_event.order == order
    assert note_event.parameters == {"message": customer_note}
示例#6
0
def test_view_product_type_edit_to_no_variants_valid(admin_client, product):
    product_type = ProductType.objects.create(
        name='New product type', has_variants=True)
    product.product_type = product_type
    product.save()

    url = reverse(
        'dashboard:product-type-update', kwargs={'pk': product_type.pk})
    # When all products have only one variant you can change
    # has_variants to false
    data = {
        'name': product_type.name,
        'product_attributes': product_type.product_attributes.values_list(
            'pk', flat=True),
        'variant_attributes': product_type.variant_attributes.values_list(
            'pk', flat=True),
        'has_variants': False}

    response = admin_client.post(url, data)

    assert response.status_code == 302
    assert get_redirect_location(response) == url
    product_type.refresh_from_db()
    assert not product_type.has_variants
    assert product.variants.count() == 1
示例#7
0
def test_view_add_variant_to_order(admin_client, order_with_lines, admin_user):
    order_with_lines.status = OrderStatus.DRAFT
    order_with_lines.save()
    variant = ProductVariant.objects.get(sku="SKU_A")
    line = OrderLine.objects.get(product_sku="SKU_A")
    line_quantity_before = line.quantity

    assert not OrderEvent.objects.exists()

    added_quantity = 2
    url = reverse(
        "dashboard:add-variant-to-order", kwargs={"order_pk": order_with_lines.pk}
    )
    data = {"variant": variant.pk, "quantity": added_quantity}

    response = admin_client.post(url, data)

    line.refresh_from_db()
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        "dashboard:order-details", kwargs={"order_pk": order_with_lines.pk}
    )
    assert line.quantity == line_quantity_before + added_quantity

    removed_items_event = OrderEvent.objects.last()
    assert removed_items_event.type == OrderEvents.DRAFT_ADDED_PRODUCTS
    assert removed_items_event.user == admin_user
    assert removed_items_event.parameters == {
        "lines": [{"quantity": line.quantity, "line_pk": line.pk, "item": str(line)}]
    }
示例#8
0
def test_view_cancel_order_line(admin_client, draft_order, track_inventory, admin_user):
    lines_before = draft_order.lines.all()
    lines_before_count = lines_before.count()
    line = lines_before.first()
    line_quantity = line.quantity
    quantity_allocated_before = line.variant.quantity_allocated

    line.variant.track_inventory = track_inventory
    line.variant.save()

    assert not OrderEvent.objects.exists()

    url = reverse(
        "dashboard:orderline-cancel",
        kwargs={"order_pk": draft_order.pk, "line_pk": line.pk},
    )

    response = admin_client.get(url)
    assert response.status_code == 200
    response = admin_client.post(url, {"csrfmiddlewaretoken": "hello"})
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        "dashboard:order-details", args=[draft_order.pk]
    )
    # check ordered item removal
    lines_after = Order.objects.get().lines.all()
    assert lines_before_count - 1 == lines_after.count()

    # check stock deallocation
    line.variant.refresh_from_db()

    if track_inventory:
        assert line.variant.quantity_allocated == (
            quantity_allocated_before - line_quantity
        )
    else:
        assert line.variant.quantity_allocated == quantity_allocated_before

    removed_items_event = OrderEvent.objects.last()
    assert removed_items_event.type == OrderEvents.DRAFT_REMOVED_PRODUCTS
    assert removed_items_event.user == admin_user
    assert removed_items_event.parameters == {
        "lines": [
            {
                "quantity": line.quantity,
                "line_pk": None,  # the line was deleted
                "item": str(line),
            }
        ]
    }

    url = reverse(
        "dashboard:orderline-cancel",
        kwargs={"order_pk": draft_order.pk, "line_pk": OrderLine.objects.get().pk},
    )
    response = admin_client.post(url, {"csrfmiddlewaretoken": "hello"}, follow=True)
    assert Order.objects.get().lines.all().count() == 0
    # check success messages after redirect
    assert response.context["messages"]
示例#9
0
def test_view_remove_draft_order(admin_client, draft_order):
    url = reverse("dashboard:draft-order-delete", kwargs={"order_pk": draft_order.pk})

    response = admin_client.post(url, {})

    assert response.status_code == 302
    assert get_redirect_location(response) == reverse("dashboard:orders")
    assert Order.objects.count() == 0
def test_checkout_flow(request_cart_with_item, client, shipping_method):  # pylint: disable=W0613,R0914
    """
    Basic test case that confirms if core checkout flow works
    """

    # Enter checkout
    checkout_index = client.get(reverse('checkout:index'), follow=True)
    # Checkout index redirects directly to shipping address step
    shipping_address = client.get(checkout_index.request['PATH_INFO'])

    # Enter shipping address data
    shipping_data = {
        'email': '*****@*****.**',
        'first_name': 'John',
        'last_name': 'Doe',
        'street_address_1': 'Aleje Jerozolimskie 2',
        'street_address_2': '',
        'city': 'Warszawa',
        'city_area': '',
        'country_area': '',
        'postal_code': '00-374',
        'country': 'PL'}
    shipping_response = client.post(shipping_address.request['PATH_INFO'],
                                    data=shipping_data, follow=True)

    # Select shipping method
    shipping_method_page = client.get(shipping_response.request['PATH_INFO'])

    # Redirect to summary after shipping method selection
    shipping_method_data = {'method': shipping_method.pk}
    shipping_method_response = client.post(shipping_method_page.request['PATH_INFO'],
                                           data=shipping_method_data, follow=True)

    # Summary page asks for Billing address, default is the same as shipping
    address_data = {'address': 'shipping_address'}
    summary_response = client.post(shipping_method_response.request['PATH_INFO'],
                                   data=address_data, follow=True)

    # After summary step, order is created and it waits for payment
    order = summary_response.context['order']

    # Select payment method
    payment_page = client.post(summary_response.request['PATH_INFO'],
                               data={'method': 'default'}, follow=True)
    assert len(payment_page.redirect_chain) == 1
    assert payment_page.status_code == 200
    # Go to payment details page, enter payment data
    payment_page_url = payment_page.redirect_chain[0][0]
    payment_data = {
        'status': PaymentStatus.PREAUTH,
        'fraud_status': FraudStatus.UNKNOWN,
        'gateway_response': '3ds-disabled',
        'verification_result': 'waiting'}
    payment_response = client.post(payment_page_url, data=payment_data)
    assert payment_response.status_code == 302
    order_password = reverse('order:create-password',
                             kwargs={'token': order.token})
    assert get_redirect_location(payment_response) == order_password
示例#11
0
def test_view_product_select_type(admin_client, product_type):
    url = reverse('dashboard:product-add-select-type')
    data = {'product_type': product_type.pk}

    response = admin_client.post(url, data)

    assert get_redirect_location(response) == reverse(
        'dashboard:product-add', kwargs={'type_pk': product_type.pk})
    assert response.status_code == 302
def test_summary_without_address(request_cart_with_item, client):  # pylint: disable=W0613
    """
    user tries to get summary step without saved shipping method -
     if is redirected to shipping method step
    """

    response = client.get(reverse('checkout:summary'))
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse('checkout:shipping-method')
示例#13
0
def test_view_connect_order_with_user_different_email(
        order, authorized_client):
    url = reverse(
        'order:connect-order-with-user', kwargs={'token': order.token})
    response = authorized_client.post(url)

    redirect_location = get_redirect_location(response)
    assert redirect_location == reverse('account:details')
    order.refresh_from_db()
    assert order.user is None
def test_client_login(request_cart_with_item, client, admin_user):
    data = {
        'username': admin_user.email,
        'password': '******'
    }
    response = client.post(reverse('registration:login'), data=data)
    assert response.status_code == 302
    assert get_redirect_location(response) == '/'
    response = client.get(reverse('checkout:shipping-address'))
    assert response.context['checkout'].cart.token == request_cart_with_item.token
示例#15
0
def test_view_product_variant_details_redirect_to_product(
        admin_client, product):
    variant = product.variants.get()
    url = reverse(
        'dashboard:variant-details',
        kwargs={'product_pk': product.pk, 'variant_pk': variant.pk})

    response = admin_client.get(url)

    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        'dashboard:product-details', kwargs={'pk': product.pk})
示例#16
0
def test_view_order_create(admin_client):
    url = reverse('dashboard:order-create')

    response = admin_client.post(url, {})

    assert response.status_code == 302
    assert Order.objects.count() == 1
    order = Order.objects.first()
    redirect_url = reverse(
        'dashboard:order-details', kwargs={'order_pk': order.pk})
    assert get_redirect_location(response) == redirect_url
    assert order.status == OrderStatus.DRAFT
def test_summary_without_shipping_method(request_cart_with_item, client, monkeypatch):  # pylint: disable=W0613
    """
    user tries to get summary step without saved shipping method -
     if is redirected to shipping method step
    """
    # address test return true
    monkeypatch.setattr('saleor.checkout.core.Checkout.email',
                        True)

    response = client.get(reverse('checkout:summary'))
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse('checkout:shipping-method')
示例#18
0
def test_view_create_from_draft_order_valid(admin_client, draft_order):
    order = draft_order
    url = reverse("dashboard:create-order-from-draft", kwargs={"order_pk": order.pk})
    data = {"csrfmiddlewaretoken": "hello"}

    response = admin_client.post(url, data)

    assert response.status_code == 302
    order.refresh_from_db()
    assert order.status == OrderStatus.UNFULFILLED
    redirect_url = reverse("dashboard:order-details", kwargs={"order_pk": order.pk})
    assert get_redirect_location(response) == redirect_url
def test_shipping_method_without_shipping(request_cart_with_item, client, monkeypatch):  # pylint: disable=W0613
    """
    user tries to get shipping method step in checkout without shipping -
     if is redirected to summary step
    """

    monkeypatch.setattr('saleor.checkout.core.Checkout.is_shipping_required',
                        False)

    response = client.get(reverse('checkout:shipping-method'))
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse('checkout:summary')
示例#20
0
def test_view_product_bulk_update_publish(admin_client, product_list):
    url = reverse("dashboard:product-bulk-update")
    products = [product.pk for product in product_list]
    data = {"action": ProductBulkAction.PUBLISH, "products": products}

    response = admin_client.post(url, data)

    assert response.status_code == 302
    assert get_redirect_location(response) == reverse("dashboard:product-list")

    for p in product_list:
        p.refresh_from_db()
        assert p.is_published
示例#21
0
def test_add_order_note_view(order, authorized_client, customer_user):
    order.user_email = customer_user.email
    order.save()
    url = reverse('order:details', kwargs={'token': order.token})
    customer_note = 'bla-bla note'
    data = {'customer_note': customer_note}

    response = authorized_client.post(url, data)

    redirect_url = reverse('order:details', kwargs={'token': order.token})
    assert get_redirect_location(response) == redirect_url
    order.refresh_from_db()
    assert order.customer_note == customer_note
示例#22
0
def test_create_user_after_order(order, client):
    order.user_email = '*****@*****.**'
    order.save()
    url = reverse('order:checkout-success', kwargs={'token': order.token})
    data = {'password': '******'}

    response = client.post(url, data)

    redirect_url = reverse('order:details', kwargs={'token': order.token})
    assert get_redirect_location(response) == redirect_url
    user = User.objects.filter(email='*****@*****.**').first()
    assert user is not None
    assert user.orders.filter(token=order.token).exists()
示例#23
0
def test_view_connect_order_with_user_authorized_user(
        order, authorized_client, customer_user):
    order.user_email = customer_user.email
    order.save()

    url = reverse(
        'order:connect-order-with-user', kwargs={'token': order.token})
    response = authorized_client.post(url)

    redirect_location = get_redirect_location(response)
    assert redirect_location == reverse('order:details', args=[order.token])
    order.refresh_from_db()
    assert order.user == customer_user
示例#24
0
def test_view_product_type_create_variantless(
        admin_client, color_attribute, size_attribute):
    url = reverse('dashboard:product-type-add')
    data = {
        'name': 'Testing Type',
        'product_attributes': [color_attribute.pk],
        'variant_attributes': [],
        'has_variants': False}
    response = admin_client.post(url, data)

    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        'dashboard:product-type-list')
    assert ProductType.objects.count() == 1
def test_checkout_flow_authenticated_user(authorized_client, billing_address,  # pylint: disable=R0914
                                          request_cart_with_item, customer_user,
                                          shipping_method):
    """
    Checkout with authenticated user and previously saved address
    """
    # Prepare some data
    customer_user.addresses.add(billing_address)
    request_cart_with_item.user = customer_user
    request_cart_with_item.save()

    # Enter checkout
    # Checkout index redirects directly to shipping address step
    shipping_address = authorized_client.get(reverse('checkout:index'), follow=True)

    # Enter shipping address data
    shipping_data = {'address': billing_address.pk}
    shipping_method_page = authorized_client.post(shipping_address.request['PATH_INFO'],
                                                  data=shipping_data, follow=True)

    # Select shipping method
    shipping_method_data = {'method': shipping_method.pk}
    shipping_method_response = authorized_client.post(shipping_method_page.request['PATH_INFO'],
                                                      data=shipping_method_data, follow=True)

    # Summary page asks for Billing address, default is the same as shipping
    payment_method_data = {'address': 'shipping_address'}
    payment_method_page = authorized_client.post(shipping_method_response.request['PATH_INFO'],
                                                 data=payment_method_data, follow=True)

    # After summary step, order is created and it waits for payment
    order = payment_method_page.context['order']

    # Select payment method
    payment_page = authorized_client.post(payment_method_page.request['PATH_INFO'],
                                          data={'method': 'default'}, follow=True)

    # Go to payment details page, enter payment data
    payment_data = {
        'status': PaymentStatus.PREAUTH,
        'fraud_status': FraudStatus.UNKNOWN,
        'gateway_response': '3ds-disabled',
        'verification_result': 'waiting'}
    payment_response = authorized_client.post(payment_page.request['PATH_INFO'],
                                              data=payment_data)

    assert payment_response.status_code == 302
    order_password = reverse('order:create-password',
                             kwargs={'token': order.token})
    assert get_redirect_location(payment_response) == order_password
示例#26
0
def test_view_order_customer_edit_to_email(admin_client, draft_order):
    url = reverse("dashboard:order-customer-edit", kwargs={"order_pk": draft_order.pk})
    data = {"user_email": "*****@*****.**", "user": "", "update_addresses": False}

    response = admin_client.post(url, data)

    assert response.status_code == 302
    draft_order.refresh_from_db()
    assert draft_order.user_email == "*****@*****.**"
    assert not draft_order.user
    redirect_url = reverse(
        "dashboard:order-details", kwargs={"order_pk": draft_order.pk}
    )
    assert get_redirect_location(response) == redirect_url
示例#27
0
def test_view_cancel_fulfillment(admin_client, fulfilled_order):
    fulfillment = fulfilled_order.fulfillments.first()
    url = reverse(
        'dashboard:fulfillment-cancel',
        kwargs={
            'order_pk': fulfilled_order.pk,
            'fulfillment_pk': fulfillment.pk})

    response = admin_client.post(url, {'csrfmiddlewaretoken': 'hello'})
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        'dashboard:order-details', kwargs={'order_pk': fulfilled_order.pk})
    fulfillment.refresh_from_db()
    assert fulfillment.status == FulfillmentStatus.CANCELED
def test_create_user_after_order(order, client):
    order.user_email = '*****@*****.**'
    order.save()
    assert not User.objects.filter(email='*****@*****.**').exists()
    data = {'password': '******'}
    url = reverse('order:create-password', kwargs={'token': order.token})
    response = client.post(url, data=data)
    redirect_location = get_redirect_location(response)
    detail_url = reverse('order:details', kwargs={'token': order.token})
    assert redirect_location == detail_url
    user = User.objects.filter(email='*****@*****.**')
    assert user.exists()
    user = user.first()
    assert user.orders.filter(token=order.token).exists()
示例#29
0
def test_view_product_edit(admin_client, product):
    url = reverse('dashboard:product-update', kwargs={'pk': product.pk})
    data = {
        'name': 'Product second name', 'description': 'Product description.',
        'price': 10, 'category': product.category.pk, 'variant-sku': '123',
        'variant-quantity': 10}

    response = admin_client.post(url, data)

    assert response.status_code == 302
    product.refresh_from_db()
    assert get_redirect_location(response) == reverse(
        'dashboard:product-details', kwargs={'pk': product.pk})
    assert product.name == 'Product second name'
示例#30
0
def test_view_order_customer_edit_to_guest_customer(admin_client, draft_order):
    url = reverse(
        'dashboard:order-customer-edit', kwargs={'order_pk': draft_order.pk})
    data = {'user_email': '', 'user': '', 'update_addresses': False}

    response = admin_client.post(url, data)

    assert response.status_code == 302
    draft_order.refresh_from_db()
    assert not draft_order.user_email
    assert not draft_order.user
    redirect_url = reverse(
        'dashboard:order-details', kwargs={'order_pk': draft_order.pk})
    assert get_redirect_location(response) == redirect_url
示例#31
0
def test_view_cancel_order_line(admin_client, draft_order, track_inventory,
                                admin_user):
    lines_before = draft_order.lines.all()
    lines_before_count = lines_before.count()
    line = lines_before.first()
    line_quantity = line.quantity
    quantity_allocated_before = line.variant.quantity_allocated

    line.variant.track_inventory = track_inventory
    line.variant.save()

    assert not OrderEvent.objects.exists()

    url = reverse(
        "dashboard:orderline-cancel",
        kwargs={
            "order_pk": draft_order.pk,
            "line_pk": line.pk
        },
    )

    response = admin_client.get(url)
    assert response.status_code == 200
    response = admin_client.post(url, {"csrfmiddlewaretoken": "hello"})
    assert response.status_code == 302
    assert get_redirect_location(response) == reverse(
        "dashboard:order-details", args=[draft_order.pk])
    # check ordered item removal
    lines_after = Order.objects.get().lines.all()
    assert lines_before_count - 1 == lines_after.count()

    # check stock deallocation
    line.variant.refresh_from_db()

    if track_inventory:
        assert line.variant.quantity_allocated == (quantity_allocated_before -
                                                   line_quantity)
    else:
        assert line.variant.quantity_allocated == quantity_allocated_before

    removed_items_event = OrderEvent.objects.last()
    assert removed_items_event.type == OrderEvents.DRAFT_REMOVED_PRODUCTS
    assert removed_items_event.user == admin_user
    assert removed_items_event.parameters == {
        "lines": [{
            "quantity": line.quantity,
            "line_pk": None,  # the line was deleted
            "item": str(line),
        }]
    }

    url = reverse(
        "dashboard:orderline-cancel",
        kwargs={
            "order_pk": draft_order.pk,
            "line_pk": OrderLine.objects.get().pk
        },
    )
    response = admin_client.post(url, {"csrfmiddlewaretoken": "hello"},
                                 follow=True)
    assert Order.objects.get().lines.all().count() == 0
    # check success messages after redirect
    assert response.context["messages"]
def test_checkout_flow_authenticated_user(
        authorized_client,
        billing_address,  # pylint: disable=R0914
        request_cart_with_item,
        customer_user,
        shipping_method):
    """
    Checkout with authenticated user and previously saved address
    """
    # Prepare some data
    customer_user.addresses.add(billing_address)
    request_cart_with_item.user = customer_user
    request_cart_with_item.save()

    # Enter checkout
    # Checkout index redirects directly to shipping address step
    shipping_address = authorized_client.get(reverse('checkout:index'),
                                             follow=True)

    # Enter shipping address data
    shipping_data = {'address': billing_address.pk}
    shipping_method_page = authorized_client.post(
        shipping_address.request['PATH_INFO'], data=shipping_data, follow=True)

    # Select shipping method
    shipping_method_data = {'method': shipping_method.pk}
    shipping_method_response = authorized_client.post(
        shipping_method_page.request['PATH_INFO'],
        data=shipping_method_data,
        follow=True)

    # Summary page asks for Billing address, default is the same as shipping
    payment_method_data = {'address': 'shipping_address'}
    payment_method_page = authorized_client.post(
        shipping_method_response.request['PATH_INFO'],
        data=payment_method_data,
        follow=True)

    # After summary step, order is created and it waits for payment
    order = payment_method_page.context['order']

    # Select payment method
    payment_page = authorized_client.post(
        payment_method_page.request['PATH_INFO'],
        data={'method': 'default'},
        follow=True)

    # Go to payment details page, enter payment data
    payment_data = {
        'status': PaymentStatus.PREAUTH,
        'fraud_status': FraudStatus.UNKNOWN,
        'gateway_response': '3ds-disabled',
        'verification_result': 'waiting'
    }
    payment_response = authorized_client.post(
        payment_page.request['PATH_INFO'], data=payment_data)

    assert payment_response.status_code == 302
    order_password = reverse('order:create-password',
                             kwargs={'token': order.token})
    assert get_redirect_location(payment_response) == order_password
def test_checkout_flow(request_cart_with_item, client, shipping_method):  # pylint: disable=W0613,R0914
    """
    Basic test case that confirms if core checkout flow works
    """

    # Enter checkout
    checkout_index = client.get(reverse('checkout:index'), follow=True)
    # Checkout index redirects directly to shipping address step
    shipping_address = client.get(checkout_index.request['PATH_INFO'])

    # Enter shipping address data
    shipping_data = {
        'email': '*****@*****.**',
        'first_name': 'John',
        'last_name': 'Doe',
        'street_address_1': 'Aleje Jerozolimskie 2',
        'street_address_2': '',
        'city': 'Warszawa',
        'city_area': '',
        'country_area': '',
        'postal_code': '00-374',
        'country': 'PL'
    }
    shipping_response = client.post(shipping_address.request['PATH_INFO'],
                                    data=shipping_data,
                                    follow=True)

    # Select shipping method
    shipping_method_page = client.get(shipping_response.request['PATH_INFO'])

    # Redirect to summary after shipping method selection
    shipping_method_data = {'method': shipping_method.pk}
    shipping_method_response = client.post(
        shipping_method_page.request['PATH_INFO'],
        data=shipping_method_data,
        follow=True)

    # Summary page asks for Billing address, default is the same as shipping
    address_data = {'address': 'shipping_address'}
    summary_response = client.post(
        shipping_method_response.request['PATH_INFO'],
        data=address_data,
        follow=True)

    # After summary step, order is created and it waits for payment
    order = summary_response.context['order']

    # Select payment method
    payment_page = client.post(summary_response.request['PATH_INFO'],
                               data={'method': 'default'},
                               follow=True)
    assert len(payment_page.redirect_chain) == 1
    assert payment_page.status_code == 200
    # Go to payment details page, enter payment data
    payment_page_url = payment_page.redirect_chain[0][0]
    payment_data = {
        'status': 'preauth',
        'fraud_status': 'unknown',
        'gateway_response': '3ds-disabled',
        'verification_result': 'waiting'
    }
    payment_response = client.post(payment_page_url, data=payment_data)
    assert payment_response.status_code == 302
    order_details = reverse('order:details', kwargs={'token': order.token})
    assert get_redirect_location(payment_response) == order_details