Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Tests, fixtures, split tests between storefront/dashboard, redirect url with inproper slug

Pacu21ee067ce78f4
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 4568601547..3c957efddb 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -127,6 +127,8 @@ def category_index(request, path, category_id):
def collection_index(request, slug, pk):
collection = get_object_or_404(Collection, id=pk)
+ if collection.slug != slug:
+ return HttpResponsePermanentRedirect(collection.get_absolute_url())
products = products_with_details(user=request.user).filter(
collections__id=collection.id).order_by('name')
product_filter = ProductCollectionFilter(
diff --git a/tests/conftest.py b/tests/conftest.py
index f181e79c01..9dc845e2bd 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -56,6 +56,12 @@ def customer_user(db): # pylint: disable=W0613
return User.objects.create_user('[email protected]', 'password')
+def customer_client(client, customer_user):
+ client.login(username=customer_user.email, password='password')
+ return client
+
+
@pytest.fixture
def request_cart(cart, monkeypatch):
# FIXME: Fixtures should not have any side effects
@@ -93,10 +99,8 @@ def admin_user(db):
@pytest.fixture()
-def admin_client(admin_user):
+def admin_client(client, admin_user):
"""Return a Django test client logged in as an admin user."""
- from django.test.client import Client
- client = Client()
client.login(username=admin_user.email, password='password')
return client
diff --git a/tests/dashboard/test_collection.py b/tests/dashboard/test_collection.py
new file mode 100644
index 0000000000..869c637c97
--- /dev/null
+++ b/tests/dashboard/test_collection.py
@@ -0,0 +1,78 @@
+import pytest
+from django.urls import reverse
+
+from saleor.dashboard.collection.forms import CollectionForm
+from saleor.product.models import Collection
+
+from ..utils import get_redirect_location
+
+
+def test_list_view(admin_client, collection):
+ response = admin_client.get(reverse('dashboard:collection-list'))
+ assert response.status_code == 200
+ context = response.context
+ assert list(context['collections']) == [collection]
+
+
+def test_collection_form_name():
+ data = {'name': 'Test Collection', 'products': []}
+ form = CollectionForm(data)
+ assert form.is_valid()
+
+ collection = form.save()
+ assert collection.slug == 'test-collection'
+
+ invalid_form = CollectionForm()
+ assert not invalid_form.is_valid()
+
+
+def test_collection_form_with_products(product_in_stock):
+ data = {'name': 'Test collection', 'products': [product_in_stock.id]}
+ form = CollectionForm(data)
+ assert form.is_valid()
+
+ collection = form.save()
+ assert collection.products.count() == 1
+
+
+def test_collection_create_view(admin_client):
+ response = admin_client.get(reverse('dashboard:collection-add'))
+ assert response.status_code == 200
+
+ data = {'name': 'Test'}
+ response = admin_client.post(reverse('dashboard:collection-add'), data)
+ assert response.status_code == 302
+
+ redirected_url = get_redirect_location(response)
+ assert redirected_url == reverse('dashboard:collection-list')
+
+
+def test_collection_update_view(admin_client, collection, product_in_stock):
+ url = reverse('dashboard:collection-update', kwargs={'pk': collection.id})
+ response = admin_client.get(url)
+ assert response.status_code == 200
+
+ current_name = collection.name
+ data = {'name': 'New name', 'products': [product_in_stock.id]}
+ response = admin_client.post(url, data)
+ assert response.status_code == 302
+
+ collection.refresh_from_db()
+ assert not current_name == collection.name
+ assert list(collection.products.all()) == [product_in_stock]
+
+
+def test_collection_delete_view(admin_client, collection):
+ # Test Http404 when collection doesn't exist
+ url404 = reverse('dashboard:collection-delete', kwargs={'pk': 123123})
+ response404 = admin_client.post(url404)
+ assert response404.status_code == 404
+
+ # Test deleting object
+ collections_count = Collection.objects.count()
+ url = reverse('dashboard:collection-delete', kwargs={'pk': collection.id})
+ response = admin_client.post(url)
+ assert response.status_code == 302
+ assert Collection.objects.count() == (collections_count - 1)
diff --git a/tests/test_collection.py b/tests/test_collection.py
index 703fce2822..c2d13ae8cb 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -1,98 +1,32 @@
-import pytest
from django.urls import reverse
-from saleor.dashboard.collection.forms import CollectionForm
-from saleor.product.models import Collection
-
from .utils import get_redirect_location
-def collection(db):
- collection = Collection.objects.create(
- name="Collection", slug='collection')
- return collection
-
-
-def test_list_view(admin_client, collection):
- response = admin_client.get(reverse('dashboard:collection-list'))
+def test_collection_index(customer_client, collection):
+ url_kwargs = {'pk': collection.id, 'slug': collection.slug}
+ url = reverse('product:collection', kwargs=url_kwargs)
+ response = customer_client.get(url)
assert response.status_code == 200
- context = response.context
- assert list(context['collections']) == [collection]
-
-
-def test_collection_form_name():
- data = {'name': 'Test Collection', 'products': []}
- form = CollectionForm(data)
- assert form.is_valid()
-
- collection = form.save()
- assert collection.slug == 'test-collection'
- invalid_form = CollectionForm()
- assert not invalid_form.is_valid()
-
-
-def test_collection_form_with_products(product_in_stock):
- data = {'name': 'Test collection',
- 'products': [product_in_stock.id]}
- form = CollectionForm(data)
- assert form.is_valid()
-
- collection = form.save()
- assert collection.products.count() == 1
-
-
-def test_collection_create_view(admin_client):
- response = admin_client.get(reverse('dashboard:collection-add'))
- assert response.status_code == 200
- data = {'name': 'Test'}
- response = admin_client.post(reverse('dashboard:collection-add'), data)
- assert response.status_code == 302
+def test_collection_incorrect_slug(customer_client, collection):
+ """When entered on the collection with proper PK but incorrect slug,
+ one should be permanently(301) redirected to the proper url.
+ """
+ url_kwargs = {'pk': collection.id, 'slug': 'incorrect-slug'}
+ url = reverse('product:collection', kwargs=url_kwargs)
+ response = customer_client.get(url)
+ # User should be redirected to the proper url
+ assert response.status_code == 301
redirected_url = get_redirect_location(response)
- assert redirected_url == reverse('dashboard:collection-list')
-
-
-def test_collection_update_view(admin_client, collection, product_in_stock):
- url = reverse('dashboard:collection-update',
- kwargs={'pk': collection.id})
- response = admin_client.get(url)
- assert response.status_code == 200
-
- current_name = collection.name
- data = {'name': 'New name', 'products': [product_in_stock.id]}
- response = admin_client.post(url, data)
- assert response.status_code == 302
-
- collection.refresh_from_db()
- assert not current_name == collection.name
- assert list(collection.products.all()) == [product_in_stock]
-
-
-def test_collection_delete_view(admin_client, collection):
- # Test Http404 when collection doesn't exist
- pk = 500
- url404 = reverse('dashboard:collection-delete',
- kwargs={'pk': pk})
- response404 = admin_client.post(url404)
- assert response404.status_code == 404
-
- # Test deleting object
- collections_count = Collection.objects.count()
- url = reverse('dashboard:collection-delete',
- kwargs={'pk': collection.id})
- response = admin_client.post(url)
- assert response.status_code == 302
- assert Collection.objects.count() == (collections_count - 1)
-
-
-def test_collection_index_in_storefront(admin_client, collection):
- response = admin_client.get(
- reverse(
- 'product:collection', kwargs={
- 'pk': collection.id, 'slug': collection.slug}))
- assert response.status_code == 200
+ proper_kwargs = {'pk': collection.id, 'slug': collection.slug}
+ proper_url = reverse('product:collection', kwargs=proper_kwargs)
+ assert redirected_url == proper_url
+
+def test_collection_not_exists(customer_client):
+ url_kwargs = {'pk': 123456, 'slug': 'incorrect-slug'}
+ url = reverse('product:collection', kwargs=url_kwargs)
+ response = customer_client.get(url)
+ assert response.status_code == 404