From 931c1ac84f06620d58915fcb019119a049292b22 Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Sat, 21 May 2022 08:27:49 -0600 Subject: [PATCH] Add tests to storefront views --- src/accounts/models.py | 2 + src/core/fixtures/orders.json | 6 +- src/core/tests.py | 3 - src/core/tests/__init__.py | 0 src/core/tests/test_models.py | 37 ++ src/core/tests/test_views.py | 0 ...t_cancelled.html => payment_canceled.html} | 0 src/storefront/tests/test_views.py | 378 +++++++++++++++++- src/storefront/urls.py | 94 ++++- src/storefront/views.py | 2 +- 10 files changed, 488 insertions(+), 34 deletions(-) delete mode 100644 src/core/tests.py create mode 100644 src/core/tests/__init__.py create mode 100644 src/core/tests/test_models.py create mode 100644 src/core/tests/test_views.py rename src/storefront/templates/storefront/{payment_cancelled.html => payment_canceled.html} (100%) diff --git a/src/accounts/models.py b/src/accounts/models.py index 2424387..2b72af9 100644 --- a/src/accounts/models.py +++ b/src/accounts/models.py @@ -4,6 +4,7 @@ from django.contrib.auth.models import AbstractUser from . import STATE_CHOICES + class Address(models.Model): first_name = models.CharField(max_length=256, blank=True) last_name = models.CharField(max_length=256, blank=True) @@ -20,6 +21,7 @@ class Address(models.Model): def __str__(self): return f'{self.street_address_1} — {self.city}' + class User(AbstractUser): addresses = models.ManyToManyField( Address, blank=True, related_name="user_addresses" diff --git a/src/core/fixtures/orders.json b/src/core/fixtures/orders.json index 7b47286..f0c17c2 100644 --- a/src/core/fixtures/orders.json +++ b/src/core/fixtures/orders.json @@ -3,7 +3,7 @@ "model": "core.order", "pk": 1, "fields": { - "customer": null, + "customer": 1, "status": "unfulfilled", "billing_address": null, "shipping_address": 1, @@ -19,7 +19,7 @@ "model": "core.order", "pk": 2, "fields": { - "customer": null, + "customer": 1, "status": "unfulfilled", "billing_address": null, "shipping_address": 1, @@ -35,7 +35,7 @@ "model": "core.order", "pk": 3, "fields": { - "customer": null, + "customer": 2, "status": "unfulfilled", "billing_address": null, "shipping_address": 1, diff --git a/src/core/tests.py b/src/core/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/src/core/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/src/core/tests/__init__.py b/src/core/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/core/tests/test_models.py b/src/core/tests/test_models.py new file mode 100644 index 0000000..009faec --- /dev/null +++ b/src/core/tests/test_models.py @@ -0,0 +1,37 @@ +from decimal import Decimal +from django.test import TestCase + +from measurement.measures import Weight + +from core.models import ( + Product, + ProductPhoto, + Coupon, + ShippingMethod, + Order, + Transaction, + OrderLine, + TrackingNumber, +) + + +class ProductModelTest(TestCase): + @classmethod + def setUpTestData(cls): + Product.objects.create( + name='Pantomime', + subtitle='Very Dark French Roast', + description='Our darkest drip. A blend of five different beans roasted two ways. Organic Africa, Indonesia, and South and Central America.', + sku='565656', + price=Decimal('15.00'), + weight=Weight(oz=16), + visible_in_listings=True, + sorting=1, + ) + + def test_get_absolute_url(self): + product = Product.objects.get(pk=1) + self.assertEqual( + product.get_absolute_url(), + '/dashboard/products/1/' + ) diff --git a/src/core/tests/test_views.py b/src/core/tests/test_views.py new file mode 100644 index 0000000..e69de29 diff --git a/src/storefront/templates/storefront/payment_cancelled.html b/src/storefront/templates/storefront/payment_canceled.html similarity index 100% rename from src/storefront/templates/storefront/payment_cancelled.html rename to src/storefront/templates/storefront/payment_canceled.html diff --git a/src/storefront/tests/test_views.py b/src/storefront/tests/test_views.py index 14e5755..1b29480 100644 --- a/src/storefront/tests/test_views.py +++ b/src/storefront/tests/test_views.py @@ -12,13 +12,42 @@ from accounts.models import User, Address from core.models import Product, Order, Coupon from core import CoffeeGrind from storefront.forms import AddressForm, OrderCreateForm -from storefront.views import OrderCreateView, CheckoutAddressView +from storefront.views import ( + CartView, CartAddProductView, CartUpdateProductView, CouponApplyView, + ProductListView, ProductDetailView, + CheckoutAddressView, OrderCreateView, + paypal_order_transaction_capture, paypal_webhook_endpoint, + PaymentDoneView, PaymentCanceledView, + CustomerDetailView, CustomerUpdateView, OrderDetailView, + CustomerAddressCreateView, CustomerAddressUpdateView, + AboutView, FairTradeView, ReviewListView, ContactFormView, +) from storefront.cart import Cart logger = logging.getLogger(__name__) -class CheckoutAddressViewTests(TestCase): +class CartViewTest(TestCase): + fixtures = ['products.json'] + + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/cart/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:cart-detail')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:cart-detail')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/cart_detail.html') + + +class CheckoutAddressViewTest(TestCase): def setUp(self): self.client = Client() @@ -33,7 +62,7 @@ class CheckoutAddressViewTests(TestCase): self.assertTrue(isinstance(response.context['form'], AddressForm)) -class OrderCreateViewTests(TestCase): +class OrderCreateViewTest(TestCase): fixtures = ['accounts.json', 'coupons.json'] @classmethod @@ -54,8 +83,6 @@ class OrderCreateViewTests(TestCase): def setUp(self): self.client = Client() - - def test_used_coupon_creates_error_on_checkout(self): session = self.client.session session['shipping_address'] = { 'first_name': 'Nathan', @@ -67,6 +94,27 @@ class OrderCreateViewTests(TestCase): 'state': 'UT', 'postal_code': '84341' } + session.save() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/checkout/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get( + reverse('storefront:order-create') + ) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get( + reverse('storefront:order-create') + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/order_form.html') + + def test_used_coupon_creates_error_on_checkout(self): + session = self.client.session session['coupon_code'] = 'MAY2022' session.save() @@ -76,3 +124,323 @@ class OrderCreateViewTests(TestCase): self.assertTrue(self.client.session.get('shipping_address')) self.assertTemplateUsed(response, 'storefront/order_form.html') self.assertContains(response, 'Coupon already used', status_code=200) + + +class ProductListViewTest(TestCase): + fixtures = ['products.json'] + + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:product-list')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:product-list')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/product_list.html') + + +class ProductDetailViewTest(TestCase): + fixtures = ['products.json'] + + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/products/1/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get( + reverse('storefront:product-detail', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get( + reverse('storefront:product-detail', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/product_detail.html') + + +class CheckoutAddressViewTest(TestCase): + fixtures = ['products.json'] + + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/checkout/address/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:checkout-address')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:checkout-address')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/checkout_address.html') + + +class OrderCreateViewTest(TestCase): + fixtures = ['products.json'] + + def setUp(self): + self.client = Client() + + +class PaymentDoneViewTest(TestCase): + fixtures = ['products.json'] + + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/done/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:payment-done')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:payment-done')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/payment_done.html') + + +class PaymentCanceledViewTest(TestCase): + fixtures = ['products.json'] + + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/canceled/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:payment-canceled')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:payment-canceled')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/payment_canceled.html') + + +class CustomerDetailViewTest(TestCase): + fixtures = ['products.json', 'accounts.json'] + + def setUp(self): + self.client = Client() + self.user = User.objects.get(pk=1) + self.client.force_login(self.user) + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/customers/1/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get( + reverse('storefront:customer-detail', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get( + reverse('storefront:customer-detail', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/customer_detail.html') + + +class CustomerUpdateViewTest(TestCase): + fixtures = ['products.json', 'accounts.json'] + + def setUp(self): + self.client = Client() + self.user = User.objects.get(pk=1) + self.client.force_login(self.user) + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/customers/1/update/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get( + reverse('storefront:customer-update', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get( + reverse('storefront:customer-update', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/customer_form.html') + + +class OrderDetailViewTest(TestCase): + fixtures = ['products.json', 'accounts.json', 'orders.json'] + + def setUp(self): + self.client = Client() + self.user = User.objects.get(pk=1) + self.client.force_login(self.user) + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/customers/1/orders/1/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get( + reverse('storefront:order-detail', kwargs={'pk': 1, 'order_pk': 1}) + ) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get( + reverse('storefront:order-detail', kwargs={'pk': 1, 'order_pk': 1}) + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/order_detail.html') + + +class CustomerAddressCreateViewTest(TestCase): + fixtures = ['products.json', 'accounts.json'] + + def setUp(self): + self.client = Client() + self.user = User.objects.get(pk=1) + self.client.force_login(self.user) + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/customers/1/addresses/new/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get( + reverse('storefront:customer-address-create', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get( + reverse('storefront:customer-address-create', kwargs={'pk': 1}) + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed( + response, 'storefront/address_create_form.html' + ) + + +class CustomerAddressUpdateViewTest(TestCase): + fixtures = ['products.json', 'accounts.json'] + + def setUp(self): + self.client = Client() + self.user = User.objects.get(pk=1) + self.client.force_login(self.user) + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/customers/1/addresses/1/update/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get( + reverse('storefront:address-update', kwargs={ + 'pk': 1, 'address_pk': 1 + }) + ) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get( + reverse('storefront:address-update', kwargs={ + 'pk': 1, 'address_pk': 1} + ) + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/address_form.html') + + +class AboutViewTest(TestCase): + + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/about/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:about')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:about')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/about.html') + + +class FairTradeViewTest(TestCase): + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/fair-trade/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:fair-trade')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:fair-trade')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/fairtrade.html') + + +class ReviewListViewTest(TestCase): + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/reviews/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:reviews')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:reviews')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/reviews.html') + + +class ContactFormViewTest(TestCase): + def setUp(self): + self.client = Client() + + def test_view_url_exists_at_desired_location(self): + response = self.client.get('/contact/') + self.assertEqual(response.status_code, 200) + + def test_view_url_accessible_by_name(self): + response = self.client.get(reverse('storefront:contact')) + self.assertEqual(response.status_code, 200) + + def test_view_uses_correct_template(self): + response = self.client.get(reverse('storefront:contact')) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'storefront/contact_form.html') diff --git a/src/storefront/urls.py b/src/storefront/urls.py index b99c650..313dca0 100644 --- a/src/storefront/urls.py +++ b/src/storefront/urls.py @@ -11,30 +11,80 @@ urlpatterns = [ path('products//', include([ path('', views.ProductDetailView.as_view(), name='product-detail'), ])), - + path('cart/', views.CartView.as_view(), name='cart-detail'), - path('cart//add/', views.CartAddProductView.as_view(), name='cart-add'), - path('cart//update//', views.CartUpdateProductView.as_view(), name='cart-update'), - path('cart//remove//', views.cart_remove_product_view, name='cart-remove'), - - path('coupon/apply/', views.CouponApplyView.as_view(), name='coupon-apply'), - - path('paypal/order//capture/', views.paypal_order_transaction_capture, name='paypal-capture'), - path('paypal/webhooks/', views.paypal_webhook_endpoint, name='paypal-webhook'), - - path('checkout/address/', views.CheckoutAddressView.as_view(), name='checkout-address'), + path( + 'cart//add/', + views.CartAddProductView.as_view(), + name='cart-add' + ), + path( + 'cart//update//', + views.CartUpdateProductView.as_view(), + name='cart-update', + ), + path( + 'cart//remove//', + views.cart_remove_product_view, + name='cart-remove', + ), + path( + 'coupon/apply/', + views.CouponApplyView.as_view(), + name='coupon-apply' + ), + path( + 'paypal/order//capture/', + views.paypal_order_transaction_capture, + name='paypal-capture', + ), + path( + 'paypal/webhooks/', + views.paypal_webhook_endpoint, + name='paypal-webhook' + ), + path( + 'checkout/address/', + views.CheckoutAddressView.as_view(), + name='checkout-address', + ), path('checkout/', views.OrderCreateView.as_view(), name='order-create'), path('done/', views.PaymentDoneView.as_view(), name='payment-done'), - path('canceled/', views.PaymentCanceledView.as_view(), name='payment-canceled'), - + path( + 'canceled/', + views.PaymentCanceledView.as_view(), + name='payment-canceled' + ), path('customers//', include([ - path('', views.CustomerDetailView.as_view(), name='customer-detail'), - path('update/', views.CustomerUpdateView.as_view(), name='customer-update'), - # path('delete/', views.CustomerDeleteView.as_view(), name='customer-delete'), - - path('orders//', views.OrderDetailView.as_view(), name='order-detail'), - path('addresses/new/', views.CustomerAddressCreateView.as_view(), name='customer-address-create'), - path('addresses//update/', views.CustomerAddressUpdateView.as_view(), name='address-update'), - ])), - + path( + '', + views.CustomerDetailView.as_view(), + name='customer-detail' + ), + path( + 'update/', + views.CustomerUpdateView.as_view(), + name='customer-update', + ), + # path( + # 'delete/', + # views.CustomerDeleteView.as_view(), + # name='customer-delete' + # ), + path( + 'orders//', + views.OrderDetailView.as_view(), + name='order-detail', + ), + path( + 'addresses/new/', + views.CustomerAddressCreateView.as_view(), + name='customer-address-create', + ), + path( + 'addresses//update/', + views.CustomerAddressUpdateView.as_view(), + name='address-update', + ) + ])) ] diff --git a/src/storefront/views.py b/src/storefront/views.py index 62aba88..a8e4c4f 100644 --- a/src/storefront/views.py +++ b/src/storefront/views.py @@ -422,7 +422,7 @@ class CustomerAddressUpdateView( raise_exception = True def test_func(self): - return self.request.user.pk == self.get_object().customer.pk + return self.request.user.pk == self.kwargs['pk'] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs)