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.

SD-1491 I can pragmatically disable Webhooks for a block code (#11482)

Aleksandar Petrushev0cc853dc9dd0
diff --git a/api/app/services/spree/webhooks.rb b/api/app/services/spree/webhooks.rb
new file mode 100644
index 0000000000..ad19c63b78
--- /dev/null
+++ b/api/app/services/spree/webhooks.rb
@@ -0,0 +1,13 @@
+module Spree
+ module Webhooks
+ def self.disable_webhooks
+ webhooks_disabled_previously = ENV['DISABLE_SPREE_WEBHOOKS']
+ begin
+ ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'
+ yield
+ ensure
+ ENV['DISABLE_SPREE_WEBHOOKS'] = webhooks_disabled_previously
+ end
+ end
+ end
+end
diff --git a/api/spec/services/spree/webhooks_spec.rb b/api/spec/services/spree/webhooks_spec.rb
new file mode 100644
index 0000000000..4623c78969
--- /dev/null
+++ b/api/spec/services/spree/webhooks_spec.rb
@@ -0,0 +1,64 @@
+require 'spec_helper'
+
+describe Spree::Webhooks do
+ describe '#disable_webhooks' do
+ let(:variant) { create(:variant) }
+ let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }
+ let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }
+
+ before do
+ allow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)
+ allow(queue_requests).to receive(:call).with(any_args)
+ end
+
+ after { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }
+
+ describe 'the value of DISABLE_SPREE_WEBHOOKS environment variable' do
+ before { ENV['DISABLE_SPREE_WEBHOOKS'] = 'some_value' }
+
+ describe 'when an error is not raised' do
+ it 'sets it to the original value' do
+ described_class.disable_webhooks { variant.discontinue! }
+ expect(ENV['DISABLE_SPREE_WEBHOOKS']).to eq('some_value')
+ end
+ end
+
+ describe 'when an error is raised' do
+ it 'sets it to the original value' do
+ begin
+ described_class.disable_webhooks { raise StandardError }
+ rescue StandardError
+ end
+ expect(ENV['DISABLE_SPREE_WEBHOOKS']).to eq('some_value')
+ end
+ end
+ end
+
+ describe 'when webhooks are already disabled' do
+ before { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }
+
+ it 'does not emit the event' do
+ described_class.disable_webhooks { variant.discontinue! }
+ expect(queue_requests).not_to have_received(:call).with(event: 'variant.discontinued', body: body)
+ end
+ end
+
+ describe 'when webhooks are enabled' do
+ before { ENV['DISABLE_SPREE_WEBHOOKS'] = nil }
+
+ describe 'when not using #disable_webhooks' do
+ it 'emits the event' do
+ variant.discontinue!
+ expect(queue_requests).to have_received(:call).with(event: 'variant.discontinued', body: body).once
+ end
+ end
+
+ describe 'when using #disable_webhooks' do
+ it 'does not emit the event' do
+ described_class.disable_webhooks { variant.discontinue! }
+ expect(queue_requests).not_to have_received(:call).with(event: 'variant.discontinued', body: body)
+ end
+ end
+ end
+ end
+end