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.

test_wishlists.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. from django.urls import reverse_lazy
  3. from oscar.core.loading import get_model
  4. from oscar.test.factories import create_product, WishListFactory
  5. from oscar.test.testcases import WebTestCase
  6. WishList = get_model('wishlists', 'WishList')
  7. class WishListTestMixin(object):
  8. is_anonymous = False
  9. def setUp(self):
  10. super().setUp()
  11. self.product = create_product()
  12. class TestProductDetailPage(WishListTestMixin, WebTestCase):
  13. def test_allows_a_product_to_be_added_to_wishlist(self):
  14. # Click add to wishlist button
  15. detail_page = self.get(self.product.get_absolute_url())
  16. form = detail_page.forms['add_to_wishlist_form']
  17. response = form.submit()
  18. self.assertIsRedirect(response)
  19. # Check a wishlist has been created
  20. wishlists = self.user.wishlists.all()
  21. self.assertEqual(1, len(wishlists))
  22. lines = wishlists[0].lines.all()
  23. self.assertEqual(1, len(lines))
  24. self.assertEqual(self.product, lines[0].product)
  25. class TestMoveProductToAnotherWishList(WishListTestMixin, WebTestCase):
  26. def setUp(self):
  27. super().setUp()
  28. self.wishlist1 = WishListFactory(owner=self.user)
  29. self.wishlist2 = WishListFactory(owner=self.user)
  30. def test_move_product_to_another_wishlist_already_containing_it(self):
  31. self.wishlist1.add(self.product)
  32. line1 = self.wishlist1.lines.get(product=self.product)
  33. self.wishlist2.add(self.product)
  34. url = reverse_lazy('customer:wishlists-move-product-to-another', kwargs={'key': self.wishlist1.key,
  35. 'line_pk': line1.pk,
  36. 'to_key': self.wishlist2.key})
  37. self.get(url)
  38. self.assertEqual(self.wishlist1.lines.filter(product=self.product).count(), 1)
  39. self.assertEqual(self.wishlist2.lines.filter(product=self.product).count(), 1)
  40. def test_move_product_to_another_wishlist(self):
  41. self.wishlist1.add(self.product)
  42. line1 = self.wishlist1.lines.get(product=self.product)
  43. url = reverse_lazy('customer:wishlists-move-product-to-another', kwargs={'key': self.wishlist1.key,
  44. 'line_pk': line1.pk,
  45. 'to_key': self.wishlist2.key})
  46. self.get(url)
  47. self.assertEqual(self.wishlist1.lines.filter(product=self.product).count(), 0)
  48. self.assertEqual(self.wishlist2.lines.filter(product=self.product).count(), 1)
  49. # Test WishList doesnt contain line and return 404
  50. self.assertEqual(self.get(url, expect_errors=True).status_code, 404)
  51. class TestWishListRemoveProduct(WishListTestMixin, WebTestCase):
  52. def setUp(self):
  53. super().setUp()
  54. self.wishlist = WishListFactory(owner=self.user)
  55. self.wishlist.add(self.product)
  56. self.line = self.wishlist.lines.get(product=self.product)
  57. def test_remove_wishlist_line(self):
  58. delete_wishlist_line_url = reverse_lazy(
  59. 'customer:wishlists-remove-product', kwargs={'key': self.wishlist.key, 'line_pk': self.line.pk}
  60. )
  61. self.get(delete_wishlist_line_url).forms[0].submit()
  62. # Test WishList doesnt contain line and return 404
  63. self.assertEqual(self.get(delete_wishlist_line_url, expect_errors=True).status_code, 404)
  64. def test_remove_wishlist_product(self):
  65. delete_wishlist_product_url = reverse_lazy(
  66. 'customer:wishlists-remove-product', kwargs={'key': self.wishlist.key, 'product_pk': self.line.product.id}
  67. )
  68. self.get(delete_wishlist_product_url).forms[0].submit()
  69. # Test WishList doesnt contain line and return 404
  70. self.assertEqual(self.get(delete_wishlist_product_url, expect_errors=True).status_code, 404)