Add some more tests for ProductAlert form validation.
@@ -20,18 +20,31 @@ from oscar.test.factories import (
class TestAUser(WebTest):
+ def setUp(self):
+ self.user = UserFactory()
+ self.product = create_product(num_in_stock=0)
+
def test_can_create_a_stock_alert(self):
- user = UserFactory()
- product = create_product(num_in_stock=0)
- product_page = self.app.get(product.get_absolute_url(), user=user)
+ product_page = self.app.get(self.product.get_absolute_url(), user=self.user)
form = product_page.forms['alert_form']
form.submit()
- alerts = ProductAlert.objects.filter(user=user)
+ alerts = ProductAlert.objects.filter(user=self.user)
self.assertEqual(1, len(alerts))
alert = alerts[0]
self.assertEqual(ProductAlert.ACTIVE, alert.status)
- self.assertEqual(alert.product, product)
+ self.assertEqual(alert.product, self.product)
+
+ def test_cannot_create_multiple_alerts_for_one_product(self):
+ alert = ProductAlertFactory(user=self.user, product=self.product,
+ status = ProductAlert.ACTIVE)
+ # Alert form should not allow creation of additional alerts.
+ form = ProductAlertForm(user=self.user, product=self.product, data={})
+
+ self.assertFalse(form.is_valid())
+ self.assertIn(
+ "You already have an active alert for this product",
+ form.errors['__all__'][0])
class TestAUserWithAnActiveStockAlert(WebTest):
@@ -98,6 +111,22 @@ class TestAnAnonymousUser(WebTest):
alert.refresh_from_db()
self.assertTrue(alert.is_cancelled)
+ def test_cannot_create_multiple_alerts_for_one_product(self):
+ product = create_product(num_in_stock=0)
+ alert = ProductAlertFactory(user=None, product=product,
+ email='john@smith.com')
+ alert.status = ProductAlert.ACTIVE
+ alert.save()
+
+ # Alert form should not allow creation of additional alerts.
+ form = ProductAlertForm(user=AnonymousUser(), product=product,
+ data={'email': 'john@smith.com'})
+
+ self.assertFalse(form.is_valid())
+ self.assertIn(
+ "There is already an active stock alert for john@smith.com",
+ form.errors['__all__'][0])
+
def test_cannot_create_multiple_unconfirmed_alerts(self):
# Create an unconfirmed alert
alert = ProductAlertFactory(
