184 lines
6.6 KiB
Python
184 lines
6.6 KiB
Python
from decimal import Decimal
|
|
|
|
from rest_framework import status
|
|
from rest_framework.test import APITestCase
|
|
|
|
from config.tests.mixins import TestUserAuthenticationMixin
|
|
from shop.models import Product, ProductPrice, Tax, Customer
|
|
|
|
|
|
class TestOrdersAPI(APITestCase, TestUserAuthenticationMixin):
|
|
model_name = "productprice"
|
|
|
|
def setUp(self):
|
|
self.create_user()
|
|
self.tax = Tax.objects.create(
|
|
code="IVA",
|
|
value=21,
|
|
)
|
|
self.customer = Customer.objects.create(
|
|
vat_id="11111111H",
|
|
first_name="Darth",
|
|
last_name="Maull",
|
|
email="darth@maul.com",
|
|
address="Dathomir",
|
|
city="Dathomir",
|
|
state="Dathomir",
|
|
country="Dathomir",
|
|
zip="00001",
|
|
)
|
|
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/",
|
|
{
|
|
"customer": self.customer.pk,
|
|
"billing_address": self.customer.address,
|
|
"billing_city": self.customer.city,
|
|
"billing_state": self.customer.state,
|
|
"billing_country": self.customer.country,
|
|
"billing_zip": self.customer.zip,
|
|
"shipping_address": self.customer.address,
|
|
"shipping_city": self.customer.city,
|
|
"shipping_state": self.customer.state,
|
|
"shipping_country": self.customer.country,
|
|
"shipping_zip": self.customer.zip,
|
|
"contact_phone": "612345678",
|
|
}
|
|
)
|
|
|
|
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/",
|
|
{
|
|
"customer": self.customer.pk,
|
|
"billing_address": self.customer.address,
|
|
"billing_city": self.customer.city,
|
|
"billing_state": self.customer.state,
|
|
"billing_country": self.customer.country,
|
|
"billing_zip": self.customer.zip,
|
|
"shipping_address": self.customer.address,
|
|
"shipping_city": self.customer.city,
|
|
"shipping_state": self.customer.state,
|
|
"shipping_country": self.customer.country,
|
|
"shipping_zip": self.customer.zip,
|
|
"contact_phone": "612345678",
|
|
}
|
|
)
|
|
|
|
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/",
|
|
{
|
|
"customer": self.customer.pk,
|
|
"billing_address": self.customer.address,
|
|
"billing_city": self.customer.city,
|
|
"billing_state": self.customer.state,
|
|
"billing_country": self.customer.country,
|
|
"billing_zip": self.customer.zip,
|
|
"shipping_address": self.customer.address,
|
|
"shipping_city": self.customer.city,
|
|
"shipping_state": self.customer.state,
|
|
"shipping_country": self.customer.country,
|
|
"shipping_zip": self.customer.zip,
|
|
"contact_phone": "612345678",
|
|
}
|
|
)
|
|
|
|
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')
|