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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
[SD-1514] Emit variant.discontinued when a variant was discontinued (#11459)
seb1763f8c8c472
diff --git a/api/app/models/spree/api/webhooks/variant_decorator.rb b/api/app/models/spree/api/webhooks/variant_decorator.rbnew file mode 100644index 0000000000..dfa8548a07--- /dev/null+++ b/api/app/models/spree/api/webhooks/variant_decorator.rb@@ -0,0 +1,14 @@+module Spree+ module Api+ module Webhooks+ module VariantDecorator+ def discontinue!+ super+ queue_webhooks_requests!('variant.discontinued')+ end+ end+ end+ end+end++Spree::Variant.prepend(Spree::Api::Webhooks::VariantDecorator)diff --git a/api/app/models/spree/webhooks/subscriber.rb b/api/app/models/spree/webhooks/subscriber.rbindex 1296b2766e..dd08c10aa0 100644--- a/api/app/models/spree/webhooks/subscriber.rb+++ b/api/app/models/spree/webhooks/subscriber.rb@@ -13,6 +13,8 @@ module Spreescope :active, -> { where(active: true) }scope :inactive, -> { where(active: false) }+ before_save :parse_subscriptions+def self.with_urls_for(event)where(case ActiveRecord::Base.connection.adapter_name@@ -35,6 +37,12 @@ module Spreeerrors.add(:url, 'the URL must have a path') if uri.blank? || uri.path.blank?end++ def parse_subscriptions+ return if subscriptions.blank? || subscriptions.is_a?(Array)++ self.subscriptions = JSON.parse(subscriptions)+ endendendenddiff --git a/api/lib/spree/api/testing_support/matchers/webhooks.rb b/api/lib/spree/api/testing_support/matchers/webhooks.rbnew file mode 100644index 0000000000..a418bee0e0--- /dev/null+++ b/api/lib/spree/api/testing_support/matchers/webhooks.rb@@ -0,0 +1,62 @@+# Passes if executing the code in the block there is a+# `Spree::Webhooks::Subscribers::QueueRequests.call` method+# call with the given `event` and `body` arguments just once.+#+# @example+# expect { order.complete }.to emit_webhook_event('order.paid')+# expect do+# order.start_processing+# order.complete+# end.to emit_webhook_event('order.paid')+#+# It can also be negated, resulting in the expectation+# waiting to not receive a `call` method call with the+# given `event` and `body` (`once` isn't taken into consideration).+#+# @example+# expect { order.complete }.not_to emit_webhook_event('order.paid')+# expect do+# order.start_processing+# order.complete+# end.not_to emit_webhook_event('order.paid')+#+# == Notes+#+# The matcher relies on a `body` method previously defined which+# isn't added to the matcher definition, because it acts in a different+# way depending on what's the resource being tested.+#+RSpec::Matchers.define :emit_webhook_event do |event_to_emit|+ match do |obj_method|+ ENV['DISABLE_SPREE_WEBHOOKS'] = nil++ queue_requests = instance_double(Spree::Webhooks::Subscribers::QueueRequests)++ allow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)+ allow(queue_requests).to receive(:call).with(any_args)++ obj_method.call+ expect(queue_requests).to have_received(:call).with(event: event_to_emit, body: body).once++ ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'+ end++ def block_body_definition(obj_method)+ # positive look-behinds must have a fixed length, using a straightforward match instead+ obj_method.source.squish[/(expect *({|do) *)(.*?)( *(}|end).(not_)*to)/, 3]+ end++ failure_message do |obj_method|+ block_body_def = block_body_definition(obj_method)+ "Expected that executing `#{block_body_def}` emits the `#{event_to_emit}` Webhook event.\n" \+ "Check that `#{block_body_def}` does implement `queue_webhooks_requests!` for " \+ "`#{event_to_emit}` with the following body: #{body}."+ end++ failure_message_when_negated do |obj_method|+ "Expected that executing `#{block_body_definition(obj_method)}` does not " \+ "emit the `#{event_to_emit}` Webhook event with the following body: #{body}."+ end++ supports_block_expectations+enddiff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rbindex 8232b2b448..a22a76fa69 100644--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb@@ -1,13 +1,6 @@require 'spec_helper'describe Spree::Webhooks::HasWebhooks do- 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' }-let(:images) { create_list(:image, 2) }let(:product) dobuild(@@ -17,69 +10,62 @@ describe Spree::Webhooks::HasWebhooks do)endlet(:store) { create(:store) }- let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }context 'with DISABLE_SPREE_WEBHOOKS equals "true" (set in spec_helper)' do- before { product.save }+ 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)+ product.save+ end++ shared_examples 'not queueing an event request' do |event_to_emit|+ it { expect(queue_requests).not_to have_received(:call).with(hash_including(event: event_to_emit)) }+ endcontext 'after_create_commit' do- it 'does not queue a request' do- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'product.create'))- end+ it_behaves_like 'not queueing an event request', 'product.create'endcontext 'after_destroy_commit' do- it 'does not queue a request' do- product.destroy- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'product.destroy'))- end+ before { product.destroy }++ it_behaves_like 'not queueing an event request', 'product.create'endcontext 'after_update_commit' do- it 'does not queue a request' do- product.update(name: 'updated')- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'product.update'))- end+ before { product.update(name: 'updated') }++ it_behaves_like 'not queueing an event request', 'product.create'endendcontext 'without DISABLE_SPREE_WEBHOOKS' dolet(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }- before do- ENV['DISABLE_SPREE_WEBHOOKS'] = nil- product.save- end-context 'after_create_commit' do- it 'queues a request through QueueRequests for product.create' do- expect(queue_requests).to have_received(:call).with(event: 'product.create', body: body).once- end+ it { expect { product.save }.to emit_webhook_event('product.create') }endcontext 'after_destroy_commit' do- it 'queues a request through QueueRequests for product.destroy' do- product.destroy- expect(queue_requests).to have_received(:call).with(event: 'product.destroy', body: body).once- end+ before { product.save }++ it { expect { product.destroy }.to emit_webhook_event('product.destroy') }endcontext 'after_update_commit' do- it 'queues a request through QueueRequests for product.update' do- product.update(name: 'updated')- expect(queue_requests).to have_received(:call).with(event: 'product.update', body: body).once- end+ before { product.save }++ it { expect { product.update(name: 'updated') }.to emit_webhook_event('product.update') }endcontext 'with a class name with multiple words' do- let!(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }- let(:body) do- Spree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash.to_json- end- let(:underscore_event) { 'cms_page.create' }+ let(:body) { Spree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash.to_json }+ let(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }it 'underscorize the event name' do- expect(queue_requests).to have_received(:call).with(event: underscore_event, body: body).once+ expect { cms_page }.to emit_webhook_event('cms_page.create')endendenddiff --git a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb b/api/spec/models/spree/api/webhooks/order_decorator_spec.rbindex 0d270f3482..3c9a4f0293 100644--- a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb+++ b/api/spec/models/spree/api/webhooks/order_decorator_spec.rb@@ -1,39 +1,24 @@require 'spec_helper'describe Spree::Order do- describe 'sending webhooks after transitioning from states' do- let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }- let(:store) { create(:store, default: true) }- let(:body) do- Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_json- end-- before do- ENV['DISABLE_SPREE_WEBHOOKS'] = nil- 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' }+ let(:store) { create(:store, default: true) }+ let(:body) do+ Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_json+ end- describe '#after_cancel' do+ describe 'order.canceled' do+ describe 'completed -> canceled' dolet(:order) { create(:completed_order_with_totals, store: store) }- it 'executes QueueRequests.call with a order.cancel event and {} body after invoking cancel' do- Timecop.freeze(Time.local(1990)) do- order.cancel- expect(queue_requests).to have_received(:call).with(event: 'order.canceled', body: body).once- end- end+ it { expect { Timecop.freeze { order.cancel } }.to emit_webhook_event('order.canceled') }end+ end- describe '#finalize!' do+ describe 'order.placed' do+ describe 'checkout -> completed' dolet(:order) { described_class.create(email: '[email protected]', store: store) }- it 'executes QueueRequests.call with a order.placed event and {} body after invoking finalize!' do- order.finalize!- expect(queue_requests).to have_received(:call).with(event: 'order.placed', body: body).once- end+ it { expect { Timecop.freeze { order.finalize! } }.to emit_webhook_event('order.placed') }endendenddiff --git a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rbindex 638ce58520..105fccdbf0 100644--- a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb+++ b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb@@ -3,78 +3,54 @@ require 'spec_helper'describe Spree::Payment dolet(:body) { Spree::Api::V2::Platform::PaymentSerializer.new(payment).serializable_hash.to_json }let(:payment) { create(:payment) }- let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }- before do- ENV['DISABLE_SPREE_WEBHOOKS'] = nil- allow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)- allow(queue_requests).to receive(:call).with(any_args)- allow(payment).to receive_message_chain(:state_changes, :create!)- end-- after { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }-- shared_examples 'emitting the event' do |event|- it do- expect(queue_requests).to have_received(:call).with(event: event, body: body).once- end- end+ before { allow(payment).to receive_message_chain(:state_changes, :create!) }describe 'payment.paid' do- shared_examples 'emits payment.paid' do- before { payment.complete }-- it_behaves_like 'emitting the event', 'payment.paid'- end-context 'processing -> completed' dobefore { payment.started_processing }- include_examples 'emits payment.paid'+ it { expect { payment.complete }.to emit_webhook_event('payment.paid') }endcontext 'pending -> completed' dobefore { payment.pend }- include_examples 'emits payment.paid'+ it { expect { payment.complete }.to emit_webhook_event('payment.paid') }endcontext 'checkout -> completed' do- include_examples 'emits payment.paid'+ it { expect { payment.complete }.to emit_webhook_event('payment.paid') }endenddescribe 'payment.voided' do- shared_examples 'emits payment.voided' do- before { payment.void }-- it_behaves_like 'emitting the event', 'payment.voided'- end-context 'pending -> void' dobefore { payment.pend }- include_examples 'emits payment.voided'+ it { expect { payment.void }.to emit_webhook_event('payment.voided') }endcontext 'processing -> void' dobefore { payment.started_processing }- include_examples 'emits payment.voided'+ it { expect { payment.void }.to emit_webhook_event('payment.voided') }endcontext 'completed -> void' dobefore { payment.complete }- include_examples 'emits payment.voided'+ it { expect { payment.void }.to emit_webhook_event('payment.voided') }endcontext 'checkout -> void' do- include_examples 'emits payment.voided'+ it { expect { payment.void }.to emit_webhook_event('payment.voided') }endenddescribe 'order.paid' do+ subject { Timecop.freeze { another_payment.complete } }+let(:body) { Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_json }let(:order) { payment.order }let!(:another_payment) { create(:payment, order: order) }@@ -82,79 +58,51 @@ describe Spree::Payment docontext 'order.paid? == true' docontext 'processing -> complete' dobefore do- # order.updated_at doesn't coincide without timecop freezing- Timecop.freeze do- payment.started_processing- payment.complete- another_payment.started_processing- another_payment.complete- end+ payment.started_processing+ payment.complete+ another_payment.started_processingend- it_behaves_like 'emitting the event', 'order.paid'+ it { expect { subject }.to emit_webhook_event('order.paid') }endcontext 'pending -> complete' dobefore do- Timecop.freeze do- payment.pend- payment.complete- another_payment.pend- another_payment.complete- end+ payment.pend+ payment.complete+ another_payment.pendend- it_behaves_like 'emitting the event', 'order.paid'+ it { expect { subject }.to emit_webhook_event('order.paid') }endcontext 'checkout -> complete' do- before do- Timecop.freeze do- payment.complete- another_payment.complete- end- end+ before { payment.complete }- it_behaves_like 'emitting the event', 'order.paid'+ it { expect { subject }.to emit_webhook_event('order.paid') }endendcontext 'order.paid? == false' do- shared_examples 'does not emit order.paid' do- it { expect(queue_requests).not_to have_received(:call).with(event: 'order.paid', body: body) }- end-before doallow(payment).to receive(:order).and_return(order)allow(order).to receive(:paid?).and_return(false)endcontext 'processing -> complete' do- before do- Timecop.freeze do- payment.started_processing- payment.complete- end- end+ before { payment.started_processing }- include_examples 'does not emit order.paid'+ it { expect { subject }.not_to emit_webhook_event('order.paid') }endcontext 'pending -> complete' do- before do- Timecop.freeze do- payment.pend- payment.complete- end- end+ before { payment.pend }- include_examples 'does not emit order.paid'+ it { expect { subject }.not_to emit_webhook_event('order.paid') }endcontext 'checkout -> complete' do- before { Timecop.freeze { payment.complete } }-- include_examples 'does not emit order.paid'+ it { expect { subject }.not_to emit_webhook_event('order.paid') }endendenddiff --git a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rbindex ede417fd5f..6c0b7d2b41 100644--- a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb+++ b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb@@ -6,28 +6,15 @@ describe Spree::Shipment dodescribe '#after_ship' dobefore do- ENV['DISABLE_SPREE_WEBHOOKS'] = nil# Avoid creating a new state change after transitioning as is defined in the model# because after_ship queues the HTTP request before finishing the transition, hence# the total state changes that are sent in the body is one less.allow(shipment).to receive_message_chain(:state_changes, :create!)- 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' }-- let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }-context 'emitting shipment.shipped' dolet(:body) { Spree::Api::V2::Platform::ShipmentSerializer.new(shipment).serializable_hash.to_json }- shared_examples 'emits shipment.shipped' do- it do- expect(queue_requests).to have_received(:call).with(event: 'shipment.shipped', body: body).once- end- end-context 'ready -> ship' dolet(:shipment) { create(:shipment, order: order) }@@ -35,19 +22,15 @@ describe Spree::Shipment doorder.update(state: 'complete', completed_at: Time.current)shipment.reloadshipment.ready- shipment.shipend- include_examples 'emits shipment.shipped'+ it { expect { shipment.ship }.to emit_webhook_event('shipment.shipped') }endcontext 'canceled -> ship' do- before do- shipment.cancel- shipment.ship- end+ before { shipment.cancel }- include_examples 'emits shipment.shipped'+ it { expect { shipment.ship }.to emit_webhook_event('shipment.shipped') }endend@@ -65,49 +48,41 @@ describe Spree::Shipment do)end- shared_examples 'does not emit order.shipped' do- it do- expect(queue_requests).not_to have_received(:call).with(event: 'order.shipped', body: body)- end- end-context 'ready -> ship' dobefore doorder.update(state: 'complete', completed_at: Time.current)shipments.each(&:reload) # must reload to make shipments order state see it's completeshipments[0].ready- shipments[0].shipendcontext 'with all order shipments shipped' do- it 'emits order.shipped' do+ before do+ shipments[0].shipshipments[1].ready- shipments[1].ship- expect(queue_requests).to have_received(:call).with(event: 'order.shipped', body: body).onceend++ it { expect { shipments[1].ship }.to emit_webhook_event('order.shipped') }endcontext 'without all order shipments shipped' do- include_examples 'does not emit order.shipped'+ it { expect { shipments[0].ship }.not_to emit_webhook_event('order.shipped') }endendcontext 'canceled -> ship' do- before do- shipments[0].cancel- shipments[0].ship- end+ before { shipments[0].cancel }context 'with all order shipments shipped' do- it 'emits order.shipped' do+ before do+ shipments[0].shipshipments[1].cancel- shipments[1].ship- expect(queue_requests).to have_received(:call).with(event: 'order.shipped', body: body).onceend++ it { expect { shipments[1].ship }.to emit_webhook_event('order.shipped') }endcontext 'without all order shipments shipped' do- include_examples 'does not emit order.shipped'+ it { expect { shipments[0].ship }.not_to emit_webhook_event('order.shipped') }endendenddiff --git a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rbnew file mode 100644index 0000000000..e17ba0ce04--- /dev/null+++ b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb@@ -0,0 +1,15 @@+require 'spec_helper'++describe Spree::Variant do+ let(:variant) { create(:variant) }++ describe '#discontinue!' do+ context 'emitting variant.discontinued' do+ subject { variant.discontinue! }++ let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash.to_json }++ it { expect { subject }.to emit_webhook_event('variant.discontinued') }+ end+ end+enddiff --git a/api/spec/models/spree/webhooks/subscriber_spec.rb b/api/spec/models/spree/webhooks/subscriber_spec.rbindex 3f234abbc6..405fcff163 100644--- a/api/spec/models/spree/webhooks/subscriber_spec.rb+++ b/api/spec/models/spree/webhooks/subscriber_spec.rb@@ -1,24 +1,42 @@require 'spec_helper'describe Spree::Webhooks::Subscriber do+ describe 'before_save' do+ context 'with subscriptions' do+ let(:subscriptions) { ['order.placed', 'order.shipped'] }++ it 'json parses the subscriptions' do+ subscriber = create(:subscriber, subscriptions: subscriptions.to_json)+ expect(subscriber.subscriptions).to eq(subscriptions)+ end+ end++ context 'without subscriptions' do+ it 'json parses the subscriptions' do+ subscriber = create(:subscriber, subscriptions: nil)+ expect(subscriber.subscriptions).to eq(nil)+ end+ end+ end+describe 'validations' docontext 'url format (UrlValidator)' doit 'is invalid with an invalid url' do- endpoint = described_class.new(url: 'google.com')- expect(endpoint.valid?).to be(false)+ subscriber = described_class.new(url: 'google.com')+ expect(subscriber.valid?).to be(false)endit 'is valid with a valid url' do- endpoint = described_class.new(url: 'http://google.com/')- expect(endpoint.valid?).to be(true)+ subscriber = described_class.new(url: 'http://google.com/')+ expect(subscriber.valid?).to be(true)endendcontext 'url path' doit 'is invalid a url without path' do- endpoint = described_class.new(url: 'http://google.com')- expect(endpoint.valid?).to be(false)- expect(endpoint.errors.messages).to eq(url: ['the URL must have a path'])+ subscriber = described_class.new(url: 'http://google.com')+ expect(subscriber.valid?).to be(false)+ expect(subscriber.errors.messages).to eq(url: ['the URL must have a path'])endendenddiff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rbindex afe3552366..f780795796 100644--- a/api/spec/spec_helper.rb+++ b/api/spec/spec_helper.rb@@ -45,6 +45,7 @@ require 'spree/api/testing_support/jobs'require 'spree/api/testing_support/helpers'require 'spree/api/testing_support/setup'require 'spree/api/testing_support/spree_webhooks'+require 'spree/api/testing_support/matchers/webhooks'require 'spree/api/testing_support/v2/base'require 'spree/api/testing_support/v2/current_order'require 'spree/api/testing_support/v2/platform_contexts'