Skip to content

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

Merge pull request #2987 from oldPadavan/api-order-update-change-user-if-email-provided

Marcin Gębala1b9bc94d0a77
diff --cc tests/api/test_order.py
index 62e6b20e28,0d8bc9f6ad..c69d0247b8
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@@ -567,17 -674,19 +646,19 @@@ def test_require_draft_order_when_remov
order = order_with_lines
line = order.lines.first()
line_id = graphene.Node.to_global_id('OrderLine', line.id)
- variables = json.dumps({'id': line_id})
- response = admin_api_client.post(
- reverse('api'),
- {'query': DRAFT_ORDER_LINE_DELETE_MUTATION, 'variables': variables})
+ variables = {'id': line_id}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders])
content = get_graphql_content(response)
data = content['data']['draftOrderLineDelete']
- assert 'errors' in data
+ assert data['errors']
-def test_order_update(admin_api_client, order_with_lines):
+def test_order_update(
+ staff_api_client, permission_manage_orders, order_with_lines):
order = order_with_lines
+ order.user = None
+ order.save()
query = """
mutation orderUpdate(
$id: ID!, $email: String, $first_name: String, $last_name: String,
@@@ -620,11 -726,101 +701,103 @@@
order.refresh_from_db()
assert order.shipping_address.first_name == first_name
assert order.billing_address.last_name == last_name
+ assert order.user_email == email
+ assert order.user is None
+
+
+ def test_order_update_anonymous_user_no_user_email(
+ admin_api_client, order_with_lines):
+ order = order_with_lines
+ order.user = None
+ order.save()
+ query = """
+ mutation orderUpdate(
+ $id: ID!, $first_name: String, $last_name: String,
+ $country_code: String) {
+ orderUpdate(
+ id: $id, input: {
+ shippingAddress:
+ {firstName: $first_name, country: $country_code},
+ billingAddress:
+ {lastName: $last_name, country: $country_code}}) {
+ errors {
+ field
+ message
+ }
+ order {
+ id
+ }
+ }
+ }
+ """
+ first_name = 'Test fname'
+ last_name = 'Test lname'
+ order_id = graphene.Node.to_global_id('Order', order.id)
+ variables = json.dumps({
+ 'id': order_id, 'first_name': first_name, 'last_name': last_name,
+ 'country_code': 'PL'})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' in content['data']['orderUpdate']
+ assert content['data']['orderUpdate']['errors'][0] == {
+ 'field': 'userEmail',
+ 'message': 'User_email field is null while order was created by '
+ 'anonymous user'}
+
+ order.refresh_from_db()
+ assert order.shipping_address.first_name != first_name
+ assert order.billing_address.last_name != last_name
+
+
+ def test_order_update_user_email_existing_user(
+ admin_api_client, order_with_lines, customer_user):
+ order = order_with_lines
+ order.user = None
+ order.save()
+ query = """
+ mutation orderUpdate(
+ $id: ID!, $email: String, $first_name: String, $last_name: String,
+ $country_code: String) {
+ orderUpdate(
+ id: $id, input: {
+ userEmail: $email, shippingAddress:
+ {firstName: $first_name, country: $country_code},
+ billingAddress:
+ {lastName: $last_name, country: $country_code}}) {
+ errors {
+ field
+ message
+ }
+ order {
+ userEmail
+ }
+ }
+ }
+ """
+ email = customer_user.email
+ first_name = 'Test fname'
+ last_name = 'Test lname'
+ order_id = graphene.Node.to_global_id('Order', order.id)
+ variables = json.dumps(
+ {'id': order_id, 'email': email, 'first_name': first_name,
+ 'last_name': last_name, 'country_code': 'PL'})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ data = content['data']['orderUpdate']['order']
+ assert data['userEmail'] == email
+
+ order.refresh_from_db()
+ assert order.shipping_address.first_name == first_name
+ assert order.billing_address.last_name == last_name
+ assert order.user_email == email
+ assert order.user == customer_user
-def test_order_add_note(admin_api_client, order_with_lines, admin_user):
+def test_order_add_note(
+ staff_api_client, permission_manage_orders, order_with_lines,
+ staff_user):
order = order_with_lines
query = """
mutation addNote($id: ID!, $message: String) {