from decimal import Decimal from django.contrib.auth import get_user_model from rest_framework import status from rest_framework.test import APITestCase from config.tests.mixins import TestUserAuthenticationMixin from shop.models import CustomerAddress, Product, ProductPrice, Tax User = get_user_model() class TestOrdersAPI(APITestCase, TestUserAuthenticationMixin): model_name = "productprice" def setUp(self): self.create_user() self.tax = Tax.objects.create( code="IVA", value=21, ) self.customer = get_user_model().objects.create_user( username="11111111H", first_name="Darth", last_name="Maull", email="darth@maul.com", password="dathomir", ) self.customer_shipping_address = CustomerAddress.objects.create( user=self.customer, address="Dathomir", address_town="Dathomir", address_zip="00001", address_state="Dathomir", address_phone="900000000", address_type=CustomerAddress.Types.SHIPPING, ) self.customer_billing_address = CustomerAddress.objects.create( user=self.customer, address="Dathomir", address_town="Dathomir", address_zip="00001", address_state="Dathomir", address_phone="900000000", address_type=CustomerAddress.Types.BILLING, ) self.create_products() def create_products(self): self.product = Product.objects.create( name="Papafritas", description="Las mejores papafritas", is_digital_asset=False, url="", ) ProductPrice.objects.create( product=self.product, price=Decimal("1.20"), tax=self.tax, ) def test_create_retrieve_order(self): self.login() response = self.client.post( f"/api/v1/shop/orders/", { "user": self.user.pk, "billing_address": self.customer_billing_address.address, "billing_city": self.customer_billing_address.address_town, "billing_state": self.customer_billing_address.address_state, "billing_country": self.customer_billing_address.address_country, "billing_zip": self.customer_billing_address.address_zip, "shipping_address": self.customer_shipping_address.address, "shipping_city": self.customer_shipping_address.address_town, "shipping_state": self.customer_shipping_address.address_state, "shipping_country": self.customer_shipping_address.address_country, "shipping_zip": self.customer_shipping_address.address_zip, "contact_phone": self.customer_shipping_address.address_phone, }, ) assert response.status_code == status.HTTP_201_CREATED pk = response.data.get("uuid") response = self.client.get(f"/api/v1/shop/orders/{pk}/") assert response.status_code == status.HTTP_200_OK def test_create_retrieve_order_with_lines(self): self.login() response = self.client.post( f"/api/v1/shop/orders/", { "user": self.user.pk, "billing_address": self.customer_billing_address.address, "billing_city": self.customer_billing_address.address_town, "billing_state": self.customer_billing_address.address_state, "billing_country": self.customer_billing_address.address_country, "billing_zip": self.customer_billing_address.address_zip, "shipping_address": self.customer_shipping_address.address, "shipping_city": self.customer_shipping_address.address_town, "shipping_state": self.customer_shipping_address.address_state, "shipping_country": self.customer_shipping_address.address_country, "shipping_zip": self.customer_shipping_address.address_zip, "contact_phone": self.customer_shipping_address.address_phone, }, ) assert response.status_code == status.HTTP_201_CREATED pk = response.data.get("uuid") response = self.client.get(f"/api/v1/shop/orders/{pk}/") assert response.status_code == status.HTTP_200_OK assert Decimal(response.data.get("base_total")) == Decimal("0.00") assert Decimal(response.data.get("total")) == Decimal("0.00") response = self.client.post( f"/api/v1/shop/orders/{pk}/lines/", { "product": self.product.pk, "quantity": "5", }, ) assert response.status_code == status.HTTP_201_CREATED response = self.client.get(f"/api/v1/shop/orders/{pk}/lines/") assert len(response.data.get("results")) == 1 response = self.client.get(f"/api/v1/shop/orders/{pk}/") assert response.status_code == status.HTTP_200_OK assert Decimal(response.data.get("base_total")) == Decimal("6.00") assert Decimal(response.data.get("total")) == Decimal("6.00") + ( Decimal("6.00") * Decimal("0.21") ) def test_create_delete_order_with_lines(self): self.login() # Create order response = self.client.post( f"/api/v1/shop/orders/", { "user": self.user.pk, "billing_address": self.customer_billing_address.address, "billing_city": self.customer_billing_address.address_town, "billing_state": self.customer_billing_address.address_state, "billing_country": self.customer_billing_address.address_country, "billing_zip": self.customer_billing_address.address_zip, "shipping_address": self.customer_shipping_address.address, "shipping_city": self.customer_shipping_address.address_town, "shipping_state": self.customer_shipping_address.address_state, "shipping_country": self.customer_shipping_address.address_country, "shipping_zip": self.customer_shipping_address.address_zip, "contact_phone": self.customer_shipping_address.address_phone, }, ) assert response.status_code == status.HTTP_201_CREATED pk = response.data.get("uuid") response = self.client.get(f"/api/v1/shop/orders/{pk}/") assert response.status_code == status.HTTP_200_OK # Assert order is created with 0.00€ assert Decimal(response.data.get("base_total")) == Decimal("0.00") assert Decimal(response.data.get("total")) == Decimal("0.00") # Create order line response = self.client.post( f"/api/v1/shop/orders/{pk}/lines/", { "product": self.product.pk, "quantity": "5", }, ) line_pk = response.data.get("id") assert response.status_code == status.HTTP_201_CREATED response = self.client.get(f"/api/v1/shop/orders/{pk}/lines/") assert len(response.data.get("results")) == 1 response = self.client.get(f"/api/v1/shop/orders/{pk}/") assert response.status_code == status.HTTP_200_OK # Assert order totals are set assert Decimal(response.data.get("base_total")) == Decimal("6.00") assert Decimal(response.data.get("total")) == Decimal("6.00") + ( Decimal("6.00") * Decimal("0.21") ) # Delete line response = self.client.delete(f"/api/v1/shop/orders/{pk}/lines/{line_pk}/") assert response.status_code == status.HTTP_204_NO_CONTENT response = self.client.get(f"/api/v1/shop/orders/{pk}/") assert response.status_code == status.HTTP_200_OK # Assert order totals are 0.00 assert Decimal(response.data.get("base_total")) == Decimal("0.00") assert Decimal(response.data.get("total")) == Decimal("0.00")