Skip to content

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

Add error codes to tests

Aleksander Spyrab0d3fc0a8b02
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index cf2b9d1e4c..95fe9ce6e0 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -25,6 +25,8 @@ from saleor.graphql.account.mutations.staff import (
UserDelete,
)
from saleor.graphql.core.enums import PermissionEnum
+from saleor.graphql.core.utils import str_to_enum
+from saleor.graphql.core.utils.error_codes import AccountErrorCode
from saleor.order.models import FulfillmentStatus, Order
from tests.api.utils import get_graphql_content
from tests.utils import create_image
@@ -1303,6 +1305,7 @@ STAFF_CREATE_MUTATION = """
errors {
field
message
+ code
}
user {
id
@@ -1498,6 +1501,7 @@ def test_staff_update_doesnt_change_existing_avatar(
errors {
field
message
+ code
}
}
}
@@ -1642,6 +1646,7 @@ def test_set_password_invalid_token(user_api_client, customer_user):
content = get_graphql_content(response)
errors = content["data"]["setPassword"]["errors"]
assert errors[0]["message"] == INVALID_TOKEN
+ assert errors[0]["code"] == AccountErrorCode.INVALID_USER_TOKEN.name
def test_set_password_invalid_email(user_api_client):
@@ -1651,6 +1656,7 @@ def test_set_password_invalid_email(user_api_client):
errors = content["data"]["setPassword"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "email"
+ assert errors[0]["code"] == AccountErrorCode.NOT_USERS_ADDRESS.name
def test_set_password_invalid_password(user_api_client, customer_user, settings):
@@ -1672,7 +1678,9 @@ def test_set_password_invalid_password(user_api_client, customer_user, settings)
errors[0]["message"]
== "This password is too short. It must contain at least 5 characters."
)
+ assert errors[0]["code"] == str_to_enum("password_too_short")
assert errors[1]["message"] == "This password is entirely numeric."
+ assert errors[1]["code"] == str_to_enum("password_entirely_numeric")
@patch("saleor.account.emails._send_user_password_reset_email.delay")
@@ -1725,7 +1733,11 @@ def test_password_reset_email_non_existing_user(
content = get_graphql_content(response)
data = content["data"]["passwordReset"]
assert data["errors"] == [
- {"field": "email", "message": "User with this email doesn't exist"}
+ {
+ "field": "email",
+ "message": "User with this email doesn't exist",
+ "code": AccountErrorCode.USER_DOES_NOT_EXIST.name,
+ }
]
send_password_reset_mock.assert_not_called()
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index c2b1123b22..3b4eb5612e 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -8,6 +8,7 @@ from django.template.defaultfilters import slugify
from graphene.utils.str_converters import to_camel_case
from saleor.graphql.core.utils import snake_to_camel_case
+from saleor.graphql.core.utils.error_codes import AttributeErrorCode
from saleor.graphql.product.enums import AttributeTypeEnum, AttributeValueType
from saleor.graphql.product.mutations.attributes import validate_value_is_unique
from saleor.graphql.product.types.attributes import resolve_attribute_value_type
@@ -369,6 +370,7 @@ CREATE_ATTRIBUTES_QUERY = """
errors {
field
message
+ code
}
attribute {
name
@@ -468,10 +470,20 @@ def test_create_attribute_with_given_slug(
@pytest.mark.parametrize(
- "name_1, name_2, error_msg",
+ "name_1, name_2, error_msg, error_code",
(
- ("Red color", "Red color", "Provided values are not unique."),
- ("Red color", "red color", "Provided values are not unique."),
+ (
+ "Red color",
+ "Red color",
+ "Provided values are not unique.",
+ AttributeErrorCode.ATTRIBUTE_VALUES_NOT_UNIQUE,
+ ),
+ (
+ "Red color",
+ "red color",
+ "Provided values are not unique.",
+ AttributeErrorCode.ATTRIBUTE_VALUES_NOT_UNIQUE,
+ ),
),
)
def test_create_attribute_and_attribute_values_errors(
@@ -479,6 +491,7 @@ def test_create_attribute_and_attribute_values_errors(
name_1,
name_2,
error_msg,
+ error_code,
permission_manage_products,
product_type,
):
@@ -492,6 +505,7 @@ def test_create_attribute_and_attribute_values_errors(
assert errors
assert errors[0]["field"] == "values"
assert errors[0]["message"] == error_msg
+ assert errors[0]["code"] == error_code.name
UPDATE_ATTRIBUTE_QUERY = """
@@ -506,6 +520,7 @@ UPDATE_ATTRIBUTE_QUERY = """
errors {
field
message
+ code
}
attribute {
name
@@ -597,10 +612,20 @@ def test_update_empty_attribute_and_add_values(
@pytest.mark.parametrize(
- "name_1, name_2, error_msg",
+ "name_1, name_2, error_msg, error_code",
(
- ("Red color", "Red color", "Provided values are not unique."),
- ("Red color", "red color", "Provided values are not unique."),
+ (
+ "Red color",
+ "Red color",
+ "Provided values are not unique.",
+ AttributeErrorCode.ATTRIBUTE_VALUES_NOT_UNIQUE,
+ ),
+ (
+ "Red color",
+ "red color",
+ "Provided values are not unique.",
+ AttributeErrorCode.ATTRIBUTE_VALUES_NOT_UNIQUE,
+ ),
),
)
def test_update_attribute_and_add_attribute_values_errors(
@@ -608,6 +633,7 @@ def test_update_attribute_and_add_attribute_values_errors(
name_1,
name_2,
error_msg,
+ error_code,
color_attribute,
permission_manage_products,
):
@@ -628,6 +654,7 @@ def test_update_attribute_and_add_attribute_values_errors(
assert errors
assert errors[0]["field"] == "addValues"
assert errors[0]["message"] == error_msg
+ assert errors[0]["code"] == error_code.name
def test_update_attribute_and_remove_others_attribute_value(
@@ -654,6 +681,7 @@ def test_update_attribute_and_remove_others_attribute_value(
assert errors[0]["field"] == "removeValues"
err_msg = "Value %s does not belong to this attribute." % str(size_attribute)
assert errors[0]["message"] == err_msg
+ assert errors[0]["code"] == AttributeErrorCode.ATTRIBUTE_BAD_VALUE.name
def test_delete_attribute(
diff --git a/tests/api/test_base_mutation.py b/tests/api/test_base_mutation.py
index 8fa31c23b7..7f1679b77a 100644
--- a/tests/api/test_base_mutation.py
+++ b/tests/api/test_base_mutation.py
@@ -5,6 +5,7 @@ import pytest
from django.core.exceptions import ImproperlyConfigured
from saleor.graphql.core.mutations import BaseMutation
+from saleor.graphql.core.utils.error_codes import CommonErrorCode
from saleor.graphql.product import types as product_types
@@ -71,6 +72,7 @@ def test_user_error_nonexistent_id(schema_context):
errors {
field
message
+ code
}
}
}
@@ -82,6 +84,7 @@ def test_user_error_nonexistent_id(schema_context):
assert user_errors
assert user_errors[0]["field"] == "productId"
assert user_errors[0]["message"] == "Couldn't resolve to a node"
+ assert user_errors[0]["code"] == CommonErrorCode.DOES_NOT_EXIST.name
def test_user_error_id_of_different_type(product, schema_context):
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 13c8f398cc..2c88fa8507 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -15,6 +15,11 @@ from saleor.graphql.checkout.mutations import (
update_checkout_shipping_method_if_invalid,
)
from saleor.graphql.core.utils import str_to_enum
+from saleor.graphql.core.utils.error_codes import (
+ AccountErrorCode,
+ CheckoutErrorCode,
+ CommonErrorCode,
+)
from saleor.order.models import Order
from saleor.payment import PaymentError
from saleor.payment.models import Transaction
@@ -101,6 +106,7 @@ MUTATION_CHECKOUT_CREATE = """
errors {
field
message
+ code
}
}
}
@@ -151,15 +157,32 @@ def test_checkout_create(api_client, variant, graphql_address_data):
@pytest.mark.parametrize(
- "quantity, expected_error_message",
+ "quantity, expected_error_message, error_code",
(
- (-1, "The quantity should be higher than zero."),
- (0, "The quantity should be higher than zero."),
- (51, "Cannot add more than 50 times this item."),
+ (
+ -1,
+ "The quantity should be higher than zero.",
+ CommonErrorCode.POSITIVE_NUMBER_REQUIRED,
+ ),
+ (
+ 0,
+ "The quantity should be higher than zero.",
+ CommonErrorCode.POSITIVE_NUMBER_REQUIRED,
+ ),
+ (
+ 51,
+ "Cannot add more than 50 times this item.",
+ CheckoutErrorCode.QUANTITY_GREATER_THAN_LIMIT,
+ ),
),
)
def test_checkout_create_cannot_add_invalid_quantities(
- api_client, variant, graphql_address_data, quantity, expected_error_message
+ api_client,
+ variant,
+ graphql_address_data,
+ quantity,
+ expected_error_message,
+ error_code,
):
variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
@@ -177,7 +200,11 @@ def test_checkout_create_cannot_add_invalid_quantities(
content = get_graphql_content(response)["data"]["checkoutCreate"]
assert content["errors"]
assert content["errors"] == [
- {"field": "quantity", "message": expected_error_message}
+ {
+ "field": "quantity",
+ "message": expected_error_message,
+ "code": error_code.name,
+ }
]
@@ -756,7 +783,8 @@ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE = """
},
errors {
field,
- message
+ message,
+ code
}
}
}"""
@@ -820,7 +848,11 @@ def test_checkout_shipping_address_with_invalid_phone_number_returns_error(
)
)["data"]["checkoutShippingAddressUpdate"]
assert response["errors"] == [
- {"field": "phone", "message": "'+33600000' is not a valid phone number."}
+ {
+ "field": "phone",
+ "message": "'+33600000' is not a valid phone number.",
+ "code": AccountErrorCode.INVALID_PHONE_NUMBER.name,
+ }
]
@@ -883,6 +915,7 @@ def test_checkout_shipping_address_update_invalid_country_code(
data = content["data"]["checkoutShippingAddressUpdate"]
assert data["errors"][0]["message"] == "Invalid country code."
assert data["errors"][0]["field"] == "country"
+ assert data["errors"][0]["code"] == AccountErrorCode.INVALID_COUNTRY.name
def test_checkout_billing_address_update(
@@ -941,6 +974,7 @@ CHECKOUT_EMAIL_UPDATE_MUTATION = """
errors {
field,
message
+ code
}
}
}
diff --git a/tests/api/test_checkout_digital.py b/tests/api/test_checkout_digital.py
index c92cf3a3b3..c11f401eec 100644
--- a/tests/api/test_checkout_digital.py
+++ b/tests/api/test_checkout_digital.py
@@ -6,6 +6,7 @@ from prices import TaxedMoney
from saleor.account.models import Address
from saleor.checkout.models import Checkout
from saleor.checkout.utils import add_variant_to_checkout
+from saleor.graphql.core.utils.error_codes import CheckoutErrorCode
from saleor.order.models import Order
from tests.api.utils import get_graphql_content
@@ -111,7 +112,11 @@ def test_checkout_update_shipping_address(
data = content["data"]["checkoutShippingAddressUpdate"]
assert data["errors"] == [
- {"field": "shippingAddress", "message": "This checkout doesn't need shipping"}
+ {
+ "field": "shippingAddress",
+ "message": "This checkout doesn't need shipping",
+ "code": CheckoutErrorCode.SHIPPING_NOT_REQUIRED.name,
+ }
]
# Ensure the address was unchanged
diff --git a/tests/api/test_digital_content.py b/tests/api/test_digital_content.py
index f06461fef9..b55db175ae 100644
--- a/tests/api/test_digital_content.py
+++ b/tests/api/test_digital_content.py
@@ -1,5 +1,6 @@
import graphene
+from saleor.graphql.core.utils.error_codes import DigitalContentErrorCode
from saleor.product.models import DigitalContent, ProductVariant
from tests.api.utils import get_graphql_content
from tests.utils import create_image
@@ -255,6 +256,7 @@ def test_digital_content_update_mutation_missing_content(
errors {
field
message
+ code
}
}
}
@@ -278,6 +280,7 @@ def test_digital_content_update_mutation_missing_content(
errors = content["data"]["digitalContentUpdate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "variantId"
+ assert errors[0]["code"] == DigitalContentErrorCode.VARIANT_NO_DIGITAL_CONTENT.name
def test_digital_content_url_create(
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 2149a7dad1..79abf1cc50 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -8,6 +8,7 @@ from django_countries import countries
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import Sale, Voucher
+from saleor.graphql.core.utils.error_codes import CheckoutErrorCode
from saleor.graphql.discount.enums import DiscountValueTypeEnum, VoucherTypeEnum
from tests.api.utils import get_graphql_content
@@ -156,6 +157,7 @@ mutation voucherCreate(
errors {
field
message
+ code
}
voucher {
type
@@ -263,6 +265,7 @@ def test_create_voucher_with_existing_gift_card_code(
errors = content["data"]["voucherCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
+ assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
def test_create_voucher_with_existing_voucher_code(
@@ -289,6 +292,7 @@ def test_create_voucher_with_existing_voucher_code(
errors = content["data"]["voucherCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
+ assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index f17e7fb694..b366c458b8 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -2,6 +2,7 @@ from datetime import date
import graphene
+from saleor.graphql.core.utils.error_codes import AccountErrorCode, CheckoutErrorCode
from tests.api.utils import get_graphql_content
from .utils import assert_no_permission
@@ -171,6 +172,7 @@ mutation giftCardCreate(
errors {
field
message
+ code
}
giftCard {
displayCode
@@ -282,6 +284,7 @@ def test_create_gift_card_with_existing_voucher_code(
errors = content["data"]["giftCardCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
+ assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
def test_create_gift_card_with_existing_gift_card_code(
@@ -297,6 +300,7 @@ def test_create_gift_card_with_existing_gift_card_code(
errors = content["data"]["giftCardCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
+ assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
def test_create_gift_card_without_user(staff_api_client, permission_manage_gift_card):
@@ -347,6 +351,7 @@ def test_create_gift_card_with_incorrect_user_email(
assert len(errors) == 1
assert errors[0]["field"] == "email"
assert errors[0]["message"] == "Customer with this email doesn't exist."
+ assert errors[0]["code"] == AccountErrorCode.USER_DOES_NOT_EXIST.name
def test_create_gift_card_without_premissions(staff_api_client):
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 1c52de3c19..e236114c81 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -11,6 +11,7 @@ from prices import Money, TaxedMoney
from saleor.account.models import CustomerEvent
from saleor.core.taxes import zero_taxed_money
from saleor.graphql.core.enums import ReportingPeriod
+from saleor.graphql.core.utils.error_codes import CommonErrorCode
from saleor.graphql.order.mutations.orders import (
clean_order_cancel,
clean_order_capture,
@@ -1428,6 +1429,7 @@ def test_paid_order_mark_as_paid(
errors {
field
message
+ code
}
order {
isPaid
@@ -1445,6 +1447,7 @@ def test_paid_order_mark_as_paid(
msg = "Orders with payments can not be manually marked as paid."
assert errors[0]["message"] == msg
assert errors[0]["field"] == "payment"
+ assert errors[0]["code"] == CommonErrorCode.PAYMENT_ERROR.name
def test_order_mark_as_paid(
@@ -1490,6 +1493,7 @@ ORDER_VOID = """
errors {
field
message
+ code
}
}
}
@@ -1533,6 +1537,7 @@ def test_order_void_payment_error(
errors = content["data"]["orderVoid"]["errors"]
assert errors[0]["field"] == "payment"
assert errors[0]["message"] == msg
+ assert errors[0]["code"] == CommonErrorCode.PAYMENT_ERROR.name
def test_order_refund(staff_api_client, permission_manage_orders, payment_txn_captured):