Skip to content

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

Exclude apps with pending installations from app queries. (#11809)

Piotr Zabieglike60eaef79f63
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.py
index 8df1ffc2d2..6b2442209f 100644
--- a/saleor/app/installation_utils.py
+++ b/saleor/app/installation_utils.py
@@ -72,7 +72,9 @@ def install_app(app_installation: AppInstallation, activate: bool = False):
manifest_url=app_installation.manifest_url,
type=AppType.THIRDPARTY,
audience=manifest_data.get("audience"),
+ is_installed=False,
)
+
app.permissions.set(app_installation.permissions.all())
for extension_data in manifest_data.get("extensions", []):
extension = AppExtension.objects.create(
diff --git a/saleor/app/migrations/0020_app_is_installed.py b/saleor/app/migrations/0020_app_is_installed.py
new file mode 100644
index 0000000000..4de6afc88d
--- /dev/null
+++ b/saleor/app/migrations/0020_app_is_installed.py
@@ -0,0 +1,22 @@
+# Generated by Django 3.2.16 on 2023-01-26 14:08
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("app", "0019_fix_constraint_names_in_app_app_permisons"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="app",
+ name="is_installed",
+ field=models.BooleanField(default=True),
+ ),
+ migrations.RunSQL(
+ "ALTER TABLE app_app ALTER COLUMN is_installed SET DEFAULT true;",
+ migrations.RunSQL.noop,
+ ),
+ ]
diff --git a/saleor/app/models.py b/saleor/app/models.py
index c9d9668854..567ba40ba6 100644
--- a/saleor/app/models.py
+++ b/saleor/app/models.py
@@ -58,6 +58,7 @@ class App(ModelWithMetadata):
manifest_url = models.URLField(blank=True, null=True)
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)
objects = AppManager()
class Meta(ModelWithMetadata.Meta):
diff --git a/saleor/app/tasks.py b/saleor/app/tasks.py
index b36a47fa15..92560f9ab2 100644
--- a/saleor/app/tasks.py
+++ b/saleor/app/tasks.py
@@ -15,8 +15,10 @@ logger = logging.getLogger(__name__)
def install_app_task(job_id, activate=False):
app_installation = AppInstallation.objects.get(id=job_id)
try:
- install_app(app_installation, activate=activate)
+ app, _ = install_app(app_installation, activate=activate)
app_installation.delete()
+ app.is_installed = True
+ app.save(update_fields=["is_installed"])
return
except ValidationError as e:
msg = ", ".join([f"{name}: {err}" for name, err in e.message_dict.items()])
diff --git a/saleor/app/tests/test_app_tasks.py b/saleor/app/tests/test_app_tasks.py
index e2a3da04b6..1ed15a11dc 100644
--- a/saleor/app/tests/test_app_tasks.py
+++ b/saleor/app/tests/test_app_tasks.py
@@ -16,6 +16,7 @@ def test_install_app_task(app_installation):
app = App.objects.filter(name=app_installation.app_name).first()
assert app
assert app.is_active is False
+ assert app.is_installed
@pytest.mark.vcr
diff --git a/saleor/graphql/app/resolvers.py b/saleor/graphql/app/resolvers.py
index b4e5decf4f..d943af4c84 100644
--- a/saleor/graphql/app/resolvers.py
+++ b/saleor/graphql/app/resolvers.py
@@ -15,7 +15,7 @@ def resolve_apps_installations(info):
def resolve_apps(info):
- return models.App.objects.all()
+ return models.App.objects.filter(is_installed=True).all()
def resolve_access_token_for_app(info, root):
@@ -45,7 +45,7 @@ def resolve_app(_info, id):
if not id:
return None
_, id = from_global_id_or_error(id, "App")
- return models.App.objects.filter(id=id).first()
+ return models.App.objects.filter(id=id, is_installed=True).first()
def resolve_app_extensions(_info):
diff --git a/saleor/graphql/app/tests/queries/test_app.py b/saleor/graphql/app/tests/queries/test_app.py
index dd2839ac64..a1ebdcb80b 100644
--- a/saleor/graphql/app/tests/queries/test_app.py
+++ b/saleor/graphql/app/tests/queries/test_app.py
@@ -316,6 +316,21 @@ def test_app_with_extensions_query(
assert assigned_permissions in returned_permission_codes
+def test_app_query_pending_installation(staff_api_client, app):
+ # given
+ app.is_installed = False
+ app.save(update_fields=["is_installed"])
+ id = graphene.Node.to_global_id("App", app.id)
+ variables = {"id": id}
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_APP, variables=variables)
+ content = get_graphql_content(response)
+
+ # then
+ assert content["data"]["app"] is None
+
+
QUERY_APP_AVAILABLE_FOR_STAFF_WITHOUT_MANAGE_APPS = """
query ($id: ID){
app(id: $id){
diff --git a/saleor/graphql/app/tests/queries/test_apps.py b/saleor/graphql/app/tests/queries/test_apps.py
index 79722c395c..e643e837e5 100644
--- a/saleor/graphql/app/tests/queries/test_apps.py
+++ b/saleor/graphql/app/tests/queries/test_apps.py
@@ -143,6 +143,23 @@ def test_apps_with_extensions_query(
assert assigned_permissions in returned_permission_codes
+def test_apps_query_no_permission(
+ staff_api_client, permission_manage_users, permission_manage_staff, app
+):
+ variables = {"filter": {}}
+ response = staff_api_client.post_graphql(
+ QUERY_APPS_WITH_FILTER, variables, permissions=[]
+ )
+ assert_no_permission(response)
+
+ response = staff_api_client.post_graphql(
+ QUERY_APPS_WITH_FILTER,
+ variables,
+ permissions=[permission_manage_users, permission_manage_staff],
+ )
+ assert_no_permission(response)
+
+
QUERY_APPS_WITH_SORT = """
query ($sort_by: AppSortingInput!) {
apps(first:5, sortBy: $sort_by) {
@@ -185,21 +202,30 @@ def test_query_apps_with_sort(
assert apps[order]["node"]["name"] == account_name
-def test_apps_query_no_permission(
- staff_api_client, permission_manage_users, permission_manage_staff, app
-):
- variables = {"filter": {}}
- response = staff_api_client.post_graphql(
- QUERY_APPS_WITH_FILTER, variables, permissions=[]
- )
- assert_no_permission(response)
+QUERY_APPS = """
+query {
+ apps(first: 5){
+ edges {
+ node {
+ id
+ }
+ }
+ }
+}
+"""
- response = staff_api_client.post_graphql(
- QUERY_APPS_WITH_FILTER,
- variables,
- permissions=[permission_manage_users, permission_manage_staff],
- )
- assert_no_permission(response)
+
+def test_apps_query_pending_installation(staff_api_client, app):
+ # given
+ app.is_installed = False
+ app.save(update_fields=["is_installed"])
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_APPS)
+ content = get_graphql_content(response)
+
+ # then
+ assert content["data"]["apps"]["edges"] == []
QUERY_APPS_FOR_FEDERATION = """
diff --git a/saleor/site/models.py b/saleor/site/models.py
index 621bba21b7..f1ade8b978 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -116,7 +116,7 @@ class SiteSettings(models.Model):
sender_address: Optional[str] = self.default_mail_sender_address
if not sender_address:
- sender_address = settings.DEFAULT_FROM_EMAIL # type: ignore[misc]
+ sender_address = settings.DEFAULT_FROM_EMAIL
if not sender_address:
raise ImproperlyConfigured("No sender email address has been set-up")