Skip to content

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

Improve app extension validators (#17841)

Lukasz Ostrowski7be9ab204ece
diff --git a/saleor/app/manifest_validations.py b/saleor/app/manifest_validations.py
index 31eeaffc70..5fd8f2ddc4 100644
--- a/saleor/app/manifest_validations.py
+++ b/saleor/app/manifest_validations.py
@@ -50,8 +50,8 @@ def _clean_extension_url_with_only_path(
):
if target == AppExtensionTarget.APP_PAGE:
return
- if target == AppExtensionTarget.NEW_TAB:
- raise ValidationError("NEW_TAB target should be absolute path")
+ if target == AppExtensionTarget.NEW_TAB and not manifest_data["appUrl"]:
+ raise ValidationError("To use relative URL, you must specify appUrl.")
if manifest_data["appUrl"]:
_clean_app_url(manifest_data["appUrl"])
else:
@@ -73,6 +73,7 @@ def _clean_extension_url(extension: dict, manifest_data: dict):
- url cannot start with protocol when target == "APP_PAGE"
"""
extension_url = extension["url"]
+ # At this point target should be already cleaned enum AppExtensionTarget
target = extension.get("target") or AppExtensionTarget.POPUP
# Assume app URL is the one that originally received the token.
@@ -88,15 +89,16 @@ def _clean_extension_url(extension: dict, manifest_data: dict):
if not app_url:
raise ValidationError("Manifest is invalid, token_target_url is missing")
+ is_new_tab_post = target == AppExtensionTarget.NEW_TAB and new_tab_method_post
+ is_widget_post = target == AppExtensionTarget.WIDGET and widget_method_post
+
if extension_url.startswith("/"):
_clean_extension_url_with_only_path(manifest_data, target, extension_url)
elif target == AppExtensionTarget.APP_PAGE:
msg = "Url cannot start with protocol when target == APP_PAGE"
logger.warning(msg)
raise ValidationError(msg)
- elif (target == AppExtensionTarget.NEW_TAB and new_tab_method_post) or (
- target == AppExtensionTarget.WIDGET and widget_method_post
- ):
+ elif (is_new_tab_post) or is_widget_post:
parsed_app_url = urlparse(app_url)
parsed_extension_url = urlparse(extension_url)
@@ -240,19 +242,15 @@ def _clean_extension_options(extension, errors):
options = extension.get("options", {})
try:
validated_options = AppExtensionOptions.model_validate(options)
+ is_widget = extension.get("target") == AppExtensionTarget.WIDGET
+ is_new_tab = extension.get("target") == AppExtensionTarget.NEW_TAB
- if (
- validated_options.widget_target
- and extension.get("target") != AppExtensionTarget.WIDGET
- ):
+ if validated_options.widget_target and not is_widget:
raise ValidationError(
"widgetTarget options must be set only on WIDGET target"
)
- if (
- validated_options.new_tab_target
- and extension.get("target") != AppExtensionTarget.NEW_TAB
- ):
+ if validated_options.new_tab_target and not is_new_tab:
raise ValidationError(
"newTabTarget options must be set only on NEW_TAB target"
)
@@ -268,6 +266,28 @@ def _clean_extension_options(extension, errors):
)
+def _validate_mounts_for_widget(mount: str):
+ widget_available_mounts = [
+ AppExtensionMount.ORDER_DETAILS_WIDGETS,
+ AppExtensionMount.PRODUCT_DETAILS_WIDGETS,
+ AppExtensionMount.VOUCHER_DETAILS_WIDGETS,
+ AppExtensionMount.DRAFT_ORDER_DETAILS_WIDGETS,
+ AppExtensionMount.GIFT_CARD_DETAILS_WIDGETS,
+ AppExtensionMount.CUSTOMER_DETAILS_WIDGETS,
+ AppExtensionMount.COLLECTION_DETAILS_WIDGETS,
+ ]
+
+ if mount not in widget_available_mounts:
+ raise ValidationError(
+ {
+ "mount": ValidationError(
+ f"Mount {mount.upper()} is not available for WIDGET target.",
+ code=AppErrorCode.INVALID.value,
+ )
+ }
+ )
+
+
def _clean_extensions(manifest_data, app_permissions, errors):
extensions = manifest_data.get("extensions", [])
@@ -279,6 +299,12 @@ def _clean_extensions(manifest_data, app_permissions, errors):
_clean_extension_enum_field(AppExtensionMount, "mount", extension, errors)
+ try:
+ if extension["target"] == AppExtensionTarget.WIDGET:
+ _validate_mounts_for_widget(extension["mount"])
+ except ValidationError as invalid_mount_error:
+ errors["extensions"].append(invalid_mount_error)
+
try:
_clean_extension_url(extension, manifest_data)
except (ValidationError, AttributeError):
diff --git a/saleor/app/tests/test_installation_utils.py b/saleor/app/tests/test_installation_utils.py
index 6c74d292db..5a3fd65c73 100644
--- a/saleor/app/tests/test_installation_utils.py
+++ b/saleor/app/tests/test_installation_utils.py
@@ -327,7 +327,7 @@ def test_install_app_with_extension_widget(
{
"label": label,
"url": url,
- "mount": "PRODUCT_OVERVIEW_CREATE",
+ "mount": "PRODUCT_DETAILS_WIDGETS",
"permissions": ["MANAGE_PRODUCTS"],
"target": "WIDGET",
"options": options,
@@ -352,7 +352,7 @@ def test_install_app_with_extension_widget(
assert app_extension.label == label
assert app_extension.url == url
- assert app_extension.mount == AppExtensionMount.PRODUCT_OVERVIEW_CREATE
+ assert app_extension.mount == AppExtensionMount.PRODUCT_DETAILS_WIDGETS
assert app_extension.target == AppExtensionTarget.WIDGET
assert list(app_extension.permissions.all()) == [permission_manage_products]
assert app_extension.http_target_method == "POST"
diff --git a/saleor/app/tests/test_validators.py b/saleor/app/tests/test_validators.py
index 3f0bf07ab0..2384e76d5a 100644
--- a/saleor/app/tests/test_validators.py
+++ b/saleor/app/tests/test_validators.py
@@ -11,6 +11,7 @@ from ..manifest_validations import (
_clean_author,
_clean_extension_options,
_clean_extension_url,
+ _clean_extensions,
_clean_required_saleor_version,
_parse_version,
)
@@ -98,15 +99,13 @@ def test_brand_validator_with_invalid_logo_url(url):
def test_clean_extensions_new_tab_valid_relative_url(app_manifest):
+ app_manifest["appUrl"] = "https://app.example.com"
extension = {
"url": "/relative/path",
"target": AppExtensionTarget.NEW_TAB,
}
- with pytest.raises(ValidationError) as error:
- _clean_extension_url(extension, app_manifest)
-
- assert error.value.message == "NEW_TAB target should be absolute path"
+ _clean_extension_url(extension, app_manifest)
@pytest.mark.parametrize(
@@ -121,14 +120,14 @@ def test_clean_extensions_new_tab_valid_relative_url(app_manifest):
},
False,
),
- # url starts with /, target NEW_TAB, should raise
+ # url starts with /, target NEW_TAB, should not raise
(
{"url": "/tab", "target": AppExtensionTarget.NEW_TAB},
{
"tokenTargetUrl": "https://app.example.com",
"appUrl": "https://app.example.com",
},
- True,
+ False,
),
# url starts with protocol, target APP_PAGE, should raise
(
@@ -176,9 +175,27 @@ def test_clean_extension_url(extension, manifest, should_raise):
_clean_extension_url(extension, manifest)
+def test_new_tab_relative_url_without_app_url(app_manifest):
+ # given
+ app_manifest["appUrl"] = None
+
+ extension = {
+ "url": "/relative/path",
+ "target": AppExtensionTarget.NEW_TAB,
+ }
+
+ app_manifest["extensions"] = [extension]
+
+ # when & then
+ with pytest.raises(ValidationError):
+ _clean_extension_url(extension, manifest_data=app_manifest)
+
+
def test_clean_extension_url_https_only(settings):
+ # given
settings.ENABLE_SSL = True
+ # when & then
with pytest.raises(ValidationError):
_clean_extension_url(
{
@@ -194,8 +211,10 @@ def test_clean_extension_url_https_only(settings):
def test_clean_extension_url_http_if_SSL_disabled(settings):
+ # given
settings.ENABLE_SSL = False
+ # when
result = _clean_extension_url(
{"url": "http://app.example.com/page", "target": AppExtensionTarget.NEW_TAB},
{
@@ -204,10 +223,12 @@ def test_clean_extension_url_http_if_SSL_disabled(settings):
},
)
+ # then
assert result is None
def test_app_extension_options_accepts_only_one():
+ # Given
parsed = AppExtensionOptions().model_validate({"widgetTarget": {"method": "GET"}})
assert parsed.new_tab_target is None
@@ -228,8 +249,10 @@ def test_app_extension_options_accepts_only_one():
}
)
+ # when
parsed = AppExtensionOptions().model_validate({})
+ # then
assert parsed.new_tab_target is None
assert parsed.widget_target is None
@@ -405,3 +428,57 @@ def test_clean_extension_options_no_options():
assert "extensions" not in errors
assert "options" in extension
assert extension["options"] == {}
+
+
+ "mount",
+ [
+ "ORDER_DETAILS_WIDGETS",
+ "PRODUCT_DETAILS_WIDGETS",
+ "VOUCHER_DETAILS_WIDGETS",
+ "DRAFT_ORDER_DETAILS_WIDGETS",
+ "GIFT_CARD_DETAILS_WIDGETS",
+ "CUSTOMER_DETAILS_WIDGETS",
+ "COLLECTION_DETAILS_WIDGETS",
+ ],
+)
+def test_widget_target_available_mounts_valid(mount, app_manifest):
+ # Given
+ extension = {
+ "target": "WIDGET",
+ "mount": mount,
+ "url": "https://example.com/widget",
+ "label": "label",
+ }
+ errors = {"extensions": []}
+
+ app_manifest["extensions"] = [extension]
+
+ # When
+ _clean_extensions(app_manifest, [], errors)
+
+ # Then
+ assert len(errors["extensions"]) == 0
+
+
+ "mount",
+ ["CATEGORY_OVERVIEW_CREATECATEGORY_OVERVIEW_MORE_ACTIONS"],
+)
+def test_widget_target_available_mounts_invalid(mount, app_manifest):
+ # Given
+ extension = {
+ "target": "WIDGET",
+ "mount": mount,
+ "url": "https://example.com/widget",
+ "label": "label",
+ }
+ errors = {"extensions": []}
+
+ app_manifest["extensions"] = [extension]
+
+ # When
+ _clean_extensions(app_manifest, [], errors)
+
+ # Then
+ assert "extensions" in errors
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 1de2d105cf..14f0bde758 100644
--- a/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py
+++ b/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py
@@ -497,7 +497,6 @@ def test_app_fetch_manifest_extensions_correct_url(
("www.example.com/app", AppExtensionTargetEnum.POPUP.name),
("https://www.example.com/app", AppExtensionTargetEnum.APP_PAGE.name),
("http://www.example.com/app", AppExtensionTargetEnum.APP_PAGE.name),
- ("/relative-app-url", AppExtensionTargetEnum.NEW_TAB.name),
],
)
def test_app_fetch_manifest_extensions_incorrect_url(