You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

product_tests.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from django.core.urlresolvers import reverse
  2. from oscar.test import factories
  3. from oscar.test.testcases import WebTestCase
  4. from oscar.core.compat import get_user_model
  5. from oscar.apps.catalogue.models import Product
  6. from oscar.test.factories import (
  7. CategoryFactory, ProductFactory, ProductClassFactory)
  8. User = get_user_model()
  9. class ProductWebTest(WebTestCase):
  10. is_staff = True
  11. def setUp(self):
  12. self.user = User.objects.create_user(username='testuser',
  13. email='test@email.com',
  14. password='somefancypassword')
  15. self.user.is_staff = self.is_staff
  16. self.user.save()
  17. def get(self, url, **kwargs):
  18. kwargs['user'] = self.user
  19. return self.app.get(url, **kwargs)
  20. class TestGatewayPage(ProductWebTest):
  21. is_staff = True
  22. def test_redirects_to_list_page_when_no_query_param(self):
  23. url = reverse('dashboard:catalogue-product-create')
  24. response = self.get(url)
  25. self.assertRedirects(response,
  26. reverse('dashboard:catalogue-product-list'))
  27. def test_redirects_to_list_page_when_invalid_query_param(self):
  28. url = reverse('dashboard:catalogue-product-create')
  29. response = self.get(url + '?product_class=bad')
  30. self.assertRedirects(response,
  31. reverse('dashboard:catalogue-product-list'))
  32. def test_redirects_to_form_page_when_valid_query_param(self):
  33. pclass = ProductClassFactory(name='Books', slug='books')
  34. url = reverse('dashboard:catalogue-product-create')
  35. response = self.get(url + '?product_class=%s' % pclass.pk)
  36. expected_url = reverse('dashboard:catalogue-product-create',
  37. kwargs={'product_class_slug': pclass.slug})
  38. self.assertRedirects(response, expected_url)
  39. class TestCreateParentProduct(ProductWebTest):
  40. is_staff = True
  41. def setUp(self):
  42. self.pclass = ProductClassFactory(name='Books', slug='books')
  43. super(TestCreateParentProduct, self).setUp()
  44. def submit(self, title=None, category=None, upc=None):
  45. url = reverse('dashboard:catalogue-product-create',
  46. kwargs={'product_class_slug': self.pclass.slug})
  47. product_form = self.get(url).form
  48. product_form['title'] = title
  49. product_form['upc'] = upc
  50. product_form['structure'] = 'parent'
  51. if category:
  52. product_form['productcategory_set-0-category'] = category.id
  53. return product_form.submit()
  54. def test_title_is_required(self):
  55. response = self.submit(title='')
  56. self.assertContains(response, "must have a title")
  57. self.assertEqual(Product.objects.count(), 0)
  58. def test_requires_a_category(self):
  59. response = self.submit(title="Nice T-Shirt")
  60. self.assertContains(response,
  61. "must have at least one category")
  62. self.assertEqual(Product.objects.count(), 0)
  63. def test_for_smoke(self):
  64. category = CategoryFactory()
  65. response = self.submit(title='testing', category=category)
  66. self.assertIsRedirect(response)
  67. self.assertEqual(Product.objects.count(), 1)
  68. def test_doesnt_allow_duplicate_upc(self):
  69. ProductFactory(parent=None, upc="12345")
  70. category = CategoryFactory()
  71. self.assertTrue(Product.objects.get(upc="12345"))
  72. response = self.submit(title="Nice T-Shirt", category=category,
  73. upc="12345")
  74. self.assertEqual(Product.objects.count(), 1)
  75. self.assertNotEqual(Product.objects.get(upc='12345').title,
  76. 'Nice T-Shirt')
  77. self.assertContains(response,
  78. "Product with this UPC already exists.")
  79. class TestCreateChildProduct(ProductWebTest):
  80. is_staff = True
  81. def setUp(self):
  82. self.pclass = ProductClassFactory(name='Books', slug='books')
  83. self.parent = ProductFactory(structure='parent', stockrecords=[])
  84. super(TestCreateChildProduct, self).setUp()
  85. def test_categories_are_not_required(self):
  86. url = reverse('dashboard:catalogue-product-create-child',
  87. kwargs={'parent_pk': self.parent.pk})
  88. page = self.get(url)
  89. product_form = page.form
  90. product_form['title'] = expected_title = 'Nice T-Shirt'
  91. product_form.submit()
  92. try:
  93. product = Product.objects.get(title=expected_title)
  94. except Product.DoesNotExist:
  95. self.fail('creating a child product did not work')
  96. self.assertEqual(product.parent, self.parent)
  97. class TestProductUpdate(ProductWebTest):
  98. def test_product_update_form(self):
  99. self.product = factories.ProductFactory()
  100. url = reverse('dashboard:catalogue-product',
  101. kwargs={'pk': self.product.id})
  102. page = self.get(url)
  103. product_form = page.form
  104. product_form['title'] = expected_title = 'Nice T-Shirt'
  105. page = product_form.submit()
  106. product = Product.objects.get(id=self.product.id)
  107. self.assertEqual(page.context['product'], self.product)
  108. self.assertEqual(product.title, expected_title)