Skip to content

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

separate command for installation of app from manifest and by arguments

Maciej Korycinski8f68eefaf673
diff --git a/saleor/app/management/commands/create_app.py b/saleor/app/management/commands/create_app.py
index 029b64a8a8..a73ee8a14e 100644
--- a/saleor/app/management/commands/create_app.py
+++ b/saleor/app/management/commands/create_app.py
@@ -1,25 +1,21 @@
import json
-from typing import Any, List, Optional
+from typing import Any, Dict, Optional
-from django.contrib.auth.models import Permission
-from django.core.exceptions import ValidationError
+import requests
+from django.contrib.sites.models import Site
from django.core.management import BaseCommand, CommandError
from django.core.management.base import CommandParser
-from django.core.validators import URLValidator
+from requests.exceptions import RequestException
-from ....core import JobStatus
-from ....core.permissions import get_permissions, get_permissions_enum_list
-from ...installation_utils import install_app
-from ...models import AppJob
-from ...tasks import install_app_task
+from ...models import App
+from .utils import clean_permissions
class Command(BaseCommand):
help = "Used to create new app."
def add_arguments(self, parser: CommandParser) -> None:
- parser.add_argument("name", type=str, help="Name of new app.")
- parser.add_argument("manifest-url", help="Url with app manifest.", type=str)
+ parser.add_argument("name", type=str)
parser.add_argument(
"--permission",
action="append",
@@ -32,55 +28,35 @@ class Command(BaseCommand):
"--activate-after-installation", action="store_true", dest="activate"
)
parser.add_argument(
- "--use-celery",
- action="store_true",
- help="Use celery do schedule installation task.",
+ "--target_url",
+ dest="target_url",
+ help="Url which will receive newly created data of app object. "
+ "Command doesn't return app data to stdout when this "
+ "argument is provided.",
)
- def validate_permissions(self, required_permissions: List[str]):
- permissions = [perm[1] for perm in get_permissions_enum_list()]
- for perm in required_permissions:
- if perm not in permissions:
- raise CommandError(
- f"Permisssion: {perm} doesn't exist in Saleor."
- f" Avaiable permissions: {permissions}"
- )
-
- def clean_permissions(self, required_permissions: List[str]) -> List[Permission]:
- permissions = get_permissions(required_permissions)
- return permissions
-
- def validate_manifest_url(self, manifest_url: str):
- url_validator = URLValidator()
+ def send_app_data(self, target_url, data: Dict[str, Any]):
+ domain = Site.objects.get_current().domain
+ headers = {"x-saleor-domain": domain}
try:
- url_validator(manifest_url)
- except ValidationError:
- raise CommandError(f"Incorrect format of manifest-url: {manifest_url}")
+ response = requests.post(target_url, json=data, headers=headers, timeout=15)
+ except RequestException as e:
+ raise CommandError(f"Request failed. Exception: {e}")
+ response.raise_for_status()
def handle(self, *args: Any, **options: Any) -> Optional[str]:
name = options["name"]
- activate = options["activate"]
- manifest_url = options["manifest-url"]
- use_celery = options["use_celery"]
-
+ is_active = options["activate"]
+ target_url = options["target_url"]
permissions = list(set(options["permissions"]))
- self.validate_permissions(permissions)
- self.validate_manifest_url(manifest_url)
-
- app_job = AppJob.objects.create(name=name, manifest_url=manifest_url)
- if permissions:
- permissions_qs = self.clean_permissions(permissions)
- app_job.permissions.set(permissions_qs)
-
- if use_celery:
- install_app_task.delay(app_job.pk, activate)
- return "Celery task has been scheduled."
-
- try:
- app = install_app(app_job, activate)
- except Exception as e:
- app_job.status = JobStatus.FAILED
- app_job.save()
- raise e
- token = app.tokens.first()
- return json.dumps({"auth_token": token.auth_token})
+ permissions = clean_permissions(permissions)
+ app = App.objects.create(name=name, is_active=is_active)
+ app.permissions.set(permissions)
+ token_obj = app.tokens.create()
+ data = {
+ "auth_token": token_obj.auth_token,
+ }
+ if target_url:
+ self.send_app_data(target_url, data)
+
+ return json.dumps(data) if not target_url else ""
diff --git a/saleor/app/management/commands/create_app_from_manifest.py b/saleor/app/management/commands/create_app_from_manifest.py
new file mode 100644
index 0000000000..4208f15462
--- /dev/null
+++ b/saleor/app/management/commands/create_app_from_manifest.py
@@ -0,0 +1,59 @@
+import json
+from typing import Any, Optional
+
+import requests
+from django.core.exceptions import ValidationError
+from django.core.management import BaseCommand, CommandError
+from django.core.management.base import CommandParser
+from django.core.validators import URLValidator
+
+from ....core import JobStatus
+from ...installation_utils import install_app
+from ...models import AppJob
+from .utils import clean_permissions
+
+
+class Command(BaseCommand):
+ help = "Used to create new app."
+
+ def add_arguments(self, parser: CommandParser) -> None:
+ parser.add_argument("manifest-url", help="Url with app manifest.", type=str)
+ parser.add_argument(
+ "--activate-after-installation", action="store_true", dest="activate"
+ )
+
+ def validate_manifest_url(self, manifest_url: str):
+ url_validator = URLValidator()
+ try:
+ url_validator(manifest_url)
+ except ValidationError:
+ raise CommandError(f"Incorrect format of manifest-url: {manifest_url}")
+
+ def fetch_manifest_data(self, manifest_url: str) -> dict:
+ response = requests.get(manifest_url)
+ response.raise_for_status()
+ return response.json()
+
+ def handle(self, *args: Any, **options: Any) -> Optional[str]:
+ activate = options["activate"]
+ manifest_url = options["manifest-url"]
+
+ self.validate_manifest_url(manifest_url)
+ manifest_data = self.fetch_manifest_data(manifest_url)
+
+ permissions = clean_permissions(manifest_data.get("permissions", []))
+
+ app_job = AppJob.objects.create(
+ app_name=manifest_data["name"], manifest_url=manifest_url
+ )
+ if permissions:
+ app_job.permissions.set(permissions)
+
+ try:
+ app = install_app(app_job, activate)
+ except Exception as e:
+ app_job.status = JobStatus.FAILED
+ app_job.save()
+ raise e
+ token = app.tokens.first()
+ return json.dumps({"auth_token": token.auth_token})
diff --git a/saleor/app/management/commands/utils.py b/saleor/app/management/commands/utils.py
new file mode 100644
index 0000000000..b6f2cea33d
--- /dev/null
+++ b/saleor/app/management/commands/utils.py
@@ -0,0 +1,19 @@
+from typing import List
+
+from django.core.management import CommandError
+
+from saleor.core.permissions import get_permissions, get_permissions_enum_list
+
+
+def clean_permissions(required_permissions: List[str]):
+ all_permissions = {perm[0]: perm[1] for perm in get_permissions_enum_list()}
+ for perm in required_permissions:
+ if not all_permissions.get(perm):
+ raise CommandError(
+ f"Permisssion: {perm} doesn't exist in Saleor."
+ f" Avaiable permissions: {all_permissions}"
+ )
+ permissions = get_permissions(
+ [all_permissions[perm] for perm in required_permissions]
+ )
+ return permissions
diff --git a/tests/cassettes/test_create_app_sends_token.yaml b/tests/cassettes/test_create_app_sends_token.yaml
deleted file mode 100644
index 3a594114af..0000000000
--- a/tests/cassettes/test_create_app_sends_token.yaml
+++ /dev/null
@@ -1,52 +0,0 @@
-interactions:
-- request:
- body: null
- headers:
- Accept:
- - '*/*'
- Accept-Encoding:
- - gzip, deflate
- Connection:
- - keep-alive
- User-Agent:
- - python-requests/2.23.0
- method: GET
- uri: http://localhost:3000/manifest
- response:
- body:
- string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
- : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
- : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
- ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
- : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
- ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
- : \"app2\"\n}"
- headers:
- Access-Control-Allow-Credentials:
- - 'true'
- Cache-Control:
- - no-cache
- Connection:
- - keep-alive
- Content-Length:
- - '457'
- Content-Type:
- - application/json; charset=utf-8
- Date:
- - Tue, 12 May 2020 08:55:55 GMT
- ETag:
- - W/"1c9-rhA8cnfjgbXJTC5B4V+OdmeL6E4"
- Expires:
- - '-1'
- Pragma:
- - no-cache
- Vary:
- - Origin, Accept-Encoding
- X-Content-Type-Options:
- - nosniff
- X-Powered-By:
- - Express
- status:
- code: 200
- message: OK
-version: 1
diff --git a/tests/cassettes/test_creates_app_object.yaml b/tests/cassettes/test_creates_app_from_manifest.yaml
similarity index 53%
rename from tests/cassettes/test_creates_app_object.yaml
rename to tests/cassettes/test_creates_app_from_manifest.yaml
index 1c01a4e1cf..c452b2026d 100644
--- a/tests/cassettes/test_creates_app_object.yaml
+++ b/tests/cassettes/test_creates_app_from_manifest.yaml
@@ -20,7 +20,8 @@ interactions:
,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
: \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
- : \"app2\"\n}"
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
headers:
Access-Control-Allow-Credentials:
- 'true'
@@ -29,13 +30,13 @@ interactions:
Connection:
- keep-alive
Content-Length:
- - '457'
+ - '521'
Content-Type:
- application/json; charset=utf-8
Date:
- - Tue, 12 May 2020 08:55:42 GMT
+ - Thu, 14 May 2020 12:35:55 GMT
ETag:
- - W/"1c9-rhA8cnfjgbXJTC5B4V+OdmeL6E4"
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
Expires:
- '-1'
Pragma:
@@ -50,7 +51,58 @@ interactions:
code: 200
message: OK
- request:
- body: '{"auth_token": "EPIiZBMM57tSkVGcc0ctOY8aObWTY7"}'
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ User-Agent:
+ - python-requests/2.23.0
+ method: GET
+ uri: http://localhost:3000/manifest
+ response:
+ body:
+ string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
+ : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
+ : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
+ ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
+ : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
+ ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
+ headers:
+ Access-Control-Allow-Credentials:
+ - 'true'
+ Cache-Control:
+ - no-cache
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '521'
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 14 May 2020 12:35:55 GMT
+ ETag:
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
+ Expires:
+ - '-1'
+ Pragma:
+ - no-cache
+ Vary:
+ - Origin, Accept-Encoding
+ X-Content-Type-Options:
+ - nosniff
+ X-Powered-By:
+ - Express
+ status:
+ code: 200
+ message: OK
+- request:
+ body: '{"auth_token": "skoQA796LD5vgYMzeSNd3QuScYbG8F"}'
headers:
Accept:
- '*/*'
@@ -70,8 +122,8 @@ interactions:
uri: http://localhost:3000/register
response:
body:
- string: "{\n \"auth_token\": \"EPIiZBMM57tSkVGcc0ctOY8aObWTY7\",\n \"id\"\
- : 14\n}"
+ string: "{\n \"auth_token\": \"skoQA796LD5vgYMzeSNd3QuScYbG8F\",\n \"id\"\
+ : 20\n}"
headers:
Access-Control-Allow-Credentials:
- 'true'
@@ -86,13 +138,13 @@ interactions:
Content-Type:
- application/json; charset=utf-8
Date:
- - Tue, 12 May 2020 08:55:42 GMT
+ - Thu, 14 May 2020 12:35:55 GMT
ETag:
- - W/"40-uIkbdADCyMAHdmTFmdr/iG/m4Kg"
+ - W/"40-W2R9WQhs0bfuwZPjNWzjUatbdFg"
Expires:
- '-1'
Location:
- - http://localhost:3000/register/14
+ - http://localhost:3000/register/20
Pragma:
- no-cache
Vary:
diff --git a/tests/cassettes/test_creates_app_from_manifest_activate_app.yaml b/tests/cassettes/test_creates_app_from_manifest_activate_app.yaml
new file mode 100644
index 0000000000..3676cfebfe
--- /dev/null
+++ b/tests/cassettes/test_creates_app_from_manifest_activate_app.yaml
@@ -0,0 +1,159 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ User-Agent:
+ - python-requests/2.23.0
+ method: GET
+ uri: http://localhost:3000/manifest
+ response:
+ body:
+ string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
+ : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
+ : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
+ ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
+ : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
+ ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
+ headers:
+ Access-Control-Allow-Credentials:
+ - 'true'
+ Cache-Control:
+ - no-cache
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '521'
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 14 May 2020 12:38:40 GMT
+ ETag:
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
+ Expires:
+ - '-1'
+ Pragma:
+ - no-cache
+ Vary:
+ - Origin, Accept-Encoding
+ X-Content-Type-Options:
+ - nosniff
+ X-Powered-By:
+ - Express
+ status:
+ code: 200
+ message: OK
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ User-Agent:
+ - python-requests/2.23.0
+ method: GET
+ uri: http://localhost:3000/manifest
+ response:
+ body:
+ string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
+ : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
+ : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
+ ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
+ : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
+ ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
+ headers:
+ Access-Control-Allow-Credentials:
+ - 'true'
+ Cache-Control:
+ - no-cache
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '521'
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 14 May 2020 12:38:40 GMT
+ ETag:
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
+ Expires:
+ - '-1'
+ Pragma:
+ - no-cache
+ Vary:
+ - Origin, Accept-Encoding
+ X-Content-Type-Options:
+ - nosniff
+ X-Powered-By:
+ - Express
+ status:
+ code: 200
+ message: OK
+- request:
+ body: '{"auth_token": "umPfSpRLbHIAfERSyxcinanPNV0c71"}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '48'
+ Content-Type:
+ - application/json
+ User-Agent:
+ - python-requests/2.23.0
+ x-saleor-domain:
+ - mirumee.com
+ method: POST
+ uri: http://localhost:3000/register
+ response:
+ body:
+ string: "{\n \"auth_token\": \"umPfSpRLbHIAfERSyxcinanPNV0c71\",\n \"id\"\
+ : 21\n}"
+ headers:
+ Access-Control-Allow-Credentials:
+ - 'true'
+ Access-Control-Expose-Headers:
+ - Location
+ Cache-Control:
+ - no-cache
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '64'
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 14 May 2020 12:38:40 GMT
+ ETag:
+ - W/"40-IeSY594Wsx2DnlKP2tiXVGPvB78"
+ Expires:
+ - '-1'
+ Location:
+ - http://localhost:3000/register/21
+ Pragma:
+ - no-cache
+ Vary:
+ - Origin, X-HTTP-Method-Override, Accept-Encoding
+ X-Content-Type-Options:
+ - nosniff
+ X-Powered-By:
+ - Express
+ status:
+ code: 201
+ message: Created
+version: 1
diff --git a/tests/cassettes/test_creates_app_from_manifest_app_has_all_required_permissions.yaml b/tests/cassettes/test_creates_app_from_manifest_app_has_all_required_permissions.yaml
new file mode 100644
index 0000000000..a63f375460
--- /dev/null
+++ b/tests/cassettes/test_creates_app_from_manifest_app_has_all_required_permissions.yaml
@@ -0,0 +1,159 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ User-Agent:
+ - python-requests/2.23.0
+ method: GET
+ uri: http://localhost:3000/manifest
+ response:
+ body:
+ string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
+ : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
+ : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
+ ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
+ : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
+ ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
+ headers:
+ Access-Control-Allow-Credentials:
+ - 'true'
+ Cache-Control:
+ - no-cache
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '521'
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 14 May 2020 12:39:14 GMT
+ ETag:
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
+ Expires:
+ - '-1'
+ Pragma:
+ - no-cache
+ Vary:
+ - Origin, Accept-Encoding
+ X-Content-Type-Options:
+ - nosniff
+ X-Powered-By:
+ - Express
+ status:
+ code: 200
+ message: OK
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ User-Agent:
+ - python-requests/2.23.0
+ method: GET
+ uri: http://localhost:3000/manifest
+ response:
+ body:
+ string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
+ : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
+ : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
+ ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
+ : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
+ ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
+ headers:
+ Access-Control-Allow-Credentials:
+ - 'true'
+ Cache-Control:
+ - no-cache
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '521'
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 14 May 2020 12:39:14 GMT
+ ETag:
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
+ Expires:
+ - '-1'
+ Pragma:
+ - no-cache
+ Vary:
+ - Origin, Accept-Encoding
+ X-Content-Type-Options:
+ - nosniff
+ X-Powered-By:
+ - Express
+ status:
+ code: 200
+ message: OK
+- request:
+ body: '{"auth_token": "mmEXydZgjYnPRo8ilvlpuSJRfzDzCb"}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '48'
+ Content-Type:
+ - application/json
+ User-Agent:
+ - python-requests/2.23.0
+ x-saleor-domain:
+ - mirumee.com
+ method: POST
+ uri: http://localhost:3000/register
+ response:
+ body:
+ string: "{\n \"auth_token\": \"mmEXydZgjYnPRo8ilvlpuSJRfzDzCb\",\n \"id\"\
+ : 22\n}"
+ headers:
+ Access-Control-Allow-Credentials:
+ - 'true'
+ Access-Control-Expose-Headers:
+ - Location
+ Cache-Control:
+ - no-cache
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '64'
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 14 May 2020 12:39:14 GMT
+ ETag:
+ - W/"40-niLZoRkUsxsjp8mtGuSXkN1RMno"
+ Expires:
+ - '-1'
+ Location:
+ - http://localhost:3000/register/22
+ Pragma:
+ - no-cache
+ Vary:
+ - Origin, X-HTTP-Method-Override, Accept-Encoding
+ X-Content-Type-Options:
+ - nosniff
+ X-Powered-By:
+ - Express
+ status:
+ code: 201
+ message: Created
+version: 1
diff --git a/tests/cassettes/test_activate_app.yaml b/tests/cassettes/test_creates_app_from_manifest_installation_failed.yaml
similarity index 63%
rename from tests/cassettes/test_activate_app.yaml
rename to tests/cassettes/test_creates_app_from_manifest_installation_failed.yaml
index 293e9bfda0..a665f22b03 100644
--- a/tests/cassettes/test_activate_app.yaml
+++ b/tests/cassettes/test_creates_app_from_manifest_installation_failed.yaml
@@ -11,7 +11,7 @@ interactions:
User-Agent:
- python-requests/2.23.0
method: GET
- uri: http://localhost:3000/manifest
+ uri: http://localhost:3000/manifest-wrong
response:
body:
string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
@@ -19,7 +19,7 @@ interactions:
: \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
: \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
- ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
+ ,\n \"token_target_url\": \"register-wrong-format\",\n \"identificator\"\
: \"app2\"\n}"
headers:
Access-Control-Allow-Credentials:
@@ -29,13 +29,13 @@ interactions:
Connection:
- keep-alive
Content-Length:
- - '457'
+ - '448'
Content-Type:
- application/json; charset=utf-8
Date:
- - Tue, 12 May 2020 08:55:34 GMT
+ - Thu, 14 May 2020 12:43:50 GMT
ETag:
- - W/"1c9-rhA8cnfjgbXJTC5B4V+OdmeL6E4"
+ - W/"1c0-JJiYGAsB1lIohC/29ugFSHRYbSM"
Expires:
- '-1'
Pragma:
@@ -50,7 +50,7 @@ interactions:
code: 200
message: OK
- request:
- body: '{"auth_token": "xzamIXYMk59gkksyREwYjUhZZ0MVLa"}'
+ body: null
headers:
Accept:
- '*/*'
@@ -58,50 +58,45 @@ interactions:
- gzip, deflate
Connection:
- keep-alive
- Content-Length:
- - '48'
- Content-Type:
- - application/json
User-Agent:
- python-requests/2.23.0
- x-saleor-domain:
- - mirumee.com
- method: POST
- uri: http://localhost:3000/register
+ method: GET
+ uri: http://localhost:3000/manifest-wrong
response:
body:
- string: "{\n \"auth_token\": \"xzamIXYMk59gkksyREwYjUhZZ0MVLa\",\n \"id\"\
- : 13\n}"
+ string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
+ : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
+ : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
+ ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
+ : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
+ ,\n \"token_target_url\": \"register-wrong-format\",\n \"identificator\"\
+ : \"app2\"\n}"
headers:
Access-Control-Allow-Credentials:
- 'true'
- Access-Control-Expose-Headers:
- - Location
Cache-Control:
- no-cache
Connection:
- keep-alive
Content-Length:
- - '64'
+ - '448'
Content-Type:
- application/json; charset=utf-8
Date:
- - Tue, 12 May 2020 08:55:34 GMT
+ - Thu, 14 May 2020 12:43:50 GMT
ETag:
- - W/"40-Vmx8/T7q6eIk5xVNe+qlMwPPEP8"
+ - W/"1c0-JJiYGAsB1lIohC/29ugFSHRYbSM"
Expires:
- '-1'
- Location:
- - http://localhost:3000/register/13
Pragma:
- no-cache
Vary:
- - Origin, X-HTTP-Method-Override, Accept-Encoding
+ - Origin, Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Powered-By:
- Express
status:
- code: 201
- message: Created
+ code: 200
+ message: OK
version: 1
diff --git a/tests/cassettes/test_app_has_all_required_permissions.yaml b/tests/cassettes/test_creates_app_from_manifest_sends_token.yaml
similarity index 63%
rename from tests/cassettes/test_app_has_all_required_permissions.yaml
rename to tests/cassettes/test_creates_app_from_manifest_sends_token.yaml
index 56a11b49fd..ea6548619a 100644
--- a/tests/cassettes/test_app_has_all_required_permissions.yaml
+++ b/tests/cassettes/test_creates_app_from_manifest_sends_token.yaml
@@ -20,7 +20,8 @@ interactions:
,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
: \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
- : \"app2\"\n}"
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
headers:
Access-Control-Allow-Credentials:
- 'true'
@@ -29,13 +30,13 @@ interactions:
Connection:
- keep-alive
Content-Length:
- - '457'
+ - '521'
Content-Type:
- application/json; charset=utf-8
Date:
- - Tue, 12 May 2020 08:55:48 GMT
+ - Thu, 14 May 2020 12:40:17 GMT
ETag:
- - W/"1c9-rhA8cnfjgbXJTC5B4V+OdmeL6E4"
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
Expires:
- '-1'
Pragma:
@@ -50,7 +51,7 @@ interactions:
code: 200
message: OK
- request:
- body: '{"auth_token": "YFXjB5IIUEJfvAflkHFPEEkM61Gchv"}'
+ body: null
headers:
Accept:
- '*/*'
@@ -58,50 +59,46 @@ interactions:
- gzip, deflate
Connection:
- keep-alive
- Content-Length:
- - '48'
- Content-Type:
- - application/json
User-Agent:
- python-requests/2.23.0
- x-saleor-domain:
- - mirumee.com
- method: POST
- uri: http://localhost:3000/register
+ method: GET
+ uri: http://localhost:3000/manifest
response:
body:
- string: "{\n \"auth_token\": \"YFXjB5IIUEJfvAflkHFPEEkM61Gchv\",\n \"id\"\
- : 15\n}"
+ string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"about_app\"\
+ : \"Lorem ipsum\",\n \"data_privacy\": \"Lorem ipsum\",\n \"data_privacy_url\"\
+ : \"http://localhost:8888/app-data-privacy\",\n \"homepage_url\": \"http://localhost:8888/homepage\"\
+ ,\n \"support_url\": \"http://localhost:8888/support\",\n \"configuration_url\"\
+ : \"htpp://localhost:8888/configuration\",\n \"app_url\": \"http://localhost:8888/app\"\
+ ,\n \"token_target_url\": \"http://localhost:3000/register\",\n \"identificator\"\
+ : \"app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
+ \n ]\n}"
headers:
Access-Control-Allow-Credentials:
- 'true'
- Access-Control-Expose-Headers:
- - Location
Cache-Control:
- no-cache
Connection:
- keep-alive
Content-Length:
- - '64'
+ - '521'
Content-Type:
- application/json; charset=utf-8
Date:
- - Tue, 12 May 2020 08:55:48 GMT
+ - Thu, 14 May 2020 12:40:17 GMT
ETag:
- - W/"40-fW5p7pz4wAbGNjq0no1jwexwcGs"
+ - W/"209-nZ8IFYM5Y7abK3rAKwcp0Qk6oSw"
Expires:
- '-1'
- Location:
- - http://localhost:3000/register/15
Pragma:
- no-cache
Vary:
- - Origin, X-HTTP-Method-Override, Accept-Encoding
+ - Origin, Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Powered-By:
- Express
status:
- code: 201
- message: Created
+ code: 200
+ message: OK
version: 1
diff --git a/tests/test_app_create_app_command.py b/tests/test_app_commands.py
similarity index 50%
rename from tests/test_app_create_app_command.py
rename to tests/test_app_commands.py
index ab9c70e2a3..b7ae510d63 100644
--- a/tests/test_app_create_app_command.py
+++ b/tests/test_app_commands.py
@@ -10,69 +10,52 @@ from saleor.core.permissions import get_permissions
@pytest.mark.vcr
-def test_creates_app_object():
- name = "Single App"
+def test_creates_app_from_manifest():
manifest_url = "http://localhost:3000/manifest"
- permissions = ["account.manage_users", "order.manage_orders"]
- call_command("create_app", name, manifest_url, permission=permissions)
+ call_command("create_app_from_manifest", manifest_url)
- apps = App.objects.filter(name=name)
- assert len(apps) == 1
+ app = App.objects.get()
- app = apps[0]
tokens = app.tokens.all()
assert len(tokens) == 1
assert not app.is_active
@pytest.mark.vcr
-def test_activate_app():
- name = "Single App"
+def test_creates_app_from_manifest_activate_app():
manifest_url = "http://localhost:3000/manifest"
- permissions = ["account.manage_users", "order.manage_orders"]
- call_command(
- "create_app", name, manifest_url, permission=permissions, activate=True
- )
+ call_command("create_app_from_manifest", manifest_url, activate=True)
- apps = App.objects.filter(name=name)
- assert len(apps) == 1
+ app = App.objects.get()
- app = apps[0]
tokens = app.tokens.all()
assert len(tokens) == 1
assert app.is_active
@pytest.mark.vcr
-def test_app_has_all_required_permissions():
- name = "SA name"
+def test_creates_app_from_manifest_app_has_all_required_permissions():
manifest_url = "http://localhost:3000/manifest"
permission_list = ["account.manage_users", "order.manage_orders"]
expected_permission = get_permissions(permission_list)
- call_command("create_app", name, manifest_url, permission=permission_list)
+ call_command("create_app_from_manifest", manifest_url)
- apps = App.objects.filter(name=name)
- assert len(apps) == 1
- app = apps[0]
+ app = App.objects.get()
assert set(app.permissions.all()) == set(expected_permission)
@pytest.mark.vcr
-def test_create_app_sends_token(monkeypatch):
+def test_creates_app_from_manifest_sends_token(monkeypatch):
mocked_response = Mock()
mocked_response.status_code = 200
mocked_post = Mock(return_value=mocked_response)
monkeypatch.setattr(requests, "post", mocked_post)
- name = "Single App"
manifest_url = "http://localhost:3000/manifest"
- permissions = [
- "account.manage_users",
- ]
- call_command("create_app", name, manifest_url, permission=permissions)
+ call_command("create_app_from_manifest", manifest_url)
- app = App.objects.filter(name=name)[0]
+ app = App.objects.get()
token = app.tokens.all()[0].auth_token
mocked_post.assert_called_once_with(
"http://localhost:3000/register",
@@ -82,38 +65,61 @@ def test_create_app_sends_token(monkeypatch):
)
-def test_schedule_installation_as_a_celery_task(monkeypatch):
- mocked_install_task = Mock()
- mocked_install = Mock()
- monkeypatch.setattr(
- "saleor.app.management.commands.create_app.install_app_task.delay",
- mocked_install_task,
- )
- monkeypatch.setattr(
- "saleor.app.management.commands.create_app.install_app", mocked_install
- )
+def test_creates_app_from_manifest_installation_failed():
+ manifest_url = "http://localhost:3000/manifest-wrong"
+
+ with pytest.raises(Exception):
+ call_command("create_app_from_manifest", manifest_url)
+ app_job = AppJob.objects.get()
+ assert app_job.status == JobStatus.FAILED
+
+
+def test_creates_app_object():
name = "Single App"
- manifest_url = "http://localhost:3000/manifest"
- permissions = ["account.manage_users", "order.manage_orders"]
+ permissions = ["MANAGE_USERS", "MANAGE_ORDERS"]
+ call_command("create_app", name, permission=permissions)
+
+ apps = App.objects.filter(name=name)
+ assert len(apps) == 1
+
+ app = apps[0]
+ tokens = app.tokens.all()
+ assert len(tokens) == 1
+
- call_command(
- "create_app", name, manifest_url, permission=permissions, use_celery=True
+def test_app_has_all_required_permissions():
+ name = "SA name"
+ expected_permission = get_permissions(
+ ["account.manage_users", "order.manage_orders"]
)
+ call_command("create_app", name, permission=["MANAGE_USERS", "MANAGE_ORDERS"])
- app_job = AppJob.objects.get()
- assert not mocked_install.called
- mocked_install_task.assert_called_once_with(app_job.pk, False)
+ apps = App.objects.filter(name=name)
+ assert len(apps) == 1
+ app = apps[0]
+ assert set(app.permissions.all()) == set(expected_permission)
-def test_installation_failed():
+def test_sends_data_to_target_url(monkeypatch):
+ mocked_response = Mock()
+ mocked_response.status_code = 200
+ mocked_post = Mock(return_value=mocked_response)
+
+ monkeypatch.setattr(requests, "post", mocked_post)
+
name = "Single App"
- manifest_url = "http://localhost:3000/manifest"
- permissions = ["account.manage_users", "order.manage_orders"]
+ target_url = "https://ss.shop.com/register"
+ permissions = ["MANAGE_USERS"]
- with pytest.raises(Exception):
- call_command("create_app", name, manifest_url, permission=permissions)
+ call_command("create_app", name, permission=permissions, target_url=target_url)
- app_job = AppJob.objects.get()
- assert app_job.status == JobStatus.FAILED
+ app = App.objects.filter(name=name)[0]
+ token = app.tokens.all()[0].auth_token
+ mocked_post.assert_called_once_with(
+ target_url,
+ headers={"x-saleor-domain": "mirumee.com"},
+ json={"auth_token": token},
+ timeout=ANY,
+ )