Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Don't try to generate payload body when there aren't any subscribers … (#11610)

Damian Legawiecdadad988ff65
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index a97ecc715e..8f5e3c15fc 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -9,8 +9,10 @@ module Spree
after_update_commit(proc { queue_webhooks_requests!(inferred_event_name(:update)) })
def queue_webhooks_requests!(event_name)
- return if disable_spree_webhooks? || webhook_payload_body.blank?
+ return if disable_spree_webhooks?
+ return if Spree::Webhooks::Subscriber.active.with_urls_for(event_name).none?
return if update_event?(event_name) && updating_only_timestamps?
+ return if webhook_payload_body.blank?
Spree::Webhooks::Subscribers::QueueRequests.call(event_name: event_name, webhook_payload_body: webhook_payload_body)
end
diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
index fb6b1ecbe3..f35e9f70d6 100644
--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
@@ -13,6 +13,7 @@ describe Spree::Webhooks::HasWebhooks do
)
end
let(:store) { create(:store) }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: ['*']) }
context 'with DISABLE_SPREE_WEBHOOKS equals "true" (set in spec_helper)' do
let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }
@@ -125,6 +126,14 @@ describe Spree::Webhooks::HasWebhooks do
expect { cms_page.update(title: 'Homepage #1') }.not_to emit_webhook_event('store.update')
end
end
+
+ context 'when no subscribers are active' do
+ before { webhook_subscriber.update(active: false) }
+
+ it 'does not emit the event' do
+ expect { product.save }.not_to emit_webhook_event('product.create')
+ end
+ end
end
describe '.default_webhook_events' do
diff --git a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb b/api/spec/models/spree/api/webhooks/order_decorator_spec.rb
index 70a602d5a9..a15abc991a 100644
--- a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/order_decorator_spec.rb
@@ -7,6 +7,7 @@ describe Spree::Api::Webhooks::OrderDecorator do
describe 'order.canceled' do
describe 'completed -> canceled' do
let(:event_name) { 'order.canceled' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:order) { create(:completed_order_with_totals, store: store) }
it { expect { Timecop.freeze { order.cancel } }.to emit_webhook_event(event_name) }
@@ -16,6 +17,7 @@ describe Spree::Api::Webhooks::OrderDecorator do
describe 'order.placed' do
describe 'checkout -> completed' do
let(:event_name) { 'order.placed' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:order) { create(:order, email: '[email protected]', store: store) }
it { expect { order.finalize! }.to emit_webhook_event(event_name) }
@@ -24,6 +26,7 @@ describe Spree::Api::Webhooks::OrderDecorator do
describe 'order.resumed' do
let(:event_name) { 'order.resumed' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:order) { create(:order, store: store, state: :canceled) }
context 'when order state changes' do
diff --git a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
index c271ced7fd..4427bbe361 100644
--- a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
@@ -8,6 +8,7 @@ describe Spree::Api::Webhooks::PaymentDecorator do
describe 'payment.paid' do
let(:event_name) { 'payment.paid' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'processing -> completed' do
before { payment.started_processing }
@@ -28,6 +29,7 @@ describe Spree::Api::Webhooks::PaymentDecorator do
describe 'payment.voided' do
let(:event_name) { 'payment.voided' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'pending -> void' do
before { payment.pend }
@@ -59,6 +61,7 @@ describe Spree::Api::Webhooks::PaymentDecorator do
let(:order) { payment.order }
let(:event_name) { 'order.paid' }
let!(:another_payment) { create(:payment, order: order) }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'order.paid? == true' do
context 'processing -> complete' do
diff --git a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
index 249f73ce73..9b2f42a603 100644
--- a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
@@ -5,6 +5,7 @@ describe Spree::Api::Webhooks::ProductDecorator do
context 'emitting product.discontinued' do
let(:webhook_payload_body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:event_name) { 'product.discontinued' }
context 'when product discontinued_on changes' do
diff --git a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
index 356e81ffbc..ff60fa5bff 100644
--- a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
@@ -15,6 +15,7 @@ describe Spree::Api::Webhooks::ShipmentDecorator do
context 'emitting shipment.shipped' do
let(:webhook_payload_body) { Spree::Api::V2::Platform::ShipmentSerializer.new(shipment).serializable_hash }
let(:event_name) { 'shipment.shipped' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'ready -> ship' do
let(:shipment) { create(:shipment, order: order) }
@@ -38,6 +39,7 @@ describe Spree::Api::Webhooks::ShipmentDecorator do
context 'emitting order.shipped' do
let(:webhook_payload_body) { Spree::Api::V2::Platform::OrderSerializer.new(order.reload).serializable_hash }
let(:event_name) { 'order.shipped' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let!(:shipments) do
create_list(
:shipment, 2,
diff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
index 9fb66f862d..07607c0d6e 100644
--- a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
@@ -12,6 +12,7 @@ describe Spree::Api::Webhooks::StockItemDecorator do
let(:webhook_payload_body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash }
let(:product) { variant.product }
let(:event_name) { 'product.backorderable' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let!(:variant2) { create(:variant, product: product) }
before { Spree::StockItem.update_all(backorderable: false) }
@@ -65,6 +66,7 @@ describe Spree::Api::Webhooks::StockItemDecorator do
subject { stock_item.save }
let(:event_name) { 'variant.backorderable' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'when variant was out of stock' do
before do
diff --git a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
index 9eaa622bd3..1ede4b1acc 100644
--- a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
@@ -14,6 +14,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
describe 'emitting product.out_of_stock' do
let(:event_name) { 'product.out_of_stock' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
before { Spree::StockItem.update_all(backorderable: false) }
@@ -100,7 +101,8 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
end
describe 'emitting product.back_in_stock' do
- let(:params) { 'product.back_in_stock' }
+ let(:event_name) { 'product.back_in_stock' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
before { Spree::StockItem.update_all(backorderable: false) }
@@ -115,7 +117,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
product.reload
end
- it { expect { subject }.to emit_webhook_event(params) }
+ it { expect { subject }.to emit_webhook_event(event_name) }
end
context 'when none of the variants is back in stock' do
@@ -128,7 +130,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
product.reload
end
- it { expect { subject }.not_to emit_webhook_event(params) }
+ it { expect { subject }.not_to emit_webhook_event(event_name) }
end
end
@@ -149,14 +151,15 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
end
end
- it { expect { subject }.not_to emit_webhook_event(params) }
+ it { expect { subject }.not_to emit_webhook_event(event_name) }
end
end
end
describe 'emitting variant.back_in_stock' do
let(:variant) { create(:variant, track_inventory: true) }
- let(:params) { 'variant.back_in_stock' }
+ let(:event_name) { 'variant.back_in_stock' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'when stock item was out of stock' do
context 'when stock item changes to be in stock' do
@@ -168,7 +171,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
stock_movement.stock_item = stock_location.set_up_stock_item(variant)
stock_movement.save
end
- end.to emit_webhook_event(params)
+ end.to emit_webhook_event(event_name)
end
end
@@ -181,7 +184,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
stock_movement.stock_item = stock_location.set_up_stock_item(variant)
stock_movement.save
end
- end.not_to emit_webhook_event(params)
+ end.not_to emit_webhook_event(event_name)
end
end
end
@@ -196,7 +199,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
stock_movement.stock_item = stock_location.set_up_stock_item(variant)
stock_movement.save
end
- end.not_to emit_webhook_event(params)
+ end.not_to emit_webhook_event(event_name)
end
end
end
@@ -205,7 +208,8 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
subject { stock_movement }
let(:stock_movement) { create(:stock_movement, stock_item: stock_item, quantity: movement_quantity) }
- let(:params) { 'variant.out_of_stock' }
+ let(:event_name) { 'variant.out_of_stock' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let!(:variant) { stock_item.variant }
before { Spree::StockItem.update_all(backorderable: false) }
@@ -214,7 +218,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
let(:movement_quantity) { -stock_item.count_on_hand }
it 'emits the variant.out_of_stock event' do
- expect { subject }.to emit_webhook_event(params)
+ expect { subject }.to emit_webhook_event(event_name)
end
end
@@ -222,7 +226,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
let(:movement_quantity) { -stock_item.count_on_hand + 1 }
it 'does not emit the variant.out_of_stock event' do
- expect { subject }.not_to emit_webhook_event(params)
+ expect { subject }.not_to emit_webhook_event(event_name)
end
end
@@ -232,7 +236,7 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
let(:movement_quantity) { 0 }
it 'does not emit the variant.out_of_stock event' do
- expect { subject }.not_to emit_webhook_event(params)
+ expect { subject }.not_to emit_webhook_event(event_name)
end
end
end
diff --git a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
index b262d635c4..497f8368e2 100644
--- a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
@@ -6,6 +6,7 @@ describe Spree::Api::Webhooks::VariantDecorator do
context 'emitting variant.discontinued' do
let(:webhook_payload_body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash }
let(:event_name) { 'variant.discontinued' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'when variant discontinued_on changes' do
context 'when the new value is "present"' do
diff --git a/api/spec/services/spree/webhooks_spec.rb b/api/spec/services/spree/webhooks_spec.rb
index a6d8efdb12..7d5b428f31 100644
--- a/api/spec/services/spree/webhooks_spec.rb
+++ b/api/spec/services/spree/webhooks_spec.rb
@@ -4,6 +4,7 @@ describe Spree::Webhooks do
describe '#disable_webhooks' do
let(:webhook_payload_body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash }
let(:event_name) { 'variant.discontinued' }
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }
let(:variant) { create(:variant) }