Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Test permissions in order API tests
Pacu29c588bced615
diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 78ce0c9069..fd32a184c0 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -21,7 +21,8 @@ from tests.api.utils import get_graphql_contentfrom .utils import assert_no_permission-def test_orderline_query(admin_api_client, fulfilled_order):+def test_orderline_query(+ staff_api_client, permission_manage_orders, fulfilled_order):order = fulfilled_orderquery = """query OrdersQuery {@@ -43,8 +44,8 @@ def test_orderline_query(admin_api_client, fulfilled_order):line = order.lines.first()line.variant = Noneline.save()-- response = admin_api_client.post_graphql(query)+ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(query)content = get_graphql_content(response)order_data = content['data']['orders']['edges'][0]['node']lines_data = order_data['lines']['edges']@@ -54,7 +55,9 @@ def test_orderline_query(admin_api_client, fulfilled_order):assert '/static/images/placeholder540x540.png' in thumbnails-def test_order_query(admin_api_client, fulfilled_order, shipping_zone):+def test_order_query(+ staff_api_client, permission_manage_orders, fulfilled_order,+ shipping_zone):order = fulfilled_orderquery = """query OrdersQuery {@@ -105,7 +108,8 @@ def test_order_query(admin_api_client, fulfilled_order, shipping_zone):}}"""- response = admin_api_client.post_graphql(query)+ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(query)content = get_graphql_content(response)order_data = content['data']['orders']['edges'][0]['node']assert order_data['number'] == str(order.pk)@@ -138,7 +142,9 @@ def test_order_query(admin_api_client, fulfilled_order, shipping_zone):assert expected_method.type.upper() == method['type']-def test_order_events_query(admin_api_client, fulfilled_order, admin_user):+def test_order_events_query(+ staff_api_client, permission_manage_orders, fulfilled_order,+ staff_user):query = """query OrdersQuery {orders(first: 1) {@@ -164,14 +170,15 @@ def test_order_events_query(admin_api_client, fulfilled_order, admin_user):"""event = fulfilled_order.events.create(type=OrderEvents.OTHER.value,- user=admin_user,+ user=staff_user,parameters={'message': 'Example note','email_type': OrderEventsEmails.PAYMENT.value,'amount': '80.00','quantity': '10','composed_id': '10-10'})- response = admin_api_client.post_graphql(query)+ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(query)content = get_graphql_content(response)data = content['data']['orders']['edges'][0]['node']['events'][0]assert data['message'] == event.parameters['message']@@ -179,7 +186,7 @@ def test_order_events_query(admin_api_client, fulfilled_order, admin_user):assert data['emailType'] == OrderEventsEmailsEnum.PAYMENT.nameassert data['quantity'] == int(event.parameters['quantity'])assert data['composedId'] == event.parameters['composed_id']- assert data['user']['email'] == admin_user.email+ assert data['user']['email'] == staff_user.emailassert data['type'] == OrderEvents.OTHER.value.upper()assert data['date'] == event.date.isoformat()@@ -211,8 +218,8 @@ def test_non_staff_user_can_only_see_his_order(user_api_client, order):def test_draft_order_create(- admin_api_client, customer_user, product_without_shipping,- shipping_method, variant, voucher):+ staff_api_client, permission_manage_orders, customer_user,+ product_without_shipping, shipping_method, variant, voucher):variant_0 = variantquery = """mutation draftCreate(@@ -259,16 +266,19 @@ def test_draft_order_create(variant_list = [{'variantId': variant_0_id, 'quantity': 2},{'variantId': variant_1_id, 'quantity': 1}]- shipping_address = {- 'firstName': 'John', 'country': 'PL'}+ shipping_address = {'firstName': 'John', 'country': 'PL'}shipping_id = graphene.Node.to_global_id('ShippingMethod', shipping_method.id)voucher_id = graphene.Node.to_global_id('Voucher', voucher.id)variables = {- 'user': user_id, 'discount': discount,- 'lines': variant_list, 'shippingAddress': shipping_address,- 'shippingMethod': shipping_id, 'voucher': voucher_id}- response = admin_api_client.post_graphql(query, variables)+ 'user': user_id,+ 'discount': discount,+ 'lines': variant_list,+ 'shippingAddress': shipping_address,+ 'shippingMethod': shipping_id,+ 'voucher': voucher_id}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['draftOrderCreate']['order']assert data['status'] == OrderStatus.DRAFT.upper()@@ -282,7 +292,8 @@ def test_draft_order_create(**{'first_name': 'John', 'country': 'PL'})-def test_draft_order_update(admin_api_client, order_with_lines):+def test_draft_order_update(+ staff_api_client, permission_manage_orders, order_with_lines):order = order_with_linesquery = """mutation draftUpdate($id: ID!, $email: String) {@@ -300,13 +311,15 @@ def test_draft_order_update(admin_api_client, order_with_lines):email = '[email protected]'order_id = graphene.Node.to_global_id('Order', order.id)variables = {'id': order_id, 'email': email}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['draftOrderUpdate']['order']assert data['userEmail'] == email-def test_draft_order_delete(admin_api_client, order_with_lines):+def test_draft_order_delete(+ staff_api_client, permission_manage_orders, order_with_lines):order = order_with_linesquery = """mutation draftDelete($id: ID!) {@@ -319,7 +332,8 @@ def test_draft_order_delete(admin_api_client, order_with_lines):"""order_id = graphene.Node.to_global_id('Order', order.id)variables = {'id': order_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])with pytest.raises(order._meta.model.DoesNotExist):order.refresh_from_db()@@ -345,7 +359,8 @@ def test_check_for_draft_order_errors_no_order_lines(order):assert errors[0].message == 'Could not create order without any products.'-def test_draft_order_complete(admin_api_client, admin_user, draft_order):+def test_draft_order_complete(+ staff_api_client, permission_manage_orders, staff_user, draft_order):order = draft_orderquery = """mutation draftComplete($id: ID!) {@@ -364,18 +379,19 @@ def test_draft_order_complete(admin_api_client, admin_user, draft_order):order_id = graphene.Node.to_global_id('Order', order.id)variables = {'id': order_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['draftOrderComplete']['order']order.refresh_from_db()assert data['status'] == order.status.upper()missing_stock_event, draft_placed_event = OrderEvent.objects.all()- assert missing_stock_event.user == admin_user+ assert missing_stock_event.user == staff_userassert missing_stock_event.type == OrderEvents.OVERSOLD_ITEMS.valueassert missing_stock_event.parameters == {'oversold_items': [str(line_2)]}- assert draft_placed_event.user == admin_user+ assert draft_placed_event.user == staff_userassert draft_placed_event.type == OrderEvents.PLACED_FROM_DRAFT.valueassert draft_placed_event.parameters == {}@@ -439,7 +455,7 @@ def test_draft_order_line_create(def test_require_draft_order_when_creating_lines(- order_with_lines, admin_api_client):+ order_with_lines, staff_api_client, permission_manage_orders):query = DRAFT_ORDER_LINE_CREATE_MUTATIONorder = order_with_linesline = order.lines.first()@@ -447,7 +463,8 @@ def test_require_draft_order_when_creating_lines(order_id = graphene.Node.to_global_id('Order', order.id)variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)variables = {'orderId': order_id, 'variantId': variant_id, 'quantity': 1}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['draftOrderLineCreate']assert data['errors']@@ -506,13 +523,14 @@ def test_draft_order_line_update(def test_require_draft_order_when_updating_lines(- order_with_lines, admin_api_client):+ order_with_lines, staff_api_client, permission_manage_orders):query = DRAFT_ORDER_LINE_UPDATE_MUTATIONorder = order_with_linesline = order.lines.first()line_id = graphene.Node.to_global_id('OrderLine', line.id)variables = {'lineId': line_id, 'quantity': 1}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['draftOrderLineUpdate']assert data['errors']@@ -544,13 +562,8 @@ def test_draft_order_line_remove(line_id = graphene.Node.to_global_id('OrderLine', line.id)variables = {'id': line_id}- # mutation should fail without proper permissions- response = staff_api_client.post_graphql(query, variables)- assert_no_permission(response)-- # assign permissions- staff_api_client.user.user_permissions.add(permission_manage_orders)- response = staff_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['draftOrderLineDelete']assert data['orderLine']['id'] == line_id@@ -558,19 +571,21 @@ def test_draft_order_line_remove(def test_require_draft_order_when_removing_lines(- admin_api_client, order_with_lines, permission_manage_orders):+ staff_api_client, order_with_lines, permission_manage_orders):query = DRAFT_ORDER_LINE_DELETE_MUTATIONorder = order_with_linesline = order.lines.first()line_id = graphene.Node.to_global_id('OrderLine', line.id)variables = {'id': line_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['draftOrderLineDelete']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_linesquery = """mutation orderUpdate(@@ -600,9 +615,13 @@ def test_order_update(admin_api_client, order_with_lines):assert not order.billing_address.last_name == last_nameorder_id = graphene.Node.to_global_id('Order', order.id)variables = {- 'id': order_id, 'email': email, 'first_name': first_name,- 'last_name': last_name, 'country_code': 'PL'}- response = admin_api_client.post_graphql(query, variables)+ 'id': order_id,+ 'email': email,+ 'first_name': first_name,+ 'last_name': last_name,+ 'country_code': 'PL'}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderUpdate']['order']assert data['userEmail'] == email@@ -612,7 +631,9 @@ def test_order_update(admin_api_client, order_with_lines):assert order.billing_address.last_name == last_name-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_linesquery = """mutation addNote($id: ID!, $message: String) {@@ -637,17 +658,18 @@ def test_order_add_note(admin_api_client, order_with_lines, admin_user):order_id = graphene.Node.to_global_id('Order', order.id)message = 'nuclear note'variables = {'id': order_id, 'message': message}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderAddNote']assert data['order']['id'] == order_id- assert data['event']['user']['email'] == admin_user.email+ assert data['event']['user']['email'] == staff_user.emailassert data['event']['message'] == messageevent = order.events.get()assert event.type == OrderEvents.NOTE_ADDED.value- assert event.user == admin_user+ assert event.user == staff_userassert event.parameters == {'message': message}@@ -662,14 +684,16 @@ CANCEL_ORDER_QUERY = """"""-def test_order_cancel_and_restock(admin_api_client, order_with_lines):+def test_order_cancel_and_restock(+ staff_api_client, permission_manage_orders, order_with_lines):order = order_with_linesquery = CANCEL_ORDER_QUERYorder_id = graphene.Node.to_global_id('Order', order.id)restock = Truequantity = order.get_total_quantity()variables = {'id': order_id, 'restock': restock}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderCancel']['order']order.refresh_from_db()@@ -679,13 +703,15 @@ def test_order_cancel_and_restock(admin_api_client, order_with_lines):assert data['status'] == order.status.upper()-def test_order_cancel(admin_api_client, order_with_lines):+def test_order_cancel(+ staff_api_client, permission_manage_orders, order_with_lines):order = order_with_linesquery = CANCEL_ORDER_QUERYorder_id = graphene.Node.to_global_id('Order', order.id)restock = Falsevariables = {'id': order_id, 'restock': restock}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderCancel']['order']order.refresh_from_db()@@ -694,7 +720,8 @@ def test_order_cancel(admin_api_client, order_with_lines):assert data['status'] == order.status.upper()-def test_order_capture(admin_api_client, payment_preauth, admin_user):+def test_order_capture(+ staff_api_client, permission_manage_orders, payment_preauth, staff_user):order = payment_preauth.orderquery = """mutation captureOrder($id: ID!, $amount: Decimal!) {@@ -712,7 +739,8 @@ def test_order_capture(admin_api_client, payment_preauth, admin_user):order_id = graphene.Node.to_global_id('Order', order.id)amount = float(payment_preauth.get_total().gross.amount)variables = {'id': order_id, 'amount': amount}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderCapture']['order']order.refresh_from_db()@@ -730,12 +758,12 @@ def test_order_capture(admin_api_client, payment_preauth, admin_user):'email': order.user_email,'email_type': OrderEventsEmails.PAYMENT.value}assert event_captured.type == OrderEvents.PAYMENT_CAPTURED.value- assert event_captured.user == admin_user+ assert event_captured.user == staff_userassert event_captured.parameters == {'amount': str(amount)}def test_paid_order_mark_as_paid(- admin_api_client, payment_preauth):+ staff_api_client, permission_manage_orders, payment_preauth):order = payment_preauth.orderquery = """mutation markPaid($id: ID!) {@@ -752,7 +780,8 @@ def test_paid_order_mark_as_paid("""order_id = graphene.Node.to_global_id('Order', order.id)variables = {'id': order_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)errors = content['data']['orderMarkAsPaid']['errors']msg = 'Orders with payments can not be manually marked as paid.'@@ -761,7 +790,8 @@ def test_paid_order_mark_as_paid(def test_order_mark_as_paid(- admin_api_client, order_with_lines, admin_user):+ staff_api_client, permission_manage_orders, order_with_lines,+ staff_user):order = order_with_linesquery = """mutation markPaid($id: ID!) {@@ -779,7 +809,8 @@ def test_order_mark_as_paid(assert not order.is_fully_paid()order_id = graphene.Node.to_global_id('Order', order.id)variables = {'id': order_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderMarkAsPaid']['order']order.refresh_from_db()@@ -787,10 +818,11 @@ def test_order_mark_as_paid(event_order_paid = order.events.first()assert event_order_paid.type == OrderEvents.ORDER_MARKED_AS_PAID.value- assert event_order_paid.user == admin_user+ assert event_order_paid.user == staff_user-def test_order_release(admin_api_client, payment_preauth, admin_user):+def test_order_release(+ staff_api_client, permission_manage_orders, payment_preauth, staff_user):order = payment_preauth.orderquery = """mutation releaseOrder($id: ID!) {@@ -803,16 +835,18 @@ def test_order_release(admin_api_client, payment_preauth, admin_user):"""order_id = graphene.Node.to_global_id('Order', order.id)variables = {'id': order_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderRelease']['order']assert data['paymentStatus'] == PaymentStatusEnum.REFUNDED.nameevent_payment_released = order.events.last()assert event_payment_released.type == OrderEvents.PAYMENT_RELEASED.value- assert event_payment_released.user == admin_user+ assert event_payment_released.user == staff_user-def test_order_refund(admin_api_client, payment_confirmed):+def test_order_refund(+ staff_api_client, permission_manage_orders, payment_confirmed):order = order = payment_confirmed.orderquery = """mutation refundOrder($id: ID!, $amount: Decimal!) {@@ -828,7 +862,8 @@ def test_order_refund(admin_api_client, payment_confirmed):order_id = graphene.Node.to_global_id('Order', order.id)amount = float(payment_confirmed.get_total().gross.amount)variables = {'id': order_id, 'amount': amount}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderRefund']['order']order.refresh_from_db()@@ -912,14 +947,16 @@ ORDER_UPDATE_SHIPPING_QUERY = """def test_order_update_shipping(- admin_api_client, order_with_lines, shipping_method, admin_user):+ staff_api_client, permission_manage_orders, order_with_lines,+ shipping_method, staff_user):order = order_with_linesquery = ORDER_UPDATE_SHIPPING_QUERYorder_id = graphene.Node.to_global_id('Order', order.id)method_id = graphene.Node.to_global_id('ShippingMethod', shipping_method.id)variables = {'order': order_id, 'shippingMethod': method_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderUpdateShipping']assert data['order']['id'] == order_id@@ -933,7 +970,8 @@ def test_order_update_shipping(def test_order_update_shipping_clear_shipping_method(- admin_api_client, order, admin_user, shipping_method):+ staff_api_client, permission_manage_orders, order, staff_user,+ shipping_method):order.shipping_method = shipping_methodorder.shipping_price = shipping_method.get_total()order.shipping_method_name = 'Example shipping'@@ -942,7 +980,8 @@ def test_order_update_shipping_clear_shipping_method(query = ORDER_UPDATE_SHIPPING_QUERYorder_id = graphene.Node.to_global_id('Order', order.id)variables = {'order': order_id, 'shippingMethod': None}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderUpdateShipping']assert data['order']['id'] == order_id@@ -954,12 +993,14 @@ def test_order_update_shipping_clear_shipping_method(def test_order_update_shipping_shipping_required(- admin_api_client, order_with_lines, admin_user):+ staff_api_client, permission_manage_orders, order_with_lines,+ staff_user):order = order_with_linesquery = ORDER_UPDATE_SHIPPING_QUERYorder_id = graphene.Node.to_global_id('Order', order.id)variables = {'order': order_id, 'shippingMethod': None}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderUpdateShipping']assert data['errors'][0]['field'] == 'shippingMethod'@@ -968,7 +1009,8 @@ def test_order_update_shipping_shipping_required(def test_order_update_shipping_no_shipping_address(- admin_api_client, order_with_lines, shipping_method, admin_user):+ staff_api_client, permission_manage_orders, order_with_lines,+ shipping_method, staff_user):order = order_with_linesorder.shipping_address = Noneorder.save()@@ -977,7 +1019,8 @@ def test_order_update_shipping_no_shipping_address(method_id = graphene.Node.to_global_id('ShippingMethod', shipping_method.id)variables = {'order': order_id, 'shippingMethod': method_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderUpdateShipping']assert data['errors'][0]['field'] == 'order'@@ -987,7 +1030,8 @@ def test_order_update_shipping_no_shipping_address(def test_order_update_shipping_incorrect_shipping_method(- admin_api_client, order_with_lines, shipping_method, admin_user):+ staff_api_client, permission_manage_orders, order_with_lines,+ shipping_method, staff_user):order = order_with_lineszone = shipping_method.shipping_zonezone.countries = ['DE']@@ -998,7 +1042,8 @@ def test_order_update_shipping_incorrect_shipping_method(method_id = graphene.Node.to_global_id('ShippingMethod', shipping_method.id)variables = {'order': order_id, 'shippingMethod': method_id}- response = admin_api_client.post_graphql(query, variables)+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders])content = get_graphql_content(response)data = content['data']['orderUpdateShipping']assert data['errors'][0]['field'] == 'shippingMethod'