Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for create_app command
Maciej Korycinskib894cd825bb6
diff --git a/tests/cassettes/test_activate_app.yaml b/tests/cassettes/test_activate_app.yamlnew file mode 100644index 0000000000..293e9bfda0--- /dev/null+++ b/tests/cassettes/test_activate_app.yaml@@ -0,0 +1,107 @@+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:34 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+- request:+ body: '{"auth_token": "xzamIXYMk59gkksyREwYjUhZZ0MVLa"}'+ 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\": \"xzamIXYMk59gkksyREwYjUhZZ0MVLa\",\n \"id\"\+ : 13\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:+ - Tue, 12 May 2020 08:55:34 GMT+ ETag:+ - W/"40-Vmx8/T7q6eIk5xVNe+qlMwPPEP8"+ Expires:+ - '-1'+ Location:+ - http://localhost:3000/register/13+ 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: 1diff --git a/tests/cassettes/test_app_has_all_required_permissions.yaml b/tests/cassettes/test_app_has_all_required_permissions.yamlnew file mode 100644index 0000000000..56a11b49fd--- /dev/null+++ b/tests/cassettes/test_app_has_all_required_permissions.yaml@@ -0,0 +1,107 @@+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:48 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+- request:+ body: '{"auth_token": "YFXjB5IIUEJfvAflkHFPEEkM61Gchv"}'+ 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\": \"YFXjB5IIUEJfvAflkHFPEEkM61Gchv\",\n \"id\"\+ : 15\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:+ - Tue, 12 May 2020 08:55:48 GMT+ ETag:+ - W/"40-fW5p7pz4wAbGNjq0no1jwexwcGs"+ Expires:+ - '-1'+ Location:+ - http://localhost:3000/register/15+ 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: 1diff --git a/tests/cassettes/test_create_app_sends_token.yaml b/tests/cassettes/test_create_app_sends_token.yamlnew file mode 100644index 0000000000..3a594114af--- /dev/null+++ b/tests/cassettes/test_create_app_sends_token.yaml@@ -0,0 +1,52 @@+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: 1diff --git a/tests/cassettes/test_creates_app_object.yaml b/tests/cassettes/test_creates_app_object.yamlnew file mode 100644index 0000000000..1c01a4e1cf--- /dev/null+++ b/tests/cassettes/test_creates_app_object.yaml@@ -0,0 +1,107 @@+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:42 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+- request:+ body: '{"auth_token": "EPIiZBMM57tSkVGcc0ctOY8aObWTY7"}'+ 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\": \"EPIiZBMM57tSkVGcc0ctOY8aObWTY7\",\n \"id\"\+ : 14\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:+ - Tue, 12 May 2020 08:55:42 GMT+ ETag:+ - W/"40-uIkbdADCyMAHdmTFmdr/iG/m4Kg"+ Expires:+ - '-1'+ Location:+ - http://localhost:3000/register/14+ 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: 1diff --git a/tests/cassettes/test_sends_data_to_target_url.yaml b/tests/cassettes/test_sends_data_to_target_url.yamlnew file mode 100644index 0000000000..4ce713b2d1--- /dev/null+++ b/tests/cassettes/test_sends_data_to_target_url.yaml@@ -0,0 +1,52 @@+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:51:24 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: 1diff --git a/tests/test_app_create_app_command.py b/tests/test_app_create_app_command.pyindex efc6c6b980..ab9c70e2a3 100644--- a/tests/test_app_create_app_command.py+++ b/tests/test_app_create_app_command.py@@ -1,12 +1,15 @@from unittest.mock import ANY, Mock+import pytestimport requestsfrom django.core.management import call_command-from saleor.app.models import App+from saleor.app.models import App, AppJob+from saleor.core import JobStatusfrom saleor.core.permissions import get_permissionsdef test_creates_app_object():name = "Single App"manifest_url = "http://localhost:3000/manifest"@@ -19,13 +22,34 @@ def test_creates_app_object():app = apps[0]tokens = app.tokens.all()assert len(tokens) == 1+ assert not app.is_active+def test_activate_app():+ name = "Single 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+ )++ apps = App.objects.filter(name=name)+ assert len(apps) == 1++ app = apps[0]+ tokens = app.tokens.all()+ assert len(tokens) == 1+ assert app.is_active++def test_app_has_all_required_permissions():name = "SA name"+ 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, permission=permission_list)+ call_command("create_app", name, manifest_url, permission=permission_list)apps = App.objects.filter(name=name)assert len(apps) == 1@@ -33,26 +57,63 @@ def test_app_has_all_required_permissions():assert set(app.permissions.all()) == set(expected_permission)-def test_sends_data_to_target_url(monkeypatch):+def test_create_app_sends_token(monkeypatch):mocked_response = Mock()mocked_response.status_code = 200mocked_post = Mock(return_value=mocked_response)monkeypatch.setattr(requests, "post", mocked_post)-name = "Single App"- target_url = "https://ss.shop.com/register"+ manifest_url = "http://localhost:3000/manifest"permissions = ["account.manage_users",]- call_command("create_app", name, permission=permissions, target_url=target_url)+ call_command("create_app", name, manifest_url, permission=permissions)app = App.objects.filter(name=name)[0]token = app.tokens.all()[0].auth_tokenmocked_post.assert_called_once_with(- target_url,- headers={"x-saleor-domain": "mirumee.com"},+ "http://localhost:3000/register",+ headers={"Content-Type": "application/json", "x-saleor-domain": "mirumee.com"},json={"auth_token": token},timeout=ANY,)+++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+ )++ name = "Single App"+ manifest_url = "http://localhost:3000/manifest"+ permissions = ["account.manage_users", "order.manage_orders"]++ call_command(+ "create_app", name, manifest_url, permission=permissions, use_celery=True+ )++ app_job = AppJob.objects.get()+ assert not mocked_install.called+ mocked_install_task.assert_called_once_with(app_job.pk, False)+++def test_installation_failed():+ name = "Single App"+ manifest_url = "http://localhost:3000/manifest"+ permissions = ["account.manage_users", "order.manage_orders"]++ with pytest.raises(Exception):+ call_command("create_app", name, manifest_url, permission=permissions)++ app_job = AppJob.objects.get()+ assert app_job.status == JobStatus.FAILED