Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Merge branch 'main' into pe-10-webhook-payload-storage
Cezary Miączc37898bed7bf
diff --cc saleor/core/__init__.pyindex b2b2505a5a,282dcb4e68..5ab2e86a4d--- a/saleor/core/__init__.py+++ b/saleor/core/__init__.py@@@ -1,11 -1,3 +1,9 @@@+from django.conf import settings+from django.core.files.storage import Storage+from django.utils.functional import LazyObject+from django.utils.module_loading import import_string+- default_app_config = "saleor.core.app.CoreAppConfig"-+class JobStatus:PENDING = "pending"SUCCESS = "success"diff --cc saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.pyindex 26180c4c0e,69d29e110d..d953e46f06--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py@@@ -195,6 -196,37 +196,37 @@@ def test_checkout_calculate_taxes_with_}+ @freeze_time("2020-03-18 12:00:00")+ def test_checkout_calculate_taxes_with_pregenerated_payload(+ checkout_with_voucher_free_shipping,+ webhook_app,+ permission_handle_taxes,+ ):+ # given+ checkout = checkout_with_voucher_free_shipping+ webhook_app.permissions.add(permission_handle_taxes)+ webhook = Webhook.objects.create(+ name="Webhook",+ app=webhook_app,+ target_url="http://www.example.com/any",+ subscription_query=TAXES_SUBSCRIPTION_QUERY,+ )+ event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES+ webhook.events.create(event_type=event_type)+ expected_payload = {"payload": "test"}++ # when+ deliveries = create_delivery_for_subscription_sync_event(+ event_type,+ checkout,+ webhook,+ pregenerated_payload=expected_payload,+ )++ # then- assert json.loads(deliveries.payload.payload) == expected_payload++ assert json.loads(deliveries.payload.get_payload()) == expected_payload++@freeze_time("2020-03-18 12:00:00")def test_checkout_calculate_taxes_with_entire_order_voucher(checkout_with_voucher,@@@ -365,6 -397,68 +397,68 @@@ def test_checkout_calculate_taxes_with_}+ @freeze_time("2020-03-18 12:00:00")+ def test_checkout_calculate_taxes_with_order_promotion(+ checkout_with_item_and_order_discount,+ webhook_app,+ permission_handle_taxes,+ ):+ # given+ checkout = checkout_with_item_and_order_discount+ line = checkout.lines.get()+ line_price = line.variant.channel_listings.get(+ channel=checkout.channel+ ).price_amount+ channel_id = to_global_id_or_none(checkout.channel)+ webhook_app.permissions.add(permission_handle_taxes)+ webhook = Webhook.objects.create(+ name="Webhook",+ app=webhook_app,+ target_url="http://www.example.com/any",+ subscription_query=TAXES_SUBSCRIPTION_QUERY,+ )+ event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES+ webhook.events.create(event_type=event_type)+ discount_amount = PromotionRule.objects.get().reward_value++ # when+ deliveries = create_delivery_for_subscription_sync_event(+ event_type, checkout, webhook+ )++ # then- assert json.loads(deliveries.payload.payload) == {++ assert json.loads(deliveries.payload.get_payload()) == {+ "__typename": "CalculateTaxes",+ "taxBase": {+ "address": None,+ "currency": "USD",+ "discounts": [{"amount": {"amount": float(discount_amount)}}],+ "channel": {"id": channel_id},+ "lines": [+ {+ "chargeTaxes": True,+ "productName": "Test product",+ "productSku": "123",+ "quantity": line.quantity,+ "sourceLine": {+ "id": to_global_id_or_none(line),+ "__typename": "CheckoutLine",+ },+ "totalPrice": {"amount": float(line_price * 3)},+ "unitPrice": {"amount": float(line_price)},+ "variantName": "",+ }+ ],+ "pricesEnteredWithTax": True,+ "shippingPrice": {"amount": 0.0},+ "sourceObject": {+ "id": to_global_id_or_none(checkout),+ "__typename": "Checkout",+ },+ },+ }++@freeze_time("2020-03-18 12:00:00")def test_checkout_calculate_taxes_empty_checkout(checkout,@@@ -981,3 -1075,66 +1075,66 @@@ def test_order_calculate_taxes_empty_or},},}+++ @freeze_time("2020-03-18 12:00:00")+ def test_order_calculate_taxes_order_promotion(+ order_with_lines_and_order_promotion, subscription_calculate_taxes_for_order+ ):+ # given+ order = order_with_lines_and_order_promotion+ order.status = OrderStatus.DRAFT+ order.save(update_fields=["status"])+ webhook = subscription_calculate_taxes_for_order++ webhook.subscription_query = TAXES_SUBSCRIPTION_QUERY+ webhook.save(update_fields=["subscription_query"])++ discount_amount = PromotionRule.objects.get().reward_value++ expected_shipping_price = Money("2.00", order.currency)+ order.undiscounted_base_shipping_price = expected_shipping_price+ order.base_shipping_price = expected_shipping_price+ order.shipping_price = TaxedMoney(+ net=expected_shipping_price, gross=expected_shipping_price+ )++ # when+ deliveries = create_delivery_for_subscription_sync_event(+ WebhookEventSyncType.ORDER_CALCULATE_TAXES, order, webhook+ )++ # then- assert json.loads(deliveries.payload.payload) == {++ assert json.loads(deliveries.payload.get_payload()) == {+ "__typename": "CalculateTaxes",+ "taxBase": {+ "address": {"id": to_global_id_or_none(order.shipping_address)},+ "currency": "USD",+ "discounts": [{"amount": {"amount": float(discount_amount)}}],+ "channel": {"id": to_global_id_or_none(order.channel)},+ "lines": [+ {+ "chargeTaxes": True,+ "productName": line.product_name,+ "productSku": line.product_sku,+ "quantity": line.quantity,+ "sourceLine": {+ "__typename": "OrderLine",+ "id": to_global_id_or_none(line),+ },+ "totalPrice": {+ "amount": float(line.base_unit_price_amount * line.quantity)+ },+ "unitPrice": {"amount": float(line.base_unit_price_amount)},+ "variantName": line.variant_name,+ }+ for line in order.lines.all()+ ],+ "pricesEnteredWithTax": True,+ "shippingPrice": {"amount": float(float(expected_shipping_price.amount))},+ "sourceObject": {+ "__typename": "Order",+ "id": to_global_id_or_none(order),+ },+ },+ }diff --cc saleor/plugins/webhook/tests/test_tax_webhook.pyindex c805f32b07,277b1e5999..41ac6cc564--- a/saleor/plugins/webhook/tests/test_tax_webhook.py+++ b/saleor/plugins/webhook/tests/test_tax_webhook.py@@@ -228,8 -234,74 +234,74 @@@ def test_get_taxes_for_checkout_with_sytax_data = plugin.get_taxes_for_checkout(checkout_info, [], app_identifier, None)# then+ mock_generate_payload.assert_called_once_with(+ event_type=WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES,+ subscribable_object=checkout,+ subscription_query=subscription_query,+ request=ANY, # SaleorContext,+ app=tax_app,+ )payload = EventPayload.objects.get()- assert payload.get_payload() == json.dumps({"taxBase": {"currency": "USD"}})- assert payload.payload == json.dumps(expected_payload)++ assert payload.get_payload() == json.dumps(expected_payload)+ delivery = EventDelivery.objects.get()+ assert delivery.status == EventDeliveryStatus.PENDING+ assert delivery.event_type == WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES+ assert delivery.payload == payload+ assert delivery.webhook == webhook+ mock_request.assert_called_once_with(delivery)+ mock_fetch.assert_not_called()+ assert tax_data == parse_tax_data(tax_data_response)+++ @freeze_time()+ @mock.patch("saleor.checkout.calculations.fetch_checkout_data")+ @mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")+ @mock.patch(+ "saleor.webhook.transport.synchronous.transport.generate_payload_from_subscription"+ )+ def test_get_taxes_for_checkout_with_sync_subscription_with_pregenerated_payload(+ mock_generate_payload,+ mock_request,+ mock_fetch,+ webhook_plugin,+ tax_data_response,+ checkout,+ tax_app,+ ):+ # given+ subscription_query = "subscription{event{... on CalculateTaxes{taxBase{currency}}}}"+ subscription_query_hash = get_subscription_query_hash(subscription_query)+ expected_payload = {"taxBase": {"currency": "USD"}}+ checkout_info = fetch_checkout_info(+ checkout, [], get_plugins_manager(allow_replica=False)+ )+ mock_request.return_value = tax_data_response+ plugin = webhook_plugin()+ webhook = Webhook.objects.create(+ name="Tax checkout webhook",+ app=tax_app,+ target_url="https://localhost:8888/tax-order",+ subscription_query=subscription_query,+ )+ webhook.events.create(event_type=WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES)+ app_identifier = None+ pregenerated_subscription_payloads = {+ tax_app.id: {subscription_query_hash: expected_payload}+ }++ # when+ tax_data = plugin.get_taxes_for_checkout(+ checkout_info,+ [],+ app_identifier,+ None,+ pregenerated_subscription_payloads=pregenerated_subscription_payloads,+ )++ # then+ mock_generate_payload.assert_not_called()+ payload = EventPayload.objects.get()- assert payload.payload == json.dumps(expected_payload)++ assert payload.get_payload() == json.dumps(expected_payload)delivery = EventDelivery.objects.get()assert delivery.status == EventDeliveryStatus.PENDINGassert delivery.event_type == WebhookEventSyncType.CHECKOUT_CALCULATE_TAXESdiff --cc saleor/webhook/const.pyindex d4ba8bff9d,429f02b038..f711c77a53--- a/saleor/webhook/const.py+++ b/saleor/webhook/const.py@@@ -1,6 -1,3 +1,10 @@@-CACHE_EXCLUDED_SHIPPING_TIME = 60 * 3-WEBHOOK_CACHE_DEFAULT_TIMEOUT: int = 5 * 60 # 5 minutes+import os++# TODO: restore hardcoded values or move to settings- CACHE_EXCLUDED_SHIPPING_TIME = int(os.environ.get("WEBHOOK_CACHE_EXCLUDED_SHIPPING_TIME", 3 * 60))- WEBHOOK_CACHE_DEFAULT_TIMEOUT = int(os.environ.get("WEBHOOK_CACHE_DEFAULT_TIMEOUT", 5 * 60))++CACHE_EXCLUDED_SHIPPING_TIME = int(++ os.environ.get("WEBHOOK_CACHE_EXCLUDED_SHIPPING_TIME", 3 * 60)++)++WEBHOOK_CACHE_DEFAULT_TIMEOUT = int(++ os.environ.get("WEBHOOK_CACHE_DEFAULT_TIMEOUT", 5 * 60)++)APP_ID_PREFIX = "app"