Skip to content

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

Remove obsolete extension validation for app extensions (#18578)

Lukasz Ostrowskib356a525ba2a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61d38bfb5c..8c54505769 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,4 +20,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix send order confirmation email to staff - #18342 by @Shaokun-X
+- Validation on `AppExtension` is now removed. Saleor will accept string values for `mount` and `target` from Manifest during App installation and JSON value for `options` field.
+Validation is now performed on the frontend (Dashboard). This change increases velocity of features related to apps and extensions, now Dashboard is only entity that ensures the contract
+
### Deprecations
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.py
index dec8a7afc7..c15685c3d9 100644
--- a/saleor/app/installation_utils.py
+++ b/saleor/app/installation_utils.py
@@ -28,7 +28,7 @@ from ..webhook.models import Webhook, WebhookEvent
from .error_codes import AppErrorCode
from .manifest_validations import clean_manifest_data
from .models import App, AppExtension, AppInstallation
-from .types import AppExtensionTarget, AppType
+from .types import DEFAULT_APP_TARGET, AppType
MAX_ICON_FILE_SIZE = 1024 * 1024 * 10 # 10MB
@@ -268,10 +268,9 @@ def install_app(app_installation: AppInstallation, activate: bool = False):
label=extension_data.get("label"),
url=extension_data.get("url"),
mount=extension_data.get("mount"),
- target=extension_data.get("target", AppExtensionTarget.POPUP),
- # This field should be removed in 3.24, ENG-1110
+ target=extension_data.get("target", DEFAULT_APP_TARGET),
http_target_method=http_target_method,
- settings=extension_data.get("options"),
+ settings=extension_data.get("options", {}),
)
extension.permissions.set(extension_data.get("permissions", []))
diff --git a/saleor/app/manifest_validations.py b/saleor/app/manifest_validations.py
index cc13d769bf..90f939276c 100644
--- a/saleor/app/manifest_validations.py
+++ b/saleor/app/manifest_validations.py
@@ -1,13 +1,10 @@
import logging
from collections import defaultdict
from collections.abc import Iterable
-from urllib.parse import urlparse
-from django.conf import settings
from django.core.exceptions import ValidationError
from django.db.models import Value
from django.db.models.functions import Concat
-from pydantic import ValidationError as PydanticValidationError
from semantic_version import NpmSpec, Version
from semantic_version.base import Range
@@ -24,8 +21,8 @@ from ..webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ..webhook.validators import custom_headers_validator
from .error_codes import AppErrorCode
from .models import App
-from .types import AppExtensionMount, AppExtensionTarget
-from .validators import AppExtensionOptions, AppURLValidator, brand_validator
+from .types import DEFAULT_APP_TARGET
+from .validators import AppURLValidator, brand_validator
logger = logging.getLogger(__name__)
@@ -45,24 +42,6 @@ def _clean_app_url(url):
url_validator(url)
-def _clean_extension_url_with_only_path(
- manifest_data: dict, target: str, extension_url: str
-):
- if target == AppExtensionTarget.APP_PAGE:
- return
- 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:
- msg = (
- "Incorrect relation between extension's target and URL fields. "
- "APP_PAGE can be used only with relative URL path."
- )
- logger.warning(msg, extra={"target": target, "url": extension_url})
- raise ValidationError(msg)
-
-
def _clean_extension_url(extension: dict, manifest_data: dict):
"""Clean assigned extension url.
@@ -73,42 +52,16 @@ 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.
app_url = manifest_data.get("tokenTargetUrl")
- new_tab_method_post = (
- extension.get("options", {}).get("newTabTarget", {}).get("method") == "POST"
- )
- widget_method_post = (
- extension.get("options", {}).get("widgetTarget", {}).get("method") == "POST"
- )
-
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 (is_new_tab_post) or is_widget_post:
- parsed_app_url = urlparse(app_url)
- parsed_extension_url = urlparse(extension_url)
-
- if parsed_extension_url.scheme != "https" and settings.ENABLE_SSL:
- raise ValidationError("Extension must start with https")
-
- if parsed_app_url.hostname != parsed_extension_url.hostname:
- raise ValidationError("Extension URL must match App URL")
-
- else:
+ # Only validate absolute URLs (with protocol)
+ # Relative URLs (starting with '/') are allowed when tokenTargetUrl is provided
+ if not extension_url.startswith("/"):
_clean_app_url(extension_url)
@@ -226,87 +179,16 @@ def _clean_extension_permissions(extension, app_permissions, errors):
extension["permissions"] = extension_permissions
-def _clean_extension_enum_field(enum, field_name, extension, errors):
- if extension[field_name] in [code.upper() for code, _ in enum.CHOICES]:
- extension[field_name] = getattr(enum, extension[field_name])
- else:
- errors["extensions"].append(
- ValidationError(
- f"Incorrect value for field: {field_name}",
- code=AppErrorCode.INVALID.value,
- )
- )
-
-
-def _clean_extension_options(extension, errors):
- """Validate the options field in an extension."""
- 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 not is_widget:
- raise ValidationError(
- "widgetTarget options must be set only on WIDGET target"
- )
-
- if validated_options.new_tab_target and not is_new_tab:
- raise ValidationError(
- "newTabTarget options must be set only on NEW_TAB target"
- )
-
- # Update the extension with the validated options
- extension["options"] = validated_options.model_dump(
- exclude_none=True, by_alias=True
- )
- except (ValidationError, PydanticValidationError) as e:
- errors["extensions"].append(
- ValidationError(
- f"Invalid options field: {str(e)}",
- code=AppErrorCode.INVALID.value,
- )
- )
-
-
-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", [])
for extension in extensions:
if "target" not in extension:
- extension["target"] = AppExtensionTarget.POPUP
- else:
- _clean_extension_enum_field(AppExtensionTarget, "target", extension, errors)
-
- _clean_extension_enum_field(AppExtensionMount, "mount", extension, errors)
+ extension["target"] = DEFAULT_APP_TARGET
- try:
- if extension["target"] == AppExtensionTarget.WIDGET:
- _validate_mounts_for_widget(extension["mount"])
- except ValidationError as invalid_mount_error:
- errors["extensions"].append(invalid_mount_error)
+ # Save in lowercase to maintain backwards compatibility with enums, that were used previously
+ extension["target"] = extension["target"].lower()
+ extension["mount"] = extension["mount"].lower()
try:
_clean_extension_url(extension, manifest_data)
@@ -320,8 +202,6 @@ def _clean_extensions(manifest_data, app_permissions, errors):
_clean_extension_permissions(extension, app_permissions, errors)
- _clean_extension_options(extension, errors)
-
def _clean_webhooks(manifest_data, errors):
webhooks = manifest_data.get("webhooks", [])
diff --git a/saleor/app/models.py b/saleor/app/models.py
index b6e2e93ca4..cf3cf77a88 100644
--- a/saleor/app/models.py
+++ b/saleor/app/models.py
@@ -12,10 +12,10 @@ from ..permission.enums import AppPermission, BasePermissionEnum
from ..permission.models import Permission
from ..webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from .types import (
- AppExtensionHttpMethod,
- AppExtensionMount,
- AppExtensionTarget,
AppType,
+ DeprecatedAppExtensionHttpMethod,
+ DeprecatedAppExtensionMount,
+ DeprecatedAppExtensionTarget,
)
@@ -157,11 +157,13 @@ class AppExtension(models.Model):
app = models.ForeignKey(App, on_delete=models.CASCADE, related_name="extensions")
label = models.CharField(max_length=256)
url = models.URLField()
- mount = models.CharField(choices=AppExtensionMount.CHOICES, max_length=256)
+ mount = models.CharField(
+ choices=DeprecatedAppExtensionMount.CHOICES, max_length=256
+ )
target = models.CharField(
- choices=AppExtensionTarget.CHOICES,
+ choices=DeprecatedAppExtensionTarget.CHOICES,
max_length=128,
- default=AppExtensionTarget.POPUP,
+ default=DeprecatedAppExtensionTarget.POPUP,
)
permissions = models.ManyToManyField(
Permission,
@@ -171,7 +173,7 @@ class AppExtension(models.Model):
http_target_method = models.CharField(
blank=False,
null=True,
- choices=AppExtensionHttpMethod.CHOICES,
+ choices=DeprecatedAppExtensionHttpMethod.CHOICES,
)
settings = models.JSONField(blank=True, default=dict, db_default={})
diff --git a/saleor/app/tests/fixtures/app_extension.py b/saleor/app/tests/fixtures/app_extension.py
index 261dd7e638..faa9ad9ce0 100644
--- a/saleor/app/tests/fixtures/app_extension.py
+++ b/saleor/app/tests/fixtures/app_extension.py
@@ -1,7 +1,6 @@
import pytest
from ....app.models import AppExtension
-from ....app.types import AppExtensionMount
@pytest.fixture
@@ -10,7 +9,7 @@ def app_with_extensions(app_with_token, permission_manage_products):
app=app_with_token,
label="Create product with App",
url="www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount="product_overview_more_actions",
)
extensions = AppExtension.objects.bulk_create(
[
@@ -19,7 +18,7 @@ def app_with_extensions(app_with_token, permission_manage_products):
app=app_with_token,
label="Update product with App",
url="www.example.com/app-product-update",
- mount=AppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
+ mount="product_details_more_actions",
),
]
)
@@ -33,7 +32,7 @@ def removed_app_with_extensions(removed_app, permission_manage_products):
app=removed_app,
label="Create product with App",
url="www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount="product_overview_more_actions",
)
extensions = AppExtension.objects.bulk_create(
[
@@ -42,7 +41,7 @@ def removed_app_with_extensions(removed_app, permission_manage_products):
app=removed_app,
label="Update product with App",
url="www.example.com/app-product-update",
- mount=AppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
+ mount="product_details_more_actions",
),
]
)
diff --git a/saleor/app/tests/test_installation_utils.py b/saleor/app/tests/test_installation_utils.py
index 53e683a37b..4951f9e15d 100644
--- a/saleor/app/tests/test_installation_utils.py
+++ b/saleor/app/tests/test_installation_utils.py
@@ -26,7 +26,7 @@ from ..installation_utils import (
validate_app_install_response,
)
from ..models import App
-from ..types import AppExtensionMount, AppExtensionTarget
+from ..types import DeprecatedAppExtensionMount, DeprecatedAppExtensionTarget
def test_validate_app_install_response():
@@ -304,8 +304,8 @@ def test_install_app_with_extension(
assert app_extension.label == label
assert app_extension.url == url
- assert app_extension.mount == AppExtensionMount.PRODUCT_OVERVIEW_CREATE
- assert app_extension.target == AppExtensionTarget.POPUP
+ assert app_extension.mount == DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE
+ assert app_extension.target == DeprecatedAppExtensionTarget.POPUP
assert list(app_extension.permissions.all()) == [permission_manage_products]
assert app_extension.http_target_method is None
@@ -352,8 +352,8 @@ def test_install_app_with_extension_widget(
assert app_extension.label == label
assert app_extension.url == url
- assert app_extension.mount == AppExtensionMount.PRODUCT_DETAILS_WIDGETS
- assert app_extension.target == AppExtensionTarget.WIDGET
+ assert app_extension.mount == DeprecatedAppExtensionMount.PRODUCT_DETAILS_WIDGETS
+ assert app_extension.target == DeprecatedAppExtensionTarget.WIDGET
assert list(app_extension.permissions.all()) == [permission_manage_products]
assert app_extension.http_target_method == "POST"
@@ -433,89 +433,12 @@ def test_install_app_with_extension_new_tab_target(
app_extension = app.extensions.get()
assert app_extension.label == label
assert app_extension.url == url
- assert app_extension.mount == AppExtensionMount.PRODUCT_OVERVIEW_CREATE
+ assert app_extension.mount == DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE
assert app_extension.target == "new_tab"
assert list(app_extension.permissions.all()) == [permission_manage_products]
assert app_extension.http_target_method == "GET"
-def test_install_app_with_extension_new_tab_target_post_url_non_https(
- app_manifest,
- app_installation,
- monkeypatch,
- permission_manage_products,
-):
- # given
- label = "Open in new tab"
- # Non-https url is prohibited
- url = "http://extenal-url.com"
- options = {"newTabTarget": {"method": "POST"}}
- app_manifest["permissions"] = ["MANAGE_PRODUCTS"]
- app_manifest["extensions"] = [
- {
- "label": label,
- "url": url,
- "mount": "PRODUCT_OVERVIEW_CREATE",
- "permissions": ["MANAGE_PRODUCTS"],
- "options": options,
- "target": "NEW_TAB",
- }
- ]
- mocked_get_response = Mock()
- mocked_get_response.json.return_value = app_manifest
-
- monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
- monkeypatch.setattr("saleor.app.installation_utils.send_app_token", Mock())
-
- app_installation.permissions.set([permission_manage_products])
-
- # when
- # then
- with pytest.raises(ValidationError) as error:
- app, _ = install_app(app_installation, activate=True)
-
- assert error.value.messages[0] == "Incorrect value for field: url."
-
-
-def test_install_app_with_extension_new_tab_target_post_url_other_than_app(
- app_manifest,
- app_installation,
- monkeypatch,
- permission_manage_products,
-):
- # given
- label = "Open in new tab"
- # Url other than app's URL is prohibited
- url = "https://extenal-url.com"
- app_manifest["tokenTargetUrl"] = "https://app-url.com"
- options = {"newTabTarget": {"method": "POST"}}
- app_manifest["permissions"] = ["MANAGE_PRODUCTS"]
- app_manifest["extensions"] = [
- {
- "label": label,
- "url": url,
- "mount": "PRODUCT_OVERVIEW_CREATE",
- "permissions": ["MANAGE_PRODUCTS"],
- "options": options,
- "target": "NEW_TAB",
- }
- ]
- mocked_get_response = Mock()
- mocked_get_response.json.return_value = app_manifest
-
- monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
- monkeypatch.setattr("saleor.app.installation_utils.send_app_token", Mock())
-
- app_installation.permissions.set([permission_manage_products])
-
- # when
- # then
- with pytest.raises(ValidationError) as error:
- app, _ = install_app(app_installation, activate=True)
-
- assert error.value.messages[0] == "Incorrect value for field: url."
-
-
@pytest.mark.parametrize(
"url",
[
@@ -662,8 +585,8 @@ def test_install_app_with_extension_post_method(
assert app_extension.label == label
assert app_extension.url == url
- assert app_extension.mount == AppExtensionMount.PRODUCT_OVERVIEW_CREATE
- assert app_extension.target == AppExtensionTarget.NEW_TAB
+ assert app_extension.mount == DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE
+ assert app_extension.target == DeprecatedAppExtensionTarget.NEW_TAB
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 4b261263ed..e85a3869ea 100644
--- a/saleor/app/tests/test_validators.py
+++ b/saleor/app/tests/test_validators.py
@@ -3,19 +3,16 @@ from django.core.exceptions import ValidationError
from ... import __version__
from ...app.validators import (
- AppExtensionOptions,
AppURLValidator,
)
from ..error_codes import AppErrorCode
from ..manifest_validations import (
_clean_author,
- _clean_extension_options,
_clean_extension_url,
- _clean_extensions,
_clean_required_saleor_version,
_parse_version,
)
-from ..types import AppExtensionTarget
+from ..types import DeprecatedAppExtensionTarget
from ..validators import brand_validator
@@ -98,387 +95,16 @@ def test_brand_validator_with_invalid_logo_url(url):
assert error.value.code == AppErrorCode.INVALID_URL_FORMAT.value
-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,
- }
-
- _clean_extension_url(extension, app_manifest)
-
-
- ("extension", "manifest", "should_raise"),
- [
- # url starts with /, target APP_PAGE, appUrl provided
- (
- {"url": "/page", "target": AppExtensionTarget.APP_PAGE},
- {
- "tokenTargetUrl": "https://app.example.com",
- "appUrl": "https://app.example.com",
- },
- False,
- ),
- # 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",
- },
- False,
- ),
- # url starts with protocol, target APP_PAGE, should raise
- (
- {
- "url": "https://app.example.com/page",
- "target": AppExtensionTarget.APP_PAGE,
- },
- {"tokenTargetUrl": "https://app.example.com"},
- True,
- ),
- # url starts with protocol, target NEW_TAB, method POST, valid host
- (
- {
- "url": "https://app.example.com/page",
- "target": AppExtensionTarget.NEW_TAB,
- "options": {"newTabTarget": {"method": "POST"}},
- },
- {"tokenTargetUrl": "https://app.example.com"},
- False,
- ),
- # url starts with protocol, target NEW_TAB, method POST, invalid host
- (
- {
- "url": "https://other.com/page",
- "target": AppExtensionTarget.NEW_TAB,
- "options": {"newTabTarget": {"method": "POST"}},
- },
- {"tokenTargetUrl": "https://app.example.com"},
- True,
- ),
- # url is valid absolute, target POPUP
- (
- {"url": "https://app.example.com/page", "target": AppExtensionTarget.POPUP},
- {"tokenTargetUrl": "https://app.example.com"},
- False,
- ),
- ],
-)
-def test_clean_extension_url(extension, manifest, should_raise):
- if should_raise:
- with pytest.raises(ValidationError):
- _clean_extension_url(extension, manifest)
-
- else:
- _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,
+ "target": DeprecatedAppExtensionTarget.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(
- {
- "url": "http://app.example.com/page",
- "target": AppExtensionTarget.NEW_TAB,
- "options": {"newTabTarget": {"method": "POST"}},
- },
- {
- "tokenTargetUrl": "https://app.example.com",
- "appUrl": "https://app.example.com",
- },
- )
-
-
-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},
- {
- "tokenTargetUrl": "https://app.example.com",
- "appUrl": "https://app.example.com",
- },
- )
-
- # 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
- assert parsed.widget_target is not None
-
- parsed = AppExtensionOptions().model_validate({"newTabTarget": {"method": "GET"}})
-
- assert parsed.new_tab_target is not None
- assert parsed.widget_target is None
-
- with pytest.raises(
- ValueError, match="Only one of 'newTabTarget' or 'widgetTarget' can be set."
- ):
- AppExtensionOptions.model_validate(
- {
- "newTabTarget": {"method": "GET"},
- "widgetTarget": {"method": "GET"},
- }
- )
-
- # when
- parsed = AppExtensionOptions().model_validate({})
-
- # then
- assert parsed.new_tab_target is None
- assert parsed.widget_target is None
-
-
- ("app_url", "extension_url", "should_raise"),
- [
- (None, "/some-path", True), # Test missing token_target_url
- ("https://example.com", "/some-path", False), # Test valid token_target_url
- ],
-)
-def test_clean_extension_url_token_target_url(app_url, extension_url, should_raise):
- # Given
- extension = {"url": extension_url, "target": "APP_PAGE"}
- manifest_data = {"tokenTargetUrl": app_url, "appUrl": "https://example.com"}
-
- # When & Then
- if should_raise:
- with pytest.raises(ValidationError, match="token_target_url is missing"):
- _clean_extension_url(extension, manifest_data)
- else:
- # Should not raise ValidationError
- _clean_extension_url(extension, manifest_data)
-
-
-def test_clean_extension_options_valid_widget_options():
- # Given
- extension = {
- "target": AppExtensionTarget.WIDGET,
- "options": {"widgetTarget": {"method": "POST"}},
- }
- errors = {"extensions": []}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- assert "extensions" in errors
- assert len(errors["extensions"]) == 0
- assert "options" in extension
- assert "widgetTarget" in extension["options"]
- assert extension["options"]["widgetTarget"]["method"] == "POST"
-
-
-def test_clean_extension_options_valid_new_tab_options():
- # Given
- extension = {
- "target": AppExtensionTarget.NEW_TAB,
- "options": {"newTabTarget": {"method": "GET"}},
- }
- errors = {"extensions": []}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- assert "extensions" in errors
- assert len(errors["extensions"]) == 0
- assert "options" in extension
- assert "newTabTarget" in extension["options"]
- assert extension["options"]["newTabTarget"]["method"] == "GET"
-
-
-def test_clean_extension_options_both_targets():
- # Given
- extension = {
- "target": AppExtensionTarget.NEW_TAB,
- "options": {
- "newTabTarget": {"method": "GET"},
- "widgetTarget": {"method": "POST"},
- },
- }
- errors = {"extensions": []}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- assert "extensions" in errors
- assert len(errors["extensions"]) == 1
- assert (
- "Only one of 'newTabTarget' or 'widgetTarget'"
- in errors["extensions"][0].message
- )
-
-
-def test_clean_extension_options_widget_target_with_wrong_target():
- # Given
- extension = {
- "target": AppExtensionTarget.NEW_TAB,
- "options": {"widgetTarget": {"method": "POST"}},
- }
- errors = {"extensions": []}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- assert "extensions" in errors
- assert len(errors["extensions"]) == 1
- assert (
- "widgetTarget options must be set only on WIDGET target"
- in errors["extensions"][0].message
- )
-
-
-def test_clean_extension_options_new_tab_target_with_wrong_target():
- # Given
- extension = {
- "target": AppExtensionTarget.WIDGET,
- "options": {"newTabTarget": {"method": "GET"}},
- }
- errors = {"extensions": []}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- assert "extensions" in errors
- assert len(errors["extensions"]) == 1
- assert (
- "newTabTarget options must be set only on NEW_TAB target"
- in errors["extensions"][0].message
- )
-
-
-def test_clean_extension_options_invalid_options():
- # Given
- extension = {
- "target": AppExtensionTarget.WIDGET,
- "options": {
- "widgetTarget": {
- "method": "INVALID_METHOD" # Only POST and GET are valid
- }
- },
- }
- errors = {"extensions": []}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- assert "extensions" in errors
- assert len(errors["extensions"]) == 1
- assert "Invalid options field" in errors["extensions"][0].message
-
-
-def test_clean_extension_options_empty():
- # Given
- extension = {"target": AppExtensionTarget.WIDGET, "options": {}}
- errors = {}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- assert "extensions" not in errors
- assert "options" in extension
- assert extension["options"] == {}
-
-
-def test_clean_extension_options_no_options():
- # Given
- extension = {
- "target": AppExtensionTarget.WIDGET,
- }
- errors = {}
-
- # When
- _clean_extension_options(extension, errors)
-
- # Then
- 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
+ _clean_extension_url(extension, manifest_data=app_manifest)
diff --git a/saleor/app/types.py b/saleor/app/types.py
index 921ec98c71..4944549e19 100644
--- a/saleor/app/types.py
+++ b/saleor/app/types.py
@@ -5,7 +5,8 @@ class AppType:
CHOICES = [(LOCAL, "local"), (THIRDPARTY, "thirdparty")]
-class AppExtensionMount:
+# Deprecated. Remove this enum - Saleor will use plain strings in tests, and the exact values are managed by the Dashboard
+class DeprecatedAppExtensionMount:
"""All places where app extension can be mounted."""
CATEGORY_OVERVIEW_CREATE = "category_overview_create"
@@ -126,7 +127,8 @@ class AppExtensionMount:
]
-class AppExtensionTarget:
+# Deprecated. Remove this enum - Saleor will use plain strings in tests, and the exact values are managed by the Dashboard
+class DeprecatedAppExtensionTarget:
"""All available ways of opening an app extension.
POPUP - app's extension will be mounted as a popup window
@@ -146,7 +148,8 @@ class AppExtensionTarget:
]
-class AppExtensionHttpMethod:
+# Deprecated. Remove this enum in 3.24, when this field is dropped from AppExtension model
+class DeprecatedAppExtensionHttpMethod:
"""HTTP methods available for app extensions.
Represents available HTTPS methods for frontend to work with extension (WIDGET and NEW_TAB)
@@ -156,3 +159,10 @@ class AppExtensionHttpMethod:
POST = "POST"
CHOICES = [("GET", "GET"), ("POST", "POST")]
+
+
+# We need special handling for popup - if it declares relative extension URL, resolver will stitch if with app URL
+POPUP_EXTENSION_TARGET = "popup"
+
+# In case of not provided, use the default value as a fallback
+DEFAULT_APP_TARGET = POPUP_EXTENSION_TARGET
diff --git a/saleor/app/validators.py b/saleor/app/validators.py
index bfc4b56196..3e7983a2ed 100644
--- a/saleor/app/validators.py
+++ b/saleor/app/validators.py
@@ -1,14 +1,11 @@
import mimetypes
import re
-from typing import Annotated, Literal
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
-from pydantic import BaseModel, Field, field_validator, model_validator
from ..thumbnail import ICON_MIME_TYPES
from .error_codes import AppErrorCode
-from .types import AppExtensionHttpMethod
class AppURLValidator(URLValidator):
@@ -47,60 +44,3 @@ def brand_validator(brand):
"Invalid file type for field: logo.default.",
code=AppErrorCode.INVALID_URL_FORMAT.value,
)
-
-
-def validate_POST_or_GET_http_method(value):
- """Validate that the HTTP method is either GET or POST."""
- if value not in [
- AppExtensionHttpMethod.GET,
- AppExtensionHttpMethod.POST,
- ]:
- raise ValueError(
- f"Method must be either {AppExtensionHttpMethod.GET} or {AppExtensionHttpMethod.POST}"
- )
- return value
-
-
-class NewTabTargetOptions(BaseModel):
- method: Literal["GET", "POST"]
-
- @field_validator("method")
- def validate_method(cls, value):
- return validate_POST_or_GET_http_method(value)
-
-
-class WidgetTargetOptions(BaseModel):
- method: Literal["GET", "POST"]
-
- @field_validator("method")
- def validate_method(cls, value):
- return validate_POST_or_GET_http_method(value)
-
-
-class AppExtensionOptions(BaseModel):
- new_tab_target: Annotated[
- NewTabTargetOptions | None,
- Field(
- validation_alias="newTabTarget",
- serialization_alias="newTabTarget",
- description="Settings for extension target NEW_TAB",
- ),
- ] = None
- widget_target: Annotated[
- WidgetTargetOptions | None,
- Field(
- validation_alias="widgetTarget",
- serialization_alias="widgetTarget",
- description="Settings for extension target WIDGET",
- ),
- ] = None
-
- @model_validator(mode="after")
- def validate_either_or(cls, values):
- new_tab = values.new_tab_target
- widget = values.widget_target
-
- if new_tab and widget:
- raise ValueError("Only one of 'newTabTarget' or 'widgetTarget' can be set.")
-
- return values
diff --git a/saleor/graphql/app/resolvers.py b/saleor/graphql/app/resolvers.py
index e67ad86965..bc998f44fc 100644
--- a/saleor/graphql/app/resolvers.py
+++ b/saleor/graphql/app/resolvers.py
@@ -3,7 +3,7 @@ from urllib.parse import urljoin, urlparse
from django.db.models import Exists, OuterRef
from ...app import models
-from ...app.types import AppExtensionTarget
+from ...app.types import DEFAULT_APP_TARGET, POPUP_EXTENSION_TARGET
from ...core.jwt import (
create_access_token_for_app,
create_access_token_for_app_extension,
@@ -89,10 +89,10 @@ def resolve_app_extension_url(root):
- target == "POPUP"
- appUrl is defined
"""
- target = root.get("target", AppExtensionTarget.POPUP)
+ target = root.get("target", DEFAULT_APP_TARGET)
app_url = root["app_url"]
url = root["url"]
- if url.startswith("/") and app_url and target == AppExtensionTarget.POPUP:
+ if url.startswith("/") and app_url and target == POPUP_EXTENSION_TARGET:
parsed_url = urlparse(app_url)
new_path = urljoin(parsed_url.path, url[1:])
return parsed_url._replace(path=new_path).geturl()
diff --git a/saleor/graphql/app/tests/benchmarks/test_app_extensions.py b/saleor/graphql/app/tests/benchmarks/test_app_extensions.py
index d59a98fce9..6e1773d433 100644
--- a/saleor/graphql/app/tests/benchmarks/test_app_extensions.py
+++ b/saleor/graphql/app/tests/benchmarks/test_app_extensions.py
@@ -1,7 +1,7 @@
import pytest
from .....app.models import AppExtension
-from .....app.types import AppExtensionMount
+from .....app.types import DeprecatedAppExtensionMount
from ....tests.utils import get_graphql_content
@@ -39,19 +39,19 @@ def test_app_extensions(
app=app,
label="Create product with App1",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
),
AppExtension(
app=app,
label="Create product with App2",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
),
AppExtension(
app=app,
label="Create product with App3",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_CREATE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE,
),
]
)
@@ -117,19 +117,19 @@ def test_app_extensions_with_filter(
app=app,
label="Create product with App1",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
),
AppExtension(
app=app,
label="Create product with App2",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
),
AppExtension(
app=app,
label="Create product with App3",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_CREATE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE,
),
]
)
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 433fef97f1..e52c86a06d 100644
--- a/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py
+++ b/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py
@@ -383,163 +383,6 @@ def test_app_fetch_manifest_missing_extension_fields(
}
- "incorrect_field",
- ["target", "mount"],
-)
-def test_app_fetch_manifest_extensions_incorrect_enum_values(
- incorrect_field, app_manifest, monkeypatch, staff_api_client, permission_manage_apps
-):
- # given
- app_manifest["extensions"] = [
- {
- "permissions": ["MANAGE_PRODUCTS"],
- "label": "Create product with App",
- "url": "http://127.0.0.1:9090/app-extension",
- "mount": "PRODUCT_OVERVIEW_CREATE",
- }
- ]
- app_manifest["extensions"][0][incorrect_field] = "INCORRECT_VALUE"
-
- mocked_get_response = Mock()
- mocked_get_response.json.return_value = app_manifest
-
- monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
- query = APP_FETCH_MANIFEST_MUTATION
- manifest_url = "http://localhost:3000/configuration/manifest"
- variables = {
- "manifest_url": manifest_url,
- }
-
- # when
- response = staff_api_client.post_graphql(
- query, variables=variables, permissions=[permission_manage_apps]
- )
-
- # then
- content = get_graphql_content(response)
- errors = content["data"]["appFetchManifest"]["errors"]
-
- assert len(errors) == 1
- expected_errors = [
- {
- "code": "INVALID",
- "field": "extensions",
- "message": f"Incorrect value for field: {incorrect_field}",
- },
- ]
-
- assert errors == expected_errors
-
-
- ("url", "target", "app_url"),
- [
- ("/app", "APP_PAGE", ""),
- ("/app", "APP_PAGE", "https://www.example.com/app"),
- ("/app", "POPUP", "https://www.example.com/app"),
- (
- "https://www.example.com/app/form",
- "NEW_TAB",
- "https://www.example.com/app",
- ),
- ],
-)
-def test_app_fetch_manifest_extensions_correct_url(
- url,
- target,
- app_url,
- app_manifest,
- monkeypatch,
- staff_api_client,
- permission_manage_apps,
-):
- # given
- app_manifest["appUrl"] = app_url
- app_manifest["extensions"] = [
- {
- "permissions": ["MANAGE_PRODUCTS"],
- "label": "Create product with App",
- "url": url,
- "mount": "PRODUCT_OVERVIEW_CREATE",
- "target": target,
- }
- ]
-
- mocked_get_response = Mock()
- mocked_get_response.json.return_value = app_manifest
-
- monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
- query = APP_FETCH_MANIFEST_MUTATION
- manifest_url = "http://localhost:3000/configuration/manifest"
- variables = {
- "manifest_url": manifest_url,
- }
-
- # when
- response = staff_api_client.post_graphql(
- query, variables=variables, permissions=[permission_manage_apps]
- )
-
- # then
- content = get_graphql_content(response)
- errors = content["data"]["appFetchManifest"]["errors"]
- assert len(errors) == 0
-
-
- ("url", "target"),
- [
- ("http:/127.0.0.1:8080/app", "POPUP"),
- ("127.0.0.1:8080/app", "POPUP"),
- ("", "POPUP"),
- ("/app", "POPUP"),
- ("www.example.com/app", "POPUP"),
- ("https://www.example.com/app", "APP_PAGE"),
- ("http://www.example.com/app", "APP_PAGE"),
- ],
-)
-def test_app_fetch_manifest_extensions_incorrect_url(
- url, target, app_manifest, monkeypatch, staff_api_client, permission_manage_apps
-):
- # given
- app_manifest["extensions"] = [
- {
- "permissions": ["MANAGE_PRODUCTS"],
- "label": "Create product with App",
- "url": url,
- "mount": "PRODUCT_OVERVIEW_CREATE",
- "target": target,
- }
- ]
-
- mocked_get_response = Mock()
- mocked_get_response.json.return_value = app_manifest
-
- monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
- query = APP_FETCH_MANIFEST_MUTATION
- manifest_url = "http://localhost:3000/configuration/manifest"
- variables = {
- "manifest_url": manifest_url,
- }
-
- # when
- response = staff_api_client.post_graphql(
- query, variables=variables, permissions=[permission_manage_apps]
- )
-
- # then
- content = get_graphql_content(response)
- errors = content["data"]["appFetchManifest"]["errors"]
-
- assert len(errors) == 1
- assert errors[0] == {
- "code": "INVALID_URL_FORMAT",
- "field": "extensions",
- "message": "Incorrect value for field: url.",
- }
-
-
@pytest.mark.parametrize(
("app_permissions", "extension_permissions"),
[
@@ -1031,3 +874,171 @@ def test_fetch_manifest_app_with_same_identifier_installed_but_marked_to_be_remo
assert manifest["identifier"] == "saleor.app.avatax"
assert all_apps.not_removed().count() == 0
assert all_apps.marked_to_be_removed().count() == 1
+
+
+def test_app_fetch_manifest_extension_without_options(
+ staff_api_client, app_manifest, permission_manage_apps, monkeypatch
+):
+ # given
+ app_manifest["extensions"] = [
+ {
+ "permissions": ["MANAGE_PRODUCTS"],
+ "label": "Product Extension",
+ "url": "http://127.0.0.1:8080/extension",
+ "mount": "PRODUCT_OVERVIEW_CREATE",
+ "target": "POPUP",
+ }
+ ]
+
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+
+ monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
+
+ query = APP_FETCH_MANIFEST_MUTATION
+ variables = {
+ "manifest_url": "http://localhost:3000/manifest",
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables=variables, permissions=[permission_manage_apps]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["appFetchManifest"]["errors"]
+ manifest = content["data"]["appFetchManifest"]["manifest"]
+ extensions = manifest["extensions"] if manifest else []
+
+ assert not errors, f"Expected no errors, got: {errors}"
+ assert len(extensions) == 1
+ assert extensions[0]["label"] == "Product Extension"
+ assert extensions[0]["url"] == "http://127.0.0.1:8080/extension"
+
+
+def test_app_fetch_manifest_extension_with_relative_url(
+ staff_api_client, app_manifest, permission_manage_apps, monkeypatch
+):
+ # given
+ app_manifest["extensions"] = [
+ {
+ "permissions": [],
+ "label": "Relative URL Extension",
+ "url": "/api/form",
+ "mount": "PRODUCT_DETAILS_WIDGETS",
+ "target": "NEW_TAB",
+ },
+ {
+ "permissions": [],
+ "label": "Root Path Extension",
+ "url": "/",
+ "mount": "PRODUCT_DETAILS_WIDGETS",
+ "target": "APP_PAGE",
+ },
+ ]
+
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+
+ monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
+
+ query = APP_FETCH_MANIFEST_MUTATION
+ variables = {
+ "manifest_url": "http://localhost:3000/manifest",
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables=variables, permissions=[permission_manage_apps]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["appFetchManifest"]["errors"]
+ manifest = content["data"]["appFetchManifest"]["manifest"]
+ extensions = manifest["extensions"] if manifest else []
+
+ assert not errors, f"Expected no errors, got: {errors}"
+ assert len(extensions) == 2
+ assert extensions[0]["url"] == "/api/form"
+ assert extensions[1]["url"] == "/"
+
+
+def test_app_fetch_manifest_extension_with_absolute_url(
+ staff_api_client, app_manifest, permission_manage_apps, monkeypatch
+):
+ # given
+ app_manifest["extensions"] = [
+ {
+ "permissions": ["MANAGE_PRODUCTS"],
+ "label": "Absolute URL Extension",
+ "url": "https://example.com/extension",
+ "mount": "PRODUCT_OVERVIEW_CREATE",
+ "target": "POPUP",
+ }
+ ]
+
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+
+ monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
+
+ query = APP_FETCH_MANIFEST_MUTATION
+ variables = {
+ "manifest_url": "http://localhost:3000/manifest",
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables=variables, permissions=[permission_manage_apps]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["appFetchManifest"]["errors"]
+ manifest = content["data"]["appFetchManifest"]["manifest"]
+ extensions = manifest["extensions"] if manifest else []
+
+ assert not errors, f"Expected no errors, got: {errors}"
+ assert len(extensions) == 1
+ assert extensions[0]["url"] == "https://example.com/extension"
+
+
+def test_app_fetch_manifest_extension_with_invalid_absolute_url(
+ staff_api_client, app_manifest, permission_manage_apps, monkeypatch
+):
+ # given
+ app_manifest["extensions"] = [
+ {
+ "permissions": [],
+ "label": "Invalid URL Extension",
+ "url": "not-a-valid-url",
+ "mount": "PRODUCT_DETAILS_WIDGETS",
+ "target": "POPUP",
+ }
+ ]
+
+ mocked_get_response = Mock()
+ mocked_get_response.json.return_value = app_manifest
+
+ monkeypatch.setattr(HTTPSession, "request", Mock(return_value=mocked_get_response))
+
+ query = APP_FETCH_MANIFEST_MUTATION
+ variables = {
+ "manifest_url": "http://localhost:3000/manifest",
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables=variables, permissions=[permission_manage_apps]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["appFetchManifest"]["errors"]
+
+ assert len(errors) == 1
+ assert errors[0]["field"] == "extensions"
+ assert errors[0]["code"] == AppErrorCode.INVALID_URL_FORMAT.name
+ assert "url" in errors[0]["message"].lower()
diff --git a/saleor/graphql/app/tests/queries/test_app_extension.py b/saleor/graphql/app/tests/queries/test_app_extension.py
index 48b40215ec..b21c8eab4c 100644
--- a/saleor/graphql/app/tests/queries/test_app_extension.py
+++ b/saleor/graphql/app/tests/queries/test_app_extension.py
@@ -2,7 +2,11 @@ import graphene
import pytest
from .....app.models import App, AppExtension
-from .....app.types import AppExtensionMount, AppExtensionTarget, AppType
+from .....app.types import (
+ AppType,
+ DeprecatedAppExtensionMount,
+ DeprecatedAppExtensionTarget,
+)
from .....core.jwt import jwt_decode
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -30,9 +34,9 @@ def test_app_extension_staff_user(app, staff_api_client, permission_manage_produ
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
http_target_method="POST",
- target=AppExtensionTarget.WIDGET,
+ target=DeprecatedAppExtensionTarget.WIDGET,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -70,7 +74,7 @@ def test_app_extension_by_app(app, app_api_client, permission_manage_products):
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -109,7 +113,7 @@ def test_app_extensions_app_removed_app(
app=removed_app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -133,7 +137,7 @@ def test_app_extension_normal_user(app, user_api_client, permission_manage_produ
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -157,7 +161,7 @@ def test_app_extension_staff_user_without_all_permissions(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -189,7 +193,7 @@ def test_app_extension_staff_user_fetching_access_token(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products, permission_manage_orders)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -231,7 +235,7 @@ def test_app_extension_access_token_with_audience(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products, permission_manage_orders)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -270,7 +274,7 @@ def test_app_extension_staff_user_partial_permission(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products, permission_manage_orders)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -318,7 +322,7 @@ def test_app_extension_with_app_query_by_staff_without_permissions(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -344,7 +348,7 @@ def test_app_extension_with_app_query_by_app_without_permissions(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -386,7 +390,7 @@ def test_app_extension_with_app_query_by_app_without_permissions_other_app(
app=another_app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -411,7 +415,7 @@ def test_app_extension_with_app_query_by_owner_app(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -435,7 +439,7 @@ def test_app_extension_with_app_query_by_staff_with_permissions(
app=external_app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -460,7 +464,7 @@ def test_app_extension_with_app_query_by_customer_without_permissions(
app=external_app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
id = graphene.Node.to_global_id("AppExtension", app_extension.id)
@@ -476,10 +480,10 @@ def test_app_extension_with_app_query_by_customer_without_permissions(
@pytest.mark.parametrize(
("target", "method"),
[
- (AppExtensionTarget.WIDGET, "POST"),
- (AppExtensionTarget.WIDGET, "GET"),
- (AppExtensionTarget.NEW_TAB, "POST"),
- (AppExtensionTarget.NEW_TAB, "GET"),
+ (DeprecatedAppExtensionTarget.WIDGET, "POST"),
+ (DeprecatedAppExtensionTarget.WIDGET, "GET"),
+ (DeprecatedAppExtensionTarget.NEW_TAB, "POST"),
+ (DeprecatedAppExtensionTarget.NEW_TAB, "GET"),
],
)
def test_app_extension_type_settings_from_http_target_method(
@@ -493,7 +497,7 @@ def test_app_extension_type_settings_from_http_target_method(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.ORDER_DETAILS_WIDGETS,
+ mount=DeprecatedAppExtensionMount.ORDER_DETAILS_WIDGETS,
http_target_method=method,
target=target,
)
@@ -513,10 +517,10 @@ def test_app_extension_type_settings_from_http_target_method(
assert extension_data["mountName"] == "ORDER_DETAILS_WIDGETS"
assert extension_data["targetName"] == app_extension.target.upper()
- if target == AppExtensionTarget.NEW_TAB:
+ if target == DeprecatedAppExtensionTarget.NEW_TAB:
assert extension_data["settings"]["newTabTarget"]["method"] == method
- if target == AppExtensionTarget.WIDGET:
+ if target == DeprecatedAppExtensionTarget.WIDGET:
assert extension_data["settings"]["widgetTarget"]["method"] == method
@@ -532,7 +536,7 @@ def test_app_extension_type_settings_from_native_settings(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.ORDER_DETAILS_WIDGETS,
+ mount=DeprecatedAppExtensionMount.ORDER_DETAILS_WIDGETS,
http_target_method=method,
settings={"newTabTarget": {"method": method}},
target=target,
@@ -553,8 +557,8 @@ def test_app_extension_type_settings_from_native_settings(
assert extension_data["mountName"] == "ORDER_DETAILS_WIDGETS"
assert extension_data["targetName"] == app_extension.target.upper()
- if target == AppExtensionTarget.NEW_TAB:
+ if target == DeprecatedAppExtensionTarget.NEW_TAB:
assert extension_data["settings"]["newTabTarget"]["method"] == method
- if target == AppExtensionTarget.WIDGET:
+ if target == DeprecatedAppExtensionTarget.WIDGET:
assert extension_data["settings"]["widgetTarget"]["method"] == method
diff --git a/saleor/graphql/app/tests/queries/test_app_extensions.py b/saleor/graphql/app/tests/queries/test_app_extensions.py
index e37ec0a726..03eb0267e6 100644
--- a/saleor/graphql/app/tests/queries/test_app_extensions.py
+++ b/saleor/graphql/app/tests/queries/test_app_extensions.py
@@ -1,7 +1,7 @@
import pytest
from .....app.models import AppExtension
-from .....app.types import AppExtensionMount, AppExtensionTarget
+from .....app.types import DeprecatedAppExtensionMount, DeprecatedAppExtensionTarget
from .....core.jwt import jwt_decode
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -33,9 +33,9 @@ def test_app_extensions(staff_api_client, app, permission_manage_products):
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
http_target_method="POST",
- target=AppExtensionTarget.WIDGET,
+ target=DeprecatedAppExtensionTarget.WIDGET,
)
app_extension.permissions.add(permission_manage_products)
variables = {}
@@ -82,7 +82,7 @@ def test_app_extensions_app_not_active(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
variables = {}
@@ -109,7 +109,7 @@ def test_app_extensions_app_removed_app(
app=removed_app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
variables = {}
@@ -136,7 +136,7 @@ def test_app_extensions_user_not_staff(
app=app,
label="Create product with App",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
)
app_extension.permissions.add(permission_manage_products)
variables = {}
@@ -190,27 +190,27 @@ def test_app_extensions_with_filter(
app=app,
label="Create product with App1",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
- target=AppExtensionTarget.APP_PAGE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ target=DeprecatedAppExtensionTarget.APP_PAGE,
),
AppExtension(
app=app,
label="Create product with App2",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
- target=AppExtensionTarget.POPUP,
+ mount=DeprecatedAppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
+ target=DeprecatedAppExtensionTarget.POPUP,
),
AppExtension(
app=app,
label="Create product with App3",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_CREATE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE,
),
AppExtension(
app=app,
label="Create product with App4",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_CREATE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE,
),
]
)
@@ -301,27 +301,27 @@ def test_app_extensions_with_name_filter(
app=app,
label="Create product with App1",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
- target=AppExtensionTarget.APP_PAGE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,
+ target=DeprecatedAppExtensionTarget.APP_PAGE,
),
AppExtension(
app=app,
label="Create product with App2",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
- target=AppExtensionTarget.POPUP,
+ mount=DeprecatedAppExtensionMount.PRODUCT_DETAILS_MORE_ACTIONS,
+ target=DeprecatedAppExtensionTarget.POPUP,
),
AppExtension(
app=app,
label="Create product with App3",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_CREATE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE,
),
AppExtension(
app=app,
label="Create product with App4",
url="https://www.example.com/app-product",
- mount=AppExtensionMount.PRODUCT_OVERVIEW_CREATE,
+ mount=DeprecatedAppExtensionMount.PRODUCT_OVERVIEW_CREATE,
),
]
)
diff --git a/saleor/graphql/app/types.py b/saleor/graphql/app/types.py
index ad9c239c57..4c3a75d435 100644
--- a/saleor/graphql/app/types.py
+++ b/saleor/graphql/app/types.py
@@ -4,7 +4,11 @@ import datetime
import graphene
from ...app import models
-from ...app.types import AppExtensionHttpMethod, AppExtensionTarget
+from ...app.types import (
+ DEFAULT_APP_TARGET,
+ DeprecatedAppExtensionHttpMethod,
+ DeprecatedAppExtensionTarget,
+)
from ...core.exceptions import PermissionDenied
from ...core.jwt import JWT_THIRDPARTY_ACCESS_TYPE
from ...core.utils import build_absolute_uri
@@ -137,10 +141,6 @@ class AppManifestExtension(BaseObjectType):
class Meta:
doc_category = DOC_CATEGORY_APPS
- @staticmethod
- def resolve_target(root, _info: ResolveInfo):
- return root.get("target") or AppExtensionTarget.POPUP
-
@staticmethod
def resolve_url(root, _info: ResolveInfo):
"""Return an extension URL."""
@@ -148,7 +148,7 @@ class AppManifestExtension(BaseObjectType):
@staticmethod
def resolve_target_name(root, _info: ResolveInfo):
- return (root.get("target") or "POPUP").upper()
+ return (root.get("target") or DEFAULT_APP_TARGET).upper()
@staticmethod
def resolve_mount_name(root, _info: ResolveInfo):
@@ -160,8 +160,8 @@ class AppManifestExtension(BaseObjectType):
class HttpMethod(BaseEnum):
- POST = AppExtensionHttpMethod.POST
- GET = AppExtensionHttpMethod.GET
+ POST = DeprecatedAppExtensionHttpMethod.POST
+ GET = DeprecatedAppExtensionHttpMethod.GET
class AppExtension(AppManifestExtension, ModelObjectType[models.AppExtension]):
@@ -251,14 +251,14 @@ class AppExtension(AppManifestExtension, ModelObjectType[models.AppExtension]):
# Fallback if settings not propagated in DB yet
# Make it case-insensitive due to migration logic - enum will become uppercased in DB
- if root.target.upper() == AppExtensionTarget.WIDGET.upper():
+ if root.target.upper() == DeprecatedAppExtensionTarget.WIDGET.upper():
return {
"widgetTarget": {
"method": http_method,
}
}
- if root.target.upper() == AppExtensionTarget.NEW_TAB.upper():
+ if root.target.upper() == DeprecatedAppExtensionTarget.NEW_TAB.upper():
return {
"newTabTarget": {
"method": http_method,