Skip to content

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

Remove tests for storefront views and forms

IKarbowiak791e5cd4d637
diff --git a/.isort.cfg b/.isort.cfg
index 3239be2d5e..0673165885 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,bleach,bootstrap4,braintree,bs4,captcha,celery,dj_database_url,dj_email_url,django,django_babel,django_cache_url,django_countries,django_elasticsearch_dsl,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,elasticsearch_dsl,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,markdown,measurement,mptt,oauthlib,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,social_core,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
+known_third_party = PIL,babel,bleach,bootstrap4,braintree,celery,dj_database_url,dj_email_url,django,django_babel,django_cache_url,django_countries,django_elasticsearch_dsl,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,elasticsearch_dsl,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphene_federation,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,markdown,measurement,mptt,oauthlib,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,social_core,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield
diff --git a/tests/api/test_middleware.py b/tests/api/test_middleware.py
index e605ef55fa..8c965c746d 100644
--- a/tests/api/test_middleware.py
+++ b/tests/api/test_middleware.py
@@ -1,6 +1,5 @@
from unittest.mock import Mock
-import pytest
from django.urls import reverse
from saleor.graphql.middleware import service_account_middleware
@@ -17,19 +16,3 @@ def test_service_account_middleware_accepts_api_requests(service_account, rf):
middleware(request)
assert request.service_account == service_account
-
-
[email protected]("path", ["account:details", "home"])
-def test_service_account_middleware_block(service_account, path, rf):
-
- # Retrieve sample request object
- request = rf.get(reverse("api"))
-
- request.path = reverse(path)
- token = service_account.tokens.first().auth_token
- request.META = {"HTTP_AUTHORIZATION": f"Bearer {token}"}
-
- middleware = service_account_middleware(Mock())
- middleware(request)
-
- assert not hasattr(request, "service_account")
diff --git a/tests/extensions/plugins/test_vatlayer.py b/tests/extensions/plugins/test_vatlayer.py
index 5f4f15aa71..afe8383ca4 100644
--- a/tests/extensions/plugins/test_vatlayer.py
+++ b/tests/extensions/plugins/test_vatlayer.py
@@ -4,7 +4,6 @@ from urllib.parse import urlparse
import pytest
from django.core.exceptions import ValidationError
-from django.urls import reverse
from django_countries.fields import Country
from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
@@ -65,42 +64,6 @@ def test_get_tax_rate_by_name_empty_taxes(product):
assert tax_rate == 0
-def test_view_checkout_with_taxes(
- settings, client, request_checkout_with_item, vatlayer, address
-):
- settings.DEFAULT_COUNTRY = "PL"
- checkout = request_checkout_with_item
- checkout.shipping_address = address
- checkout.save()
- product = checkout.lines.first().variant.product
- product.meta = {"taxes": {"vatlayer": {"code": "standard", "description": ""}}}
- product.save()
- response = client.get(reverse("checkout:index"))
- response_checkout_line = response.context[0]["checkout_lines"][0]
- line_net = Money(amount="8.13", currency="USD")
- line_gross = Money(amount="10.00", currency="USD")
-
- assert response_checkout_line["get_total"].tax.amount
- assert response_checkout_line["get_total"] == TaxedMoney(line_net, line_gross)
- assert response.status_code == 200
-
-
-def test_view_update_checkout_quantity_with_taxes(
- client, request_checkout_with_item, vatlayer, monkeypatch
-):
- monkeypatch.setattr(
- "saleor.checkout.views.to_local_currency", lambda price, currency: price
- )
- variant = request_checkout_with_item.lines.get().variant
- response = client.post(
- reverse("checkout:update-line", kwargs={"variant_id": variant.id}),
- {"quantity": 3},
- HTTP_X_REQUESTED_WITH="XMLHttpRequest",
- )
- assert response.status_code == 200
- assert request_checkout_with_item.quantity == 3
-
-
@pytest.mark.parametrize(
"price, charge_taxes, expected_price",
[
diff --git a/tests/test_account.py b/tests/test_account.py
index bba9966e86..9cc1b89618 100644
--- a/tests/test_account.py
+++ b/tests/test_account.py
@@ -1,27 +1,15 @@
import re
-from unittest.mock import patch
from urllib.parse import urlencode
import i18naddress
import pytest
-from captcha import constants as recaptcha_constants
-from captcha.client import RecaptchaResponse
-from django.contrib.auth.tokens import default_token_generator
-from django.core import mail
from django.core.exceptions import ValidationError
from django.core.files import File
-from django.forms import Form
from django.http import QueryDict
from django.template import Context, Template
-from django.templatetags.static import static
-from django.urls import reverse
-from django.utils.encoding import force_bytes
-from django.utils.http import urlsafe_base64_encode
from django_countries.fields import Country
-from templated_email import send_templated_mail
from saleor.account import forms, i18n
-from saleor.account.forms import FormWithReCaptcha, NameForm
from saleor.account.models import User
from saleor.account.templatetags.i18n_address_tags import format_address
from saleor.account.utils import (
@@ -31,7 +19,6 @@ from saleor.account.utils import (
remove_staff_member,
)
from saleor.account.validators import validate_possible_number
-from saleor.core.utils import build_absolute_uri
@pytest.mark.parametrize("country", ["CN", "PL", "US", "IE"])
@@ -180,19 +167,6 @@ def test_validate_possible_number(input_data, is_valid):
validate_possible_number(**input_data)
-def test_order_with_lines_pagination(authorized_client, order_list, settings):
- settings.PAGINATE_BY = 1
- data = {"page": "1"}
- url = reverse("account:details")
- response = authorized_client.get(url, data)
- assert response.status_code == 200
-
- data = {"page": "2"}
- url = reverse("account:details")
- response = authorized_client.get(url, data)
- assert response.status_code == 200
-
-
def test_format_address(address):
formatted_address = format_address(address)
address_html = "<br>".join(map(str, formatted_address["address_lines"]))
@@ -353,107 +327,6 @@ def test_get_random_avatar():
assert re.match(r"avatar\d+.png", avatar.name)
-def test_disabled_recaptcha():
- """
- This test creates a new form that should not contain any recaptcha field.
- """
-
- class TestForm(Form, FormWithReCaptcha):
- pass
-
- form = TestForm({})
- assert form.is_valid()
-
-
-@patch("captcha.fields.client.submit")
-def test_requires_recaptcha(captcha_submit_mock, settings):
- """
- This test creates a new form
- that should contain a (required) recaptcha field.
- """
- captcha_submit_mock.return_value = RecaptchaResponse(is_valid=True)
-
- settings.RECAPTCHA_PUBLIC_KEY = recaptcha_constants.TEST_PUBLIC_KEY
- settings.RECAPTCHA_PRIVATE_KEY = recaptcha_constants.TEST_PRIVATE_KEY
-
- class TestForm(Form, FormWithReCaptcha):
- pass
-
- form = TestForm({})
- assert not form.is_valid()
-
- form = TestForm({"g-recaptcha-response": "PASSED"})
- assert form.is_valid()
-
-
-def test_view_account_delete_login_required(customer_user, client):
- url = reverse("account:delete")
- response = client.post(url)
- assert response.status_code == 302
-
-
-def test_view_account_post_required(customer_user, authorized_client):
- url = reverse("account:delete")
- response = authorized_client.get(url)
- assert response.status_code == 405
-
-
-@patch("saleor.account.views.send_account_delete_confirmation_email")
-def test_view_account_delete(
- send_confirmation_mock, customer_user, authorized_client, staff_user
-):
- url = reverse("account:delete")
- response = authorized_client.post(url)
- assert response.status_code == 302
- send_confirmation_mock.assert_called_once_with(customer_user)
-
-
-def test_view_account_delete_confirm(customer_user, staff_user, authorized_client):
- # Non existing token
- invalid_token = default_token_generator.make_token(staff_user)
- url = reverse("account:delete-confirm", args=[invalid_token])
- response = authorized_client.get(url)
- assert response.status_code == 404
-
- authorized_client.force_login(customer_user)
- token = default_token_generator.make_token(customer_user)
- deletion_url = reverse("account:delete-confirm", args=[token])
-
- # getting the page should not delete the user
- response = authorized_client.get(deletion_url)
- assert response.status_code == 200
- customer_user = User.objects.filter(pk=customer_user.pk).first()
- assert customer_user is not None
-
- # posting onto the page should delete the user
- response = authorized_client.post(deletion_url)
- assert response.status_code == 302
- customer_user = User.objects.filter(pk=customer_user.pk).first()
- assert customer_user is None
-
-
-def test_form_add_names_to_user(customer_user):
- name_form = NameForm(
- {"first_name": "Jan", "last_name": "Nowak"}, instance=customer_user
- )
- name_form.is_valid()
- name_form.save()
- updated_user = User.objects.get(pk=customer_user.pk)
- assert updated_user.first_name == "Jan"
- assert updated_user.last_name == "Nowak"
-
-
-def test_view_add_names_to_user(customer_user, authorized_client):
- url = reverse("account:details")
- response = authorized_client.post(
- url, data={"first_name": "Jan", "last_name": "Nowak"}
- )
- assert response.status_code == 200
- updated_user = User.objects.get(pk=customer_user.pk)
- assert updated_user.first_name == "Jan"
- assert updated_user.last_name == "Nowak"
-
-
def test_remove_staff_member_with_orders(staff_user, permission_manage_products, order):
order.user = staff_user
order.save()
@@ -468,29 +341,3 @@ def test_remove_staff_member_with_orders(staff_user, permission_manage_products,
def test_remove_staff_member(staff_user):
remove_staff_member(staff_user)
assert not User.objects.filter(pk=staff_user.pk).exists()
-
-
-def test_send_set_password_customer_email(customer_user, site_settings):
- site = site_settings.site
- uid = urlsafe_base64_encode(force_bytes(customer_user.pk))
- token = default_token_generator.make_token(customer_user)
- logo_url = build_absolute_uri(static("images/logo-light.svg"))
- password_set_url = build_absolute_uri(
- reverse(
- "account:reset-password-confirm", kwargs={"token": token, "uidb64": uid}
- )
- )
- ctx = {
- "logo_url": logo_url,
- "password_set_url": password_set_url,
- "site_name": site.name,
- }
- send_templated_mail(
- template_name="dashboard/customer/set_password",
- from_email=site_settings.default_from_email,
- recipient_list=[customer_user.email],
- context=ctx,
- )
- assert len(mail.outbox) == 1
- sended_message = mail.outbox[0].body
- assert password_set_url in sended_message
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 616c96dc11..cdae427ee5 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -3,7 +3,6 @@ from unittest.mock import Mock, patch
import pytest
from django.contrib.auth.models import AnonymousUser
-from django.urls import reverse
from django.utils import timezone
from django_countries.fields import Country
from freezegun import freeze_time
@@ -12,7 +11,7 @@ from prices import Money, TaxedMoney, TaxedMoneyRange
from saleor.account import CustomerEvents
from saleor.account.models import Address, CustomerEvent, User
from saleor.account.utils import store_user_address
-from saleor.checkout import AddressType, calculations, views
+from saleor.checkout import AddressType, calculations
from saleor.checkout.forms import CheckoutVoucherForm, CountryForm
from saleor.checkout.models import Checkout
from saleor.checkout.utils import (
@@ -40,8 +39,6 @@ from saleor.order import OrderEvents, OrderEventsEmails
from saleor.order.models import OrderEvent
from saleor.shipping.models import ShippingZone
-from .utils import get_redirect_location
-
def test_country_form_country_choices():
form = CountryForm(data={"csrf": "", "country": "PL"})
@@ -81,527 +78,6 @@ def test_clear_shipping_method(checkout, shipping_method):
assert not checkout.shipping_method
- "checkout_length, is_shipping_required, redirect_url",
- [
- (0, True, reverse("checkout:index")),
- (0, False, reverse("checkout:index")),
- (1, True, reverse("checkout:shipping-address")),
- (1, False, reverse("checkout:summary")),
- ],
-)
-def test_view_checkout_index(
- monkeypatch, rf, checkout_length, is_shipping_required, redirect_url
-):
- checkout = Mock(
- __len__=Mock(return_value=checkout_length),
- is_shipping_required=Mock(return_value=is_shipping_required),
- )
- monkeypatch.setattr(
- "saleor.checkout.utils.get_checkout_from_request", lambda req, qs: checkout
- )
- url = reverse("checkout:start")
- request = rf.get(url, follow=True)
-
- response = views.checkout_start(request)
-
- assert response.url == redirect_url
-
-
-def test_view_checkout_index_authorized_user(
- authorized_client, customer_user, request_checkout_with_item
-):
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.save()
- url = reverse("checkout:start")
-
- response = authorized_client.get(url, follow=True)
-
- redirect_url = reverse("checkout:shipping-address")
- assert response.request["PATH_INFO"] == redirect_url
-
-
-def test_view_checkout_shipping_address(client, request_checkout_with_item):
- url = reverse("checkout:shipping-address")
- data = {
- "email": "[email protected]",
- "first_name": "John",
- "last_name": "Doe",
- "street_address_1": "Aleje Jerozolimskie 2",
- "street_address_2": "",
- "city": "Warszawa",
- "city_area": "",
- "country_area": "",
- "postal_code": "00-374",
- "phone": "+48536984008",
- "country": "PL",
- }
-
- response = client.get(url)
-
- assert response.request["PATH_INFO"] == url
-
- response = client.post(url, data, follow=True)
-
- redirect_url = reverse("checkout:shipping-method")
- assert response.request["PATH_INFO"] == redirect_url
- assert request_checkout_with_item.email == "[email protected]"
-
-
-def test_view_checkout_shipping_address_with_invalid_data(
- client, request_checkout_with_item
-):
- url = reverse("checkout:shipping-address")
- data = {
- "email": "[email protected]",
- "first_name": "John",
- "last_name": "Doe",
- "street_address_1": "Aleje Jerozolimskie 2",
- "street_address_2": "",
- "city": "Warszawa",
- "city_area": "",
- "country_area": "",
- "postal_code": "00-37412",
- "phone": "+48536984008",
- "country": "PL",
- }
-
- response = client.post(url, data, follow=True)
- assert response.request["PATH_INFO"] == url
-
-
-def test_view_checkout_shipping_address_authorized_user(
- authorized_client, customer_user, request_checkout_with_item
-):
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.save()
- url = reverse("checkout:shipping-address")
- data = {"address": customer_user.default_billing_address.pk}
-
- response = authorized_client.post(url, data, follow=True)
-
- redirect_url = reverse("checkout:shipping-method")
- assert response.request["PATH_INFO"] == redirect_url
- assert request_checkout_with_item.email == customer_user.email
-
-
-def test_view_checkout_shipping_address_without_shipping(
- request_checkout, product_without_shipping, client
-):
- variant = product_without_shipping.variants.get()
- add_variant_to_checkout(request_checkout, variant)
- url = reverse("checkout:shipping-address")
-
- response = client.get(url)
-
- assert response.status_code == 302
- assert get_redirect_location(response) == reverse("checkout:summary")
- assert not request_checkout.email
-
-
-def test_view_checkout_shipping_method(
- client, shipping_zone, address, request_checkout_with_item
-):
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.email = "[email protected]"
- request_checkout_with_item.save()
- url = reverse("checkout:shipping-method")
- data = {"shipping_method": shipping_zone.shipping_methods.first().pk}
-
- response = client.get(url)
-
- assert response.request["PATH_INFO"] == url
-
- response = client.post(url, data, follow=True)
-
- redirect_url = reverse("checkout:summary")
- assert response.request["PATH_INFO"] == redirect_url
-
-
-def test_view_checkout_shipping_method_authorized_user(
- authorized_client, customer_user, shipping_zone, address, request_checkout_with_item
-):
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.email = customer_user.email
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.save()
- url = reverse("checkout:shipping-method")
- data = {"shipping_method": shipping_zone.shipping_methods.first().pk}
-
- response = authorized_client.get(url)
-
- assert response.request["PATH_INFO"] == url
-
- response = authorized_client.post(url, data, follow=True)
-
- redirect_url = reverse("checkout:summary")
- assert response.request["PATH_INFO"] == redirect_url
-
-
-def test_view_checkout_shipping_method_without_shipping(
- request_checkout, product_without_shipping, client
-):
- variant = product_without_shipping.variants.get()
- add_variant_to_checkout(request_checkout, variant)
- url = reverse("checkout:shipping-method")
-
- response = client.get(url)
-
- assert response.status_code == 302
- assert get_redirect_location(response) == reverse("checkout:summary")
-
-
-def test_view_checkout_shipping_method_without_address(
- request_checkout_with_item, client
-):
- url = reverse("checkout:shipping-method")
-
- response = client.get(url)
-
- assert response.status_code == 302
- redirect_url = reverse("checkout:shipping-address")
- assert get_redirect_location(response) == redirect_url
-
-
-@patch("saleor.checkout.utils.send_order_confirmation")
-def test_view_checkout_summary_finalize_with_voucher(
- mock_send_confirmation,
- client,
- shipping_zone,
- address,
- request_checkout_with_item,
- voucher,
-):
- checkout = request_checkout_with_item
- expected_voucher_usage_count = voucher.used + 1
-
- checkout.shipping_address = address
- checkout.voucher_code = voucher.code
- checkout.email = "[email protected]"
- checkout.shipping_method = shipping_zone.shipping_methods.first()
- checkout.save()
-
- url = reverse("checkout:summary")
- data = {"address": "shipping_address"}
-
- response = client.get(url)
- assert response.status_code == 200
- assert response.request["PATH_INFO"] == url
-
- response = client.post(url, data, follow=True)
- assert response.status_code == 200
-
- order = response.context["order"]
- assert order.user_email == "[email protected]"
- redirect_url = reverse("order:payment", kwargs={"token": order.token})
- assert response.request["PATH_INFO"] == redirect_url
-
- # we expect the user to be anonymous, thus None
- mock_send_confirmation.delay.assert_called_once_with(order.pk, None)
-
- # checkout should be deleted after order is created
- assert checkout.pk is None
-
- # Ensure the voucher was updated
- voucher.refresh_from_db(fields=["used"])
- assert voucher.used == expected_voucher_usage_count
-
-
-@patch("saleor.checkout.utils.send_order_confirmation")
-def test_view_checkout_summary_anonymous_user(
- mock_send_confirmation, client, shipping_zone, address, request_checkout_with_item
-):
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.email = "[email protected]"
- request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
- request_checkout_with_item.save()
- url = reverse("checkout:summary")
- data = {"address": "shipping_address"}
-
- response = client.get(url)
-
- assert response.request["PATH_INFO"] == url
-
- response = client.post(url, data, follow=True)
-
- order = response.context["order"]
- assert order.user_email == "[email protected]"
- redirect_url = reverse("order:payment", kwargs={"token": order.token})
- assert response.request["PATH_INFO"] == redirect_url
-
- # we expect the user to be anonymous, thus None
- mock_send_confirmation.delay.assert_called_once_with(order.pk, None)
-
- # checkout should be deleted after order is created
- assert request_checkout_with_item.pk is None
-
-
-@patch("saleor.checkout.utils.send_order_confirmation")
-def test_view_checkout_summary_authorized_user(
- mock_send_confirmation,
- authorized_client,
- customer_user,
- shipping_zone,
- address,
- request_checkout_with_item,
-):
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.email = customer_user.email
- request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
- request_checkout_with_item.save()
- url = reverse("checkout:summary")
- data = {"address": "shipping_address"}
-
- response = authorized_client.get(url)
-
- assert response.request["PATH_INFO"] == url
-
- response = authorized_client.post(url, data, follow=True)
-
- order = response.context["order"]
- assert order.user_email == customer_user.email
- redirect_url = reverse("order:payment", kwargs={"token": order.token})
- assert response.request["PATH_INFO"] == redirect_url
- mock_send_confirmation.delay.assert_called_once_with(order.pk, customer_user.pk)
-
-
-@patch("saleor.checkout.utils.send_order_confirmation")
-def test_view_checkout_summary_save_language(
- mock_send_confirmation,
- authorized_client,
- customer_user,
- shipping_zone,
- address,
- request_checkout_with_item,
- settings,
-):
- settings.LANGUAGE_CODE = "en"
- user_language = "fr"
- authorized_client.cookies[settings.LANGUAGE_COOKIE_NAME] = user_language
- url = reverse("set_language")
- data = {"language": "fr"}
-
- authorized_client.post(url, data)
-
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.email = customer_user.email
- request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
- request_checkout_with_item.save()
- url = reverse("checkout:summary")
- data = {"address": "shipping_address"}
-
- response = authorized_client.get(url, HTTP_ACCEPT_LANGUAGE=user_language)
-
- assert response.request["PATH_INFO"] == url
-
- response = authorized_client.post(
- url, data, follow=True, HTTP_ACCEPT_LANGUAGE=user_language
- )
-
- order = response.context["order"]
- assert order.user_email == customer_user.email
- assert order.language_code == user_language
- redirect_url = reverse("order:payment", kwargs={"token": order.token})
- assert response.request["PATH_INFO"] == redirect_url
- mock_send_confirmation.delay.assert_called_once_with(order.pk, customer_user.pk)
-
-
-def test_view_checkout_summary_without_address(request_checkout_with_item, client):
- url = reverse("checkout:summary")
-
- response = client.get(url)
-
- assert response.status_code == 302
- redirect_url = reverse("checkout:shipping-address")
- assert get_redirect_location(response) == redirect_url
-
-
-def test_view_checkout_summary_without_shipping_zone(
- request_checkout_with_item, client, address
-):
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.email = "[email protected]"
- request_checkout_with_item.save()
-
- url = reverse("checkout:summary")
- response = client.get(url)
-
- assert response.status_code == 302
- redirect_url = reverse("checkout:shipping-method")
- assert get_redirect_location(response) == redirect_url
-
-
-def test_view_checkout_summary_with_invalid_voucher(
- client, request_checkout_with_item, shipping_zone, address, voucher
-):
- voucher.usage_limit = 3
- voucher.save()
-
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.email = "[email protected]"
- request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
- request_checkout_with_item.save()
-
- url = reverse("checkout:summary")
- voucher_url = "{url}?next={url}".format(url=url)
- data = {"discount-voucher": voucher.code}
-
- response = client.post(voucher_url, data, follow=True, HTTP_REFERER=url)
-
- assert response.context["checkout"].voucher_code == voucher.code
-
- voucher.used = 3
- voucher.save()
-
- data = {"address": "shipping_address"}
- response = client.post(url, data, follow=True)
- checkout = response.context["checkout"]
- assert not checkout.voucher_code
- assert not checkout.discount_amount
- assert not checkout.discount_name
-
- response = client.post(url, data, follow=True)
- order = response.context["order"]
- assert not order.voucher
- assert not order.discount_amount
- assert not order.discount_name
-
-
-def test_view_checkout_summary_with_invalid_voucher_code(
- client, request_checkout_with_item, shipping_zone, address
-):
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.email = "[email protected]"
- request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
- request_checkout_with_item.save()
-
- url = reverse("checkout:summary")
- voucher_url = "{url}?next={url}".format(url=url)
- data = {"discount-voucher": "invalid-code"}
-
- response = client.post(voucher_url, data, follow=True, HTTP_REFERER=url)
-
- assert "voucher" in response.context["voucher_form"].errors
- assert response.context["checkout"].voucher_code is None
-
-
-def test_view_checkout_place_order_with_expired_voucher_code(
- client, request_checkout_with_item, shipping_zone, address, voucher
-):
-
- checkout = request_checkout_with_item
-
- # add shipping information to the checkout
- checkout.shipping_address = address
- checkout.email = "[email protected]"
- checkout.shipping_method = shipping_zone.shipping_methods.first()
-
- # set voucher to be expired
- yesterday = timezone.now() - datetime.timedelta(days=1)
- voucher.end_date = yesterday
- voucher.save()
-
- # put the voucher code to checkout
- checkout.voucher_code = voucher.code
-
- # save the checkout
- checkout.save()
-
- checkout_url = reverse("checkout:summary")
-
- # place order
- data = {"address": "shipping_address"}
- response = client.post(checkout_url, data, follow=True)
-
- # order should not have been placed
- assert response.request["PATH_INFO"] == checkout_url
-
- # ensure the voucher was removed
- checkout.refresh_from_db()
- assert not checkout.voucher_code
-
-
-def test_view_checkout_place_order_with_item_out_of_stock(
- client, request_checkout_with_item, shipping_zone, address, voucher, product
-):
-
- checkout = request_checkout_with_item
- variant = product.variants.get()
-
- # add shipping information to the checkout
- checkout.shipping_address = address
- checkout.email = "[email protected]"
- checkout.shipping_method = shipping_zone.shipping_methods.first()
- checkout.save()
-
- # make the variant be out of stock
- variant.quantity = 0
- variant.save()
-
- checkout_url = reverse("checkout:summary")
- redirect_url = reverse("checkout:index")
-
- # place order
- data = {"address": "shipping_address"}
- response = client.post(checkout_url, data, follow=True)
-
- # order should have been aborted,
- # and user should have been redirected to its checkout
- assert response.request["PATH_INFO"] == redirect_url
-
-
-def test_view_checkout_place_order_without_shipping_address(
- client, request_checkout_with_item, shipping_zone
-):
-
- checkout = request_checkout_with_item
-
- # add shipping information to the checkout
- checkout.email = "[email protected]"
- checkout.shipping_method = shipping_zone.shipping_methods.first()
-
- # save the checkout
- checkout.save()
-
- checkout_url = reverse("checkout:summary")
- redirect_url = reverse("checkout:shipping-address")
-
- # place order
- data = {"address": "shipping_address"}
- response = client.post(checkout_url, data, follow=True)
-
- # order should have been aborted,
- # and user should have been redirected to its checkout
- assert response.request["PATH_INFO"] == redirect_url
-
-
-def test_view_checkout_summary_remove_voucher(
- client, request_checkout_with_item, shipping_zone, voucher, address
-):
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.email = "[email protected]"
- request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
- request_checkout_with_item.save()
-
- remove_voucher_url = reverse("checkout:summary")
- voucher_url = "{url}?next={url}".format(url=remove_voucher_url)
- data = {"discount-voucher": voucher.code}
-
- response = client.post(
- voucher_url, data, follow=True, HTTP_REFERER=remove_voucher_url
- )
-
- assert response.context["checkout"].voucher_code == voucher.code
-
- url = reverse("checkout:remove-voucher")
-
- response = client.post(url, follow=True, HTTP_REFERER=remove_voucher_url)
-
- assert not response.context["checkout"].voucher_code
-
-
@pytest.mark.parametrize("is_anonymous_user", (True, False))
def test_create_order_creates_expected_events(
request_checkout_with_item, customer_user, shipping_method, is_anonymous_user
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 51e542e765..09e8f96486 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -1,13 +1,9 @@
from unittest import mock
import pytest
-from django.contrib.auth.tokens import default_token_generator
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
from django.templatetags.static import static
-from django.urls import reverse
-from django.utils.encoding import force_bytes
-from django.utils.http import urlsafe_base64_encode
from templated_email import get_connection
import saleor.account.emails as account_emails
@@ -229,13 +225,7 @@ def test_email_with_email_not_configured_raises_error(settings, site_settings):
def test_send_set_password_email(staff_user, site_settings):
- uid = urlsafe_base64_encode(force_bytes(staff_user.pk))
- token = default_token_generator.make_token(staff_user)
- password_set_url = build_absolute_uri(
- reverse(
- "account:reset-password-confirm", kwargs={"token": token, "uidb64": uid}
- )
- )
+ password_set_url = "https://www.example.com"
template_name = "dashboard/staff/set_password"
recipient_email = staff_user.email
@@ -244,9 +234,5 @@ def test_send_set_password_email(staff_user, site_settings):
)
assert len(mail.outbox) == 1
- generated_link = reverse(
- "account:reset-password-confirm", kwargs={"uidb64": uid, "token": token}
- )
- absolute_generated_link = build_absolute_uri(generated_link)
sended_message = mail.outbox[0].body
- assert absolute_generated_link in sended_message
+ assert password_set_url in sended_message
diff --git a/tests/test_order.py b/tests/test_order.py
index 5f22dfe58d..a34e266e4b 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -2,12 +2,8 @@ from decimal import Decimal
from unittest.mock import MagicMock, Mock, patch
import pytest
-from django.urls import reverse
from prices import Money, TaxedMoney
-from saleor.account import events as account_events
-from saleor.account.models import User
-from saleor.checkout.utils import create_order, prepare_order_data
from saleor.core.exceptions import InsufficientStock
from saleor.core.weight import zero_weight
from saleor.discount.models import (
@@ -17,7 +13,7 @@ from saleor.discount.models import (
VoucherType,
)
from saleor.discount.utils import validate_voucher_in_order
-from saleor.order import OrderStatus, events as order_events, models
+from saleor.order import OrderStatus, models
from saleor.order.emails import send_fulfillment_confirmation_to_customer
from saleor.order.events import OrderEvent, OrderEventsEmails
from saleor.order.models import Order
@@ -37,8 +33,6 @@ from saleor.payment import ChargeStatus
from saleor.payment.models import Payment
from saleor.product.models import Collection
-from .utils import get_redirect_location
-
def test_total_setter():
price = TaxedMoney(net=Money(10, "USD"), gross=Money(15, "USD"))
@@ -140,65 +134,6 @@ def test_add_variant_to_order_allow_overselling(order_with_lines):
assert variant.quantity_allocated == stock_before + quantity
-def test_view_connect_order_with_user_authorized_user(
- order, authorized_client, customer_user
-):
- order.user_email = customer_user.email
- order.save()
-
- url = reverse("order:connect-order-with-user", kwargs={"token": order.token})
- response = authorized_client.post(url)
-
- redirect_location = get_redirect_location(response)
- assert redirect_location == reverse("order:details", args=[order.token])
- order.refresh_from_db()
- assert order.user == customer_user
-
-
-def test_view_connect_order_with_user_different_email(
- order, authorized_client, customer_user
-):
- """Order was placed from different email, than user's
- we are trying to assign it to."""
- order.user = None
- order.user_email = "[email protected]"
- order.save()
-
- assert order.user_email != customer_user.email
-
- url = reverse("order:connect-order-with-user", kwargs={"token": order.token})
- response = authorized_client.post(url)
-
- redirect_location = get_redirect_location(response)
- assert redirect_location == reverse("account:details")
- order.refresh_from_db()
- assert order.user is None
-
-
-def test_view_order_with_deleted_variant(authorized_client, order_with_lines):
- order = order_with_lines
- order_details_url = reverse("order:details", kwargs={"token": order.token})
-
- # delete a variant associated to the order
- order.lines.first().variant.delete()
-
- # check if the order details view handles the deleted variant
- response = authorized_client.get(order_details_url)
- assert response.status_code == 200
-
-
-def test_view_fulfilled_order_with_deleted_variant(authorized_client, fulfilled_order):
- order = fulfilled_order
- order_details_url = reverse("order:details", kwargs={"token": order.token})
-
- # delete a variant associated to the order
- order.lines.first().variant.delete()
-
- # check if the order details view handles the deleted variant
- response = authorized_client.get(order_details_url)
- assert response.status_code == 200
-
-
@pytest.mark.parametrize("track_inventory", (True, False))
def test_restock_order_lines(order_with_lines, track_inventory):
@@ -407,154 +342,6 @@ def test_update_order_prices(order_with_lines):
assert order_with_lines.total == total
-def test_order_payment_flow(
- request_checkout_with_item, client, address, customer_user, shipping_zone
-):
- request_checkout_with_item.shipping_address = address
- request_checkout_with_item.billing_address = address.get_copy()
- request_checkout_with_item.email = "[email protected]"
- request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
- request_checkout_with_item.save()
-
- order = create_order(
- checkout=request_checkout_with_item,
- order_data=prepare_order_data(
- checkout=request_checkout_with_item,
- tracking_code="tracking_code",
- discounts=None,
- ),
- user=customer_user,
- )
-
- gateway = "Dummy"
- # Select payment
- url = reverse("order:payment", kwargs={"token": order.token})
- data = {"gateway": gateway}
- response = client.post(url, data, follow=True)
-
- assert len(response.redirect_chain) == 1
- assert response.status_code == 200
- redirect_url = reverse(
- "order:payment", kwargs={"token": order.token, "gateway": gateway}
- )
- assert response.request["PATH_INFO"] == redirect_url
-
- # Go to payment details page, enter payment data
- data = {
- "gateway": gateway,
- "is_active": True,
- "total": order.total.gross.amount,
- "currency": order.total.gross.currency,
- "charge_status": ChargeStatus.FULLY_CHARGED,
- }
- response = client.post(redirect_url, data)
-
- assert response.status_code == 302
- redirect_url = reverse("order:payment-success", kwargs={"token": order.token})
- assert get_redirect_location(response) == redirect_url
-
- # Assert that payment object was created and contains correct data
- payment = order.payments.all()[0]
- assert payment.total == order.total.gross.amount
- assert payment.currency == order.total.gross.currency
- assert payment.transactions.count() == 1
- assert payment.transactions.last().kind == "capture"
-
-
-def test_create_user_after_order(order, client):
- order.user_email = "[email protected]"
- order.save()
- url = reverse("order:checkout-success", kwargs={"token": order.token})
- data = {"password": "password"}
-
- response = client.post(url, data)
-
- redirect_url = reverse("order:details", kwargs={"token": order.token})
- assert get_redirect_location(response) == redirect_url
- user = User.objects.filter(email="[email protected]").first()
- assert user is not None
- assert user.orders.filter(token=order.token).exists()
-
-
-def test_view_order_details(order, client):
- url = reverse("order:details", kwargs={"token": order.token})
- response = client.get(url)
- assert response.status_code == 200
-
-
-def test_add_order_note_view(order, authorized_client, customer_user):
- order.user_email = customer_user.email
- order.save()
- url = reverse("order:details", kwargs={"token": order.token})
- customer_note = "bla-bla note"
- data = {"customer_note": customer_note}
-
- response = authorized_client.post(url, data)
-
- redirect_url = reverse("order:details", kwargs={"token": order.token})
- assert get_redirect_location(response) == redirect_url
- order.refresh_from_db()
- assert order.customer_note == customer_note
-
- # Ensure an order event was triggered
- note_event = order_events.OrderEvent.objects.get() # type: order_events.OrderEvent
- assert note_event.type == order_events.OrderEvents.NOTE_ADDED
- assert note_event.user == customer_user
- assert note_event.order == order
- assert note_event.parameters == {"message": customer_note}
-
- # Ensure a customer event was triggered
- note_event = account_events.CustomerEvent.objects.get()
- assert note_event.type == account_events.CustomerEvents.NOTE_ADDED_TO_ORDER
- assert note_event.user == customer_user
- assert note_event.order == order
- assert note_event.parameters == {"message": customer_note}
-
-
-def test_add_order_note_view_anonymous_order(order, authorized_client, customer_user):
- order.user_email = customer_user.email
- order.user = None
- order.save(update_fields=["user_email", "user"])
- url = reverse("order:details", kwargs={"token": order.token})
- customer_note = "bla-bla note"
- data = {"customer_note": customer_note}
-
- response = authorized_client.post(url, data)
- assert response.status_code == 302
-
- # Ensure an order event was triggered
- note_event = order_events.OrderEvent.objects.last() # type: order_events.OrderEvent
- assert note_event.type == order_events.OrderEvents.NOTE_ADDED
- assert note_event.user == customer_user
- assert note_event.order == order
- assert note_event.parameters == {"message": customer_note}
-
- # Ensure a customer event was not triggered because the order has no user
- assert not account_events.CustomerEvent.objects.exists()
-
-
-def test_anonymously_add_order_note_view_anonymous_order(order, client, customer_user):
- order.user_email = customer_user.email
- order.user = None
- order.save(update_fields=["user_email", "user"])
- url = reverse("order:details", kwargs={"token": order.token})
- customer_note = "bla-bla note"
- data = {"customer_note": customer_note}
-
- response = client.post(url, data)
- assert response.status_code == 302
-
- # Ensure an order event was triggered
- note_event = order_events.OrderEvent.objects.last() # type: order_events.OrderEvent
- assert note_event.type == order_events.OrderEvents.NOTE_ADDED
- assert note_event.user is None
- assert note_event.order == order
- assert note_event.parameters == {"message": customer_note}
-
- # Ensure a customer event was not triggered because the order has no user
- assert not account_events.CustomerEvent.objects.exists()
-
-
def _calculate_order_weight_from_lines(order):
weight = zero_weight()
for line in order:
diff --git a/tests/test_page.py b/tests/test_page.py
deleted file mode 100644
index f4075b69c7..0000000000
--- a/tests/test_page.py
+++ /dev/null
@@ -1,16 +0,0 @@
-def test_draft_page_details(client, admin_client, page):
- page.is_published = False
- page.save()
- page_url = page.get_absolute_url()
- response = client.get(page_url)
- assert response.status_code == 404
- response = admin_client.get(page_url)
- assert response.status_code == 200
-
-
-def test_public_page_details(client, admin_client, page):
- page_url = page.get_absolute_url()
- response = client.get(page_url)
- assert response.status_code == 200
- response = admin_client.get(page_url)
- assert response.status_code == 200
diff --git a/tests/test_product.py b/tests/test_product.py
index eba97f6bff..0aacf6afde 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -1,35 +1,21 @@
import datetime
import os
-import re
from decimal import Decimal
from unittest.mock import MagicMock, Mock, patch
import pytest
-from bs4 import BeautifulSoup, Tag
from django.forms import HiddenInput
-from django.http import JsonResponse
-from django.template.response import TemplateResponse
from django.urls import reverse
from freezegun import freeze_time
from prices import Money, MoneyRange, TaxedMoney
from saleor.account import events as account_events
-from saleor.checkout import utils
-from saleor.checkout.models import Checkout
-from saleor.checkout.utils import add_variant_to_checkout
from saleor.menu.models import MenuItemTranslation
from saleor.menu.utils import update_menu
-from saleor.product import AttributeInputType, ProductAvailabilityStatus, models
+from saleor.product import AttributeInputType, models
from saleor.product.filters import filter_products_by_attributes_values
from saleor.product.forms import VariantChoiceField
-from saleor.product.models import (
- Attribute,
- AttributeTranslation,
- AttributeValue,
- AttributeValueTranslation,
- DigitalContentUrl,
- Product,
-)
+from saleor.product.models import Attribute, AttributeValue, DigitalContentUrl
from saleor.product.thumbnails import create_product_thumbnails
from saleor.product.utils import (
allocate_stock,
@@ -38,7 +24,6 @@ from saleor.product.utils import (
increase_stock,
)
from saleor.product.utils.attributes import associate_attribute_values_to_instance
-from saleor.product.utils.availability import get_product_availability_status
from saleor.product.utils.costs import get_margin_for_variant
from saleor.product.utils.digital_products import increment_download_count
from saleor.product.utils.variants_picker import get_variant_picker_data
@@ -209,375 +194,6 @@ def test_render_product_detail_with_taxes(client, product):
assert response.status_code == 200
-def test_view_invalid_add_to_checkout(client, product, request_checkout):
- variant = product.variants.get()
- add_variant_to_checkout(request_checkout, variant, 2)
- response = client.post(
- reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- ),
- {},
- )
- assert response.status_code == 200
- assert request_checkout.quantity == 2
-
-
-def test_view_add_to_checkout_invalid_variant(client, product, request_checkout):
- response = client.post(
- reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- ),
- {"variant": 1234, "quantity": 1},
- HTTP_X_REQUESTED_WITH="XMLHttpRequest",
- )
- assert response.status_code == 400
- assert request_checkout.quantity == 0
-
-
-def test_view_add_to_checkout(authorized_client, product, user_checkout):
- variant = product.variants.first()
-
- # Ignore stock
- variant.track_inventory = False
- variant.save()
-
- # Add the variant to the user checkout and retrieve the variant line
- add_variant_to_checkout(user_checkout, variant)
- checkout_line = user_checkout.lines.last()
-
- # Retrieve the test url
- checkout_url = reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- )
-
- # Attempt to set the quantity to 50
- response = authorized_client.post(
- checkout_url,
- {"quantity": 49, "variant": variant.pk},
- HTTP_X_REQUESTED_WITH="XMLHttpRequest",
- ) # type: JsonResponse
- assert response.status_code == 200
-
- # Ensure the line quantity was updated to 50
- checkout_line.refresh_from_db(fields=["quantity"])
- assert checkout_line.quantity == 50
-
- # Attempt to increase the quantity to a too high count
- response = authorized_client.post(
- checkout_url,
- {"quantity": 1, "variant": variant.pk},
- HTTP_X_REQUESTED_WITH="XMLHttpRequest",
- )
- assert response.status_code == 400
-
- # Ensure the line quantity was not updated to 51
- checkout_line.refresh_from_db(fields=["quantity"])
- assert checkout_line.quantity == 50
-
-
-def test_adding_to_checkout_with_current_user_token(
- customer_user, authorized_client, product, request_checkout_with_item
-):
- key = utils.COOKIE_NAME
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.save()
-
- response = authorized_client.get(reverse("checkout:index"))
-
- utils.set_checkout_cookie(request_checkout_with_item, response)
- authorized_client.cookies[key] = response.cookies[key]
- variant = request_checkout_with_item.lines.first().variant
- url = reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- )
- data = {"quantity": 1, "variant": variant.pk}
-
- authorized_client.post(url, data)
-
- assert Checkout.objects.count() == 1
- assert Checkout.objects.get(user=customer_user).pk == request_checkout_with_item.pk
-
-
-def test_adding_to_checkout_with_another_user_token(
- admin_user, admin_client, product, customer_user, request_checkout_with_item
-):
- client = admin_client
- key = utils.COOKIE_NAME
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.save()
-
- response = client.get(reverse("checkout:index"))
-
- utils.set_checkout_cookie(request_checkout_with_item, response)
- client.cookies[key] = response.cookies[key]
- variant = request_checkout_with_item.lines.first().variant
- url = reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- )
- data = {"quantity": 1, "variant": variant.pk}
-
- client.post(url, data)
-
- assert Checkout.objects.count() == 2
- assert Checkout.objects.get(user=admin_user).pk != request_checkout_with_item.pk
-
-
-def test_anonymous_adding_to_checkout_with_another_user_token(
- client, product, customer_user, request_checkout_with_item
-):
- key = utils.COOKIE_NAME
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.save()
-
- response = client.get(reverse("checkout:index"))
-
- utils.set_checkout_cookie(request_checkout_with_item, response)
- client.cookies[key] = response.cookies[key]
- variant = product.variants.get()
- url = reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- )
- data = {"quantity": 1, "variant": variant.pk}
-
- client.post(url, data)
-
- assert Checkout.objects.count() == 2
- assert Checkout.objects.get(user=None).pk != request_checkout_with_item.pk
-
-
-def test_adding_to_checkout_with_deleted_checkout_token(
- customer_user, authorized_client, product, request_checkout_with_item
-):
- key = utils.COOKIE_NAME
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.save()
- old_token = request_checkout_with_item.token
-
- response = authorized_client.get(reverse("checkout:index"))
-
- utils.set_checkout_cookie(request_checkout_with_item, response)
- authorized_client.cookies[key] = response.cookies[key]
- request_checkout_with_item.delete()
- variant = product.variants.get()
- url = reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- )
- data = {"quantity": 1, "variant": variant.pk}
-
- authorized_client.post(url, data)
-
- assert Checkout.objects.count() == 1
- assert not Checkout.objects.filter(token=old_token).exists()
-
-
-def test_adding_to_checkout_with_closed_checkout_token(
- customer_user, authorized_client, product, request_checkout_with_item
-):
- key = utils.COOKIE_NAME
- request_checkout_with_item.user = customer_user
- request_checkout_with_item.save()
-
- response = authorized_client.get(reverse("checkout:index"))
- utils.set_checkout_cookie(request_checkout_with_item, response)
- authorized_client.cookies[key] = response.cookies[key]
- variant = product.variants.get()
- url = reverse(
- "product:add-to-checkout",
- kwargs={"slug": product.get_slug(), "product_id": product.pk},
- )
- data = {"quantity": 1, "variant": variant.pk}
-
- authorized_client.post(url, data)
-
- assert customer_user.checkouts.count() == 1
-
-
-def test_product_filter_before_filtering(authorized_client, product, category):
- products = (
- models.Product.objects.all()
- .filter(category__name=category)
- .order_by("-price_amount")
- )
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
-
- response = authorized_client.get(url)
-
- assert list(products) == list(response.context["filter_set"].qs)
-
-
-def test_product_filter_product_exists(authorized_client, product, category):
- products = (
- models.Product.objects.all()
- .filter(category__name=category)
- .order_by("-price_amount")
- )
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
- data = {
- "minimal_variant_price_amount_min": [""],
- "minimal_variant_price_amount_max": ["20"],
- }
-
- response = authorized_client.get(url, data)
-
- assert list(response.context["filter_set"].qs) == list(products)
-
-
-def test_product_filter_multi_values_attribute(
- authorized_client, product_with_multiple_values_attributes, category
-):
- """This tests the filters against multiple values attributes.
-
- It ensures:
- - It can filter by selecting multiple values
- - It can filter by selecting only one value
- - Having no occurrence can actually happen
- """
-
- product = product_with_multiple_values_attributes
- product_type = product.product_type
- attribute = product_type.product_attributes.first()
- attribute_values = attribute.values.in_bulk() # type: dict
-
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
-
- # Try selecting all the values
- data = {"modes": list(attribute_values.keys())}
- response = authorized_client.get(url, data)
- assert list(response.context["filter_set"].qs) == [product]
-
- # Try filtering with only one value
- data["modes"].pop()
- response = authorized_client.get(url, data)
- assert list(response.context["filter_set"].qs) == [product]
-
- # Try filtering with no occurrence
- attr_product_assoc = product.attributes.get(assignment__attribute_id=attribute.pk)
- attr_product_assoc.values.remove(data["modes"][0])
- response = authorized_client.get(url, data)
- assert list(response.context["filter_set"].qs) == []
-
-
-def test_product_filter_non_filterable(
- authorized_client,
- product_with_multiple_values_attributes,
- category,
- product_list_published,
-):
- """Ensures one cannot filter using a non filterable attribute"""
-
- product = product_with_multiple_values_attributes
- product_type = product.product_type
- attribute = product_type.product_attributes.first()
- attribute_values = attribute.values.in_bulk() # type: dict
- attribute.filterable_in_storefront = False
- attribute.save(update_fields=["filterable_in_storefront"])
-
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
-
- # Try selecting by the disabled attribute
- data = {"modes": list(attribute_values.keys())}
- response = authorized_client.get(url, data)
-
- # Nothing should have been filtered, thus returning all the products
- assert list(response.context["filter_set"].qs) == list(Product.objects.all())
-
-
-def test_product_filter_handles_garbage_input(
- authorized_client,
- product_with_multiple_values_attributes,
- category,
- product_list_published,
-):
- """Ensure filtering products through an invalid ID (non integer input) doesn't
- trigger any crash or error.
- """
-
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
-
- # Try selecting by the disabled attribute
- data = {"modes": "123-invalid-pk"}
- response = authorized_client.get(url, data)
- assert response.status_code == 200
-
- # Nothing should have been filtered, thus returning all the products
- assert list(response.context["filter_set"].qs) == list(Product.objects.all())
-
-
-def test_product_filter_product_does_not_exist(authorized_client, product, category):
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
- data = {"minimal_variant_price_min": ["20"], "minimal_variant_price_max": [""]}
-
- response = authorized_client.get(url, data)
-
- assert not list(response.context["filter_set"].qs)
-
-
-def test_product_filter_form(authorized_client, product, category):
- products = (
- models.Product.objects.all()
- .filter(category__name=category)
- .order_by("-minimal_variant_price_amount")
- )
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
-
- response = authorized_client.get(url)
-
- assert "minimal_variant_price" in response.context["filter_set"].form.fields.keys()
- assert "sort_by" in response.context["filter_set"].form.fields.keys()
- assert list(response.context["filter_set"].qs) == list(products)
-
-
-def test_product_filter_sorted_by_price_descending(
- authorized_client, product_list, category
-):
- products = (
- models.Product.objects.all()
- .filter(category__name=category, is_published=True)
- .order_by("-minimal_variant_price_amount")
- )
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
- data = {"sort_by": "-minimal_variant_price_amount"}
-
- response = authorized_client.get(url, data)
-
- assert list(response.context["filter_set"].qs) == list(products)
-
-
-def test_product_filter_sorted_by_wrong_parameter(authorized_client, product, category):
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
- data = {"sort_by": "aaa"}
-
- response = authorized_client.get(url, data)
-
- assert not response.context["filter_set"].form.is_valid()
- assert not response.context["products"]
-
-
def test_get_variant_picker_data_proper_variant_count(product):
data = get_variant_picker_data(
product, discounts=None, extensions=None, local_currency=None
@@ -617,126 +233,6 @@ def test_get_variant_picker_data_no_nested_attributes(variant, product_type, cat
assert len(data["variantAttributes"]) == 0
-def test_render_product_page_with_no_variant(unavailable_product, admin_client):
- product = unavailable_product
- product.is_published = True
- product.product_type.has_variants = True
- product.save()
- status = get_product_availability_status(product)
- assert status == ProductAvailabilityStatus.VARIANTS_MISSSING
- url = reverse(
- "product:details", kwargs={"product_id": product.pk, "slug": product.get_slug()}
- )
- response = admin_client.get(url)
- assert response.status_code == 200
-
-
-def test_render_product_page_with_multi_values_attribute(
- client, product_with_multiple_values_attributes
-):
- """This test ensures the rendering of a product without attribute doesn't fail."""
- product = product_with_multiple_values_attributes
- url = reverse(
- "product:details", kwargs={"product_id": product.pk, "slug": product.get_slug()}
- )
- response = client.get(url)
- assert response.status_code == 200
-
-
-def test_product_page_renders_attributes_properly(
- settings, client, product_with_multiple_values_attributes, color_attribute
-):
- """This test ensures the product attributes are properly rendered as expected
- including the translations."""
-
- settings.LANGUAGE_CODE = "fr"
-
- product = product_with_multiple_values_attributes
- multi_values_attribute = product.product_type.product_attributes.first()
-
- # Retrieve the attributes' values
- red, blue = color_attribute.values.all()
- eco_mode, performance_mode = multi_values_attribute.values.all()
-
- # Assign the dropdown attribute to the product
- product.product_type.product_attributes.add(color_attribute)
- associate_attribute_values_to_instance(product, color_attribute, red)
-
- # Create the attribute name translations
- AttributeTranslation.objects.bulk_create(
- [
- AttributeTranslation(
- language_code="fr",
- attribute=multi_values_attribute,
- name="Multiple Valeurs",
- ),
- AttributeTranslation(
- language_code="fr", attribute=color_attribute, name="Couleur"
- ),
- ]
- )
-
- # Create the attribute value translations
- AttributeValueTranslation.objects.bulk_create(
- [
- AttributeValueTranslation(
- language_code="fr", attribute_value=red, name="Rouge"
- ),
- AttributeValueTranslation(
- language_code="fr", attribute_value=blue, name="Bleu"
- ),
- AttributeValueTranslation(
- language_code="fr", attribute_value=eco_mode, name="Mode économique"
- ),
- AttributeValueTranslation(
- language_code="fr",
- attribute_value=performance_mode,
- name="Mode performance",
- ),
- ]
- )
-
- # Render the page
- url = reverse(
- "product:details", kwargs={"product_id": product.pk, "slug": product.get_slug()}
- )
- response = client.get(url) # type: TemplateResponse
- assert response.status_code == 200
-
- # Retrieve the attribute table
- soup = BeautifulSoup(response.content, "lxml")
- attribute_table = soup.select_one(".product__info table") # type: Tag
- assert attribute_table, "Did not find the attribute table"
-
- # Retrieve the table rows
- expected_attributes_re = re.compile(
- r"Multiple Valeurs:Mode économique,\s+Mode performance\n\s*" # noqa: no black!
- r"Couleur:Rouge"
- )
-
- attribute_rows = attribute_table.select("tr")
- actual_attributes = "\n".join(row.get_text(strip=True) for row in attribute_rows)
-
- assert len(attribute_rows) == 2
- assert expected_attributes_re.match(actual_attributes)
-
-
-def test_include_products_from_subcategories_in_main_view(
- category, product, authorized_client
-):
- subcategory = models.Category.objects.create(
- name="sub", slug="test", parent=category
- )
- product.category = subcategory
- product.save()
- # URL to parent category view
- url = reverse(
- "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
- )
- response = authorized_client.get(url)
- assert product in response.context_data["products"][0]
-
-
@patch("saleor.product.thumbnails.create_thumbnails")
def test_create_product_thumbnails(mock_create_thumbnails, product_with_image):
product_image = product_with_image.images.first()