From 2ce2205bd3b2ca88146c2bf82b78508f9b702e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Fri, 29 Mar 2019 17:06:53 +0100 Subject: [PATCH] Adding more tests, finding more errors. --- src/shop/tests.py | 29 +++++++++++++---------------- src/shop/views.py | 28 ++++++++++++++++------------ 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/shop/tests.py b/src/shop/tests.py index 7a994ece..b622f908 100644 --- a/src/shop/tests.py +++ b/src/shop/tests.py @@ -109,12 +109,11 @@ class TestProductDetailView(TestCase): def setUp(self): self.user = UserFactory() self.product = ProductFactory() + self.path = reverse("shop:product_detail", kwargs={"slug": self.product.slug}) def test_product_is_available(self): self.client.force_login(self.user) - response = self.client.get( - reverse("shop:product_detail", kwargs={"slug": self.product.slug}) - ) + response = self.client.get(self.path) self.assertContains(response, "Add to order") self.assertEqual(response.status_code, 200) @@ -126,9 +125,7 @@ class TestProductDetailView(TestCase): OrderProductRelationFactory(product=self.product, quantity=1, order__open=None) self.client.force_login(self.user) - response = self.client.get( - reverse("shop:product_detail", kwargs={"slug": self.product.slug}) - ) + response = self.client.get(self.path) self.assertContains(response, "1 available") self.assertEqual(response.status_code, 200) @@ -140,9 +137,7 @@ class TestProductDetailView(TestCase): OrderProductRelationFactory(product=self.product, quantity=1, order__open=None) self.client.force_login(self.user) - response = self.client.get( - reverse("shop:product_detail", kwargs={"slug": self.product.slug}) - ) + response = self.client.get(self.path) self.assertContains(response, "Sold out.") self.assertEqual(response.status_code, 200) @@ -150,8 +145,7 @@ class TestProductDetailView(TestCase): def test_adding_product_to_new_order(self): self.client.force_login(self.user) - path = reverse("shop:product_detail", kwargs={"slug": self.product.slug}) - response = self.client.post(path, data={'quantity': 1}) + response = self.client.post(self.path, data={'quantity': 1}) order = self.user.orders.get() @@ -162,9 +156,7 @@ class TestProductDetailView(TestCase): OrderProductRelationFactory(product=self.product, quantity=1, order__open=True, order__user=self.user) self.client.force_login(self.user) - response = self.client.get( - reverse("shop:product_detail", kwargs={"slug": self.product.slug}) - ) + response = self.client.get(self.path) self.assertContains(response, "Update order") @@ -177,9 +169,14 @@ class TestProductDetailView(TestCase): self.client.force_login(self.user) - path = reverse("shop:product_detail", kwargs={"slug": self.product.slug}) - response = self.client.post(path, data={'quantity': 2}) + response = self.client.post(self.path, data={'quantity': 2}) self.assertRedirects(response, reverse('shop:order_detail', kwargs={"pk": opr.order.pk})) opr.refresh_from_db() self.assertEquals(opr.quantity, 2) + + def test_product_category_not_public(self): + self.product.category.public = False + self.product.category.save() + response = self.client.get(self.path) + self.assertEquals(response.status_code, 404) diff --git a/src/shop/views.py b/src/shop/views.py index f94378d2..d8958d05 100644 --- a/src/shop/views.py +++ b/src/shop/views.py @@ -228,7 +228,9 @@ class ProductDetailView(FormView, DetailView): return kwargs def get_initial(self): - return {'quantity': self.opr.quantity} + if self.opr: + return {'quantity': self.opr.quantity} + return super().get_initial() def get_context_data(self, **kwargs): if hasattr(self.opr, 'order'): @@ -239,17 +241,19 @@ class ProductDetailView(FormView, DetailView): def dispatch(self, request, *args, **kwargs): self.object = self.get_object() - try: - self.opr = OrderProductRelation.objects.get( - order__user=self.request.user, - order__open__isnull=False, - product=self.object - ) - except OrderProductRelation.DoesNotExist: - self.opr = OrderProductRelation( - product=self.get_object(), - quantity=1, - ) + self.opr = None + if self.request.user.is_authenticated: + try: + self.opr = OrderProductRelation.objects.get( + order__user=self.request.user, + order__open__isnull=False, + product=self.object + ) + except OrderProductRelation.DoesNotExist: + self.opr = OrderProductRelation( + product=self.get_object(), + quantity=1, + ) if not self.object.category.public: # this product is not publicly available