2017-03-29 16:28:43 +00:00
|
|
|
import factory
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
|
|
from rest_framework.test import APITestCase
|
|
|
|
from rest_framework import status
|
|
|
|
|
|
|
|
from wagtail_factories import PageFactory
|
|
|
|
|
2017-09-17 12:01:12 +00:00
|
|
|
|
2017-03-29 16:28:43 +00:00
|
|
|
from longclaw.longclawbasket.models import BasketItem
|
2017-08-09 13:46:48 +00:00
|
|
|
from longclaw.longclaworders.models import Order
|
2017-05-24 10:12:36 +00:00
|
|
|
from longclaw.longclawshipping.models import Address, Country, ShippingRate
|
2017-09-17 12:01:12 +00:00
|
|
|
from longclaw.utils import ProductVariant, maybe_get_product_model
|
2017-03-29 16:28:43 +00:00
|
|
|
|
2017-05-24 10:12:36 +00:00
|
|
|
class OrderFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = Order
|
|
|
|
|
|
|
|
class CountryFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = Country
|
|
|
|
|
|
|
|
iso = factory.Faker('pystr', max_chars=2, min_chars=2)
|
|
|
|
name_official = factory.Faker('text', max_nb_chars=128)
|
|
|
|
name = factory.Faker('text', max_nb_chars=128)
|
|
|
|
|
|
|
|
|
|
|
|
class AddressFactory(factory.django.DjangoModelFactory):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Address
|
|
|
|
|
|
|
|
name = factory.Faker('text', max_nb_chars=64)
|
|
|
|
line_1 = factory.Faker('text', max_nb_chars=128)
|
|
|
|
line_2 = factory.Faker('text', max_nb_chars=128)
|
|
|
|
city = factory.Faker('text', max_nb_chars=64)
|
|
|
|
postcode = factory.Faker('text', max_nb_chars=10)
|
|
|
|
country = factory.SubFactory(CountryFactory)
|
|
|
|
|
|
|
|
class ShippingRateFactory(factory.django.DjangoModelFactory):
|
|
|
|
class Meta:
|
|
|
|
model = ShippingRate
|
|
|
|
|
|
|
|
name = factory.Faker('text', max_nb_chars=32)
|
|
|
|
rate = 1.0
|
|
|
|
carrier = 'Royal Mail'
|
|
|
|
description = 'Test'
|
|
|
|
|
|
|
|
@factory.post_generation
|
|
|
|
def countries(self, create, extracted, **kwargs):
|
|
|
|
if not create:
|
|
|
|
# Simple build, do nothing.
|
|
|
|
return
|
|
|
|
|
|
|
|
if extracted:
|
|
|
|
# A list of countries were passed in, use them
|
|
|
|
for country in extracted:
|
|
|
|
self.countries.add(country)
|
|
|
|
|
2017-03-29 16:28:43 +00:00
|
|
|
class ProductFactory(PageFactory):
|
2017-10-01 10:19:37 +00:00
|
|
|
""" Create a random Product
|
|
|
|
"""
|
2017-09-17 12:01:12 +00:00
|
|
|
|
2017-03-29 16:28:43 +00:00
|
|
|
class Meta:
|
2017-09-17 12:01:12 +00:00
|
|
|
model = maybe_get_product_model()
|
2017-03-29 16:28:43 +00:00
|
|
|
|
|
|
|
title = factory.Faker('sentence', nb_words=1)
|
|
|
|
description = factory.Faker('text')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _create(cls, model_class, *args, **kwargs):
|
2017-04-02 09:14:20 +00:00
|
|
|
kwargs['parent'] = None
|
|
|
|
return super(ProductFactory, cls)._create(model_class, *args, **kwargs)
|
2017-03-29 16:28:43 +00:00
|
|
|
|
|
|
|
class ProductVariantFactory(factory.django.DjangoModelFactory):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ProductVariant
|
|
|
|
|
|
|
|
product = factory.SubFactory(ProductFactory)
|
2017-04-02 09:14:20 +00:00
|
|
|
description = factory.Faker('text')
|
2017-09-17 15:36:04 +00:00
|
|
|
base_price = factory.Faker('pyfloat', positive=True, left_digits=2, right_digits=2)
|
2017-03-29 16:28:43 +00:00
|
|
|
ref = factory.Faker('pystr', min_chars=3, max_chars=10)
|
|
|
|
stock = factory.Faker('pyint')
|
|
|
|
|
|
|
|
|
|
|
|
class BasketItemFactory(factory.django.DjangoModelFactory):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BasketItem
|
|
|
|
|
|
|
|
quantity = 1
|
|
|
|
variant = factory.SubFactory(ProductVariantFactory)
|
|
|
|
|
|
|
|
class LongclawTestCase(APITestCase):
|
|
|
|
|
|
|
|
def get_test(self, urlname, urlkwargs=None, **kwargs):
|
2017-10-01 10:19:37 +00:00
|
|
|
""" Submit a GET request and assert the response status code is 200
|
2017-03-29 16:28:43 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
urlname (str): The url name to pass to the 'reverse' function
|
|
|
|
urlkwargs (dict): The `kwargs` parameter to pass to the `reverse` function
|
2017-10-01 10:19:37 +00:00
|
|
|
"""
|
2017-03-29 16:28:43 +00:00
|
|
|
response = self.client.get(reverse(urlname, kwargs=urlkwargs), **kwargs)
|
2017-04-02 09:14:20 +00:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2017-03-29 16:28:43 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
def post_test(self, data, urlname, urlkwargs=None, **kwargs):
|
2017-10-01 10:19:37 +00:00
|
|
|
""" Submit a POST request and assert the response status code is 201
|
2017-03-29 16:28:43 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
data (dict): The data to pass to the post request
|
|
|
|
urlname (str): The url name to pass to the 'reverse' function
|
|
|
|
urlkwargs (dict): The `kwargs` parameter to pass to the `reverse` function
|
2017-10-01 10:19:37 +00:00
|
|
|
"""
|
2017-03-29 16:28:43 +00:00
|
|
|
response = self.client.post(reverse(urlname, kwargs=urlkwargs), data, **kwargs)
|
|
|
|
self.assertIn(response.status_code,
|
2017-05-26 21:11:27 +00:00
|
|
|
(status.HTTP_201_CREATED, status.HTTP_200_OK, status.HTTP_204_NO_CONTENT))
|
2017-03-29 16:28:43 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
def patch_test(self, data, urlname, urlkwargs=None, **kwargs):
|
2017-10-01 10:19:37 +00:00
|
|
|
""" Submit a PATCH request and assert the response status code is 200
|
|
|
|
"""
|
2017-03-29 16:28:43 +00:00
|
|
|
response = self.client.patch(reverse(urlname, kwargs=urlkwargs), data, **kwargs)
|
2017-04-02 09:14:20 +00:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2017-03-29 16:28:43 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
def put_test(self, data, urlname, urlkwargs=None, **kwargs):
|
|
|
|
response = self.client.put(reverse(urlname, kwargs=urlkwargs), data, **kwargs)
|
2017-04-02 09:14:20 +00:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
|
|
|
|
return response
|
2017-05-26 16:46:38 +00:00
|
|
|
|
|
|
|
def del_test(self, urlname, urlkwargs=None, **kwargs):
|
|
|
|
response = self.client.delete(reverse(urlname, kwargs=urlkwargs), **kwargs)
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
return response
|