Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add timeout to appFetchManifest mutation
Maciej Korycinski88ad0219c4d7
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.pyindex 87ebd8c37e..36ba92e909 100644--- a/saleor/app/installation_utils.py+++ b/saleor/app/installation_utils.py@@ -6,7 +6,7 @@ from ..app.validators import AppURLValidatorfrom .models import App, AppInstallationfrom .types import AppType-REQUEST_TIMEOUT = 30+REQUEST_TIMEOUT = 25def send_app_token(target_url: str, token: str):diff --git a/saleor/graphql/app/mutations.py b/saleor/graphql/app/mutations.pyindex 5346cb9258..6507f18b14 100644--- a/saleor/graphql/app/mutations.py+++ b/saleor/graphql/app/mutations.py@@ -6,6 +6,7 @@ from django.core.exceptions import ValidationErrorfrom ...app import modelsfrom ...app.error_codes import AppErrorCode+from ...app.installation_utils import REQUEST_TIMEOUTfrom ...app.tasks import install_app_taskfrom ...app.validators import AppURLValidatorfrom ...core import JobStatus@@ -421,9 +422,13 @@ class AppFetchManifest(BaseMutation):@classmethoddef fetch_manifest(cls, manifest_url):try:- response = requests.get(manifest_url)+ response = requests.get(manifest_url, timeout=REQUEST_TIMEOUT)response.raise_for_status()return response.json()+ except requests.Timeout:+ msg = "The request to fetch manifest data timed out."+ code = AppErrorCode.MANIFEST_URL_CANT_CONNECT.value+ raise ValidationError({"manifest_url": ValidationError(msg, code=code)})except requests.HTTPError:msg = "Unable to fetch manifest data."code = AppErrorCode.MANIFEST_URL_CANT_CONNECT.valuediff --git a/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py b/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.pyindex 074e197a1c..190477dfc5 100644--- a/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py+++ b/saleor/graphql/app/tests/mutations/test_app_fetch_manifest.py@@ -131,6 +131,33 @@ def test_app_fetch_manifest_unable_to_connect(}+def test_app_fetch_manifest_timeout(+ staff_user, staff_api_client, permission_manage_apps, monkeypatch+):+ mocked_request = Mock()+ mocked_request.side_effect = requests.Timeout()+ monkeypatch.setattr("saleor.graphql.app.mutations.requests.get", mocked_request)+ manifest_url = "http://localhost:3000/manifest-doesnt-exist"+ query = APP_FETCH_MANIFEST_MUTATION+ variables = {+ "manifest_url": manifest_url,+ }+ staff_user.user_permissions.set([permission_manage_apps])+ response = staff_api_client.post_graphql(+ query,+ variables=variables,+ )+ content = get_graphql_content(response)+ errors = content["data"]["appFetchManifest"]["errors"]++ assert len(errors) == 1+ assert errors[0] == {+ "field": "manifestUrl",+ "message": "The request to fetch manifest data timed out.",+ "code": "MANIFEST_URL_CANT_CONNECT",+ }++@pytest.mark.vcrdef test_app_fetch_manifest_wrong_format_of_response(staff_user, staff_api_client, permission_manage_apps