Ejemplo n.º 1
0
def test_adding_to_checkout_with_deleted_checkout_token(
        customer_user, authorized_client, product, request_checkout_with_item):
    key = utils.COOKIE_NAME
    request_checkout_with_item.user = customer_user
    request_checkout_with_item.save()
    old_token = request_checkout_with_item.token

    response = authorized_client.get(reverse("checkout:index"))

    utils.set_checkout_cookie(request_checkout_with_item, response)
    authorized_client.cookies[key] = response.cookies[key]
    request_checkout_with_item.delete()
    variant = product.variants.get()
    url = reverse(
        "product:add-to-checkout",
        kwargs={
            "slug": product.get_slug(),
            "product_id": product.pk
        },
    )
    data = {"quantity": 1, "variant": variant.pk}

    authorized_client.post(url, data)

    assert Checkout.objects.count() == 1
    assert not Checkout.objects.filter(token=old_token).exists()
Ejemplo n.º 2
0
def test_adding_to_checkout_with_another_user_token(
        admin_user, admin_client, product, customer_user,
        request_checkout_with_item):
    client = admin_client
    key = utils.COOKIE_NAME
    request_checkout_with_item.user = customer_user
    request_checkout_with_item.save()

    response = client.get(reverse("checkout:index"))

    utils.set_checkout_cookie(request_checkout_with_item, response)
    client.cookies[key] = response.cookies[key]
    variant = request_checkout_with_item.lines.first().variant
    url = reverse(
        "product:add-to-checkout",
        kwargs={
            "slug": product.get_slug(),
            "product_id": product.pk
        },
    )
    data = {"quantity": 1, "variant": variant.pk}

    client.post(url, data)

    assert Checkout.objects.count() == 2
    assert Checkout.objects.get(
        user=admin_user).pk != request_checkout_with_item.pk
Ejemplo n.º 3
0
    def add_to_checkout(self, request, *args, **kwargs):
        response_data = {
            'token': None,
            'errors': None,
        }

        instance = self.get_object()
        form, checkout = handle_checkout_form(request,
                                              instance,
                                              create_checkout=True)

        if form.is_valid():
            form.save()

            if request.user.is_authenticated:
                if request.user != checkout.user:
                    checkout.user = request.user

            if not request.user.is_authenticated:
                checkout.user = None

            checkout.save()

            response_data['token'] = checkout.token

            response = Response(response_data)
        else:
            response_data['errors'] = form.error_messages
            response = Response(response_data, status=400)

        if not request.user.is_authenticated:
            set_checkout_cookie(checkout, response)

        return response
Ejemplo n.º 4
0
def test_adding_to_checkout_with_closed_checkout_token(
    customer_user, authorized_client, product, request_checkout_with_item
):
    key = utils.COOKIE_NAME
    request_checkout_with_item.user = customer_user
    request_checkout_with_item.save()

    response = authorized_client.get(reverse("checkout:index"))
    utils.set_checkout_cookie(request_checkout_with_item, response)
    authorized_client.cookies[key] = response.cookies[key]
    variant = product.variants.get()
    url = reverse(
        "product:add-to-checkout",
        kwargs={"slug": product.get_slug(), "product_id": product.pk},
    )
    data = {"quantity": 1, "variant": variant.pk}

    authorized_client.post(url, data)

    assert customer_user.checkouts.count() == 1
Ejemplo n.º 5
0
    def add_to_checkout(self, request, *args, **kwargs):
        response_data = {
            'token': None,
            'errors': None,
        }

        instance = self.get_object()
        form, checkout = handle_checkout_form(request,
                                              instance,
                                              create_checkout=True)

        if form.is_valid():
            form.save()

            if request.user.is_authenticated:
                if request.user != checkout.user:
                    checkout.user = request.user

            if not request.user.is_authenticated:
                checkout.user = None

            checkout.save()

            response_data['token'] = checkout.token

            response = Response(response_data)
        else:
            response_data['errors'] = {}
            for field in form:
                if field.errors:
                    response_data['errors'][field.name] = field.errors  # pylint: disable=unsupported-assignment-operation

            response = Response(response_data,
                                status=status.HTTP_400_BAD_REQUEST)

        if not request.user.is_authenticated:
            set_checkout_cookie(checkout, response)

        return response
def test_anonymous_adding_to_checkout_with_another_user_token(
        client, product, customer_user, request_checkout_with_item):
    key = utils.COOKIE_NAME
    request_checkout_with_item.user = customer_user
    request_checkout_with_item.save()

    response = client.get(reverse('checkout:index'))

    utils.set_checkout_cookie(request_checkout_with_item, response)
    client.cookies[key] = response.cookies[key]
    variant = product.variants.get()
    url = reverse('product:add-to-checkout',
                  kwargs={
                      'slug': product.get_slug(),
                      'product_id': product.pk
                  })
    data = {'quantity': 1, 'variant': variant.pk}

    client.post(url, data)

    assert Checkout.objects.count() == 2
    assert Checkout.objects.get(user=None).pk != request_checkout_with_item.pk
def test_adding_to_checkout_with_current_user_token(
        customer_user, authorized_client, product, request_checkout_with_item):
    key = utils.COOKIE_NAME
    request_checkout_with_item.user = customer_user
    request_checkout_with_item.save()

    response = authorized_client.get(reverse('checkout:index'))

    utils.set_checkout_cookie(request_checkout_with_item, response)
    authorized_client.cookies[key] = response.cookies[key]
    variant = request_checkout_with_item.lines.first().variant
    url = reverse('product:add-to-checkout',
                  kwargs={
                      'slug': product.get_slug(),
                      'product_id': product.pk
                  })
    data = {'quantity': 1, 'variant': variant.pk}

    authorized_client.post(url, data)

    assert Checkout.objects.count() == 1
    assert Checkout.objects.get(
        user=customer_user).pk == request_checkout_with_item.pk