Пример #1
0
def test_address_model():
    user = UserFactory()
    billing_address = AddressFactory(address_type="billing",
                                     is_default=False,
                                     user=user)
    shipping_address = AddressFactory(address_type="shipping",
                                      is_default=False,
                                      user=get_sentinel_user_anonymous())
    assert billing_address.is_default == True
    assert shipping_address.is_default == False

    billing_address2 = AddressFactory(address_type="billing",
                                      is_default=True,
                                      user=user)
    assert billing_address2.is_default == True
    assert (not Address.objects.filter(
        user=user, address_type="billing",
        is_default=True).exclude(id=billing_address2.id).exists())

    # testing custom manager
    shipping_address.user = user
    shipping_address.save()
    assert Address.objects.default_shipping(user)[0] == shipping_address
    assert Address.objects.default_billing(user)[0] == billing_address2

    billing_address.user = user
    billing_address.save()
    assert Address.objects.default_billing(user)[0] == billing_address
Пример #2
0
def test_order_model():
    item1 = ItemWearFactory(price=25.00, discount_price=10.00)
    item2 = ItemAccessoryFactory(quantity=10,
                                 price=50.00,
                                 discount_price=25.00)
    item1.sizes.add(WearSizeFactory(size="M", quantity=10))
    order_item1 = OrderItemFactory(item=item1,
                                   size=item1.sizes.first().size,
                                   quantity=5,
                                   price=item1.actual_price)
    order_item2 = OrderItemFactory(item=item2,
                                   price=item2.actual_price,
                                   quantity=1)
    user = UserFactory(email="*****@*****.**")
    order = OrderFactory(user=user)
    address = AddressFactory(address_type="shipping", email="*****@*****.**")
    order2 = OrderFactory(user=get_sentinel_user_anonymous(),
                          shipping_address=address)
    order.items.add(order_item1, order_item2)
    assert order.get_total == Decimal(75)
    assert order.items_quantity == 6
    assert order.get_email == "*****@*****.**"
    assert order2.get_email == "*****@*****.**"
    assert order.paid() == False

    # testing custom manager
    order2 = OrderFactory(user=user)
    Order.objects.filter(user=user).update(status="paid")
    assert list(Order.objects.search_by_user(user)) == [order2, order]
    assert Order.objects.latest("id").paid() == True
Пример #3
0
 def save(self, *args, **kwargs):
     default_shipping = type(self).objects.default_shipping(self.user)
     default_billing = type(self).objects.default_billing(self.user)
     if not default_billing.exists() or not default_shipping.exists():
         if self.user == get_sentinel_user_anonymous():
             pass
         else:
             self.is_default = True
     if (self.is_default and default_billing.exists()) or (
             self.is_default and default_shipping.exists()):
         if self.id:
             pass
         elif self.address_type == "billing":
             default_billing.update(is_default=False)
         else:
             default_shipping.update(is_default=False)
     super(Address, self).save(*args, **kwargs)
Пример #4
0
 def get_email(self):
     if self.user == get_sentinel_user_anonymous():
         return self.shipping_address.email
     return self.user.email
Пример #5
0
def test_checkout(base_items, user_client, user_user):
    wear, item = base_items
    client = user_client
    user = user_user
    url = reverse("orders:checkout")
    response = client.get(url)
    messages = [m.message for m in get_messages(response.wsgi_request)]
    assert "Your cart is empty" == messages[0]
    assert response.status_code == 302

    # logged-in user
    url1 = reverse("cart:add_to_cart", kwargs={"item_id": item.id})
    response1 = client.post(url1)
    url2 = reverse("orders:checkout")
    response2 = client.get(url2)
    session = client.session
    cart = session[settings.CART_SESSION_ID]
    assert response2.status_code == 200
    assert response2.context["cart"]
    assert response2.context["total"]

    ## test forms-first creation
    assert response2.context["billing_form"]["address_type"].value() == "billing"
    assert response2.context["shipping_form"]["address_type"].value() == "shipping"
    with pytest.raises(KeyError):
        assert response2.context["shipping_form"]["email"]
    assert response2.context["shipping_form"]["is_default"].value() == False

    billing_data = {"billing-" + x: y for x, y in normalized_data.items()}
    shipping_data = {"shipping-" + x: y for x, y in normalized_data.items()}
    form_billing = BillingAddressForm(data=normalized_data, user=user)
    form_shipping = ShippingAddressForm(data=normalized_data, user=user)
    assert form_billing.is_valid()
    assert form_shipping.is_valid()

    full_data = {**billing_data, **shipping_data}
    response3 = client.post(url2, data=full_data)
    order_qs = Order.objects.filter(user=user, status="created")
    order = order_qs[0]
    shipping_address = [str(x) for x in order.shipping_address.__dict__.values()]
    billing_address = [str(x) for x in order.billing_address.__dict__.values()]
    assert response3.status_code == 302
    assert order_qs.count() == 1
    for value in shipping_data.values():
        assert value in shipping_address
    for value in billing_data.values():
        assert value in billing_address
    assert order.user == user
    assert list(order.items.all()) == list(OrderItem.objects.all())

    # AnonymousUser
    client = Client()
    url1 = reverse("cart:add_to_cart", kwargs={"item_id": item.id})
    response1 = client.post(url1)
    url2 = reverse("orders:checkout")
    response2 = client.get(url2)
    session = client.session
    cart = session[settings.CART_SESSION_ID]
    assert response2.status_code == 200
    assert response2.context["cart"]
    assert response2.context["total"]

    ## test forms
    assert response2.context["billing_form"]["address_type"].value() == "billing"
    assert response2.context["shipping_form"]["address_type"].value() == "shipping"
    with pytest.raises(KeyError):
        assert response2.context["shipping_form"]["is_default"]
    assert response2.context["shipping_form"]["email"]

    response3 = client.post(url2, data=full_data)
    assert response3.status_code == 302
    order_qs = Order.objects.filter(
        user=get_sentinel_user_anonymous(), status="created"
    )
    order = order_qs[0]
    shipping_address = [str(x) for x in order.shipping_address.__dict__.values()]
    billing_address = [str(x) for x in order.billing_address.__dict__.values()]
    assert order_qs.count() == 1
    assert Order.objects.all().count() == 2
    for value in shipping_data.values():
        assert value in shipping_address
    for value in billing_data.values():
        assert value in billing_address
    assert order.user == get_sentinel_user_anonymous()
    assert order.items.all().count() == 1