Skip to content

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

Return info about authenticated app (#6928)

Damian Wysocki6074c93407be
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 21e3f42d01..3c8556d18e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -61,6 +61,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Deallocate stocks for order lines in a bulk way - #6896 by @IKarbowiak
- Prevent negative available quantity - #6897 by @d-wysocki
- Fix CheckoutLinesInfoByCheckoutTokenLoader dataloader - #6929 by @IKarbowiak
+- Change the `app` query to return info about the currently authenticated app - #6928 by @d-wysocki
# 2.11.1
diff --git a/saleor/graphql/app/resolvers.py b/saleor/graphql/app/resolvers.py
index 34ca1ec60f..18b67c5799 100644
--- a/saleor/graphql/app/resolvers.py
+++ b/saleor/graphql/app/resolvers.py
@@ -1,5 +1,9 @@
+import graphene
+
from ...app import models
from ...core.jwt import create_access_token_for_app
+from ...core.permissions import AppPermission
+from ..decorators import permission_required
from .enums import AppTypeEnum
@@ -19,3 +23,10 @@ def resolve_access_token(info, root, **_kwargs):
if user.is_anonymous:
return None
return create_access_token_for_app(root, user)
+
+
+@permission_required(AppPermission.MANAGE_APPS)
+def _resolve_app(info, id):
+ from .types import App
+
+ return graphene.Node.get_node_from_global_id(info, id, App)
diff --git a/saleor/graphql/app/schema.py b/saleor/graphql/app/schema.py
index 7f467d614e..a47ed0615a 100644
--- a/saleor/graphql/app/schema.py
+++ b/saleor/graphql/app/schema.py
@@ -19,7 +19,7 @@ from .mutations import (
AppTokenVerify,
AppUpdate,
)
-from .resolvers import resolve_apps, resolve_apps_installations
+from .resolvers import _resolve_app, resolve_apps, resolve_apps_installations
from .sorters import AppSortingInput
from .types import App, AppInstallation
@@ -43,8 +43,11 @@ class AppQueries(graphene.ObjectType):
)
app = graphene.Field(
App,
- id=graphene.Argument(graphene.ID, description="ID of the app.", required=True),
- description="Look up a app by ID.",
+ id=graphene.Argument(graphene.ID, description="ID of the app.", required=False),
+ description=(
+ "Look up an app by ID. "
+ "If ID is not provided, return the currently authenticated app."
+ ),
)
@permission_required(AppPermission.MANAGE_APPS)
@@ -55,9 +58,11 @@ class AppQueries(graphene.ObjectType):
def resolve_apps(self, info, **kwargs):
return resolve_apps(info, **kwargs)
- @permission_required(AppPermission.MANAGE_APPS)
- def resolve_app(self, info, id):
- return graphene.Node.get_node_from_global_id(info, id, App)
+ def resolve_app(self, info, id=None):
+ app = info.context.app
+ if not id and app:
+ return app
+ return _resolve_app(info, id)
class AppMutations(graphene.ObjectType):
diff --git a/saleor/graphql/app/tests/queries/test_app.py b/saleor/graphql/app/tests/queries/test_app.py
index afc38331e0..f4841265f2 100644
--- a/saleor/graphql/app/tests/queries/test_app.py
+++ b/saleor/graphql/app/tests/queries/test_app.py
@@ -3,12 +3,10 @@ import pytest
from freezegun import freeze_time
from .....core.jwt import create_access_token_for_app
-from .....webhook.event_types import WebhookEventType
-from .....webhook.models import Webhook
from ....tests.utils import assert_no_permission, get_graphql_content
QUERY_APP = """
- query ($id: ID! ){
+ query ($id: ID){
app(id: $id){
id
created
@@ -46,14 +44,14 @@ def test_app_query(
permission_manage_staff,
app,
external_app,
+ webhook,
):
app = app if app_type == "custom" else external_app
app.permissions.add(permission_manage_staff)
- webhook = Webhook.objects.create(
- name="Simple webhook", app=app, target_url="http://www.example.com/test"
- )
- webhook.events.create(event_type=WebhookEventType.ORDER_CREATED)
+ webhook = webhook
+ webhook.app = app
+ webhook.save()
id = graphene.Node.to_global_id("App", app.id)
variables = {"id": id}
@@ -131,3 +129,96 @@ def test_app_with_access_to_resources(
query, permissions=[permission_manage_orders]
)
get_graphql_content(response)
+
+
+def test_app_without_id_as_staff(
+ staff_api_client, app, permission_manage_apps, order_with_lines, webhook
+):
+ response = staff_api_client.post_graphql(
+ QUERY_APP, permissions=[permission_manage_apps]
+ )
+ content = get_graphql_content(response)
+ assert content["data"]["app"] is None
+
+
+def test_own_app_without_id(
+ app_api_client, app, permission_manage_orders, order_with_lines, webhook
+):
+ response = app_api_client.post_graphql(
+ QUERY_APP,
+ )
+ 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 len(app_data["webhooks"]) == 1
+ assert app_data["webhooks"][0]["name"] == webhook.name
+ assert app_data["type"] == app.type.upper()
+ assert app_data["aboutApp"] == app.about_app
+ assert app_data["dataPrivacy"] == app.data_privacy
+ assert app_data["dataPrivacyUrl"] == app.data_privacy_url
+ assert app_data["homepageUrl"] == app.homepage_url
+ assert app_data["supportUrl"] == app.support_url
+ assert app_data["configurationUrl"] == app.configuration_url
+ assert app_data["appUrl"] == app.app_url
+
+
+def test_app_query_without_permission(
+ app_api_client,
+ app,
+ order_with_lines,
+):
+ id = graphene.Node.to_global_id("App", app.id)
+ variables = {"id": id}
+ response = app_api_client.post_graphql(
+ QUERY_APP,
+ variables,
+ )
+ assert_no_permission(response)
+
+
+def test_app_query_without_permission_and_id(
+ staff_api_client,
+ app,
+ order_with_lines,
+):
+ response = staff_api_client.post_graphql(
+ QUERY_APP,
+ )
+ assert_no_permission(response)
+
+
+def test_app_query_with_permission(
+ app_api_client, permission_manage_apps, app, external_app, webhook
+):
+ app.permissions.add(permission_manage_apps)
+ id = graphene.Node.to_global_id("App", app.id)
+ variables = {"id": id}
+ response = app_api_client.post_graphql(
+ QUERY_APP,
+ variables,
+ )
+ 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 len(app_data["webhooks"]) == 1
+ assert app_data["webhooks"][0]["name"] == webhook.name
+ assert app_data["type"] == app.type.upper()
+ assert app_data["aboutApp"] == app.about_app
+ assert app_data["dataPrivacy"] == app.data_privacy
+ assert app_data["dataPrivacyUrl"] == app.data_privacy_url
+ assert app_data["homepageUrl"] == app.homepage_url
+ assert app_data["supportUrl"] == app.support_url
+ assert app_data["configurationUrl"] == app.configuration_url
+ assert app_data["appUrl"] == app.app_url
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 9eb9340684..4a5b97ca62 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -4509,7 +4509,7 @@ type Query {
attribute(id: ID, slug: String): Attribute
appsInstallations: [AppInstallation!]!
apps(filter: AppFilterInput, sortBy: AppSortingInput, before: String, after: String, first: Int, last: Int): AppCountableConnection
- app(id: ID!): App
+ app(id: ID): App
addressValidationRules(countryCode: CountryCode!, countryArea: String, city: String, cityArea: String): AddressValidationData
address(id: ID!): Address
customers(filter: CustomerFilterInput, sortBy: UserSortingInput, before: String, after: String, first: Int, last: Int): UserCountableConnection