Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for app
Maciej Korycinski5a8edab3b0eb
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.pyindex ad72c03f79..e51a4302e2 100644--- a/saleor/graphql/core/mutations.py+++ b/saleor/graphql/core/mutations.py@@ -256,7 +256,7 @@ class BaseMutation(graphene.Mutation):@classmethoddef check_permissions(cls, context, permissions=None):- """Determine whether user or service account has rights to perform this mutation.+ """Determine whether user or app has rights to perform this mutation.Default implementation assumes that account is allowed to perform anymutation. By overriding this method or defining required permissionsdiff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex d234f079ee..bdfd6294ee 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -5076,7 +5076,8 @@ type Webhook implements Node {secretKey: Stringid: ID!events: [WebhookEvent!]!- serviceAccount: ServiceAccount!+ serviceAccount: ServiceAccount! @deprecated(reason: "Use the `app` field instead. This field will be removed after 2020-07-31.")+ app: App!}type WebhookCountableConnection {@@ -5165,6 +5166,7 @@ enum WebhookSortField {NAMESERVICE_ACCOUNTTARGET_URL+ APP}input WebhookSortingInput {diff --git a/saleor/graphql/webhook/mutations.py b/saleor/graphql/webhook/mutations.pyindex f79881e145..d34d74029e 100644--- a/saleor/graphql/webhook/mutations.py+++ b/saleor/graphql/webhook/mutations.py@@ -18,7 +18,8 @@ class WebhookCreateInput(graphene.InputObjectType):)service_account = graphene.ID(required=False,- description="ID of the service account to which webhook belongs.",+ description="DEPRECATED: Use the `app` field instead. This field will be "+ "removed after 2020-07-31.",)app = graphene.ID(required=False, description="ID of the app to which webhook belongs.",@@ -108,7 +109,8 @@ class WebhookUpdateInput(graphene.InputObjectType):)service_account = graphene.ID(required=False,- description="ID of the service account to which webhook belongs.",+ description="DEPRECATED: Use the `app` field instead. This field will be "+ "removed after 2020-07-31.",)app = graphene.ID(required=False, description="ID of the app to which webhook belongs.",@@ -189,7 +191,7 @@ class WebhookDelete(ModelDeleteMutation):if app:if not app.is_active:raise ValidationError(- "Service account needs to be active to delete webhook",+ "App needs to be active to delete webhook",code=WebhookErrorCode.INVALID,)try:diff --git a/saleor/graphql/webhook/sorters.py b/saleor/graphql/webhook/sorters.pyindex 3bb63c6815..bd190d9223 100644--- a/saleor/graphql/webhook/sorters.py+++ b/saleor/graphql/webhook/sorters.py@@ -7,6 +7,7 @@ class WebhookSortField(graphene.Enum):NAME = "name"SERVICE_ACCOUNT = "app__name"TARGET_URL = "target_url"+ APP = "app__name"@propertydef description(self):diff --git a/saleor/graphql/webhook/types.py b/saleor/graphql/webhook/types.pyindex 6d1a3a95fc..3976446beb 100644--- a/saleor/graphql/webhook/types.py+++ b/saleor/graphql/webhook/types.py@@ -4,6 +4,7 @@ import graphene_django_optimizer as gql_optimizerfrom ...webhook import modelsfrom ...webhook.event_types import WebhookEventTypefrom ..account.deprecated.types import ServiceAccount+from ..app.types import Appfrom ..core.connection import CountableDjangoObjectTypefrom .enums import WebhookEventTypeEnum@@ -34,7 +35,14 @@ class Webhook(CountableDjangoObjectType):),model_field="events",)- service_account = graphene.Field(ServiceAccount, required=True)+ service_account = graphene.Field(+ ServiceAccount,+ required=True,+ deprecation_reason=(+ "Use the `app` field instead. This field will be removed after 2020-07-31."+ ),+ )+ app = graphene.Field(App, required=True)class Meta:description = "Webhook."diff --git a/tests/api/pagination/test_webhook.py b/tests/api/pagination/test_webhook.pyindex 3d418ca5fa..27af3a2ad5 100644--- a/tests/api/pagination/test_webhook.py+++ b/tests/api/pagination/test_webhook.py@@ -77,7 +77,7 @@ QUERY_WEBHOOKS_PAGINATION = """["WebhookWebhook2", "WebhookWebhook1", "Webhook3"],),(- {"field": "SERVICE_ACCOUNT", "direction": "ASC"},+ {"field": "APP", "direction": "ASC"},["Webhook3", "WebhookWebhook1", "Webhook2"],),(diff --git a/tests/api/test_account_service_account.py b/tests/api/test_account_depreceated_service_account.pysimilarity index 98%rename from tests/api/test_account_service_account.pyrename to tests/api/test_account_depreceated_service_account.pyindex 1850d6d9ea..9381d45ad8 100644--- a/tests/api/test_account_service_account.py+++ b/tests/api/test_account_depreceated_service_account.py@@ -1,5 +1,6 @@import grapheneimport pytest+from django.contrib.auth.models import Permissionfrom freezegun import freeze_timefrom saleor.app.models import App, AppToken@@ -7,6 +8,12 @@ from saleor.graphql.core.enums import PermissionEnumfrom .utils import assert_no_permission, get_graphql_content++def permission_manage_service_accounts():+ return Permission.objects.get(codename="manage_apps")++SERVICE_ACCOUNT_CREATE_MUTATION = """mutation ServiceAccountCreate($name: String, $is_active: Boolean $permissions: [PermissionEnum]){diff --git a/tests/api/test_app.py b/tests/api/test_app.pynew file mode 100644index 0000000000..132d868d22--- /dev/null+++ b/tests/api/test_app.py@@ -0,0 +1,419 @@+import graphene+import pytest+from freezegun import freeze_time++from saleor.app.models import App, AppToken+from saleor.graphql.core.enums import PermissionEnum++from .utils import assert_no_permission, get_graphql_content++APP_CREATE_MUTATION = """+ mutation AppCreate(+ $name: String, $is_active: Boolean $permissions: [PermissionEnum]){+ appCreate(input:+ {name: $name, isActive: $is_active, permissions: $permissions})+ {+ authToken+ app{+ permissions{+ code+ name+ }+ id+ isActive+ name+ tokens{+ authToken+ }+ }+ errors{+ field+ message+ }+ }+ }+ """+++def test_app_create_mutation(+ permission_manage_apps, permission_manage_products, staff_api_client, staff_user,+):+ query = APP_CREATE_MUTATION+ staff_user.user_permissions.add(permission_manage_apps)++ variables = {+ "name": "New integration",+ "is_active": True,+ "permissions": [PermissionEnum.MANAGE_PRODUCTS.name],+ }+ response = staff_api_client.post_graphql(query, variables=variables)+ content = get_graphql_content(response)+ app_data = content["data"]["appCreate"]["app"]+ default_token = content["data"]["appCreate"]["authToken"]+ app = App.objects.get()+ assert app_data["isActive"] == app.is_active+ assert app_data["name"] == app.name+ assert list(app.permissions.all()) == [permission_manage_products]+ assert default_token == app.tokens.get().auth_token+++def test_app_create_mutation_no_permissions(+ permission_manage_apps, permission_manage_products, staff_api_client, staff_user,+):+ query = APP_CREATE_MUTATION+ variables = {+ "name": "New integration",+ "is_active": True,+ "permissions": [PermissionEnum.MANAGE_PRODUCTS.name],+ }+ response = staff_api_client.post_graphql(query, variables=variables)+ assert_no_permission(response)+++APP_UPDATE_MUTATION = """+mutation AppUpdate($id: ID!, $is_active: Boolean,+ $permissions: [PermissionEnum]){+ appUpdate(id: $id,+ input:{isActive: $is_active, permissions:$permissions}){+ app{+ isActive+ id+ permissions{+ code+ name+ }+ tokens{+ authToken+ }+ name+ }+ errors{+ field+ message+ }+ }+}+"""+++def test_app_update_mutation(+ app,+ permission_manage_apps,+ permission_manage_products,+ permission_manage_users,+ staff_api_client,+ staff_user,+):+ query = APP_UPDATE_MUTATION+ staff_user.user_permissions.add(permission_manage_apps)+ id = graphene.Node.to_global_id("App", app.id)++ variables = {+ "id": id,+ "is_active": False,+ "permissions": [+ PermissionEnum.MANAGE_PRODUCTS.name,+ PermissionEnum.MANAGE_USERS.name,+ ],+ }+ response = staff_api_client.post_graphql(query, variables=variables)+ content = get_graphql_content(response)++ app_data = content["data"]["appUpdate"]["app"]+ tokens_data = app_data["tokens"]+ app.refresh_from_db()+ tokens = app.tokens.all()++ assert app_data["isActive"] == app.is_active+ assert app.is_active is False+ assert len(tokens_data) == 1+ assert tokens_data[0]["authToken"] == tokens.get().auth_token[-4:]+ assert set(app.permissions.all()) == {+ permission_manage_products,+ permission_manage_users,+ }+++def test_app_update_no_permission(app, staff_api_client, staff_user):+ query = APP_UPDATE_MUTATION+ id = graphene.Node.to_global_id("App", app.id)+ variables = {+ "id": id,+ "is_active": False,+ "permissions": [PermissionEnum.MANAGE_PRODUCTS.name],+ }+ response = staff_api_client.post_graphql(query, variables=variables)+ assert_no_permission(response)+++QUERY_APPS_WITH_FILTER = """+ query ($filter: AppFilterInput ){+ apps(first: 5, filter: $filter){+ edges{+ node{+ id+ isActive+ permissions{+ name+ code+ }+ tokens{+ authToken+ }+ name+ }+ }+ }+ }+ """+++ "app_filter, count", (({"search": "Sample"}, 1), ({"isActive": False}, 1), ({}, 2)),+)+def test_apps_query(+ staff_api_client, permission_manage_apps, app, app_filter, count,+):+ second_app = App.objects.create(name="Simple service")+ second_app.is_active = False+ second_app.tokens.create(name="default")+ second_app.save()++ variables = {"filter": app_filter}+ response = staff_api_client.post_graphql(+ QUERY_APPS_WITH_FILTER, variables, permissions=[permission_manage_apps],+ )+ content = get_graphql_content(response)++ apps_data = content["data"]["apps"]["edges"]+ for app_data in apps_data:+ tokens = app_data["node"]["tokens"]+ assert len(tokens) == 1+ assert len(tokens[0]["authToken"]) == 4+ assert len(apps_data) == count+++QUERY_APPS_WITH_SORT = """+ query ($sort_by: AppSortingInput!) {+ apps(first:5, sortBy: $sort_by) {+ edges{+ node{+ name+ }+ }+ }+ }+"""+++ "apps_sort, result_order",+ [+ ({"field": "NAME", "direction": "ASC"}, ["facebook", "google"]),+ ({"field": "NAME", "direction": "DESC"}, ["google", "facebook"]),+ ({"field": "CREATION_DATE", "direction": "ASC"}, ["google", "facebook"]),+ ({"field": "CREATION_DATE", "direction": "DESC"}, ["facebook", "google"]),+ ],+)+def test_query_apps_with_sort(+ apps_sort, result_order, staff_api_client, permission_manage_apps,+):+ with freeze_time("2018-05-31 12:00:01"):+ App.objects.create(name="google", is_active=True)+ with freeze_time("2019-05-31 12:00:01"):+ App.objects.create(name="facebook", is_active=True)+ variables = {"sort_by": apps_sort}+ staff_api_client.user.user_permissions.add(permission_manage_apps)+ response = staff_api_client.post_graphql(QUERY_APPS_WITH_SORT, variables)+ content = get_graphql_content(response)+ apps = content["data"]["apps"]["edges"]++ for order, account_name in enumerate(result_order):+ assert apps[order]["node"]["name"] == account_name+++def test_apps_query_no_permission(+ staff_api_client, permission_manage_users, permission_manage_staff, app+):+ variables = {"filter": {}}+ response = staff_api_client.post_graphql(+ QUERY_APPS_WITH_FILTER, variables, permissions=[]+ )+ assert_no_permission(response)++ response = staff_api_client.post_graphql(+ QUERY_APPS_WITH_FILTER,+ variables,+ permissions=[permission_manage_users, permission_manage_staff],+ )+ assert_no_permission(response)+++QUERY_APP = """+ query ($id: ID! ){+ app(id: $id){+ id+ created+ isActive+ permissions{+ code+ name+ }+ tokens{+ authToken+ }+ name+ }+ }+ """+++def test_app_query(+ staff_api_client, permission_manage_apps, permission_manage_staff, app,+):+ app.permissions.add(permission_manage_staff)++ id = graphene.Node.to_global_id("App", app.id)+ variables = {"id": id}+ response = staff_api_client.post_graphql(+ QUERY_APP, variables, permissions=[permission_manage_apps],+ )+ content = get_graphql_content(response)++ tokens = app.tokens.all()+ app_data = content["data"]["app"]+ tokens_data = app_data["tokens"]+ assert tokens.count() == 1+ assert tokens_data[0]["authToken"] == tokens.first().auth_token[-4:]++ assert app_data["isActive"] == app.is_active+ assert app_data["permissions"] == [+ {"code": "MANAGE_STAFF", "name": "Manage staff."}+ ]+++def test_app_query_no_permission(+ staff_api_client, permission_manage_staff, permission_manage_users, app+):+ app.permissions.add(permission_manage_staff)++ id = graphene.Node.to_global_id("App", app.id)+ variables = {"id": id}+ response = staff_api_client.post_graphql(QUERY_APP, variables, permissions=[])+ assert_no_permission(response)++ response = staff_api_client.post_graphql(+ QUERY_APP,+ variables,+ permissions=[permission_manage_users, permission_manage_staff],+ )+ assert_no_permission(response)+++def test_app_with_access_to_resources(+ app_api_client, app, permission_manage_orders, order_with_lines,+):+ query = """+ query {+ orders(first: 5) {+ edges {+ node {+ id+ }+ }+ }+ }+ """+ response = app_api_client.post_graphql(query)+ assert_no_permission(response)+ response = app_api_client.post_graphql(+ query, permissions=[permission_manage_orders]+ )+ get_graphql_content(response)+++APP_TOKEN_CREATE_MUTATION = """+mutation appTokenCreate($input: AppTokenInput!) {+ appTokenCreate(input: $input){+ authToken+ appToken{+ name+ authToken+ id+ }+ errors{+ field+ message+ }+ }+}+"""+++def test_app_token_create(permission_manage_apps, staff_api_client, staff_user):++ app = App.objects.create(name="New_sa")+ query = APP_TOKEN_CREATE_MUTATION+ staff_user.user_permissions.add(permission_manage_apps)++ id = graphene.Node.to_global_id("App", app.id)+ variables = {"name": "Default token", "app": id}+ response = staff_api_client.post_graphql(query, variables={"input": variables})+ content = get_graphql_content(response)+ token_data = content["data"]["appTokenCreate"]["appToken"]+ auth_token_data = content["data"]["appTokenCreate"]["authToken"]+ auth_token = app.tokens.get().auth_token+ assert auth_token_data == auth_token++ assert token_data["authToken"] == auth_token[-4:]+ assert token_data["name"] == "Default token"+++def test_app_token_create_no_permissions(staff_api_client, staff_user):+ app = App.objects.create(name="New_app")+ query = APP_TOKEN_CREATE_MUTATION+ id = graphene.Node.to_global_id("App", app.id)+ variables = {"name": "Default token", "app": id}+ response = staff_api_client.post_graphql(query, variables={"input": variables})+ assert_no_permission(response)+++APP_TOKEN_DELETE_MUTATION = """+ mutation appTokenDelete($id: ID!){+ appTokenDelete(id: $id){+ errors{+ field+ message+ }+ appToken{+ name+ authToken+ }+ }+ }+"""+++def test_app_token_delete(permission_manage_apps, staff_api_client, staff_user, app):++ query = APP_TOKEN_DELETE_MUTATION+ token = app.tokens.get()+ id = graphene.Node.to_global_id("AppToken", token.id)+ staff_user.user_permissions.add(permission_manage_apps)++ variables = {"id": id}+ response = staff_api_client.post_graphql(query, variables=variables)+ get_graphql_content(response)+ assert not AppToken.objects.filter(id=token.id).first()+++def test_app_token_delete_no_permissions(staff_api_client, staff_user, app):++ query = APP_TOKEN_DELETE_MUTATION+ token = app.tokens.get()+ id = graphene.Node.to_global_id("AppToken", token.id)++ variables = {"id": id}+ response = staff_api_client.post_graphql(query, variables=variables)+ assert_no_permission(response)+ token.refresh_from_db()diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex f982012fc1..aa6558e225 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -1806,7 +1806,7 @@ def test_query_anonymous_customer_checkout_as_staff_user(assert content["data"]["checkout"]["token"] == str(checkout.token)-def test_query_anonymous_customer_checkout_as_service_account(+def test_query_anonymous_customer_checkout_as_app(app_api_client, checkout, permission_manage_checkouts):variables = {"token": str(checkout.token)}@@ -1867,7 +1867,7 @@ def test_query_customer_checkout_as_staff_user(assert content["data"]["checkout"]["token"] == str(checkout.token)-def test_query_customer_checkout_as_service_account(+def test_query_customer_checkout_as_app(staff_api_client, checkout, customer_user, permission_manage_checkouts):checkout.user = customer_userdiff --git a/tests/api/test_middleware.py b/tests/api/test_middleware.pyindex e494315006..46f9fc5146 100644--- a/tests/api/test_middleware.py+++ b/tests/api/test_middleware.py@@ -5,7 +5,7 @@ from django.urls import reversefrom saleor.graphql.middleware import app_middleware-def test_service_account_middleware_accepts_api_requests(app, rf):+def test_app_middleware_accepts_api_requests(app, rf):# Retrieve sample request objectrequest = rf.get(reverse("api"))diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 5405ea06cb..fa18144cc9 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -503,9 +503,7 @@ def test_non_staff_user_cannot_only_see_his_order(user_api_client, order):assert_no_permission(response)-def test_query_order_as_service_account(- app_api_client, permission_manage_orders, order-):+def test_query_order_as_app(app_api_client, permission_manage_orders, order):query = """query OrderQuery($id: ID!) {order(id: $id) {diff --git a/tests/api/test_webhook.py b/tests/api/test_webhook.pyindex 4e8b7805bd..038fe4d273 100644--- a/tests/api/test_webhook.py+++ b/tests/api/test_webhook.py@@ -13,7 +13,7 @@ from saleor.webhook.models import Webhookfrom .utils import assert_no_permission, get_graphql_content-WEBHOOK_CREATE_BY_SERVICE_ACCOUNT = """+WEBHOOK_CREATE_BY_APP = """mutation webhookCreate($name: String, $target_url: String,$events: [WebhookEventTypeEnum]){webhookCreate(input:{name: $name, targetUrl:$target_url, events:$events}){@@ -29,8 +29,8 @@ WEBHOOK_CREATE_BY_SERVICE_ACCOUNT = """"""-def test_webhook_create_by_service_account(app_api_client, permission_manage_orders):- query = WEBHOOK_CREATE_BY_SERVICE_ACCOUNT+def test_webhook_create_by_app(app_api_client, permission_manage_orders):+ query = WEBHOOK_CREATE_BY_APPvariables = {"name": "New integration","target_url": "https://www.example.com",@@ -54,12 +54,10 @@ def test_webhook_create_by_service_account(app_api_client, permission_manage_ordassert events[0].event_type == WebhookEventTypeEnum.ORDER_CREATED.value-def test_webhook_create_inactive_service_account(- app_api_client, app, permission_manage_orders-):+def test_webhook_create_inactive_app(app_api_client, app, permission_manage_orders):app.is_active = Falseapp.save()- query = WEBHOOK_CREATE_BY_SERVICE_ACCOUNT+ query = WEBHOOK_CREATE_BY_APPvariables = {"target_url": "https://www.example.com","events": [WebhookEventTypeEnum.ORDER_CREATED.name],@@ -72,10 +70,10 @@ def test_webhook_create_inactive_service_account(assert_no_permission(response)-def test_webhook_create_without_service_account(app_api_client, app):+def test_webhook_create_without_app(app_api_client, app):app_api_client.app = Noneapp_api_client.app_token = None- query = WEBHOOK_CREATE_BY_SERVICE_ACCOUNT+ query = WEBHOOK_CREATE_BY_APPvariables = {"target_url": "https://www.example.com","events": [WebhookEventTypeEnum.ORDER_CREATED.name],@@ -85,10 +83,10 @@ def test_webhook_create_without_service_account(app_api_client, app):assert_no_permission(response)-def test_webhook_create_service_account_doesnt_exist(app_api_client, app):+def test_webhook_create_app_doesnt_exist(app_api_client, app):app.delete()- query = WEBHOOK_CREATE_BY_SERVICE_ACCOUNT+ query = WEBHOOK_CREATE_BY_APPvariables = {"target_url": "https://www.example.com","events": [WebhookEventTypeEnum.ORDER_CREATED.name],@@ -100,9 +98,9 @@ def test_webhook_create_service_account_doesnt_exist(app_api_client, app):WEBHOOK_CREATE_BY_STAFF = """mutation webhookCreate(- $target_url: String, $events: [WebhookEventTypeEnum], $service_account: ID){+ $target_url: String, $events: [WebhookEventTypeEnum], $app: ID){webhookCreate(input:{- targetUrl:$target_url, events:$events, serviceAccount: $service_account}){+ targetUrl:$target_url, events:$events, app: $app}){errors{fieldmessage@@ -120,11 +118,11 @@ def test_webhook_create_by_staff():query = WEBHOOK_CREATE_BY_STAFFapp.permissions.add(permission_manage_orders)- service_account_id = graphene.Node.to_global_id("ServiceAccount", app.pk)+ app_id = graphene.Node.to_global_id("App", app.pk)variables = {"target_url": "https://www.example.com","events": [WebhookEventTypeEnum.ORDER_CREATED.name],- "service_account": service_account_id,+ "app": app_id,}staff_api_client.user.user_permissions.add(permission_manage_webhooks)response = staff_api_client.post_graphql(query, variables=variables)@@ -137,14 +135,14 @@ def test_webhook_create_by_staff(assert events[0].event_type == WebhookEventTypeEnum.ORDER_CREATED.value-def test_webhook_create_by_staff_with_inactive_service_account(staff_api_client, app):+def test_webhook_create_by_staff_with_inactive_app(staff_api_client, app):app.is_active = Falsequery = WEBHOOK_CREATE_BY_STAFF- service_account_id = graphene.Node.to_global_id("ServiceAccount", app.pk)+ app_id = graphene.Node.to_global_id("App", app.pk)variables = {"target_url": "https://www.example.com","events": [WebhookEventTypeEnum.ORDER_CREATED.name],- "service_account": service_account_id,+ "app": app_id,}response = staff_api_client.post_graphql(query, variables=variables)assert_no_permission(response)@@ -153,18 +151,18 @@ def test_webhook_create_by_staff_with_inactive_service_account(staff_api_client,def test_webhook_create_by_staff_without_permission(staff_api_client, app):query = WEBHOOK_CREATE_BY_STAFF- service_account_id = graphene.Node.to_global_id("ServiceAccount", app.pk)+ app_id = graphene.Node.to_global_id("App", app.pk)variables = {"target_url": "https://www.example.com","events": [WebhookEventTypeEnum.ORDER_CREATED.name],- "service_account": service_account_id,+ "app": app_id,}response = staff_api_client.post_graphql(query, variables=variables)assert_no_permission(response)assert Webhook.objects.count() == 0-WEBHOOK_DELETE_BY_SERVICE_ACCOUNT = """+WEBHOOK_DELETE_BY_APP = """mutation webhookDelete($id: ID!){webhookDelete(id: $id){webhookErrors{@@ -177,8 +175,8 @@ WEBHOOK_DELETE_BY_SERVICE_ACCOUNT = """"""-def test_webhook_delete_by_service_account(app_api_client, webhook):- query = WEBHOOK_DELETE_BY_SERVICE_ACCOUNT+def test_webhook_delete_by_app(app_api_client, webhook):+ query = WEBHOOK_DELETE_BY_APPwebhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)variables = {"id": webhook_id}response = app_api_client.post_graphql(query, variables=variables)@@ -186,12 +184,14 @@ def test_webhook_delete_by_service_account(app_api_client, webhook):assert Webhook.objects.count() == 0-def test_webhook_delete_by_service_account_with_wrong_webhook(app_api_client, webhook):+def test_webhook_delete_by_app_and_webhook_assigned_to_other_app(+ app_api_client, webhook+):second_app = App.objects.create(name="second")webhook.app = second_appwebhook.save()- query = WEBHOOK_DELETE_BY_SERVICE_ACCOUNT+ query = WEBHOOK_DELETE_BY_APPwebhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)variables = {"id": webhook_id}response = app_api_client.post_graphql(query, variables=variables)@@ -201,8 +201,8 @@ def test_webhook_delete_by_service_account_with_wrong_webhook(app_api_client, weassert Webhook.objects.count() == 1-def test_webhook_delete_by_service_account_wrong_webhook(app_api_client, webhook):- query = WEBHOOK_DELETE_BY_SERVICE_ACCOUNT+def test_webhook_delete_by_app_and_missing_webhook(app_api_client, webhook):+ query = WEBHOOK_DELETE_BY_APPwebhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)variables = {"id": webhook_id}webhook.delete()@@ -213,21 +213,21 @@ def test_webhook_delete_by_service_account_wrong_webhook(app_api_client, webhookassert errors[0]["code"] == "GRAPHQL_ERROR"-def test_webhook_delete_by_inactive_service_account(app_api_client, webhook):+def test_webhook_delete_by_inactive_app(app_api_client, webhook):app = webhook.appapp.is_active = Falseapp.save()- query = WEBHOOK_DELETE_BY_SERVICE_ACCOUNT+ query = WEBHOOK_DELETE_BY_APPwebhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)variables = {"id": webhook_id}response = app_api_client.post_graphql(query, variables=variables)assert_no_permission(response)-def test_webhook_delete_service_account_doesnt_exist(app_api_client, app):+def test_webhook_delete_when_app_doesnt_exist(app_api_client, app):app.delete()- query = WEBHOOK_DELETE_BY_SERVICE_ACCOUNT+ query = WEBHOOK_DELETE_BY_APPwebhook_id = graphene.Node.to_global_id("Webhook", 1)variables = {"id": webhook_id}response = app_api_client.post_graphql(query, variables=variables)@@ -253,7 +253,7 @@ WEBHOOK_UPDATE = """"""-def test_webhook_update_not_allowed_by_service_account(app_api_client, app, webhook):+def test_webhook_update_not_allowed_by_app(app_api_client, app, webhook):query = WEBHOOK_UPDATEwebhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)variables = {@@ -308,7 +308,7 @@ QUERY_WEBHOOKS = """webhooks(first:5, filter: $filter){edges{node{- serviceAccount{+ app{id}targetUrl@@ -342,7 +342,7 @@ def test_query_webhooks_by_staff_without_permission(staff_api_client, webhook):assert_no_permission(response)-def test_query_webhooks_by_service_account(app_api_client, webhook):+def test_query_webhooks_by_app(app_api_client, webhook):second_app = App.objects.create(name="Sample app", is_active=True)second_webhook = Webhook.objects.create(app=second_app, target_url="http://www.example.com/test"@@ -412,14 +412,8 @@ QUERY_WEBHOOKS_WITH_SORT = """[({"field": "NAME", "direction": "ASC"}, ["backup", "hook1", "hook2"]),({"field": "NAME", "direction": "DESC"}, ["hook2", "hook1", "backup"]),- (- {"field": "SERVICE_ACCOUNT", "direction": "ASC"},- ["hook2", "backup", "hook1"],- ),- (- {"field": "SERVICE_ACCOUNT", "direction": "DESC"},- ["backup", "hook1", "hook2"],- ),+ ({"field": "APP", "direction": "ASC"}, ["hook2", "backup", "hook1"],),+ ({"field": "APP", "direction": "DESC"}, ["backup", "hook1", "hook2"],),({"field": "TARGET_URL", "direction": "ASC"}, ["hook2", "hook1", "backup"]),({"field": "TARGET_URL", "direction": "DESC"}, ["backup", "hook1", "hook2"]),],@@ -450,7 +444,7 @@ def test_query_webhooks_with_sort(assert webhooks[order]["node"]["name"] == webhook_name-def test_query_webhooks_by_service_account_without_permissions(app_api_client,):+def test_query_webhooks_by_app_without_permissions(app_api_client,):second_app = App.objects.create(name="Sample app account", is_active=True)second_webhook = Webhook.objects.create(app=second_app, target_url="http://www.example.com/test"@@ -503,7 +497,7 @@ def test_query_webhook_by_staff_without_permission(staff_api_client, webhook):assert_no_permission(response)-def test_query_webhook_by_service_account(app_api_client, webhook):+def test_query_webhook_by_app(app_api_client, webhook):query = QUERY_WEBHOOKwebhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)@@ -519,7 +513,7 @@ def test_query_webhook_by_service_account(app_api_client, webhook):assert events[0].event_type == WebhookEventTypeEnum.ORDER_CREATED.value-def test_query_webhook_by_service_account_without_permission(app_api_client,):+def test_query_webhook_by_app_without_permission(app_api_client,):second_app = App.objects.create(name="Sample app account", is_active=True)webhook = Webhook.objects.create(app=second_app, target_url="http://www.example.com/test"@@ -585,7 +579,7 @@ SAMPLE_PAYLOAD_QUERY = """(WebhookSampleEventTypeEnum.FULFILLMENT_CREATED, True),],)-def test_sample_payload_query_by_service_account(+def test_sample_payload_query_by_app(mock_generate_sample_payload,event_type,has_access,diff --git a/tests/api/test_webhook_deprecated_service_account.py b/tests/api/test_webhook_deprecated_service_account.pynew file mode 100644index 0000000000..646d492519--- /dev/null+++ b/tests/api/test_webhook_deprecated_service_account.py@@ -0,0 +1,113 @@+import graphene+import pytest++from saleor.app.models import App+from saleor.graphql.webhook.enums import WebhookEventTypeEnum+from saleor.webhook.models import Webhook++from .test_webhook import QUERY_WEBHOOKS_WITH_SORT+from .utils import assert_no_permission, get_graphql_content++WEBHOOK_CREATE_BY_STAFF = """+ mutation webhookCreate(+ $target_url: String, $events: [WebhookEventTypeEnum], $service_account: ID){+ webhookCreate(input:{+ targetUrl:$target_url, events:$events, serviceAccount: $service_account}){+ errors{+ field+ message+ }+ webhook{+ id+ }+ }+ }+"""+++def test_webhook_create_by_staff_and_service_account(+ staff_api_client, app, permission_manage_webhooks, permission_manage_orders,+):+ query = WEBHOOK_CREATE_BY_STAFF+ app.permissions.add(permission_manage_orders)+ service_account_id = graphene.Node.to_global_id("ServiceAccount", app.pk)+ variables = {+ "target_url": "https://www.example.com",+ "events": [WebhookEventTypeEnum.ORDER_CREATED.name],+ "service_account": service_account_id,+ }+ staff_api_client.user.user_permissions.add(permission_manage_webhooks)+ response = staff_api_client.post_graphql(query, variables=variables)+ get_graphql_content(response)+ new_webhook = Webhook.objects.get()+ assert new_webhook.target_url == "https://www.example.com"+ assert new_webhook.app == app+ events = new_webhook.events.all()+ assert len(events) == 1+ assert events[0].event_type == WebhookEventTypeEnum.ORDER_CREATED.value+++def test_webhook_create_by_staff_with_inactive_service_account(staff_api_client, app):+ app.is_active = False+ query = WEBHOOK_CREATE_BY_STAFF+ service_account_id = graphene.Node.to_global_id("ServiceAccount", app.pk)+ variables = {+ "target_url": "https://www.example.com",+ "events": [WebhookEventTypeEnum.ORDER_CREATED.name],+ "service_account": service_account_id,+ }+ response = staff_api_client.post_graphql(query, variables=variables)+ assert_no_permission(response)+ assert Webhook.objects.count() == 0+++def test_webhook_create_by_staff_for_sa_without_permission(staff_api_client, app):+ query = WEBHOOK_CREATE_BY_STAFF+ service_account_id = graphene.Node.to_global_id("ServiceAccount", app.pk)+ variables = {+ "target_url": "https://www.example.com",+ "events": [WebhookEventTypeEnum.ORDER_CREATED.name],+ "service_account": service_account_id,+ }+ response = staff_api_client.post_graphql(query, variables=variables)+ assert_no_permission(response)+ assert Webhook.objects.count() == 0+++ "webhooks_sort, result_order",+ [+ (+ {"field": "SERVICE_ACCOUNT", "direction": "ASC"},+ ["hook2", "backup", "hook1"],+ ),+ (+ {"field": "SERVICE_ACCOUNT", "direction": "DESC"},+ ["backup", "hook1", "hook2"],+ ),+ ],+)+def test_query_webhooks_with_sort(+ webhooks_sort, result_order, staff_api_client, permission_manage_webhooks+):+ backupApp = App.objects.create(name="backupApp", is_active=True)+ app = App.objects.create(name="app", is_active=True)+ Webhook.objects.bulk_create(+ [+ Webhook(+ name="hook1", app=backupApp, target_url="http://example.com/activate2",+ ),+ Webhook(name="hook2", app=app, target_url="http://example.com/activate",),+ Webhook(+ name="backup", app=backupApp, target_url="http://example.com/backup",+ ),+ ]+ )+ variables = {"sort_by": webhooks_sort}+ staff_api_client.user.user_permissions.add(permission_manage_webhooks)+ response = staff_api_client.post_graphql(QUERY_WEBHOOKS_WITH_SORT, variables)+ content = get_graphql_content(response)+ webhooks = content["data"]["webhooks"]["edges"]++ for order, webhook_name in enumerate(result_order):+ assert webhooks[order]["node"]["name"] == webhook_namediff --git a/tests/conftest.py b/tests/conftest.pyindex 3535638151..acdb35f5ef 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -578,11 +578,6 @@ def permission_manage_plugins():return Permission.objects.get(codename="manage_plugins")-def permission_manage_service_accounts():- return Permission.objects.get(codename="manage_apps")--@pytest.fixturedef permission_manage_apps():return Permission.objects.get(codename="manage_apps")