Skip to content

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

Add "author" field to the app manifest and App model (#12166)

Przemysław Łada444b48f0a69a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 99f5988ac6..5f70a2b4ad 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,10 +9,10 @@ All notable, unreleased changes to this project will be documented in this file.
### GraphQL API
- [Preview] Add `StockBulkUpdate` mutation - #12139 by @SzymJ
- Upgrade GraphiQL to `2.4.0` for playground - #12271 by @zaiste
-
- Add new object type `AppManifestRequiredSaleorVersion` - #12164 by @przlada
- Add new optional field `Manifest.requiredSaleorVersion`
- Add `requiredSaleorVersion` validation to `appInstall` and `appFetchManifest` mutations
+- Add new field `author` to `Manifest` and `App` object types - #12166 by @przlada
### Other changes
- Create order discounts for all voucher types - #12272 by @IKarbowiak
@@ -20,6 +20,7 @@ All notable, unreleased changes to this project will be documented in this file.
### Saleor Apps
- Add `requiredSaleorVersion` field to the App manifest determining the required Saleor version as semver range - #12164 by @przlada
+- Add new field `author` to the App manifest - #12166 by @przlada
# 3.12.0
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.py
index 2a3520226d..24bef813fc 100644
--- a/saleor/app/installation_utils.py
+++ b/saleor/app/installation_utils.py
@@ -73,6 +73,7 @@ def install_app(app_installation: AppInstallation, activate: bool = False):
type=AppType.THIRDPARTY,
audience=manifest_data.get("audience"),
is_installed=False,
+ author=manifest_data.get("author"),
)
app.permissions.set(app_installation.permissions.all())
diff --git a/saleor/app/manifest_validations.py b/saleor/app/manifest_validations.py
index 1ac8f3f070..2efc96faab 100644
--- a/saleor/app/manifest_validations.py
+++ b/saleor/app/manifest_validations.py
@@ -119,6 +119,11 @@ def clean_manifest_data(manifest_data, raise_for_saleor_version=False):
except ValidationError as e:
errors["requiredSaleorVersion"].append(e)
+ try:
+ manifest_data["author"] = clean_author(manifest_data.get("author"))
+ except ValidationError as e:
+ errors["author"].append(e)
+
saleor_permissions = get_permissions().annotate(
formated_codename=Concat("content_type__app_label", Value("."), "codename")
)
@@ -324,3 +329,14 @@ def clean_required_saleor_version(
msg = f"Saleor version {saleor_version} is not supported by the app."
raise ValidationError(msg, code=AppErrorCode.UNSUPPORTED_SALEOR_VERSION.value)
return {"constraint": required_version, "satisfied": satisfied}
+
+
+def clean_author(author) -> Optional[str]:
+ if author is None:
+ return None
+ if isinstance(author, str):
+ if clean := author.strip():
+ return clean
+ raise ValidationError(
+ "Incorrect value for field: author", code=AppErrorCode.INVALID.value
+ )
diff --git a/saleor/app/migrations/0021_app_author.py b/saleor/app/migrations/0021_app_author.py
new file mode 100644
index 0000000000..b5fcbf3c12
--- /dev/null
+++ b/saleor/app/migrations/0021_app_author.py
@@ -0,0 +1,17 @@
+# Generated by Django 3.2.18 on 2023-03-13 13:03
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("app", "0020_app_is_installed"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="app",
+ name="author",
+ field=models.CharField(blank=True, max_length=60, null=True),
+ ),
+ ]
diff --git a/saleor/app/models.py b/saleor/app/models.py
index 567ba40ba6..f8fd83727a 100644
--- a/saleor/app/models.py
+++ b/saleor/app/models.py
@@ -59,6 +59,7 @@ class App(ModelWithMetadata):
version = models.CharField(max_length=60, blank=True, null=True)
audience = models.CharField(blank=True, null=True, max_length=256)
is_installed = models.BooleanField(default=True)
+ author = models.CharField(blank=True, null=True, max_length=60)
objects = AppManager()
class Meta(ModelWithMetadata.Meta):
diff --git a/saleor/app/tests/test_installation_utils.py b/saleor/app/tests/test_installation_utils.py
index 1785ad52e6..c14b96dc3d 100644
--- a/saleor/app/tests/test_installation_utils.py
+++ b/saleor/app/tests/test_installation_utils.py
@@ -132,6 +132,40 @@ def test_install_app_when_saleor_version_unsupported(
assert errors[0].code == AppErrorCode.UNSUPPORTED_SALEOR_VERSION.value
+def test_install_app_with_author(app_manifest, app_installation, monkeypatch):
+ # given
+ app_manifest["author"] = "Acme Ltd"
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+ monkeypatch.setattr(requests, "get", Mock(return_value=mocked_get_response))
+ monkeypatch.setattr("saleor.app.installation_utils.send_app_token", Mock())
+
+ # when
+ app, _ = install_app(app_installation, activate=True)
+
+ # then
+ assert App.objects.get().id == app.id
+ assert app.author == app_manifest["author"]
+
+
+def test_install_app_with_empty_author(app_manifest, app_installation, monkeypatch):
+ # given
+ app_manifest["author"] = " "
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+ monkeypatch.setattr(requests, "get", Mock(return_value=mocked_get_response))
+ monkeypatch.setattr("saleor.app.installation_utils.send_app_token", Mock())
+
+ # when
+ with pytest.raises(ValidationError) as validation_error:
+ install_app(app_installation, activate=True)
+
+ # then
+ errors = validation_error.value.error_dict["author"]
+ assert len(errors) == 1
+ assert errors[0].code == AppErrorCode.INVALID.value
+
+
@freeze_time("2022-05-12 12:00:00")
@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
diff --git a/saleor/app/tests/test_validators.py b/saleor/app/tests/test_validators.py
index 933dc92f6f..e6702b445e 100644
--- a/saleor/app/tests/test_validators.py
+++ b/saleor/app/tests/test_validators.py
@@ -4,7 +4,11 @@ from django.core.exceptions import ValidationError
from ... import __version__
from ...app.validators import AppURLValidator
from ..error_codes import AppErrorCode
-from ..manifest_validations import clean_required_saleor_version, parse_version
+from ..manifest_validations import (
+ clean_author,
+ clean_required_saleor_version,
+ parse_version,
+)
def test_validate_url():
@@ -56,3 +60,8 @@ def test_clean_required_saleor_version_raise_for_saleor_version():
with pytest.raises(ValidationError) as error:
clean_required_saleor_version("^3.13", True, "3.12.1")
assert error.value.code == AppErrorCode.UNSUPPORTED_SALEOR_VERSION.value
+
+
[email protected]("author,cleaned", [(None, None), (" Acme Ltd ", "Acme Ltd")])
+def test_clean_author(author, cleaned):
+ assert clean_author(author) == cleaned
diff --git a/saleor/graphql/app/mutations/app_fetch_manifest.py b/saleor/graphql/app/mutations/app_fetch_manifest.py
index ed986041aa..36b5942c81 100644
--- a/saleor/graphql/app/mutations/app_fetch_manifest.py
+++ b/saleor/graphql/app/mutations/app_fetch_manifest.py
@@ -72,6 +72,7 @@ class AppFetchManifest(BaseMutation):
webhooks=cleaned_data.get("webhooks", []),
audience=cleaned_data.get("audience"),
required_saleor_version=cleaned_data.get("requiredSaleorVersion"),
+ author=cleaned_data.get("author"),
)
@classmethod
diff --git a/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py b/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py
index 2d413aaaa6..c8c8b88eb0 100644
--- a/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py
+++ b/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py
@@ -30,6 +30,7 @@ mutation AppFetchManifest($manifest_url: String!){
constraint
satisfied
}
+ author
extensions{
label
url
@@ -685,3 +686,50 @@ def test_app_fetch_manifest_with_invalid_required_saleor_version(
assert len(errors) == 1
assert errors[0]["field"] == "requiredSaleorVersion"
assert errors[0]["code"] == AppErrorCode.INVALID.name
+
+
+def test_app_fetch_manifest_with_author(
+ staff_api_client, app_manifest, permission_manage_apps, monkeypatch
+):
+ # given
+ app_manifest["author"] = "Acme Ltd"
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+ monkeypatch.setattr(requests, "get", Mock(return_value=mocked_get_response))
+
+ # when
+ response = staff_api_client.post_graphql(
+ APP_FETCH_MANIFEST_MUTATION,
+ variables={"manifest_url": "http://localhost:3000/manifest"},
+ permissions=[permission_manage_apps],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ manifest = content["data"]["appFetchManifest"]["manifest"]
+ assert len(content["data"]["appFetchManifest"]["errors"]) == 0
+ assert manifest["author"] == app_manifest["author"]
+
+
+def test_app_fetch_manifest_with_empty_author(
+ staff_api_client, app_manifest, permission_manage_apps, monkeypatch
+):
+ # given
+ app_manifest["author"] = " "
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+ monkeypatch.setattr(requests, "get", Mock(return_value=mocked_get_response))
+
+ # when
+ response = staff_api_client.post_graphql(
+ APP_FETCH_MANIFEST_MUTATION,
+ variables={"manifest_url": "http://localhost:3000/manifest"},
+ permissions=[permission_manage_apps],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["appFetchManifest"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "author"
+ assert errors[0]["code"] == AppErrorCode.INVALID.name
diff --git a/saleor/graphql/app/tests/queries/test_app.py b/saleor/graphql/app/tests/queries/test_app.py
index a1ebdcb80b..ea39f13b67 100644
--- a/saleor/graphql/app/tests/queries/test_app.py
+++ b/saleor/graphql/app/tests/queries/test_app.py
@@ -32,6 +32,7 @@ QUERY_APP = """
configurationUrl
appUrl
accessToken
+ author
extensions{
id
label
@@ -72,6 +73,7 @@ def test_app_query(
app = app if app_type == "custom" else external_app
app.permissions.add(permission_manage_staff)
app.store_value_in_metadata({"test": "123"})
+ app.author = "Acme Ltd"
app.save()
webhook = webhook
@@ -105,6 +107,7 @@ def test_app_query(
assert app_data["supportUrl"] == app.support_url
assert app_data["configurationUrl"] == app.configuration_url
assert app_data["appUrl"] == app.app_url
+ assert app_data["author"] == app.author
if app_type == "external":
assert app_data["accessToken"] == create_access_token_for_app(
app, staff_api_client.user
diff --git a/saleor/graphql/app/types.py b/saleor/graphql/app/types.py
index 8e2dba10af..4c27d03427 100644
--- a/saleor/graphql/app/types.py
+++ b/saleor/graphql/app/types.py
@@ -250,6 +250,9 @@ class Manifest(graphene.ObjectType):
+ PREVIEW_FEATURE
),
)
+ author = graphene.String(
+ description=("The App's author name." + ADDED_IN_313 + PREVIEW_FEATURE)
+ )
class Meta:
description = "The manifest definition."
@@ -337,6 +340,9 @@ class App(ModelObjectType[models.App]):
access_token = graphene.String(
description="JWT token used to authenticate by thridparty app."
)
+ author = graphene.String(
+ description=("The App's author name." + ADDED_IN_313 + PREVIEW_FEATURE)
+ )
extensions = NonNullList(
AppExtension,
description="App's dashboard extensions." + ADDED_IN_31 + PREVIEW_FEATURE,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 154db7311e..6de4c14874 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2606,6 +2606,15 @@ type App implements Node & ObjectWithMetadata {
"""JWT token used to authenticate by thridparty app."""
accessToken: String
+ """
+ The App's author name.
+
+ Added in Saleor 3.13.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ author: String
+
"""
App's dashboard extensions.
@@ -23503,6 +23512,15 @@ type Manifest {
Note: this API is currently in Feature Preview and can be subject to changes at later point.
"""
requiredSaleorVersion: AppManifestRequiredSaleorVersion
+
+ """
+ The App's author name.
+
+ Added in Saleor 3.13.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ author: String
}
type AppManifestExtension {