Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
test_create_and_update_customer (#14680)
Renatae5bae199d3a1
diff --git a/saleor/tests/e2e/users/__init__.py b/saleor/tests/e2e/users/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/tests/e2e/users/test_create_and_update_customer_with_metadata.py b/saleor/tests/e2e/users/test_create_and_update_customer_with_metadata.pynew file mode 100644index 0000000000..dfdcd05927--- /dev/null+++ b/saleor/tests/e2e/users/test_create_and_update_customer_with_metadata.py@@ -0,0 +1,51 @@+import pytest++from ..utils import assign_permissions+from .utils import create_customer, customer_update+++def test_create_and_update_customer_with_metadata_core_1514(+ e2e_staff_api_client,+ permission_manage_users,+):+ # Before+ permissions = [+ permission_manage_users,+ ]+ assign_permissions(e2e_staff_api_client, permissions)++ # Step 1 - Create a customer with metadata+ email = "[email protected]"+ metadata = [{"key": "customer_code", "value": "abc"}]+ private_metadata = [{"key": "priv_customer_code", "value": "priv_abc"}]+ user_data = create_customer(+ e2e_staff_api_client,+ email,+ metadata,+ private_metadata,+ )++ assert user_data is not None+ user_id = user_data["id"]+ assert user_id is not None+ assert user_data["metadata"] == metadata+ assert user_data["privateMetadata"] == private_metadata++ # Step 2 - Update customer's metadata+ updated_metadata = [{"key": "customer_code", "value": "update_abc"}]+ updated_private_metadata = [+ {"key": "priv_customer_code", "value": "update_privabc"}+ ]+ update_input = {+ "metadata": updated_metadata,+ "privateMetadata": updated_private_metadata,+ }+ data = customer_update(+ e2e_staff_api_client,+ user_id,+ update_input,+ )+ assert data["id"] == user_id+ assert data["metadata"] == updated_metadata+ assert data["privateMetadata"] == updated_private_metadatadiff --git a/saleor/tests/e2e/users/test_should_bulk_update_customers_metadata.py b/saleor/tests/e2e/users/test_should_bulk_update_customers_metadata.pynew file mode 100644index 0000000000..d153fe5f38--- /dev/null+++ b/saleor/tests/e2e/users/test_should_bulk_update_customers_metadata.py@@ -0,0 +1,66 @@+import pytest++from ..utils import assign_permissions+from .utils import create_customer, customer_bulk_update+++def test_should_bulk_update_customers_metadata_core_1513(+ e2e_staff_api_client,+ permission_manage_users,+):+ # Before+ permissions = [+ permission_manage_users,+ ]+ assign_permissions(e2e_staff_api_client, permissions)++ # Step 1 - Create customers+ customer_ids = []+ for i in range(5):+ email = f"test_{i + 1}@saleor.io"+ user_data = create_customer(e2e_staff_api_client, email)+ user_id = user_data["id"]+ customer_ids.append(user_id)+ assert customer_ids is not None++ # Step 2 - Update customers' metadata in bulk+ error_policy = "IGNORE_FAILED"+ customer_updates = []+ for i in range(5):+ metadata_value = f"metadata_{i + 1}"+ private_metadata_value = f"private_metadata_{i + 1}"+ metadata = [{"key": "customer_code", "value": metadata_value}]+ private_metadata = [+ {"key": "priv_customer_code", "value": private_metadata_value}+ ]+ customer_updates.append(+ {+ "id": customer_ids[i],+ "input": {+ "metadata": metadata,+ "privateMetadata": private_metadata,+ },+ }+ )++ data = customer_bulk_update(+ e2e_staff_api_client,+ customer_updates,+ error_policy,+ )++ assert "count" in data+ assert "results" in data+ assert len(data["results"]) == 5++ for i in range(5):+ user_id = data["results"][i]["customer"]["id"]+ updated_metadata = data["results"][i]["customer"]["metadata"]+ updated_private_metadata = data["results"][i]["customer"]["privateMetadata"]++ assert user_id == customer_ids[i]+ assert updated_metadata == customer_updates[i]["input"]["metadata"]+ assert (+ updated_private_metadata == customer_updates[i]["input"]["privateMetadata"]+ )diff --git a/saleor/tests/e2e/users/utils/__init__.py b/saleor/tests/e2e/users/utils/__init__.pynew file mode 100644index 0000000000..a7593d0731--- /dev/null+++ b/saleor/tests/e2e/users/utils/__init__.py@@ -0,0 +1,9 @@+from .create_customer import create_customer+from .customer_bulk_update import customer_bulk_update+from .customer_update import customer_update++__all__ = [+ "create_customer",+ "customer_bulk_update",+ "customer_update",+]diff --git a/saleor/tests/e2e/users/utils/create_customer.py b/saleor/tests/e2e/users/utils/create_customer.pynew file mode 100644index 0000000000..ab7cfc4f29--- /dev/null+++ b/saleor/tests/e2e/users/utils/create_customer.py@@ -0,0 +1,54 @@+from ...utils import get_graphql_content++CUSTOMER_CREATE_MUTATION = """+mutation CreateCustomer ($input: UserCreateInput!) {+ customerCreate(+ input: $input+ ) {+ errors {+ field+ message+ code+ }+ user {+ id+ metadata {+ key+ value+ }+ privateMetadata {+ key+ value+ }+ }+ }+}+"""+++def create_customer(+ staff_api_client,+ email,+ metadata=None,+ private_metadata=None,+):+ variables = {+ "input": {+ "email": email,+ "metadata": metadata,+ "privateMetadata": private_metadata,+ }+ }++ response = staff_api_client.post_graphql(+ CUSTOMER_CREATE_MUTATION,+ variables,+ )+ content = get_graphql_content(response)++ assert content["data"]["customerCreate"]["errors"] == []++ data = content["data"]["customerCreate"]["user"]+ assert data["id"] is not None++ return datadiff --git a/saleor/tests/e2e/users/utils/customer_bulk_update.py b/saleor/tests/e2e/users/utils/customer_bulk_update.pynew file mode 100644index 0000000000..26cc6aeb10--- /dev/null+++ b/saleor/tests/e2e/users/utils/customer_bulk_update.py@@ -0,0 +1,54 @@+from ...utils import get_graphql_content++CUSTOMER_BULK_UPDATE_MUTATION = """+mutation CustomerBulkUpdate($errorPolicy: ErrorPolicyEnum, $customers: [CustomerBulkUpdateInput!]!) {+ customerBulkUpdate(errorPolicy: $errorPolicy, customers: $customers) {+ count+ results {+ customer {+ id+ metadata {+ key+ value+ }+ privateMetadata {+ key+ value+ }+ }+ errors {+ path+ message+ code+ }+ }+ errors {+ path+ message+ code+ }+ }+}+"""+++def customer_bulk_update(+ staff_api_client,+ customers,+ error_policy="REJECT_EVERYTHING",+):+ variables = {+ "customers": customers,+ "errorPolicy": error_policy,+ }++ response = staff_api_client.post_graphql(+ CUSTOMER_BULK_UPDATE_MUTATION,+ variables,+ )+ content = get_graphql_content(response)++ data = content["data"]["customerBulkUpdate"]++ return datadiff --git a/saleor/tests/e2e/users/utils/customer_update.py b/saleor/tests/e2e/users/utils/customer_update.pynew file mode 100644index 0000000000..55ae192318--- /dev/null+++ b/saleor/tests/e2e/users/utils/customer_update.py@@ -0,0 +1,50 @@+from ...utils import get_graphql_content++CUSTOMER_UPDATE_MUTATION = """+mutation CustomerUpdate($id: ID!, $input: CustomerInput!) {+ customerUpdate(id: $id, input: $input) {+ errors {+ field+ message+ code+ }+ user {+ id+ isActive+ isConfirmed+ isStaff+ metadata {+ key+ value+ }+ privateMetadata {+ key+ value+ }+ }+ }+}+"""+++def customer_update(+ api_client,+ user_id,+ input_data,+):+ variables = {+ "id": user_id,+ "input": input_data,+ }++ response = api_client.post_graphql(+ CUSTOMER_UPDATE_MUTATION,+ variables,+ )+ content = get_graphql_content(response)+ data = content["data"]["customerUpdate"]+ assert data["errors"] == []+ user_data = data["user"]++ return user_data