Skip to content

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

Prevent app already installed error in quiet mode (#18742)

Cezary Miączcde6e435e96f
diff --git a/saleor/app/management/commands/install_app.py b/saleor/app/management/commands/install_app.py
index 5c9105275f..fe6414f315 100644
--- a/saleor/app/management/commands/install_app.py
+++ b/saleor/app/management/commands/install_app.py
@@ -5,6 +5,7 @@ from django.core.exceptions import ValidationError
from django.core.management import BaseCommand, CommandError
from django.core.management.base import CommandParser
+from ....app.models import App
from ....app.validators import AppURLValidator
from ....core import JobStatus
from ...installation_utils import fetch_manifest, install_app
@@ -46,9 +47,18 @@ class Command(BaseCommand):
self.validate_manifest_url(manifest_url)
manifest_data = fetch_manifest(manifest_url)
-
permissions = clean_permissions(manifest_data.get("permissions", []))
+ if quiet and (
+ app := App.objects.not_removed()
+ .filter(identifier=manifest_data.get("id"))
+ .first()
+ ):
+ self.stdout.write(
+ f"App with the same identifier is already installed: {app.name}"
+ )
+ return None
+
app_job = AppInstallation.objects.create(
app_name=manifest_data["name"], manifest_url=manifest_url
)
diff --git a/saleor/app/tests/test_app_commands.py b/saleor/app/tests/test_app_commands.py
index 36af15ad6e..6ff4d98b75 100644
--- a/saleor/app/tests/test_app_commands.py
+++ b/saleor/app/tests/test_app_commands.py
@@ -1,4 +1,4 @@
-from unittest.mock import ANY, Mock, call
+from unittest.mock import ANY, Mock, call, patch
import graphene
import pytest
@@ -10,6 +10,7 @@ from ... import schema_version
from ...core import JobStatus
from ...permission.enums import get_permissions
from ..models import App, AppInstallation
+from ..types import AppType
@pytest.mark.vcr
@@ -107,6 +108,49 @@ def test_creates_app_from_manifest_installation_failed():
assert app_job.uuid is not None
+def installed_app():
+ app = App.objects.create(
+ name="Installed App",
+ identifier="app.already.installed",
+ is_active=True,
+ is_installed=True,
+ manifest_url="http://otherapp:3000/manifest",
+ type=AppType.THIRDPARTY,
+ )
+ manifest_data = {
+ "name": app.name,
+ "version": "1.0.0",
+ "id": app.identifier,
+ "tokenTargetUrl": "http://otherapp:3000/register",
+ }
+ with patch(
+ "saleor.app.management.commands.install_app.fetch_manifest",
+ return_value=manifest_data,
+ ):
+ with patch(
+ "saleor.app.installation_utils.fetch_manifest", return_value=manifest_data
+ ):
+ yield app
+
+
+def test_creates_app_from_manifest_fails_on_already_installed_app(installed_app):
+ with pytest.raises(
+ ValidationError,
+ match="App with the same identifier is already installed",
+ ):
+ call_command("install_app", installed_app.manifest_url)
+
+
+def test_creates_app_from_manifest_quiet_skips_already_installed_app(installed_app):
+ call_command("install_app", installed_app.manifest_url, quiet=True)
+
+ assert App.objects.all().count() == 1
+
+ app = App.objects.get()
+ assert app.pk == installed_app.pk
+
+
def test_creates_app_object():
name = "Single App"
permissions = ["MANAGE_USERS", "MANAGE_ORDERS"]