Permalink
Browse files

notifications feature now has funtional test using webtest

* all existing functional tests for notifications to
    are using ``WebTest`` now
* removes the ``DeleteView`` for notifications that allow a
    registered user to delete notifications as it is not used
    anywhere
* the notification button on the catalogue list page is now
    hidden. It is only displayed on the product page
  • Loading branch information...
1 parent 78836cd commit 9bddf69de7d8bfab7a40ed6723147f8a991fbeec Sebastian Vetter committed Sep 6, 2012
@@ -11,7 +11,6 @@ class ProductNotificationApplication(Application):
unsubscribe_view = views.NotificationUnsubscribeView
create_view = views.ProductNotificationCreateView
update_view = views.ProductNotificationSetStatusView
- delete_view = views.ProductNotificationDeleteView
def get_urls(self):
urlpatterns = patterns('',
@@ -30,9 +29,6 @@ def get_urls(self):
),
login_required(self.update_view.as_view()),
name='notification-set-status'),
- url(r'^(?P<notification_pk>\d+)/delete/$',
- login_required(self.delete_view.as_view()),
- name='notification-delete'),
)
return urlpatterns
@@ -294,17 +294,3 @@ def get_success_url(self):
detail_url = reverse('catalogue:detail',
args=(self.product.slug, self.product.pk))
return self.request.META.get('HTTP_REFERER', detail_url)
-
-
-class ProductNotificationDeleteView(generic.DeleteView):
- """
- View to delete a product notification. This should not be available
- to the users as their can only activate and deactivate notifications.
- It should be checked for logged in staff member.
- """
- model = ProductNotification
- template_name = 'notification/delete.html'
- pk_url_kwarg = 'notification_pk'
-
- def get_success_url(self):
- return reverse('dashboard:index')
@@ -28,7 +28,7 @@
<h3><a href="{{ product.get_absolute_url }}">{{ product.get_title|truncatewords:7 }}</a></h3>
<div class="product_price">
- {% include "catalogue/partials/stock_record.html" %}
+ {% include "catalogue/partials/stock_record.html" with anon_notifications="hide" %}
{% include "catalogue/partials/add_to_basket_form_compact.html" %}
</div>
</article>
@@ -11,7 +11,7 @@ <h2 class="price_color">{{ product.stockrecord.price_incl_tax|currency }}</h2>
{% has_product_notification user product as notification_exists %}
{% if not notification_exists %}
- <p>{% include "notification/partials/notification_form.html" with form=notification_form %}</p>
+ <p>{% include "notification/partials/notification_form.html" with form=notification_form anon_notifications=anon_notifications %}</p>
{% else %}
<p class="alert alert-success">{% trans "You will be notified when this product is available." %}</p>
{% endif %}
@@ -1,19 +0,0 @@
-{% extends "base.html" %}
-{% load i18n %}
-
-{% block main-column %}
- <div class="well well-danger">
- <h1>{% trans "Delete notification?" %}</h1>
- <form action="." method="post">
- {% blocktrans with email=notification.get_notification_email product_title=notification.product.title %}
- Are you sure that you want to delete notification of
- {{ email }} for product
- {{ product_title }}?
- {% endblocktrans %}
- <div class="form-actions">
- <button type='submit' name='submit' class="btn btn-danger">{% trans "Delete" %}</button> or
- <a href="{% url customer:summary %}">{% trans "cancel" %}</a>
- </div>
- </form>
- </div>
-{% endblock %}
@@ -1,6 +1,6 @@
{% load i18n %}
-{% if product.stockrecord.num_in_stock == 0 %}
+{% if product.stockrecord.num_in_stock == 0 and anon_notifications != "hide" %}
<form method="post" action="{% url catalogue:notification-create product.slug product.id %}">
{% csrf_token %}
<p>{% trans "You can get an email notification when this product is in stock." %}</p>
@@ -6,37 +6,18 @@
from django.core.urlresolvers import reverse
from django_dynamic_fixture import get
+from webtest.app import AppError
from django_webtest import WebTest
from django_dynamic_fixture import get as G
-from oscar.test import ClientTestCase
from oscar.apps.catalogue.models import Product, ProductClass
from oscar.apps.partner.models import StockRecord
from oscar.apps.catalogue.notification.models import ProductNotification
Partner = get_model('partner', 'partner')
-class CreatorMixin(object):
-
- def create_product_class(self, name='books'):
- self.product_class = ProductClass.objects.create(name=name)
-
- def create_product(self):
- product_id = self.product_counter.next()
- product = get(Product, product_class=self.product_class,
- title='product_%s' % product_id,
- upc='00000000000%s' % product_id, parent=None)
-
- G(StockRecord, product=product, num_in_stock=0)
- return product
-
-
-class NotificationTestCase(ClientTestCase, CreatorMixin):
- product_counter = itertools.count()
-
-
-class NotificationWebTest(WebTest, CreatorMixin):
+class NotificationWebTest(WebTest):
product_counter = itertools.count()
username = 'testuser'
password = 'somerandompassword'
@@ -51,6 +32,18 @@ def setUp(self):
password=self.password,
email=self.email)
+ def create_product_class(self, name='books'):
+ self.product_class = ProductClass.objects.create(name=name)
+
+ def create_product(self):
+ product_id = self.product_counter.next()
+ product = get(Product, product_class=self.product_class,
+ title='product_%s' % product_id,
+ upc='00000000000%s' % product_id, parent=None)
+
+ G(StockRecord, product=product, num_in_stock=0)
+ return product
+
def get(self, *args, **kwargs):
if self.user and not 'user' in kwargs:
kwargs['user'] = self.user
@@ -62,36 +55,33 @@ def post(self, *args, **kwargs):
return self.app.post(*args, **kwargs)
-class TestNotifyMeButtons(NotificationTestCase):
+class TestNotifyMeButtons(NotificationWebTest):
def setUp(self):
self.create_product_class()
self.product = self.create_product()
def test_are_displayed_on_unavailable_product_page(self):
- self.product.stockrecord.num_in_stock = 0
- self.product.stockrecord.save()
url = reverse('catalogue:detail', args=(self.product.slug,
self.product.id))
- response = self.client.get(url)
-
- self.assertContains(response, 'notify-me', status_code=200)
+ page = self.app.get(url)
+ self.assertContains(page, 'notify-me', status_code=200)
def test_are_not_displayed_on_available_product_page(self):
self.product.stockrecord.num_in_stock = 20
self.product.stockrecord.save()
url = reverse('catalogue:detail', args=(self.product.slug,
self.product.id))
- response = self.client.get(url)
- self.assertNotContains(response, 'notify-me', status_code=200)
+ page = self.app.get(url)
+ self.assertNotContains(page, 'notify-me', status_code=200)
self.product.stockrecord.num_in_stock = 1
self.product.stockrecord.save()
- response = self.client.get(url)
- self.assertNotContains(response, 'notify-me', status_code=200)
+ page = self.app.get(url)
+ self.assertNotContains(page, 'notify-me', status_code=200)
class TestAnAnonymousUserRequestingANotification(NotificationWebTest):
@@ -104,13 +94,19 @@ def setUp(self):
self.out_of_stock_product = self.create_product()
def create_notification(self):
- page = self.get(reverse('catalogue:index'))
+ page = self.get(reverse('catalogue:detail', args=(
+ self.out_of_stock_product.slug,
+ self.out_of_stock_product.id,
+ )))
notify_form = page.forms[1]
notify_form['email'] = self.email
return notify_form.submit()
def test_submitting_an_invalid_email_redirects_back_to_page(self):
- page = self.get(reverse('catalogue:index'))
+ page = self.get(reverse('catalogue:detail', args=(
+ self.out_of_stock_product.slug,
+ self.out_of_stock_product.id,
+ )))
notify_form = page.forms[1]
notify_form['email'] = u"invalid_email.com"
page = notify_form.submit()
@@ -173,39 +169,34 @@ def test_can_unsubscribe_from_a_notification(self):
self.assertEquals(notification.status, ProductNotification.INACTIVE)
-class TestARegisteredUserRequestingANotification(NotificationTestCase):
+class TestARegisteredUserRequestingANotification(NotificationWebTest):
is_anonymous = False
- is_staff = False
email = 'testuser@oscar.com'
def setUp(self):
super(TestARegisteredUserRequestingANotification, self).setUp()
self.create_product_class()
self.product = self.create_product()
- #self.create_product()
def test_sees_email_on_product_page(self):
product_url = reverse('catalogue:detail',
args=(self.product.slug, self.product.id))
- self.client.login()
- response = self.client.get(product_url)
+ page = self.get(product_url)
- self.assertContains(response, self.email, status_code=200)
+ self.assertContains(page, self.email, status_code=200)
def test_creates_a_notification_object(self):
# Test creating a notification for an authenticated user with the
# providing the account email address in the (hidden) signup form.
self.assertEquals(self.user.notifications.count(), 0)
- self.client.login()
- notification_url = reverse('catalogue:notification-create',
- args=(self.product.slug,
- self.product.id))
- response = self.client.post(notification_url,
- data={'email': self.email},
- follow=True)
+ page = self.get(reverse('catalogue:detail', args=(self.product.slug,
+ self.product.id)))
+ notification_form = page.forms[1]
+ notification_form['email'] = self.email
+ page = notification_form.submit().follow()
- self.assertContains(response, self.product.title, status_code=200)
+ self.assertContains(page, self.product.title, status_code=200)
self.assertEquals(self.user.notifications.count(), 1)
notification = self.user.notifications.all()[0]
@@ -218,13 +209,13 @@ def test_can_specify_an_alternative_email_address(self):
# Test creating a notification with an email address that is different
# from the user's account email. This should set the account email
# address instead of the provided email in POST data.
- notification_url = reverse('catalogue:notification-create',
- args=(self.product.slug, self.product.id))
- response = self.client.post(notification_url,
- data={'email': 'someother@oscar.com'},
- follow=True)
+ page = self.get(reverse('catalogue:detail', args=(self.product.slug,
+ self.product.id)))
+ notification_form = page.forms[1]
+ notification_form['email'] = 'someother@oscar.com'
+ page = notification_form.submit().follow()
- self.assertContains(response, 'notified', status_code=200)
+ self.assertContains(page, 'notified', status_code=200)
self.assertEquals(self.user.notifications.count(), 1)
@@ -239,29 +230,26 @@ def test_cannot_create_duplicate_notifications(self):
# Test creating a notification when the user has already signed up for
# this product notification. The user should be redirected to the product
# page with a notification that he has already signed up.
- notification = get(ProductNotification, product=self.product,
- user=self.user)
- notification_url = reverse('catalogue:notification-create',
- args=(self.product.slug, self.product.id))
- response = self.client.post(notification_url,
- data={'email': self.user.email},
- follow=True)
+ get(ProductNotification, product=self.product, user=self.user)
- self.assertContains(response, self.product.title, status_code=200)
- self.assertEquals(self.user.notifications.count(), 1)
- self.assertEquals(notification,
- self.user.notifications.all()[0].productnotification)
+ page = self.get(reverse('catalogue:detail', args=(self.product.slug,
+ self.product.id)))
+ self.assertContains(
+ page,
+ "You will be notified when this product is available",
+ )
-class TestAnAnonymousButExistingUserRequestingANotification(NotificationTestCase):
+class TestAnAnonymousButExistingUserRequestingANotification(NotificationWebTest):
is_anonymous = True
email = 'testuser@oscar.com'
username = 'testuser'
- password = 'password'
+ password = 'somerandompassword'
def setUp(self):
super(TestAnAnonymousButExistingUserRequestingANotification, self).setUp()
- self.create_user()
+ User.objects.create(username=self.username, password=self.password,
+ email=self.email)
self.create_product_class()
self.product = self.create_product()
@@ -271,23 +259,19 @@ def test_gets_redirected_to_login_page(self):
# user profile has this email address set, the user will be redirected
# to the login page and from there right back to the product detail
# page where the user hits the 'Notify Me' button again.
- notification_url = reverse('catalogue:notification-create',
- args=(self.product.slug,
- self.product.id))
- response = self.client.post(notification_url,
- data={'email': self.email},
- follow=True)
+ product_url = reverse('catalogue:detail', args=(self.product.slug,
+ self.product.id))
+ page = self.app.get(product_url)
+ notification_form = page.forms[1]
+ notification_form['email'] = self.email
+ page = notification_form.submit()
- self.assertContains(response, 'Password', status_code=200)
- self.assertEquals(
- response.context[0].get('next'),
- reverse('catalogue:detail', args=(self.product.slug,
- self.product.id)))
+ redirect_url = "%s?next=%s" % (reverse('customer:login'), product_url)
+ self.assertRedirects(page, redirect_url)
-class TestASignedInUser(NotificationTestCase):
+class TestASignedInUser(NotificationWebTest):
is_anonymous = False
- is_staff = False
email = 'testuser@oscar.com'
username = 'testuser'
password = 'password'
@@ -297,36 +281,27 @@ def setUp(self):
self.create_product_class()
self.product = self.create_product()
self.notification = ProductNotification.objects.create(
- email=self.email,
+ user=self.user,
product=self.product,
status=ProductNotification.ACTIVE)
def test_can_deactivate_a_notification(self):
self.assertEquals(self.notification.status, ProductNotification.ACTIVE)
+
status_url = reverse('catalogue:notification-set-status',
args=(self.product.slug, self.product.id,
self.notification.id,
ProductNotification.INACTIVE))
- self.client.get(status_url)
+ self.get(status_url)
notification = ProductNotification.objects.get(id=self.notification.id)
self.assertEquals(notification.status, ProductNotification.INACTIVE)
def test_gets_a_404_when_accessing_invalid_url(self):
self.assertEquals(self.notification.status, ProductNotification.ACTIVE)
- response = self.client.get(
- '/products/40-2/notify-me/set-status/1/invalid/')
- self.assertEquals(response.status_code, 404)
-
- def test_can_delete_a_notification(self):
- delete_url = reverse('catalogue:notification-delete',
- args=(self.product.slug, self.product.id,
- self.notification.id))
-
- self.assertEquals(ProductNotification.objects.count(), 1)
-
- response = self.client.post(delete_url, data={'submit': 'submit'},
- follow=True)
- self.assertContains(response, '', status_code=200)
- self.assertEquals(ProductNotification.objects.count(), 0)
+ try:
+ self.get('/products/40-2/notify-me/set-status/1/invalid/')
+ self.fail('expected 404 but did not happen')
+ except AppError:
+ pass

0 comments on commit 9bddf69

Please sign in to comment.