Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix pytest
fowczrek7d7ff8875f7c
diff --git a/saleor/account/migrations/0055_alter_user_options.py b/saleor/account/migrations/0055_alter_user_options.pyindex ec5e58f7ba..529465e1ab 100644--- a/saleor/account/migrations/0055_alter_user_options.py+++ b/saleor/account/migrations/0055_alter_user_options.py@@ -6,7 +6,12 @@ from django.db.models.signals import post_migratedef assign_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnGroup = apps.get_model("account", "Group")Permission = apps.get_model("permission", "Permission")ContentType = apps.get_model("contenttypes", "ContentType")diff --git a/saleor/app/migrations/0014_alter_app_options.py b/saleor/app/migrations/0014_alter_app_options.pyindex 0effb26e20..de49f57588 100644--- a/saleor/app/migrations/0014_alter_app_options.py+++ b/saleor/app/migrations/0014_alter_app_options.py@@ -6,7 +6,12 @@ from django.db.models.signals import post_migratedef assign_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnGroup = apps.get_model("account", "Group")Permission = apps.get_model("permission", "Permission")ContentType = apps.get_model("contenttypes", "ContentType")diff --git a/saleor/channel/migrations/0001_initial.py b/saleor/channel/migrations/0001_initial.pyindex e2e68ddd97..336f5d78ec 100644--- a/saleor/channel/migrations/0001_initial.py+++ b/saleor/channel/migrations/0001_initial.py@@ -7,9 +7,14 @@ from django.db.models.signals import post_migratefrom django.apps import apps as registry-def assing_permissions(apps, schema_editor):+def assign_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnGroup = apps.get_model("account", "Group")Permission = apps.get_model("permission", "Permission")ContentType = apps.get_model("contenttypes", "ContentType")@@ -106,5 +111,5 @@ class Migration(migrations.Migration):},),migrations.RunPython(create_default_channel, migrations.RunPython.noop),- migrations.RunPython(assing_permissions, migrations.RunPython.noop),+ migrations.RunPython(assign_permissions, migrations.RunPython.noop),]diff --git a/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py b/saleor/checkout/migrations/0040_add_handle_checkouts_permission.pyindex 12cab4cc32..2bd1713ae6 100644--- a/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py+++ b/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py@@ -7,7 +7,12 @@ from django.db.models.signals import post_migratedef assign_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnPermission = apps.get_model("permission", "Permission")App = apps.get_model("app", "App")Group = apps.get_model("account", "Group")diff --git a/saleor/checkout/migrations/0051_auto_20220713_1103.py b/saleor/checkout/migrations/0051_auto_20220713_1103.pyindex 9aa8505a93..2aa0abe2f6 100644--- a/saleor/checkout/migrations/0051_auto_20220713_1103.py+++ b/saleor/checkout/migrations/0051_auto_20220713_1103.py@@ -8,7 +8,12 @@ from django.db.models.signals import post_migratedef assign_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnPermission = apps.get_model("permission", "Permission")App = apps.get_model("app", "App")Group = apps.get_model("account", "Group")diff --git a/saleor/checkout/migrations/0054_alter_checkout_options.py b/saleor/checkout/migrations/0054_alter_checkout_options.pyindex 20b6264120..7e5875c656 100644--- a/saleor/checkout/migrations/0054_alter_checkout_options.py+++ b/saleor/checkout/migrations/0054_alter_checkout_options.py@@ -7,7 +7,12 @@ from django.db.models.signals import post_migratedef assign_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnPermission = apps.get_model("permission", "Permission")App = apps.get_model("app", "App")Group = apps.get_model("account", "Group")diff --git a/saleor/checkout/tests/test_order_from_checkout.py b/saleor/checkout/tests/test_order_from_checkout.pyindex 000770a90b..80b543ab07 100644--- a/saleor/checkout/tests/test_order_from_checkout.py+++ b/saleor/checkout/tests/test_order_from_checkout.py@@ -2,7 +2,6 @@ from decimal import Decimalfrom unittest import mockimport pytest-from django.contrib.auth.models import AnonymousUserfrom django.test import override_settingsfrom prices import TaxedMoney@@ -589,7 +588,7 @@ def test_create_order_from_checkout_update_display_gross_prices(checkout_lines=checkout_lines,discounts=[],manager=manager,- user=AnonymousUser(),+ user=None,app=app,tracking_code="tracking_code",)diff --git a/saleor/core/auth_backend.py b/saleor/core/auth_backend.pyindex 7561984559..b444fa2ad2 100644--- a/saleor/core/auth_backend.py+++ b/saleor/core/auth_backend.py@@ -1,5 +1,4 @@import jwt-from django.contrib.auth.backends import ModelBackendfrom ..account.models import Userfrom ..graphql.account.dataloaders import UserByEmailLoader@@ -15,7 +14,31 @@ from .jwt import (from .permissions import get_permissions_from_codenames, get_permissions_from_names-class JSONWebTokenBackend(ModelBackend):+# Moved from `django.contrib.auth.backends.ModelBackend`+class BaseBackend:+ def authenticate(self, request, **kwargs):+ return None++ def get_user(self, user_id):+ return None++ def get_user_permissions(self, user_obj, obj=None):+ return set()++ def get_group_permissions(self, user_obj, obj=None):+ return set()++ def get_all_permissions(self, user_obj, obj=None):+ return {+ *self.get_user_permissions(user_obj, obj=obj),+ *self.get_group_permissions(user_obj, obj=obj),+ }++ def has_perm(self, user_obj, perm, obj=None):+ return perm in self.get_all_permissions(user_obj, obj=obj)+++class JSONWebTokenBackend(BaseBackend):def authenticate(self, request=None, **kwargs):return load_user_from_request(request)@@ -49,6 +72,34 @@ class JSONWebTokenBackend(ModelBackend):setattr(user_obj, perm_cache_name, {f"{ct}.{name}" for ct, name in perms})return getattr(user_obj, perm_cache_name)+ # Moved from `django.contrib.auth.backends.ModelBackend`+ def get_user_permissions(self, user_obj, obj=None): # noqa: D205, D212, D400, D415+ """+ Return a set of permission strings the user `user_obj` has from their+ `user_permissions`.+ """+ return self._get_permissions(user_obj, obj, "user")++ # Moved from `django.contrib.auth.backends.ModelBackend`+ def get_group_permissions(self, user_obj, obj=None): # noqa: D205, D212, D400, D415+ """+ Return a set of permission strings the user `user_obj` has from the+ groups they belong.+ """+ return self._get_permissions(user_obj, obj, "group")++ # Moved from `django.contrib.auth.backends.ModelBackend`+ def get_all_permissions(self, user_obj, obj=None):+ if not user_obj.is_active or user_obj.is_anonymous or obj is not None:+ return set()+ if not hasattr(user_obj, "_perm_cache"):+ user_obj._perm_cache = super().get_all_permissions(user_obj)+ return user_obj._perm_cache++ # Moved from `django.contrib.auth.backends.ModelBackend`+ def has_perm(self, user_obj, perm, obj=None):+ return user_obj.is_active and super().has_perm(user_obj, perm, obj=obj)+class PluginBackend(JSONWebTokenBackend):def authenticate(self, request=None, **kwargs):diff --git a/saleor/page/migrations/0017_pagetype.py b/saleor/page/migrations/0017_pagetype.pyindex f9f56468ba..c937933468 100644--- a/saleor/page/migrations/0017_pagetype.py+++ b/saleor/page/migrations/0017_pagetype.py@@ -8,7 +8,12 @@ import saleor.core.utils.json_serializerdef update_groups_with_manage_pages_with_new_permission(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnGroup = apps.get_model("account", "Group")Permission = apps.get_model("permission", "Permission")ContentType = apps.get_model("contenttypes", "ContentType")diff --git a/saleor/payment/migrations/0029_alter_payment_options.py b/saleor/payment/migrations/0029_alter_payment_options.pyindex f1850ec1de..bb5512f339 100644--- a/saleor/payment/migrations/0029_alter_payment_options.py+++ b/saleor/payment/migrations/0029_alter_payment_options.py@@ -7,7 +7,12 @@ from django.db.models.signals import post_migratedef assing_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnGroup = apps.get_model("account", "Group")Permission = apps.get_model("permission", "Permission")ContentType = apps.get_model("contenttypes", "ContentType")diff --git a/saleor/product/migrations/0129_add_product_types_and_attributes_perm.py b/saleor/product/migrations/0129_add_product_types_and_attributes_perm.pyindex 9e6c01ab62..0e09f0d850 100644--- a/saleor/product/migrations/0129_add_product_types_and_attributes_perm.py+++ b/saleor/product/migrations/0129_add_product_types_and_attributes_perm.py@@ -7,7 +7,12 @@ from django.apps import apps as registrydef update_groups_with_manage_products_with_new_permission(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnGroup = apps.get_model("account", "Group")Permission = apps.get_model("permission", "Permission")ContentType = apps.get_model("contenttypes", "ContentType")diff --git a/saleor/tax/migrations/0003_add_manage_taxes_permission.py b/saleor/tax/migrations/0003_add_manage_taxes_permission.pyindex a488ad4f37..2affcd252a 100644--- a/saleor/tax/migrations/0003_add_manage_taxes_permission.py+++ b/saleor/tax/migrations/0003_add_manage_taxes_permission.py@@ -1,12 +1,18 @@# Generated by Django 3.2.13 on 2022-05-27 13:17+from django.apps import apps as registryfrom django.db import migrationsfrom django.db.models.signals import post_migratedef assign_permissions(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):- apps = kwargs["apps"]+ try:+ apps = kwargs["apps"]+ except KeyError:+ # In test when we use use `@pytest.mark.django_db(transaction=True)`+ # pytest trigger additional post_migrate signal without `apps` in kwargs.+ returnPermission = apps.get_model("permission", "Permission")Group = apps.get_model("account", "Group")ContentType = apps.get_model("contenttypes", "ContentType")@@ -26,7 +32,8 @@ def assign_permissions(apps, schema_editor):for group in groups:group.permissions.add(manage_taxes)- post_migrate.connect(on_migrations_complete, weak=False)+ sender = registry.get_app_config("tax")+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)class Migration(migrations.Migration):