Show the call (14,805 characters)
/work/core/saleor/graphql/account/tests/mutations/customer_type/test_customer_type_delete.py
from unittest.mock import patch
import graphene
import pytest
from ......account.error_codes import CustomerTypeDeleteErrorCode
from ......account.models import CustomerType
from ......attribute.models import Attribute, AttributeCustomerType
from ......tests import race_condition
from .....tests.utils import (
assert_no_permission,
get_graphql_content,
get_graphql_content_from_response,
)
CUSTOMER_TYPE_DELETE_MUTATION = """
mutation CustomerTypeDelete($id: ID!) {
customerTypeDelete(id: $id) {
customerType {
id
name
slug
isDefault
}
errors {
field
code
message
}
}
}
"""
MUTATION_MODULE = (
"saleor.graphql.account.mutations.customer_type.customer_type_delete"
)
def test_delete(
staff_api_client,
permission_manage_customer_types_and_attributes,
customer_type,
default_customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)
variables = {"id": customer_type_id}
# when
response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["errors"] == []
assert data["customerType"] == {
"id": customer_type_id,
"name": customer_type.name,
"slug": customer_type.slug,
"isDefault": False,
}
assert CustomerType.objects.get() == default_customer_type
def test_delete_by_app(
app_api_client,
permission_manage_customer_types_and_attributes,
customer_type,
):
# given
app_api_client.app.permissions.add(permission_manage_customer_types_and_attributes)
variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}
# when
response = app_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["errors"] == []
assert CustomerType.objects.filter(pk=customer_type.pk).exists() is False
def test_delete_keeps_assigned_attributes(
staff_api_client,
permission_manage_customer_types_and_attributes,
customer_type_with_attributes,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
customer_type = customer_type_with_attributes
attribute_pks = set(customer_type.customer_attributes.values_list("pk", flat=True))
assert len(attribute_pks) == 3
variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}
# when
response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["errors"] == []
assert CustomerType.objects.filter(pk=customer_type.pk).exists() is False
assert (
AttributeCustomerType.objects.filter(customer_type_id=customer_type.pk).exists()
is False
)
assert (
set(
Attribute.objects.filter(pk__in=attribute_pks).values_list("pk", flat=True)
)
== attribute_pks
)
def test_delete_default_type(
staff_api_client,
permission_manage_customer_types_and_attributes,
default_customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
assert default_customer_type.is_default is True
variables = {
"id": graphene.Node.to_global_id("CustomerType", default_customer_type.pk)
}
# when
response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["customerType"] is None
assert data["errors"] == [
{
"field": "id",
"code": CustomerTypeDeleteErrorCode.CANNOT_DELETE_DEFAULT.name,
"message": (
"The default customer type cannot be deleted. Mark another "
"customer type as the default first."
),
}
]
assert CustomerType.objects.get(is_default=True) == default_customer_type
@pytest.mark.parametrize(
("_case", "hook_target"),
[
(
"becomes the default before the instance is fetched",
"CustomerTypeDelete.get_instance",
),
(
"becomes the default after the instance is fetched",
"CustomerTypeDelete.clean_instance",
),
(
"becomes the default right before the customer types are locked",
"customer_type_qs_select_for_update",
),
],
)
def test_delete_type_that_becomes_default_in_the_meantime(
_case,
hook_target,
staff_api_client,
permission_manage_customer_types_and_attributes,
customer_type,
default_customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}
def transfer_default_flag(*args, **kwargs):
CustomerType.objects.filter(is_default=True).update(is_default=False)
CustomerType.objects.filter(pk=customer_type.pk).update(is_default=True)
# when
with race_condition.RunBefore(
f"{MUTATION_MODULE}.{hook_target}", transfer_default_flag
):
response = staff_api_client.post_graphql(
CUSTOMER_TYPE_DELETE_MUTATION, variables
)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["customerType"] is None
assert len(data["errors"]) == 1
error = data["errors"][0]
assert error["field"] == "id"
assert error["code"] == CustomerTypeDeleteErrorCode.CANNOT_DELETE_DEFAULT.name
assert error["message"] == (
"The default customer type cannot be deleted. Mark another customer "
"type as the default first."
)
assert CustomerType.objects.get(is_default=True) == customer_type
assert CustomerType.objects.filter(pk=default_customer_type.pk).exists() is True
def test_delete_type_deleted_in_the_meantime(
staff_api_client,
permission_manage_customer_types_and_attributes,
customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)
variables = {"id": customer_type_id}
def delete_customer_type(*args, **kwargs):
CustomerType.objects.filter(pk=customer_type.pk).delete()
# when
with (
race_condition.RunBefore(
f"{MUTATION_MODULE}.customer_type_qs_select_for_update",
delete_customer_type,
),
patch(
"saleor.plugins.manager.PluginsManager.customer_type_deleted"
) as mocked_customer_type_deleted,
):
response = staff_api_client.post_graphql(
CUSTOMER_TYPE_DELETE_MUTATION, variables
)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["customerType"] is None
assert data["errors"] == [
{
"field": "id",
"code": CustomerTypeDeleteErrorCode.NOT_FOUND.name,
"message": f"Couldn't resolve to a node: {customer_type_id}",
}
]
mocked_customer_type_deleted.assert_not_called()
def test_delete_not_existing_type(
staff_api_client,
permission_manage_customer_types_and_attributes,
default_customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
customer_type_id = graphene.Node.to_global_id("CustomerType", -1)
variables = {"id": customer_type_id}
# when
response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["customerType"] is None
assert data["errors"] == [
{
"field": "id",
"code": CustomerTypeDeleteErrorCode.NOT_FOUND.name,
"message": f"Couldn't resolve to a node: {customer_type_id}",
}
]
assert CustomerType.objects.get() == default_customer_type
@pytest.mark.parametrize(
("_case", "customer_type_id", "message"),
[
(
"ID of another type",
graphene.Node.to_global_id("ProductType", 1),
"Must receive a CustomerType id.",
),
(
"malformed ID",
"not-a-global-id",
"Invalid ID: not-a-global-id. Expected: CustomerType.",
),
],
)
def test_delete_with_invalid_id(
_case,
customer_type_id,
message,
staff_api_client,
permission_manage_customer_types_and_attributes,
customer_type,
default_customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
variables = {"id": customer_type_id}
# when
response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert data["customerType"] is None
assert data["errors"] == [
{
"field": "id",
"code": CustomerTypeDeleteErrorCode.GRAPHQL_ERROR.name,
"message": message,
}
]
assert CustomerType.objects.count() == 2
@patch("saleor.plugins.manager.PluginsManager.customer_type_deleted")
def test_delete_triggers_webhook(
mocked_customer_type_deleted,
staff_api_client,
permission_manage_customer_types_and_attributes,
customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}
# when
response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
assert content["data"]["customerTypeDelete"]["errors"] == []
mocked_customer_type_deleted.assert_called_once_with(customer_type)
deleted_customer_type = mocked_customer_type_deleted.call_args.args[0]
assert deleted_customer_type.pk == customer_type.pk
@patch("saleor.plugins.manager.PluginsManager.customer_type_deleted")
def test_delete_default_type_does_not_trigger_webhook(
mocked_customer_type_deleted,
staff_api_client,
permission_manage_customer_types_and_attributes,
default_customer_type,
):
# given
staff_api_client.user.user_permissions.add(
permission_manage_customer_types_and_attributes
)
variables = {
"id": graphene.Node.to_global_id("CustomerType", default_customer_type.pk)
}
# when
response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)
# then
content = get_graphql_content(response)
data = content["data"]["customerTypeDelete"]
assert len(data["errors"]) == 1
assert (
data["errors"][0]["code"]
== CustomerTypeDeleteErrorCode.CANNOT_DELETE_DEFAULT.name
)
mocked_customer_type_deleted.assert_not_called()
@pytest.mark.parametrize(
("_case", "client_fixture", "permission_fixture", "is_allowed"),
Cut here: 14,805 characters in all. The download has the rest.
Result (15,891 characters)
File created successfully at: /work/core/saleor/graphql/account/tests/mutations/customer_type/test_customer_type_delete.py (file state is current in your context — no need to Read it back)
[metadata] {"type": "create", "filePath": "/work/core/saleor/graphql/account/tests/mutations/customer_type/test_customer_type_delete.py", "content": "from unittest.mock import patch\n\nimport graphene\nimport pytest\n\nfrom ......account.error_codes import CustomerTypeDeleteErrorCode\nfrom ......account.models import CustomerType\nfrom ......attribute.models import Attribute, AttributeCustomerType\nfrom ......tests import race_condition\nfrom .....tests.utils import (\n assert_no_permission,\n get_graphql_content,\n get_graphql_content_from_response,\n)\n\nCUSTOMER_TYPE_DELETE_MUTATION = \"\"\"\n mutation CustomerTypeDelete($id: ID!) {\n customerTypeDelete(id: $id) {\n customerType {\n id\n name\n slug\n isDefault\n }\n errors {\n field\n code\n message\n }\n }\n }\n\"\"\"\n\nMUTATION_MODULE = (\n \"saleor.graphql.account.mutations.customer_type.customer_type_delete\"\n)\n\n\ndef test_delete(\n staff_api_client,\n permission_manage_customer_types_and_attributes,\n customer_type,\n default_customer_type,\n):\n # given\n staff_api_client.user.user_permissions.add(\n permission_manage_customer_types_and_attributes\n )\n customer_type_id = graphene.Node.to_global_id(\"CustomerType\", customer_type.pk)\n variables = {\"id\": customer_type_id}\n\n # when\n response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)\n\n # then\n content = get_graphql_content(response)\n data = content[\"data\"][\"customerTypeDelete\"]\n assert data[\"errors\"] == []\n assert data[\"customerType\"] == {\n \"id\": customer_type_id,\n \"name\": customer_type.name,\n \"slug\": customer_type.slug,\n \"isDefault\": False,\n }\n assert CustomerType.objects.get() == default_customer_type\n\n\ndef test_delete_by_app(\n app_api_client,\n permission_manage_customer_types_and_attributes,\n customer_type,\n):\n # given\n app_api_client.app.permissions.add(permission_manage_customer_types_and_attributes)\n variables = {\"id\": graphene.Node.to_global_id(\"CustomerType\", customer_type.pk)}\n\n # when\n response = app_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)\n\n # then\n content = get_graphql_content(response)\n data = content[\"data\"][\"customerTypeDelete\"]\n assert data[\"errors\"] == []\n assert CustomerType.objects.filter(pk=customer_type.pk).exists() is False\n\n\ndef test_delete_keeps_assigned_attributes(\n staff_api_client,\n permission_manage_customer_types_and_attributes,\n customer_type_with_attributes,\n):\n # given\n staff_api_client.user.user_permissions.add(\n permission_manage_customer_types_and_attributes\n )\n customer_type = customer_type_with_attributes\n attribute_pks = set(customer_type.customer_attributes.values_list(\"pk\", flat=True))\n assert len(attribute_pks) == 3\n variables = {\"id\": graphene.Node.to_global_id(\"CustomerType\", customer_type.pk)}\n\n # when\n response = staff_api_client.post_graphql(CUSTOMER_TYPE_DELETE_MUTATION, variables)\n\n # then\n content = get_graphql_content(response)\n data = content[\"data\"][\"customerTypeDelete\"]\n assert data[\"errors\"] == []\n assert CustomerType.objects.filter(pk=customer_type.pk).exists() is False\n assert (\n AttributeCustomerType.objects.filter(customer_type_id=customer_type.pk).exists()\n is False\n )\n assert (\n set(\n Attribute.objects.filter(pk__in=attribute_pks).values_list(\"pk\", flat=True)\n )\n == attribute_pks\n )\n\n\nCut here: 15,891 characters in all. The download has the rest.